idl: rebuild drsuapi.idl
[Samba/aatanasov.git] / source3 / libsmb / clispnego.c
blob5d7e43d941f79f20c7f78f8c5f7ac3fd8e3ac92e
1 /*
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/>.
22 #include "includes.h"
23 #include "../libcli/auth/spnego.h"
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(char guid[16],
30 const char *OIDs[],
31 const char *principal)
33 int i;
34 ASN1_DATA *data;
35 DATA_BLOB ret;
37 data = asn1_init(talloc_tos());
38 if (data == NULL) {
39 return data_blob_null;
42 asn1_write(data, guid, 16);
43 asn1_push_tag(data,ASN1_APPLICATION(0));
44 asn1_write_OID(data,OID_SPNEGO);
45 asn1_push_tag(data,ASN1_CONTEXT(0));
46 asn1_push_tag(data,ASN1_SEQUENCE(0));
48 asn1_push_tag(data,ASN1_CONTEXT(0));
49 asn1_push_tag(data,ASN1_SEQUENCE(0));
50 for (i=0; OIDs[i]; i++) {
51 asn1_write_OID(data,OIDs[i]);
53 asn1_pop_tag(data);
54 asn1_pop_tag(data);
56 asn1_push_tag(data, ASN1_CONTEXT(3));
57 asn1_push_tag(data, ASN1_SEQUENCE(0));
58 asn1_push_tag(data, ASN1_CONTEXT(0));
59 asn1_write_GeneralString(data,principal);
60 asn1_pop_tag(data);
61 asn1_pop_tag(data);
62 asn1_pop_tag(data);
64 asn1_pop_tag(data);
65 asn1_pop_tag(data);
67 asn1_pop_tag(data);
69 if (data->has_error) {
70 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
73 ret = data_blob(data->data, data->length);
74 asn1_free(data);
76 return ret;
80 Generate a negTokenInit as used by the client side ... It has a mechType
81 (OID), and a mechToken (a security blob) ...
83 Really, we need to break out the NTLMSSP stuff as well, because it could be
84 raw in the packets!
86 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
88 ASN1_DATA *data;
89 DATA_BLOB ret;
91 data = asn1_init(talloc_tos());
92 if (data == NULL) {
93 return data_blob_null;
96 asn1_push_tag(data, ASN1_APPLICATION(0));
97 asn1_write_OID(data,OID_SPNEGO);
98 asn1_push_tag(data, ASN1_CONTEXT(0));
99 asn1_push_tag(data, ASN1_SEQUENCE(0));
101 asn1_push_tag(data, ASN1_CONTEXT(0));
102 asn1_push_tag(data, ASN1_SEQUENCE(0));
103 asn1_write_OID(data, OID);
104 asn1_pop_tag(data);
105 asn1_pop_tag(data);
107 asn1_push_tag(data, ASN1_CONTEXT(2));
108 asn1_write_OctetString(data,blob.data,blob.length);
109 asn1_pop_tag(data);
111 asn1_pop_tag(data);
112 asn1_pop_tag(data);
114 asn1_pop_tag(data);
116 if (data->has_error) {
117 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
120 ret = data_blob(data->data, data->length);
121 asn1_free(data);
123 return ret;
127 parse a negTokenInit packet giving a GUID, a list of supported
128 OIDs (the mechanisms) and a principal name string
130 bool spnego_parse_negTokenInit(DATA_BLOB blob,
131 char *OIDs[ASN1_MAX_OIDS],
132 char **principal)
134 int i;
135 bool ret;
136 ASN1_DATA *data;
138 data = asn1_init(talloc_tos());
139 if (data == NULL) {
140 return false;
143 asn1_load(data, blob);
145 asn1_start_tag(data,ASN1_APPLICATION(0));
147 asn1_check_OID(data,OID_SPNEGO);
148 asn1_start_tag(data,ASN1_CONTEXT(0));
149 asn1_start_tag(data,ASN1_SEQUENCE(0));
151 asn1_start_tag(data,ASN1_CONTEXT(0));
152 asn1_start_tag(data,ASN1_SEQUENCE(0));
153 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
154 const char *oid_str = NULL;
155 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
156 OIDs[i] = CONST_DISCARD(char *, oid_str);
158 OIDs[i] = NULL;
159 asn1_end_tag(data);
160 asn1_end_tag(data);
162 *principal = NULL;
163 if (asn1_tag_remaining(data) > 0) {
164 asn1_start_tag(data, ASN1_CONTEXT(3));
165 asn1_start_tag(data, ASN1_SEQUENCE(0));
166 asn1_start_tag(data, ASN1_CONTEXT(0));
167 asn1_read_GeneralString(data,talloc_autofree_context(),principal);
168 asn1_end_tag(data);
169 asn1_end_tag(data);
170 asn1_end_tag(data);
173 asn1_end_tag(data);
174 asn1_end_tag(data);
176 asn1_end_tag(data);
178 ret = !data->has_error;
179 if (data->has_error) {
180 int j;
181 TALLOC_FREE(*principal);
182 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
183 TALLOC_FREE(OIDs[j]);
187 asn1_free(data);
188 return ret;
192 generate a negTokenTarg packet given a list of OIDs and a security blob
194 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
196 int i;
197 ASN1_DATA *data;
198 DATA_BLOB ret;
200 data = asn1_init(talloc_tos());
201 if (data == NULL) {
202 return data_blob_null;
205 asn1_push_tag(data, ASN1_APPLICATION(0));
206 asn1_write_OID(data,OID_SPNEGO);
207 asn1_push_tag(data, ASN1_CONTEXT(0));
208 asn1_push_tag(data, ASN1_SEQUENCE(0));
210 asn1_push_tag(data, ASN1_CONTEXT(0));
211 asn1_push_tag(data, ASN1_SEQUENCE(0));
212 for (i=0; OIDs[i]; i++) {
213 asn1_write_OID(data,OIDs[i]);
215 asn1_pop_tag(data);
216 asn1_pop_tag(data);
218 asn1_push_tag(data, ASN1_CONTEXT(2));
219 asn1_write_OctetString(data,blob.data,blob.length);
220 asn1_pop_tag(data);
222 asn1_pop_tag(data);
223 asn1_pop_tag(data);
225 asn1_pop_tag(data);
227 if (data->has_error) {
228 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
231 ret = data_blob(data->data, data->length);
232 asn1_free(data);
234 return ret;
238 parse a negTokenTarg packet giving a list of OIDs and a security blob
240 bool parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
242 int i;
243 ASN1_DATA *data;
245 data = asn1_init(talloc_tos());
246 if (data == NULL) {
247 return false;
250 asn1_load(data, blob);
251 asn1_start_tag(data, ASN1_APPLICATION(0));
252 asn1_check_OID(data,OID_SPNEGO);
253 asn1_start_tag(data, ASN1_CONTEXT(0));
254 asn1_start_tag(data, ASN1_SEQUENCE(0));
256 asn1_start_tag(data, ASN1_CONTEXT(0));
257 asn1_start_tag(data, ASN1_SEQUENCE(0));
258 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
259 const char *oid_str = NULL;
260 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
261 OIDs[i] = CONST_DISCARD(char *, oid_str);
263 OIDs[i] = NULL;
264 asn1_end_tag(data);
265 asn1_end_tag(data);
267 /* Skip any optional req_flags that are sent per RFC 4178 */
268 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
269 uint8 flags;
271 asn1_start_tag(data, ASN1_CONTEXT(1));
272 asn1_start_tag(data, ASN1_BIT_STRING);
273 while (asn1_tag_remaining(data) > 0)
274 asn1_read_uint8(data, &flags);
275 asn1_end_tag(data);
276 asn1_end_tag(data);
279 asn1_start_tag(data, ASN1_CONTEXT(2));
280 asn1_read_OctetString(data,talloc_autofree_context(),secblob);
281 asn1_end_tag(data);
283 asn1_end_tag(data);
284 asn1_end_tag(data);
286 asn1_end_tag(data);
288 if (data->has_error) {
289 int j;
290 data_blob_free(secblob);
291 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
292 TALLOC_FREE(OIDs[j]);
294 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data->ofs));
295 asn1_free(data);
296 return False;
299 asn1_free(data);
300 return True;
304 generate a krb5 GSS-API wrapper packet given a ticket
306 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
308 ASN1_DATA *data;
309 DATA_BLOB ret;
311 data = asn1_init(talloc_tos());
312 if (data == NULL) {
313 return data_blob_null;
316 asn1_push_tag(data, ASN1_APPLICATION(0));
317 asn1_write_OID(data, OID_KERBEROS5);
319 asn1_write(data, tok_id, 2);
320 asn1_write(data, ticket.data, ticket.length);
321 asn1_pop_tag(data);
323 if (data->has_error) {
324 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
327 ret = data_blob(data->data, data->length);
328 asn1_free(data);
330 return ret;
334 parse a krb5 GSS-API wrapper packet giving a ticket
336 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
338 bool ret;
339 ASN1_DATA *data;
340 int data_remaining;
342 data = asn1_init(talloc_tos());
343 if (data == NULL) {
344 return false;
347 asn1_load(data, blob);
348 asn1_start_tag(data, ASN1_APPLICATION(0));
349 asn1_check_OID(data, OID_KERBEROS5);
351 data_remaining = asn1_tag_remaining(data);
353 if (data_remaining < 3) {
354 data->has_error = True;
355 } else {
356 asn1_read(data, tok_id, 2);
357 data_remaining -= 2;
358 *ticket = data_blob(NULL, data_remaining);
359 asn1_read(data, ticket->data, ticket->length);
362 asn1_end_tag(data);
364 ret = !data->has_error;
366 if (data->has_error) {
367 data_blob_free(ticket);
370 asn1_free(data);
372 return ret;
377 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
378 kerberos session setup
380 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
381 DATA_BLOB *targ,
382 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
383 time_t *expire_time)
385 int retval;
386 DATA_BLOB tkt, tkt_wrapped;
387 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
389 /* get a kerberos ticket for the service and extract the session key */
390 retval = cli_krb5_get_ticket(principal, time_offset,
391 &tkt, session_key_krb5, extra_ap_opts, NULL,
392 expire_time);
394 if (retval)
395 return retval;
397 /* wrap that up in a nice GSS-API wrapping */
398 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
400 /* and wrap that in a shiny SPNEGO wrapper */
401 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
403 data_blob_free(&tkt_wrapped);
404 data_blob_free(&tkt);
406 return retval;
411 parse a spnego NTLMSSP challenge packet giving two security blobs
413 bool spnego_parse_challenge(const DATA_BLOB blob,
414 DATA_BLOB *chal1, DATA_BLOB *chal2)
416 bool ret;
417 ASN1_DATA *data;
419 ZERO_STRUCTP(chal1);
420 ZERO_STRUCTP(chal2);
422 data = asn1_init(talloc_tos());
423 if (data == NULL) {
424 return false;
427 asn1_load(data, blob);
428 asn1_start_tag(data,ASN1_CONTEXT(1));
429 asn1_start_tag(data,ASN1_SEQUENCE(0));
431 asn1_start_tag(data,ASN1_CONTEXT(0));
432 asn1_check_enumerated(data,1);
433 asn1_end_tag(data);
435 asn1_start_tag(data,ASN1_CONTEXT(1));
436 asn1_check_OID(data, OID_NTLMSSP);
437 asn1_end_tag(data);
439 asn1_start_tag(data,ASN1_CONTEXT(2));
440 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
441 asn1_end_tag(data);
443 /* the second challenge is optional (XP doesn't send it) */
444 if (asn1_tag_remaining(data)) {
445 asn1_start_tag(data,ASN1_CONTEXT(3));
446 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
447 asn1_end_tag(data);
450 asn1_end_tag(data);
451 asn1_end_tag(data);
453 ret = !data->has_error;
455 if (data->has_error) {
456 data_blob_free(chal1);
457 data_blob_free(chal2);
460 asn1_free(data);
461 return ret;
466 generate a SPNEGO auth packet. This will contain the encrypted passwords
468 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
470 ASN1_DATA *data;
471 DATA_BLOB ret;
473 data = asn1_init(talloc_tos());
474 if (data == NULL) {
475 return data_blob_null;
478 asn1_push_tag(data, ASN1_CONTEXT(1));
479 asn1_push_tag(data, ASN1_SEQUENCE(0));
480 asn1_push_tag(data, ASN1_CONTEXT(2));
481 asn1_write_OctetString(data,blob.data,blob.length);
482 asn1_pop_tag(data);
483 asn1_pop_tag(data);
484 asn1_pop_tag(data);
486 ret = data_blob(data->data, data->length);
488 asn1_free(data);
490 return ret;
494 parse a SPNEGO auth packet. This contains the encrypted passwords
496 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
498 ASN1_DATA *data;
500 data = asn1_init(talloc_tos());
501 if (data == NULL) {
502 return false;
505 asn1_load(data, blob);
506 asn1_start_tag(data, ASN1_CONTEXT(1));
507 asn1_start_tag(data, ASN1_SEQUENCE(0));
508 asn1_start_tag(data, ASN1_CONTEXT(2));
509 asn1_read_OctetString(data, talloc_autofree_context(), auth);
510 asn1_end_tag(data);
511 asn1_end_tag(data);
512 asn1_end_tag(data);
514 if (data->has_error) {
515 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data->ofs));
516 data_blob_free(auth);
517 asn1_free(data);
518 return False;
521 asn1_free(data);
522 return True;
526 generate a minimal SPNEGO response packet. Doesn't contain much.
528 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
529 const char *mechOID)
531 ASN1_DATA *data;
532 DATA_BLOB ret;
533 uint8 negResult;
535 if (NT_STATUS_IS_OK(nt_status)) {
536 negResult = SPNEGO_ACCEPT_COMPLETED;
537 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
538 negResult = SPNEGO_ACCEPT_INCOMPLETE;
539 } else {
540 negResult = SPNEGO_REJECT;
543 data = asn1_init(talloc_tos());
544 if (data == NULL) {
545 return data_blob_null;
548 asn1_push_tag(data, ASN1_CONTEXT(1));
549 asn1_push_tag(data, ASN1_SEQUENCE(0));
550 asn1_push_tag(data, ASN1_CONTEXT(0));
551 asn1_write_enumerated(data, negResult);
552 asn1_pop_tag(data);
554 if (mechOID) {
555 asn1_push_tag(data,ASN1_CONTEXT(1));
556 asn1_write_OID(data, mechOID);
557 asn1_pop_tag(data);
560 if (reply && reply->data != NULL) {
561 asn1_push_tag(data,ASN1_CONTEXT(2));
562 asn1_write_OctetString(data, reply->data, reply->length);
563 asn1_pop_tag(data);
566 asn1_pop_tag(data);
567 asn1_pop_tag(data);
569 ret = data_blob(data->data, data->length);
570 asn1_free(data);
571 return ret;
575 parse a SPNEGO auth packet. This contains the encrypted passwords
577 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
578 const char *mechOID,
579 DATA_BLOB *auth)
581 ASN1_DATA *data;
582 uint8 negResult;
584 if (NT_STATUS_IS_OK(nt_status)) {
585 negResult = SPNEGO_ACCEPT_COMPLETED;
586 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
587 negResult = SPNEGO_ACCEPT_INCOMPLETE;
588 } else {
589 negResult = SPNEGO_REJECT;
592 data = asn1_init(talloc_tos());
593 if (data == NULL) {
594 return false;
597 asn1_load(data, blob);
598 asn1_start_tag(data, ASN1_CONTEXT(1));
599 asn1_start_tag(data, ASN1_SEQUENCE(0));
600 asn1_start_tag(data, ASN1_CONTEXT(0));
601 asn1_check_enumerated(data, negResult);
602 asn1_end_tag(data);
604 *auth = data_blob_null;
606 if (asn1_tag_remaining(data)) {
607 asn1_start_tag(data,ASN1_CONTEXT(1));
608 asn1_check_OID(data, mechOID);
609 asn1_end_tag(data);
611 if (asn1_tag_remaining(data)) {
612 asn1_start_tag(data,ASN1_CONTEXT(2));
613 asn1_read_OctetString(data, talloc_autofree_context(), auth);
614 asn1_end_tag(data);
616 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
617 data->has_error = 1;
620 /* Binding against Win2K DC returns a duplicate of the responseToken in
621 * the optional mechListMIC field. This is a bug in Win2K. We ignore
622 * this field if it exists. Win2K8 may return a proper mechListMIC at
623 * which point we need to implement the integrity checking. */
624 if (asn1_tag_remaining(data)) {
625 DATA_BLOB mechList = data_blob_null;
626 asn1_start_tag(data, ASN1_CONTEXT(3));
627 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
628 asn1_end_tag(data);
629 data_blob_free(&mechList);
630 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
631 "ignoring.\n"));
634 asn1_end_tag(data);
635 asn1_end_tag(data);
637 if (data->has_error) {
638 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
639 asn1_free(data);
640 data_blob_free(auth);
641 return False;
644 asn1_free(data);
645 return True;