/* parse a krb5 GSS-API wrapper packet giving a ticket */ BOOL spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2]) { BOOL ret; ASN1_DATA data; int data_remaining; asn1_load(&data, blob); asn1_start_tag(&data, ASN1_APPLICATION(0)); asn1_check_OID(&data, OID_KERBEROS5); data_remaining = asn1_tag_remaining(&data); if (data_remaining < 3) { data.has_error = True; } else { asn1_read(&data, tok_id, 2); data_remaining -= 2; *ticket = data_blob(NULL, data_remaining); asn1_read(&data, ticket->data, ticket->length); } asn1_end_tag(&data); ret = !data.has_error; asn1_free(&data); return ret; }
/* parse a krb5 GSS-API wrapper packet giving a ticket */ static bool gensec_gssapi_parse_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, DATA_BLOB *ticket, uint8_t tok_id[2]) { bool ret; struct asn1_data *data = asn1_init(mem_ctx); int data_remaining; if (!data) { return false; } asn1_load(data, *blob); asn1_start_tag(data, ASN1_APPLICATION(0)); asn1_check_OID(data, GENSEC_OID_KERBEROS5); data_remaining = asn1_tag_remaining(data); if (data_remaining < 3) { data->has_error = true; } else { asn1_read(data, tok_id, 2); data_remaining -= 2; *ticket = data_blob_talloc(mem_ctx, NULL, data_remaining); asn1_read(data, ticket->data, ticket->length); } asn1_end_tag(data); ret = !data->has_error; asn1_free(data); return ret; }
ssize_t read_spnego_data(DATA_BLOB data, SPNEGO_DATA *token) { ASN1_DATA asn1; ssize_t ret = -1; ZERO_STRUCTP(token); ZERO_STRUCT(asn1); asn1_load(&asn1, data); switch (asn1.data[asn1.ofs]) { case ASN1_APPLICATION(0): asn1_start_tag(&asn1, ASN1_APPLICATION(0)); asn1_check_OID(&asn1, OID_SPNEGO); if (read_negTokenInit(&asn1, &token->negTokenInit)) { token->type = SPNEGO_NEG_TOKEN_INIT; } asn1_end_tag(&asn1); break; case ASN1_CONTEXT(1): if (read_negTokenTarg(&asn1, &token->negTokenTarg)) { token->type = SPNEGO_NEG_TOKEN_TARG; } break; default: break; } if (!asn1.has_error) ret = asn1.ofs; asn1_free(&asn1); return ret; }
/* decode/process data */ static NTSTATUS ldapsrv_decode(void *private_data, DATA_BLOB blob) { NTSTATUS status; struct ldapsrv_connection *conn = talloc_get_type(private_data, struct ldapsrv_connection); struct asn1_data *asn1 = asn1_init(conn); struct ldap_message *msg = talloc(conn, struct ldap_message); if (asn1 == NULL || msg == NULL) { return NT_STATUS_NO_MEMORY; } if (!asn1_load(asn1, blob)) { talloc_free(msg); talloc_free(asn1); return NT_STATUS_NO_MEMORY; } status = ldap_decode(asn1, samba_ldap_control_handlers(), msg); if (!NT_STATUS_IS_OK(status)) { asn1_free(asn1); return status; } data_blob_free(&blob); talloc_steal(conn, msg); asn1_free(asn1); ldapsrv_process_message(conn, msg); return NT_STATUS_OK; }
/* parse a spnego NTLMSSP challenge packet giving two security blobs */ bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob, DATA_BLOB *chal1, DATA_BLOB *chal2) { bool ret = false; ASN1_DATA *data; ZERO_STRUCTP(chal1); ZERO_STRUCTP(chal2); data = asn1_init(talloc_tos()); if (data == NULL) { return false; } if (!asn1_load(data, blob)) goto err; if (!asn1_start_tag(data,ASN1_CONTEXT(1))) goto err; if (!asn1_start_tag(data,ASN1_SEQUENCE(0))) goto err; if (!asn1_start_tag(data,ASN1_CONTEXT(0))) goto err; if (!asn1_check_enumerated(data,1)) goto err; if (!asn1_end_tag(data)) goto err; if (!asn1_start_tag(data,ASN1_CONTEXT(1))) goto err; if (!asn1_check_OID(data, OID_NTLMSSP)) goto err; if (!asn1_end_tag(data)) goto err; if (!asn1_start_tag(data,ASN1_CONTEXT(2))) goto err; if (!asn1_read_OctetString(data, ctx, chal1)) goto err; if (!asn1_end_tag(data)) goto err; /* the second challenge is optional (XP doesn't send it) */ if (asn1_tag_remaining(data)) { if (!asn1_start_tag(data,ASN1_CONTEXT(3))) goto err; if (!asn1_read_OctetString(data, ctx, chal2)) goto err; if (!asn1_end_tag(data)) goto err; } if (!asn1_end_tag(data)) goto err; if (!asn1_end_tag(data)) goto err; ret = !asn1_has_error(data); err: if (asn1_has_error(data)) { data_blob_free(chal1); data_blob_free(chal2); } asn1_free(data); return ret; }
ssize_t spnego_read_data(TALLOC_CTX *mem_ctx, DATA_BLOB data, struct spnego_data *token) { struct asn1_data *asn1; ssize_t ret = -1; uint8_t context; ZERO_STRUCTP(token); if (data.length == 0) { return ret; } asn1 = asn1_init(mem_ctx); if (asn1 == NULL) { return -1; } asn1_load(asn1, data); if (!asn1_peek_uint8(asn1, &context)) { asn1->has_error = true; } else { switch (context) { case ASN1_APPLICATION(0): asn1_start_tag(asn1, ASN1_APPLICATION(0)); asn1_check_OID(asn1, GENSEC_OID_SPNEGO); if (read_negTokenInit(asn1, mem_ctx, &token->negTokenInit)) { token->type = SPNEGO_NEG_TOKEN_INIT; } asn1_end_tag(asn1); break; case ASN1_CONTEXT(1): if (read_negTokenTarg(asn1, mem_ctx, &token->negTokenTarg)) { token->type = SPNEGO_NEG_TOKEN_TARG; } break; default: asn1->has_error = true; break; } } if (!asn1->has_error) ret = asn1->ofs; asn1_free(asn1); return ret; }
/* magic check a GSS-API wrapper packet for an Kerberos OID */ static bool gensec_gssapi_check_oid(const DATA_BLOB *blob, const char *oid) { bool ret; struct asn1_data *data = asn1_init(NULL); if (!data) return false; asn1_load(data, *blob); asn1_start_tag(data, ASN1_APPLICATION(0)); asn1_check_OID(data, oid); ret = !data->has_error; asn1_free(data); return ret; }
/* parse a SPNEGO NTLMSSP auth packet. This contains the encrypted passwords */ BOOL spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status, DATA_BLOB *auth) { ASN1_DATA data; uint8 negResult; if (NT_STATUS_IS_OK(nt_status)) { negResult = SPNEGO_NEG_RESULT_ACCEPT; } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) { negResult = SPNEGO_NEG_RESULT_INCOMPLETE; } else { negResult = SPNEGO_NEG_RESULT_REJECT; } asn1_load(&data, blob); asn1_start_tag(&data, ASN1_CONTEXT(1)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_CONTEXT(0)); asn1_check_enumerated(&data, negResult); asn1_end_tag(&data); if (negResult == SPNEGO_NEG_RESULT_INCOMPLETE) { asn1_start_tag(&data,ASN1_CONTEXT(1)); asn1_check_OID(&data, OID_NTLMSSP); asn1_end_tag(&data); asn1_start_tag(&data,ASN1_CONTEXT(2)); asn1_read_OctetString(&data, auth); asn1_end_tag(&data); } asn1_end_tag(&data); asn1_end_tag(&data); if (data.has_error) { DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data.ofs)); asn1_free(&data); data_blob_free(auth); return False; } asn1_free(&data); return True; }
/* parse a negTokenInit packet giving a GUID, a list of supported OIDs (the mechanisms) and a principal name string */ BOOL spnego_parse_negTokenInit(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], char **principal) { int i; BOOL ret; ASN1_DATA data; asn1_load(&data, blob); asn1_start_tag(&data,ASN1_APPLICATION(0)); asn1_check_OID(&data,OID_SPNEGO); asn1_start_tag(&data,ASN1_CONTEXT(0)); asn1_start_tag(&data,ASN1_SEQUENCE(0)); asn1_start_tag(&data,ASN1_CONTEXT(0)); asn1_start_tag(&data,ASN1_SEQUENCE(0)); for (i=0; asn1_tag_remaining(&data) > 0 && i < ASN1_MAX_OIDS; i++) { char *oid_str = NULL; asn1_read_OID(&data,&oid_str); OIDs[i] = oid_str; } OIDs[i] = NULL; asn1_end_tag(&data); asn1_end_tag(&data); asn1_start_tag(&data, ASN1_CONTEXT(3)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_CONTEXT(0)); asn1_read_GeneralString(&data,principal); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); ret = !data.has_error; asn1_free(&data); return ret; }
/* parse a negTokenTarg packet giving a list of OIDs and a security blob */ BOOL parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob) { int i; ASN1_DATA data; asn1_load(&data, blob); asn1_start_tag(&data, ASN1_APPLICATION(0)); asn1_check_OID(&data,OID_SPNEGO); asn1_start_tag(&data, ASN1_CONTEXT(0)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_CONTEXT(0)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); for (i=0; asn1_tag_remaining(&data) > 0 && i < ASN1_MAX_OIDS; i++) { char *oid_str = NULL; asn1_read_OID(&data,&oid_str); OIDs[i] = oid_str; } OIDs[i] = NULL; asn1_end_tag(&data); asn1_end_tag(&data); asn1_start_tag(&data, ASN1_CONTEXT(2)); asn1_read_OctetString(&data,secblob); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); if (data.has_error) { DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data.ofs)); asn1_free(&data); return False; } asn1_free(&data); return True; }
/* parse a spnego NTLMSSP challenge packet giving two security blobs */ BOOL spnego_parse_challenge(const DATA_BLOB blob, DATA_BLOB *chal1, DATA_BLOB *chal2) { BOOL ret; ASN1_DATA data; ZERO_STRUCTP(chal1); ZERO_STRUCTP(chal2); asn1_load(&data, blob); asn1_start_tag(&data,ASN1_CONTEXT(1)); asn1_start_tag(&data,ASN1_SEQUENCE(0)); asn1_start_tag(&data,ASN1_CONTEXT(0)); asn1_check_enumerated(&data,1); asn1_end_tag(&data); asn1_start_tag(&data,ASN1_CONTEXT(1)); asn1_check_OID(&data, OID_NTLMSSP); asn1_end_tag(&data); asn1_start_tag(&data,ASN1_CONTEXT(2)); asn1_read_OctetString(&data, chal1); asn1_end_tag(&data); /* the second challenge is optional (XP doesn't send it) */ if (asn1_tag_remaining(&data)) { asn1_start_tag(&data,ASN1_CONTEXT(3)); asn1_read_OctetString(&data, chal2); asn1_end_tag(&data); } asn1_end_tag(&data); asn1_end_tag(&data); ret = !data.has_error; asn1_free(&data); return ret; }
/* parse a SPNEGO auth packet. This contains the encrypted passwords */ BOOL spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth) { ASN1_DATA data; asn1_load(&data, blob); asn1_start_tag(&data, ASN1_CONTEXT(1)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_CONTEXT(2)); asn1_read_OctetString(&data,auth); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); if (data.has_error) { DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data.ofs)); asn1_free(&data); return False; } asn1_free(&data); return True; }
/* parse a krb5 GSS-API wrapper packet giving a ticket */ bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2]) { bool ret; ASN1_DATA *data; int data_remaining; data = asn1_init(talloc_tos()); if (data == NULL) { return false; } asn1_load(data, blob); asn1_start_tag(data, ASN1_APPLICATION(0)); asn1_check_OID(data, OID_KERBEROS5); data_remaining = asn1_tag_remaining(data); if (data_remaining < 3) { data->has_error = True; } else { asn1_read(data, tok_id, 2); data_remaining -= 2; *ticket = data_blob_talloc(ctx, NULL, data_remaining); asn1_read(data, ticket->data, ticket->length); } asn1_end_tag(data); ret = !data->has_error; if (data->has_error) { data_blob_free(ticket); } asn1_free(data); return ret; }
BOOL unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data) { DATA_BLOB pac_contents; ASN1_DATA data; int data_type; if (!auth_data->length) { return False; } asn1_load(&data, *auth_data); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_CONTEXT(0)); asn1_read_Integer(&data, &data_type); if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) { DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type)); asn1_free(&data); return False; } asn1_end_tag(&data); asn1_start_tag(&data, ASN1_CONTEXT(1)); asn1_read_OctetString(&data, &pac_contents); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_free(&data); *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length); data_blob_free(&pac_contents); return True; }
static NTSTATUS check_spnego_blob_complete(uint16 smbpid, uint16 vuid, DATA_BLOB *pblob) { struct pending_auth_data *pad = NULL; ASN1_DATA data; size_t needed_len = 0; pad = get_pending_auth_data(smbpid); /* Ensure we have some data. */ if (pblob->length == 0) { /* Caller can cope. */ DEBUG(2,("check_spnego_blob_complete: zero blob length !\n")); delete_partial_auth(pad); return NT_STATUS_OK; } /* Were we waiting for more data ? */ if (pad) { DATA_BLOB tmp_blob; size_t copy_len = MIN(65536, pblob->length); /* Integer wrap paranoia.... */ if (pad->partial_data.length + copy_len < pad->partial_data.length || pad->partial_data.length + copy_len < copy_len) { DEBUG(2,("check_spnego_blob_complete: integer wrap " "pad->partial_data.length = %u, " "copy_len = %u\n", (unsigned int)pad->partial_data.length, (unsigned int)copy_len )); delete_partial_auth(pad); return NT_STATUS_INVALID_PARAMETER; } DEBUG(10,("check_spnego_blob_complete: " "pad->partial_data.length = %u, " "pad->needed_len = %u, " "copy_len = %u, " "pblob->length = %u,\n", (unsigned int)pad->partial_data.length, (unsigned int)pad->needed_len, (unsigned int)copy_len, (unsigned int)pblob->length )); tmp_blob = data_blob(NULL, pad->partial_data.length + copy_len); /* Concatenate the two (up to copy_len) bytes. */ memcpy(tmp_blob.data, pad->partial_data.data, pad->partial_data.length); memcpy(tmp_blob.data + pad->partial_data.length, pblob->data, copy_len); /* Replace the partial data. */ data_blob_free(&pad->partial_data); pad->partial_data = tmp_blob; ZERO_STRUCT(tmp_blob); /* Are we done ? */ if (pblob->length >= pad->needed_len) { /* Yes, replace pblob. */ data_blob_free(pblob); *pblob = pad->partial_data; ZERO_STRUCT(pad->partial_data); delete_partial_auth(pad); return NT_STATUS_OK; } /* Still need more data. */ pad->needed_len -= copy_len; return NT_STATUS_MORE_PROCESSING_REQUIRED; } if ((pblob->data[0] != ASN1_APPLICATION(0)) && (pblob->data[0] != ASN1_CONTEXT(1))) { /* Not something we can determine the * length of. */ return NT_STATUS_OK; } /* This is a new SPNEGO sessionsetup - see if * the data given in this blob is enough. */ asn1_load(&data, *pblob); asn1_start_tag(&data, pblob->data[0]); if (data.has_error || data.nesting == NULL) { asn1_free(&data); /* Let caller catch. */ return NT_STATUS_OK; } /* Integer wrap paranoia.... */ if (data.nesting->taglen + data.nesting->start < data.nesting->taglen || data.nesting->taglen + data.nesting->start < data.nesting->start) { DEBUG(2,("check_spnego_blob_complete: integer wrap " "data.nesting->taglen = %u, " "data.nesting->start = %u\n", (unsigned int)data.nesting->taglen, (unsigned int)data.nesting->start )); asn1_free(&data); return NT_STATUS_INVALID_PARAMETER; } /* Total length of the needed asn1 is the tag length * plus the current offset. */ needed_len = data.nesting->taglen + data.nesting->start; asn1_free(&data); DEBUG(10,("check_spnego_blob_complete: needed_len = %u, " "pblob->length = %u\n", (unsigned int)needed_len, (unsigned int)pblob->length )); if (needed_len <= pblob->length) { /* Nothing to do - blob is complete. */ return NT_STATUS_OK; } /* Refuse the blob if it's bigger than 64k. */ if (needed_len > 65536) { DEBUG(2,("check_spnego_blob_complete: needed_len too large (%u)\n", (unsigned int)needed_len )); return NT_STATUS_INVALID_PARAMETER; } /* We must store this blob until complete. */ pad = SMB_MALLOC(sizeof(struct pending_auth_data)); if (!pad) { return NT_STATUS_NO_MEMORY; } pad->needed_len = needed_len - pblob->length; pad->partial_data = data_blob(pblob->data, pblob->length); if (pad->partial_data.data == NULL) { SAFE_FREE(pad); return NT_STATUS_NO_MEMORY; } pad->smbpid = smbpid; pad->vuid = vuid; DLIST_ADD(pd_list, pad); return NT_STATUS_MORE_PROCESSING_REQUIRED; }
/* receive a cldap netlogon reply */ static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx, int sock, uint32_t *nt_version, union nbt_cldap_netlogon **reply) { int ret; ASN1_DATA data; DATA_BLOB blob = data_blob_null; DATA_BLOB os1 = data_blob_null; DATA_BLOB os2 = data_blob_null; DATA_BLOB os3 = data_blob_null; int i1; /* half the time of a regular ldap timeout, not less than 3 seconds. */ unsigned int al_secs = MAX(3,lp_ldap_timeout()/2); union nbt_cldap_netlogon *r = NULL; blob = data_blob(NULL, 8192); if (blob.data == NULL) { DEBUG(1, ("data_blob failed\n")); errno = ENOMEM; return -1; } /* Setup timeout */ gotalarm = 0; CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); alarm(al_secs); /* End setup timeout. */ ret = read(sock, blob.data, blob.length); /* Teardown timeout. */ CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN); alarm(0); if (ret <= 0) { DEBUG(1,("no reply received to cldap netlogon\n")); data_blob_free(&blob); return -1; } blob.length = ret; asn1_load(&data, blob); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_read_Integer(&data, &i1); asn1_start_tag(&data, ASN1_APPLICATION(4)); asn1_read_OctetString(&data, &os1); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_read_OctetString(&data, &os2); asn1_start_tag(&data, ASN1_SET); asn1_read_OctetString(&data, &os3); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); if (data.has_error) { data_blob_free(&blob); data_blob_free(&os1); data_blob_free(&os2); data_blob_free(&os3); asn1_free(&data); DEBUG(1,("Failed to parse cldap reply\n")); return -1; } r = TALLOC_ZERO_P(mem_ctx, union nbt_cldap_netlogon); if (!r) { errno = ENOMEM; data_blob_free(&os1); data_blob_free(&os2); data_blob_free(&os3); data_blob_free(&blob); return -1; } if (!pull_mailslot_cldap_reply(mem_ctx, &os3, r, nt_version)) { data_blob_free(&os1); data_blob_free(&os2); data_blob_free(&os3); data_blob_free(&blob); TALLOC_FREE(r); return -1; } data_blob_free(&os1); data_blob_free(&os2); data_blob_free(&os3); data_blob_free(&blob); asn1_free(&data); if (reply) { *reply = r; } else { TALLOC_FREE(r); } return 0; }
/* parse a negTokenInit packet giving a GUID, a list of supported OIDs (the mechanisms) and a principal name string */ bool spnego_parse_negTokenInit(TALLOC_CTX *ctx, DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], char **principal, DATA_BLOB *secblob) { int i; bool ret; ASN1_DATA *data; data = asn1_init(talloc_tos()); if (data == NULL) { return false; } asn1_load(data, blob); asn1_start_tag(data,ASN1_APPLICATION(0)); asn1_check_OID(data,OID_SPNEGO); /* negTokenInit [0] NegTokenInit */ asn1_start_tag(data,ASN1_CONTEXT(0)); asn1_start_tag(data,ASN1_SEQUENCE(0)); /* mechTypes [0] MechTypeList OPTIONAL */ /* Not really optional, we depend on this to decide * what mechanisms we have to work with. */ asn1_start_tag(data,ASN1_CONTEXT(0)); asn1_start_tag(data,ASN1_SEQUENCE(0)); for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) { asn1_read_OID(data,ctx, &OIDs[i]); if (data->has_error) { break; } } OIDs[i] = NULL; asn1_end_tag(data); asn1_end_tag(data); if (principal) { *principal = NULL; } if (secblob) { *secblob = data_blob_null; } /* Win7 + Live Sign-in Assistant attaches a mechToken ASN1_CONTEXT(2) to the negTokenInit packet which breaks our negotiation if we just assume the next tag is ASN1_CONTEXT(3). */ if (asn1_peek_tag(data, ASN1_CONTEXT(1))) { uint8 flags; /* reqFlags [1] ContextFlags OPTIONAL */ asn1_start_tag(data, ASN1_CONTEXT(1)); asn1_start_tag(data, ASN1_BIT_STRING); while (asn1_tag_remaining(data) > 0) { asn1_read_uint8(data, &flags); } asn1_end_tag(data); asn1_end_tag(data); } if (asn1_peek_tag(data, ASN1_CONTEXT(2))) { DATA_BLOB sblob = data_blob_null; /* mechToken [2] OCTET STRING OPTIONAL */ asn1_start_tag(data, ASN1_CONTEXT(2)); asn1_read_OctetString(data, ctx, &sblob); asn1_end_tag(data); if (secblob) { *secblob = sblob; } else { data_blob_free(&sblob); } } if (asn1_peek_tag(data, ASN1_CONTEXT(3))) { char *princ = NULL; /* mechListMIC [3] OCTET STRING OPTIONAL */ asn1_start_tag(data, ASN1_CONTEXT(3)); asn1_start_tag(data, ASN1_SEQUENCE(0)); asn1_start_tag(data, ASN1_CONTEXT(0)); asn1_read_GeneralString(data, ctx, &princ); asn1_end_tag(data); asn1_end_tag(data); asn1_end_tag(data); if (principal) { *principal = princ; } else { TALLOC_FREE(princ); } } asn1_end_tag(data); asn1_end_tag(data); asn1_end_tag(data); ret = !data->has_error; if (data->has_error) { int j; if (principal) { TALLOC_FREE(*principal); } if (secblob) { data_blob_free(secblob); } for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) { TALLOC_FREE(OIDs[j]); } } asn1_free(data); return ret; }
/* parse a SPNEGO auth packet. This contains the encrypted passwords */ bool spnego_parse_auth_response(TALLOC_CTX *ctx, DATA_BLOB blob, NTSTATUS nt_status, const char *mechOID, DATA_BLOB *auth) { ASN1_DATA *data; uint8 negResult; if (NT_STATUS_IS_OK(nt_status)) { negResult = SPNEGO_ACCEPT_COMPLETED; } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) { negResult = SPNEGO_ACCEPT_INCOMPLETE; } else { negResult = SPNEGO_REJECT; } data = asn1_init(talloc_tos()); if (data == NULL) { return false; } asn1_load(data, blob); asn1_start_tag(data, ASN1_CONTEXT(1)); asn1_start_tag(data, ASN1_SEQUENCE(0)); asn1_start_tag(data, ASN1_CONTEXT(0)); asn1_check_enumerated(data, negResult); asn1_end_tag(data); *auth = data_blob_null; if (asn1_tag_remaining(data)) { asn1_start_tag(data,ASN1_CONTEXT(1)); asn1_check_OID(data, mechOID); asn1_end_tag(data); if (asn1_tag_remaining(data)) { asn1_start_tag(data,ASN1_CONTEXT(2)); asn1_read_OctetString(data, ctx, auth); asn1_end_tag(data); } } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) { data->has_error = 1; } /* Binding against Win2K DC returns a duplicate of the responseToken in * the optional mechListMIC field. This is a bug in Win2K. We ignore * this field if it exists. Win2K8 may return a proper mechListMIC at * which point we need to implement the integrity checking. */ if (asn1_tag_remaining(data)) { DATA_BLOB mechList = data_blob_null; asn1_start_tag(data, ASN1_CONTEXT(3)); asn1_read_OctetString(data, ctx, &mechList); asn1_end_tag(data); data_blob_free(&mechList); DEBUG(5,("spnego_parse_auth_response received mechListMIC, " "ignoring.\n")); } asn1_end_tag(data); asn1_end_tag(data); if (data->has_error) { DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs)); asn1_free(data); data_blob_free(auth); return False; } asn1_free(data); return True; }
/* receive a cldap netlogon reply */ static int recv_cldap_netlogon(int sock, struct cldap_netlogon_reply *reply) { int ret; ASN1_DATA data; DATA_BLOB blob; DATA_BLOB os1, os2, os3; uint32 i1; char *p; blob = data_blob(NULL, 8192); ret = read(sock, blob.data, blob.length); if (ret <= 0) { d_printf("no reply received to cldap netlogon\n"); return -1; } blob.length = ret; asn1_load(&data, blob); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_read_Integer(&data, &i1); asn1_start_tag(&data, ASN1_APPLICATION(4)); asn1_read_OctetString(&data, &os1); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_start_tag(&data, ASN1_SEQUENCE(0)); asn1_read_OctetString(&data, &os2); asn1_start_tag(&data, ASN1_SET); asn1_read_OctetString(&data, &os3); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); asn1_end_tag(&data); if (data.has_error) { d_printf("Failed to parse cldap reply\n"); return -1; } p = (char *)os3.data; reply->type = IVAL(p, 0); p += 4; reply->flags = IVAL(p, 0); p += 4; memcpy(&reply->guid.info, p, UUID_FLAT_SIZE); p += UUID_FLAT_SIZE; p += pull_netlogon_string(reply->forest, p, (const char *)os3.data); p += pull_netlogon_string(reply->domain, p, (const char *)os3.data); p += pull_netlogon_string(reply->hostname, p, (const char *)os3.data); p += pull_netlogon_string(reply->netbios_domain, p, (const char *)os3.data); p += pull_netlogon_string(reply->netbios_hostname, p, (const char *)os3.data); p += pull_netlogon_string(reply->unk, p, (const char *)os3.data); if (reply->type == SAMLOGON_AD_R) { p += pull_netlogon_string(reply->user_name, p, (const char *)os3.data); } else { *reply->user_name = 0; } p += pull_netlogon_string(reply->site_name, p, (const char *)os3.data); p += pull_netlogon_string(reply->site_name_2, p, (const char *)os3.data); reply->version = IVAL(p, 0); reply->lmnt_token = SVAL(p, 4); reply->lm20_token = SVAL(p, 6); data_blob_free(&os1); data_blob_free(&os2); data_blob_free(&os3); data_blob_free(&blob); return 0; }
/* handle recv events on a cldap socket */ static bool cldap_socket_recv_dgram(struct cldap_socket *c, struct cldap_incoming *in) { DATA_BLOB blob; struct asn1_data *asn1; void *p; struct cldap_search_state *search; NTSTATUS status; if (in->recv_errno != 0) { goto error; } blob = data_blob_const(in->buf, in->len); asn1 = asn1_init(in); if (!asn1) { goto nomem; } if (!asn1_load(asn1, blob)) { goto nomem; } in->ldap_msg = talloc(in, struct ldap_message); if (in->ldap_msg == NULL) { goto nomem; } /* this initial decode is used to find the message id */ status = ldap_decode(asn1, NULL, in->ldap_msg); if (!NT_STATUS_IS_OK(status)) { goto nterror; } /* find the pending request */ p = idr_find(c->searches.idr, in->ldap_msg->messageid); if (p == NULL) { if (!c->incoming.handler) { TALLOC_FREE(in); return true; } /* this function should free or steal 'in' */ c->incoming.handler(c, c->incoming.private_data, in); return false; } search = talloc_get_type_abort(p, struct cldap_search_state); search->response.in = talloc_move(search, &in); search->response.asn1 = asn1; search->response.asn1->ofs = 0; DLIST_REMOVE(c->searches.list, search); if (cldap_recvfrom_setup(c)) { tevent_req_done(search->req); return true; } /* * This request was ok, just defer the notify of the caller * and then just fail the next request if needed */ tevent_req_defer_callback(search->req, search->caller.ev); tevent_req_done(search->req); status = NT_STATUS_NO_MEMORY; /* in is NULL it this point */ goto nterror; nomem: in->recv_errno = ENOMEM; error: status = map_nt_error_from_unix_common(in->recv_errno); nterror: TALLOC_FREE(in); /* in connected mode the first pending search gets the error */ if (!c->connected) { /* otherwise we just ignore the error */ return false; } if (!c->searches.list) { return false; } /* * We might called tevent_req_done() for a successful * search before, so we better deliver the failure * after the success, that is why we better also * use tevent_req_defer_callback() here. */ tevent_req_defer_callback(c->searches.list->req, c->searches.list->caller.ev); tevent_req_nterror(c->searches.list->req, status); return false; }
static void ldapsrv_call_read_done(struct tevent_req *subreq) { struct ldapsrv_connection *conn = tevent_req_callback_data(subreq, struct ldapsrv_connection); NTSTATUS status; struct ldapsrv_call *call; struct asn1_data *asn1; DATA_BLOB blob; call = talloc_zero(conn, struct ldapsrv_call); if (!call) { ldapsrv_terminate_connection(conn, "no memory"); return; } call->conn = conn; status = tstream_read_pdu_blob_recv(subreq, call, &blob); TALLOC_FREE(subreq); if (!NT_STATUS_IS_OK(status)) { const char *reason; reason = talloc_asprintf(call, "ldapsrv_call_loop: " "tstream_read_pdu_blob_recv() - %s", nt_errstr(status)); if (!reason) { reason = nt_errstr(status); } ldapsrv_terminate_connection(conn, reason); return; } asn1 = asn1_init(call); if (asn1 == NULL) { ldapsrv_terminate_connection(conn, "no memory"); return; } call->request = talloc(call, struct ldap_message); if (call->request == NULL) { ldapsrv_terminate_connection(conn, "no memory"); return; } if (!asn1_load(asn1, blob)) { ldapsrv_terminate_connection(conn, "asn1_load failed"); return; } status = ldap_decode(asn1, samba_ldap_control_handlers(), call->request); if (!NT_STATUS_IS_OK(status)) { ldapsrv_terminate_connection(conn, nt_errstr(status)); return; } data_blob_free(&blob); /* queue the call in the global queue */ subreq = ldapsrv_process_call_send(call, conn->connection->event.ctx, conn->service->call_queue, call); if (subreq == NULL) { ldapsrv_terminate_connection(conn, "ldapsrv_process_call_send failed"); return; } tevent_req_set_callback(subreq, ldapsrv_call_process_done, call); conn->active_call = subreq; }
/* handle recv events on a cldap socket */ static void cldap_socket_recv_dgram(struct cldap_socket *c, struct cldap_incoming *in) { DATA_BLOB blob; struct asn1_data *asn1; void *p; struct cldap_search_state *search; NTSTATUS status; if (in->recv_errno != 0) { goto error; } blob = data_blob_const(in->buf, in->len); asn1 = asn1_init(in); if (!asn1) { goto nomem; } if (!asn1_load(asn1, blob)) { goto nomem; } in->ldap_msg = talloc(in, struct ldap_message); if (in->ldap_msg == NULL) { goto nomem; } /* this initial decode is used to find the message id */ status = ldap_decode(asn1, NULL, in->ldap_msg); if (!NT_STATUS_IS_OK(status)) { goto nterror; } /* find the pending request */ p = idr_find(c->searches.idr, in->ldap_msg->messageid); if (p == NULL) { if (!c->incoming.handler) { goto done; } /* this function should free or steal 'in' */ c->incoming.handler(c, c->incoming.private_data, in); return; } search = talloc_get_type(p, struct cldap_search_state); search->response.in = talloc_move(search, &in); search->response.asn1 = asn1; search->response.asn1->ofs = 0; tevent_req_done(search->req); goto done; nomem: in->recv_errno = ENOMEM; error: status = map_nt_error_from_unix(in->recv_errno); nterror: /* in connected mode the first pending search gets the error */ if (!c->connected) { /* otherwise we just ignore the error */ goto done; } if (!c->searches.list) { goto done; } tevent_req_nterror(c->searches.list->req, status); done: talloc_free(in); }