s3-libsmb: Use data_blob_talloc to get krb5 ticket and session keys
[sfrench/samba-autobuild/.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(TALLOC_CTX *ctx,
33                                   const char *OIDs[],
34                                   DATA_BLOB *psecblob,
35                                   const char *principal)
36 {
37         int i;
38         ASN1_DATA *data;
39         DATA_BLOB ret;
40
41         data = asn1_init(talloc_tos());
42         if (data == NULL) {
43                 return data_blob_null;
44         }
45
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));
50
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]);
55         }
56         asn1_pop_tag(data);
57         asn1_pop_tag(data);
58
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);
64         }
65
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);
74         }
75
76         asn1_pop_tag(data);
77         asn1_pop_tag(data);
78
79         asn1_pop_tag(data);
80
81         if (data->has_error) {
82                 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
83         }
84
85         ret = data_blob_talloc(ctx, data->data, data->length);
86         asn1_free(data);
87
88         return ret;
89 }
90
91 /*
92   parse a negTokenInit packet giving a GUID, a list of supported
93   OIDs (the mechanisms) and a principal name string 
94 */
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)
100 {
101         int i;
102         bool ret;
103         ASN1_DATA *data;
104
105         data = asn1_init(talloc_tos());
106         if (data == NULL) {
107                 return false;
108         }
109
110         asn1_load(data, blob);
111
112         asn1_start_tag(data,ASN1_APPLICATION(0));
113
114         asn1_check_OID(data,OID_SPNEGO);
115
116         /* negTokenInit  [0]  NegTokenInit */
117         asn1_start_tag(data,ASN1_CONTEXT(0));
118         asn1_start_tag(data,ASN1_SEQUENCE(0));
119
120         /* mechTypes [0] MechTypeList  OPTIONAL */
121
122         /* Not really optional, we depend on this to decide
123          * what mechanisms we have to work with. */
124
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);
131         }
132         OIDs[i] = NULL;
133         asn1_end_tag(data);
134         asn1_end_tag(data);
135
136         if (principal) {
137                 *principal = NULL;
138         }
139         if (secblob) {
140                 *secblob = data_blob_null;
141         }
142
143         /*
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).
148         */
149
150         if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
151                 uint8 flags;
152
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);
158                 }
159                 asn1_end_tag(data);
160                 asn1_end_tag(data);
161         }
162
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);
173                 }
174         }
175
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);
190                 }
191         }
192
193         asn1_end_tag(data);
194         asn1_end_tag(data);
195
196         asn1_end_tag(data);
197
198         ret = !data->has_error;
199         if (data->has_error) {
200                 int j;
201                 if (principal) {
202                         TALLOC_FREE(*principal);
203                 }
204                 if (secblob) {
205                         data_blob_free(secblob);
206                 }
207                 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
208                         TALLOC_FREE(OIDs[j]);
209                 }
210         }
211
212         asn1_free(data);
213         return ret;
214 }
215
216 /*
217   generate a krb5 GSS-API wrapper packet given a ticket
218 */
219 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
220 {
221         ASN1_DATA *data;
222         DATA_BLOB ret;
223
224         data = asn1_init(talloc_tos());
225         if (data == NULL) {
226                 return data_blob_null;
227         }
228
229         asn1_push_tag(data, ASN1_APPLICATION(0));
230         asn1_write_OID(data, OID_KERBEROS5);
231
232         asn1_write(data, tok_id, 2);
233         asn1_write(data, ticket.data, ticket.length);
234         asn1_pop_tag(data);
235
236         if (data->has_error) {
237                 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
238         }
239
240         ret = data_blob_talloc(ctx, data->data, data->length);
241         asn1_free(data);
242
243         return ret;
244 }
245
246 /*
247   parse a krb5 GSS-API wrapper packet giving a ticket
248 */
249 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
250 {
251         bool ret;
252         ASN1_DATA *data;
253         int data_remaining;
254
255         data = asn1_init(talloc_tos());
256         if (data == NULL) {
257                 return false;
258         }
259
260         asn1_load(data, blob);
261         asn1_start_tag(data, ASN1_APPLICATION(0));
262         asn1_check_OID(data, OID_KERBEROS5);
263
264         data_remaining = asn1_tag_remaining(data);
265
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);
273         }
274
275         asn1_end_tag(data);
276
277         ret = !data->has_error;
278
279         if (data->has_error) {
280                 data_blob_free(ticket);
281         }
282
283         asn1_free(data);
284
285         return ret;
286 }
287
288
289 /* 
290    generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
291    kerberos session setup
292 */
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)
298 {
299         int retval;
300         DATA_BLOB tkt, tkt_wrapped;
301         const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
302
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;
310         }
311
312         /* wrap that up in a nice GSS-API wrapping */
313         tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
314
315         /* and wrap that in a shiny SPNEGO wrapper */
316         *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
317
318         data_blob_free(&tkt_wrapped);
319         data_blob_free(&tkt);
320
321         return retval;
322 }
323
324
325 /*
326   parse a spnego NTLMSSP challenge packet giving two security blobs
327 */
328 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
329                             DATA_BLOB *chal1, DATA_BLOB *chal2)
330 {
331         bool ret;
332         ASN1_DATA *data;
333
334         ZERO_STRUCTP(chal1);
335         ZERO_STRUCTP(chal2);
336
337         data = asn1_init(talloc_tos());
338         if (data == NULL) {
339                 return false;
340         }
341
342         asn1_load(data, blob);
343         asn1_start_tag(data,ASN1_CONTEXT(1));
344         asn1_start_tag(data,ASN1_SEQUENCE(0));
345
346         asn1_start_tag(data,ASN1_CONTEXT(0));
347         asn1_check_enumerated(data,1);
348         asn1_end_tag(data);
349
350         asn1_start_tag(data,ASN1_CONTEXT(1));
351         asn1_check_OID(data, OID_NTLMSSP);
352         asn1_end_tag(data);
353
354         asn1_start_tag(data,ASN1_CONTEXT(2));
355         asn1_read_OctetString(data, ctx, chal1);
356         asn1_end_tag(data);
357
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);
363         }
364
365         asn1_end_tag(data);
366         asn1_end_tag(data);
367
368         ret = !data->has_error;
369
370         if (data->has_error) {
371                 data_blob_free(chal1);
372                 data_blob_free(chal2);
373         }
374
375         asn1_free(data);
376         return ret;
377 }
378
379
380 /*
381  generate a SPNEGO auth packet. This will contain the encrypted passwords
382 */
383 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
384 {
385         ASN1_DATA *data;
386         DATA_BLOB ret;
387
388         data = asn1_init(talloc_tos());
389         if (data == NULL) {
390                 return data_blob_null;
391         }
392
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);
400
401         ret = data_blob_talloc(ctx, data->data, data->length);
402
403         asn1_free(data);
404
405         return ret;
406 }
407
408 /*
409  parse a SPNEGO auth packet. This contains the encrypted passwords
410 */
411 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
412 {
413         ssize_t len;
414         struct spnego_data token;
415
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;
420         }
421
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;
427         }
428
429         *auth = data_blob_talloc(ctx,
430                                  token.negTokenTarg.responseToken.data,
431                                  token.negTokenTarg.responseToken.length);
432         spnego_free_data(&token);
433
434         return true;
435 }
436
437 /*
438   generate a minimal SPNEGO response packet.  Doesn't contain much.
439 */
440 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx,
441                                    DATA_BLOB *reply, NTSTATUS nt_status,
442                                    const char *mechOID)
443 {
444         ASN1_DATA *data;
445         DATA_BLOB ret;
446         uint8 negResult;
447
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;
454         }
455
456         data = asn1_init(talloc_tos());
457         if (data == NULL) {
458                 return data_blob_null;
459         }
460
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);
466
467         if (mechOID) {
468                 asn1_push_tag(data,ASN1_CONTEXT(1));
469                 asn1_write_OID(data, mechOID);
470                 asn1_pop_tag(data);
471         }
472
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);
477         }
478
479         asn1_pop_tag(data);
480         asn1_pop_tag(data);
481
482         ret = data_blob_talloc(ctx, data->data, data->length);
483         asn1_free(data);
484         return ret;
485 }
486
487 /*
488  parse a SPNEGO auth packet. This contains the encrypted passwords
489 */
490 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
491                                 DATA_BLOB blob, NTSTATUS nt_status,
492                                 const char *mechOID,
493                                 DATA_BLOB *auth)
494 {
495         ASN1_DATA *data;
496         uint8 negResult;
497
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;
504         }
505
506         data = asn1_init(talloc_tos());
507         if (data == NULL) {
508                 return false;
509         }
510
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);
517
518         *auth = data_blob_null;
519
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);
524
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);
529                 }
530         } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
531                 data->has_error = 1;
532         }
533
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"));
546         }
547
548         asn1_end_tag(data);
549         asn1_end_tag(data);
550
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;
556         }
557
558         asn1_free(data);
559         return True;
560 }