s3-libsmb: Initialise ticket to ensure we do not invalid memory
[Samba.git] / source3 / libsmb / clispnego.c
blob3200380b26bd01233b9eeb83c89b6b5f3c362b63
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"
24 #include "smb_krb5.h"
27 generate a negTokenInit packet given a GUID, a list of supported
28 OIDs (the mechanisms) and a principal name string
30 DATA_BLOB spnego_gen_negTokenInit(char guid[16],
31 const char *OIDs[],
32 const char *principal)
34 int i;
35 ASN1_DATA *data;
36 DATA_BLOB ret;
38 data = asn1_init(talloc_tos());
39 if (data == NULL) {
40 return data_blob_null;
43 asn1_write(data, guid, 16);
44 asn1_push_tag(data,ASN1_APPLICATION(0));
45 asn1_write_OID(data,OID_SPNEGO);
46 asn1_push_tag(data,ASN1_CONTEXT(0));
47 asn1_push_tag(data,ASN1_SEQUENCE(0));
49 asn1_push_tag(data,ASN1_CONTEXT(0));
50 asn1_push_tag(data,ASN1_SEQUENCE(0));
51 for (i=0; OIDs[i]; i++) {
52 asn1_write_OID(data,OIDs[i]);
54 asn1_pop_tag(data);
55 asn1_pop_tag(data);
57 asn1_push_tag(data, ASN1_CONTEXT(3));
58 asn1_push_tag(data, ASN1_SEQUENCE(0));
59 asn1_push_tag(data, ASN1_CONTEXT(0));
60 asn1_write_GeneralString(data,principal);
61 asn1_pop_tag(data);
62 asn1_pop_tag(data);
63 asn1_pop_tag(data);
65 asn1_pop_tag(data);
66 asn1_pop_tag(data);
68 asn1_pop_tag(data);
70 if (data->has_error) {
71 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
74 ret = data_blob(data->data, data->length);
75 asn1_free(data);
77 return ret;
81 Generate a negTokenInit as used by the client side ... It has a mechType
82 (OID), and a mechToken (a security blob) ...
84 Really, we need to break out the NTLMSSP stuff as well, because it could be
85 raw in the packets!
87 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
89 ASN1_DATA *data;
90 DATA_BLOB ret;
92 data = asn1_init(talloc_tos());
93 if (data == NULL) {
94 return data_blob_null;
97 asn1_push_tag(data, ASN1_APPLICATION(0));
98 asn1_write_OID(data,OID_SPNEGO);
99 asn1_push_tag(data, ASN1_CONTEXT(0));
100 asn1_push_tag(data, ASN1_SEQUENCE(0));
102 asn1_push_tag(data, ASN1_CONTEXT(0));
103 asn1_push_tag(data, ASN1_SEQUENCE(0));
104 asn1_write_OID(data, OID);
105 asn1_pop_tag(data);
106 asn1_pop_tag(data);
108 asn1_push_tag(data, ASN1_CONTEXT(2));
109 asn1_write_OctetString(data,blob.data,blob.length);
110 asn1_pop_tag(data);
112 asn1_pop_tag(data);
113 asn1_pop_tag(data);
115 asn1_pop_tag(data);
117 if (data->has_error) {
118 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
121 ret = data_blob(data->data, data->length);
122 asn1_free(data);
124 return ret;
128 parse a negTokenInit packet giving a GUID, a list of supported
129 OIDs (the mechanisms) and a principal name string
131 bool spnego_parse_negTokenInit(DATA_BLOB blob,
132 char *OIDs[ASN1_MAX_OIDS],
133 char **principal)
135 int i;
136 bool ret;
137 ASN1_DATA *data;
139 for (i = 0; i < ASN1_MAX_OIDS; i++) {
140 OIDs[i] = NULL;
143 data = asn1_init(talloc_tos());
144 if (data == NULL) {
145 return false;
148 asn1_load(data, blob);
150 asn1_start_tag(data,ASN1_APPLICATION(0));
152 asn1_check_OID(data,OID_SPNEGO);
154 /* negTokenInit [0] NegTokenInit */
155 asn1_start_tag(data,ASN1_CONTEXT(0));
156 asn1_start_tag(data,ASN1_SEQUENCE(0));
158 /* mechTypes [0] MechTypeList OPTIONAL */
160 /* Not really optional, we depend on this to decide
161 * what mechanisms we have to work with. */
163 asn1_start_tag(data,ASN1_CONTEXT(0));
164 asn1_start_tag(data,ASN1_SEQUENCE(0));
165 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
166 const char *oid_str = NULL;
167 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
168 if (data->has_error) {
169 break;
171 OIDs[i] = CONST_DISCARD(char *, oid_str);
173 OIDs[i] = NULL;
174 asn1_end_tag(data);
175 asn1_end_tag(data);
177 *principal = NULL;
180 Win7 + Live Sign-in Assistant attaches a mechToken
181 ASN1_CONTEXT(2) to the negTokenInit packet
182 which breaks our negotiation if we just assume
183 the next tag is ASN1_CONTEXT(3).
186 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
187 uint8 flags;
189 /* reqFlags [1] ContextFlags OPTIONAL */
190 asn1_start_tag(data, ASN1_CONTEXT(1));
191 asn1_start_tag(data, ASN1_BIT_STRING);
192 while (asn1_tag_remaining(data) > 0) {
193 asn1_read_uint8(data, &flags);
195 asn1_end_tag(data);
196 asn1_end_tag(data);
199 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
200 /* mechToken [2] OCTET STRING OPTIONAL */
201 DATA_BLOB token;
202 asn1_start_tag(data, ASN1_CONTEXT(2));
203 asn1_read_OctetString(data, talloc_autofree_context(),
204 &token);
205 asn1_end_tag(data);
206 /* Throw away the token - not used. */
207 data_blob_free(&token);
210 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
211 /* mechListMIC [3] OCTET STRING OPTIONAL */
212 asn1_start_tag(data, ASN1_CONTEXT(3));
213 asn1_start_tag(data, ASN1_SEQUENCE(0));
214 asn1_start_tag(data, ASN1_CONTEXT(0));
215 asn1_read_GeneralString(data,talloc_autofree_context(),
216 principal);
217 asn1_end_tag(data);
218 asn1_end_tag(data);
219 asn1_end_tag(data);
222 asn1_end_tag(data);
223 asn1_end_tag(data);
225 asn1_end_tag(data);
227 ret = !data->has_error;
228 if (data->has_error) {
229 int j;
230 TALLOC_FREE(*principal);
231 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
232 TALLOC_FREE(OIDs[j]);
236 asn1_free(data);
237 return ret;
241 generate a negTokenTarg packet given a list of OIDs and a security blob
243 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
245 int i;
246 ASN1_DATA *data;
247 DATA_BLOB ret;
249 data = asn1_init(talloc_tos());
250 if (data == NULL) {
251 return data_blob_null;
254 asn1_push_tag(data, ASN1_APPLICATION(0));
255 asn1_write_OID(data,OID_SPNEGO);
256 asn1_push_tag(data, ASN1_CONTEXT(0));
257 asn1_push_tag(data, ASN1_SEQUENCE(0));
259 asn1_push_tag(data, ASN1_CONTEXT(0));
260 asn1_push_tag(data, ASN1_SEQUENCE(0));
261 for (i=0; OIDs[i]; i++) {
262 asn1_write_OID(data,OIDs[i]);
264 asn1_pop_tag(data);
265 asn1_pop_tag(data);
267 asn1_push_tag(data, ASN1_CONTEXT(2));
268 asn1_write_OctetString(data,blob.data,blob.length);
269 asn1_pop_tag(data);
271 asn1_pop_tag(data);
272 asn1_pop_tag(data);
274 asn1_pop_tag(data);
276 if (data->has_error) {
277 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
280 ret = data_blob(data->data, data->length);
281 asn1_free(data);
283 return ret;
287 parse a negTokenTarg packet giving a list of OIDs and a security blob
289 bool parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
291 int i;
292 ASN1_DATA *data;
294 data = asn1_init(talloc_tos());
295 if (data == NULL) {
296 return false;
299 asn1_load(data, blob);
300 asn1_start_tag(data, ASN1_APPLICATION(0));
301 asn1_check_OID(data,OID_SPNEGO);
302 asn1_start_tag(data, ASN1_CONTEXT(0));
303 asn1_start_tag(data, ASN1_SEQUENCE(0));
305 asn1_start_tag(data, ASN1_CONTEXT(0));
306 asn1_start_tag(data, ASN1_SEQUENCE(0));
307 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
308 const char *oid_str = NULL;
309 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
310 OIDs[i] = CONST_DISCARD(char *, oid_str);
312 OIDs[i] = NULL;
313 asn1_end_tag(data);
314 asn1_end_tag(data);
316 /* Skip any optional req_flags that are sent per RFC 4178 */
317 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
318 uint8 flags;
320 asn1_start_tag(data, ASN1_CONTEXT(1));
321 asn1_start_tag(data, ASN1_BIT_STRING);
322 while (asn1_tag_remaining(data) > 0)
323 asn1_read_uint8(data, &flags);
324 asn1_end_tag(data);
325 asn1_end_tag(data);
328 asn1_start_tag(data, ASN1_CONTEXT(2));
329 asn1_read_OctetString(data,talloc_autofree_context(),secblob);
330 asn1_end_tag(data);
332 asn1_end_tag(data);
333 asn1_end_tag(data);
335 asn1_end_tag(data);
337 if (data->has_error) {
338 int j;
339 data_blob_free(secblob);
340 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
341 TALLOC_FREE(OIDs[j]);
343 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data->ofs));
344 asn1_free(data);
345 return False;
348 asn1_free(data);
349 return True;
353 generate a krb5 GSS-API wrapper packet given a ticket
355 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
357 ASN1_DATA *data;
358 DATA_BLOB ret;
360 data = asn1_init(talloc_tos());
361 if (data == NULL) {
362 return data_blob_null;
365 asn1_push_tag(data, ASN1_APPLICATION(0));
366 asn1_write_OID(data, OID_KERBEROS5);
368 asn1_write(data, tok_id, 2);
369 asn1_write(data, ticket.data, ticket.length);
370 asn1_pop_tag(data);
372 if (data->has_error) {
373 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
376 ret = data_blob(data->data, data->length);
377 asn1_free(data);
379 return ret;
383 parse a krb5 GSS-API wrapper packet giving a ticket
385 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
387 bool ret;
388 ASN1_DATA *data;
389 int data_remaining;
390 *ticket = data_blob_null;
392 data = asn1_init(talloc_tos());
393 if (data == NULL) {
394 return false;
397 asn1_load(data, blob);
398 asn1_start_tag(data, ASN1_APPLICATION(0));
399 asn1_check_OID(data, OID_KERBEROS5);
401 data_remaining = asn1_tag_remaining(data);
403 if (data_remaining < 3) {
404 data->has_error = True;
405 } else {
406 asn1_read(data, tok_id, 2);
407 data_remaining -= 2;
408 *ticket = data_blob(NULL, data_remaining);
409 asn1_read(data, ticket->data, ticket->length);
412 asn1_end_tag(data);
414 ret = !data->has_error;
416 if (data->has_error) {
417 data_blob_free(ticket);
420 asn1_free(data);
422 return ret;
427 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
428 kerberos session setup
430 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
431 DATA_BLOB *targ,
432 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
433 time_t *expire_time)
435 int retval;
436 DATA_BLOB tkt, tkt_wrapped;
437 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
439 /* get a kerberos ticket for the service and extract the session key */
440 retval = cli_krb5_get_ticket(principal, time_offset,
441 &tkt, session_key_krb5, extra_ap_opts, NULL,
442 expire_time, NULL);
444 if (retval)
445 return retval;
447 /* wrap that up in a nice GSS-API wrapping */
448 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
450 /* and wrap that in a shiny SPNEGO wrapper */
451 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
453 data_blob_free(&tkt_wrapped);
454 data_blob_free(&tkt);
456 return retval;
461 parse a spnego NTLMSSP challenge packet giving two security blobs
463 bool spnego_parse_challenge(const DATA_BLOB blob,
464 DATA_BLOB *chal1, DATA_BLOB *chal2)
466 bool ret;
467 ASN1_DATA *data;
469 ZERO_STRUCTP(chal1);
470 ZERO_STRUCTP(chal2);
472 data = asn1_init(talloc_tos());
473 if (data == NULL) {
474 return false;
477 asn1_load(data, blob);
478 asn1_start_tag(data,ASN1_CONTEXT(1));
479 asn1_start_tag(data,ASN1_SEQUENCE(0));
481 asn1_start_tag(data,ASN1_CONTEXT(0));
482 asn1_check_enumerated(data,1);
483 asn1_end_tag(data);
485 asn1_start_tag(data,ASN1_CONTEXT(1));
486 asn1_check_OID(data, OID_NTLMSSP);
487 asn1_end_tag(data);
489 asn1_start_tag(data,ASN1_CONTEXT(2));
490 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
491 asn1_end_tag(data);
493 /* the second challenge is optional (XP doesn't send it) */
494 if (asn1_tag_remaining(data)) {
495 asn1_start_tag(data,ASN1_CONTEXT(3));
496 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
497 asn1_end_tag(data);
500 asn1_end_tag(data);
501 asn1_end_tag(data);
503 ret = !data->has_error;
505 if (data->has_error) {
506 data_blob_free(chal1);
507 data_blob_free(chal2);
510 asn1_free(data);
511 return ret;
516 generate a SPNEGO auth packet. This will contain the encrypted passwords
518 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
520 ASN1_DATA *data;
521 DATA_BLOB ret;
523 data = asn1_init(talloc_tos());
524 if (data == NULL) {
525 return data_blob_null;
528 asn1_push_tag(data, ASN1_CONTEXT(1));
529 asn1_push_tag(data, ASN1_SEQUENCE(0));
530 asn1_push_tag(data, ASN1_CONTEXT(2));
531 asn1_write_OctetString(data,blob.data,blob.length);
532 asn1_pop_tag(data);
533 asn1_pop_tag(data);
534 asn1_pop_tag(data);
536 ret = data_blob(data->data, data->length);
538 asn1_free(data);
540 return ret;
544 parse a SPNEGO auth packet. This contains the encrypted passwords
546 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
548 ssize_t len;
549 struct spnego_data token;
551 len = spnego_read_data(talloc_tos(), blob, &token);
552 if (len == -1) {
553 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
554 return false;
557 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
558 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
559 token.type));
560 spnego_free_data(&token);
561 return false;
564 *auth = data_blob_talloc(talloc_tos(),
565 token.negTokenTarg.responseToken.data,
566 token.negTokenTarg.responseToken.length);
567 spnego_free_data(&token);
569 return true;
573 generate a minimal SPNEGO response packet. Doesn't contain much.
575 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
576 const char *mechOID)
578 ASN1_DATA *data;
579 DATA_BLOB ret;
580 uint8 negResult;
582 if (NT_STATUS_IS_OK(nt_status)) {
583 negResult = SPNEGO_ACCEPT_COMPLETED;
584 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
585 negResult = SPNEGO_ACCEPT_INCOMPLETE;
586 } else {
587 negResult = SPNEGO_REJECT;
590 data = asn1_init(talloc_tos());
591 if (data == NULL) {
592 return data_blob_null;
595 asn1_push_tag(data, ASN1_CONTEXT(1));
596 asn1_push_tag(data, ASN1_SEQUENCE(0));
597 asn1_push_tag(data, ASN1_CONTEXT(0));
598 asn1_write_enumerated(data, negResult);
599 asn1_pop_tag(data);
601 if (mechOID) {
602 asn1_push_tag(data,ASN1_CONTEXT(1));
603 asn1_write_OID(data, mechOID);
604 asn1_pop_tag(data);
607 if (reply && reply->data != NULL) {
608 asn1_push_tag(data,ASN1_CONTEXT(2));
609 asn1_write_OctetString(data, reply->data, reply->length);
610 asn1_pop_tag(data);
613 asn1_pop_tag(data);
614 asn1_pop_tag(data);
616 ret = data_blob(data->data, data->length);
617 asn1_free(data);
618 return ret;
622 parse a SPNEGO auth packet. This contains the encrypted passwords
624 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
625 const char *mechOID,
626 DATA_BLOB *auth)
628 ASN1_DATA *data;
629 uint8 negResult;
631 if (NT_STATUS_IS_OK(nt_status)) {
632 negResult = SPNEGO_ACCEPT_COMPLETED;
633 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
634 negResult = SPNEGO_ACCEPT_INCOMPLETE;
635 } else {
636 negResult = SPNEGO_REJECT;
639 data = asn1_init(talloc_tos());
640 if (data == NULL) {
641 return false;
644 asn1_load(data, blob);
645 asn1_start_tag(data, ASN1_CONTEXT(1));
646 asn1_start_tag(data, ASN1_SEQUENCE(0));
647 asn1_start_tag(data, ASN1_CONTEXT(0));
648 asn1_check_enumerated(data, negResult);
649 asn1_end_tag(data);
651 *auth = data_blob_null;
653 if (asn1_tag_remaining(data)) {
654 asn1_start_tag(data,ASN1_CONTEXT(1));
655 asn1_check_OID(data, mechOID);
656 asn1_end_tag(data);
658 if (asn1_tag_remaining(data)) {
659 asn1_start_tag(data,ASN1_CONTEXT(2));
660 asn1_read_OctetString(data, talloc_autofree_context(), auth);
661 asn1_end_tag(data);
663 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
664 data->has_error = 1;
667 /* Binding against Win2K DC returns a duplicate of the responseToken in
668 * the optional mechListMIC field. This is a bug in Win2K. We ignore
669 * this field if it exists. Win2K8 may return a proper mechListMIC at
670 * which point we need to implement the integrity checking. */
671 if (asn1_tag_remaining(data)) {
672 DATA_BLOB mechList = data_blob_null;
673 asn1_start_tag(data, ASN1_CONTEXT(3));
674 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
675 asn1_end_tag(data);
676 data_blob_free(&mechList);
677 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
678 "ignoring.\n"));
681 asn1_end_tag(data);
682 asn1_end_tag(data);
684 if (data->has_error) {
685 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
686 asn1_free(data);
687 data_blob_free(auth);
688 return False;
691 asn1_free(data);
692 return True;