s3: Fix uninitialized memory read in talloc_free()
[Samba.git] / source3 / libsmb / clispnego.c
blob0a907ba7197d3fbbd15fe68b70aab53358cf28be
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
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/>.
23 #include "includes.h"
24 #include "../libcli/auth/spnego.h"
25 #include "smb_krb5.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,
34 const char *OIDs[],
35 DATA_BLOB *psecblob,
36 const char *principal)
38 int i;
39 ASN1_DATA *data;
40 DATA_BLOB ret;
42 data = asn1_init(talloc_tos());
43 if (data == NULL) {
44 return data_blob_null;
47 asn1_push_tag(data,ASN1_APPLICATION(0));
48 asn1_write_OID(data,OID_SPNEGO);
49 asn1_push_tag(data,ASN1_CONTEXT(0));
50 asn1_push_tag(data,ASN1_SEQUENCE(0));
52 asn1_push_tag(data,ASN1_CONTEXT(0));
53 asn1_push_tag(data,ASN1_SEQUENCE(0));
54 for (i=0; OIDs[i]; i++) {
55 asn1_write_OID(data,OIDs[i]);
57 asn1_pop_tag(data);
58 asn1_pop_tag(data);
60 if (psecblob && psecblob->length && psecblob->data) {
61 asn1_push_tag(data, ASN1_CONTEXT(2));
62 asn1_write_OctetString(data,psecblob->data,
63 psecblob->length);
64 asn1_pop_tag(data);
67 if (principal) {
68 asn1_push_tag(data, ASN1_CONTEXT(3));
69 asn1_push_tag(data, ASN1_SEQUENCE(0));
70 asn1_push_tag(data, ASN1_CONTEXT(0));
71 asn1_write_GeneralString(data,principal);
72 asn1_pop_tag(data);
73 asn1_pop_tag(data);
74 asn1_pop_tag(data);
77 asn1_pop_tag(data);
78 asn1_pop_tag(data);
80 asn1_pop_tag(data);
82 if (data->has_error) {
83 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
86 ret = data_blob_talloc(ctx, data->data, data->length);
87 asn1_free(data);
89 return ret;
93 parse a negTokenInit packet giving a GUID, a list of supported
94 OIDs (the mechanisms) and a principal name string
96 bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
97 DATA_BLOB blob,
98 char *OIDs[ASN1_MAX_OIDS],
99 char **principal,
100 DATA_BLOB *secblob)
102 int i;
103 bool ret;
104 ASN1_DATA *data;
106 data = asn1_init(talloc_tos());
107 if (data == NULL) {
108 return false;
111 asn1_load(data, blob);
113 asn1_start_tag(data,ASN1_APPLICATION(0));
115 asn1_check_OID(data,OID_SPNEGO);
117 /* negTokenInit [0] NegTokenInit */
118 asn1_start_tag(data,ASN1_CONTEXT(0));
119 asn1_start_tag(data,ASN1_SEQUENCE(0));
121 /* mechTypes [0] MechTypeList OPTIONAL */
123 /* Not really optional, we depend on this to decide
124 * what mechanisms we have to work with. */
126 asn1_start_tag(data,ASN1_CONTEXT(0));
127 asn1_start_tag(data,ASN1_SEQUENCE(0));
128 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
129 asn1_read_OID(data,ctx, &OIDs[i]);
130 if (data->has_error) {
131 break;
134 OIDs[i] = NULL;
135 asn1_end_tag(data);
136 asn1_end_tag(data);
138 if (principal) {
139 *principal = NULL;
141 if (secblob) {
142 *secblob = data_blob_null;
146 Win7 + Live Sign-in Assistant attaches a mechToken
147 ASN1_CONTEXT(2) to the negTokenInit packet
148 which breaks our negotiation if we just assume
149 the next tag is ASN1_CONTEXT(3).
152 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
153 uint8 flags;
155 /* reqFlags [1] ContextFlags OPTIONAL */
156 asn1_start_tag(data, ASN1_CONTEXT(1));
157 asn1_start_tag(data, ASN1_BIT_STRING);
158 while (asn1_tag_remaining(data) > 0) {
159 asn1_read_uint8(data, &flags);
161 asn1_end_tag(data);
162 asn1_end_tag(data);
165 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
166 DATA_BLOB sblob = data_blob_null;
167 /* mechToken [2] OCTET STRING OPTIONAL */
168 asn1_start_tag(data, ASN1_CONTEXT(2));
169 asn1_read_OctetString(data, ctx, &sblob);
170 asn1_end_tag(data);
171 if (secblob) {
172 *secblob = sblob;
173 } else {
174 data_blob_free(&sblob);
178 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
179 char *princ = NULL;
180 /* mechListMIC [3] OCTET STRING OPTIONAL */
181 asn1_start_tag(data, ASN1_CONTEXT(3));
182 asn1_start_tag(data, ASN1_SEQUENCE(0));
183 asn1_start_tag(data, ASN1_CONTEXT(0));
184 asn1_read_GeneralString(data, ctx, &princ);
185 asn1_end_tag(data);
186 asn1_end_tag(data);
187 asn1_end_tag(data);
188 if (principal) {
189 *principal = princ;
190 } else {
191 TALLOC_FREE(princ);
195 asn1_end_tag(data);
196 asn1_end_tag(data);
198 asn1_end_tag(data);
200 ret = !data->has_error;
201 if (data->has_error) {
202 int j;
203 if (principal) {
204 TALLOC_FREE(*principal);
206 if (secblob) {
207 data_blob_free(secblob);
209 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
210 TALLOC_FREE(OIDs[j]);
214 asn1_free(data);
215 return ret;
219 generate a krb5 GSS-API wrapper packet given a ticket
221 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
223 ASN1_DATA *data;
224 DATA_BLOB ret;
226 data = asn1_init(talloc_tos());
227 if (data == NULL) {
228 return data_blob_null;
231 asn1_push_tag(data, ASN1_APPLICATION(0));
232 asn1_write_OID(data, OID_KERBEROS5);
234 asn1_write(data, tok_id, 2);
235 asn1_write(data, ticket.data, ticket.length);
236 asn1_pop_tag(data);
238 if (data->has_error) {
239 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
242 ret = data_blob_talloc(ctx, data->data, data->length);
243 asn1_free(data);
245 return ret;
249 parse a krb5 GSS-API wrapper packet giving a ticket
251 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
253 bool ret;
254 ASN1_DATA *data;
255 int data_remaining;
257 data = asn1_init(talloc_tos());
258 if (data == NULL) {
259 return false;
262 asn1_load(data, blob);
263 asn1_start_tag(data, ASN1_APPLICATION(0));
264 asn1_check_OID(data, OID_KERBEROS5);
266 data_remaining = asn1_tag_remaining(data);
268 if (data_remaining < 3) {
269 data->has_error = True;
270 } else {
271 asn1_read(data, tok_id, 2);
272 data_remaining -= 2;
273 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
274 asn1_read(data, ticket->data, ticket->length);
277 asn1_end_tag(data);
279 ret = !data->has_error;
281 if (data->has_error) {
282 data_blob_free(ticket);
285 asn1_free(data);
287 return ret;
292 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
293 kerberos session setup
295 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
296 const char *principal, int time_offset,
297 DATA_BLOB *targ,
298 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
299 time_t *expire_time)
301 int retval;
302 DATA_BLOB tkt, tkt_wrapped;
303 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
305 /* get a kerberos ticket for the service and extract the session key */
306 retval = cli_krb5_get_ticket(ctx, principal, time_offset,
307 &tkt, session_key_krb5,
308 extra_ap_opts, NULL,
309 expire_time, NULL);
310 if (retval) {
311 return retval;
314 /* wrap that up in a nice GSS-API wrapping */
315 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
317 /* and wrap that in a shiny SPNEGO wrapper */
318 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
320 data_blob_free(&tkt_wrapped);
321 data_blob_free(&tkt);
323 return retval;
328 parse a spnego NTLMSSP challenge packet giving two security blobs
330 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
331 DATA_BLOB *chal1, DATA_BLOB *chal2)
333 bool ret;
334 ASN1_DATA *data;
336 ZERO_STRUCTP(chal1);
337 ZERO_STRUCTP(chal2);
339 data = asn1_init(talloc_tos());
340 if (data == NULL) {
341 return false;
344 asn1_load(data, blob);
345 asn1_start_tag(data,ASN1_CONTEXT(1));
346 asn1_start_tag(data,ASN1_SEQUENCE(0));
348 asn1_start_tag(data,ASN1_CONTEXT(0));
349 asn1_check_enumerated(data,1);
350 asn1_end_tag(data);
352 asn1_start_tag(data,ASN1_CONTEXT(1));
353 asn1_check_OID(data, OID_NTLMSSP);
354 asn1_end_tag(data);
356 asn1_start_tag(data,ASN1_CONTEXT(2));
357 asn1_read_OctetString(data, ctx, chal1);
358 asn1_end_tag(data);
360 /* the second challenge is optional (XP doesn't send it) */
361 if (asn1_tag_remaining(data)) {
362 asn1_start_tag(data,ASN1_CONTEXT(3));
363 asn1_read_OctetString(data, ctx, chal2);
364 asn1_end_tag(data);
367 asn1_end_tag(data);
368 asn1_end_tag(data);
370 ret = !data->has_error;
372 if (data->has_error) {
373 data_blob_free(chal1);
374 data_blob_free(chal2);
377 asn1_free(data);
378 return ret;
383 generate a SPNEGO auth packet. This will contain the encrypted passwords
385 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
387 ASN1_DATA *data;
388 DATA_BLOB ret;
390 data = asn1_init(talloc_tos());
391 if (data == NULL) {
392 return data_blob_null;
395 asn1_push_tag(data, ASN1_CONTEXT(1));
396 asn1_push_tag(data, ASN1_SEQUENCE(0));
397 asn1_push_tag(data, ASN1_CONTEXT(2));
398 asn1_write_OctetString(data,blob.data,blob.length);
399 asn1_pop_tag(data);
400 asn1_pop_tag(data);
401 asn1_pop_tag(data);
403 ret = data_blob_talloc(ctx, data->data, data->length);
405 asn1_free(data);
407 return ret;
411 parse a SPNEGO auth packet. This contains the encrypted passwords
413 bool spnego_parse_auth_and_mic(TALLOC_CTX *ctx, DATA_BLOB blob,
414 DATA_BLOB *auth, DATA_BLOB *signature)
416 ssize_t len;
417 struct spnego_data token;
419 len = spnego_read_data(talloc_tos(), blob, &token);
420 if (len == -1) {
421 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
422 return false;
425 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
426 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
427 token.type));
428 spnego_free_data(&token);
429 return false;
432 *auth = data_blob_talloc(ctx,
433 token.negTokenTarg.responseToken.data,
434 token.negTokenTarg.responseToken.length);
436 if (!signature) {
437 goto done;
440 *signature = data_blob_talloc(ctx,
441 token.negTokenTarg.mechListMIC.data,
442 token.negTokenTarg.mechListMIC.length);
444 done:
445 spnego_free_data(&token);
447 return true;
450 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
452 return spnego_parse_auth_and_mic(ctx, blob, auth, NULL);
456 generate a minimal SPNEGO response packet. Doesn't contain much.
458 DATA_BLOB spnego_gen_auth_response_and_mic(TALLOC_CTX *ctx,
459 NTSTATUS nt_status,
460 const char *mechOID,
461 DATA_BLOB *reply,
462 DATA_BLOB *mechlistMIC)
464 ASN1_DATA *data;
465 DATA_BLOB ret;
466 uint8 negResult;
468 if (NT_STATUS_IS_OK(nt_status)) {
469 negResult = SPNEGO_ACCEPT_COMPLETED;
470 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
471 negResult = SPNEGO_ACCEPT_INCOMPLETE;
472 } else {
473 negResult = SPNEGO_REJECT;
476 data = asn1_init(talloc_tos());
477 if (data == NULL) {
478 return data_blob_null;
481 asn1_push_tag(data, ASN1_CONTEXT(1));
482 asn1_push_tag(data, ASN1_SEQUENCE(0));
483 asn1_push_tag(data, ASN1_CONTEXT(0));
484 asn1_write_enumerated(data, negResult);
485 asn1_pop_tag(data);
487 if (mechOID) {
488 asn1_push_tag(data,ASN1_CONTEXT(1));
489 asn1_write_OID(data, mechOID);
490 asn1_pop_tag(data);
493 if (reply && reply->data != NULL) {
494 asn1_push_tag(data,ASN1_CONTEXT(2));
495 asn1_write_OctetString(data, reply->data, reply->length);
496 asn1_pop_tag(data);
499 if (mechlistMIC && mechlistMIC->data != NULL) {
500 asn1_push_tag(data, ASN1_CONTEXT(3));
501 asn1_write_OctetString(data,
502 mechlistMIC->data,
503 mechlistMIC->length);
504 asn1_pop_tag(data);
507 asn1_pop_tag(data);
508 asn1_pop_tag(data);
510 ret = data_blob_talloc(ctx, data->data, data->length);
511 asn1_free(data);
512 return ret;
515 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx, DATA_BLOB *reply,
516 NTSTATUS nt_status, const char *mechOID)
518 return spnego_gen_auth_response_and_mic(ctx, nt_status,
519 mechOID, reply, NULL);
523 parse a SPNEGO auth packet. This contains the encrypted passwords
525 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
526 DATA_BLOB blob, NTSTATUS nt_status,
527 const char *mechOID,
528 DATA_BLOB *auth)
530 ASN1_DATA *data;
531 uint8 negResult;
533 if (NT_STATUS_IS_OK(nt_status)) {
534 negResult = SPNEGO_ACCEPT_COMPLETED;
535 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
536 negResult = SPNEGO_ACCEPT_INCOMPLETE;
537 } else {
538 negResult = SPNEGO_REJECT;
541 data = asn1_init(talloc_tos());
542 if (data == NULL) {
543 return false;
546 asn1_load(data, blob);
547 asn1_start_tag(data, ASN1_CONTEXT(1));
548 asn1_start_tag(data, ASN1_SEQUENCE(0));
549 asn1_start_tag(data, ASN1_CONTEXT(0));
550 asn1_check_enumerated(data, negResult);
551 asn1_end_tag(data);
553 *auth = data_blob_null;
555 if (asn1_tag_remaining(data)) {
556 asn1_start_tag(data,ASN1_CONTEXT(1));
557 asn1_check_OID(data, mechOID);
558 asn1_end_tag(data);
560 if (asn1_tag_remaining(data)) {
561 asn1_start_tag(data,ASN1_CONTEXT(2));
562 asn1_read_OctetString(data, ctx, auth);
563 asn1_end_tag(data);
565 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
566 data->has_error = 1;
569 /* Binding against Win2K DC returns a duplicate of the responseToken in
570 * the optional mechListMIC field. This is a bug in Win2K. We ignore
571 * this field if it exists. Win2K8 may return a proper mechListMIC at
572 * which point we need to implement the integrity checking. */
573 if (asn1_tag_remaining(data)) {
574 DATA_BLOB mechList = data_blob_null;
575 asn1_start_tag(data, ASN1_CONTEXT(3));
576 asn1_read_OctetString(data, ctx, &mechList);
577 asn1_end_tag(data);
578 data_blob_free(&mechList);
579 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
580 "ignoring.\n"));
583 asn1_end_tag(data);
584 asn1_end_tag(data);
586 if (data->has_error) {
587 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
588 asn1_free(data);
589 data_blob_free(auth);
590 return False;
593 asn1_free(data);
594 return True;
597 bool spnego_mech_list_blob(TALLOC_CTX *mem_ctx,
598 char **oid_list, DATA_BLOB *raw_data)
600 ASN1_DATA *data;
601 unsigned int idx;
603 if (!oid_list || !oid_list[0] || !raw_data) {
604 return false;
607 data = asn1_init(talloc_tos());
608 if (data == NULL) {
609 return false;
612 asn1_push_tag(data, ASN1_SEQUENCE(0));
613 for (idx = 0; oid_list[idx]; idx++) {
614 asn1_write_OID(data, oid_list[idx]);
616 asn1_pop_tag(data);
618 if (data->has_error) {
619 DEBUG(3, (__location__ " failed at %d\n", (int)data->ofs));
620 asn1_free(data);
621 return false;
624 *raw_data = data_blob_talloc(mem_ctx, data->data, data->length);
625 if (!raw_data->data) {
626 DEBUG(3, (__location__": data_blob_talloc() failed!\n"));
627 asn1_free(data);
628 return false;
631 asn1_free(data);
632 return true;