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"
26 #include "../lib/util/asn1.h"
29 generate a negTokenInit packet given a list of supported
30 OIDs (the mechanisms) a blob, and a principal name string
33 DATA_BLOB
spnego_gen_negTokenInit(TALLOC_CTX
*ctx
,
36 const char *principal
)
42 data
= asn1_init(talloc_tos());
44 return data_blob_null
;
47 asn1_push_tag(data
,ASN1_APPLICATION(0));
48 asn1_write_OID(data
,OID_SPNEGO
);
49 asn1_push_tag(data
,ASN1_CONTEXT(0));
50 asn1_push_tag(data
,ASN1_SEQUENCE(0));
52 asn1_push_tag(data
,ASN1_CONTEXT(0));
53 asn1_push_tag(data
,ASN1_SEQUENCE(0));
54 for (i
=0; OIDs
[i
]; i
++) {
55 asn1_write_OID(data
,OIDs
[i
]);
60 if (psecblob
&& psecblob
->length
&& psecblob
->data
) {
61 asn1_push_tag(data
, ASN1_CONTEXT(2));
62 asn1_write_OctetString(data
,psecblob
->data
,
68 asn1_push_tag(data
, ASN1_CONTEXT(3));
69 asn1_push_tag(data
, ASN1_SEQUENCE(0));
70 asn1_push_tag(data
, ASN1_CONTEXT(0));
71 asn1_write_GeneralString(data
,principal
);
82 if (data
->has_error
) {
83 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
->ofs
));
86 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
93 parse a negTokenInit packet giving a GUID, a list of supported
94 OIDs (the mechanisms) and a principal name string
96 bool spnego_parse_negTokenInit(TALLOC_CTX
*ctx
,
98 char *OIDs
[ASN1_MAX_OIDS
],
106 for (i
= 0; i
< ASN1_MAX_OIDS
; i
++) {
110 data
= asn1_init(talloc_tos());
115 asn1_load(data
, blob
);
117 asn1_start_tag(data
,ASN1_APPLICATION(0));
119 asn1_check_OID(data
,OID_SPNEGO
);
121 /* negTokenInit [0] NegTokenInit */
122 asn1_start_tag(data
,ASN1_CONTEXT(0));
123 asn1_start_tag(data
,ASN1_SEQUENCE(0));
125 /* mechTypes [0] MechTypeList OPTIONAL */
127 /* Not really optional, we depend on this to decide
128 * what mechanisms we have to work with. */
130 asn1_start_tag(data
,ASN1_CONTEXT(0));
131 asn1_start_tag(data
,ASN1_SEQUENCE(0));
132 for (i
=0; asn1_tag_remaining(data
) > 0 && i
< ASN1_MAX_OIDS
-1; i
++) {
133 asn1_read_OID(data
,ctx
, &OIDs
[i
]);
134 if (data
->has_error
) {
146 *secblob
= data_blob_null
;
150 Win7 + Live Sign-in Assistant attaches a mechToken
151 ASN1_CONTEXT(2) to the negTokenInit packet
152 which breaks our negotiation if we just assume
153 the next tag is ASN1_CONTEXT(3).
156 if (asn1_peek_tag(data
, ASN1_CONTEXT(1))) {
159 /* reqFlags [1] ContextFlags OPTIONAL */
160 asn1_start_tag(data
, ASN1_CONTEXT(1));
161 asn1_start_tag(data
, ASN1_BIT_STRING
);
162 while (asn1_tag_remaining(data
) > 0) {
163 asn1_read_uint8(data
, &flags
);
169 if (asn1_peek_tag(data
, ASN1_CONTEXT(2))) {
170 DATA_BLOB sblob
= data_blob_null
;
171 /* mechToken [2] OCTET STRING OPTIONAL */
172 asn1_start_tag(data
, ASN1_CONTEXT(2));
173 asn1_read_OctetString(data
, ctx
, &sblob
);
178 data_blob_free(&sblob
);
182 if (asn1_peek_tag(data
, ASN1_CONTEXT(3))) {
184 /* mechListMIC [3] OCTET STRING OPTIONAL */
185 asn1_start_tag(data
, ASN1_CONTEXT(3));
186 asn1_start_tag(data
, ASN1_SEQUENCE(0));
187 asn1_start_tag(data
, ASN1_CONTEXT(0));
188 asn1_read_GeneralString(data
, ctx
, &princ
);
204 ret
= !data
->has_error
;
205 if (data
->has_error
) {
208 TALLOC_FREE(*principal
);
211 data_blob_free(secblob
);
213 for(j
= 0; j
< i
&& j
< ASN1_MAX_OIDS
-1; j
++) {
214 TALLOC_FREE(OIDs
[j
]);
223 generate a krb5 GSS-API wrapper packet given a ticket
225 DATA_BLOB
spnego_gen_krb5_wrap(TALLOC_CTX
*ctx
, const DATA_BLOB ticket
, const uint8 tok_id
[2])
230 data
= asn1_init(talloc_tos());
232 return data_blob_null
;
235 asn1_push_tag(data
, ASN1_APPLICATION(0));
236 asn1_write_OID(data
, OID_KERBEROS5
);
238 asn1_write(data
, tok_id
, 2);
239 asn1_write(data
, ticket
.data
, ticket
.length
);
242 if (data
->has_error
) {
243 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
->ofs
));
246 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
253 parse a krb5 GSS-API wrapper packet giving a ticket
255 bool spnego_parse_krb5_wrap(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
260 *ticket
= data_blob_null
;
262 data
= asn1_init(talloc_tos());
267 asn1_load(data
, blob
);
268 asn1_start_tag(data
, ASN1_APPLICATION(0));
269 asn1_check_OID(data
, OID_KERBEROS5
);
271 data_remaining
= asn1_tag_remaining(data
);
273 if (data_remaining
< 3) {
274 data
->has_error
= True
;
276 asn1_read(data
, tok_id
, 2);
278 *ticket
= data_blob_talloc(ctx
, NULL
, data_remaining
);
279 asn1_read(data
, ticket
->data
, ticket
->length
);
284 ret
= !data
->has_error
;
286 if (data
->has_error
) {
287 data_blob_free(ticket
);
297 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
298 kerberos session setup
300 int spnego_gen_krb5_negTokenInit(TALLOC_CTX
*ctx
,
301 const char *principal
, int time_offset
,
303 DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
307 DATA_BLOB tkt
, tkt_wrapped
;
308 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_KERBEROS5
, OID_NTLMSSP
, NULL
};
310 /* get a kerberos ticket for the service and extract the session key */
311 retval
= cli_krb5_get_ticket(ctx
, principal
, time_offset
,
312 &tkt
, session_key_krb5
,
319 /* wrap that up in a nice GSS-API wrapping */
320 tkt_wrapped
= spnego_gen_krb5_wrap(ctx
, tkt
, TOK_ID_KRB_AP_REQ
);
322 /* and wrap that in a shiny SPNEGO wrapper */
323 *targ
= spnego_gen_negTokenInit(ctx
, krb_mechs
, &tkt_wrapped
, NULL
);
325 data_blob_free(&tkt_wrapped
);
326 data_blob_free(&tkt
);
333 parse a spnego NTLMSSP challenge packet giving two security blobs
335 bool spnego_parse_challenge(TALLOC_CTX
*ctx
, const DATA_BLOB blob
,
336 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
344 data
= asn1_init(talloc_tos());
349 asn1_load(data
, blob
);
350 asn1_start_tag(data
,ASN1_CONTEXT(1));
351 asn1_start_tag(data
,ASN1_SEQUENCE(0));
353 asn1_start_tag(data
,ASN1_CONTEXT(0));
354 asn1_check_enumerated(data
,1);
357 asn1_start_tag(data
,ASN1_CONTEXT(1));
358 asn1_check_OID(data
, OID_NTLMSSP
);
361 asn1_start_tag(data
,ASN1_CONTEXT(2));
362 asn1_read_OctetString(data
, ctx
, chal1
);
365 /* the second challenge is optional (XP doesn't send it) */
366 if (asn1_tag_remaining(data
)) {
367 asn1_start_tag(data
,ASN1_CONTEXT(3));
368 asn1_read_OctetString(data
, ctx
, chal2
);
375 ret
= !data
->has_error
;
377 if (data
->has_error
) {
378 data_blob_free(chal1
);
379 data_blob_free(chal2
);
388 generate a SPNEGO auth packet. This will contain the encrypted passwords
390 DATA_BLOB
spnego_gen_auth(TALLOC_CTX
*ctx
, DATA_BLOB blob
)
395 data
= asn1_init(talloc_tos());
397 return data_blob_null
;
400 asn1_push_tag(data
, ASN1_CONTEXT(1));
401 asn1_push_tag(data
, ASN1_SEQUENCE(0));
402 asn1_push_tag(data
, ASN1_CONTEXT(2));
403 asn1_write_OctetString(data
,blob
.data
,blob
.length
);
408 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
416 parse a SPNEGO auth packet. This contains the encrypted passwords
418 bool spnego_parse_auth_and_mic(TALLOC_CTX
*ctx
, DATA_BLOB blob
,
419 DATA_BLOB
*auth
, DATA_BLOB
*signature
)
422 struct spnego_data token
;
424 len
= spnego_read_data(talloc_tos(), blob
, &token
);
426 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
430 if (token
.type
!= SPNEGO_NEG_TOKEN_TARG
) {
431 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
433 spnego_free_data(&token
);
437 *auth
= data_blob_talloc(ctx
,
438 token
.negTokenTarg
.responseToken
.data
,
439 token
.negTokenTarg
.responseToken
.length
);
445 *signature
= data_blob_talloc(ctx
,
446 token
.negTokenTarg
.mechListMIC
.data
,
447 token
.negTokenTarg
.mechListMIC
.length
);
450 spnego_free_data(&token
);
455 bool spnego_parse_auth(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*auth
)
457 return spnego_parse_auth_and_mic(ctx
, blob
, auth
, NULL
);
461 generate a minimal SPNEGO response packet. Doesn't contain much.
463 DATA_BLOB
spnego_gen_auth_response_and_mic(TALLOC_CTX
*ctx
,
467 DATA_BLOB
*mechlistMIC
)
473 if (NT_STATUS_IS_OK(nt_status
)) {
474 negResult
= SPNEGO_ACCEPT_COMPLETED
;
475 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
476 negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
478 negResult
= SPNEGO_REJECT
;
481 data
= asn1_init(talloc_tos());
483 return data_blob_null
;
486 asn1_push_tag(data
, ASN1_CONTEXT(1));
487 asn1_push_tag(data
, ASN1_SEQUENCE(0));
488 asn1_push_tag(data
, ASN1_CONTEXT(0));
489 asn1_write_enumerated(data
, negResult
);
493 asn1_push_tag(data
,ASN1_CONTEXT(1));
494 asn1_write_OID(data
, mechOID
);
498 if (reply
&& reply
->data
!= NULL
) {
499 asn1_push_tag(data
,ASN1_CONTEXT(2));
500 asn1_write_OctetString(data
, reply
->data
, reply
->length
);
504 if (mechlistMIC
&& mechlistMIC
->data
!= NULL
) {
505 asn1_push_tag(data
, ASN1_CONTEXT(3));
506 asn1_write_OctetString(data
,
508 mechlistMIC
->length
);
515 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
520 DATA_BLOB
spnego_gen_auth_response(TALLOC_CTX
*ctx
, DATA_BLOB
*reply
,
521 NTSTATUS nt_status
, const char *mechOID
)
523 return spnego_gen_auth_response_and_mic(ctx
, nt_status
,
524 mechOID
, reply
, NULL
);
528 parse a SPNEGO auth packet. This contains the encrypted passwords
530 bool spnego_parse_auth_response(TALLOC_CTX
*ctx
,
531 DATA_BLOB blob
, NTSTATUS nt_status
,
538 if (NT_STATUS_IS_OK(nt_status
)) {
539 negResult
= SPNEGO_ACCEPT_COMPLETED
;
540 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
541 negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
543 negResult
= SPNEGO_REJECT
;
546 data
= asn1_init(talloc_tos());
551 asn1_load(data
, blob
);
552 asn1_start_tag(data
, ASN1_CONTEXT(1));
553 asn1_start_tag(data
, ASN1_SEQUENCE(0));
554 asn1_start_tag(data
, ASN1_CONTEXT(0));
555 asn1_check_enumerated(data
, negResult
);
558 *auth
= data_blob_null
;
560 if (asn1_tag_remaining(data
)) {
561 asn1_start_tag(data
,ASN1_CONTEXT(1));
562 asn1_check_OID(data
, mechOID
);
565 if (asn1_tag_remaining(data
)) {
566 asn1_start_tag(data
,ASN1_CONTEXT(2));
567 asn1_read_OctetString(data
, ctx
, auth
);
570 } else if (negResult
== SPNEGO_ACCEPT_INCOMPLETE
) {
574 /* Binding against Win2K DC returns a duplicate of the responseToken in
575 * the optional mechListMIC field. This is a bug in Win2K. We ignore
576 * this field if it exists. Win2K8 may return a proper mechListMIC at
577 * which point we need to implement the integrity checking. */
578 if (asn1_tag_remaining(data
)) {
579 DATA_BLOB mechList
= data_blob_null
;
580 asn1_start_tag(data
, ASN1_CONTEXT(3));
581 asn1_read_OctetString(data
, ctx
, &mechList
);
583 data_blob_free(&mechList
);
584 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
591 if (data
->has_error
) {
592 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data
->ofs
));
594 data_blob_free(auth
);
602 bool spnego_mech_list_blob(TALLOC_CTX
*mem_ctx
,
603 char **oid_list
, DATA_BLOB
*raw_data
)
608 if (!oid_list
|| !oid_list
[0] || !raw_data
) {
612 data
= asn1_init(talloc_tos());
617 asn1_push_tag(data
, ASN1_SEQUENCE(0));
618 for (idx
= 0; oid_list
[idx
]; idx
++) {
619 asn1_write_OID(data
, oid_list
[idx
]);
623 if (data
->has_error
) {
624 DEBUG(3, (__location__
" failed at %d\n", (int)data
->ofs
));
629 *raw_data
= data_blob_talloc(mem_ctx
, data
->data
, data
->length
);
630 if (!raw_data
->data
) {
631 DEBUG(3, (__location__
": data_blob_talloc() failed!\n"));