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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "../libcli/auth/spnego.h"
27 generate a negTokenInit packet given a GUID, a list of supported
28 OIDs (the mechanisms) and a principal name string
30 DATA_BLOB spnego_gen_negTokenInit(char guid[16],
32 const char *principal)
38 data = asn1_init(talloc_tos());
40 return data_blob_null;
43 asn1_write(data, guid, 16);
44 asn1_push_tag(data,ASN1_APPLICATION(0));
45 asn1_write_OID(data,OID_SPNEGO);
46 asn1_push_tag(data,ASN1_CONTEXT(0));
47 asn1_push_tag(data,ASN1_SEQUENCE(0));
49 asn1_push_tag(data,ASN1_CONTEXT(0));
50 asn1_push_tag(data,ASN1_SEQUENCE(0));
51 for (i=0; OIDs[i]; i++) {
52 asn1_write_OID(data,OIDs[i]);
57 asn1_push_tag(data, ASN1_CONTEXT(3));
58 asn1_push_tag(data, ASN1_SEQUENCE(0));
59 asn1_push_tag(data, ASN1_CONTEXT(0));
60 asn1_write_GeneralString(data,principal);
70 if (data->has_error) {
71 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
74 ret = data_blob(data->data, data->length);
81 Generate a negTokenInit as used by the client side ... It has a mechType
82 (OID), and a mechToken (a security blob) ...
84 Really, we need to break out the NTLMSSP stuff as well, because it could be
87 DATA_BLOB gen_negTokenInit(const char *OID, DATA_BLOB blob)
92 data = asn1_init(talloc_tos());
94 return data_blob_null;
97 asn1_push_tag(data, ASN1_APPLICATION(0));
98 asn1_write_OID(data,OID_SPNEGO);
99 asn1_push_tag(data, ASN1_CONTEXT(0));
100 asn1_push_tag(data, ASN1_SEQUENCE(0));
102 asn1_push_tag(data, ASN1_CONTEXT(0));
103 asn1_push_tag(data, ASN1_SEQUENCE(0));
104 asn1_write_OID(data, OID);
108 asn1_push_tag(data, ASN1_CONTEXT(2));
109 asn1_write_OctetString(data,blob.data,blob.length);
117 if (data->has_error) {
118 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
121 ret = data_blob(data->data, data->length);
128 parse a negTokenInit packet giving a GUID, a list of supported
129 OIDs (the mechanisms) and a principal name string
131 bool spnego_parse_negTokenInit(DATA_BLOB blob,
132 char *OIDs[ASN1_MAX_OIDS],
139 data = asn1_init(talloc_tos());
144 asn1_load(data, blob);
146 asn1_start_tag(data,ASN1_APPLICATION(0));
148 asn1_check_OID(data,OID_SPNEGO);
149 asn1_start_tag(data,ASN1_CONTEXT(0));
150 asn1_start_tag(data,ASN1_SEQUENCE(0));
152 asn1_start_tag(data,ASN1_CONTEXT(0));
153 asn1_start_tag(data,ASN1_SEQUENCE(0));
154 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
155 const char *oid_str = NULL;
156 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
157 OIDs[i] = CONST_DISCARD(char *, oid_str);
164 if (asn1_tag_remaining(data) > 0) {
165 asn1_start_tag(data, ASN1_CONTEXT(3));
166 asn1_start_tag(data, ASN1_SEQUENCE(0));
167 asn1_start_tag(data, ASN1_CONTEXT(0));
168 asn1_read_GeneralString(data,talloc_autofree_context(),principal);
179 ret = !data->has_error;
180 if (data->has_error) {
182 TALLOC_FREE(*principal);
183 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
184 TALLOC_FREE(OIDs[j]);
193 generate a negTokenTarg packet given a list of OIDs and a security blob
195 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
201 data = asn1_init(talloc_tos());
203 return data_blob_null;
206 asn1_push_tag(data, ASN1_APPLICATION(0));
207 asn1_write_OID(data,OID_SPNEGO);
208 asn1_push_tag(data, ASN1_CONTEXT(0));
209 asn1_push_tag(data, ASN1_SEQUENCE(0));
211 asn1_push_tag(data, ASN1_CONTEXT(0));
212 asn1_push_tag(data, ASN1_SEQUENCE(0));
213 for (i=0; OIDs[i]; i++) {
214 asn1_write_OID(data,OIDs[i]);
219 asn1_push_tag(data, ASN1_CONTEXT(2));
220 asn1_write_OctetString(data,blob.data,blob.length);
228 if (data->has_error) {
229 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data->ofs));
232 ret = data_blob(data->data, data->length);
239 parse a negTokenTarg packet giving a list of OIDs and a security blob
241 bool parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
246 data = asn1_init(talloc_tos());
251 asn1_load(data, blob);
252 asn1_start_tag(data, ASN1_APPLICATION(0));
253 asn1_check_OID(data,OID_SPNEGO);
254 asn1_start_tag(data, ASN1_CONTEXT(0));
255 asn1_start_tag(data, ASN1_SEQUENCE(0));
257 asn1_start_tag(data, ASN1_CONTEXT(0));
258 asn1_start_tag(data, ASN1_SEQUENCE(0));
259 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
260 const char *oid_str = NULL;
261 asn1_read_OID(data,talloc_autofree_context(),&oid_str);
262 OIDs[i] = CONST_DISCARD(char *, oid_str);
268 /* Skip any optional req_flags that are sent per RFC 4178 */
269 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
272 asn1_start_tag(data, ASN1_CONTEXT(1));
273 asn1_start_tag(data, ASN1_BIT_STRING);
274 while (asn1_tag_remaining(data) > 0)
275 asn1_read_uint8(data, &flags);
280 asn1_start_tag(data, ASN1_CONTEXT(2));
281 asn1_read_OctetString(data,talloc_autofree_context(),secblob);
289 if (data->has_error) {
291 data_blob_free(secblob);
292 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
293 TALLOC_FREE(OIDs[j]);
295 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data->ofs));
305 generate a krb5 GSS-API wrapper packet given a ticket
307 DATA_BLOB spnego_gen_krb5_wrap(const DATA_BLOB ticket, const uint8 tok_id[2])
312 data = asn1_init(talloc_tos());
314 return data_blob_null;
317 asn1_push_tag(data, ASN1_APPLICATION(0));
318 asn1_write_OID(data, OID_KERBEROS5);
320 asn1_write(data, tok_id, 2);
321 asn1_write(data, ticket.data, ticket.length);
324 if (data->has_error) {
325 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data->ofs));
328 ret = data_blob(data->data, data->length);
335 parse a krb5 GSS-API wrapper packet giving a ticket
337 bool spnego_parse_krb5_wrap(DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
343 data = asn1_init(talloc_tos());
348 asn1_load(data, blob);
349 asn1_start_tag(data, ASN1_APPLICATION(0));
350 asn1_check_OID(data, OID_KERBEROS5);
352 data_remaining = asn1_tag_remaining(data);
354 if (data_remaining < 3) {
355 data->has_error = True;
357 asn1_read(data, tok_id, 2);
359 *ticket = data_blob(NULL, data_remaining);
360 asn1_read(data, ticket->data, ticket->length);
365 ret = !data->has_error;
367 if (data->has_error) {
368 data_blob_free(ticket);
378 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
379 kerberos session setup
381 int spnego_gen_negTokenTarg(const char *principal, int time_offset,
383 DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
387 DATA_BLOB tkt, tkt_wrapped;
388 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
390 /* get a kerberos ticket for the service and extract the session key */
391 retval = cli_krb5_get_ticket(principal, time_offset,
392 &tkt, session_key_krb5, extra_ap_opts, NULL,
398 /* wrap that up in a nice GSS-API wrapping */
399 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
401 /* and wrap that in a shiny SPNEGO wrapper */
402 *targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
404 data_blob_free(&tkt_wrapped);
405 data_blob_free(&tkt);
412 parse a spnego NTLMSSP challenge packet giving two security blobs
414 bool spnego_parse_challenge(const DATA_BLOB blob,
415 DATA_BLOB *chal1, DATA_BLOB *chal2)
423 data = asn1_init(talloc_tos());
428 asn1_load(data, blob);
429 asn1_start_tag(data,ASN1_CONTEXT(1));
430 asn1_start_tag(data,ASN1_SEQUENCE(0));
432 asn1_start_tag(data,ASN1_CONTEXT(0));
433 asn1_check_enumerated(data,1);
436 asn1_start_tag(data,ASN1_CONTEXT(1));
437 asn1_check_OID(data, OID_NTLMSSP);
440 asn1_start_tag(data,ASN1_CONTEXT(2));
441 asn1_read_OctetString(data, talloc_autofree_context(), chal1);
444 /* the second challenge is optional (XP doesn't send it) */
445 if (asn1_tag_remaining(data)) {
446 asn1_start_tag(data,ASN1_CONTEXT(3));
447 asn1_read_OctetString(data, talloc_autofree_context(), chal2);
454 ret = !data->has_error;
456 if (data->has_error) {
457 data_blob_free(chal1);
458 data_blob_free(chal2);
467 generate a SPNEGO auth packet. This will contain the encrypted passwords
469 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
474 data = asn1_init(talloc_tos());
476 return data_blob_null;
479 asn1_push_tag(data, ASN1_CONTEXT(1));
480 asn1_push_tag(data, ASN1_SEQUENCE(0));
481 asn1_push_tag(data, ASN1_CONTEXT(2));
482 asn1_write_OctetString(data,blob.data,blob.length);
487 ret = data_blob(data->data, data->length);
495 parse a SPNEGO auth packet. This contains the encrypted passwords
497 bool spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
500 struct spnego_data token;
502 len = spnego_read_data(talloc_tos(), blob, &token);
504 DEBUG(3,("spnego_parse_auth: spnego_read_data failed\n"));
508 if (token.type != SPNEGO_NEG_TOKEN_TARG) {
509 DEBUG(3,("spnego_parse_auth: wrong token type: %d\n",
511 spnego_free_data(&token);
515 *auth = data_blob_talloc(talloc_tos(),
516 token.negTokenTarg.responseToken.data,
517 token.negTokenTarg.responseToken.length);
518 spnego_free_data(&token);
524 generate a minimal SPNEGO response packet. Doesn't contain much.
526 DATA_BLOB spnego_gen_auth_response(DATA_BLOB *reply, NTSTATUS nt_status,
533 if (NT_STATUS_IS_OK(nt_status)) {
534 negResult = SPNEGO_ACCEPT_COMPLETED;
535 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
536 negResult = SPNEGO_ACCEPT_INCOMPLETE;
538 negResult = SPNEGO_REJECT;
541 data = asn1_init(talloc_tos());
543 return data_blob_null;
546 asn1_push_tag(data, ASN1_CONTEXT(1));
547 asn1_push_tag(data, ASN1_SEQUENCE(0));
548 asn1_push_tag(data, ASN1_CONTEXT(0));
549 asn1_write_enumerated(data, negResult);
553 asn1_push_tag(data,ASN1_CONTEXT(1));
554 asn1_write_OID(data, mechOID);
558 if (reply && reply->data != NULL) {
559 asn1_push_tag(data,ASN1_CONTEXT(2));
560 asn1_write_OctetString(data, reply->data, reply->length);
567 ret = data_blob(data->data, data->length);
573 parse a SPNEGO auth packet. This contains the encrypted passwords
575 bool spnego_parse_auth_response(DATA_BLOB blob, NTSTATUS nt_status,
582 if (NT_STATUS_IS_OK(nt_status)) {
583 negResult = SPNEGO_ACCEPT_COMPLETED;
584 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
585 negResult = SPNEGO_ACCEPT_INCOMPLETE;
587 negResult = SPNEGO_REJECT;
590 data = asn1_init(talloc_tos());
595 asn1_load(data, blob);
596 asn1_start_tag(data, ASN1_CONTEXT(1));
597 asn1_start_tag(data, ASN1_SEQUENCE(0));
598 asn1_start_tag(data, ASN1_CONTEXT(0));
599 asn1_check_enumerated(data, negResult);
602 *auth = data_blob_null;
604 if (asn1_tag_remaining(data)) {
605 asn1_start_tag(data,ASN1_CONTEXT(1));
606 asn1_check_OID(data, mechOID);
609 if (asn1_tag_remaining(data)) {
610 asn1_start_tag(data,ASN1_CONTEXT(2));
611 asn1_read_OctetString(data, talloc_autofree_context(), auth);
614 } else if (negResult == SPNEGO_ACCEPT_INCOMPLETE) {
618 /* Binding against Win2K DC returns a duplicate of the responseToken in
619 * the optional mechListMIC field. This is a bug in Win2K. We ignore
620 * this field if it exists. Win2K8 may return a proper mechListMIC at
621 * which point we need to implement the integrity checking. */
622 if (asn1_tag_remaining(data)) {
623 DATA_BLOB mechList = data_blob_null;
624 asn1_start_tag(data, ASN1_CONTEXT(3));
625 asn1_read_OctetString(data, talloc_autofree_context(), &mechList);
627 data_blob_free(&mechList);
628 DEBUG(5,("spnego_parse_auth_response received mechListMIC, "
635 if (data->has_error) {
636 DEBUG(3,("spnego_parse_auth_response failed at %d\n", (int)data->ofs));
638 data_blob_free(auth);