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 GUID, a list of supported
28 OIDs (the mechanisms) and a principal name string
30 DATA_BLOB spnego_gen_negTokenInit(char guid[16],
32 const char *principal)
38 data = asn1_init(talloc_tos());
40 return data_blob_null;
43 asn1_write(data, guid, 16);
44 asn1_push_tag(data,ASN1_APPLICATION(0));
45 asn1_write_OID(data,OID_SPNEGO);
46 asn1_push_tag(data,ASN1_CONTEXT(0));
47 asn1_push_tag(data,ASN1_SEQUENCE(0));
49 asn1_push_tag(data,ASN1_CONTEXT(0));
50 asn1_push_tag(data,ASN1_SEQUENCE(0));
51 for (i=0; OIDs[i]; i++) {
52 asn1_write_OID(data,OIDs[i]);
57 asn1_push_tag(data, ASN1_CONTEXT(3));
58 asn1_push_tag(data, ASN1_SEQUENCE(0));
59 asn1_push_tag(data, ASN1_CONTEXT(0));
60 asn1_write_GeneralString(data,principal);
70 if (data->has_error) {
71 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
74 ret = data_blob(data->data, data->length);
81 Generate a negTokenInit as used by the client side ... It has a mechType
82 (OID), and a mechToken (a security blob) ...
84 Really, we need to break out the NTLMSSP stuff as well, because it could be
87 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
92 data = asn1_init(talloc_tos());
94 return data_blob_null;
97 asn1_push_tag(data, ASN1_APPLICATION(0));
98 asn1_write_OID(data,OID_SPNEGO);
99 asn1_push_tag(data, ASN1_CONTEXT(0));
100 asn1_push_tag(data, ASN1_SEQUENCE(0));
102 asn1_push_tag(data, ASN1_CONTEXT(0));
103 asn1_push_tag(data, ASN1_SEQUENCE(0));
104 asn1_write_OID(data, OID);
108 asn1_push_tag(data, ASN1_CONTEXT(2));
109 asn1_write_OctetString(data,blob.data,blob.length);
117 if (data->has_error) {
118 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
121 ret = data_blob(data->data, data->length);
128 parse a negTokenInit packet giving a GUID, a list of supported
129 OIDs (the mechanisms) and a principal name string
131 bool spnego_parse_negTokenInit(DATA_BLOB blob,
132 char *OIDs[ASN1_MAX_OIDS],
139 for (i = 0; i < ASN1_MAX_OIDS; i++) {
143 data = asn1_init(talloc_tos());
148 asn1_load(data, blob);
150 asn1_start_tag(data,ASN1_APPLICATION(0));
152 asn1_check_OID(data,OID_SPNEGO);
154 /* negTokenInit [0] NegTokenInit */
155 asn1_start_tag(data,ASN1_CONTEXT(0));
156 asn1_start_tag(data,ASN1_SEQUENCE(0));
158 /* mechTypes [0] MechTypeList OPTIONAL */
160 /* Not really optional, we depend on this to decide
161 * what mechanisms we have to work with. */
163 asn1_start_tag(data,ASN1_CONTEXT(0));
164 asn1_start_tag(data,ASN1_SEQUENCE(0));
165 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
166 const char *oid_str = NULL;
167 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
168 if (data->has_error) {
171 OIDs[i] = CONST_DISCARD(char *, oid_str);
180 Win7 + Live Sign-in Assistant attaches a mechToken
181 ASN1_CONTEXT(2) to the negTokenInit packet
182 which breaks our negotiation if we just assume
183 the next tag is ASN1_CONTEXT(3).
186 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
189 /* reqFlags [1] ContextFlags OPTIONAL */
190 asn1_start_tag(data, ASN1_CONTEXT(1));
191 asn1_start_tag(data, ASN1_BIT_STRING);
192 while (asn1_tag_remaining(data) > 0) {
193 asn1_read_uint8(data, &flags);
199 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
200 /* mechToken [2] OCTET STRING OPTIONAL */
202 asn1_start_tag(data, ASN1_CONTEXT(2));
203 asn1_read_OctetString(data, talloc_autofree_context(),
206 /* Throw away the token - not used. */
207 data_blob_free(&token);
210 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
211 /* mechListMIC [3] OCTET STRING OPTIONAL */
212 asn1_start_tag(data, ASN1_CONTEXT(3));
213 asn1_start_tag(data, ASN1_SEQUENCE(0));
214 asn1_start_tag(data, ASN1_CONTEXT(0));
215 asn1_read_GeneralString(data,talloc_autofree_context(),
227 ret = !data->has_error;
228 if (data->has_error) {
230 TALLOC_FREE(*principal);
231 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
232 TALLOC_FREE(OIDs[j]);
241 generate a negTokenTarg packet given a list of OIDs and a security blob
243 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
249 data = asn1_init(talloc_tos());
251 return data_blob_null;
254 asn1_push_tag(data, ASN1_APPLICATION(0));
255 asn1_write_OID(data,OID_SPNEGO);
256 asn1_push_tag(data, ASN1_CONTEXT(0));
257 asn1_push_tag(data, ASN1_SEQUENCE(0));
259 asn1_push_tag(data, ASN1_CONTEXT(0));
260 asn1_push_tag(data, ASN1_SEQUENCE(0));
261 for (i=0; OIDs[i]; i++) {
262 asn1_write_OID(data,OIDs[i]);
267 asn1_push_tag(data, ASN1_CONTEXT(2));
268 asn1_write_OctetString(data,blob.data,blob.length);
276 if (data->has_error) {
277 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
280 ret = data_blob(data->data, data->length);
287 parse a negTokenTarg packet giving a list of OIDs and a security blob
289 bool parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
294 data = asn1_init(talloc_tos());
299 asn1_load(data, blob);
300 asn1_start_tag(data, ASN1_APPLICATION(0));
301 asn1_check_OID(data,OID_SPNEGO);
302 asn1_start_tag(data, ASN1_CONTEXT(0));
303 asn1_start_tag(data, ASN1_SEQUENCE(0));
305 asn1_start_tag(data, ASN1_CONTEXT(0));
306 asn1_start_tag(data, ASN1_SEQUENCE(0));
307 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
308 const char *oid_str = NULL;
309 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
310 OIDs[i] = CONST_DISCARD(char *, oid_str);
316 /* Skip any optional req_flags that are sent per RFC 4178 */
317 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
320 asn1_start_tag(data, ASN1_CONTEXT(1));
321 asn1_start_tag(data, ASN1_BIT_STRING);
322 while (asn1_tag_remaining(data) > 0)
323 asn1_read_uint8(data, &flags);
328 asn1_start_tag(data, ASN1_CONTEXT(2));
329 asn1_read_OctetString(data,talloc_autofree_context(),secblob);
337 if (data->has_error) {
339 data_blob_free(secblob);
340 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
341 TALLOC_FREE(OIDs[j]);
343 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data->ofs));
353 generate a krb5 GSS-API wrapper packet given a ticket
355 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
360 data = asn1_init(talloc_tos());
362 return data_blob_null;
365 asn1_push_tag(data, ASN1_APPLICATION(0));
366 asn1_write_OID(data, OID_KERBEROS5);
368 asn1_write(data, tok_id, 2);
369 asn1_write(data, ticket.data, ticket.length);
372 if (data->has_error) {
373 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
376 ret = data_blob(data->data, data->length);
383 parse a krb5 GSS-API wrapper packet giving a ticket
385 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
390 *ticket = data_blob_null;
392 data = asn1_init(talloc_tos());
397 asn1_load(data, blob);
398 asn1_start_tag(data, ASN1_APPLICATION(0));
399 asn1_check_OID(data, OID_KERBEROS5);
401 data_remaining = asn1_tag_remaining(data);
403 if (data_remaining < 3) {
404 data->has_error = True;
406 asn1_read(data, tok_id, 2);
408 *ticket = data_blob(NULL, data_remaining);
409 asn1_read(data, ticket->data, ticket->length);
414 ret = !data->has_error;
416 if (data->has_error) {
417 data_blob_free(ticket);
427 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
428 kerberos session setup
430 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
432 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
436 DATA_BLOB tkt, tkt_wrapped;
437 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
439 /* get a kerberos ticket for the service and extract the session key */
440 retval = cli_krb5_get_ticket(principal, time_offset,
441 &tkt, session_key_krb5, extra_ap_opts, NULL,
447 /* wrap that up in a nice GSS-API wrapping */
448 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
450 /* and wrap that in a shiny SPNEGO wrapper */
451 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
453 data_blob_free(&tkt_wrapped);
454 data_blob_free(&tkt);
461 parse a spnego NTLMSSP challenge packet giving two security blobs
463 bool spnego_parse_challenge(const DATA_BLOB blob,
464 DATA_BLOB *chal1, DATA_BLOB *chal2)
472 data = asn1_init(talloc_tos());
477 asn1_load(data, blob);
478 asn1_start_tag(data,ASN1_CONTEXT(1));
479 asn1_start_tag(data,ASN1_SEQUENCE(0));
481 asn1_start_tag(data,ASN1_CONTEXT(0));
482 asn1_check_enumerated(data,1);
485 asn1_start_tag(data,ASN1_CONTEXT(1));
486 asn1_check_OID(data, OID_NTLMSSP);
489 asn1_start_tag(data,ASN1_CONTEXT(2));
490 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
493 /* the second challenge is optional (XP doesn't send it) */
494 if (asn1_tag_remaining(data)) {
495 asn1_start_tag(data,ASN1_CONTEXT(3));
496 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
503 ret = !data->has_error;
505 if (data->has_error) {
506 data_blob_free(chal1);
507 data_blob_free(chal2);
516 generate a SPNEGO auth packet. This will contain the encrypted passwords
518 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
523 data = asn1_init(talloc_tos());
525 return data_blob_null;
528 asn1_push_tag(data, ASN1_CONTEXT(1));
529 asn1_push_tag(data, ASN1_SEQUENCE(0));
530 asn1_push_tag(data, ASN1_CONTEXT(2));
531 asn1_write_OctetString(data,blob.data,blob.length);
536 ret = data_blob(data->data, data->length);
544 parse a SPNEGO auth packet. This contains the encrypted passwords
546 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
549 struct spnego_data token;
551 len = spnego_read_data(talloc_tos(), blob, &token);
553 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
557 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
558 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
560 spnego_free_data(&token);
564 *auth = data_blob_talloc(talloc_tos(),
565 token.negTokenTarg.responseToken.data,
566 token.negTokenTarg.responseToken.length);
567 spnego_free_data(&token);
573 generate a minimal SPNEGO response packet. Doesn't contain much.
575 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
582 if (NT_STATUS_IS_OK(nt_status)) {
583 negResult = SPNEGO_ACCEPT_COMPLETED;
584 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
585 negResult = SPNEGO_ACCEPT_INCOMPLETE;
587 negResult = SPNEGO_REJECT;
590 data = asn1_init(talloc_tos());
592 return data_blob_null;
595 asn1_push_tag(data, ASN1_CONTEXT(1));
596 asn1_push_tag(data, ASN1_SEQUENCE(0));
597 asn1_push_tag(data, ASN1_CONTEXT(0));
598 asn1_write_enumerated(data, negResult);
602 asn1_push_tag(data,ASN1_CONTEXT(1));
603 asn1_write_OID(data, mechOID);
607 if (reply && reply->data != NULL) {
608 asn1_push_tag(data,ASN1_CONTEXT(2));
609 asn1_write_OctetString(data, reply->data, reply->length);
616 ret = data_blob(data->data, data->length);
622 parse a SPNEGO auth packet. This contains the encrypted passwords
624 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
631 if (NT_STATUS_IS_OK(nt_status)) {
632 negResult = SPNEGO_ACCEPT_COMPLETED;
633 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
634 negResult = SPNEGO_ACCEPT_INCOMPLETE;
636 negResult = SPNEGO_REJECT;
639 data = asn1_init(talloc_tos());
644 asn1_load(data, blob);
645 asn1_start_tag(data, ASN1_CONTEXT(1));
646 asn1_start_tag(data, ASN1_SEQUENCE(0));
647 asn1_start_tag(data, ASN1_CONTEXT(0));
648 asn1_check_enumerated(data, negResult);
651 *auth = data_blob_null;
653 if (asn1_tag_remaining(data)) {
654 asn1_start_tag(data,ASN1_CONTEXT(1));
655 asn1_check_OID(data, mechOID);
658 if (asn1_tag_remaining(data)) {
659 asn1_start_tag(data,ASN1_CONTEXT(2));
660 asn1_read_OctetString(data, talloc_autofree_context(), auth);
663 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
667 /* Binding against Win2K DC returns a duplicate of the responseToken in
668 * the optional mechListMIC field. This is a bug in Win2K. We ignore
669 * this field if it exists. Win2K8 may return a proper mechListMIC at
670 * which point we need to implement the integrity checking. */
671 if (asn1_tag_remaining(data)) {
672 DATA_BLOB mechList = data_blob_null;
673 asn1_start_tag(data, ASN1_CONTEXT(3));
674 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
676 data_blob_free(&mechList);
677 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
684 if (data->has_error) {
685 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
687 data_blob_free(auth);