0935041a3407d7f6c8716f20273f688e1aabbff3
[mat/samba.git] / source3 / libsmb / clispnego.c
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
8
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "../libcli/auth/spnego.h"
25 #include "smb_krb5.h"
26
27 /*
28   generate a negTokenInit packet given a list of supported
29   OIDs (the mechanisms) a blob, and a principal name string
30 */
31
32 DATA_BLOB spnego_gen_negTokenInit(const char *OIDs[],
33                                   DATA_BLOB *psecblob,
34                                   const char *principal)
35 {
36         int i;
37         ASN1_DATA *data;
38         DATA_BLOB ret;
39
40         data = asn1_init(talloc_tos());
41         if (data == NULL) {
42                 return data_blob_null;
43         }
44
45         asn1_push_tag(data,ASN1_APPLICATION(0));
46         asn1_write_OID(data,OID_SPNEGO);
47         asn1_push_tag(data,ASN1_CONTEXT(0));
48         asn1_push_tag(data,ASN1_SEQUENCE(0));
49
50         asn1_push_tag(data,ASN1_CONTEXT(0));
51         asn1_push_tag(data,ASN1_SEQUENCE(0));
52         for (i=0; OIDs[i]; i++) {
53                 asn1_write_OID(data,OIDs[i]);
54         }
55         asn1_pop_tag(data);
56         asn1_pop_tag(data);
57
58         if (psecblob && psecblob->length && psecblob->data) {
59                 asn1_push_tag(data, ASN1_CONTEXT(2));
60                 asn1_write_OctetString(data,psecblob->data,
61                         psecblob->length);
62                 asn1_pop_tag(data);
63         }
64
65         if (principal) {
66                 asn1_push_tag(data, ASN1_CONTEXT(3));
67                 asn1_push_tag(data, ASN1_SEQUENCE(0));
68                 asn1_push_tag(data, ASN1_CONTEXT(0));
69                 asn1_write_GeneralString(data,principal);
70                 asn1_pop_tag(data);
71                 asn1_pop_tag(data);
72                 asn1_pop_tag(data);
73         }
74
75         asn1_pop_tag(data);
76         asn1_pop_tag(data);
77
78         asn1_pop_tag(data);
79
80         if (data->has_error) {
81                 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
82         }
83
84         ret = data_blob(data->data, data->length);
85         asn1_free(data);
86
87         return ret;
88 }
89
90 /*
91   parse a negTokenInit packet giving a GUID, a list of supported
92   OIDs (the mechanisms) and a principal name string 
93 */
94 bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
95                                DATA_BLOB blob,
96                                char *OIDs[ASN1_MAX_OIDS],
97                                char **principal,
98                                DATA_BLOB *secblob)
99 {
100         int i;
101         bool ret;
102         ASN1_DATA *data;
103
104         data = asn1_init(talloc_tos());
105         if (data == NULL) {
106                 return false;
107         }
108
109         asn1_load(data, blob);
110
111         asn1_start_tag(data,ASN1_APPLICATION(0));
112
113         asn1_check_OID(data,OID_SPNEGO);
114
115         /* negTokenInit  [0]  NegTokenInit */
116         asn1_start_tag(data,ASN1_CONTEXT(0));
117         asn1_start_tag(data,ASN1_SEQUENCE(0));
118
119         /* mechTypes [0] MechTypeList  OPTIONAL */
120
121         /* Not really optional, we depend on this to decide
122          * what mechanisms we have to work with. */
123
124         asn1_start_tag(data,ASN1_CONTEXT(0));
125         asn1_start_tag(data,ASN1_SEQUENCE(0));
126         for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
127                 const char *oid_str = NULL;
128                 asn1_read_OID(data,ctx,&oid_str);
129                 OIDs[i] = CONST_DISCARD(char *, oid_str);
130         }
131         OIDs[i] = NULL;
132         asn1_end_tag(data);
133         asn1_end_tag(data);
134
135         if (principal) {
136                 *principal = NULL;
137         }
138         if (secblob) {
139                 *secblob = data_blob_null;
140         }
141
142         /*
143           Win7 + Live Sign-in Assistant attaches a mechToken
144           ASN1_CONTEXT(2) to the negTokenInit packet
145           which breaks our negotiation if we just assume
146           the next tag is ASN1_CONTEXT(3).
147         */
148
149         if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
150                 uint8 flags;
151
152                 /* reqFlags [1] ContextFlags  OPTIONAL */
153                 asn1_start_tag(data, ASN1_CONTEXT(1));
154                 asn1_start_tag(data, ASN1_BIT_STRING);
155                 while (asn1_tag_remaining(data) > 0) {
156                         asn1_read_uint8(data, &flags);
157                 }
158                 asn1_end_tag(data);
159                 asn1_end_tag(data);
160         }
161
162         if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
163                 DATA_BLOB sblob = data_blob_null;
164                 /* mechToken [2] OCTET STRING  OPTIONAL */
165                 asn1_start_tag(data, ASN1_CONTEXT(2));
166                 asn1_read_OctetString(data, ctx, &sblob);
167                 asn1_end_tag(data);
168                 if (secblob) {
169                         *secblob = sblob;
170                 } else {
171                         data_blob_free(&sblob);
172                 }
173         }
174
175         if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
176                 char *princ = NULL;
177                 /* mechListMIC [3] OCTET STRING  OPTIONAL */
178                 asn1_start_tag(data, ASN1_CONTEXT(3));
179                 asn1_start_tag(data, ASN1_SEQUENCE(0));
180                 asn1_start_tag(data, ASN1_CONTEXT(0));
181                 asn1_read_GeneralString(data, ctx, &princ);
182                 asn1_end_tag(data);
183                 asn1_end_tag(data);
184                 asn1_end_tag(data);
185                 if (principal) {
186                         *principal = princ;
187                 } else {
188                         TALLOC_FREE(princ);
189                 }
190         }
191
192         asn1_end_tag(data);
193         asn1_end_tag(data);
194
195         asn1_end_tag(data);
196
197         ret = !data->has_error;
198         if (data->has_error) {
199                 int j;
200                 if (principal) {
201                         TALLOC_FREE(*principal);
202                 }
203                 if (secblob) {
204                         data_blob_free(secblob);
205                 }
206                 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
207                         TALLOC_FREE(OIDs[j]);
208                 }
209         }
210
211         asn1_free(data);
212         return ret;
213 }
214
215 /*
216   generate a krb5 GSS-API wrapper packet given a ticket
217 */
218 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
219 {
220         ASN1_DATA *data;
221         DATA_BLOB ret;
222
223         data = asn1_init(talloc_tos());
224         if (data == NULL) {
225                 return data_blob_null;
226         }
227
228         asn1_push_tag(data, ASN1_APPLICATION(0));
229         asn1_write_OID(data, OID_KERBEROS5);
230
231         asn1_write(data, tok_id, 2);
232         asn1_write(data, ticket.data, ticket.length);
233         asn1_pop_tag(data);
234
235         if (data->has_error) {
236                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
237         }
238
239         ret = data_blob(data->data, data->length);
240         asn1_free(data);
241
242         return ret;
243 }
244
245 /*
246   parse a krb5 GSS-API wrapper packet giving a ticket
247 */
248 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
249 {
250         bool ret;
251         ASN1_DATA *data;
252         int data_remaining;
253
254         data = asn1_init(talloc_tos());
255         if (data == NULL) {
256                 return false;
257         }
258
259         asn1_load(data, blob);
260         asn1_start_tag(data, ASN1_APPLICATION(0));
261         asn1_check_OID(data, OID_KERBEROS5);
262
263         data_remaining = asn1_tag_remaining(data);
264
265         if (data_remaining < 3) {
266                 data->has_error = True;
267         } else {
268                 asn1_read(data, tok_id, 2);
269                 data_remaining -= 2;
270                 *ticket = data_blob(NULL, data_remaining);
271                 asn1_read(data, ticket->data, ticket->length);
272         }
273
274         asn1_end_tag(data);
275
276         ret = !data->has_error;
277
278         if (data->has_error) {
279                 data_blob_free(ticket);
280         }
281
282         asn1_free(data);
283
284         return ret;
285 }
286
287
288 /* 
289    generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
290    kerberos session setup
291 */
292 int spnego_gen_krb5_negTokenInit(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)
296 {
297         int retval;
298         DATA_BLOB tkt, tkt_wrapped;
299         const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
300
301         /* get a kerberos ticket for the service and extract the session key */
302         retval = cli_krb5_get_ticket(principal, time_offset,
303                                         &tkt, session_key_krb5, extra_ap_opts, NULL, 
304                                         expire_time, NULL);
305
306         if (retval)
307                 return retval;
308
309         /* wrap that up in a nice GSS-API wrapping */
310         tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
311
312         /* and wrap that in a shiny SPNEGO wrapper */
313         *targ = spnego_gen_negTokenInit(krb_mechs, &tkt_wrapped, NULL);
314
315         data_blob_free(&tkt_wrapped);
316         data_blob_free(&tkt);
317
318         return retval;
319 }
320
321
322 /*
323   parse a spnego NTLMSSP challenge packet giving two security blobs
324 */
325 bool spnego_parse_challenge(const DATA_BLOB blob,
326                             DATA_BLOB *chal1, DATA_BLOB *chal2)
327 {
328         bool ret;
329         ASN1_DATA *data;
330
331         ZERO_STRUCTP(chal1);
332         ZERO_STRUCTP(chal2);
333
334         data = asn1_init(talloc_tos());
335         if (data == NULL) {
336                 return false;
337         }
338
339         asn1_load(data, blob);
340         asn1_start_tag(data,ASN1_CONTEXT(1));
341         asn1_start_tag(data,ASN1_SEQUENCE(0));
342
343         asn1_start_tag(data,ASN1_CONTEXT(0));
344         asn1_check_enumerated(data,1);
345         asn1_end_tag(data);
346
347         asn1_start_tag(data,ASN1_CONTEXT(1));
348         asn1_check_OID(data, OID_NTLMSSP);
349         asn1_end_tag(data);
350
351         asn1_start_tag(data,ASN1_CONTEXT(2));
352         asn1_read_OctetString(data, talloc_autofree_context(), chal1);
353         asn1_end_tag(data);
354
355         /* the second challenge is optional (XP doesn't send it) */
356         if (asn1_tag_remaining(data)) {
357                 asn1_start_tag(data,ASN1_CONTEXT(3));
358                 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
359                 asn1_end_tag(data);
360         }
361
362         asn1_end_tag(data);
363         asn1_end_tag(data);
364
365         ret = !data->has_error;
366
367         if (data->has_error) {
368                 data_blob_free(chal1);
369                 data_blob_free(chal2);
370         }
371
372         asn1_free(data);
373         return ret;
374 }
375
376
377 /*
378  generate a SPNEGO auth packet. This will contain the encrypted passwords
379 */
380 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
381 {
382         ASN1_DATA *data;
383         DATA_BLOB ret;
384
385         data = asn1_init(talloc_tos());
386         if (data == NULL) {
387                 return data_blob_null;
388         }
389
390         asn1_push_tag(data, ASN1_CONTEXT(1));
391         asn1_push_tag(data, ASN1_SEQUENCE(0));
392         asn1_push_tag(data, ASN1_CONTEXT(2));
393         asn1_write_OctetString(data,blob.data,blob.length);
394         asn1_pop_tag(data);
395         asn1_pop_tag(data);
396         asn1_pop_tag(data);
397
398         ret = data_blob(data->data, data->length);
399
400         asn1_free(data);
401
402         return ret;
403 }
404
405 /*
406  parse a SPNEGO auth packet. This contains the encrypted passwords
407 */
408 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
409 {
410         ssize_t len;
411         struct spnego_data token;
412
413         len = spnego_read_data(talloc_tos(), blob, &token);
414         if (len == -1) {
415                 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
416                 return false;
417         }
418
419         if (token.type != SPNEGO_NEG_TOKEN_TARG) {
420                 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
421                         token.type));
422                 spnego_free_data(&token);
423                 return false;
424         }
425
426         *auth = data_blob_talloc(talloc_tos(),
427                                  token.negTokenTarg.responseToken.data,
428                                  token.negTokenTarg.responseToken.length);
429         spnego_free_data(&token);
430
431         return true;
432 }
433
434 /*
435   generate a minimal SPNEGO response packet.  Doesn't contain much.
436 */
437 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
438                                    const char *mechOID)
439 {
440         ASN1_DATA *data;
441         DATA_BLOB ret;
442         uint8 negResult;
443
444         if (NT_STATUS_IS_OK(nt_status)) {
445                 negResult = SPNEGO_ACCEPT_COMPLETED;
446         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
447                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
448         } else {
449                 negResult = SPNEGO_REJECT;
450         }
451
452         data = asn1_init(talloc_tos());
453         if (data == NULL) {
454                 return data_blob_null;
455         }
456
457         asn1_push_tag(data, ASN1_CONTEXT(1));
458         asn1_push_tag(data, ASN1_SEQUENCE(0));
459         asn1_push_tag(data, ASN1_CONTEXT(0));
460         asn1_write_enumerated(data, negResult);
461         asn1_pop_tag(data);
462
463         if (mechOID) {
464                 asn1_push_tag(data,ASN1_CONTEXT(1));
465                 asn1_write_OID(data, mechOID);
466                 asn1_pop_tag(data);
467         }
468
469         if (reply && reply->data != NULL) {
470                 asn1_push_tag(data,ASN1_CONTEXT(2));
471                 asn1_write_OctetString(data, reply->data, reply->length);
472                 asn1_pop_tag(data);
473         }
474
475         asn1_pop_tag(data);
476         asn1_pop_tag(data);
477
478         ret = data_blob(data->data, data->length);
479         asn1_free(data);
480         return ret;
481 }
482
483 /*
484  parse a SPNEGO auth packet. This contains the encrypted passwords
485 */
486 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
487                                 const char *mechOID,
488                                 DATA_BLOB *auth)
489 {
490         ASN1_DATA *data;
491         uint8 negResult;
492
493         if (NT_STATUS_IS_OK(nt_status)) {
494                 negResult = SPNEGO_ACCEPT_COMPLETED;
495         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
496                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
497         } else {
498                 negResult = SPNEGO_REJECT;
499         }
500
501         data = asn1_init(talloc_tos());
502         if (data == NULL) {
503                 return false;
504         }
505
506         asn1_load(data, blob);
507         asn1_start_tag(data, ASN1_CONTEXT(1));
508         asn1_start_tag(data, ASN1_SEQUENCE(0));
509         asn1_start_tag(data, ASN1_CONTEXT(0));
510         asn1_check_enumerated(data, negResult);
511         asn1_end_tag(data);
512
513         *auth = data_blob_null;
514
515         if (asn1_tag_remaining(data)) {
516                 asn1_start_tag(data,ASN1_CONTEXT(1));
517                 asn1_check_OID(data, mechOID);
518                 asn1_end_tag(data);
519
520                 if (asn1_tag_remaining(data)) {
521                         asn1_start_tag(data,ASN1_CONTEXT(2));
522                         asn1_read_OctetString(data, talloc_autofree_context(), auth);
523                         asn1_end_tag(data);
524                 }
525         } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
526                 data->has_error = 1;
527         }
528
529         /* Binding against Win2K DC returns a duplicate of the responseToken in
530          * the optional mechListMIC field. This is a bug in Win2K. We ignore
531          * this field if it exists. Win2K8 may return a proper mechListMIC at
532          * which point we need to implement the integrity checking. */
533         if (asn1_tag_remaining(data)) {
534                 DATA_BLOB mechList = data_blob_null;
535                 asn1_start_tag(data, ASN1_CONTEXT(3));
536                 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
537                 asn1_end_tag(data);
538                 data_blob_free(&mechList);
539                 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
540                     "ignoring.\n"));
541         }
542
543         asn1_end_tag(data);
544         asn1_end_tag(data);
545
546         if (data->has_error) {
547                 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
548                 asn1_free(data);
549                 data_blob_free(auth);
550                 return False;
551         }
552
553         asn1_free(data);
554         return True;
555 }