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(TALLOC_CTX
*ctx
,
35 const char *principal
)
41 data
= asn1_init(talloc_tos());
43 return data_blob_null
;
46 asn1_push_tag(data
,ASN1_APPLICATION(0));
47 asn1_write_OID(data
,OID_SPNEGO
);
48 asn1_push_tag(data
,ASN1_CONTEXT(0));
49 asn1_push_tag(data
,ASN1_SEQUENCE(0));
51 asn1_push_tag(data
,ASN1_CONTEXT(0));
52 asn1_push_tag(data
,ASN1_SEQUENCE(0));
53 for (i
=0; OIDs
[i
]; i
++) {
54 asn1_write_OID(data
,OIDs
[i
]);
59 if (psecblob
&& psecblob
->length
&& psecblob
->data
) {
60 asn1_push_tag(data
, ASN1_CONTEXT(2));
61 asn1_write_OctetString(data
,psecblob
->data
,
67 asn1_push_tag(data
, ASN1_CONTEXT(3));
68 asn1_push_tag(data
, ASN1_SEQUENCE(0));
69 asn1_push_tag(data
, ASN1_CONTEXT(0));
70 asn1_write_GeneralString(data
,principal
);
81 if (data
->has_error
) {
82 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
->ofs
));
85 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
92 parse a negTokenInit packet giving a GUID, a list of supported
93 OIDs (the mechanisms) and a principal name string
95 bool spnego_parse_negTokenInit(TALLOC_CTX
*ctx
,
97 char *OIDs
[ASN1_MAX_OIDS
],
105 data
= asn1_init(talloc_tos());
110 asn1_load(data
, blob
);
112 asn1_start_tag(data
,ASN1_APPLICATION(0));
114 asn1_check_OID(data
,OID_SPNEGO
);
116 /* negTokenInit [0] NegTokenInit */
117 asn1_start_tag(data
,ASN1_CONTEXT(0));
118 asn1_start_tag(data
,ASN1_SEQUENCE(0));
120 /* mechTypes [0] MechTypeList OPTIONAL */
122 /* Not really optional, we depend on this to decide
123 * what mechanisms we have to work with. */
125 asn1_start_tag(data
,ASN1_CONTEXT(0));
126 asn1_start_tag(data
,ASN1_SEQUENCE(0));
127 for (i
=0; asn1_tag_remaining(data
) > 0 && i
< ASN1_MAX_OIDS
-1; i
++) {
128 asn1_read_OID(data
,ctx
, &OIDs
[i
]);
138 *secblob
= data_blob_null
;
142 Win7 + Live Sign-in Assistant attaches a mechToken
143 ASN1_CONTEXT(2) to the negTokenInit packet
144 which breaks our negotiation if we just assume
145 the next tag is ASN1_CONTEXT(3).
148 if (asn1_peek_tag(data
, ASN1_CONTEXT(1))) {
151 /* reqFlags [1] ContextFlags OPTIONAL */
152 asn1_start_tag(data
, ASN1_CONTEXT(1));
153 asn1_start_tag(data
, ASN1_BIT_STRING
);
154 while (asn1_tag_remaining(data
) > 0) {
155 asn1_read_uint8(data
, &flags
);
161 if (asn1_peek_tag(data
, ASN1_CONTEXT(2))) {
162 DATA_BLOB sblob
= data_blob_null
;
163 /* mechToken [2] OCTET STRING OPTIONAL */
164 asn1_start_tag(data
, ASN1_CONTEXT(2));
165 asn1_read_OctetString(data
, ctx
, &sblob
);
170 data_blob_free(&sblob
);
174 if (asn1_peek_tag(data
, ASN1_CONTEXT(3))) {
176 /* mechListMIC [3] OCTET STRING OPTIONAL */
177 asn1_start_tag(data
, ASN1_CONTEXT(3));
178 asn1_start_tag(data
, ASN1_SEQUENCE(0));
179 asn1_start_tag(data
, ASN1_CONTEXT(0));
180 asn1_read_GeneralString(data
, ctx
, &princ
);
196 ret
= !data
->has_error
;
197 if (data
->has_error
) {
200 TALLOC_FREE(*principal
);
203 data_blob_free(secblob
);
205 for(j
= 0; j
< i
&& j
< ASN1_MAX_OIDS
-1; j
++) {
206 TALLOC_FREE(OIDs
[j
]);
215 generate a krb5 GSS-API wrapper packet given a ticket
217 DATA_BLOB
spnego_gen_krb5_wrap(TALLOC_CTX
*ctx
, const DATA_BLOB ticket
, const uint8 tok_id
[2])
222 data
= asn1_init(talloc_tos());
224 return data_blob_null
;
227 asn1_push_tag(data
, ASN1_APPLICATION(0));
228 asn1_write_OID(data
, OID_KERBEROS5
);
230 asn1_write(data
, tok_id
, 2);
231 asn1_write(data
, ticket
.data
, ticket
.length
);
234 if (data
->has_error
) {
235 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
->ofs
));
238 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
245 parse a krb5 GSS-API wrapper packet giving a ticket
247 bool spnego_parse_krb5_wrap(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
253 data
= asn1_init(talloc_tos());
258 asn1_load(data
, blob
);
259 asn1_start_tag(data
, ASN1_APPLICATION(0));
260 asn1_check_OID(data
, OID_KERBEROS5
);
262 data_remaining
= asn1_tag_remaining(data
);
264 if (data_remaining
< 3) {
265 data
->has_error
= True
;
267 asn1_read(data
, tok_id
, 2);
269 *ticket
= data_blob_talloc(ctx
, NULL
, data_remaining
);
270 asn1_read(data
, ticket
->data
, ticket
->length
);
275 ret
= !data
->has_error
;
277 if (data
->has_error
) {
278 data_blob_free(ticket
);
288 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
289 kerberos session setup
291 int spnego_gen_krb5_negTokenInit(TALLOC_CTX
*ctx
,
292 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(ctx
, principal
, time_offset
,
303 &tkt
, session_key_krb5
,
310 /* wrap that up in a nice GSS-API wrapping */
311 tkt_wrapped
= spnego_gen_krb5_wrap(ctx
, tkt
, TOK_ID_KRB_AP_REQ
);
313 /* and wrap that in a shiny SPNEGO wrapper */
314 *targ
= spnego_gen_negTokenInit(ctx
, krb_mechs
, &tkt_wrapped
, NULL
);
316 data_blob_free(&tkt_wrapped
);
317 data_blob_free(&tkt
);
324 parse a spnego NTLMSSP challenge packet giving two security blobs
326 bool spnego_parse_challenge(TALLOC_CTX
*ctx
, const DATA_BLOB blob
,
327 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
335 data
= asn1_init(talloc_tos());
340 asn1_load(data
, blob
);
341 asn1_start_tag(data
,ASN1_CONTEXT(1));
342 asn1_start_tag(data
,ASN1_SEQUENCE(0));
344 asn1_start_tag(data
,ASN1_CONTEXT(0));
345 asn1_check_enumerated(data
,1);
348 asn1_start_tag(data
,ASN1_CONTEXT(1));
349 asn1_check_OID(data
, OID_NTLMSSP
);
352 asn1_start_tag(data
,ASN1_CONTEXT(2));
353 asn1_read_OctetString(data
, ctx
, chal1
);
356 /* the second challenge is optional (XP doesn't send it) */
357 if (asn1_tag_remaining(data
)) {
358 asn1_start_tag(data
,ASN1_CONTEXT(3));
359 asn1_read_OctetString(data
, ctx
, chal2
);
366 ret
= !data
->has_error
;
368 if (data
->has_error
) {
369 data_blob_free(chal1
);
370 data_blob_free(chal2
);
379 generate a SPNEGO auth packet. This will contain the encrypted passwords
381 DATA_BLOB
spnego_gen_auth(TALLOC_CTX
*ctx
, DATA_BLOB blob
)
386 data
= asn1_init(talloc_tos());
388 return data_blob_null
;
391 asn1_push_tag(data
, ASN1_CONTEXT(1));
392 asn1_push_tag(data
, ASN1_SEQUENCE(0));
393 asn1_push_tag(data
, ASN1_CONTEXT(2));
394 asn1_write_OctetString(data
,blob
.data
,blob
.length
);
399 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
407 parse a SPNEGO auth packet. This contains the encrypted passwords
409 bool spnego_parse_auth_and_mic(TALLOC_CTX
*ctx
, DATA_BLOB blob
,
410 DATA_BLOB
*auth
, DATA_BLOB
*signature
)
413 struct spnego_data token
;
415 len
= spnego_read_data(talloc_tos(), blob
, &token
);
417 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
421 if (token
.type
!= SPNEGO_NEG_TOKEN_TARG
) {
422 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
424 spnego_free_data(&token
);
428 *auth
= data_blob_talloc(ctx
,
429 token
.negTokenTarg
.responseToken
.data
,
430 token
.negTokenTarg
.responseToken
.length
);
436 *signature
= data_blob_talloc(ctx
,
437 token
.negTokenTarg
.mechListMIC
.data
,
438 token
.negTokenTarg
.mechListMIC
.length
);
441 spnego_free_data(&token
);
446 bool spnego_parse_auth(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*auth
)
448 return spnego_parse_auth_and_mic(ctx
, blob
, auth
, NULL
);
452 generate a minimal SPNEGO response packet. Doesn't contain much.
454 DATA_BLOB
spnego_gen_auth_response_and_mic(TALLOC_CTX
*ctx
,
458 DATA_BLOB
*mechlistMIC
)
464 if (NT_STATUS_IS_OK(nt_status
)) {
465 negResult
= SPNEGO_ACCEPT_COMPLETED
;
466 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
467 negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
469 negResult
= SPNEGO_REJECT
;
472 data
= asn1_init(talloc_tos());
474 return data_blob_null
;
477 asn1_push_tag(data
, ASN1_CONTEXT(1));
478 asn1_push_tag(data
, ASN1_SEQUENCE(0));
479 asn1_push_tag(data
, ASN1_CONTEXT(0));
480 asn1_write_enumerated(data
, negResult
);
484 asn1_push_tag(data
,ASN1_CONTEXT(1));
485 asn1_write_OID(data
, mechOID
);
489 if (reply
&& reply
->data
!= NULL
) {
490 asn1_push_tag(data
,ASN1_CONTEXT(2));
491 asn1_write_OctetString(data
, reply
->data
, reply
->length
);
495 if (mechlistMIC
&& mechlistMIC
->data
!= NULL
) {
496 asn1_push_tag(data
, ASN1_CONTEXT(3));
497 asn1_write_OctetString(data
,
499 mechlistMIC
->length
);
506 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
511 DATA_BLOB
spnego_gen_auth_response(TALLOC_CTX
*ctx
, DATA_BLOB
*reply
,
512 NTSTATUS nt_status
, const char *mechOID
)
514 return spnego_gen_auth_response_and_mic(ctx
, nt_status
,
515 mechOID
, reply
, NULL
);
519 parse a SPNEGO auth packet. This contains the encrypted passwords
521 bool spnego_parse_auth_response(TALLOC_CTX
*ctx
,
522 DATA_BLOB blob
, NTSTATUS nt_status
,
529 if (NT_STATUS_IS_OK(nt_status
)) {
530 negResult
= SPNEGO_ACCEPT_COMPLETED
;
531 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
532 negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
534 negResult
= SPNEGO_REJECT
;
537 data
= asn1_init(talloc_tos());
542 asn1_load(data
, blob
);
543 asn1_start_tag(data
, ASN1_CONTEXT(1));
544 asn1_start_tag(data
, ASN1_SEQUENCE(0));
545 asn1_start_tag(data
, ASN1_CONTEXT(0));
546 asn1_check_enumerated(data
, negResult
);
549 *auth
= data_blob_null
;
551 if (asn1_tag_remaining(data
)) {
552 asn1_start_tag(data
,ASN1_CONTEXT(1));
553 asn1_check_OID(data
, mechOID
);
556 if (asn1_tag_remaining(data
)) {
557 asn1_start_tag(data
,ASN1_CONTEXT(2));
558 asn1_read_OctetString(data
, ctx
, auth
);
561 } else if (negResult
== SPNEGO_ACCEPT_INCOMPLETE
) {
565 /* Binding against Win2K DC returns a duplicate of the responseToken in
566 * the optional mechListMIC field. This is a bug in Win2K. We ignore
567 * this field if it exists. Win2K8 may return a proper mechListMIC at
568 * which point we need to implement the integrity checking. */
569 if (asn1_tag_remaining(data
)) {
570 DATA_BLOB mechList
= data_blob_null
;
571 asn1_start_tag(data
, ASN1_CONTEXT(3));
572 asn1_read_OctetString(data
, ctx
, &mechList
);
574 data_blob_free(&mechList
);
575 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
582 if (data
->has_error
) {
583 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data
->ofs
));
585 data_blob_free(auth
);
593 bool spnego_mech_list_blob(TALLOC_CTX
*mem_ctx
,
594 char **oid_list
, DATA_BLOB
*raw_data
)
599 if (!oid_list
|| !oid_list
[0] || !raw_data
) {
603 data
= asn1_init(talloc_tos());
608 asn1_push_tag(data
, ASN1_SEQUENCE(0));
609 for (idx
= 0; oid_list
[idx
]; idx
++) {
610 asn1_write_OID(data
, oid_list
[idx
]);
614 if (data
->has_error
) {
615 DEBUG(3, (__location__
" failed at %d\n", (int)data
->ofs
));
620 *raw_data
= data_blob_talloc(mem_ctx
, data
->data
, data
->length
);
621 if (!raw_data
->data
) {
622 DEBUG(3, (__location__
": data_blob_talloc() failed!\n"));