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_SEQ 16 /* Sequence */
53 #define ASN1_SET 17 /* Set */
54 #define ASN1_NUMSTR 18 /* Numerical String */
55 #define ASN1_PRNSTR 19 /* Printable String */
56 #define ASN1_TEXSTR 20 /* Teletext String */
57 #define ASN1_VIDSTR 21 /* Video String */
58 #define ASN1_IA5STR 22 /* IA5 String */
59 #define ASN1_UNITIM 23 /* Universal Time */
60 #define ASN1_GENTIM 24 /* General Time */
61 #define ASN1_GRASTR 25 /* Graphical String */
62 #define ASN1_VISSTR 26 /* Visible String */
63 #define ASN1_GENSTR 27 /* General String */
65 /* Primitive / Constructed methods*/
66 #define ASN1_PRI 0 /* Primitive */
67 #define ASN1_CON 1 /* Constructed */
72 #define ASN1_ERR_NOERROR 0
73 #define ASN1_ERR_DEC_EMPTY 2
74 #define ASN1_ERR_DEC_EOC_MISMATCH 3
75 #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
76 #define ASN1_ERR_DEC_BADVALUE 5
78 #define SPNEGO_OID_LEN 7
79 #define NTLMSSP_OID_LEN 10
80 #define KRB5_OID_LEN 7
81 #define MSKRB5_OID_LEN 7
82 static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
83 static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
84 static unsigned long KRB5_OID[7] = { 1, 2, 840, 113554, 1, 2, 2 };
85 static unsigned long MSKRB5_OID[7] = { 1, 2, 840, 48018, 1, 2, 2 };
91 int error; /* Error condition */
92 unsigned char *pointer; /* Octet just to be decoded */
93 unsigned char *begin; /* First octet */
94 unsigned char *end; /* Octet after last octet */
98 * Octet string (not null terminated)
106 asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
109 ctx->end = buf + len;
111 ctx->error = ASN1_ERR_NOERROR;
115 asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
117 if (ctx->pointer >= ctx->end) {
118 ctx->error = ASN1_ERR_DEC_EMPTY;
121 *ch = *(ctx->pointer)++;
126 asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
133 if (!asn1_octet_decode(ctx, &ch))
137 } while ((ch & 0x80) == 0x80);
142 asn1_id_decode(struct asn1_ctx *ctx,
143 unsigned int *cls, unsigned int *con, unsigned int *tag)
147 if (!asn1_octet_decode(ctx, &ch))
150 *cls = (ch & 0xC0) >> 6;
151 *con = (ch & 0x20) >> 5;
155 if (!asn1_tag_decode(ctx, tag))
162 asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
164 unsigned char ch, cnt;
166 if (!asn1_octet_decode(ctx, &ch))
177 cnt = (unsigned char) (ch & 0x7F);
181 if (!asn1_octet_decode(ctx, &ch))
190 /* don't trust len bigger than ctx buffer */
191 if (*len > ctx->end - ctx->pointer)
198 asn1_header_decode(struct asn1_ctx *ctx,
200 unsigned int *cls, unsigned int *con, unsigned int *tag)
202 unsigned int def = 0;
203 unsigned int len = 0;
205 if (!asn1_id_decode(ctx, cls, con, tag))
208 if (!asn1_length_decode(ctx, &def, &len))
211 /* primitive shall be definite, indefinite shall be constructed */
212 if (*con == ASN1_PRI && !def)
216 *eoc = ctx->pointer + len;
223 asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
228 if (!asn1_octet_decode(ctx, &ch))
232 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
236 if (!asn1_octet_decode(ctx, &ch))
240 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
245 if (ctx->pointer != eoc) {
246 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
253 /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
260 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
261 unsigned char *eoc, long *integer)
266 if (!asn1_octet_decode(ctx, &ch))
269 *integer = (signed char) ch;
272 while (ctx->pointer < eoc) {
273 if (++len > sizeof(long)) {
274 ctx->error = ASN1_ERR_DEC_BADVALUE;
278 if (!asn1_octet_decode(ctx, &ch))
287 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
289 unsigned int *integer)
294 if (!asn1_octet_decode(ctx, &ch))
303 while (ctx->pointer < eoc) {
304 if (++len > sizeof(unsigned int)) {
305 ctx->error = ASN1_ERR_DEC_BADVALUE;
309 if (!asn1_octet_decode(ctx, &ch))
318 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
320 unsigned long *integer)
325 if (!asn1_octet_decode(ctx, &ch))
334 while (ctx->pointer < eoc) {
335 if (++len > sizeof(unsigned long)) {
336 ctx->error = ASN1_ERR_DEC_BADVALUE;
340 if (!asn1_octet_decode(ctx, &ch))
350 asn1_octets_decode(struct asn1_ctx *ctx,
352 unsigned char **octets, unsigned int *len)
358 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
359 if (*octets == NULL) {
364 while (ctx->pointer < eoc) {
365 if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
376 asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
383 if (!asn1_octet_decode(ctx, &ch))
388 } while ((ch & 0x80) == 0x80);
393 asn1_oid_decode(struct asn1_ctx *ctx,
394 unsigned char *eoc, unsigned long **oid, unsigned int *len)
400 size = eoc - ctx->pointer + 1;
402 /* first subid actually encodes first two subids */
403 if (size < 2 || size > UINT_MAX/sizeof(unsigned long))
406 *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
412 if (!asn1_subid_decode(ctx, &subid)) {
421 } else if (subid < 80) {
423 optr[1] = subid - 40;
426 optr[1] = subid - 80;
432 while (ctx->pointer < eoc) {
433 if (++(*len) > size) {
434 ctx->error = ASN1_ERR_DEC_BADVALUE;
440 if (!asn1_subid_decode(ctx, optr++)) {
450 compare_oid(unsigned long *oid1, unsigned int oid1len,
451 unsigned long *oid2, unsigned int oid2len)
455 if (oid1len != oid2len)
458 for (i = 0; i < oid1len; i++) {
459 if (oid1[i] != oid2[i])
466 /* BB check for endian conversion issues here */
469 decode_negTokenInit(unsigned char *security_blob, int length,
470 enum securityEnum *secType)
474 unsigned char *sequence_end;
475 unsigned long *oid = NULL;
476 unsigned int cls, con, tag, oidlen, rc;
477 bool use_ntlmssp = false;
478 bool use_kerberos = false;
480 *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default*/
482 /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
484 asn1_open(&ctx, security_blob, length);
487 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
488 cFYI(1, ("Error decoding negTokenInit header"));
490 } else if ((cls != ASN1_APL) || (con != ASN1_CON)
491 || (tag != ASN1_EOC)) {
492 cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
496 /* Check for SPNEGO OID -- remember to free obj->oid */
497 rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
499 if ((tag == ASN1_OJI) && (con == ASN1_PRI) &&
501 rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
503 rc = compare_oid(oid, oidlen, SPNEGO_OID,
511 /* SPNEGO OID not present or garbled -- bail out */
513 cFYI(1, ("Error decoding negTokenInit header"));
517 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
518 cFYI(1, ("Error decoding negTokenInit"));
520 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
521 || (tag != ASN1_EOC)) {
523 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
524 cls, con, tag, end, *end));
528 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
529 cFYI(1, ("Error decoding negTokenInit"));
531 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
532 || (tag != ASN1_SEQ)) {
534 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
535 cls, con, tag, end, *end));
539 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
540 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
542 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
543 || (tag != ASN1_EOC)) {
545 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
546 cls, con, tag, end, *end));
550 if (asn1_header_decode
551 (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
552 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
554 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
555 || (tag != ASN1_SEQ)) {
557 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
558 cls, con, tag, end, *end));
562 while (!asn1_eoc_decode(&ctx, sequence_end)) {
563 rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
566 ("Error decoding negTokenInit hdr exit2"));
569 if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
570 if (asn1_oid_decode(&ctx, end, &oid, &oidlen)) {
572 cFYI(1, ("OID len = %d oid = 0x%lx 0x%lx "
573 "0x%lx 0x%lx", oidlen, *oid,
574 *(oid + 1), *(oid + 2), *(oid + 3)));
576 if (compare_oid(oid, oidlen, MSKRB5_OID,
579 else if (compare_oid(oid, oidlen, KRB5_OID,
582 else if (compare_oid(oid, oidlen, NTLMSSP_OID,
589 cFYI(1, ("Should be an oid what is going on?"));
593 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
594 cFYI(1, ("Error decoding last part negTokenInit exit3"));
596 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
597 /* tag = 3 indicating mechListMIC */
598 cFYI(1, ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
599 cls, con, tag, end, *end));
602 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
603 cFYI(1, ("Error decoding last part negTokenInit exit5"));
605 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
606 || (tag != ASN1_SEQ)) {
607 cFYI(1, ("cls = %d con = %d tag = %d end = %p (%d)",
608 cls, con, tag, end, *end));
611 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
612 cFYI(1, ("Error decoding last part negTokenInit exit 7"));
614 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
615 cFYI(1, ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
616 cls, con, tag, end, *end));
619 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
620 cFYI(1, ("Error decoding last part negTokenInit exit9"));
622 } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
623 || (tag != ASN1_GENSTR)) {
624 cFYI(1, ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
625 cls, con, tag, end, *end));
628 cFYI(1, ("Need to call asn1_octets_decode() function for %s",
629 ctx.pointer)); /* is this UTF-8 or ASCII? */
633 else if (use_ntlmssp)