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])
391 data = asn1_init(talloc_tos());
396 asn1_load(data, blob);
397 asn1_start_tag(data, ASN1_APPLICATION(0));
398 asn1_check_OID(data, OID_KERBEROS5);
400 data_remaining = asn1_tag_remaining(data);
402 if (data_remaining < 3) {
403 data->has_error = True;
405 asn1_read(data, tok_id, 2);
407 *ticket = data_blob(NULL, data_remaining);
408 asn1_read(data, ticket->data, ticket->length);
413 ret = !data->has_error;
415 if (data->has_error) {
416 data_blob_free(ticket);
426 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
427 kerberos session setup
429 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
431 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
435 DATA_BLOB tkt, tkt_wrapped;
436 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
438 /* get a kerberos ticket for the service and extract the session key */
439 retval = cli_krb5_get_ticket(principal, time_offset,
440 &tkt, session_key_krb5, extra_ap_opts, NULL,
446 /* wrap that up in a nice GSS-API wrapping */
447 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
449 /* and wrap that in a shiny SPNEGO wrapper */
450 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
452 data_blob_free(&tkt_wrapped);
453 data_blob_free(&tkt);
460 parse a spnego NTLMSSP challenge packet giving two security blobs
462 bool spnego_parse_challenge(const DATA_BLOB blob,
463 DATA_BLOB *chal1, DATA_BLOB *chal2)
471 data = asn1_init(talloc_tos());
476 asn1_load(data, blob);
477 asn1_start_tag(data,ASN1_CONTEXT(1));
478 asn1_start_tag(data,ASN1_SEQUENCE(0));
480 asn1_start_tag(data,ASN1_CONTEXT(0));
481 asn1_check_enumerated(data,1);
484 asn1_start_tag(data,ASN1_CONTEXT(1));
485 asn1_check_OID(data, OID_NTLMSSP);
488 asn1_start_tag(data,ASN1_CONTEXT(2));
489 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
492 /* the second challenge is optional (XP doesn't send it) */
493 if (asn1_tag_remaining(data)) {
494 asn1_start_tag(data,ASN1_CONTEXT(3));
495 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
502 ret = !data->has_error;
504 if (data->has_error) {
505 data_blob_free(chal1);
506 data_blob_free(chal2);
515 generate a SPNEGO auth packet. This will contain the encrypted passwords
517 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
522 data = asn1_init(talloc_tos());
524 return data_blob_null;
527 asn1_push_tag(data, ASN1_CONTEXT(1));
528 asn1_push_tag(data, ASN1_SEQUENCE(0));
529 asn1_push_tag(data, ASN1_CONTEXT(2));
530 asn1_write_OctetString(data,blob.data,blob.length);
535 ret = data_blob(data->data, data->length);
543 parse a SPNEGO auth packet. This contains the encrypted passwords
545 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
548 struct spnego_data token;
550 len = spnego_read_data(talloc_tos(), blob, &token);
552 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
556 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
557 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
559 spnego_free_data(&token);
563 *auth = data_blob_talloc(talloc_tos(),
564 token.negTokenTarg.responseToken.data,
565 token.negTokenTarg.responseToken.length);
566 spnego_free_data(&token);
572 generate a minimal SPNEGO response packet. Doesn't contain much.
574 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
581 if (NT_STATUS_IS_OK(nt_status)) {
582 negResult = SPNEGO_ACCEPT_COMPLETED;
583 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
584 negResult = SPNEGO_ACCEPT_INCOMPLETE;
586 negResult = SPNEGO_REJECT;
589 data = asn1_init(talloc_tos());
591 return data_blob_null;
594 asn1_push_tag(data, ASN1_CONTEXT(1));
595 asn1_push_tag(data, ASN1_SEQUENCE(0));
596 asn1_push_tag(data, ASN1_CONTEXT(0));
597 asn1_write_enumerated(data, negResult);
601 asn1_push_tag(data,ASN1_CONTEXT(1));
602 asn1_write_OID(data, mechOID);
606 if (reply && reply->data != NULL) {
607 asn1_push_tag(data,ASN1_CONTEXT(2));
608 asn1_write_OctetString(data, reply->data, reply->length);
615 ret = data_blob(data->data, data->length);
621 parse a SPNEGO auth packet. This contains the encrypted passwords
623 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
630 if (NT_STATUS_IS_OK(nt_status)) {
631 negResult = SPNEGO_ACCEPT_COMPLETED;
632 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
633 negResult = SPNEGO_ACCEPT_INCOMPLETE;
635 negResult = SPNEGO_REJECT;
638 data = asn1_init(talloc_tos());
643 asn1_load(data, blob);
644 asn1_start_tag(data, ASN1_CONTEXT(1));
645 asn1_start_tag(data, ASN1_SEQUENCE(0));
646 asn1_start_tag(data, ASN1_CONTEXT(0));
647 asn1_check_enumerated(data, negResult);
650 *auth = data_blob_null;
652 if (asn1_tag_remaining(data)) {
653 asn1_start_tag(data,ASN1_CONTEXT(1));
654 asn1_check_OID(data, mechOID);
657 if (asn1_tag_remaining(data)) {
658 asn1_start_tag(data,ASN1_CONTEXT(2));
659 asn1_read_OctetString(data, talloc_autofree_context(), auth);
662 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
666 /* Binding against Win2K DC returns a duplicate of the responseToken in
667 * the optional mechListMIC field. This is a bug in Win2K. We ignore
668 * this field if it exists. Win2K8 may return a proper mechListMIC at
669 * which point we need to implement the integrity checking. */
670 if (asn1_tag_remaining(data)) {
671 DATA_BLOB mechList = data_blob_null;
672 asn1_start_tag(data, ASN1_CONTEXT(3));
673 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
675 data_blob_free(&mechList);
676 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
683 if (data->has_error) {
684 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
686 data_blob_free(auth);