WHATSNEW: Update changes.
[Samba.git] / source3 / libsmb / clispnego.c
blob539b41105698751c88e647cee3f39692872855a9
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 const char *oid_str = NULL;
129 asn1_read_OID(data,ctx,&oid_str);
130 OIDs[i] = CONST_DISCARD(char *, oid_str);
132 OIDs[i] = NULL;
133 asn1_end_tag(data);
134 asn1_end_tag(data);
136 if (principal) {
137 *principal = NULL;
139 if (secblob) {
140 *secblob = data_blob_null;
144 Win7 + Live Sign-in Assistant attaches a mechToken
145 ASN1_CONTEXT(2) to the negTokenInit packet
146 which breaks our negotiation if we just assume
147 the next tag is ASN1_CONTEXT(3).
150 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
151 uint8 flags;
153 /* reqFlags [1] ContextFlags OPTIONAL */
154 asn1_start_tag(data, ASN1_CONTEXT(1));
155 asn1_start_tag(data, ASN1_BIT_STRING);
156 while (asn1_tag_remaining(data) > 0) {
157 asn1_read_uint8(data, &flags);
159 asn1_end_tag(data);
160 asn1_end_tag(data);
163 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
164 DATA_BLOB sblob = data_blob_null;
165 /* mechToken [2] OCTET STRING OPTIONAL */
166 asn1_start_tag(data, ASN1_CONTEXT(2));
167 asn1_read_OctetString(data, ctx, &sblob);
168 asn1_end_tag(data);
169 if (secblob) {
170 *secblob = sblob;
171 } else {
172 data_blob_free(&sblob);
176 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
177 char *princ = NULL;
178 /* mechListMIC [3] OCTET STRING OPTIONAL */
179 asn1_start_tag(data, ASN1_CONTEXT(3));
180 asn1_start_tag(data, ASN1_SEQUENCE(0));
181 asn1_start_tag(data, ASN1_CONTEXT(0));
182 asn1_read_GeneralString(data, ctx, &princ);
183 asn1_end_tag(data);
184 asn1_end_tag(data);
185 asn1_end_tag(data);
186 if (principal) {
187 *principal = princ;
188 } else {
189 TALLOC_FREE(princ);
193 asn1_end_tag(data);
194 asn1_end_tag(data);
196 asn1_end_tag(data);
198 ret = !data->has_error;
199 if (data->has_error) {
200 int j;
201 if (principal) {
202 TALLOC_FREE(*principal);
204 if (secblob) {
205 data_blob_free(secblob);
207 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
208 TALLOC_FREE(OIDs[j]);
212 asn1_free(data);
213 return ret;
217 generate a krb5 GSS-API wrapper packet given a ticket
219 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
221 ASN1_DATA *data;
222 DATA_BLOB ret;
224 data = asn1_init(talloc_tos());
225 if (data == NULL) {
226 return data_blob_null;
229 asn1_push_tag(data, ASN1_APPLICATION(0));
230 asn1_write_OID(data, OID_KERBEROS5);
232 asn1_write(data, tok_id, 2);
233 asn1_write(data, ticket.data, ticket.length);
234 asn1_pop_tag(data);
236 if (data->has_error) {
237 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
240 ret = data_blob_talloc(ctx, data->data, data->length);
241 asn1_free(data);
243 return ret;
247 parse a krb5 GSS-API wrapper packet giving a ticket
249 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
251 bool ret;
252 ASN1_DATA *data;
253 int data_remaining;
255 data = asn1_init(talloc_tos());
256 if (data == NULL) {
257 return false;
260 asn1_load(data, blob);
261 asn1_start_tag(data, ASN1_APPLICATION(0));
262 asn1_check_OID(data, OID_KERBEROS5);
264 data_remaining = asn1_tag_remaining(data);
266 if (data_remaining < 3) {
267 data->has_error = True;
268 } else {
269 asn1_read(data, tok_id, 2);
270 data_remaining -= 2;
271 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
272 asn1_read(data, ticket->data, ticket->length);
275 asn1_end_tag(data);
277 ret = !data->has_error;
279 if (data->has_error) {
280 data_blob_free(ticket);
283 asn1_free(data);
285 return ret;
290 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
291 kerberos session setup
293 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
294 const char *principal, int time_offset,
295 DATA_BLOB *targ,
296 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
297 time_t *expire_time)
299 int retval;
300 DATA_BLOB tkt, tkt_wrapped;
301 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
303 /* get a kerberos ticket for the service and extract the session key */
304 retval = cli_krb5_get_ticket(ctx, principal, time_offset,
305 &tkt, session_key_krb5,
306 extra_ap_opts, NULL,
307 expire_time, NULL);
308 if (retval) {
309 return retval;
312 /* wrap that up in a nice GSS-API wrapping */
313 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
315 /* and wrap that in a shiny SPNEGO wrapper */
316 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
318 data_blob_free(&tkt_wrapped);
319 data_blob_free(&tkt);
321 return retval;
326 parse a spnego NTLMSSP challenge packet giving two security blobs
328 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
329 DATA_BLOB *chal1, DATA_BLOB *chal2)
331 bool ret;
332 ASN1_DATA *data;
334 ZERO_STRUCTP(chal1);
335 ZERO_STRUCTP(chal2);
337 data = asn1_init(talloc_tos());
338 if (data == NULL) {
339 return false;
342 asn1_load(data, blob);
343 asn1_start_tag(data,ASN1_CONTEXT(1));
344 asn1_start_tag(data,ASN1_SEQUENCE(0));
346 asn1_start_tag(data,ASN1_CONTEXT(0));
347 asn1_check_enumerated(data,1);
348 asn1_end_tag(data);
350 asn1_start_tag(data,ASN1_CONTEXT(1));
351 asn1_check_OID(data, OID_NTLMSSP);
352 asn1_end_tag(data);
354 asn1_start_tag(data,ASN1_CONTEXT(2));
355 asn1_read_OctetString(data, ctx, chal1);
356 asn1_end_tag(data);
358 /* the second challenge is optional (XP doesn't send it) */
359 if (asn1_tag_remaining(data)) {
360 asn1_start_tag(data,ASN1_CONTEXT(3));
361 asn1_read_OctetString(data, ctx, chal2);
362 asn1_end_tag(data);
365 asn1_end_tag(data);
366 asn1_end_tag(data);
368 ret = !data->has_error;
370 if (data->has_error) {
371 data_blob_free(chal1);
372 data_blob_free(chal2);
375 asn1_free(data);
376 return ret;
381 generate a SPNEGO auth packet. This will contain the encrypted passwords
383 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
385 ASN1_DATA *data;
386 DATA_BLOB ret;
388 data = asn1_init(talloc_tos());
389 if (data == NULL) {
390 return data_blob_null;
393 asn1_push_tag(data, ASN1_CONTEXT(1));
394 asn1_push_tag(data, ASN1_SEQUENCE(0));
395 asn1_push_tag(data, ASN1_CONTEXT(2));
396 asn1_write_OctetString(data,blob.data,blob.length);
397 asn1_pop_tag(data);
398 asn1_pop_tag(data);
399 asn1_pop_tag(data);
401 ret = data_blob_talloc(ctx, data->data, data->length);
403 asn1_free(data);
405 return ret;
409 parse a SPNEGO auth packet. This contains the encrypted passwords
411 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
413 ssize_t len;
414 struct spnego_data token;
416 len = spnego_read_data(talloc_tos(), blob, &token);
417 if (len == -1) {
418 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
419 return false;
422 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
423 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
424 token.type));
425 spnego_free_data(&token);
426 return false;
429 *auth = data_blob_talloc(ctx,
430 token.negTokenTarg.responseToken.data,
431 token.negTokenTarg.responseToken.length);
432 spnego_free_data(&token);
434 return true;
438 generate a minimal SPNEGO response packet. Doesn't contain much.
440 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx,
441 DATA_BLOB *reply, NTSTATUS nt_status,
442 const char *mechOID)
444 ASN1_DATA *data;
445 DATA_BLOB ret;
446 uint8 negResult;
448 if (NT_STATUS_IS_OK(nt_status)) {
449 negResult = SPNEGO_ACCEPT_COMPLETED;
450 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
451 negResult = SPNEGO_ACCEPT_INCOMPLETE;
452 } else {
453 negResult = SPNEGO_REJECT;
456 data = asn1_init(talloc_tos());
457 if (data == NULL) {
458 return data_blob_null;
461 asn1_push_tag(data, ASN1_CONTEXT(1));
462 asn1_push_tag(data, ASN1_SEQUENCE(0));
463 asn1_push_tag(data, ASN1_CONTEXT(0));
464 asn1_write_enumerated(data, negResult);
465 asn1_pop_tag(data);
467 if (mechOID) {
468 asn1_push_tag(data,ASN1_CONTEXT(1));
469 asn1_write_OID(data, mechOID);
470 asn1_pop_tag(data);
473 if (reply && reply->data != NULL) {
474 asn1_push_tag(data,ASN1_CONTEXT(2));
475 asn1_write_OctetString(data, reply->data, reply->length);
476 asn1_pop_tag(data);
479 asn1_pop_tag(data);
480 asn1_pop_tag(data);
482 ret = data_blob_talloc(ctx, data->data, data->length);
483 asn1_free(data);
484 return ret;
488 parse a SPNEGO auth packet. This contains the encrypted passwords
490 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
491 DATA_BLOB blob, NTSTATUS nt_status,
492 const char *mechOID,
493 DATA_BLOB *auth)
495 ASN1_DATA *data;
496 uint8 negResult;
498 if (NT_STATUS_IS_OK(nt_status)) {
499 negResult = SPNEGO_ACCEPT_COMPLETED;
500 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
501 negResult = SPNEGO_ACCEPT_INCOMPLETE;
502 } else {
503 negResult = SPNEGO_REJECT;
506 data = asn1_init(talloc_tos());
507 if (data == NULL) {
508 return false;
511 asn1_load(data, blob);
512 asn1_start_tag(data, ASN1_CONTEXT(1));
513 asn1_start_tag(data, ASN1_SEQUENCE(0));
514 asn1_start_tag(data, ASN1_CONTEXT(0));
515 asn1_check_enumerated(data, negResult);
516 asn1_end_tag(data);
518 *auth = data_blob_null;
520 if (asn1_tag_remaining(data)) {
521 asn1_start_tag(data,ASN1_CONTEXT(1));
522 asn1_check_OID(data, mechOID);
523 asn1_end_tag(data);
525 if (asn1_tag_remaining(data)) {
526 asn1_start_tag(data,ASN1_CONTEXT(2));
527 asn1_read_OctetString(data, ctx, auth);
528 asn1_end_tag(data);
530 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
531 data->has_error = 1;
534 /* Binding against Win2K DC returns a duplicate of the responseToken in
535 * the optional mechListMIC field. This is a bug in Win2K. We ignore
536 * this field if it exists. Win2K8 may return a proper mechListMIC at
537 * which point we need to implement the integrity checking. */
538 if (asn1_tag_remaining(data)) {
539 DATA_BLOB mechList = data_blob_null;
540 asn1_start_tag(data, ASN1_CONTEXT(3));
541 asn1_read_OctetString(data, ctx, &mechList);
542 asn1_end_tag(data);
543 data_blob_free(&mechList);
544 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
545 "ignoring.\n"));
548 asn1_end_tag(data);
549 asn1_end_tag(data);
551 if (data->has_error) {
552 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
553 asn1_free(data);
554 data_blob_free(auth);
555 return False;
558 asn1_free(data);
559 return True;