2 * The ASB.1/BER parsing code is derived from ip_nat_snmp_basic.c which was in
3 * turn derived from the gxsnmp package by Gregory McLean & Jochen Friedrich
5 * Copyright (c) 2000 RP Internet (www.rpi.net.au).
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 #include <linux/slab.h>
27 #include "cifs_debug.h"
28 #include "cifsproto.h"
30 /*****************************************************************************
32 * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
34 *****************************************************************************/
37 #define ASN1_UNI 0 /* Universal */
38 #define ASN1_APL 1 /* Application */
39 #define ASN1_CTX 2 /* Context */
40 #define ASN1_PRV 3 /* Private */
43 #define ASN1_EOC 0 /* End Of Contents or N/A */
44 #define ASN1_BOL 1 /* Boolean */
45 #define ASN1_INT 2 /* Integer */
46 #define ASN1_BTS 3 /* Bit String */
47 #define ASN1_OTS 4 /* Octet String */
48 #define ASN1_NUL 5 /* Null */
49 #define ASN1_OJI 6 /* Object Identifier */
50 #define ASN1_OJD 7 /* Object Description */
51 #define ASN1_EXT 8 /* External */
52 #define ASN1_ENUM 10 /* Enumerated */
53 #define ASN1_SEQ 16 /* Sequence */
54 #define ASN1_SET 17 /* Set */
55 #define ASN1_NUMSTR 18 /* Numerical String */
56 #define ASN1_PRNSTR 19 /* Printable String */
57 #define ASN1_TEXSTR 20 /* Teletext String */
58 #define ASN1_VIDSTR 21 /* Video String */
59 #define ASN1_IA5STR 22 /* IA5 String */
60 #define ASN1_UNITIM 23 /* Universal Time */
61 #define ASN1_GENTIM 24 /* General Time */
62 #define ASN1_GRASTR 25 /* Graphical String */
63 #define ASN1_VISSTR 26 /* Visible String */
64 #define ASN1_GENSTR 27 /* General String */
66 /* Primitive / Constructed methods*/
67 #define ASN1_PRI 0 /* Primitive */
68 #define ASN1_CON 1 /* Constructed */
73 #define ASN1_ERR_NOERROR 0
74 #define ASN1_ERR_DEC_EMPTY 2
75 #define ASN1_ERR_DEC_EOC_MISMATCH 3
76 #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
77 #define ASN1_ERR_DEC_BADVALUE 5
79 #define SPNEGO_OID_LEN 7
80 #define NTLMSSP_OID_LEN 10
81 #define KRB5_OID_LEN 7
82 #define KRB5U2U_OID_LEN 8
83 #define MSKRB5_OID_LEN 7
84 static unsigned long SPNEGO_OID
[7] = { 1, 3, 6, 1, 5, 5, 2 };
85 static unsigned long NTLMSSP_OID
[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
86 static unsigned long KRB5_OID
[7] = { 1, 2, 840, 113554, 1, 2, 2 };
87 static unsigned long KRB5U2U_OID
[8] = { 1, 2, 840, 113554, 1, 2, 2, 3 };
88 static unsigned long MSKRB5_OID
[7] = { 1, 2, 840, 48018, 1, 2, 2 };
94 int error
; /* Error condition */
95 unsigned char *pointer
; /* Octet just to be decoded */
96 unsigned char *begin
; /* First octet */
97 unsigned char *end
; /* Octet after last octet */
101 * Octet string (not null terminated)
109 asn1_open(struct asn1_ctx
*ctx
, unsigned char *buf
, unsigned int len
)
112 ctx
->end
= buf
+ len
;
114 ctx
->error
= ASN1_ERR_NOERROR
;
118 asn1_octet_decode(struct asn1_ctx
*ctx
, unsigned char *ch
)
120 if (ctx
->pointer
>= ctx
->end
) {
121 ctx
->error
= ASN1_ERR_DEC_EMPTY
;
124 *ch
= *(ctx
->pointer
)++;
128 #if 0 /* will be needed later by spnego decoding/encoding of ntlmssp */
130 asn1_enum_decode(struct asn1_ctx
*ctx
, __le32
*val
)
134 if (ctx
->pointer
>= ctx
->end
) {
135 ctx
->error
= ASN1_ERR_DEC_EMPTY
;
139 ch
= *(ctx
->pointer
)++; /* ch has 0xa, ptr points to length octet */
140 if ((ch
) == ASN1_ENUM
) /* if ch value is ENUM, 0xa */
141 *val
= *(++(ctx
->pointer
)); /* value has enum value */
151 asn1_tag_decode(struct asn1_ctx
*ctx
, unsigned int *tag
)
158 if (!asn1_octet_decode(ctx
, &ch
))
162 } while ((ch
& 0x80) == 0x80);
167 asn1_id_decode(struct asn1_ctx
*ctx
,
168 unsigned int *cls
, unsigned int *con
, unsigned int *tag
)
172 if (!asn1_octet_decode(ctx
, &ch
))
175 *cls
= (ch
& 0xC0) >> 6;
176 *con
= (ch
& 0x20) >> 5;
180 if (!asn1_tag_decode(ctx
, tag
))
187 asn1_length_decode(struct asn1_ctx
*ctx
, unsigned int *def
, unsigned int *len
)
189 unsigned char ch
, cnt
;
191 if (!asn1_octet_decode(ctx
, &ch
))
202 cnt
= (unsigned char) (ch
& 0x7F);
206 if (!asn1_octet_decode(ctx
, &ch
))
215 /* don't trust len bigger than ctx buffer */
216 if (*len
> ctx
->end
- ctx
->pointer
)
223 asn1_header_decode(struct asn1_ctx
*ctx
,
225 unsigned int *cls
, unsigned int *con
, unsigned int *tag
)
227 unsigned int def
= 0;
228 unsigned int len
= 0;
230 if (!asn1_id_decode(ctx
, cls
, con
, tag
))
233 if (!asn1_length_decode(ctx
, &def
, &len
))
236 /* primitive shall be definite, indefinite shall be constructed */
237 if (*con
== ASN1_PRI
&& !def
)
241 *eoc
= ctx
->pointer
+ len
;
248 asn1_eoc_decode(struct asn1_ctx
*ctx
, unsigned char *eoc
)
253 if (!asn1_octet_decode(ctx
, &ch
))
257 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
261 if (!asn1_octet_decode(ctx
, &ch
))
265 ctx
->error
= ASN1_ERR_DEC_EOC_MISMATCH
;
270 if (ctx
->pointer
!= eoc
) {
271 ctx
->error
= ASN1_ERR_DEC_LENGTH_MISMATCH
;
278 /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
285 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
286 unsigned char *eoc, long *integer)
291 if (!asn1_octet_decode(ctx, &ch))
294 *integer = (signed char) ch;
297 while (ctx->pointer < eoc) {
298 if (++len > sizeof(long)) {
299 ctx->error = ASN1_ERR_DEC_BADVALUE;
303 if (!asn1_octet_decode(ctx, &ch))
312 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
314 unsigned int *integer)
319 if (!asn1_octet_decode(ctx, &ch))
328 while (ctx->pointer < eoc) {
329 if (++len > sizeof(unsigned int)) {
330 ctx->error = ASN1_ERR_DEC_BADVALUE;
334 if (!asn1_octet_decode(ctx, &ch))
343 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
345 unsigned long *integer)
350 if (!asn1_octet_decode(ctx, &ch))
359 while (ctx->pointer < eoc) {
360 if (++len > sizeof(unsigned long)) {
361 ctx->error = ASN1_ERR_DEC_BADVALUE;
365 if (!asn1_octet_decode(ctx, &ch))
375 asn1_octets_decode(struct asn1_ctx *ctx,
377 unsigned char **octets, unsigned int *len)
383 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
384 if (*octets == NULL) {
389 while (ctx->pointer < eoc) {
390 if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
401 asn1_subid_decode(struct asn1_ctx
*ctx
, unsigned long *subid
)
408 if (!asn1_octet_decode(ctx
, &ch
))
413 } while ((ch
& 0x80) == 0x80);
418 asn1_oid_decode(struct asn1_ctx
*ctx
,
419 unsigned char *eoc
, unsigned long **oid
, unsigned int *len
)
425 size
= eoc
- ctx
->pointer
+ 1;
427 /* first subid actually encodes first two subids */
428 if (size
< 2 || size
> UINT_MAX
/sizeof(unsigned long))
431 *oid
= kmalloc(size
* sizeof(unsigned long), GFP_ATOMIC
);
437 if (!asn1_subid_decode(ctx
, &subid
)) {
446 } else if (subid
< 80) {
448 optr
[1] = subid
- 40;
451 optr
[1] = subid
- 80;
457 while (ctx
->pointer
< eoc
) {
458 if (++(*len
) > size
) {
459 ctx
->error
= ASN1_ERR_DEC_BADVALUE
;
465 if (!asn1_subid_decode(ctx
, optr
++)) {
475 compare_oid(unsigned long *oid1
, unsigned int oid1len
,
476 unsigned long *oid2
, unsigned int oid2len
)
480 if (oid1len
!= oid2len
)
483 for (i
= 0; i
< oid1len
; i
++) {
484 if (oid1
[i
] != oid2
[i
])
491 /* BB check for endian conversion issues here */
494 decode_negTokenInit(unsigned char *security_blob
, int length
,
495 enum securityEnum
*secType
)
499 unsigned char *sequence_end
;
500 unsigned long *oid
= NULL
;
501 unsigned int cls
, con
, tag
, oidlen
, rc
;
502 bool use_ntlmssp
= false;
503 bool use_kerberos
= false;
504 bool use_kerberosu2u
= false;
505 bool use_mskerberos
= false;
507 /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
509 asn1_open(&ctx
, security_blob
, length
);
512 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
513 cFYI(1, ("Error decoding negTokenInit header"));
515 } else if ((cls
!= ASN1_APL
) || (con
!= ASN1_CON
)
516 || (tag
!= ASN1_EOC
)) {
517 cFYI(1, ("cls = %d con = %d tag = %d", cls
, con
, tag
));
521 /* Check for SPNEGO OID -- remember to free obj->oid */
522 rc
= asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
);
524 if ((tag
== ASN1_OJI
) && (con
== ASN1_PRI
) &&
526 rc
= asn1_oid_decode(&ctx
, end
, &oid
, &oidlen
);
528 rc
= compare_oid(oid
, oidlen
, SPNEGO_OID
,
536 /* SPNEGO OID not present or garbled -- bail out */
538 cFYI(1, ("Error decoding negTokenInit header"));
543 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
544 cFYI(1, ("Error decoding negTokenInit"));
546 } else if ((cls
!= ASN1_CTX
) || (con
!= ASN1_CON
)
547 || (tag
!= ASN1_EOC
)) {
549 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
550 cls
, con
, tag
, end
, *end
));
555 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
556 cFYI(1, ("Error decoding negTokenInit"));
558 } else if ((cls
!= ASN1_UNI
) || (con
!= ASN1_CON
)
559 || (tag
!= ASN1_SEQ
)) {
561 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
562 cls
, con
, tag
, end
, *end
));
567 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
568 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
570 } else if ((cls
!= ASN1_CTX
) || (con
!= ASN1_CON
)
571 || (tag
!= ASN1_EOC
)) {
573 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
574 cls
, con
, tag
, end
, *end
));
579 if (asn1_header_decode
580 (&ctx
, &sequence_end
, &cls
, &con
, &tag
) == 0) {
581 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
583 } else if ((cls
!= ASN1_UNI
) || (con
!= ASN1_CON
)
584 || (tag
!= ASN1_SEQ
)) {
586 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
587 cls
, con
, tag
, end
, *end
));
591 /* list of security mechanisms */
592 while (!asn1_eoc_decode(&ctx
, sequence_end
)) {
593 rc
= asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
);
596 ("Error decoding negTokenInit hdr exit2"));
599 if ((tag
== ASN1_OJI
) && (con
== ASN1_PRI
)) {
600 if (asn1_oid_decode(&ctx
, end
, &oid
, &oidlen
)) {
602 cFYI(1, ("OID len = %d oid = 0x%lx 0x%lx "
603 "0x%lx 0x%lx", oidlen
, *oid
,
604 *(oid
+ 1), *(oid
+ 2), *(oid
+ 3)));
606 if (compare_oid(oid
, oidlen
, MSKRB5_OID
,
609 use_mskerberos
= true;
610 else if (compare_oid(oid
, oidlen
, KRB5U2U_OID
,
613 use_kerberosu2u
= true;
614 else if (compare_oid(oid
, oidlen
, KRB5_OID
,
618 else if (compare_oid(oid
, oidlen
, NTLMSSP_OID
,
625 cFYI(1, ("Should be an oid what is going on?"));
630 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
631 /* Check if we have reached the end of the blob, but with
632 no mechListMic (e.g. NTLMSSP instead of KRB5) */
633 if (ctx
.error
== ASN1_ERR_DEC_EMPTY
)
634 goto decode_negtoken_exit
;
635 cFYI(1, ("Error decoding last part negTokenInit exit3"));
637 } else if ((cls
!= ASN1_CTX
) || (con
!= ASN1_CON
)) {
638 /* tag = 3 indicating mechListMIC */
639 cFYI(1, ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
640 cls
, con
, tag
, end
, *end
));
645 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
646 cFYI(1, ("Error decoding last part negTokenInit exit5"));
648 } else if ((cls
!= ASN1_UNI
) || (con
!= ASN1_CON
)
649 || (tag
!= ASN1_SEQ
)) {
650 cFYI(1, ("cls = %d con = %d tag = %d end = %p (%d)",
651 cls
, con
, tag
, end
, *end
));
655 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
656 cFYI(1, ("Error decoding last part negTokenInit exit 7"));
658 } else if ((cls
!= ASN1_CTX
) || (con
!= ASN1_CON
)) {
659 cFYI(1, ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
660 cls
, con
, tag
, end
, *end
));
665 if (asn1_header_decode(&ctx
, &end
, &cls
, &con
, &tag
) == 0) {
666 cFYI(1, ("Error decoding last part negTokenInit exit9"));
668 } else if ((cls
!= ASN1_UNI
) || (con
!= ASN1_PRI
)
669 || (tag
!= ASN1_GENSTR
)) {
670 cFYI(1, ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
671 cls
, con
, tag
, end
, *end
));
674 cFYI(1, ("Need to call asn1_octets_decode() function for %s",
675 ctx
.pointer
)); /* is this UTF-8 or ASCII? */
676 decode_negtoken_exit
:
679 else if (use_mskerberos
)
680 *secType
= MSKerberos
;
681 else if (use_ntlmssp
)
682 *secType
= RawNTLMSSP
;