script/autobuild.py: remove --rebase-master and --push-master options
[Samba/gebeck_regimport.git] / source3 / libsmb / clispnego.c
bloba17efbf75b2eb7e2da287667a26e5cdd7ac49cc3
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
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.
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.
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/>.
23 #include "includes.h"
24 #include "../libcli/auth/spnego.h"
25 #include "smb_krb5.h"
26 #include "../lib/util/asn1.h"
29 generate a negTokenInit packet given a list of supported
30 OIDs (the mechanisms) a blob, and a principal name string
33 DATA_BLOB spnego_gen_negTokenInit(TALLOC_CTX *ctx,
34 const char *OIDs[],
35 DATA_BLOB *psecblob,
36 const char *principal)
38 int i;
39 ASN1_DATA *data;
40 DATA_BLOB ret;
42 data = asn1_init(talloc_tos());
43 if (data == NULL) {
44 return data_blob_null;
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));
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]);
57 asn1_pop_tag(data);
58 asn1_pop_tag(data);
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);
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);
77 asn1_pop_tag(data);
78 asn1_pop_tag(data);
80 asn1_pop_tag(data);
82 if (data->has_error) {
83 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
86 ret = data_blob_talloc(ctx, data->data, data->length);
87 asn1_free(data);
89 return ret;
93 parse a negTokenInit packet giving a GUID, a list of supported
94 OIDs (the mechanisms) and a principal name string
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)
102 int i;
103 bool ret;
104 ASN1_DATA *data;
106 data = asn1_init(talloc_tos());
107 if (data == NULL) {
108 return false;
111 asn1_load(data, blob);
113 asn1_start_tag(data,ASN1_APPLICATION(0));
115 asn1_check_OID(data,OID_SPNEGO);
117 /* negTokenInit [0] NegTokenInit */
118 asn1_start_tag(data,ASN1_CONTEXT(0));
119 asn1_start_tag(data,ASN1_SEQUENCE(0));
121 /* mechTypes [0] MechTypeList OPTIONAL */
123 /* Not really optional, we depend on this to decide
124 * what mechanisms we have to work with. */
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;
132 if (data->has_error) {
133 break;
136 OIDs[i] = NULL;
137 asn1_end_tag(data);
138 asn1_end_tag(data);
140 if (principal) {
141 *principal = NULL;
143 if (secblob) {
144 *secblob = data_blob_null;
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).
154 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
155 uint8 flags;
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);
163 asn1_end_tag(data);
164 asn1_end_tag(data);
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);
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);
197 asn1_end_tag(data);
198 asn1_end_tag(data);
200 asn1_end_tag(data);
202 ret = !data->has_error;
203 if (data->has_error) {
204 int j;
205 if (principal) {
206 TALLOC_FREE(*principal);
208 if (secblob) {
209 data_blob_free(secblob);
211 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
212 TALLOC_FREE(OIDs[j]);
216 asn1_free(data);
217 return ret;
221 generate a krb5 GSS-API wrapper packet given a ticket
223 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
225 ASN1_DATA *data;
226 DATA_BLOB ret;
228 data = asn1_init(talloc_tos());
229 if (data == NULL) {
230 return data_blob_null;
233 asn1_push_tag(data, ASN1_APPLICATION(0));
234 asn1_write_OID(data, OID_KERBEROS5);
236 asn1_write(data, tok_id, 2);
237 asn1_write(data, ticket.data, ticket.length);
238 asn1_pop_tag(data);
240 if (data->has_error) {
241 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
244 ret = data_blob_talloc(ctx, data->data, data->length);
245 asn1_free(data);
247 return ret;
251 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
252 kerberos session setup
254 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
255 const char *principal, int time_offset,
256 DATA_BLOB *targ,
257 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
258 const char *ccname, time_t *expire_time)
260 int retval;
261 DATA_BLOB tkt, tkt_wrapped;
262 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
264 /* get a kerberos ticket for the service and extract the session key */
265 retval = cli_krb5_get_ticket(ctx, principal, time_offset,
266 &tkt, session_key_krb5,
267 extra_ap_opts, ccname,
268 expire_time, NULL);
269 if (retval) {
270 return retval;
273 /* wrap that up in a nice GSS-API wrapping */
274 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
276 /* and wrap that in a shiny SPNEGO wrapper */
277 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
279 data_blob_free(&tkt_wrapped);
280 data_blob_free(&tkt);
282 return retval;
287 parse a spnego NTLMSSP challenge packet giving two security blobs
289 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
290 DATA_BLOB *chal1, DATA_BLOB *chal2)
292 bool ret;
293 ASN1_DATA *data;
295 ZERO_STRUCTP(chal1);
296 ZERO_STRUCTP(chal2);
298 data = asn1_init(talloc_tos());
299 if (data == NULL) {
300 return false;
303 asn1_load(data, blob);
304 asn1_start_tag(data,ASN1_CONTEXT(1));
305 asn1_start_tag(data,ASN1_SEQUENCE(0));
307 asn1_start_tag(data,ASN1_CONTEXT(0));
308 asn1_check_enumerated(data,1);
309 asn1_end_tag(data);
311 asn1_start_tag(data,ASN1_CONTEXT(1));
312 asn1_check_OID(data, OID_NTLMSSP);
313 asn1_end_tag(data);
315 asn1_start_tag(data,ASN1_CONTEXT(2));
316 asn1_read_OctetString(data, ctx, chal1);
317 asn1_end_tag(data);
319 /* the second challenge is optional (XP doesn't send it) */
320 if (asn1_tag_remaining(data)) {
321 asn1_start_tag(data,ASN1_CONTEXT(3));
322 asn1_read_OctetString(data, ctx, chal2);
323 asn1_end_tag(data);
326 asn1_end_tag(data);
327 asn1_end_tag(data);
329 ret = !data->has_error;
331 if (data->has_error) {
332 data_blob_free(chal1);
333 data_blob_free(chal2);
336 asn1_free(data);
337 return ret;
342 generate a SPNEGO auth packet. This will contain the encrypted passwords
344 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
346 ASN1_DATA *data;
347 DATA_BLOB ret;
349 data = asn1_init(talloc_tos());
350 if (data == NULL) {
351 return data_blob_null;
354 asn1_push_tag(data, ASN1_CONTEXT(1));
355 asn1_push_tag(data, ASN1_SEQUENCE(0));
356 asn1_push_tag(data, ASN1_CONTEXT(2));
357 asn1_write_OctetString(data,blob.data,blob.length);
358 asn1_pop_tag(data);
359 asn1_pop_tag(data);
360 asn1_pop_tag(data);
362 ret = data_blob_talloc(ctx, data->data, data->length);
364 asn1_free(data);
366 return ret;
370 parse a SPNEGO auth packet. This contains the encrypted passwords
372 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
373 DATA_BLOB blob, NTSTATUS nt_status,
374 const char *mechOID,
375 DATA_BLOB *auth)
377 ASN1_DATA *data;
378 uint8 negResult;
380 if (NT_STATUS_IS_OK(nt_status)) {
381 negResult = SPNEGO_ACCEPT_COMPLETED;
382 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
383 negResult = SPNEGO_ACCEPT_INCOMPLETE;
384 } else {
385 negResult = SPNEGO_REJECT;
388 data = asn1_init(talloc_tos());
389 if (data == NULL) {
390 return false;
393 asn1_load(data, blob);
394 asn1_start_tag(data, ASN1_CONTEXT(1));
395 asn1_start_tag(data, ASN1_SEQUENCE(0));
396 asn1_start_tag(data, ASN1_CONTEXT(0));
397 asn1_check_enumerated(data, negResult);
398 asn1_end_tag(data);
400 *auth = data_blob_null;
402 if (asn1_tag_remaining(data)) {
403 asn1_start_tag(data,ASN1_CONTEXT(1));
404 asn1_check_OID(data, mechOID);
405 asn1_end_tag(data);
407 if (asn1_tag_remaining(data)) {
408 asn1_start_tag(data,ASN1_CONTEXT(2));
409 asn1_read_OctetString(data, ctx, auth);
410 asn1_end_tag(data);
412 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
413 data->has_error = 1;
416 /* Binding against Win2K DC returns a duplicate of the responseToken in
417 * the optional mechListMIC field. This is a bug in Win2K. We ignore
418 * this field if it exists. Win2K8 may return a proper mechListMIC at
419 * which point we need to implement the integrity checking. */
420 if (asn1_tag_remaining(data)) {
421 DATA_BLOB mechList = data_blob_null;
422 asn1_start_tag(data, ASN1_CONTEXT(3));
423 asn1_read_OctetString(data, ctx, &mechList);
424 asn1_end_tag(data);
425 data_blob_free(&mechList);
426 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
427 "ignoring.\n"));
430 asn1_end_tag(data);
431 asn1_end_tag(data);
433 if (data->has_error) {
434 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
435 asn1_free(data);
436 data_blob_free(auth);
437 return False;
440 asn1_free(data);
441 return True;