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/>.
25 generate a negTokenInit packet given a GUID, a list of supported
26 OIDs (the mechanisms) and a principal name string
28 DATA_BLOB
spnego_gen_negTokenInit(char guid
[16],
30 const char *principal
)
36 memset(&data
, 0, sizeof(data
));
38 asn1_write(&data
, guid
, 16);
39 asn1_push_tag(&data
,ASN1_APPLICATION(0));
40 asn1_write_OID(&data
,OID_SPNEGO
);
41 asn1_push_tag(&data
,ASN1_CONTEXT(0));
42 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
44 asn1_push_tag(&data
,ASN1_CONTEXT(0));
45 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
46 for (i
=0; OIDs
[i
]; i
++) {
47 asn1_write_OID(&data
,OIDs
[i
]);
52 asn1_push_tag(&data
, ASN1_CONTEXT(3));
53 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
54 asn1_push_tag(&data
, ASN1_CONTEXT(0));
55 asn1_write_GeneralString(&data
,principal
);
66 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
.ofs
));
70 ret
= data_blob(data
.data
, data
.length
);
77 Generate a negTokenInit as used by the client side ... It has a mechType
78 (OID), and a mechToken (a security blob) ...
80 Really, we need to break out the NTLMSSP stuff as well, because it could be
83 DATA_BLOB
gen_negTokenInit(const char *OID
, DATA_BLOB blob
)
88 memset(&data
, 0, sizeof(data
));
90 asn1_push_tag(&data
, ASN1_APPLICATION(0));
91 asn1_write_OID(&data
,OID_SPNEGO
);
92 asn1_push_tag(&data
, ASN1_CONTEXT(0));
93 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
95 asn1_push_tag(&data
, ASN1_CONTEXT(0));
96 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
97 asn1_write_OID(&data
, OID
);
101 asn1_push_tag(&data
, ASN1_CONTEXT(2));
102 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
110 if (data
.has_error
) {
111 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
.ofs
));
115 ret
= data_blob(data
.data
, data
.length
);
122 parse a negTokenInit packet giving a GUID, a list of supported
123 OIDs (the mechanisms) and a principal name string
125 bool spnego_parse_negTokenInit(DATA_BLOB blob
,
126 char *OIDs
[ASN1_MAX_OIDS
],
133 asn1_load(&data
, blob
);
135 asn1_start_tag(&data
,ASN1_APPLICATION(0));
136 asn1_check_OID(&data
,OID_SPNEGO
);
137 asn1_start_tag(&data
,ASN1_CONTEXT(0));
138 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
140 asn1_start_tag(&data
,ASN1_CONTEXT(0));
141 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
142 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
-1; i
++) {
143 char *oid_str
= NULL
;
144 asn1_read_OID(&data
,&oid_str
);
152 if (asn1_tag_remaining(&data
) > 0) {
153 asn1_start_tag(&data
, ASN1_CONTEXT(3));
154 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
155 asn1_start_tag(&data
, ASN1_CONTEXT(0));
156 asn1_read_GeneralString(&data
,principal
);
167 ret
= !data
.has_error
;
168 if (data
.has_error
) {
170 SAFE_FREE(*principal
);
171 for(j
= 0; j
< i
&& j
< ASN1_MAX_OIDS
-1; j
++) {
181 generate a negTokenTarg packet given a list of OIDs and a security blob
183 DATA_BLOB
gen_negTokenTarg(const char *OIDs
[], DATA_BLOB blob
)
189 memset(&data
, 0, sizeof(data
));
191 asn1_push_tag(&data
, ASN1_APPLICATION(0));
192 asn1_write_OID(&data
,OID_SPNEGO
);
193 asn1_push_tag(&data
, ASN1_CONTEXT(0));
194 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
196 asn1_push_tag(&data
, ASN1_CONTEXT(0));
197 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
198 for (i
=0; OIDs
[i
]; i
++) {
199 asn1_write_OID(&data
,OIDs
[i
]);
204 asn1_push_tag(&data
, ASN1_CONTEXT(2));
205 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
213 if (data
.has_error
) {
214 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data
.ofs
));
218 ret
= data_blob(data
.data
, data
.length
);
225 parse a negTokenTarg packet giving a list of OIDs and a security blob
227 bool parse_negTokenTarg(DATA_BLOB blob
, char *OIDs
[ASN1_MAX_OIDS
], DATA_BLOB
*secblob
)
232 asn1_load(&data
, blob
);
233 asn1_start_tag(&data
, ASN1_APPLICATION(0));
234 asn1_check_OID(&data
,OID_SPNEGO
);
235 asn1_start_tag(&data
, ASN1_CONTEXT(0));
236 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
238 asn1_start_tag(&data
, ASN1_CONTEXT(0));
239 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
240 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
-1; i
++) {
241 char *oid_str
= NULL
;
242 asn1_read_OID(&data
,&oid_str
);
249 asn1_start_tag(&data
, ASN1_CONTEXT(2));
250 asn1_read_OctetString(&data
,secblob
);
258 if (data
.has_error
) {
260 data_blob_free(secblob
);
261 for(j
= 0; j
< i
&& j
< ASN1_MAX_OIDS
-1; j
++) {
264 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data
.ofs
));
274 generate a krb5 GSS-API wrapper packet given a ticket
276 DATA_BLOB
spnego_gen_krb5_wrap(const DATA_BLOB ticket
, const uint8 tok_id
[2])
281 memset(&data
, 0, sizeof(data
));
283 asn1_push_tag(&data
, ASN1_APPLICATION(0));
284 asn1_write_OID(&data
, OID_KERBEROS5
);
286 asn1_write(&data
, tok_id
, 2);
287 asn1_write(&data
, ticket
.data
, ticket
.length
);
290 if (data
.has_error
) {
291 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
.ofs
));
295 ret
= data_blob(data
.data
, data
.length
);
302 parse a krb5 GSS-API wrapper packet giving a ticket
304 bool spnego_parse_krb5_wrap(DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
310 asn1_load(&data
, blob
);
311 asn1_start_tag(&data
, ASN1_APPLICATION(0));
312 asn1_check_OID(&data
, OID_KERBEROS5
);
314 data_remaining
= asn1_tag_remaining(&data
);
316 if (data_remaining
< 3) {
317 data
.has_error
= True
;
319 asn1_read(&data
, tok_id
, 2);
321 *ticket
= data_blob(NULL
, data_remaining
);
322 asn1_read(&data
, ticket
->data
, ticket
->length
);
327 ret
= !data
.has_error
;
329 if (data
.has_error
) {
330 data_blob_free(ticket
);
340 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
341 kerberos session setup
343 int spnego_gen_negTokenTarg(const char *principal
, int time_offset
,
345 DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
349 DATA_BLOB tkt
, tkt_wrapped
;
350 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_KERBEROS5
, OID_NTLMSSP
, NULL
};
352 /* get a kerberos ticket for the service and extract the session key */
353 retval
= cli_krb5_get_ticket(principal
, time_offset
,
354 &tkt
, session_key_krb5
, extra_ap_opts
, NULL
,
360 /* wrap that up in a nice GSS-API wrapping */
361 tkt_wrapped
= spnego_gen_krb5_wrap(tkt
, TOK_ID_KRB_AP_REQ
);
363 /* and wrap that in a shiny SPNEGO wrapper */
364 *targ
= gen_negTokenTarg(krb_mechs
, tkt_wrapped
);
366 data_blob_free(&tkt_wrapped
);
367 data_blob_free(&tkt
);
374 parse a spnego NTLMSSP challenge packet giving two security blobs
376 bool spnego_parse_challenge(const DATA_BLOB blob
,
377 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
385 asn1_load(&data
, blob
);
386 asn1_start_tag(&data
,ASN1_CONTEXT(1));
387 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
389 asn1_start_tag(&data
,ASN1_CONTEXT(0));
390 asn1_check_enumerated(&data
,1);
393 asn1_start_tag(&data
,ASN1_CONTEXT(1));
394 asn1_check_OID(&data
, OID_NTLMSSP
);
397 asn1_start_tag(&data
,ASN1_CONTEXT(2));
398 asn1_read_OctetString(&data
, chal1
);
401 /* the second challenge is optional (XP doesn't send it) */
402 if (asn1_tag_remaining(&data
)) {
403 asn1_start_tag(&data
,ASN1_CONTEXT(3));
404 asn1_read_OctetString(&data
, chal2
);
411 ret
= !data
.has_error
;
413 if (data
.has_error
) {
414 data_blob_free(chal1
);
415 data_blob_free(chal2
);
424 generate a SPNEGO auth packet. This will contain the encrypted passwords
426 DATA_BLOB
spnego_gen_auth(DATA_BLOB blob
)
431 memset(&data
, 0, sizeof(data
));
433 asn1_push_tag(&data
, ASN1_CONTEXT(1));
434 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
435 asn1_push_tag(&data
, ASN1_CONTEXT(2));
436 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
441 ret
= data_blob(data
.data
, data
.length
);
449 parse a SPNEGO auth packet. This contains the encrypted passwords
451 bool spnego_parse_auth(DATA_BLOB blob
, DATA_BLOB
*auth
)
455 asn1_load(&data
, blob
);
456 asn1_start_tag(&data
, ASN1_CONTEXT(1));
457 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
458 asn1_start_tag(&data
, ASN1_CONTEXT(2));
459 asn1_read_OctetString(&data
,auth
);
464 if (data
.has_error
) {
465 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data
.ofs
));
466 data_blob_free(auth
);
476 generate a minimal SPNEGO response packet. Doesn't contain much.
478 DATA_BLOB
spnego_gen_auth_response(DATA_BLOB
*reply
, NTSTATUS nt_status
,
485 if (NT_STATUS_IS_OK(nt_status
)) {
486 negResult
= SPNEGO_NEG_RESULT_ACCEPT
;
487 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
488 negResult
= SPNEGO_NEG_RESULT_INCOMPLETE
;
490 negResult
= SPNEGO_NEG_RESULT_REJECT
;
495 asn1_push_tag(&data
, ASN1_CONTEXT(1));
496 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
497 asn1_push_tag(&data
, ASN1_CONTEXT(0));
498 asn1_write_enumerated(&data
, negResult
);
502 asn1_push_tag(&data
,ASN1_CONTEXT(1));
503 asn1_write_OID(&data
, mechOID
);
507 if (reply
&& reply
->data
!= NULL
) {
508 asn1_push_tag(&data
,ASN1_CONTEXT(2));
509 asn1_write_OctetString(&data
, reply
->data
, reply
->length
);
516 ret
= data_blob(data
.data
, data
.length
);
522 parse a SPNEGO auth packet. This contains the encrypted passwords
524 bool spnego_parse_auth_response(DATA_BLOB blob
, NTSTATUS nt_status
,
531 if (NT_STATUS_IS_OK(nt_status
)) {
532 negResult
= SPNEGO_NEG_RESULT_ACCEPT
;
533 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
534 negResult
= SPNEGO_NEG_RESULT_INCOMPLETE
;
536 negResult
= SPNEGO_NEG_RESULT_REJECT
;
539 asn1_load(&data
, blob
);
540 asn1_start_tag(&data
, ASN1_CONTEXT(1));
541 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
542 asn1_start_tag(&data
, ASN1_CONTEXT(0));
543 asn1_check_enumerated(&data
, negResult
);
546 *auth
= data_blob_null
;
548 if (asn1_tag_remaining(&data
)) {
549 asn1_start_tag(&data
,ASN1_CONTEXT(1));
550 asn1_check_OID(&data
, mechOID
);
553 if (asn1_tag_remaining(&data
)) {
554 asn1_start_tag(&data
,ASN1_CONTEXT(2));
555 asn1_read_OctetString(&data
, auth
);
558 } else if (negResult
== SPNEGO_NEG_RESULT_INCOMPLETE
) {
562 /* Binding against Win2K DC returns a duplicate of the responseToken in
563 * the optional mechListMIC field. This is a bug in Win2K. We ignore
564 * this field if it exists. Win2K8 may return a proper mechListMIC at
565 * which point we need to implement the integrity checking. */
566 if (asn1_tag_remaining(&data
)) {
567 DATA_BLOB mechList
= data_blob_null
;
568 asn1_start_tag(&data
, ASN1_CONTEXT(3));
569 asn1_read_OctetString(&data
, &mechList
);
571 data_blob_free(&mechList
);
572 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
579 if (data
.has_error
) {
580 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data
.ofs
));
582 data_blob_free(auth
);