Rename spnego_gen_negTokenTarg() -> spnego_gen_krb5_negTokenInit()
[kai/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(DATA_BLOB blob,
95                                char *OIDs[ASN1_MAX_OIDS],
96                                char **principal,
97                                DATA_BLOB *secblob)
98 {
99         int i;
100         bool ret;
101         ASN1_DATA *data;
102
103         data = asn1_init(talloc_tos());
104         if (data == NULL) {
105                 return false;
106         }
107
108         asn1_load(data, blob);
109
110         asn1_start_tag(data,ASN1_APPLICATION(0));
111
112         asn1_check_OID(data,OID_SPNEGO);
113
114         /* negTokenInit  [0]  NegTokenInit */
115         asn1_start_tag(data,ASN1_CONTEXT(0));
116         asn1_start_tag(data,ASN1_SEQUENCE(0));
117
118         /* mechTypes [0] MechTypeList  OPTIONAL */
119
120         /* Not really optional, we depend on this to decide
121          * what mechanisms we have to work with. */
122
123         asn1_start_tag(data,ASN1_CONTEXT(0));
124         asn1_start_tag(data,ASN1_SEQUENCE(0));
125         for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
126                 const char *oid_str = NULL;
127                 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
128                 OIDs[i] = CONST_DISCARD(char *, oid_str);
129         }
130         OIDs[i] = NULL;
131         asn1_end_tag(data);
132         asn1_end_tag(data);
133
134         if (principal) {
135                 *principal = NULL;
136         }
137         if (secblob) {
138                 *secblob = data_blob_null;
139         }
140
141         /*
142           Win7 + Live Sign-in Assistant attaches a mechToken
143           ASN1_CONTEXT(2) to the negTokenInit packet
144           which breaks our negotiation if we just assume
145           the next tag is ASN1_CONTEXT(3).
146         */
147
148         if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
149                 uint8 flags;
150
151                 /* reqFlags [1] ContextFlags  OPTIONAL */
152                 asn1_start_tag(data, ASN1_CONTEXT(1));
153                 asn1_start_tag(data, ASN1_BIT_STRING);
154                 while (asn1_tag_remaining(data) > 0) {
155                         asn1_read_uint8(data, &flags);
156                 }
157                 asn1_end_tag(data);
158                 asn1_end_tag(data);
159         }
160
161         if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
162                 DATA_BLOB sblob = data_blob_null;
163                 /* mechToken [2] OCTET STRING  OPTIONAL */
164                 asn1_start_tag(data, ASN1_CONTEXT(2));
165                 asn1_read_OctetString(data, talloc_autofree_context(),
166                         &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,talloc_autofree_context(),
182                         &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(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(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(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(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(const char *principal, int time_offset, 
294                             DATA_BLOB *targ, 
295                             DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
296                             time_t *expire_time)
297 {
298         int retval;
299         DATA_BLOB tkt, tkt_wrapped;
300         const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
301
302         /* get a kerberos ticket for the service and extract the session key */
303         retval = cli_krb5_get_ticket(principal, time_offset,
304                                         &tkt, session_key_krb5, extra_ap_opts, NULL, 
305                                         expire_time, NULL);
306
307         if (retval)
308                 return retval;
309
310         /* wrap that up in a nice GSS-API wrapping */
311         tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
312
313         /* and wrap that in a shiny SPNEGO wrapper */
314         *targ = spnego_gen_negTokenInit(krb_mechs, &tkt_wrapped, NULL);
315
316         data_blob_free(&tkt_wrapped);
317         data_blob_free(&tkt);
318
319         return retval;
320 }
321
322
323 /*
324   parse a spnego NTLMSSP challenge packet giving two security blobs
325 */
326 bool spnego_parse_challenge(const DATA_BLOB blob,
327                             DATA_BLOB *chal1, DATA_BLOB *chal2)
328 {
329         bool ret;
330         ASN1_DATA *data;
331
332         ZERO_STRUCTP(chal1);
333         ZERO_STRUCTP(chal2);
334
335         data = asn1_init(talloc_tos());
336         if (data == NULL) {
337                 return false;
338         }
339
340         asn1_load(data, blob);
341         asn1_start_tag(data,ASN1_CONTEXT(1));
342         asn1_start_tag(data,ASN1_SEQUENCE(0));
343
344         asn1_start_tag(data,ASN1_CONTEXT(0));
345         asn1_check_enumerated(data,1);
346         asn1_end_tag(data);
347
348         asn1_start_tag(data,ASN1_CONTEXT(1));
349         asn1_check_OID(data, OID_NTLMSSP);
350         asn1_end_tag(data);
351
352         asn1_start_tag(data,ASN1_CONTEXT(2));
353         asn1_read_OctetString(data, talloc_autofree_context(), chal1);
354         asn1_end_tag(data);
355
356         /* the second challenge is optional (XP doesn't send it) */
357         if (asn1_tag_remaining(data)) {
358                 asn1_start_tag(data,ASN1_CONTEXT(3));
359                 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
360                 asn1_end_tag(data);
361         }
362
363         asn1_end_tag(data);
364         asn1_end_tag(data);
365
366         ret = !data->has_error;
367
368         if (data->has_error) {
369                 data_blob_free(chal1);
370                 data_blob_free(chal2);
371         }
372
373         asn1_free(data);
374         return ret;
375 }
376
377
378 /*
379  generate a SPNEGO auth packet. This will contain the encrypted passwords
380 */
381 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
382 {
383         ASN1_DATA *data;
384         DATA_BLOB ret;
385
386         data = asn1_init(talloc_tos());
387         if (data == NULL) {
388                 return data_blob_null;
389         }
390
391         asn1_push_tag(data, ASN1_CONTEXT(1));
392         asn1_push_tag(data, ASN1_SEQUENCE(0));
393         asn1_push_tag(data, ASN1_CONTEXT(2));
394         asn1_write_OctetString(data,blob.data,blob.length);
395         asn1_pop_tag(data);
396         asn1_pop_tag(data);
397         asn1_pop_tag(data);
398
399         ret = data_blob(data->data, data->length);
400
401         asn1_free(data);
402
403         return ret;
404 }
405
406 /*
407  parse a SPNEGO auth packet. This contains the encrypted passwords
408 */
409 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
410 {
411         ssize_t len;
412         struct spnego_data token;
413
414         len = spnego_read_data(talloc_tos(), blob, &token);
415         if (len == -1) {
416                 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
417                 return false;
418         }
419
420         if (token.type != SPNEGO_NEG_TOKEN_TARG) {
421                 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
422                         token.type));
423                 spnego_free_data(&token);
424                 return false;
425         }
426
427         *auth = data_blob_talloc(talloc_tos(),
428                                  token.negTokenTarg.responseToken.data,
429                                  token.negTokenTarg.responseToken.length);
430         spnego_free_data(&token);
431
432         return true;
433 }
434
435 /*
436   generate a minimal SPNEGO response packet.  Doesn't contain much.
437 */
438 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
439                                    const char *mechOID)
440 {
441         ASN1_DATA *data;
442         DATA_BLOB ret;
443         uint8 negResult;
444
445         if (NT_STATUS_IS_OK(nt_status)) {
446                 negResult = SPNEGO_ACCEPT_COMPLETED;
447         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
448                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
449         } else {
450                 negResult = SPNEGO_REJECT;
451         }
452
453         data = asn1_init(talloc_tos());
454         if (data == NULL) {
455                 return data_blob_null;
456         }
457
458         asn1_push_tag(data, ASN1_CONTEXT(1));
459         asn1_push_tag(data, ASN1_SEQUENCE(0));
460         asn1_push_tag(data, ASN1_CONTEXT(0));
461         asn1_write_enumerated(data, negResult);
462         asn1_pop_tag(data);
463
464         if (mechOID) {
465                 asn1_push_tag(data,ASN1_CONTEXT(1));
466                 asn1_write_OID(data, mechOID);
467                 asn1_pop_tag(data);
468         }
469
470         if (reply && reply->data != NULL) {
471                 asn1_push_tag(data,ASN1_CONTEXT(2));
472                 asn1_write_OctetString(data, reply->data, reply->length);
473                 asn1_pop_tag(data);
474         }
475
476         asn1_pop_tag(data);
477         asn1_pop_tag(data);
478
479         ret = data_blob(data->data, data->length);
480         asn1_free(data);
481         return ret;
482 }
483
484 /*
485  parse a SPNEGO auth packet. This contains the encrypted passwords
486 */
487 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
488                                 const char *mechOID,
489                                 DATA_BLOB *auth)
490 {
491         ASN1_DATA *data;
492         uint8 negResult;
493
494         if (NT_STATUS_IS_OK(nt_status)) {
495                 negResult = SPNEGO_ACCEPT_COMPLETED;
496         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
497                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
498         } else {
499                 negResult = SPNEGO_REJECT;
500         }
501
502         data = asn1_init(talloc_tos());
503         if (data == NULL) {
504                 return false;
505         }
506
507         asn1_load(data, blob);
508         asn1_start_tag(data, ASN1_CONTEXT(1));
509         asn1_start_tag(data, ASN1_SEQUENCE(0));
510         asn1_start_tag(data, ASN1_CONTEXT(0));
511         asn1_check_enumerated(data, negResult);
512         asn1_end_tag(data);
513
514         *auth = data_blob_null;
515
516         if (asn1_tag_remaining(data)) {
517                 asn1_start_tag(data,ASN1_CONTEXT(1));
518                 asn1_check_OID(data, mechOID);
519                 asn1_end_tag(data);
520
521                 if (asn1_tag_remaining(data)) {
522                         asn1_start_tag(data,ASN1_CONTEXT(2));
523                         asn1_read_OctetString(data, talloc_autofree_context(), auth);
524                         asn1_end_tag(data);
525                 }
526         } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
527                 data->has_error = 1;
528         }
529
530         /* Binding against Win2K DC returns a duplicate of the responseToken in
531          * the optional mechListMIC field. This is a bug in Win2K. We ignore
532          * this field if it exists. Win2K8 may return a proper mechListMIC at
533          * which point we need to implement the integrity checking. */
534         if (asn1_tag_remaining(data)) {
535                 DATA_BLOB mechList = data_blob_null;
536                 asn1_start_tag(data, ASN1_CONTEXT(3));
537                 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
538                 asn1_end_tag(data);
539                 data_blob_free(&mechList);
540                 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
541                     "ignoring.\n"));
542         }
543
544         asn1_end_tag(data);
545         asn1_end_tag(data);
546
547         if (data->has_error) {
548                 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
549                 asn1_free(data);
550                 data_blob_free(auth);
551                 return False;
552         }
553
554         asn1_free(data);
555         return True;
556 }