2 Unix SMB/Netbios implementation.
4 simple kerberos5/SPNEGO routines
5 Copyright (C) Andrew Tridgell 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 generate a negTokenInit packet given a GUID, a list of supported
26 OIDs (the mechanisms) and a principle name string
28 DATA_BLOB spnego_gen_negTokenInit(uint8 guid[16],
30 const char *principle)
36 memset(&data, 0, sizeof(data));
38 asn1_write(&data, guid, 16);
39 asn1_push_tag(&data,ASN1_APPLICATION(0));
40 asn1_write_OID(&data,OID_SPNEGO);
41 asn1_push_tag(&data,ASN1_CONTEXT(0));
42 asn1_push_tag(&data,ASN1_SEQUENCE(0));
44 asn1_push_tag(&data,ASN1_CONTEXT(0));
45 asn1_push_tag(&data,ASN1_SEQUENCE(0));
46 for (i=0; OIDs[i]; i++) {
47 asn1_write_OID(&data,OIDs[i]);
52 asn1_push_tag(&data, ASN1_CONTEXT(3));
53 asn1_push_tag(&data, ASN1_SEQUENCE(0));
54 asn1_push_tag(&data, ASN1_CONTEXT(0));
55 asn1_write_GeneralString(&data,principle);
66 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data.ofs));
70 ret = data_blob(data.data, data.length);
78 parse a negTokenInit packet giving a GUID, a list of supported
79 OIDs (the mechanisms) and a principle name string
81 BOOL spnego_parse_negTokenInit(DATA_BLOB blob,
83 char *OIDs[ASN1_MAX_OIDS],
90 asn1_load(&data, blob);
92 asn1_read(&data, guid, 16);
93 asn1_start_tag(&data,ASN1_APPLICATION(0));
94 asn1_check_OID(&data,OID_SPNEGO);
95 asn1_start_tag(&data,ASN1_CONTEXT(0));
96 asn1_start_tag(&data,ASN1_SEQUENCE(0));
98 asn1_start_tag(&data,ASN1_CONTEXT(0));
99 asn1_start_tag(&data,ASN1_SEQUENCE(0));
100 for (i=0; asn1_tag_remaining(&data) > 0 && i < ASN1_MAX_OIDS; i++) {
102 asn1_read_OID(&data,&oid);
109 asn1_start_tag(&data, ASN1_CONTEXT(3));
110 asn1_start_tag(&data, ASN1_SEQUENCE(0));
111 asn1_start_tag(&data, ASN1_CONTEXT(0));
112 asn1_read_GeneralString(&data,principle);
122 ret = !data.has_error;
129 generate a negTokenTarg packet given a list of OIDs and a security blob
131 DATA_BLOB gen_negTokenTarg(const char *OIDs[], DATA_BLOB blob)
137 memset(&data, 0, sizeof(data));
139 asn1_push_tag(&data, ASN1_APPLICATION(0));
140 asn1_write_OID(&data,OID_SPNEGO);
141 asn1_push_tag(&data, ASN1_CONTEXT(0));
142 asn1_push_tag(&data, ASN1_SEQUENCE(0));
144 asn1_push_tag(&data, ASN1_CONTEXT(0));
145 asn1_push_tag(&data, ASN1_SEQUENCE(0));
146 for (i=0; OIDs[i]; i++) {
147 asn1_write_OID(&data,OIDs[i]);
152 asn1_push_tag(&data, ASN1_CONTEXT(2));
153 asn1_write_OctetString(&data,blob.data,blob.length);
161 if (data.has_error) {
162 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data.ofs));
166 ret = data_blob(data.data, data.length);
174 parse a negTokenTarg packet giving a list of OIDs and a security blob
176 BOOL parse_negTokenTarg(DATA_BLOB blob, char *OIDs[ASN1_MAX_OIDS], DATA_BLOB *secblob)
181 asn1_load(&data, blob);
182 asn1_start_tag(&data, ASN1_APPLICATION(0));
183 asn1_check_OID(&data,OID_SPNEGO);
184 asn1_start_tag(&data, ASN1_CONTEXT(0));
185 asn1_start_tag(&data, ASN1_SEQUENCE(0));
187 asn1_start_tag(&data, ASN1_CONTEXT(0));
188 asn1_start_tag(&data, ASN1_SEQUENCE(0));
189 for (i=0; asn1_tag_remaining(&data) > 0 && i < ASN1_MAX_OIDS; i++) {
191 asn1_read_OID(&data,&oid);
198 asn1_start_tag(&data, ASN1_CONTEXT(2));
199 asn1_read_OctetString(&data,secblob);
207 if (data.has_error) {
208 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data.ofs));
218 generate a krb5 GSS-API wrapper packet given a ticket
220 static DATA_BLOB spnego_gen_krb5_wrap(DATA_BLOB ticket)
225 memset(&data, 0, sizeof(data));
227 asn1_push_tag(&data, ASN1_APPLICATION(0));
228 asn1_write_OID(&data, OID_KERBEROS5);
229 asn1_write_BOOLEAN(&data, 0);
230 asn1_write(&data, ticket.data, ticket.length);
233 if (data.has_error) {
234 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data.ofs));
238 ret = data_blob(data.data, data.length);
246 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
247 kerberos session setup
249 DATA_BLOB spnego_gen_negTokenTarg(struct cli_state *cli, char *principle)
254 DATA_BLOB tkt, tkt_wrapped, targ;
255 const char *krb_mechs[] = {OID_KERBEROS5_OLD, OID_NTLMSSP, NULL};
257 fstrcpy(service, principle);
258 p = strchr_m(service, '@');
260 DEBUG(1,("Malformed principle [%s] in spnego_gen_negTokenTarg\n",
262 return data_blob(NULL, 0);
267 /* get a kerberos ticket for the service */
268 tkt = krb5_get_ticket(service, realm);
270 /* wrap that up in a nice GSS-API wrapping */
271 tkt_wrapped = spnego_gen_krb5_wrap(tkt);
273 /* and wrap that in a shiny SPNEGO wrapper */
274 targ = gen_negTokenTarg(krb_mechs, tkt_wrapped);
276 data_blob_free(&tkt_wrapped);
277 data_blob_free(&tkt);
284 parse a spnego NTLMSSP challenge packet giving two security blobs
286 BOOL spnego_parse_challenge(DATA_BLOB blob,
287 DATA_BLOB *chal1, DATA_BLOB *chal2)
295 asn1_load(&data, blob);
296 asn1_start_tag(&data,ASN1_CONTEXT(1));
297 asn1_start_tag(&data,ASN1_SEQUENCE(0));
299 asn1_start_tag(&data,ASN1_CONTEXT(0));
300 asn1_check_enumerated(&data,1);
303 asn1_start_tag(&data,ASN1_CONTEXT(1));
304 asn1_check_OID(&data, OID_NTLMSSP);
307 asn1_start_tag(&data,ASN1_CONTEXT(2));
308 asn1_read_OctetString(&data, chal1);
311 /* the second challenge is optional (XP doesn't send it) */
312 if (asn1_tag_remaining(&data)) {
313 asn1_start_tag(&data,ASN1_CONTEXT(3));
314 asn1_read_OctetString(&data, chal2);
321 ret = !data.has_error;
328 generate a spnego NTLMSSP challenge packet given two security blobs
329 The second challenge is optional
331 BOOL spnego_gen_challenge(DATA_BLOB *blob,
332 DATA_BLOB *chal1, DATA_BLOB *chal2)
338 asn1_push_tag(&data,ASN1_CONTEXT(1));
339 asn1_push_tag(&data,ASN1_SEQUENCE(0));
341 asn1_push_tag(&data,ASN1_CONTEXT(0));
342 asn1_write_enumerated(&data,1);
345 asn1_push_tag(&data,ASN1_CONTEXT(1));
346 asn1_write_OID(&data, OID_NTLMSSP);
349 asn1_push_tag(&data,ASN1_CONTEXT(2));
350 asn1_write_OctetString(&data, chal1->data, chal1->length);
353 /* the second challenge is optional (XP doesn't send it) */
355 asn1_push_tag(&data,ASN1_CONTEXT(3));
356 asn1_write_OctetString(&data, chal2->data, chal2->length);
363 if (data.has_error) {
367 *blob = data_blob(data.data, data.length);
373 generate a SPNEGO NTLMSSP auth packet. This will contain the encrypted passwords
375 DATA_BLOB spnego_gen_auth(DATA_BLOB blob)
380 memset(&data, 0, sizeof(data));
382 asn1_push_tag(&data, ASN1_CONTEXT(1));
383 asn1_push_tag(&data, ASN1_SEQUENCE(0));
384 asn1_push_tag(&data, ASN1_CONTEXT(2));
385 asn1_write_OctetString(&data,blob.data,blob.length);
390 ret = data_blob(data.data, data.length);
398 parse a SPNEGO NTLMSSP auth packet. This contains the encrypted passwords
400 BOOL spnego_parse_auth(DATA_BLOB blob, DATA_BLOB *auth)
404 asn1_load(&data, blob);
405 asn1_start_tag(&data, ASN1_CONTEXT(1));
406 asn1_start_tag(&data, ASN1_SEQUENCE(0));
407 asn1_start_tag(&data, ASN1_CONTEXT(2));
408 asn1_read_OctetString(&data,auth);
413 if (data.has_error) {
414 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data.ofs));
425 this is a tiny msrpc packet generator. I am only using this to
426 avoid tying this code to a particular varient of our rpc code. This
427 generator is not general enough for all our rpc needs, its just
428 enough for the spnego/ntlmssp code
430 format specifiers are:
432 U = unicode string (input is unix string)
433 B = data blob (pointer + length)
434 b = data blob in header (pointer + length)
436 C = constant ascii string
438 BOOL msrpc_gen(DATA_BLOB *blob,
439 const char *format, ...)
445 int head_size=0, data_size=0;
446 int head_ofs, data_ofs;
448 /* first scan the format to work out the header and body size */
449 va_start(ap, format);
450 for (i=0; format[i]; i++) {
453 s = va_arg(ap, char *);
455 data_size += str_charnum(s) * 2;
458 b = va_arg(ap, uint8 *);
460 data_size += va_arg(ap, int);
463 b = va_arg(ap, uint8 *);
464 head_size += va_arg(ap, int);
471 s = va_arg(ap, char *);
472 head_size += str_charnum(s) + 1;
478 /* allocate the space, then scan the format again to fill in the values */
479 blob->data = malloc(head_size + data_size);
480 blob->length = head_size + data_size;
481 if (!blob->data) return False;
484 data_ofs = head_size;
486 va_start(ap, format);
487 for (i=0; format[i]; i++) {
490 s = va_arg(ap, char *);
492 SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
493 SSVAL(blob->data, head_ofs, n*2); head_ofs += 2;
494 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
495 push_string(NULL, blob->data+data_ofs, s, n*2, STR_UNICODE|STR_NOALIGN);
499 b = va_arg(ap, uint8 *);
501 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
502 SSVAL(blob->data, head_ofs, n); head_ofs += 2;
503 SIVAL(blob->data, head_ofs, data_ofs); head_ofs += 4;
504 memcpy(blob->data+data_ofs, b, n);
509 SIVAL(blob->data, head_ofs, n); head_ofs += 4;
512 b = va_arg(ap, uint8 *);
514 memcpy(blob->data + head_ofs, b, n);
518 s = va_arg(ap, char *);
519 head_ofs += push_string(NULL, blob->data+head_ofs, s, -1,
520 STR_ASCII|STR_TERMINATE);
531 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
533 format specifiers are:
535 U = unicode string (input is unix string)
537 b = data blob in header
539 C = constant ascii string
541 BOOL msrpc_parse(DATA_BLOB *blob,
542 const char *format, ...)
554 va_start(ap, format);
555 for (i=0; format[i]; i++) {
558 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
559 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
560 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
561 /* make sure its in the right format - be strict */
562 if (len1 != len2 || (len1&1) || ptr + len1 > blob->length) {
565 ps = va_arg(ap, char **);
566 pull_string(NULL, p, blob->data + ptr, -1, len1,
567 STR_UNICODE|STR_NOALIGN);
571 len1 = SVAL(blob->data, head_ofs); head_ofs += 2;
572 len2 = SVAL(blob->data, head_ofs); head_ofs += 2;
573 ptr = IVAL(blob->data, head_ofs); head_ofs += 4;
574 /* make sure its in the right format - be strict */
575 if (len1 != len2 || ptr + len1 > blob->length) {
578 b = (DATA_BLOB *)va_arg(ap, void *);
579 *b = data_blob(blob->data + ptr, len1);
582 b = (DATA_BLOB *)va_arg(ap, void *);
583 len1 = va_arg(ap, unsigned);
584 *b = data_blob(blob->data + head_ofs, len1);
588 v = va_arg(ap, uint32 *);
589 *v = IVAL(blob->data, head_ofs); head_ofs += 4;
592 s = va_arg(ap, char *);
593 head_ofs += pull_string(NULL, p, blob->data+head_ofs, -1,
594 blob->length - head_ofs,
595 STR_ASCII|STR_TERMINATE);
596 if (strcmp(s, p) != 0) {