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
)
40 DATA_BLOB ret
= data_blob_null
;
42 data
= asn1_init(talloc_tos());
44 return data_blob_null
;
47 if (!asn1_push_tag(data
,ASN1_APPLICATION(0))) goto err
;
48 if (!asn1_write_OID(data
,OID_SPNEGO
)) goto err
;
49 if (!asn1_push_tag(data
,ASN1_CONTEXT(0))) goto err
;
50 if (!asn1_push_tag(data
,ASN1_SEQUENCE(0))) goto err
;
52 if (!asn1_push_tag(data
,ASN1_CONTEXT(0))) goto err
;
53 if (!asn1_push_tag(data
,ASN1_SEQUENCE(0))) goto err
;
54 for (i
=0; OIDs
[i
]; i
++) {
55 if (!asn1_write_OID(data
,OIDs
[i
])) goto err
;
57 if (!asn1_pop_tag(data
)) goto err
;
58 if (!asn1_pop_tag(data
)) goto err
;
60 if (psecblob
&& psecblob
->length
&& psecblob
->data
) {
61 if (!asn1_push_tag(data
, ASN1_CONTEXT(2))) goto err
;
62 if (!asn1_write_OctetString(data
,psecblob
->data
,
63 psecblob
->length
)) goto err
;
64 if (!asn1_pop_tag(data
)) goto err
;
68 if (!asn1_push_tag(data
, ASN1_CONTEXT(3))) goto err
;
69 if (!asn1_push_tag(data
, ASN1_SEQUENCE(0))) goto err
;
70 if (!asn1_push_tag(data
, ASN1_CONTEXT(0))) goto err
;
71 if (!asn1_write_GeneralString(data
,principal
)) goto err
;
72 if (!asn1_pop_tag(data
)) goto err
;
73 if (!asn1_pop_tag(data
)) goto err
;
74 if (!asn1_pop_tag(data
)) goto err
;
77 if (!asn1_pop_tag(data
)) goto err
;
78 if (!asn1_pop_tag(data
)) goto err
;
80 if (!asn1_pop_tag(data
)) goto err
;
82 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
86 if (data
->has_error
) {
87 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
->ofs
));
96 parse a negTokenInit packet giving a GUID, a list of supported
97 OIDs (the mechanisms) and a principal name string
99 bool spnego_parse_negTokenInit(TALLOC_CTX
*ctx
,
101 char *OIDs
[ASN1_MAX_OIDS
],
109 for (i
= 0; i
< ASN1_MAX_OIDS
; i
++) {
117 *secblob
= data_blob_null
;
120 data
= asn1_init(talloc_tos());
125 if (!asn1_load(data
, blob
)) goto err
;
127 if (!asn1_start_tag(data
,ASN1_APPLICATION(0))) goto err
;
129 if (!asn1_check_OID(data
,OID_SPNEGO
)) goto err
;
131 /* negTokenInit [0] NegTokenInit */
132 if (!asn1_start_tag(data
,ASN1_CONTEXT(0))) goto err
;
133 if (!asn1_start_tag(data
,ASN1_SEQUENCE(0))) goto err
;
135 /* mechTypes [0] MechTypeList OPTIONAL */
137 /* Not really optional, we depend on this to decide
138 * what mechanisms we have to work with. */
140 if (!asn1_start_tag(data
,ASN1_CONTEXT(0))) goto err
;
141 if (!asn1_start_tag(data
,ASN1_SEQUENCE(0))) goto err
;
142 for (i
=0; asn1_tag_remaining(data
) > 0 && i
< ASN1_MAX_OIDS
-1; i
++) {
143 if (!asn1_read_OID(data
,ctx
, &OIDs
[i
])) {
146 if (data
->has_error
) {
151 if (!asn1_end_tag(data
)) goto err
;
152 if (!asn1_end_tag(data
)) goto err
;
155 Win7 + Live Sign-in Assistant attaches a mechToken
156 ASN1_CONTEXT(2) to the negTokenInit packet
157 which breaks our negotiation if we just assume
158 the next tag is ASN1_CONTEXT(3).
161 if (asn1_peek_tag(data
, ASN1_CONTEXT(1))) {
164 /* reqFlags [1] ContextFlags OPTIONAL */
165 if (!asn1_start_tag(data
, ASN1_CONTEXT(1))) goto err
;
166 if (!asn1_start_tag(data
, ASN1_BIT_STRING
)) goto err
;
167 while (asn1_tag_remaining(data
) > 0) {
168 if (!asn1_read_uint8(data
, &flags
)) goto err
;
170 if (!asn1_end_tag(data
)) goto err
;
171 if (!asn1_end_tag(data
)) goto err
;
174 if (asn1_peek_tag(data
, ASN1_CONTEXT(2))) {
175 DATA_BLOB sblob
= data_blob_null
;
176 /* mechToken [2] OCTET STRING OPTIONAL */
177 if (!asn1_start_tag(data
, ASN1_CONTEXT(2))) goto err
;
178 if (!asn1_read_OctetString(data
, ctx
, &sblob
)) goto err
;
179 if (!asn1_end_tag(data
)) {
180 data_blob_free(&sblob
);
186 data_blob_free(&sblob
);
190 if (asn1_peek_tag(data
, ASN1_CONTEXT(3))) {
192 /* mechListMIC [3] OCTET STRING OPTIONAL */
193 if (!asn1_start_tag(data
, ASN1_CONTEXT(3))) goto err
;
194 if (!asn1_start_tag(data
, ASN1_SEQUENCE(0))) goto err
;
195 if (!asn1_start_tag(data
, ASN1_CONTEXT(0))) goto err
;
196 if (!asn1_read_GeneralString(data
, ctx
, &princ
)) goto err
;
197 if (!asn1_end_tag(data
)) goto err
;
198 if (!asn1_end_tag(data
)) goto err
;
199 if (!asn1_end_tag(data
)) goto err
;
207 if (!asn1_end_tag(data
)) goto err
;
208 if (!asn1_end_tag(data
)) goto err
;
210 if (!asn1_end_tag(data
)) goto err
;
212 ret
= !data
->has_error
;
216 if (data
->has_error
) {
219 TALLOC_FREE(*principal
);
222 data_blob_free(secblob
);
224 for(j
= 0; j
< i
&& j
< ASN1_MAX_OIDS
-1; j
++) {
225 TALLOC_FREE(OIDs
[j
]);
234 generate a krb5 GSS-API wrapper packet given a ticket
236 DATA_BLOB
spnego_gen_krb5_wrap(TALLOC_CTX
*ctx
, const DATA_BLOB ticket
, const uint8_t tok_id
[2])
239 DATA_BLOB ret
= data_blob_null
;
241 data
= asn1_init(talloc_tos());
243 return data_blob_null
;
246 if (!asn1_push_tag(data
, ASN1_APPLICATION(0))) goto err
;
247 if (!asn1_write_OID(data
, OID_KERBEROS5
)) goto err
;
249 if (!asn1_write(data
, tok_id
, 2)) goto err
;
250 if (!asn1_write(data
, ticket
.data
, ticket
.length
)) goto err
;
251 if (!asn1_pop_tag(data
)) goto err
;
253 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
257 if (data
->has_error
) {
258 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
->ofs
));
267 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
268 kerberos session setup
270 int spnego_gen_krb5_negTokenInit(TALLOC_CTX
*ctx
,
271 const char *principal
, int time_offset
,
273 DATA_BLOB
*session_key_krb5
, uint32_t extra_ap_opts
,
274 const char *ccname
, time_t *expire_time
)
277 DATA_BLOB tkt
, tkt_wrapped
;
278 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_KERBEROS5
, OID_NTLMSSP
, NULL
};
280 /* get a kerberos ticket for the service and extract the session key */
281 retval
= cli_krb5_get_ticket(ctx
, principal
, time_offset
,
282 &tkt
, session_key_krb5
,
283 extra_ap_opts
, ccname
,
289 /* wrap that up in a nice GSS-API wrapping */
290 tkt_wrapped
= spnego_gen_krb5_wrap(ctx
, tkt
, TOK_ID_KRB_AP_REQ
);
292 /* and wrap that in a shiny SPNEGO wrapper */
293 *targ
= spnego_gen_negTokenInit(ctx
, krb_mechs
, &tkt_wrapped
, NULL
);
295 data_blob_free(&tkt_wrapped
);
296 data_blob_free(&tkt
);
303 parse a spnego NTLMSSP challenge packet giving two security blobs
305 bool spnego_parse_challenge(TALLOC_CTX
*ctx
, const DATA_BLOB blob
,
306 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
314 data
= asn1_init(talloc_tos());
319 if (!asn1_load(data
, blob
)) goto err
;
320 if (!asn1_start_tag(data
,ASN1_CONTEXT(1))) goto err
;
321 if (!asn1_start_tag(data
,ASN1_SEQUENCE(0))) goto err
;
323 if (!asn1_start_tag(data
,ASN1_CONTEXT(0))) goto err
;
324 if (!asn1_check_enumerated(data
,1)) goto err
;
325 if (!asn1_end_tag(data
)) goto err
;
327 if (!asn1_start_tag(data
,ASN1_CONTEXT(1))) goto err
;
328 if (!asn1_check_OID(data
, OID_NTLMSSP
)) goto err
;
329 if (!asn1_end_tag(data
)) goto err
;
331 if (!asn1_start_tag(data
,ASN1_CONTEXT(2))) goto err
;
332 if (!asn1_read_OctetString(data
, ctx
, chal1
)) goto err
;
333 if (!asn1_end_tag(data
)) goto err
;
335 /* the second challenge is optional (XP doesn't send it) */
336 if (asn1_tag_remaining(data
)) {
337 if (!asn1_start_tag(data
,ASN1_CONTEXT(3))) goto err
;
338 if (!asn1_read_OctetString(data
, ctx
, chal2
)) goto err
;
339 if (!asn1_end_tag(data
)) goto err
;
342 if (!asn1_end_tag(data
)) goto err
;
343 if (!asn1_end_tag(data
)) goto err
;
345 ret
= !data
->has_error
;
349 if (data
->has_error
) {
350 data_blob_free(chal1
);
351 data_blob_free(chal2
);
360 generate a SPNEGO auth packet. This will contain the encrypted passwords
362 DATA_BLOB
spnego_gen_auth(TALLOC_CTX
*ctx
, DATA_BLOB blob
)
365 DATA_BLOB ret
= data_blob_null
;
367 data
= asn1_init(talloc_tos());
369 return data_blob_null
;
372 if (!asn1_push_tag(data
, ASN1_CONTEXT(1))) goto err
;
373 if (!asn1_push_tag(data
, ASN1_SEQUENCE(0))) goto err
;
374 if (!asn1_push_tag(data
, ASN1_CONTEXT(2))) goto err
;
375 if (!asn1_write_OctetString(data
,blob
.data
,blob
.length
)) goto err
;
376 if (!asn1_pop_tag(data
)) goto err
;
377 if (!asn1_pop_tag(data
)) goto err
;
378 if (!asn1_pop_tag(data
)) goto err
;
380 ret
= data_blob_talloc(ctx
, data
->data
, data
->length
);
390 parse a SPNEGO auth packet. This contains the encrypted passwords
392 bool spnego_parse_auth_response(TALLOC_CTX
*ctx
,
393 DATA_BLOB blob
, NTSTATUS nt_status
,
401 if (NT_STATUS_IS_OK(nt_status
)) {
402 negResult
= SPNEGO_ACCEPT_COMPLETED
;
403 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
404 negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
406 negResult
= SPNEGO_REJECT
;
409 data
= asn1_init(talloc_tos());
414 *auth
= data_blob_null
;
416 if (!asn1_load(data
, blob
)) goto err
;
417 if (!asn1_start_tag(data
, ASN1_CONTEXT(1))) goto err
;
418 if (!asn1_start_tag(data
, ASN1_SEQUENCE(0))) goto err
;
419 if (!asn1_start_tag(data
, ASN1_CONTEXT(0))) goto err
;
420 if (!asn1_check_enumerated(data
, negResult
)) goto err
;
421 if (!asn1_end_tag(data
)) goto err
;
423 if (asn1_tag_remaining(data
)) {
424 if (!asn1_start_tag(data
,ASN1_CONTEXT(1))) goto err
;
425 if (!asn1_check_OID(data
, mechOID
)) goto err
;
426 if (!asn1_end_tag(data
)) goto err
;
428 if (asn1_tag_remaining(data
)) {
429 if (!asn1_start_tag(data
,ASN1_CONTEXT(2))) goto err
;
430 if (!asn1_read_OctetString(data
, ctx
, auth
)) goto err
;
431 if (!asn1_end_tag(data
)) goto err
;
433 } else if (negResult
== SPNEGO_ACCEPT_INCOMPLETE
) {
438 /* Binding against Win2K DC returns a duplicate of the responseToken in
439 * the optional mechListMIC field. This is a bug in Win2K. We ignore
440 * this field if it exists. Win2K8 may return a proper mechListMIC at
441 * which point we need to implement the integrity checking. */
442 if (asn1_tag_remaining(data
)) {
443 DATA_BLOB mechList
= data_blob_null
;
444 if (!asn1_start_tag(data
, ASN1_CONTEXT(3))) goto err
;
445 if (!asn1_read_OctetString(data
, ctx
, &mechList
)) goto err
;
446 data_blob_free(&mechList
);
447 if (!asn1_end_tag(data
)) goto err
;
448 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
452 if (!asn1_end_tag(data
)) goto err
;
453 if (!asn1_end_tag(data
)) goto err
;
455 ret
= !data
->has_error
;
459 if (data
->has_error
) {
460 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data
->ofs
));
462 data_blob_free(auth
);