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