2 Unix SMB/CIFS implementation.
3 simple kerberos5/SPNEGO routines
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jim McDonough 2002
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
);
77 Generate a negTokenInit as used by the client side ... It has a mechType
78 (OID), and a mechToken (a security blob) ...
80 Really, we need to break out the NTLMSSP stuff as well, because it could be
83 DATA_BLOB
gen_negTokenInit(const char *OID
, DATA_BLOB blob
)
88 memset(&data
, 0, sizeof(data
));
90 asn1_push_tag(&data
, ASN1_APPLICATION(0));
91 asn1_write_OID(&data
,OID_SPNEGO
);
92 asn1_push_tag(&data
, ASN1_CONTEXT(0));
93 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
95 asn1_push_tag(&data
, ASN1_CONTEXT(0));
96 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
97 asn1_write_OID(&data
, OID
);
101 asn1_push_tag(&data
, ASN1_CONTEXT(2));
102 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
110 if (data
.has_error
) {
111 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data
.ofs
));
115 ret
= data_blob(data
.data
, data
.length
);
122 parse a negTokenInit packet giving a GUID, a list of supported
123 OIDs (the mechanisms) and a principal name string
125 BOOL
spnego_parse_negTokenInit(DATA_BLOB blob
,
126 char *OIDs
[ASN1_MAX_OIDS
],
133 asn1_load(&data
, blob
);
135 asn1_start_tag(&data
,ASN1_APPLICATION(0));
136 asn1_check_OID(&data
,OID_SPNEGO
);
137 asn1_start_tag(&data
,ASN1_CONTEXT(0));
138 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
140 asn1_start_tag(&data
,ASN1_CONTEXT(0));
141 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
142 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
; i
++) {
144 asn1_read_OID(&data
,&oid
);
151 asn1_start_tag(&data
, ASN1_CONTEXT(3));
152 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
153 asn1_start_tag(&data
, ASN1_CONTEXT(0));
154 asn1_read_GeneralString(&data
,principal
);
164 ret
= !data
.has_error
;
171 generate a negTokenTarg packet given a list of OIDs and a security blob
173 DATA_BLOB
gen_negTokenTarg(const char *OIDs
[], DATA_BLOB blob
)
179 memset(&data
, 0, sizeof(data
));
181 asn1_push_tag(&data
, ASN1_APPLICATION(0));
182 asn1_write_OID(&data
,OID_SPNEGO
);
183 asn1_push_tag(&data
, ASN1_CONTEXT(0));
184 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
186 asn1_push_tag(&data
, ASN1_CONTEXT(0));
187 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
188 for (i
=0; OIDs
[i
]; i
++) {
189 asn1_write_OID(&data
,OIDs
[i
]);
194 asn1_push_tag(&data
, ASN1_CONTEXT(2));
195 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
203 if (data
.has_error
) {
204 DEBUG(1,("Failed to build negTokenTarg at offset %d\n", (int)data
.ofs
));
208 ret
= data_blob(data
.data
, data
.length
);
216 parse a negTokenTarg packet giving a list of OIDs and a security blob
218 BOOL
parse_negTokenTarg(DATA_BLOB blob
, char *OIDs
[ASN1_MAX_OIDS
], DATA_BLOB
*secblob
)
223 asn1_load(&data
, blob
);
224 asn1_start_tag(&data
, ASN1_APPLICATION(0));
225 asn1_check_OID(&data
,OID_SPNEGO
);
226 asn1_start_tag(&data
, ASN1_CONTEXT(0));
227 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
229 asn1_start_tag(&data
, ASN1_CONTEXT(0));
230 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
231 for (i
=0; asn1_tag_remaining(&data
) > 0 && i
< ASN1_MAX_OIDS
; i
++) {
233 asn1_read_OID(&data
,&oid
);
240 asn1_start_tag(&data
, ASN1_CONTEXT(2));
241 asn1_read_OctetString(&data
,secblob
);
249 if (data
.has_error
) {
250 DEBUG(1,("Failed to parse negTokenTarg at offset %d\n", (int)data
.ofs
));
260 generate a krb5 GSS-API wrapper packet given a ticket
262 DATA_BLOB
spnego_gen_krb5_wrap(DATA_BLOB ticket
)
267 memset(&data
, 0, sizeof(data
));
269 asn1_push_tag(&data
, ASN1_APPLICATION(0));
270 asn1_write_OID(&data
, OID_KERBEROS5
);
271 asn1_write_BOOLEAN(&data
, 0);
272 asn1_write(&data
, ticket
.data
, ticket
.length
);
275 if (data
.has_error
) {
276 DEBUG(1,("Failed to build krb5 wrapper at offset %d\n", (int)data
.ofs
));
280 ret
= data_blob(data
.data
, data
.length
);
287 parse a krb5 GSS-API wrapper packet giving a ticket
289 BOOL
spnego_parse_krb5_wrap(DATA_BLOB blob
, DATA_BLOB
*ticket
)
295 asn1_load(&data
, blob
);
296 asn1_start_tag(&data
, ASN1_APPLICATION(0));
297 asn1_check_OID(&data
, OID_KERBEROS5
);
298 asn1_check_BOOLEAN(&data
, 0);
300 data_remaining
= asn1_tag_remaining(&data
);
302 if (data_remaining
< 1) {
303 data
.has_error
= True
;
306 *ticket
= data_blob(data
.data
, data_remaining
);
307 asn1_read(&data
, ticket
->data
, ticket
->length
);
312 ret
= !data
.has_error
;
321 generate a SPNEGO negTokenTarg packet, ready for a EXTENDED_SECURITY
322 kerberos session setup
324 DATA_BLOB
spnego_gen_negTokenTarg(const char *principal
, int time_offset
)
326 DATA_BLOB tkt
, tkt_wrapped
, targ
;
327 const char *krb_mechs
[] = {OID_KERBEROS5_OLD
, OID_NTLMSSP
, NULL
};
329 /* get a kerberos ticket for the service */
330 tkt
= krb5_get_ticket(principal
, time_offset
);
332 /* wrap that up in a nice GSS-API wrapping */
333 tkt_wrapped
= spnego_gen_krb5_wrap(tkt
);
335 /* and wrap that in a shiny SPNEGO wrapper */
336 targ
= gen_negTokenTarg(krb_mechs
, tkt_wrapped
);
338 data_blob_free(&tkt_wrapped
);
339 data_blob_free(&tkt
);
346 parse a spnego NTLMSSP challenge packet giving two security blobs
348 BOOL
spnego_parse_challenge(DATA_BLOB blob
,
349 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
357 asn1_load(&data
, blob
);
358 asn1_start_tag(&data
,ASN1_CONTEXT(1));
359 asn1_start_tag(&data
,ASN1_SEQUENCE(0));
361 asn1_start_tag(&data
,ASN1_CONTEXT(0));
362 asn1_check_enumerated(&data
,1);
365 asn1_start_tag(&data
,ASN1_CONTEXT(1));
366 asn1_check_OID(&data
, OID_NTLMSSP
);
369 asn1_start_tag(&data
,ASN1_CONTEXT(2));
370 asn1_read_OctetString(&data
, chal1
);
373 /* the second challenge is optional (XP doesn't send it) */
374 if (asn1_tag_remaining(&data
)) {
375 asn1_start_tag(&data
,ASN1_CONTEXT(3));
376 asn1_read_OctetString(&data
, chal2
);
383 ret
= !data
.has_error
;
390 generate a spnego NTLMSSP challenge packet given two security blobs
391 The second challenge is optional
393 BOOL
spnego_gen_challenge(DATA_BLOB
*blob
,
394 DATA_BLOB
*chal1
, DATA_BLOB
*chal2
)
400 asn1_push_tag(&data
,ASN1_CONTEXT(1));
401 asn1_push_tag(&data
,ASN1_SEQUENCE(0));
403 asn1_push_tag(&data
,ASN1_CONTEXT(0));
404 asn1_write_enumerated(&data
,1);
407 asn1_push_tag(&data
,ASN1_CONTEXT(1));
408 asn1_write_OID(&data
, OID_NTLMSSP
);
411 asn1_push_tag(&data
,ASN1_CONTEXT(2));
412 asn1_write_OctetString(&data
, chal1
->data
, chal1
->length
);
415 /* the second challenge is optional (XP doesn't send it) */
417 asn1_push_tag(&data
,ASN1_CONTEXT(3));
418 asn1_write_OctetString(&data
, chal2
->data
, chal2
->length
);
425 if (data
.has_error
) {
429 *blob
= data_blob(data
.data
, data
.length
);
435 generate a SPNEGO NTLMSSP auth packet. This will contain the encrypted passwords
437 DATA_BLOB
spnego_gen_auth(DATA_BLOB blob
)
442 memset(&data
, 0, sizeof(data
));
444 asn1_push_tag(&data
, ASN1_CONTEXT(1));
445 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
446 asn1_push_tag(&data
, ASN1_CONTEXT(2));
447 asn1_write_OctetString(&data
,blob
.data
,blob
.length
);
452 ret
= data_blob(data
.data
, data
.length
);
460 parse a SPNEGO NTLMSSP auth packet. This contains the encrypted passwords
462 BOOL
spnego_parse_auth(DATA_BLOB blob
, DATA_BLOB
*auth
)
466 asn1_load(&data
, blob
);
467 asn1_start_tag(&data
, ASN1_CONTEXT(1));
468 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
469 asn1_start_tag(&data
, ASN1_CONTEXT(2));
470 asn1_read_OctetString(&data
,auth
);
475 if (data
.has_error
) {
476 DEBUG(3,("spnego_parse_auth failed at %d\n", (int)data
.ofs
));
486 generate a minimal SPNEGO NTLMSSP response packet. Doesn't contain much.
488 DATA_BLOB
spnego_gen_auth_response(void)
493 memset(&data
, 0, sizeof(data
));
495 asn1_push_tag(&data
, ASN1_CONTEXT(1));
496 asn1_push_tag(&data
, ASN1_SEQUENCE(0));
497 asn1_push_tag(&data
, ASN1_CONTEXT(0));
498 asn1_write_enumerated(&data
, 0);
503 ret
= data_blob(data
.data
, data
.length
);
509 this is a tiny msrpc packet generator. I am only using this to
510 avoid tying this code to a particular varient of our rpc code. This
511 generator is not general enough for all our rpc needs, its just
512 enough for the spnego/ntlmssp code
514 format specifiers are:
516 U = unicode string (input is unix string)
517 a = address (1 byte type, 1 byte length, unicode string, all inline)
518 A = ASCII string (pointer + length) Actually same as B
519 B = data blob (pointer + length)
520 b = data blob in header (pointer + length)
523 C = constant ascii string
525 BOOL
msrpc_gen(DATA_BLOB
*blob
,
526 const char *format
, ...)
532 int head_size
=0, data_size
=0;
533 int head_ofs
, data_ofs
;
535 /* first scan the format to work out the header and body size */
536 va_start(ap
, format
);
537 for (i
=0; format
[i
]; i
++) {
540 s
= va_arg(ap
, char *);
542 data_size
+= str_charnum(s
) * 2;
546 s
= va_arg(ap
, char *);
547 data_size
+= (str_charnum(s
) * 2) + 4;
551 b
= va_arg(ap
, uint8
*);
553 data_size
+= va_arg(ap
, int);
556 b
= va_arg(ap
, uint8
*);
557 head_size
+= va_arg(ap
, int);
564 s
= va_arg(ap
, char *);
565 head_size
+= str_charnum(s
) + 1;
571 /* allocate the space, then scan the format again to fill in the values */
572 *blob
= data_blob(NULL
, head_size
+ data_size
);
575 data_ofs
= head_size
;
577 va_start(ap
, format
);
578 for (i
=0; format
[i
]; i
++) {
581 s
= va_arg(ap
, char *);
583 SSVAL(blob
->data
, head_ofs
, n
*2); head_ofs
+= 2;
584 SSVAL(blob
->data
, head_ofs
, n
*2); head_ofs
+= 2;
585 SIVAL(blob
->data
, head_ofs
, data_ofs
); head_ofs
+= 4;
586 push_string(NULL
, blob
->data
+data_ofs
, s
, n
*2, STR_UNICODE
|STR_NOALIGN
);
591 SSVAL(blob
->data
, data_ofs
, n
); data_ofs
+= 2;
592 s
= va_arg(ap
, char *);
594 SSVAL(blob
->data
, data_ofs
, n
*2); data_ofs
+= 2;
596 push_string(NULL
, blob
->data
+data_ofs
, s
, n
*2,
597 STR_UNICODE
|STR_NOALIGN
);
604 b
= va_arg(ap
, uint8
*);
606 SSVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 2;
607 SSVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 2;
608 SIVAL(blob
->data
, head_ofs
, data_ofs
); head_ofs
+= 4;
609 memcpy(blob
->data
+data_ofs
, b
, n
);
614 SIVAL(blob
->data
, head_ofs
, n
); head_ofs
+= 4;
617 b
= va_arg(ap
, uint8
*);
619 memcpy(blob
->data
+ head_ofs
, b
, n
);
623 s
= va_arg(ap
, char *);
624 head_ofs
+= push_string(NULL
, blob
->data
+head_ofs
, s
, -1,
625 STR_ASCII
|STR_TERMINATE
);
636 this is a tiny msrpc packet parser. This the the partner of msrpc_gen
638 format specifiers are:
640 U = unicode string (output is unix string)
643 b = data blob in header
645 C = constant ascii string
647 BOOL
msrpc_parse(DATA_BLOB
*blob
,
648 const char *format
, ...)
660 va_start(ap
, format
);
661 for (i
=0; format
[i
]; i
++) {
664 len1
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
665 len2
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
666 ptr
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
667 /* make sure its in the right format - be strict */
668 if (len1
!= len2
|| (len1
&1) || ptr
+ len1
> blob
->length
) {
671 ps
= va_arg(ap
, char **);
672 pull_string(NULL
, p
, blob
->data
+ ptr
, -1, len1
,
673 STR_UNICODE
|STR_NOALIGN
);
677 len1
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
678 len2
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
679 ptr
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
681 /* make sure its in the right format - be strict */
682 if (len1
!= len2
|| ptr
+ len1
> blob
->length
) {
685 ps
= va_arg(ap
, char **);
687 pull_string(NULL
, p
, blob
->data
+ ptr
, -1,
688 len1
, STR_ASCII
|STR_NOALIGN
);
695 len1
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
696 len2
= SVAL(blob
->data
, head_ofs
); head_ofs
+= 2;
697 ptr
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
698 /* make sure its in the right format - be strict */
699 if (len1
!= len2
|| ptr
+ len1
> blob
->length
) {
702 b
= (DATA_BLOB
*)va_arg(ap
, void *);
703 *b
= data_blob(blob
->data
+ ptr
, len1
);
706 b
= (DATA_BLOB
*)va_arg(ap
, void *);
707 len1
= va_arg(ap
, unsigned);
708 *b
= data_blob(blob
->data
+ head_ofs
, len1
);
712 v
= va_arg(ap
, uint32
*);
713 *v
= IVAL(blob
->data
, head_ofs
); head_ofs
+= 4;
716 s
= va_arg(ap
, char *);
717 head_ofs
+= pull_string(NULL
, p
, blob
->data
+head_ofs
, -1,
718 blob
->length
- head_ofs
,
719 STR_ASCII
|STR_TERMINATE
);
720 if (strcmp(s
, p
) != 0) {
732 * Print out the NTLMSSP flags for debugging
735 void debug_ntlmssp_flags(uint32 neg_flags
)
737 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags
));
739 if (neg_flags
& NTLMSSP_NEGOTIATE_UNICODE
)
740 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
741 if (neg_flags
& NTLMSSP_NEGOTIATE_OEM
)
742 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
743 if (neg_flags
& NTLMSSP_REQUEST_TARGET
)
744 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
745 if (neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)
746 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
747 if (neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)
748 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
749 if (neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
750 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
751 if (neg_flags
& NTLMSSP_NEGOTIATE_NETWARE
)
752 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
753 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM
)
754 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
755 if (neg_flags
& NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED
)
756 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
757 if (neg_flags
& NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED
)
758 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
759 if (neg_flags
& NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL
)
760 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
761 if (neg_flags
& NTLMSSP_NEGOTIATE_ALWAYS_SIGN
)
762 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
763 if (neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
)
764 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
765 if (neg_flags
& NTLMSSP_CHAL_TARGET_INFO
)
766 DEBUGADD(4, (" NTLMSSP_CHAL_TARGET_INFO\n"));
767 if (neg_flags
& NTLMSSP_NEGOTIATE_128
)
768 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
769 if (neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
)
770 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));