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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 generate a negTokenInit packet given a GUID, a list of supported
27 OIDs (the mechanisms) and a principal name string
29 DATA_BLOB
spnego_gen_negTokenInit(uint8 guid
[16],
31 const char *principal
)
37 memset(&data
, 0, sizeof(data
));
39 asn1_write(&data
, guid
, 16);
40 asn1_push_tag(&data
,ASN1_APPLICATION(0));
41 asn1_write_OID(&data
,OID_SPNEGO
);
42 asn1_push_tag(&data
,ASN1_CONTEXT(0));
43 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
45 asn1_push_tag(&data
,ASN1_CONTEXT(0));
46 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
47 for (i
=0; OIDs
[i
]; i
++) {
48 asn1_write_OID(&data
,OIDs
[i
]);
53 asn1_push_tag(&data
, ASN1_CONTEXT(3));
54 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
55 asn1_push_tag(&data
, ASN1_CONTEXT(0));
56 asn1_write_GeneralString(&data
,principal
);
67 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
.ofs
));
71 ret
= data_blob(data
.data
, data
.length
);
78 Generate a negTokenInit as used by the client side ... It has a mechType
79 (OID), and a mechToken (a security blob) ...
81 Really, we need to break out the NTLMSSP stuff as well, because it could be
84 DATA_BLOB
gen_negTokenInit(const char *OID
, DATA_BLOB blob
)
89 memset(&data
, 0, sizeof(data
));
91 asn1_push_tag(&data
, ASN1_APPLICATION(0));
92 asn1_write_OID(&data
,OID_SPNEGO
);
93 asn1_push_tag(&data
, ASN1_CONTEXT(0));
94 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
96 asn1_push_tag(&data
, ASN1_CONTEXT(0));
97 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
98 asn1_write_OID(&data
, OID
);
102 asn1_push_tag(&data
, ASN1_CONTEXT(2));
103 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
111 if (data
.has_error
) {
112 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
.ofs
));
116 ret
= data_blob(data
.data
, data
.length
);
123 parse a negTokenInit packet giving a GUID, a list of supported
124 OIDs (the mechanisms) and a principal name string
126 BOOL
spnego_parse_negTokenInit(DATA_BLOB blob
,
127 char *OIDs
[ASN1_MAX_OIDS
],
134 asn1_load(&data
, blob
);
136 asn1_start_tag(&data
,ASN1_APPLICATION(0));
137 asn1_check_OID(&data
,OID_SPNEGO
);
138 asn1_start_tag(&data
,ASN1_CONTEXT(0));
139 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
141 asn1_start_tag(&data
,ASN1_CONTEXT(0));
142 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
143 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
; i
++) {
144 char *oid_str
= NULL
;
145 asn1_read_OID(&data
,&oid_str
);
152 asn1_start_tag(&data
, ASN1_CONTEXT(3));
153 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
154 asn1_start_tag(&data
, ASN1_CONTEXT(0));
155 asn1_read_GeneralString(&data
,principal
);
165 ret
= !data
.has_error
;
172 generate a negTokenTarg packet given a list of OIDs and a security blob
174 DATA_BLOB
gen_negTokenTarg(const char *OIDs
[], DATA_BLOB blob
)
180 memset(&data
, 0, sizeof(data
));
182 asn1_push_tag(&data
, ASN1_APPLICATION(0));
183 asn1_write_OID(&data
,OID_SPNEGO
);
184 asn1_push_tag(&data
, ASN1_CONTEXT(0));
185 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
187 asn1_push_tag(&data
, ASN1_CONTEXT(0));
188 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
189 for (i
=0; OIDs
[i
]; i
++) {
190 asn1_write_OID(&data
,OIDs
[i
]);
195 asn1_push_tag(&data
, ASN1_CONTEXT(2));
196 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
204 if (data
.has_error
) {
205 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data
.ofs
));
209 ret
= data_blob(data
.data
, data
.length
);
217 parse a negTokenTarg packet giving a list of OIDs and a security blob
219 BOOL
parse_negTokenTarg(DATA_BLOB blob
, char *OIDs
[ASN1_MAX_OIDS
], DATA_BLOB
*secblob
)
224 asn1_load(&data
, blob
);
225 asn1_start_tag(&data
, ASN1_APPLICATION(0));
226 asn1_check_OID(&data
,OID_SPNEGO
);
227 asn1_start_tag(&data
, ASN1_CONTEXT(0));
228 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
230 asn1_start_tag(&data
, ASN1_CONTEXT(0));
231 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
232 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
; i
++) {
233 char *oid_str
= NULL
;
234 asn1_read_OID(&data
,&oid_str
);
241 asn1_start_tag(&data
, ASN1_CONTEXT(2));
242 asn1_read_OctetString(&data
,secblob
);
250 if (data
.has_error
) {
251 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data
.ofs
));
261 generate a krb5 GSS-API wrapper packet given a ticket
263 DATA_BLOB
spnego_gen_krb5_wrap(DATA_BLOB ticket
, const uint8 tok_id
[2])
268 memset(&data
, 0, sizeof(data
));
270 asn1_push_tag(&data
, ASN1_APPLICATION(0));
271 asn1_write_OID(&data
, OID_KERBEROS5
);
273 asn1_write(&data
, tok_id
, 2);
274 asn1_write(&data
, ticket
.data
, ticket
.length
);
277 if (data
.has_error
) {
278 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
.ofs
));
282 ret
= data_blob(data
.data
, data
.length
);
289 parse a krb5 GSS-API wrapper packet giving a ticket
291 BOOL
spnego_parse_krb5_wrap(DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
297 asn1_load(&data
, blob
);
298 asn1_start_tag(&data
, ASN1_APPLICATION(0));
299 asn1_check_OID(&data
, OID_KERBEROS5
);
301 data_remaining
= asn1_tag_remaining(&data
);
303 if (data_remaining
< 3) {
304 data
.has_error
= True
;
306 asn1_read(&data
, tok_id
, 2);
308 *ticket
= data_blob(NULL
, data_remaining
);
309 asn1_read(&data
, ticket
->data
, ticket
->length
);
314 ret
= !data
.has_error
;
323 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
324 kerberos session setup
326 int spnego_gen_negTokenTarg(const char *principal
, int time_offset
,
328 DATA_BLOB
*session_key_krb5
)
331 DATA_BLOB tkt
, tkt_wrapped
;
332 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_NTLMSSP
, NULL
};
334 /* get a kerberos ticket for the service and extract the session key */
335 retval
= cli_krb5_get_ticket(principal
, time_offset
, &tkt
, session_key_krb5
);
340 /* wrap that up in a nice GSS-API wrapping */
341 tkt_wrapped
= spnego_gen_krb5_wrap(tkt
, TOK_ID_KRB_AP_REQ
);
343 /* and wrap that in a shiny SPNEGO wrapper */
344 *targ
= gen_negTokenTarg(krb_mechs
, tkt_wrapped
);
346 data_blob_free(&tkt_wrapped
);
347 data_blob_free(&tkt
);
354 parse a spnego NTLMSSP challenge packet giving two security blobs
356 BOOL
spnego_parse_challenge(const DATA_BLOB blob
,
357 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
365 asn1_load(&data
, blob
);
366 asn1_start_tag(&data
,ASN1_CONTEXT(1));
367 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
369 asn1_start_tag(&data
,ASN1_CONTEXT(0));
370 asn1_check_enumerated(&data
,1);
373 asn1_start_tag(&data
,ASN1_CONTEXT(1));
374 asn1_check_OID(&data
, OID_NTLMSSP
);
377 asn1_start_tag(&data
,ASN1_CONTEXT(2));
378 asn1_read_OctetString(&data
, chal1
);
381 /* the second challenge is optional (XP doesn't send it) */
382 if (asn1_tag_remaining(&data
)) {
383 asn1_start_tag(&data
,ASN1_CONTEXT(3));
384 asn1_read_OctetString(&data
, chal2
);
391 ret
= !data
.has_error
;
398 generate a SPNEGO auth packet. This will contain the encrypted passwords
400 DATA_BLOB
spnego_gen_auth(DATA_BLOB blob
)
405 memset(&data
, 0, sizeof(data
));
407 asn1_push_tag(&data
, ASN1_CONTEXT(1));
408 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
409 asn1_push_tag(&data
, ASN1_CONTEXT(2));
410 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
415 ret
= data_blob(data
.data
, data
.length
);
423 parse a SPNEGO auth packet. This contains the encrypted passwords
425 BOOL
spnego_parse_auth(DATA_BLOB blob
, DATA_BLOB
*auth
)
429 asn1_load(&data
, blob
);
430 asn1_start_tag(&data
, ASN1_CONTEXT(1));
431 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
432 asn1_start_tag(&data
, ASN1_CONTEXT(2));
433 asn1_read_OctetString(&data
,auth
);
438 if (data
.has_error
) {
439 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data
.ofs
));
449 generate a minimal SPNEGO response packet. Doesn't contain much.
451 DATA_BLOB
spnego_gen_auth_response(DATA_BLOB
*reply
, NTSTATUS nt_status
,
458 if (NT_STATUS_IS_OK(nt_status
)) {
459 negResult
= SPNEGO_NEG_RESULT_ACCEPT
;
460 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
461 negResult
= SPNEGO_NEG_RESULT_INCOMPLETE
;
463 negResult
= SPNEGO_NEG_RESULT_REJECT
;
468 asn1_push_tag(&data
, ASN1_CONTEXT(1));
469 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
470 asn1_push_tag(&data
, ASN1_CONTEXT(0));
471 asn1_write_enumerated(&data
, negResult
);
474 if (reply
->data
!= NULL
) {
475 asn1_push_tag(&data
,ASN1_CONTEXT(1));
476 asn1_write_OID(&data
, mechOID
);
479 asn1_push_tag(&data
,ASN1_CONTEXT(2));
480 asn1_write_OctetString(&data
, reply
->data
, reply
->length
);
487 ret
= data_blob(data
.data
, data
.length
);
493 parse a SPNEGO NTLMSSP auth packet. This contains the encrypted passwords
495 BOOL
spnego_parse_auth_response(DATA_BLOB blob
, NTSTATUS nt_status
,
501 if (NT_STATUS_IS_OK(nt_status
)) {
502 negResult
= SPNEGO_NEG_RESULT_ACCEPT
;
503 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
504 negResult
= SPNEGO_NEG_RESULT_INCOMPLETE
;
506 negResult
= SPNEGO_NEG_RESULT_REJECT
;
509 asn1_load(&data
, blob
);
510 asn1_start_tag(&data
, ASN1_CONTEXT(1));
511 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
512 asn1_start_tag(&data
, ASN1_CONTEXT(0));
513 asn1_check_enumerated(&data
, negResult
);
516 if (negResult
== SPNEGO_NEG_RESULT_INCOMPLETE
) {
517 asn1_start_tag(&data
,ASN1_CONTEXT(1));
518 asn1_check_OID(&data
, OID_NTLMSSP
);
521 asn1_start_tag(&data
,ASN1_CONTEXT(2));
522 asn1_read_OctetString(&data
, auth
);
529 if (data
.has_error
) {
530 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data
.ofs
));
532 data_blob_free(auth
);