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