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/>.
24 #include "../libcli/auth/spnego.h"
28 generate a negTokenInit packet given a list of supported
29 OIDs (the mechanisms) a blob, and a principal name string
32 DATA_BLOB spnego_gen_negTokenInit(TALLOC_CTX *ctx,
35 const char *principal)
41 data = asn1_init(talloc_tos());
43 return data_blob_null;
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));
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]);
59 if (psecblob && psecblob->length && psecblob->data) {
60 asn1_push_tag(data, ASN1_CONTEXT(2));
61 asn1_write_OctetString(data,psecblob->data,
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);
81 if (data->has_error) {
82 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
85 ret = data_blob_talloc(ctx, data->data, data->length);
92 parse a negTokenInit packet giving a GUID, a list of supported
93 OIDs (the mechanisms) and a principal name string
95 bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
97 char *OIDs[ASN1_MAX_OIDS],
105 data = asn1_init(talloc_tos());
110 asn1_load(data, blob);
112 asn1_start_tag(data,ASN1_APPLICATION(0));
114 asn1_check_OID(data,OID_SPNEGO);
116 /* negTokenInit [0] NegTokenInit */
117 asn1_start_tag(data,ASN1_CONTEXT(0));
118 asn1_start_tag(data,ASN1_SEQUENCE(0));
120 /* mechTypes [0] MechTypeList OPTIONAL */
122 /* Not really optional, we depend on this to decide
123 * what mechanisms we have to work with. */
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);
140 *secblob = data_blob_null;
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).
150 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
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);
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);
172 data_blob_free(&sblob);
176 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
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);
198 ret = !data->has_error;
199 if (data->has_error) {
202 TALLOC_FREE(*principal);
205 data_blob_free(secblob);
207 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
208 TALLOC_FREE(OIDs[j]);
217 generate a krb5 GSS-API wrapper packet given a ticket
219 DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
224 data = asn1_init(talloc_tos());
226 return data_blob_null;
229 asn1_push_tag(data, ASN1_APPLICATION(0));
230 asn1_write_OID(data, OID_KERBEROS5);
232 asn1_write(data, tok_id, 2);
233 asn1_write(data, ticket.data, ticket.length);
236 if (data->has_error) {
237 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
240 ret = data_blob_talloc(ctx, data->data, data->length);
247 parse a krb5 GSS-API wrapper packet giving a ticket
249 bool spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
255 data = asn1_init(talloc_tos());
260 asn1_load(data, blob);
261 asn1_start_tag(data, ASN1_APPLICATION(0));
262 asn1_check_OID(data, OID_KERBEROS5);
264 data_remaining = asn1_tag_remaining(data);
266 if (data_remaining < 3) {
267 data->has_error = True;
269 asn1_read(data, tok_id, 2);
271 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
272 asn1_read(data, ticket->data, ticket->length);
277 ret = !data->has_error;
279 if (data->has_error) {
280 data_blob_free(ticket);
290 generate a SPNEGO krb5 negTokenInit packet, ready for a EXTENDED_SECURITY
291 kerberos session setup
293 int spnego_gen_krb5_negTokenInit(TALLOC_CTX *ctx,
294 const char *principal, int time_offset,
296 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
300 DATA_BLOB tkt, tkt_wrapped;
301 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
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,
312 /* wrap that up in a nice GSS-API wrapping */
313 tkt_wrapped = spnego_gen_krb5_wrap(ctx, tkt, TOK_ID_KRB_AP_REQ);
315 /* and wrap that in a shiny SPNEGO wrapper */
316 *targ = spnego_gen_negTokenInit(ctx, krb_mechs, &tkt_wrapped, NULL);
318 data_blob_free(&tkt_wrapped);
319 data_blob_free(&tkt);
326 parse a spnego NTLMSSP challenge packet giving two security blobs
328 bool spnego_parse_challenge(TALLOC_CTX *ctx, const DATA_BLOB blob,
329 DATA_BLOB *chal1, DATA_BLOB *chal2)
337 data = asn1_init(talloc_tos());
342 asn1_load(data, blob);
343 asn1_start_tag(data,ASN1_CONTEXT(1));
344 asn1_start_tag(data,ASN1_SEQUENCE(0));
346 asn1_start_tag(data,ASN1_CONTEXT(0));
347 asn1_check_enumerated(data,1);
350 asn1_start_tag(data,ASN1_CONTEXT(1));
351 asn1_check_OID(data, OID_NTLMSSP);
354 asn1_start_tag(data,ASN1_CONTEXT(2));
355 asn1_read_OctetString(data, ctx, chal1);
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);
368 ret = !data->has_error;
370 if (data->has_error) {
371 data_blob_free(chal1);
372 data_blob_free(chal2);
381 generate a SPNEGO auth packet. This will contain the encrypted passwords
383 DATA_BLOB spnego_gen_auth(TALLOC_CTX *ctx, DATA_BLOB blob)
388 data = asn1_init(talloc_tos());
390 return data_blob_null;
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);
401 ret = data_blob_talloc(ctx, data->data, data->length);
409 parse a SPNEGO auth packet. This contains the encrypted passwords
411 bool spnego_parse_auth(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *auth)
414 struct spnego_data token;
416 len = spnego_read_data(talloc_tos(), blob, &token);
418 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
422 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
423 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
425 spnego_free_data(&token);
429 *auth = data_blob_talloc(ctx,
430 token.negTokenTarg.responseToken.data,
431 token.negTokenTarg.responseToken.length);
432 spnego_free_data(&token);
438 generate a minimal SPNEGO response packet. Doesn't contain much.
440 DATA_BLOB spnego_gen_auth_response(TALLOC_CTX *ctx,
441 DATA_BLOB *reply, NTSTATUS nt_status,
448 if (NT_STATUS_IS_OK(nt_status)) {
449 negResult = SPNEGO_ACCEPT_COMPLETED;
450 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
451 negResult = SPNEGO_ACCEPT_INCOMPLETE;
453 negResult = SPNEGO_REJECT;
456 data = asn1_init(talloc_tos());
458 return data_blob_null;
461 asn1_push_tag(data, ASN1_CONTEXT(1));
462 asn1_push_tag(data, ASN1_SEQUENCE(0));
463 asn1_push_tag(data, ASN1_CONTEXT(0));
464 asn1_write_enumerated(data, negResult);
468 asn1_push_tag(data,ASN1_CONTEXT(1));
469 asn1_write_OID(data, mechOID);
473 if (reply && reply->data != NULL) {
474 asn1_push_tag(data,ASN1_CONTEXT(2));
475 asn1_write_OctetString(data, reply->data, reply->length);
482 ret = data_blob_talloc(ctx, data->data, data->length);
488 parse a SPNEGO auth packet. This contains the encrypted passwords
490 bool spnego_parse_auth_response(TALLOC_CTX *ctx,
491 DATA_BLOB blob, NTSTATUS nt_status,
498 if (NT_STATUS_IS_OK(nt_status)) {
499 negResult = SPNEGO_ACCEPT_COMPLETED;
500 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
501 negResult = SPNEGO_ACCEPT_INCOMPLETE;
503 negResult = SPNEGO_REJECT;
506 data = asn1_init(talloc_tos());
511 asn1_load(data, blob);
512 asn1_start_tag(data, ASN1_CONTEXT(1));
513 asn1_start_tag(data, ASN1_SEQUENCE(0));
514 asn1_start_tag(data, ASN1_CONTEXT(0));
515 asn1_check_enumerated(data, negResult);
518 *auth = data_blob_null;
520 if (asn1_tag_remaining(data)) {
521 asn1_start_tag(data,ASN1_CONTEXT(1));
522 asn1_check_OID(data, mechOID);
525 if (asn1_tag_remaining(data)) {
526 asn1_start_tag(data,ASN1_CONTEXT(2));
527 asn1_read_OctetString(data, ctx, auth);
530 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
534 /* Binding against Win2K DC returns a duplicate of the responseToken in
535 * the optional mechListMIC field. This is a bug in Win2K. We ignore
536 * this field if it exists. Win2K8 may return a proper mechListMIC at
537 * which point we need to implement the integrity checking. */
538 if (asn1_tag_remaining(data)) {
539 DATA_BLOB mechList = data_blob_null;
540 asn1_start_tag(data, ASN1_CONTEXT(3));
541 asn1_read_OctetString(data, ctx, &mechList);
543 data_blob_free(&mechList);
544 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
551 if (data->has_error) {
552 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
554 data_blob_free(auth);