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 principal name string
28 DATA_BLOB
spnego_gen_negTokenInit(uint8 guid
[16],
30 const char *principal
)
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
,principal
);
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 principal 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
,principal
);
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
);
245 parse a krb5 GSS-API wrapper packet giving a ticket
247 BOOL
spnego_parse_krb5_wrap(DATA_BLOB blob
, DATA_BLOB
*ticket
)
253 asn1_load(&data
, blob
);
254 asn1_start_tag(&data
, ASN1_APPLICATION(0));
255 asn1_check_OID(&data
, OID_KERBEROS5
);
256 asn1_check_BOOLEAN(&data
, 0);
258 data_remaining
= asn1_tag_remaining(&data
);
260 if (data_remaining
< 1) {
261 data
.has_error
= True
;
263 *ticket
= data_blob(data
.data
, data_remaining
);
264 asn1_read(&data
, ticket
->data
, ticket
->length
);
269 ret
= !data
.has_error
;
278 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
279 kerberos session setup
281 DATA_BLOB
spnego_gen_negTokenTarg(struct cli_state
*cli
, char *principal
)
283 DATA_BLOB tkt
, tkt_wrapped
, targ
;
284 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_NTLMSSP
, NULL
};
286 /* get a kerberos ticket for the service */
287 tkt
= krb5_get_ticket(principal
);
289 /* wrap that up in a nice GSS-API wrapping */
290 tkt_wrapped
= spnego_gen_krb5_wrap(tkt
);
292 /* and wrap that in a shiny SPNEGO wrapper */
293 targ
= gen_negTokenTarg(krb_mechs
, tkt_wrapped
);
295 data_blob_free(&tkt_wrapped
);
296 data_blob_free(&tkt
);
303 parse a spnego NTLMSSP challenge packet giving two security blobs
305 BOOL
spnego_parse_challenge(DATA_BLOB blob
,
306 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
314 asn1_load(&data
, blob
);
315 asn1_start_tag(&data
,ASN1_CONTEXT(1));
316 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
318 asn1_start_tag(&data
,ASN1_CONTEXT(0));
319 asn1_check_enumerated(&data
,1);
322 asn1_start_tag(&data
,ASN1_CONTEXT(1));
323 asn1_check_OID(&data
, OID_NTLMSSP
);
326 asn1_start_tag(&data
,ASN1_CONTEXT(2));
327 asn1_read_OctetString(&data
, chal1
);
330 /* the second challenge is optional (XP doesn't send it) */
331 if (asn1_tag_remaining(&data
)) {
332 asn1_start_tag(&data
,ASN1_CONTEXT(3));
333 asn1_read_OctetString(&data
, chal2
);
340 ret
= !data
.has_error
;
347 generate a spnego NTLMSSP challenge packet given two security blobs
348 The second challenge is optional
350 BOOL
spnego_gen_challenge(DATA_BLOB
*blob
,
351 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
357 asn1_push_tag(&data
,ASN1_CONTEXT(1));
358 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
360 asn1_push_tag(&data
,ASN1_CONTEXT(0));
361 asn1_write_enumerated(&data
,1);
364 asn1_push_tag(&data
,ASN1_CONTEXT(1));
365 asn1_write_OID(&data
, OID_NTLMSSP
);
368 asn1_push_tag(&data
,ASN1_CONTEXT(2));
369 asn1_write_OctetString(&data
, chal1
->data
, chal1
->length
);
372 /* the second challenge is optional (XP doesn't send it) */
374 asn1_push_tag(&data
,ASN1_CONTEXT(3));
375 asn1_write_OctetString(&data
, chal2
->data
, chal2
->length
);
382 if (data
.has_error
) {
386 *blob
= data_blob(data
.data
, data
.length
);
392 generate a SPNEGO NTLMSSP auth packet. This will contain the encrypted passwords
394 DATA_BLOB
spnego_gen_auth(DATA_BLOB blob
)
399 memset(&data
, 0, sizeof(data
));
401 asn1_push_tag(&data
, ASN1_CONTEXT(1));
402 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
403 asn1_push_tag(&data
, ASN1_CONTEXT(2));
404 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
409 ret
= data_blob(data
.data
, data
.length
);
417 parse a SPNEGO NTLMSSP auth packet. This contains the encrypted passwords
419 BOOL
spnego_parse_auth(DATA_BLOB blob
, DATA_BLOB
*auth
)
423 asn1_load(&data
, blob
);
424 asn1_start_tag(&data
, ASN1_CONTEXT(1));
425 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
426 asn1_start_tag(&data
, ASN1_CONTEXT(2));
427 asn1_read_OctetString(&data
,auth
);
432 if (data
.has_error
) {
433 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data
.ofs
));
444 this is a tiny msrpc packet generator. I am only using this to
445 avoid tying this code to a particular varient of our rpc code. This
446 generator is not general enough for all our rpc needs, its just
447 enough for the spnego/ntlmssp code
449 format specifiers are:
451 U = unicode string (input is unix string)
452 B = data blob (pointer + length)
453 b = data blob in header (pointer + length)
455 C = constant ascii string
457 BOOL
msrpc_gen(DATA_BLOB
*blob
,
458 const char *format
, ...)
464 int head_size
=0, data_size
=0;
465 int head_ofs
, data_ofs
;
467 /* first scan the format to work out the header and body size */
468 va_start(ap
, format
);
469 for (i
=0; format
[i
]; i
++) {
472 s
= va_arg(ap
, char *);
474 data_size
+= str_charnum(s
) * 2;
477 b
= va_arg(ap
, uint8
*);
479 data_size
+= va_arg(ap
, int);
482 b
= va_arg(ap
, uint8
*);
483 head_size
+= va_arg(ap
, int);
490 s
= va_arg(ap
, char *);
491 head_size
+= str_charnum(s
) + 1;
497 /* allocate the space, then scan the format again to fill in the values */
498 blob
->data
= malloc(head_size
+ data_size
);
499 blob
->length
= head_size
+ data_size
;
500 if (!blob
->data
) return False
;
503 data_ofs
= head_size
;
505 va_start(ap
, format
);
506 for (i
=0; format
[i
]; i
++) {
509 s
= va_arg(ap
, char *);
511 SSVAL(blob
->data
, head_ofs
, n
*2); head_ofs
+= 2;
512 SSVAL(blob
->data
, head_ofs
, n
*2); head_ofs
+= 2;
513 SIVAL(blob
->data
, head_ofs
, data_ofs
); head_ofs
+= 4;
514 push_string(NULL
, blob
->data
+data_ofs
, s
, n
*2, STR_UNICODE
|STR_NOALIGN
);
518 b
= va_arg(ap
, uint8
*);
520 SSVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 2;
521 SSVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 2;
522 SIVAL(blob
->data
, head_ofs
, data_ofs
); head_ofs
+= 4;
523 memcpy(blob
->data
+data_ofs
, b
, n
);
528 SIVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 4;
531 b
= va_arg(ap
, uint8
*);
533 memcpy(blob
->data
+ head_ofs
, b
, n
);
537 s
= va_arg(ap
, char *);
538 head_ofs
+= push_string(NULL
, blob
->data
+head_ofs
, s
, -1,
539 STR_ASCII
|STR_TERMINATE
);
550 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
552 format specifiers are:
554 U = unicode string (input is unix string)
556 b = data blob in header
558 C = constant ascii string
560 BOOL
msrpc_parse(DATA_BLOB
*blob
,
561 const char *format
, ...)
573 va_start(ap
, format
);
574 for (i
=0; format
[i
]; i
++) {
577 len1
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
578 len2
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
579 ptr
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
580 /* make sure its in the right format - be strict */
581 if (len1
!= len2
|| (len1
&1) || ptr
+ len1
> blob
->length
) {
584 ps
= va_arg(ap
, char **);
585 pull_string(NULL
, p
, blob
->data
+ ptr
, -1, len1
,
586 STR_UNICODE
|STR_NOALIGN
);
590 len1
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
591 len2
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
592 ptr
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
593 /* make sure its in the right format - be strict */
594 if (len1
!= len2
|| ptr
+ len1
> blob
->length
) {
597 b
= (DATA_BLOB
*)va_arg(ap
, void *);
598 *b
= data_blob(blob
->data
+ ptr
, len1
);
601 b
= (DATA_BLOB
*)va_arg(ap
, void *);
602 len1
= va_arg(ap
, unsigned);
603 *b
= data_blob(blob
->data
+ head_ofs
, len1
);
607 v
= va_arg(ap
, uint32
*);
608 *v
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
611 s
= va_arg(ap
, char *);
612 head_ofs
+= pull_string(NULL
, p
, blob
->data
+head_ofs
, -1,
613 blob
->length
- head_ofs
,
614 STR_ASCII
|STR_TERMINATE
);
615 if (strcmp(s
, p
) != 0) {