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(const char *OIDs[],
34 const char *principal)
40 data = asn1_init(talloc_tos());
42 return data_blob_null;
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));
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]);
58 if (psecblob && psecblob->length && psecblob->data) {
59 asn1_push_tag(data, ASN1_CONTEXT(2));
60 asn1_write_OctetString(data,psecblob->data,
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);
80 if (data->has_error) {
81 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
84 ret = data_blob(data->data, data->length);
91 parse a negTokenInit packet giving a GUID, a list of supported
92 OIDs (the mechanisms) and a principal name string
94 bool spnego_parse_negTokenInit(DATA_BLOB blob,
95 char *OIDs[ASN1_MAX_OIDS],
103 data = asn1_init(talloc_tos());
108 asn1_load(data, blob);
110 asn1_start_tag(data,ASN1_APPLICATION(0));
112 asn1_check_OID(data,OID_SPNEGO);
114 /* negTokenInit [0] NegTokenInit */
115 asn1_start_tag(data,ASN1_CONTEXT(0));
116 asn1_start_tag(data,ASN1_SEQUENCE(0));
118 /* mechTypes [0] MechTypeList OPTIONAL */
120 /* Not really optional, we depend on this to decide
121 * what mechanisms we have to work with. */
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);
138 *secblob = data_blob_null;
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).
148 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
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);
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(),
171 data_blob_free(&sblob);
175 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
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(),
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(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(data->data, data->length);
247 parse a krb5 GSS-API wrapper packet giving a ticket
249 bool spnego_parse_krb5_wrap(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(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(const char *principal, int time_offset,
295 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
299 DATA_BLOB tkt, tkt_wrapped;
300 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
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,
310 /* wrap that up in a nice GSS-API wrapping */
311 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
313 /* and wrap that in a shiny SPNEGO wrapper */
314 *targ = spnego_gen_negTokenInit(krb_mechs, &tkt_wrapped, NULL);
316 data_blob_free(&tkt_wrapped);
317 data_blob_free(&tkt);
324 parse a spnego NTLMSSP challenge packet giving two security blobs
326 bool spnego_parse_challenge(const DATA_BLOB blob,
327 DATA_BLOB *chal1, DATA_BLOB *chal2)
335 data = asn1_init(talloc_tos());
340 asn1_load(data, blob);
341 asn1_start_tag(data,ASN1_CONTEXT(1));
342 asn1_start_tag(data,ASN1_SEQUENCE(0));
344 asn1_start_tag(data,ASN1_CONTEXT(0));
345 asn1_check_enumerated(data,1);
348 asn1_start_tag(data,ASN1_CONTEXT(1));
349 asn1_check_OID(data, OID_NTLMSSP);
352 asn1_start_tag(data,ASN1_CONTEXT(2));
353 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
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);
366 ret = !data->has_error;
368 if (data->has_error) {
369 data_blob_free(chal1);
370 data_blob_free(chal2);
379 generate a SPNEGO auth packet. This will contain the encrypted passwords
381 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
386 data = asn1_init(talloc_tos());
388 return data_blob_null;
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);
399 ret = data_blob(data->data, data->length);
407 parse a SPNEGO auth packet. This contains the encrypted passwords
409 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
412 struct spnego_data token;
414 len = spnego_read_data(talloc_tos(), blob, &token);
416 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
420 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
421 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
423 spnego_free_data(&token);
427 *auth = data_blob_talloc(talloc_tos(),
428 token.negTokenTarg.responseToken.data,
429 token.negTokenTarg.responseToken.length);
430 spnego_free_data(&token);
436 generate a minimal SPNEGO response packet. Doesn't contain much.
438 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
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;
450 negResult = SPNEGO_REJECT;
453 data = asn1_init(talloc_tos());
455 return data_blob_null;
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);
465 asn1_push_tag(data,ASN1_CONTEXT(1));
466 asn1_write_OID(data, mechOID);
470 if (reply && reply->data != NULL) {
471 asn1_push_tag(data,ASN1_CONTEXT(2));
472 asn1_write_OctetString(data, reply->data, reply->length);
479 ret = data_blob(data->data, data->length);
485 parse a SPNEGO auth packet. This contains the encrypted passwords
487 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
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;
499 negResult = SPNEGO_REJECT;
502 data = asn1_init(talloc_tos());
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);
514 *auth = data_blob_null;
516 if (asn1_tag_remaining(data)) {
517 asn1_start_tag(data,ASN1_CONTEXT(1));
518 asn1_check_OID(data, mechOID);
521 if (asn1_tag_remaining(data)) {
522 asn1_start_tag(data,ASN1_CONTEXT(2));
523 asn1_read_OctetString(data, talloc_autofree_context(), auth);
526 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
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);
539 data_blob_free(&mechList);
540 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
547 if (data->has_error) {
548 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
550 data_blob_free(auth);