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