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 data = asn1_init(talloc_tos());
144 asn1_load(data, blob);
146 asn1_start_tag(data,ASN1_APPLICATION(0));
148 asn1_check_OID(data,OID_SPNEGO);
150 /* negTokenInit [0] NegTokenInit */
151 asn1_start_tag(data,ASN1_CONTEXT(0));
152 asn1_start_tag(data,ASN1_SEQUENCE(0));
154 /* mechTypes [0] MechTypeList OPTIONAL */
156 /* Not really optional, we depend on this to decide
157 * what mechanisms we have to work with. */
159 asn1_start_tag(data,ASN1_CONTEXT(0));
160 asn1_start_tag(data,ASN1_SEQUENCE(0));
161 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
162 const char *oid_str = NULL;
163 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
164 if (data->has_error) {
167 OIDs[i] = CONST_DISCARD(char *, oid_str);
176 Win7 + Live Sign-in Assistant attaches a mechToken
177 ASN1_CONTEXT(2) to the negTokenInit packet
178 which breaks our negotiation if we just assume
179 the next tag is ASN1_CONTEXT(3).
182 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
185 /* reqFlags [1] ContextFlags OPTIONAL */
186 asn1_start_tag(data, ASN1_CONTEXT(1));
187 asn1_start_tag(data, ASN1_BIT_STRING);
188 while (asn1_tag_remaining(data) > 0) {
189 asn1_read_uint8(data, &flags);
195 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
196 /* mechToken [2] OCTET STRING OPTIONAL */
198 asn1_start_tag(data, ASN1_CONTEXT(2));
199 asn1_read_OctetString(data, talloc_autofree_context(),
202 /* Throw away the token - not used. */
203 data_blob_free(&token);
206 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
207 /* mechListMIC [3] OCTET STRING OPTIONAL */
208 asn1_start_tag(data, ASN1_CONTEXT(3));
209 asn1_start_tag(data, ASN1_SEQUENCE(0));
210 asn1_start_tag(data, ASN1_CONTEXT(0));
211 asn1_read_GeneralString(data,talloc_autofree_context(),
223 ret = !data->has_error;
224 if (data->has_error) {
226 TALLOC_FREE(*principal);
227 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
228 TALLOC_FREE(OIDs[j]);
237 generate a negTokenTarg packet given a list of OIDs and a security blob
239 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
245 data = asn1_init(talloc_tos());
247 return data_blob_null;
250 asn1_push_tag(data, ASN1_APPLICATION(0));
251 asn1_write_OID(data,OID_SPNEGO);
252 asn1_push_tag(data, ASN1_CONTEXT(0));
253 asn1_push_tag(data, ASN1_SEQUENCE(0));
255 asn1_push_tag(data, ASN1_CONTEXT(0));
256 asn1_push_tag(data, ASN1_SEQUENCE(0));
257 for (i=0; OIDs[i]; i++) {
258 asn1_write_OID(data,OIDs[i]);
263 asn1_push_tag(data, ASN1_CONTEXT(2));
264 asn1_write_OctetString(data,blob.data,blob.length);
272 if (data->has_error) {
273 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
276 ret = data_blob(data->data, data->length);
283 parse a negTokenTarg packet giving a list of OIDs and a security blob
285 bool parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
290 data = asn1_init(talloc_tos());
295 asn1_load(data, blob);
296 asn1_start_tag(data, ASN1_APPLICATION(0));
297 asn1_check_OID(data,OID_SPNEGO);
298 asn1_start_tag(data, ASN1_CONTEXT(0));
299 asn1_start_tag(data, ASN1_SEQUENCE(0));
301 asn1_start_tag(data, ASN1_CONTEXT(0));
302 asn1_start_tag(data, ASN1_SEQUENCE(0));
303 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
304 const char *oid_str = NULL;
305 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
306 OIDs[i] = CONST_DISCARD(char *, oid_str);
312 /* Skip any optional req_flags that are sent per RFC 4178 */
313 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
316 asn1_start_tag(data, ASN1_CONTEXT(1));
317 asn1_start_tag(data, ASN1_BIT_STRING);
318 while (asn1_tag_remaining(data) > 0)
319 asn1_read_uint8(data, &flags);
324 asn1_start_tag(data, ASN1_CONTEXT(2));
325 asn1_read_OctetString(data,talloc_autofree_context(),secblob);
333 if (data->has_error) {
335 data_blob_free(secblob);
336 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
337 TALLOC_FREE(OIDs[j]);
339 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data->ofs));
349 generate a krb5 GSS-API wrapper packet given a ticket
351 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
356 data = asn1_init(talloc_tos());
358 return data_blob_null;
361 asn1_push_tag(data, ASN1_APPLICATION(0));
362 asn1_write_OID(data, OID_KERBEROS5);
364 asn1_write(data, tok_id, 2);
365 asn1_write(data, ticket.data, ticket.length);
368 if (data->has_error) {
369 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
372 ret = data_blob(data->data, data->length);
379 parse a krb5 GSS-API wrapper packet giving a ticket
381 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
387 data = asn1_init(talloc_tos());
392 asn1_load(data, blob);
393 asn1_start_tag(data, ASN1_APPLICATION(0));
394 asn1_check_OID(data, OID_KERBEROS5);
396 data_remaining = asn1_tag_remaining(data);
398 if (data_remaining < 3) {
399 data->has_error = True;
401 asn1_read(data, tok_id, 2);
403 *ticket = data_blob(NULL, data_remaining);
404 asn1_read(data, ticket->data, ticket->length);
409 ret = !data->has_error;
411 if (data->has_error) {
412 data_blob_free(ticket);
422 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
423 kerberos session setup
425 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
427 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
431 DATA_BLOB tkt, tkt_wrapped;
432 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
434 /* get a kerberos ticket for the service and extract the session key */
435 retval = cli_krb5_get_ticket(principal, time_offset,
436 &tkt, session_key_krb5, extra_ap_opts, NULL,
442 /* wrap that up in a nice GSS-API wrapping */
443 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
445 /* and wrap that in a shiny SPNEGO wrapper */
446 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
448 data_blob_free(&tkt_wrapped);
449 data_blob_free(&tkt);
456 parse a spnego NTLMSSP challenge packet giving two security blobs
458 bool spnego_parse_challenge(const DATA_BLOB blob,
459 DATA_BLOB *chal1, DATA_BLOB *chal2)
467 data = asn1_init(talloc_tos());
472 asn1_load(data, blob);
473 asn1_start_tag(data,ASN1_CONTEXT(1));
474 asn1_start_tag(data,ASN1_SEQUENCE(0));
476 asn1_start_tag(data,ASN1_CONTEXT(0));
477 asn1_check_enumerated(data,1);
480 asn1_start_tag(data,ASN1_CONTEXT(1));
481 asn1_check_OID(data, OID_NTLMSSP);
484 asn1_start_tag(data,ASN1_CONTEXT(2));
485 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
488 /* the second challenge is optional (XP doesn't send it) */
489 if (asn1_tag_remaining(data)) {
490 asn1_start_tag(data,ASN1_CONTEXT(3));
491 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
498 ret = !data->has_error;
500 if (data->has_error) {
501 data_blob_free(chal1);
502 data_blob_free(chal2);
511 generate a SPNEGO auth packet. This will contain the encrypted passwords
513 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
518 data = asn1_init(talloc_tos());
520 return data_blob_null;
523 asn1_push_tag(data, ASN1_CONTEXT(1));
524 asn1_push_tag(data, ASN1_SEQUENCE(0));
525 asn1_push_tag(data, ASN1_CONTEXT(2));
526 asn1_write_OctetString(data,blob.data,blob.length);
531 ret = data_blob(data->data, data->length);
539 parse a SPNEGO auth packet. This contains the encrypted passwords
541 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
544 struct spnego_data token;
546 len = spnego_read_data(talloc_tos(), blob, &token);
548 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
552 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
553 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
555 spnego_free_data(&token);
559 *auth = data_blob_talloc(talloc_tos(),
560 token.negTokenTarg.responseToken.data,
561 token.negTokenTarg.responseToken.length);
562 spnego_free_data(&token);
568 generate a minimal SPNEGO response packet. Doesn't contain much.
570 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, 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());
587 return data_blob_null;
590 asn1_push_tag(data, ASN1_CONTEXT(1));
591 asn1_push_tag(data, ASN1_SEQUENCE(0));
592 asn1_push_tag(data, ASN1_CONTEXT(0));
593 asn1_write_enumerated(data, negResult);
597 asn1_push_tag(data,ASN1_CONTEXT(1));
598 asn1_write_OID(data, mechOID);
602 if (reply && reply->data != NULL) {
603 asn1_push_tag(data,ASN1_CONTEXT(2));
604 asn1_write_OctetString(data, reply->data, reply->length);
611 ret = data_blob(data->data, data->length);
617 parse a SPNEGO auth packet. This contains the encrypted passwords
619 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
626 if (NT_STATUS_IS_OK(nt_status)) {
627 negResult = SPNEGO_ACCEPT_COMPLETED;
628 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
629 negResult = SPNEGO_ACCEPT_INCOMPLETE;
631 negResult = SPNEGO_REJECT;
634 data = asn1_init(talloc_tos());
639 asn1_load(data, blob);
640 asn1_start_tag(data, ASN1_CONTEXT(1));
641 asn1_start_tag(data, ASN1_SEQUENCE(0));
642 asn1_start_tag(data, ASN1_CONTEXT(0));
643 asn1_check_enumerated(data, negResult);
646 *auth = data_blob_null;
648 if (asn1_tag_remaining(data)) {
649 asn1_start_tag(data,ASN1_CONTEXT(1));
650 asn1_check_OID(data, mechOID);
653 if (asn1_tag_remaining(data)) {
654 asn1_start_tag(data,ASN1_CONTEXT(2));
655 asn1_read_OctetString(data, talloc_autofree_context(), auth);
658 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
662 /* Binding against Win2K DC returns a duplicate of the responseToken in
663 * the optional mechListMIC field. This is a bug in Win2K. We ignore
664 * this field if it exists. Win2K8 may return a proper mechListMIC at
665 * which point we need to implement the integrity checking. */
666 if (asn1_tag_remaining(data)) {
667 DATA_BLOB mechList = data_blob_null;
668 asn1_start_tag(data, ASN1_CONTEXT(3));
669 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
671 data_blob_free(&mechList);
672 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
679 if (data->has_error) {
680 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
682 data_blob_free(auth);