WHATSNEW: Start release notes for Samba 3.6.14.
[Samba.git] / source3 / libsmb / clispnego.c
blob98b575dc5a0c978d62ec7023d2475e175ad4b9be
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 for (i = 0; i < ASN1_MAX_OIDS; i++) {
107 OIDs[i] = NULL;
110 data = asn1_init(talloc_tos());
111 if (data == NULL) {
112 return false;
115 asn1_load(data, blob);
117 asn1_start_tag(data,ASN1_APPLICATION(0));
119 asn1_check_OID(data,OID_SPNEGO);
121 /* negTokenInit [0] NegTokenInit */
122 asn1_start_tag(data,ASN1_CONTEXT(0));
123 asn1_start_tag(data,ASN1_SEQUENCE(0));
125 /* mechTypes [0] MechTypeList OPTIONAL */
127 /* Not really optional, we depend on this to decide
128 * what mechanisms we have to work with. */
130 asn1_start_tag(data,ASN1_CONTEXT(0));
131 asn1_start_tag(data,ASN1_SEQUENCE(0));
132 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
133 asn1_read_OID(data,ctx, &OIDs[i]);
134 if (data->has_error) {
135 break;
138 OIDs[i] = NULL;
139 asn1_end_tag(data);
140 asn1_end_tag(data);
142 if (principal) {
143 *principal = NULL;
145 if (secblob) {
146 *secblob = data_blob_null;
150 Win7 + Live Sign-in Assistant attaches a mechToken
151 ASN1_CONTEXT(2) to the negTokenInit packet
152 which breaks our negotiation if we just assume
153 the next tag is ASN1_CONTEXT(3).
156 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
157 uint8 flags;
159 /* reqFlags [1] ContextFlags OPTIONAL */
160 asn1_start_tag(data, ASN1_CONTEXT(1));
161 asn1_start_tag(data, ASN1_BIT_STRING);
162 while (asn1_tag_remaining(data) > 0) {
163 asn1_read_uint8(data, &flags);
165 asn1_end_tag(data);
166 asn1_end_tag(data);
169 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
170 DATA_BLOB sblob = data_blob_null;
171 /* mechToken [2] OCTET STRING OPTIONAL */
172 asn1_start_tag(data, ASN1_CONTEXT(2));
173 asn1_read_OctetString(data, ctx, &sblob);
174 asn1_end_tag(data);
175 if (secblob) {
176 *secblob = sblob;
177 } else {
178 data_blob_free(&sblob);
182 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
183 char *princ = NULL;
184 /* mechListMIC [3] OCTET STRING OPTIONAL */
185 asn1_start_tag(data, ASN1_CONTEXT(3));
186 asn1_start_tag(data, ASN1_SEQUENCE(0));
187 asn1_start_tag(data, ASN1_CONTEXT(0));
188 asn1_read_GeneralString(data, ctx, &princ);
189 asn1_end_tag(data);
190 asn1_end_tag(data);
191 asn1_end_tag(data);
192 if (principal) {
193 *principal = princ;
194 } else {
195 TALLOC_FREE(princ);
199 asn1_end_tag(data);
200 asn1_end_tag(data);
202 asn1_end_tag(data);
204 ret = !data->has_error;
205 if (data->has_error) {
206 int j;
207 if (principal) {
208 TALLOC_FREE(*principal);
210 if (secblob) {
211 data_blob_free(secblob);
213 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
214 TALLOC_FREE(OIDs[j]);
218 asn1_free(data);
219 return ret;
223 generate a krb5 GSS-API wrapper packet given a ticket
225 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
227 ASN1_DATA *data;
228 DATA_BLOB ret;
230 data = asn1_init(talloc_tos());
231 if (data == NULL) {
232 return data_blob_null;
235 asn1_push_tag(data, ASN1_APPLICATION(0));
236 asn1_write_OID(data, OID_KERBEROS5);
238 asn1_write(data, tok_id, 2);
239 asn1_write(data, ticket.data, ticket.length);
240 asn1_pop_tag(data);
242 if (data->has_error) {
243 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
246 ret = data_blob_talloc(ctx, data->data, data->length);
247 asn1_free(data);
249 return ret;
253 parse a krb5 GSS-API wrapper packet giving a ticket
255 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
257 bool ret;
258 ASN1_DATA *data;
259 int data_remaining;
260 *ticket = data_blob_null;
262 data = asn1_init(talloc_tos());
263 if (data == NULL) {
264 return false;
267 asn1_load(data, blob);
268 asn1_start_tag(data, ASN1_APPLICATION(0));
269 asn1_check_OID(data, OID_KERBEROS5);
271 data_remaining = asn1_tag_remaining(data);
273 if (data_remaining < 3) {
274 data->has_error = True;
275 } else {
276 asn1_read(data, tok_id, 2);
277 data_remaining -= 2;
278 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
279 asn1_read(data, ticket->data, ticket->length);
282 asn1_end_tag(data);
284 ret = !data->has_error;
286 if (data->has_error) {
287 data_blob_free(ticket);
290 asn1_free(data);
292 return ret;
297 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
298 kerberos session setup
300 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
301 const char *principal, int time_offset,
302 DATA_BLOB *targ,
303 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
304 time_t *expire_time)
306 int retval;
307 DATA_BLOB tkt, tkt_wrapped;
308 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
310 /* get a kerberos ticket for the service and extract the session key */
311 retval = cli_krb5_get_ticket(ctx, principal, time_offset,
312 &tkt, session_key_krb5,
313 extra_ap_opts, NULL,
314 expire_time, NULL);
315 if (retval) {
316 return retval;
319 /* wrap that up in a nice GSS-API wrapping */
320 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
322 /* and wrap that in a shiny SPNEGO wrapper */
323 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
325 data_blob_free(&tkt_wrapped);
326 data_blob_free(&tkt);
328 return retval;
333 parse a spnego NTLMSSP challenge packet giving two security blobs
335 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
336 DATA_BLOB *chal1, DATA_BLOB *chal2)
338 bool ret;
339 ASN1_DATA *data;
341 ZERO_STRUCTP(chal1);
342 ZERO_STRUCTP(chal2);
344 data = asn1_init(talloc_tos());
345 if (data == NULL) {
346 return false;
349 asn1_load(data, blob);
350 asn1_start_tag(data,ASN1_CONTEXT(1));
351 asn1_start_tag(data,ASN1_SEQUENCE(0));
353 asn1_start_tag(data,ASN1_CONTEXT(0));
354 asn1_check_enumerated(data,1);
355 asn1_end_tag(data);
357 asn1_start_tag(data,ASN1_CONTEXT(1));
358 asn1_check_OID(data, OID_NTLMSSP);
359 asn1_end_tag(data);
361 asn1_start_tag(data,ASN1_CONTEXT(2));
362 asn1_read_OctetString(data, ctx, chal1);
363 asn1_end_tag(data);
365 /* the second challenge is optional (XP doesn't send it) */
366 if (asn1_tag_remaining(data)) {
367 asn1_start_tag(data,ASN1_CONTEXT(3));
368 asn1_read_OctetString(data, ctx, chal2);
369 asn1_end_tag(data);
372 asn1_end_tag(data);
373 asn1_end_tag(data);
375 ret = !data->has_error;
377 if (data->has_error) {
378 data_blob_free(chal1);
379 data_blob_free(chal2);
382 asn1_free(data);
383 return ret;
388 generate a SPNEGO auth packet. This will contain the encrypted passwords
390 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
392 ASN1_DATA *data;
393 DATA_BLOB ret;
395 data = asn1_init(talloc_tos());
396 if (data == NULL) {
397 return data_blob_null;
400 asn1_push_tag(data, ASN1_CONTEXT(1));
401 asn1_push_tag(data, ASN1_SEQUENCE(0));
402 asn1_push_tag(data, ASN1_CONTEXT(2));
403 asn1_write_OctetString(data,blob.data,blob.length);
404 asn1_pop_tag(data);
405 asn1_pop_tag(data);
406 asn1_pop_tag(data);
408 ret = data_blob_talloc(ctx, data->data, data->length);
410 asn1_free(data);
412 return ret;
416 parse a SPNEGO auth packet. This contains the encrypted passwords
418 bool spnego_parse_auth_and_mic(TALLOC_CTX *ctx, DATA_BLOB blob,
419 DATA_BLOB *auth, DATA_BLOB *signature)
421 ssize_t len;
422 struct spnego_data token;
424 len = spnego_read_data(talloc_tos(), blob, &token);
425 if (len == -1) {
426 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
427 return false;
430 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
431 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
432 token.type));
433 spnego_free_data(&token);
434 return false;
437 *auth = data_blob_talloc(ctx,
438 token.negTokenTarg.responseToken.data,
439 token.negTokenTarg.responseToken.length);
441 if (!signature) {
442 goto done;
445 *signature = data_blob_talloc(ctx,
446 token.negTokenTarg.mechListMIC.data,
447 token.negTokenTarg.mechListMIC.length);
449 done:
450 spnego_free_data(&token);
452 return true;
455 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
457 return spnego_parse_auth_and_mic(ctx, blob, auth, NULL);
461 generate a minimal SPNEGO response packet. Doesn't contain much.
463 DATA_BLOB spnego_gen_auth_response_and_mic(TALLOC_CTX *ctx,
464 NTSTATUS nt_status,
465 const char *mechOID,
466 DATA_BLOB *reply,
467 DATA_BLOB *mechlistMIC)
469 ASN1_DATA *data;
470 DATA_BLOB ret;
471 uint8 negResult;
473 if (NT_STATUS_IS_OK(nt_status)) {
474 negResult = SPNEGO_ACCEPT_COMPLETED;
475 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
476 negResult = SPNEGO_ACCEPT_INCOMPLETE;
477 } else {
478 negResult = SPNEGO_REJECT;
481 data = asn1_init(talloc_tos());
482 if (data == NULL) {
483 return data_blob_null;
486 asn1_push_tag(data, ASN1_CONTEXT(1));
487 asn1_push_tag(data, ASN1_SEQUENCE(0));
488 asn1_push_tag(data, ASN1_CONTEXT(0));
489 asn1_write_enumerated(data, negResult);
490 asn1_pop_tag(data);
492 if (mechOID) {
493 asn1_push_tag(data,ASN1_CONTEXT(1));
494 asn1_write_OID(data, mechOID);
495 asn1_pop_tag(data);
498 if (reply && reply->data != NULL) {
499 asn1_push_tag(data,ASN1_CONTEXT(2));
500 asn1_write_OctetString(data, reply->data, reply->length);
501 asn1_pop_tag(data);
504 if (mechlistMIC && mechlistMIC->data != NULL) {
505 asn1_push_tag(data, ASN1_CONTEXT(3));
506 asn1_write_OctetString(data,
507 mechlistMIC->data,
508 mechlistMIC->length);
509 asn1_pop_tag(data);
512 asn1_pop_tag(data);
513 asn1_pop_tag(data);
515 ret = data_blob_talloc(ctx, data->data, data->length);
516 asn1_free(data);
517 return ret;
520 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx, DATA_BLOB *reply,
521 NTSTATUS nt_status, const char *mechOID)
523 return spnego_gen_auth_response_and_mic(ctx, nt_status,
524 mechOID, reply, NULL);
528 parse a SPNEGO auth packet. This contains the encrypted passwords
530 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
531 DATA_BLOB blob, NTSTATUS nt_status,
532 const char *mechOID,
533 DATA_BLOB *auth)
535 ASN1_DATA *data;
536 uint8 negResult;
538 if (NT_STATUS_IS_OK(nt_status)) {
539 negResult = SPNEGO_ACCEPT_COMPLETED;
540 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
541 negResult = SPNEGO_ACCEPT_INCOMPLETE;
542 } else {
543 negResult = SPNEGO_REJECT;
546 data = asn1_init(talloc_tos());
547 if (data == NULL) {
548 return false;
551 asn1_load(data, blob);
552 asn1_start_tag(data, ASN1_CONTEXT(1));
553 asn1_start_tag(data, ASN1_SEQUENCE(0));
554 asn1_start_tag(data, ASN1_CONTEXT(0));
555 asn1_check_enumerated(data, negResult);
556 asn1_end_tag(data);
558 *auth = data_blob_null;
560 if (asn1_tag_remaining(data)) {
561 asn1_start_tag(data,ASN1_CONTEXT(1));
562 asn1_check_OID(data, mechOID);
563 asn1_end_tag(data);
565 if (asn1_tag_remaining(data)) {
566 asn1_start_tag(data,ASN1_CONTEXT(2));
567 asn1_read_OctetString(data, ctx, auth);
568 asn1_end_tag(data);
570 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
571 data->has_error = 1;
574 /* Binding against Win2K DC returns a duplicate of the responseToken in
575 * the optional mechListMIC field. This is a bug in Win2K. We ignore
576 * this field if it exists. Win2K8 may return a proper mechListMIC at
577 * which point we need to implement the integrity checking. */
578 if (asn1_tag_remaining(data)) {
579 DATA_BLOB mechList = data_blob_null;
580 asn1_start_tag(data, ASN1_CONTEXT(3));
581 asn1_read_OctetString(data, ctx, &mechList);
582 asn1_end_tag(data);
583 data_blob_free(&mechList);
584 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
585 "ignoring.\n"));
588 asn1_end_tag(data);
589 asn1_end_tag(data);
591 if (data->has_error) {
592 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
593 asn1_free(data);
594 data_blob_free(auth);
595 return False;
598 asn1_free(data);
599 return True;
602 bool spnego_mech_list_blob(TALLOC_CTX *mem_ctx,
603 char **oid_list, DATA_BLOB *raw_data)
605 ASN1_DATA *data;
606 unsigned int idx;
608 if (!oid_list || !oid_list[0] || !raw_data) {
609 return false;
612 data = asn1_init(talloc_tos());
613 if (data == NULL) {
614 return false;
617 asn1_push_tag(data, ASN1_SEQUENCE(0));
618 for (idx = 0; oid_list[idx]; idx++) {
619 asn1_write_OID(data, oid_list[idx]);
621 asn1_pop_tag(data);
623 if (data->has_error) {
624 DEBUG(3, (__location__ " failed at %d\n", (int)data->ofs));
625 asn1_free(data);
626 return false;
629 *raw_data = data_blob_talloc(mem_ctx, data->data, data->length);
630 if (!raw_data->data) {
631 DEBUG(3, (__location__": data_blob_talloc() failed!\n"));
632 asn1_free(data);
633 return false;
636 asn1_free(data);
637 return true;