s3-dcerpc: add spnego server helpers
[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(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_and_mic(TALLOC_CTX *ctx, DATA_BLOB blob,
412                                 DATA_BLOB *auth, DATA_BLOB *signature)
413 {
414         ssize_t len;
415         struct spnego_data token;
416
417         len = spnego_read_data(talloc_tos(), blob, &token);
418         if (len == -1) {
419                 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
420                 return false;
421         }
422
423         if (token.type != SPNEGO_NEG_TOKEN_TARG) {
424                 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
425                         token.type));
426                 spnego_free_data(&token);
427                 return false;
428         }
429
430         *auth = data_blob_talloc(ctx,
431                                  token.negTokenTarg.responseToken.data,
432                                  token.negTokenTarg.responseToken.length);
433
434         if (!signature) {
435                 goto done;
436         }
437
438         *signature = data_blob_talloc(ctx,
439                                  token.negTokenTarg.mechListMIC.data,
440                                  token.negTokenTarg.mechListMIC.length);
441
442 done:
443         spnego_free_data(&token);
444
445         return true;
446 }
447
448 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
449 {
450         return spnego_parse_auth_and_mic(ctx, blob, auth, NULL);
451 }
452
453 /*
454   generate a minimal SPNEGO response packet.  Doesn't contain much.
455 */
456 DATA_BLOB spnego_gen_auth_response_and_mic(TALLOC_CTX *ctx,
457                                            NTSTATUS nt_status,
458                                            const char *mechOID,
459                                            DATA_BLOB *reply,
460                                            DATA_BLOB *mechlistMIC)
461 {
462         ASN1_DATA *data;
463         DATA_BLOB ret;
464         uint8 negResult;
465
466         if (NT_STATUS_IS_OK(nt_status)) {
467                 negResult = SPNEGO_ACCEPT_COMPLETED;
468         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
469                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
470         } else {
471                 negResult = SPNEGO_REJECT;
472         }
473
474         data = asn1_init(talloc_tos());
475         if (data == NULL) {
476                 return data_blob_null;
477         }
478
479         asn1_push_tag(data, ASN1_CONTEXT(1));
480         asn1_push_tag(data, ASN1_SEQUENCE(0));
481         asn1_push_tag(data, ASN1_CONTEXT(0));
482         asn1_write_enumerated(data, negResult);
483         asn1_pop_tag(data);
484
485         if (mechOID) {
486                 asn1_push_tag(data,ASN1_CONTEXT(1));
487                 asn1_write_OID(data, mechOID);
488                 asn1_pop_tag(data);
489         }
490
491         if (reply && reply->data != NULL) {
492                 asn1_push_tag(data,ASN1_CONTEXT(2));
493                 asn1_write_OctetString(data, reply->data, reply->length);
494                 asn1_pop_tag(data);
495         }
496
497         if (mechlistMIC && mechlistMIC->data != NULL) {
498                 asn1_push_tag(data, ASN1_CONTEXT(3));
499                 asn1_write_OctetString(data,
500                                         mechlistMIC->data,
501                                         mechlistMIC->length);
502                 asn1_pop_tag(data);
503         }
504
505         asn1_pop_tag(data);
506         asn1_pop_tag(data);
507
508         ret = data_blob_talloc(ctx, data->data, data->length);
509         asn1_free(data);
510         return ret;
511 }
512
513 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx, DATA_BLOB *reply,
514                                    NTSTATUS nt_status, const char *mechOID)
515 {
516         return spnego_gen_auth_response_and_mic(ctx, nt_status,
517                                                 mechOID, reply, NULL);
518 }
519
520 /*
521  parse a SPNEGO auth packet. This contains the encrypted passwords
522 */
523 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
524                                 DATA_BLOB blob, NTSTATUS nt_status,
525                                 const char *mechOID,
526                                 DATA_BLOB *auth)
527 {
528         ASN1_DATA *data;
529         uint8 negResult;
530
531         if (NT_STATUS_IS_OK(nt_status)) {
532                 negResult = SPNEGO_ACCEPT_COMPLETED;
533         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
534                 negResult = SPNEGO_ACCEPT_INCOMPLETE;
535         } else {
536                 negResult = SPNEGO_REJECT;
537         }
538
539         data = asn1_init(talloc_tos());
540         if (data == NULL) {
541                 return false;
542         }
543
544         asn1_load(data, blob);
545         asn1_start_tag(data, ASN1_CONTEXT(1));
546         asn1_start_tag(data, ASN1_SEQUENCE(0));
547         asn1_start_tag(data, ASN1_CONTEXT(0));
548         asn1_check_enumerated(data, negResult);
549         asn1_end_tag(data);
550
551         *auth = data_blob_null;
552
553         if (asn1_tag_remaining(data)) {
554                 asn1_start_tag(data,ASN1_CONTEXT(1));
555                 asn1_check_OID(data, mechOID);
556                 asn1_end_tag(data);
557
558                 if (asn1_tag_remaining(data)) {
559                         asn1_start_tag(data,ASN1_CONTEXT(2));
560                         asn1_read_OctetString(data, ctx, auth);
561                         asn1_end_tag(data);
562                 }
563         } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
564                 data->has_error = 1;
565         }
566
567         /* Binding against Win2K DC returns a duplicate of the responseToken in
568          * the optional mechListMIC field. This is a bug in Win2K. We ignore
569          * this field if it exists. Win2K8 may return a proper mechListMIC at
570          * which point we need to implement the integrity checking. */
571         if (asn1_tag_remaining(data)) {
572                 DATA_BLOB mechList = data_blob_null;
573                 asn1_start_tag(data, ASN1_CONTEXT(3));
574                 asn1_read_OctetString(data, ctx, &mechList);
575                 asn1_end_tag(data);
576                 data_blob_free(&mechList);
577                 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
578                     "ignoring.\n"));
579         }
580
581         asn1_end_tag(data);
582         asn1_end_tag(data);
583
584         if (data->has_error) {
585                 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
586                 asn1_free(data);
587                 data_blob_free(auth);
588                 return False;
589         }
590
591         asn1_free(data);
592         return True;
593 }
594
595 bool spnego_mech_list_blob(TALLOC_CTX *mem_ctx,
596                            char **oid_list, DATA_BLOB *raw_data)
597 {
598         ASN1_DATA *data;
599         unsigned int idx;
600
601         if (!oid_list || !oid_list[0] || !raw_data) {
602                 return false;
603         }
604
605         data = asn1_init(talloc_tos());
606         if (data == NULL) {
607                 return false;
608         }
609
610         asn1_push_tag(data, ASN1_SEQUENCE(0));
611         for (idx = 0; oid_list[idx]; idx++) {
612                 asn1_write_OID(data, oid_list[idx]);
613         }
614         asn1_pop_tag(data);
615
616         if (data->has_error) {
617                 DEBUG(3, (__location__ " failed at %d\n", (int)data->ofs));
618                 asn1_free(data);
619                 return false;
620         }
621
622         *raw_data = data_blob_talloc(mem_ctx, data->data, data->length);
623         if (!raw_data->data) {
624                 DEBUG(3, (__location__": data_blob_talloc() failed!\n"));
625                 asn1_free(data);
626                 return false;
627         }
628
629         asn1_free(data);
630         return true;
631 }