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 static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
81 static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
87 int error; /* Error condition */
88 unsigned char *pointer; /* Octet just to be decoded */
89 unsigned char *begin; /* First octet */
90 unsigned char *end; /* Octet after last octet */
94 * Octet string (not null terminated)
102 asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
105 ctx->end = buf + len;
107 ctx->error = ASN1_ERR_NOERROR;
111 asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
113 if (ctx->pointer >= ctx->end) {
114 ctx->error = ASN1_ERR_DEC_EMPTY;
117 *ch = *(ctx->pointer)++;
122 asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
129 if (!asn1_octet_decode(ctx, &ch))
133 } while ((ch & 0x80) == 0x80);
138 asn1_id_decode(struct asn1_ctx *ctx,
139 unsigned int *cls, unsigned int *con, unsigned int *tag)
143 if (!asn1_octet_decode(ctx, &ch))
146 *cls = (ch & 0xC0) >> 6;
147 *con = (ch & 0x20) >> 5;
151 if (!asn1_tag_decode(ctx, tag))
158 asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
160 unsigned char ch, cnt;
162 if (!asn1_octet_decode(ctx, &ch))
173 cnt = (unsigned char) (ch & 0x7F);
177 if (!asn1_octet_decode(ctx, &ch))
189 asn1_header_decode(struct asn1_ctx *ctx,
191 unsigned int *cls, unsigned int *con, unsigned int *tag)
193 unsigned int def = 0;
194 unsigned int len = 0;
196 if (!asn1_id_decode(ctx, cls, con, tag))
199 if (!asn1_length_decode(ctx, &def, &len))
203 *eoc = ctx->pointer + len;
210 asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
215 if (!asn1_octet_decode(ctx, &ch))
219 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
223 if (!asn1_octet_decode(ctx, &ch))
227 ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
232 if (ctx->pointer != eoc) {
233 ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
240 /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
247 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
248 unsigned char *eoc, long *integer)
253 if (!asn1_octet_decode(ctx, &ch))
256 *integer = (signed char) ch;
259 while (ctx->pointer < eoc) {
260 if (++len > sizeof(long)) {
261 ctx->error = ASN1_ERR_DEC_BADVALUE;
265 if (!asn1_octet_decode(ctx, &ch))
274 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
276 unsigned int *integer)
281 if (!asn1_octet_decode(ctx, &ch))
290 while (ctx->pointer < eoc) {
291 if (++len > sizeof(unsigned int)) {
292 ctx->error = ASN1_ERR_DEC_BADVALUE;
296 if (!asn1_octet_decode(ctx, &ch))
305 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
307 unsigned long *integer)
312 if (!asn1_octet_decode(ctx, &ch))
321 while (ctx->pointer < eoc) {
322 if (++len > sizeof(unsigned long)) {
323 ctx->error = ASN1_ERR_DEC_BADVALUE;
327 if (!asn1_octet_decode(ctx, &ch))
337 asn1_octets_decode(struct asn1_ctx *ctx,
339 unsigned char **octets, unsigned int *len)
345 *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
346 if (*octets == NULL) {
351 while (ctx->pointer < eoc) {
352 if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
363 asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
370 if (!asn1_octet_decode(ctx, &ch))
375 } while ((ch & 0x80) == 0x80);
380 asn1_oid_decode(struct asn1_ctx *ctx,
381 unsigned char *eoc, unsigned long **oid, unsigned int *len)
387 size = eoc - ctx->pointer + 1;
388 *oid = kmalloc(size * sizeof (unsigned long), GFP_ATOMIC);
395 if (!asn1_subid_decode(ctx, &subid)) {
404 } else if (subid < 80) {
406 optr[1] = subid - 40;
409 optr[1] = subid - 80;
415 while (ctx->pointer < eoc) {
416 if (++(*len) > size) {
417 ctx->error = ASN1_ERR_DEC_BADVALUE;
423 if (!asn1_subid_decode(ctx, optr++)) {
433 compare_oid(unsigned long *oid1, unsigned int oid1len,
434 unsigned long *oid2, unsigned int oid2len)
438 if (oid1len != oid2len)
441 for (i = 0; i < oid1len; i++) {
442 if (oid1[i] != oid2[i])
449 /* BB check for endian conversion issues here */
452 decode_negTokenInit(unsigned char *security_blob, int length,
453 enum securityEnum *secType)
457 unsigned char *sequence_end;
458 unsigned long *oid = NULL;
459 unsigned int cls, con, tag, oidlen, rc;
460 int use_ntlmssp = FALSE;
462 *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default*/
464 /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
466 asn1_open(&ctx, security_blob, length);
468 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
469 cFYI(1, ("Error decoding negTokenInit header"));
471 } else if ((cls != ASN1_APL) || (con != ASN1_CON)
472 || (tag != ASN1_EOC)) {
473 cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
476 /* remember to free obj->oid */
477 rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
479 if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
480 rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
482 rc = compare_oid(oid, oidlen,
492 cFYI(1, ("Error decoding negTokenInit header"));
496 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
497 cFYI(1, ("Error decoding negTokenInit"));
499 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
500 || (tag != ASN1_EOC)) {
502 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
503 cls, con, tag, end, *end));
507 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
508 cFYI(1, ("Error decoding negTokenInit"));
510 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
511 || (tag != ASN1_SEQ)) {
513 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
514 cls, con, tag, end, *end));
518 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
519 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
521 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
522 || (tag != ASN1_EOC)) {
524 ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
525 cls, con, tag, end, *end));
529 if (asn1_header_decode
530 (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
531 cFYI(1, ("Error decoding 2nd part of negTokenInit"));
533 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
534 || (tag != ASN1_SEQ)) {
536 ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
537 cls, con, tag, end, *end));
541 while (!asn1_eoc_decode(&ctx, sequence_end)) {
542 rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
545 ("Error decoding negTokenInit hdr exit2"));
548 if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
549 rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
552 ("OID len = %d oid = 0x%lx 0x%lx "
554 oidlen, *oid, *(oid + 1),
555 *(oid + 2), *(oid + 3)));
556 rc = compare_oid(oid, oidlen,
557 NTLMSSP_OID, NTLMSSP_OID_LEN);
563 cFYI(1, ("Should be an oid what is going on?"));
567 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
569 ("Error decoding last part negTokenInit exit3"));
571 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
572 /* tag = 3 indicating mechListMIC */
574 ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
575 cls, con, tag, end, *end));
578 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
580 ("Error decoding last part negTokenInit exit5"));
582 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
583 || (tag != ASN1_SEQ)) {
585 ("Exit 6 cls = %d con = %d tag = %d end = %p (%d)",
586 cls, con, tag, end, *end));
589 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
591 ("Error decoding last part negTokenInit exit 7"));
593 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
595 ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
596 cls, con, tag, end, *end));
599 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
601 ("Error decoding last part negTokenInit exit9"));
603 } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
604 || (tag != ASN1_GENSTR)) {
606 ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
607 cls, con, tag, end, *end));
610 cFYI(1, ("Need to call asn1_octets_decode() function for %s",
611 ctx.pointer)); /* is this UTF-8 or ASCII? */