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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "../libcli/auth/spnego.h"
27 generate a negTokenInit packet given a list of supported
28 OIDs (the mechanisms) and a principal name string
30 DATA_BLOB spnego_gen_negTokenInit(const char *OIDs[],
31 const char *principal)
37 data = asn1_init(talloc_tos());
39 return data_blob_null;
42 asn1_push_tag(data,ASN1_APPLICATION(0));
43 asn1_write_OID(data,OID_SPNEGO);
44 asn1_push_tag(data,ASN1_CONTEXT(0));
45 asn1_push_tag(data,ASN1_SEQUENCE(0));
47 asn1_push_tag(data,ASN1_CONTEXT(0));
48 asn1_push_tag(data,ASN1_SEQUENCE(0));
49 for (i=0; OIDs[i]; i++) {
50 asn1_write_OID(data,OIDs[i]);
55 asn1_push_tag(data, ASN1_CONTEXT(3));
56 asn1_push_tag(data, ASN1_SEQUENCE(0));
57 asn1_push_tag(data, ASN1_CONTEXT(0));
58 asn1_write_GeneralString(data,principal);
68 if (data->has_error) {
69 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
72 ret = data_blob(data->data, data->length);
79 Generate a negTokenInit as used by the client side ... It has a mechType
80 (OID), and a mechToken (a security blob) ...
82 Really, we need to break out the NTLMSSP stuff as well, because it could be
85 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
90 data = asn1_init(talloc_tos());
92 return data_blob_null;
95 asn1_push_tag(data, ASN1_APPLICATION(0));
96 asn1_write_OID(data,OID_SPNEGO);
97 asn1_push_tag(data, ASN1_CONTEXT(0));
98 asn1_push_tag(data, ASN1_SEQUENCE(0));
100 asn1_push_tag(data, ASN1_CONTEXT(0));
101 asn1_push_tag(data, ASN1_SEQUENCE(0));
102 asn1_write_OID(data, OID);
106 asn1_push_tag(data, ASN1_CONTEXT(2));
107 asn1_write_OctetString(data,blob.data,blob.length);
115 if (data->has_error) {
116 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
119 ret = data_blob(data->data, data->length);
126 parse a negTokenInit packet giving a GUID, a list of supported
127 OIDs (the mechanisms) and a principal name string
129 bool spnego_parse_negTokenInit(DATA_BLOB blob,
130 char *OIDs[ASN1_MAX_OIDS],
138 data = asn1_init(talloc_tos());
143 asn1_load(data, blob);
145 asn1_start_tag(data,ASN1_APPLICATION(0));
147 asn1_check_OID(data,OID_SPNEGO);
149 /* negTokenInit [0] NegTokenInit */
150 asn1_start_tag(data,ASN1_CONTEXT(0));
151 asn1_start_tag(data,ASN1_SEQUENCE(0));
153 /* mechTypes [0] MechTypeList OPTIONAL */
155 /* Not really optional, we depend on this to decide
156 * what mechanisms we have to work with. */
158 asn1_start_tag(data,ASN1_CONTEXT(0));
159 asn1_start_tag(data,ASN1_SEQUENCE(0));
160 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
161 const char *oid_str = NULL;
162 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
163 OIDs[i] = CONST_DISCARD(char *, oid_str);
173 *secblob = data_blob_null;
177 Win7 + Live Sign-in Assistant attaches a mechToken
178 ASN1_CONTEXT(2) to the negTokenInit packet
179 which breaks our negotiation if we just assume
180 the next tag is ASN1_CONTEXT(3).
183 if (asn1_tag_remaining(data) > 0) {
184 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
187 /* reqFlags [1] ContextFlags OPTIONAL */
188 asn1_start_tag(data, ASN1_CONTEXT(1));
189 asn1_start_tag(data, ASN1_BIT_STRING);
190 while (asn1_tag_remaining(data) > 0) {
191 asn1_read_uint8(data, &flags);
198 if (asn1_tag_remaining(data) > 0) {
199 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
200 DATA_BLOB sblob = data_blob_null;
201 /* mechToken [2] OCTET STRING OPTIONAL */
202 asn1_start_tag(data, ASN1_CONTEXT(2));
203 asn1_read_OctetString(data, talloc_autofree_context(),
212 if (asn1_tag_remaining(data) > 0) {
213 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
215 /* mechListMIC [3] OCTET STRING OPTIONAL */
216 asn1_start_tag(data, ASN1_CONTEXT(3));
217 asn1_start_tag(data, ASN1_SEQUENCE(0));
218 asn1_start_tag(data, ASN1_CONTEXT(0));
219 asn1_read_GeneralString(data,talloc_autofree_context(),
235 ret = !data->has_error;
236 if (data->has_error) {
239 TALLOC_FREE(*principal);
242 data_blob_free(secblob);
244 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
245 TALLOC_FREE(OIDs[j]);
254 generate a negTokenTarg packet given a list of OIDs and a security blob
256 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
262 data = asn1_init(talloc_tos());
264 return data_blob_null;
267 asn1_push_tag(data, ASN1_APPLICATION(0));
268 asn1_write_OID(data,OID_SPNEGO);
269 asn1_push_tag(data, ASN1_CONTEXT(0));
270 asn1_push_tag(data, ASN1_SEQUENCE(0));
272 asn1_push_tag(data, ASN1_CONTEXT(0));
273 asn1_push_tag(data, ASN1_SEQUENCE(0));
274 for (i=0; OIDs[i]; i++) {
275 asn1_write_OID(data,OIDs[i]);
280 asn1_push_tag(data, ASN1_CONTEXT(2));
281 asn1_write_OctetString(data,blob.data,blob.length);
289 if (data->has_error) {
290 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
293 ret = data_blob(data->data, data->length);
300 generate a krb5 GSS-API wrapper packet given a ticket
302 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
307 data = asn1_init(talloc_tos());
309 return data_blob_null;
312 asn1_push_tag(data, ASN1_APPLICATION(0));
313 asn1_write_OID(data, OID_KERBEROS5);
315 asn1_write(data, tok_id, 2);
316 asn1_write(data, ticket.data, ticket.length);
319 if (data->has_error) {
320 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
323 ret = data_blob(data->data, data->length);
330 parse a krb5 GSS-API wrapper packet giving a ticket
332 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
338 data = asn1_init(talloc_tos());
343 asn1_load(data, blob);
344 asn1_start_tag(data, ASN1_APPLICATION(0));
345 asn1_check_OID(data, OID_KERBEROS5);
347 data_remaining = asn1_tag_remaining(data);
349 if (data_remaining < 3) {
350 data->has_error = True;
352 asn1_read(data, tok_id, 2);
354 *ticket = data_blob(NULL, data_remaining);
355 asn1_read(data, ticket->data, ticket->length);
360 ret = !data->has_error;
362 if (data->has_error) {
363 data_blob_free(ticket);
373 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
374 kerberos session setup
376 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
378 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
382 DATA_BLOB tkt, tkt_wrapped;
383 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
385 /* get a kerberos ticket for the service and extract the session key */
386 retval = cli_krb5_get_ticket(principal, time_offset,
387 &tkt, session_key_krb5, extra_ap_opts, NULL,
393 /* wrap that up in a nice GSS-API wrapping */
394 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
396 /* and wrap that in a shiny SPNEGO wrapper */
397 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
399 data_blob_free(&tkt_wrapped);
400 data_blob_free(&tkt);
407 parse a spnego NTLMSSP challenge packet giving two security blobs
409 bool spnego_parse_challenge(const DATA_BLOB blob,
410 DATA_BLOB *chal1, DATA_BLOB *chal2)
418 data = asn1_init(talloc_tos());
423 asn1_load(data, blob);
424 asn1_start_tag(data,ASN1_CONTEXT(1));
425 asn1_start_tag(data,ASN1_SEQUENCE(0));
427 asn1_start_tag(data,ASN1_CONTEXT(0));
428 asn1_check_enumerated(data,1);
431 asn1_start_tag(data,ASN1_CONTEXT(1));
432 asn1_check_OID(data, OID_NTLMSSP);
435 asn1_start_tag(data,ASN1_CONTEXT(2));
436 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
439 /* the second challenge is optional (XP doesn't send it) */
440 if (asn1_tag_remaining(data)) {
441 asn1_start_tag(data,ASN1_CONTEXT(3));
442 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
449 ret = !data->has_error;
451 if (data->has_error) {
452 data_blob_free(chal1);
453 data_blob_free(chal2);
462 generate a SPNEGO auth packet. This will contain the encrypted passwords
464 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
469 data = asn1_init(talloc_tos());
471 return data_blob_null;
474 asn1_push_tag(data, ASN1_CONTEXT(1));
475 asn1_push_tag(data, ASN1_SEQUENCE(0));
476 asn1_push_tag(data, ASN1_CONTEXT(2));
477 asn1_write_OctetString(data,blob.data,blob.length);
482 ret = data_blob(data->data, data->length);
490 parse a SPNEGO auth packet. This contains the encrypted passwords
492 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
495 struct spnego_data token;
497 len = spnego_read_data(talloc_tos(), blob, &token);
499 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
503 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
504 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
506 spnego_free_data(&token);
510 *auth = data_blob_talloc(talloc_tos(),
511 token.negTokenTarg.responseToken.data,
512 token.negTokenTarg.responseToken.length);
513 spnego_free_data(&token);
519 generate a minimal SPNEGO response packet. Doesn't contain much.
521 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
528 if (NT_STATUS_IS_OK(nt_status)) {
529 negResult = SPNEGO_ACCEPT_COMPLETED;
530 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
531 negResult = SPNEGO_ACCEPT_INCOMPLETE;
533 negResult = SPNEGO_REJECT;
536 data = asn1_init(talloc_tos());
538 return data_blob_null;
541 asn1_push_tag(data, ASN1_CONTEXT(1));
542 asn1_push_tag(data, ASN1_SEQUENCE(0));
543 asn1_push_tag(data, ASN1_CONTEXT(0));
544 asn1_write_enumerated(data, negResult);
548 asn1_push_tag(data,ASN1_CONTEXT(1));
549 asn1_write_OID(data, mechOID);
553 if (reply && reply->data != NULL) {
554 asn1_push_tag(data,ASN1_CONTEXT(2));
555 asn1_write_OctetString(data, reply->data, reply->length);
562 ret = data_blob(data->data, data->length);
568 parse a SPNEGO auth packet. This contains the encrypted passwords
570 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
577 if (NT_STATUS_IS_OK(nt_status)) {
578 negResult = SPNEGO_ACCEPT_COMPLETED;
579 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
580 negResult = SPNEGO_ACCEPT_INCOMPLETE;
582 negResult = SPNEGO_REJECT;
585 data = asn1_init(talloc_tos());
590 asn1_load(data, blob);
591 asn1_start_tag(data, ASN1_CONTEXT(1));
592 asn1_start_tag(data, ASN1_SEQUENCE(0));
593 asn1_start_tag(data, ASN1_CONTEXT(0));
594 asn1_check_enumerated(data, negResult);
597 *auth = data_blob_null;
599 if (asn1_tag_remaining(data)) {
600 asn1_start_tag(data,ASN1_CONTEXT(1));
601 asn1_check_OID(data, mechOID);
604 if (asn1_tag_remaining(data)) {
605 asn1_start_tag(data,ASN1_CONTEXT(2));
606 asn1_read_OctetString(data, talloc_autofree_context(), auth);
609 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
613 /* Binding against Win2K DC returns a duplicate of the responseToken in
614 * the optional mechListMIC field. This is a bug in Win2K. We ignore
615 * this field if it exists. Win2K8 may return a proper mechListMIC at
616 * which point we need to implement the integrity checking. */
617 if (asn1_tag_remaining(data)) {
618 DATA_BLOB mechList = data_blob_null;
619 asn1_start_tag(data, ASN1_CONTEXT(3));
620 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
622 data_blob_free(&mechList);
623 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
630 if (data->has_error) {
631 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
633 data_blob_free(auth);