2 Unix SMB/CIFS implementation.
3 simple kerberos5/SPNEGO routines
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Jeremy Allison 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "../libcli/auth/spnego.h"
28 generate a negTokenInit packet given a list of supported
29 OIDs (the mechanisms) a blob, and a principal name string
32 DATA_BLOB spnego_gen_negTokenInit(const char *OIDs[],
34 const char *principal)
40 data = asn1_init(talloc_tos());
42 return data_blob_null;
45 asn1_push_tag(data,ASN1_APPLICATION(0));
46 asn1_write_OID(data,OID_SPNEGO);
47 asn1_push_tag(data,ASN1_CONTEXT(0));
48 asn1_push_tag(data,ASN1_SEQUENCE(0));
50 asn1_push_tag(data,ASN1_CONTEXT(0));
51 asn1_push_tag(data,ASN1_SEQUENCE(0));
52 for (i=0; OIDs[i]; i++) {
53 asn1_write_OID(data,OIDs[i]);
58 if (psecblob && psecblob->length && psecblob->data) {
59 asn1_push_tag(data, ASN1_CONTEXT(2));
60 asn1_write_OctetString(data,psecblob->data,
66 asn1_push_tag(data, ASN1_CONTEXT(3));
67 asn1_push_tag(data, ASN1_SEQUENCE(0));
68 asn1_push_tag(data, ASN1_CONTEXT(0));
69 asn1_write_GeneralString(data,principal);
80 if (data->has_error) {
81 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
84 ret = data_blob(data->data, data->length);
91 parse a negTokenInit packet giving a GUID, a list of supported
92 OIDs (the mechanisms) and a principal name string
94 bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
96 char *OIDs[ASN1_MAX_OIDS],
104 data = asn1_init(talloc_tos());
109 asn1_load(data, blob);
111 asn1_start_tag(data,ASN1_APPLICATION(0));
113 asn1_check_OID(data,OID_SPNEGO);
115 /* negTokenInit [0] NegTokenInit */
116 asn1_start_tag(data,ASN1_CONTEXT(0));
117 asn1_start_tag(data,ASN1_SEQUENCE(0));
119 /* mechTypes [0] MechTypeList OPTIONAL */
121 /* Not really optional, we depend on this to decide
122 * what mechanisms we have to work with. */
124 asn1_start_tag(data,ASN1_CONTEXT(0));
125 asn1_start_tag(data,ASN1_SEQUENCE(0));
126 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
127 const char *oid_str = NULL;
128 asn1_read_OID(data,ctx,&oid_str);
129 OIDs[i] = CONST_DISCARD(char *, oid_str);
139 *secblob = data_blob_null;
143 Win7 + Live Sign-in Assistant attaches a mechToken
144 ASN1_CONTEXT(2) to the negTokenInit packet
145 which breaks our negotiation if we just assume
146 the next tag is ASN1_CONTEXT(3).
149 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
152 /* reqFlags [1] ContextFlags OPTIONAL */
153 asn1_start_tag(data, ASN1_CONTEXT(1));
154 asn1_start_tag(data, ASN1_BIT_STRING);
155 while (asn1_tag_remaining(data) > 0) {
156 asn1_read_uint8(data, &flags);
162 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
163 DATA_BLOB sblob = data_blob_null;
164 /* mechToken [2] OCTET STRING OPTIONAL */
165 asn1_start_tag(data, ASN1_CONTEXT(2));
166 asn1_read_OctetString(data, ctx, &sblob);
171 data_blob_free(&sblob);
175 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
177 /* mechListMIC [3] OCTET STRING OPTIONAL */
178 asn1_start_tag(data, ASN1_CONTEXT(3));
179 asn1_start_tag(data, ASN1_SEQUENCE(0));
180 asn1_start_tag(data, ASN1_CONTEXT(0));
181 asn1_read_GeneralString(data, ctx, &princ);
197 ret = !data->has_error;
198 if (data->has_error) {
201 TALLOC_FREE(*principal);
204 data_blob_free(secblob);
206 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
207 TALLOC_FREE(OIDs[j]);
216 generate a krb5 GSS-API wrapper packet given a ticket
218 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
223 data = asn1_init(talloc_tos());
225 return data_blob_null;
228 asn1_push_tag(data, ASN1_APPLICATION(0));
229 asn1_write_OID(data, OID_KERBEROS5);
231 asn1_write(data, tok_id, 2);
232 asn1_write(data, ticket.data, ticket.length);
235 if (data->has_error) {
236 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
239 ret = data_blob(data->data, data->length);
246 parse a krb5 GSS-API wrapper packet giving a ticket
248 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
254 data = asn1_init(talloc_tos());
259 asn1_load(data, blob);
260 asn1_start_tag(data, ASN1_APPLICATION(0));
261 asn1_check_OID(data, OID_KERBEROS5);
263 data_remaining = asn1_tag_remaining(data);
265 if (data_remaining < 3) {
266 data->has_error = True;
268 asn1_read(data, tok_id, 2);
270 *ticket = data_blob(NULL, data_remaining);
271 asn1_read(data, ticket->data, ticket->length);
276 ret = !data->has_error;
278 if (data->has_error) {
279 data_blob_free(ticket);
289 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
290 kerberos session setup
292 int spnego_gen_krb5_negTokenInit(const char *principal, int time_offset,
294 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
298 DATA_BLOB tkt, tkt_wrapped;
299 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
301 /* get a kerberos ticket for the service and extract the session key */
302 retval = cli_krb5_get_ticket(principal, time_offset,
303 &tkt, session_key_krb5, extra_ap_opts, NULL,
309 /* wrap that up in a nice GSS-API wrapping */
310 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
312 /* and wrap that in a shiny SPNEGO wrapper */
313 *targ = spnego_gen_negTokenInit(krb_mechs, &tkt_wrapped, NULL);
315 data_blob_free(&tkt_wrapped);
316 data_blob_free(&tkt);
323 parse a spnego NTLMSSP challenge packet giving two security blobs
325 bool spnego_parse_challenge(const DATA_BLOB blob,
326 DATA_BLOB *chal1, DATA_BLOB *chal2)
334 data = asn1_init(talloc_tos());
339 asn1_load(data, blob);
340 asn1_start_tag(data,ASN1_CONTEXT(1));
341 asn1_start_tag(data,ASN1_SEQUENCE(0));
343 asn1_start_tag(data,ASN1_CONTEXT(0));
344 asn1_check_enumerated(data,1);
347 asn1_start_tag(data,ASN1_CONTEXT(1));
348 asn1_check_OID(data, OID_NTLMSSP);
351 asn1_start_tag(data,ASN1_CONTEXT(2));
352 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
355 /* the second challenge is optional (XP doesn't send it) */
356 if (asn1_tag_remaining(data)) {
357 asn1_start_tag(data,ASN1_CONTEXT(3));
358 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
365 ret = !data->has_error;
367 if (data->has_error) {
368 data_blob_free(chal1);
369 data_blob_free(chal2);
378 generate a SPNEGO auth packet. This will contain the encrypted passwords
380 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
385 data = asn1_init(talloc_tos());
387 return data_blob_null;
390 asn1_push_tag(data, ASN1_CONTEXT(1));
391 asn1_push_tag(data, ASN1_SEQUENCE(0));
392 asn1_push_tag(data, ASN1_CONTEXT(2));
393 asn1_write_OctetString(data,blob.data,blob.length);
398 ret = data_blob(data->data, data->length);
406 parse a SPNEGO auth packet. This contains the encrypted passwords
408 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
411 struct spnego_data token;
413 len = spnego_read_data(talloc_tos(), blob, &token);
415 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
419 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
420 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
422 spnego_free_data(&token);
426 *auth = data_blob_talloc(talloc_tos(),
427 token.negTokenTarg.responseToken.data,
428 token.negTokenTarg.responseToken.length);
429 spnego_free_data(&token);
435 generate a minimal SPNEGO response packet. Doesn't contain much.
437 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
444 if (NT_STATUS_IS_OK(nt_status)) {
445 negResult = SPNEGO_ACCEPT_COMPLETED;
446 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
447 negResult = SPNEGO_ACCEPT_INCOMPLETE;
449 negResult = SPNEGO_REJECT;
452 data = asn1_init(talloc_tos());
454 return data_blob_null;
457 asn1_push_tag(data, ASN1_CONTEXT(1));
458 asn1_push_tag(data, ASN1_SEQUENCE(0));
459 asn1_push_tag(data, ASN1_CONTEXT(0));
460 asn1_write_enumerated(data, negResult);
464 asn1_push_tag(data,ASN1_CONTEXT(1));
465 asn1_write_OID(data, mechOID);
469 if (reply && reply->data != NULL) {
470 asn1_push_tag(data,ASN1_CONTEXT(2));
471 asn1_write_OctetString(data, reply->data, reply->length);
478 ret = data_blob(data->data, data->length);
484 parse a SPNEGO auth packet. This contains the encrypted passwords
486 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
493 if (NT_STATUS_IS_OK(nt_status)) {
494 negResult = SPNEGO_ACCEPT_COMPLETED;
495 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
496 negResult = SPNEGO_ACCEPT_INCOMPLETE;
498 negResult = SPNEGO_REJECT;
501 data = asn1_init(talloc_tos());
506 asn1_load(data, blob);
507 asn1_start_tag(data, ASN1_CONTEXT(1));
508 asn1_start_tag(data, ASN1_SEQUENCE(0));
509 asn1_start_tag(data, ASN1_CONTEXT(0));
510 asn1_check_enumerated(data, negResult);
513 *auth = data_blob_null;
515 if (asn1_tag_remaining(data)) {
516 asn1_start_tag(data,ASN1_CONTEXT(1));
517 asn1_check_OID(data, mechOID);
520 if (asn1_tag_remaining(data)) {
521 asn1_start_tag(data,ASN1_CONTEXT(2));
522 asn1_read_OctetString(data, talloc_autofree_context(), auth);
525 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
529 /* Binding against Win2K DC returns a duplicate of the responseToken in
530 * the optional mechListMIC field. This is a bug in Win2K. We ignore
531 * this field if it exists. Win2K8 may return a proper mechListMIC at
532 * which point we need to implement the integrity checking. */
533 if (asn1_tag_remaining(data)) {
534 DATA_BLOB mechList = data_blob_null;
535 asn1_start_tag(data, ASN1_CONTEXT(3));
536 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
538 data_blob_free(&mechList);
539 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
546 if (data->has_error) {
547 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
549 data_blob_free(auth);