s3-printing: Initiate pcap reload from parent smbd
[Samba.git] / source3 / libsmb / clispnego.c
blob382a29ac657deeb1ab895f3bed8087dffa6e80f4
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"
28 generate a negTokenInit packet given a list of supported
29 OIDs (the mechanisms) a blob, and a principal name string
32 DATA_BLOB spnego_gen_negTokenInit(TALLOC_CTX *ctx,
33 const char *OIDs[],
34 DATA_BLOB *psecblob,
35 const char *principal)
37 int i;
38 ASN1_DATA *data;
39 DATA_BLOB ret;
41 data = asn1_init(talloc_tos());
42 if (data == NULL) {
43 return data_blob_null;
46 asn1_push_tag(data,ASN1_APPLICATION(0));
47 asn1_write_OID(data,OID_SPNEGO);
48 asn1_push_tag(data,ASN1_CONTEXT(0));
49 asn1_push_tag(data,ASN1_SEQUENCE(0));
51 asn1_push_tag(data,ASN1_CONTEXT(0));
52 asn1_push_tag(data,ASN1_SEQUENCE(0));
53 for (i=0; OIDs[i]; i++) {
54 asn1_write_OID(data,OIDs[i]);
56 asn1_pop_tag(data);
57 asn1_pop_tag(data);
59 if (psecblob && psecblob->length && psecblob->data) {
60 asn1_push_tag(data, ASN1_CONTEXT(2));
61 asn1_write_OctetString(data,psecblob->data,
62 psecblob->length);
63 asn1_pop_tag(data);
66 if (principal) {
67 asn1_push_tag(data, ASN1_CONTEXT(3));
68 asn1_push_tag(data, ASN1_SEQUENCE(0));
69 asn1_push_tag(data, ASN1_CONTEXT(0));
70 asn1_write_GeneralString(data,principal);
71 asn1_pop_tag(data);
72 asn1_pop_tag(data);
73 asn1_pop_tag(data);
76 asn1_pop_tag(data);
77 asn1_pop_tag(data);
79 asn1_pop_tag(data);
81 if (data->has_error) {
82 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
85 ret = data_blob_talloc(ctx, data->data, data->length);
86 asn1_free(data);
88 return ret;
92 parse a negTokenInit packet giving a GUID, a list of supported
93 OIDs (the mechanisms) and a principal name string
95 bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
96 DATA_BLOB blob,
97 char *OIDs[ASN1_MAX_OIDS],
98 char **principal,
99 DATA_BLOB *secblob)
101 int i;
102 bool ret;
103 ASN1_DATA *data;
105 data = asn1_init(talloc_tos());
106 if (data == NULL) {
107 return false;
110 asn1_load(data, blob);
112 asn1_start_tag(data,ASN1_APPLICATION(0));
114 asn1_check_OID(data,OID_SPNEGO);
116 /* negTokenInit [0] NegTokenInit */
117 asn1_start_tag(data,ASN1_CONTEXT(0));
118 asn1_start_tag(data,ASN1_SEQUENCE(0));
120 /* mechTypes [0] MechTypeList OPTIONAL */
122 /* Not really optional, we depend on this to decide
123 * what mechanisms we have to work with. */
125 asn1_start_tag(data,ASN1_CONTEXT(0));
126 asn1_start_tag(data,ASN1_SEQUENCE(0));
127 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
128 asn1_read_OID(data,ctx, &OIDs[i]);
130 OIDs[i] = NULL;
131 asn1_end_tag(data);
132 asn1_end_tag(data);
134 if (principal) {
135 *principal = NULL;
137 if (secblob) {
138 *secblob = data_blob_null;
142 Win7 + Live Sign-in Assistant attaches a mechToken
143 ASN1_CONTEXT(2) to the negTokenInit packet
144 which breaks our negotiation if we just assume
145 the next tag is ASN1_CONTEXT(3).
148 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
149 uint8 flags;
151 /* reqFlags [1] ContextFlags OPTIONAL */
152 asn1_start_tag(data, ASN1_CONTEXT(1));
153 asn1_start_tag(data, ASN1_BIT_STRING);
154 while (asn1_tag_remaining(data) > 0) {
155 asn1_read_uint8(data, &flags);
157 asn1_end_tag(data);
158 asn1_end_tag(data);
161 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
162 DATA_BLOB sblob = data_blob_null;
163 /* mechToken [2] OCTET STRING OPTIONAL */
164 asn1_start_tag(data, ASN1_CONTEXT(2));
165 asn1_read_OctetString(data, ctx, &sblob);
166 asn1_end_tag(data);
167 if (secblob) {
168 *secblob = sblob;
169 } else {
170 data_blob_free(&sblob);
174 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
175 char *princ = NULL;
176 /* mechListMIC [3] OCTET STRING OPTIONAL */
177 asn1_start_tag(data, ASN1_CONTEXT(3));
178 asn1_start_tag(data, ASN1_SEQUENCE(0));
179 asn1_start_tag(data, ASN1_CONTEXT(0));
180 asn1_read_GeneralString(data, ctx, &princ);
181 asn1_end_tag(data);
182 asn1_end_tag(data);
183 asn1_end_tag(data);
184 if (principal) {
185 *principal = princ;
186 } else {
187 TALLOC_FREE(princ);
191 asn1_end_tag(data);
192 asn1_end_tag(data);
194 asn1_end_tag(data);
196 ret = !data->has_error;
197 if (data->has_error) {
198 int j;
199 if (principal) {
200 TALLOC_FREE(*principal);
202 if (secblob) {
203 data_blob_free(secblob);
205 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
206 TALLOC_FREE(OIDs[j]);
210 asn1_free(data);
211 return ret;
215 generate a krb5 GSS-API wrapper packet given a ticket
217 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
219 ASN1_DATA *data;
220 DATA_BLOB ret;
222 data = asn1_init(talloc_tos());
223 if (data == NULL) {
224 return data_blob_null;
227 asn1_push_tag(data, ASN1_APPLICATION(0));
228 asn1_write_OID(data, OID_KERBEROS5);
230 asn1_write(data, tok_id, 2);
231 asn1_write(data, ticket.data, ticket.length);
232 asn1_pop_tag(data);
234 if (data->has_error) {
235 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
238 ret = data_blob_talloc(ctx, data->data, data->length);
239 asn1_free(data);
241 return ret;
245 parse a krb5 GSS-API wrapper packet giving a ticket
247 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
249 bool ret;
250 ASN1_DATA *data;
251 int data_remaining;
253 data = asn1_init(talloc_tos());
254 if (data == NULL) {
255 return false;
258 asn1_load(data, blob);
259 asn1_start_tag(data, ASN1_APPLICATION(0));
260 asn1_check_OID(data, OID_KERBEROS5);
262 data_remaining = asn1_tag_remaining(data);
264 if (data_remaining < 3) {
265 data->has_error = True;
266 } else {
267 asn1_read(data, tok_id, 2);
268 data_remaining -= 2;
269 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
270 asn1_read(data, ticket->data, ticket->length);
273 asn1_end_tag(data);
275 ret = !data->has_error;
277 if (data->has_error) {
278 data_blob_free(ticket);
281 asn1_free(data);
283 return ret;
288 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
289 kerberos session setup
291 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
292 const char *principal, int time_offset,
293 DATA_BLOB *targ,
294 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
295 time_t *expire_time)
297 int retval;
298 DATA_BLOB tkt, tkt_wrapped;
299 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
301 /* get a kerberos ticket for the service and extract the session key */
302 retval = cli_krb5_get_ticket(ctx, principal, time_offset,
303 &tkt, session_key_krb5,
304 extra_ap_opts, NULL,
305 expire_time, NULL);
306 if (retval) {
307 return retval;
310 /* wrap that up in a nice GSS-API wrapping */
311 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
313 /* and wrap that in a shiny SPNEGO wrapper */
314 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
316 data_blob_free(&tkt_wrapped);
317 data_blob_free(&tkt);
319 return retval;
324 parse a spnego NTLMSSP challenge packet giving two security blobs
326 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
327 DATA_BLOB *chal1, DATA_BLOB *chal2)
329 bool ret;
330 ASN1_DATA *data;
332 ZERO_STRUCTP(chal1);
333 ZERO_STRUCTP(chal2);
335 data = asn1_init(talloc_tos());
336 if (data == NULL) {
337 return false;
340 asn1_load(data, blob);
341 asn1_start_tag(data,ASN1_CONTEXT(1));
342 asn1_start_tag(data,ASN1_SEQUENCE(0));
344 asn1_start_tag(data,ASN1_CONTEXT(0));
345 asn1_check_enumerated(data,1);
346 asn1_end_tag(data);
348 asn1_start_tag(data,ASN1_CONTEXT(1));
349 asn1_check_OID(data, OID_NTLMSSP);
350 asn1_end_tag(data);
352 asn1_start_tag(data,ASN1_CONTEXT(2));
353 asn1_read_OctetString(data, ctx, chal1);
354 asn1_end_tag(data);
356 /* the second challenge is optional (XP doesn't send it) */
357 if (asn1_tag_remaining(data)) {
358 asn1_start_tag(data,ASN1_CONTEXT(3));
359 asn1_read_OctetString(data, ctx, chal2);
360 asn1_end_tag(data);
363 asn1_end_tag(data);
364 asn1_end_tag(data);
366 ret = !data->has_error;
368 if (data->has_error) {
369 data_blob_free(chal1);
370 data_blob_free(chal2);
373 asn1_free(data);
374 return ret;
379 generate a SPNEGO auth packet. This will contain the encrypted passwords
381 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
383 ASN1_DATA *data;
384 DATA_BLOB ret;
386 data = asn1_init(talloc_tos());
387 if (data == NULL) {
388 return data_blob_null;
391 asn1_push_tag(data, ASN1_CONTEXT(1));
392 asn1_push_tag(data, ASN1_SEQUENCE(0));
393 asn1_push_tag(data, ASN1_CONTEXT(2));
394 asn1_write_OctetString(data,blob.data,blob.length);
395 asn1_pop_tag(data);
396 asn1_pop_tag(data);
397 asn1_pop_tag(data);
399 ret = data_blob_talloc(ctx, data->data, data->length);
401 asn1_free(data);
403 return ret;
407 parse a SPNEGO auth packet. This contains the encrypted passwords
409 bool spnego_parse_auth_and_mic(TALLOC_CTX *ctx, DATA_BLOB blob,
410 DATA_BLOB *auth, DATA_BLOB *signature)
412 ssize_t len;
413 struct spnego_data token;
415 len = spnego_read_data(talloc_tos(), blob, &token);
416 if (len == -1) {
417 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
418 return false;
421 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
422 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
423 token.type));
424 spnego_free_data(&token);
425 return false;
428 *auth = data_blob_talloc(ctx,
429 token.negTokenTarg.responseToken.data,
430 token.negTokenTarg.responseToken.length);
432 if (!signature) {
433 goto done;
436 *signature = data_blob_talloc(ctx,
437 token.negTokenTarg.mechListMIC.data,
438 token.negTokenTarg.mechListMIC.length);
440 done:
441 spnego_free_data(&token);
443 return true;
446 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
448 return spnego_parse_auth_and_mic(ctx, blob, auth, NULL);
452 generate a minimal SPNEGO response packet. Doesn't contain much.
454 DATA_BLOB spnego_gen_auth_response_and_mic(TALLOC_CTX *ctx,
455 NTSTATUS nt_status,
456 const char *mechOID,
457 DATA_BLOB *reply,
458 DATA_BLOB *mechlistMIC)
460 ASN1_DATA *data;
461 DATA_BLOB ret;
462 uint8 negResult;
464 if (NT_STATUS_IS_OK(nt_status)) {
465 negResult = SPNEGO_ACCEPT_COMPLETED;
466 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
467 negResult = SPNEGO_ACCEPT_INCOMPLETE;
468 } else {
469 negResult = SPNEGO_REJECT;
472 data = asn1_init(talloc_tos());
473 if (data == NULL) {
474 return data_blob_null;
477 asn1_push_tag(data, ASN1_CONTEXT(1));
478 asn1_push_tag(data, ASN1_SEQUENCE(0));
479 asn1_push_tag(data, ASN1_CONTEXT(0));
480 asn1_write_enumerated(data, negResult);
481 asn1_pop_tag(data);
483 if (mechOID) {
484 asn1_push_tag(data,ASN1_CONTEXT(1));
485 asn1_write_OID(data, mechOID);
486 asn1_pop_tag(data);
489 if (reply && reply->data != NULL) {
490 asn1_push_tag(data,ASN1_CONTEXT(2));
491 asn1_write_OctetString(data, reply->data, reply->length);
492 asn1_pop_tag(data);
495 if (mechlistMIC && mechlistMIC->data != NULL) {
496 asn1_push_tag(data, ASN1_CONTEXT(3));
497 asn1_write_OctetString(data,
498 mechlistMIC->data,
499 mechlistMIC->length);
500 asn1_pop_tag(data);
503 asn1_pop_tag(data);
504 asn1_pop_tag(data);
506 ret = data_blob_talloc(ctx, data->data, data->length);
507 asn1_free(data);
508 return ret;
511 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx, DATA_BLOB *reply,
512 NTSTATUS nt_status, const char *mechOID)
514 return spnego_gen_auth_response_and_mic(ctx, nt_status,
515 mechOID, reply, NULL);
519 parse a SPNEGO auth packet. This contains the encrypted passwords
521 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
522 DATA_BLOB blob, NTSTATUS nt_status,
523 const char *mechOID,
524 DATA_BLOB *auth)
526 ASN1_DATA *data;
527 uint8 negResult;
529 if (NT_STATUS_IS_OK(nt_status)) {
530 negResult = SPNEGO_ACCEPT_COMPLETED;
531 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
532 negResult = SPNEGO_ACCEPT_INCOMPLETE;
533 } else {
534 negResult = SPNEGO_REJECT;
537 data = asn1_init(talloc_tos());
538 if (data == NULL) {
539 return false;
542 asn1_load(data, blob);
543 asn1_start_tag(data, ASN1_CONTEXT(1));
544 asn1_start_tag(data, ASN1_SEQUENCE(0));
545 asn1_start_tag(data, ASN1_CONTEXT(0));
546 asn1_check_enumerated(data, negResult);
547 asn1_end_tag(data);
549 *auth = data_blob_null;
551 if (asn1_tag_remaining(data)) {
552 asn1_start_tag(data,ASN1_CONTEXT(1));
553 asn1_check_OID(data, mechOID);
554 asn1_end_tag(data);
556 if (asn1_tag_remaining(data)) {
557 asn1_start_tag(data,ASN1_CONTEXT(2));
558 asn1_read_OctetString(data, ctx, auth);
559 asn1_end_tag(data);
561 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
562 data->has_error = 1;
565 /* Binding against Win2K DC returns a duplicate of the responseToken in
566 * the optional mechListMIC field. This is a bug in Win2K. We ignore
567 * this field if it exists. Win2K8 may return a proper mechListMIC at
568 * which point we need to implement the integrity checking. */
569 if (asn1_tag_remaining(data)) {
570 DATA_BLOB mechList = data_blob_null;
571 asn1_start_tag(data, ASN1_CONTEXT(3));
572 asn1_read_OctetString(data, ctx, &mechList);
573 asn1_end_tag(data);
574 data_blob_free(&mechList);
575 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
576 "ignoring.\n"));
579 asn1_end_tag(data);
580 asn1_end_tag(data);
582 if (data->has_error) {
583 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
584 asn1_free(data);
585 data_blob_free(auth);
586 return False;
589 asn1_free(data);
590 return True;
593 bool spnego_mech_list_blob(TALLOC_CTX *mem_ctx,
594 char **oid_list, DATA_BLOB *raw_data)
596 ASN1_DATA *data;
597 unsigned int idx;
599 if (!oid_list || !oid_list[0] || !raw_data) {
600 return false;
603 data = asn1_init(talloc_tos());
604 if (data == NULL) {
605 return false;
608 asn1_push_tag(data, ASN1_SEQUENCE(0));
609 for (idx = 0; oid_list[idx]; idx++) {
610 asn1_write_OID(data, oid_list[idx]);
612 asn1_pop_tag(data);
614 if (data->has_error) {
615 DEBUG(3, (__location__ " failed at %d\n", (int)data->ofs));
616 asn1_free(data);
617 return false;
620 *raw_data = data_blob_talloc(mem_ctx, data->data, data->length);
621 if (!raw_data->data) {
622 DEBUG(3, (__location__": data_blob_talloc() failed!\n"));
623 asn1_free(data);
624 return false;
627 asn1_free(data);
628 return true;