ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / cifs / asn1.c
1 /* 
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
4  *      
5  * Copyright (c) 2000 RP Internet (www.rpi.net.au).
6  *
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
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifs_debug.h"
29
30 /*****************************************************************************
31  *
32  * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
33  *
34  *****************************************************************************/
35
36 /* Class */
37 #define ASN1_UNI        0       /* Universal */
38 #define ASN1_APL        1       /* Application */
39 #define ASN1_CTX        2       /* Context */
40 #define ASN1_PRV        3       /* Private */
41
42 /* Tag */
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 */
64
65 /* Primitive / Constructed methods*/
66 #define ASN1_PRI        0       /* Primitive */
67 #define ASN1_CON        1       /* Constructed */
68
69 /*
70  * Error codes.
71  */
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
77
78 #define SPNEGO_OID_LEN 7
79 #define NTLMSSP_OID_LEN  10
80 unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
81 unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
82
83 /* 
84  * ASN.1 context.
85  */
86 struct asn1_ctx {
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 */
91 };
92
93 /*
94  * Octet string (not null terminated)
95  */
96 struct asn1_octstr {
97         unsigned char *data;
98         unsigned int len;
99 };
100
101 static void
102 asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
103 {
104         ctx->begin = buf;
105         ctx->end = buf + len;
106         ctx->pointer = buf;
107         ctx->error = ASN1_ERR_NOERROR;
108 }
109
110 static unsigned char
111 asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
112 {
113         if (ctx->pointer >= ctx->end) {
114                 ctx->error = ASN1_ERR_DEC_EMPTY;
115                 return 0;
116         }
117         *ch = *(ctx->pointer)++;
118         return 1;
119 }
120
121 static unsigned char
122 asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
123 {
124         unsigned char ch;
125
126         *tag = 0;
127
128         do {
129                 if (!asn1_octet_decode(ctx, &ch))
130                         return 0;
131                 *tag <<= 7;
132                 *tag |= ch & 0x7F;
133         } while ((ch & 0x80) == 0x80);
134         return 1;
135 }
136
137 static unsigned char
138 asn1_id_decode(struct asn1_ctx *ctx,
139                unsigned int *cls, unsigned int *con, unsigned int *tag)
140 {
141         unsigned char ch;
142
143         if (!asn1_octet_decode(ctx, &ch))
144                 return 0;
145
146         *cls = (ch & 0xC0) >> 6;
147         *con = (ch & 0x20) >> 5;
148         *tag = (ch & 0x1F);
149
150         if (*tag == 0x1F) {
151                 if (!asn1_tag_decode(ctx, tag))
152                         return 0;
153         }
154         return 1;
155 }
156
157 static unsigned char
158 asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
159 {
160         unsigned char ch, cnt;
161
162         if (!asn1_octet_decode(ctx, &ch))
163                 return 0;
164
165         if (ch == 0x80)
166                 *def = 0;
167         else {
168                 *def = 1;
169
170                 if (ch < 0x80)
171                         *len = ch;
172                 else {
173                         cnt = (unsigned char) (ch & 0x7F);
174                         *len = 0;
175
176                         while (cnt > 0) {
177                                 if (!asn1_octet_decode(ctx, &ch))
178                                         return 0;
179                                 *len <<= 8;
180                                 *len |= ch;
181                                 cnt--;
182                         }
183                 }
184         }
185         return 1;
186 }
187
188 static unsigned char
189 asn1_header_decode(struct asn1_ctx *ctx,
190                    unsigned char **eoc,
191                    unsigned int *cls, unsigned int *con, unsigned int *tag)
192 {
193         unsigned int def, len;
194
195         if (!asn1_id_decode(ctx, cls, con, tag))
196                 return 0;
197
198         if (!asn1_length_decode(ctx, &def, &len))
199                 return 0;
200
201         if (def)
202                 *eoc = ctx->pointer + len;
203         else
204                 *eoc = 0;
205         return 1;
206 }
207
208 static unsigned char
209 asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
210 {
211         unsigned char ch;
212
213         if (eoc == 0) {
214                 if (!asn1_octet_decode(ctx, &ch))
215                         return 0;
216
217                 if (ch != 0x00) {
218                         ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
219                         return 0;
220                 }
221
222                 if (!asn1_octet_decode(ctx, &ch))
223                         return 0;
224
225                 if (ch != 0x00) {
226                         ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
227                         return 0;
228                 }
229                 return 1;
230         } else {
231                 if (ctx->pointer != eoc) {
232                         ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
233                         return 0;
234                 }
235                 return 1;
236         }
237 }
238
239 /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
240                                       unsigned char *eoc)
241 {
242         ctx->pointer = eoc;
243         return 1;
244 }
245
246 static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
247                                       unsigned char *eoc, long *integer)
248 {
249         unsigned char ch;
250         unsigned int len;
251
252         if (!asn1_octet_decode(ctx, &ch))
253                 return 0;
254
255         *integer = (signed char) ch;
256         len = 1;
257
258         while (ctx->pointer < eoc) {
259                 if (++len > sizeof(long)) {
260                         ctx->error = ASN1_ERR_DEC_BADVALUE;
261                         return 0;
262                 }
263
264                 if (!asn1_octet_decode(ctx, &ch))
265                         return 0;
266
267                 *integer <<= 8;
268                 *integer |= ch;
269         }
270         return 1;
271 }
272
273 static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
274                                       unsigned char *eoc,
275                                       unsigned int *integer)
276 {
277         unsigned char ch;
278         unsigned int len;
279
280         if (!asn1_octet_decode(ctx, &ch))
281                 return 0;
282
283         *integer = ch;
284         if (ch == 0)
285                 len = 0;
286         else
287                 len = 1;
288
289         while (ctx->pointer < eoc) {
290                 if (++len > sizeof(unsigned int)) {
291                         ctx->error = ASN1_ERR_DEC_BADVALUE;
292                         return 0;
293                 }
294
295                 if (!asn1_octet_decode(ctx, &ch))
296                         return 0;
297
298                 *integer <<= 8;
299                 *integer |= ch;
300         }
301         return 1;
302 }
303
304 static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
305                                        unsigned char *eoc,
306                                        unsigned long *integer)
307 {
308         unsigned char ch;
309         unsigned int len;
310
311         if (!asn1_octet_decode(ctx, &ch))
312                 return 0;
313
314         *integer = ch;
315         if (ch == 0)
316                 len = 0;
317         else
318                 len = 1;
319
320         while (ctx->pointer < eoc) {
321                 if (++len > sizeof(unsigned long)) {
322                         ctx->error = ASN1_ERR_DEC_BADVALUE;
323                         return 0;
324                 }
325
326                 if (!asn1_octet_decode(ctx, &ch))
327                         return 0;
328
329                 *integer <<= 8;
330                 *integer |= ch;
331         }
332         return 1;
333
334
335 static unsigned char
336 asn1_octets_decode(struct asn1_ctx *ctx,
337                    unsigned char *eoc,
338                    unsigned char **octets, unsigned int *len)
339 {
340         unsigned char *ptr;
341
342         *len = 0;
343
344         *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
345         if (*octets == NULL) {
346                 return 0;
347         }
348
349         ptr = *octets;
350         while (ctx->pointer < eoc) {
351                 if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
352                         kfree(*octets);
353                         *octets = NULL;
354                         return 0;
355                 }
356                 (*len)++;
357         }
358         return 1;
359 } */
360
361 static unsigned char
362 asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
363 {
364         unsigned char ch;
365
366         *subid = 0;
367
368         do {
369                 if (!asn1_octet_decode(ctx, &ch))
370                         return 0;
371
372                 *subid <<= 7;
373                 *subid |= ch & 0x7F;
374         } while ((ch & 0x80) == 0x80);
375         return 1;
376 }
377
378 static unsigned char
379 asn1_oid_decode(struct asn1_ctx *ctx,
380                 unsigned char *eoc, unsigned long **oid, unsigned int *len)
381 {
382         unsigned long subid;
383         unsigned int size;
384         unsigned long *optr;
385
386         size = eoc - ctx->pointer + 1;
387         *oid = kmalloc(size * sizeof (unsigned long), GFP_ATOMIC);
388         if (*oid == NULL) {
389                 return 0;
390         }
391
392         optr = *oid;
393
394         if (!asn1_subid_decode(ctx, &subid)) {
395                 kfree(*oid);
396                 *oid = NULL;
397                 return 0;
398         }
399
400         if (subid < 40) {
401                 optr[0] = 0;
402                 optr[1] = subid;
403         } else if (subid < 80) {
404                 optr[0] = 1;
405                 optr[1] = subid - 40;
406         } else {
407                 optr[0] = 2;
408                 optr[1] = subid - 80;
409         }
410
411         *len = 2;
412         optr += 2;
413
414         while (ctx->pointer < eoc) {
415                 if (++(*len) > size) {
416                         ctx->error = ASN1_ERR_DEC_BADVALUE;
417                         kfree(*oid);
418                         *oid = NULL;
419                         return 0;
420                 }
421
422                 if (!asn1_subid_decode(ctx, optr++)) {
423                         kfree(*oid);
424                         *oid = NULL;
425                         return 0;
426                 }
427         }
428         return 1;
429 }
430
431 static int
432 compare_oid(unsigned long *oid1, unsigned int oid1len,
433             unsigned long *oid2, unsigned int oid2len)
434 {
435         unsigned int i;
436
437         if (oid1len != oid2len)
438                 return 0;
439         else {
440                 for (i = 0; i < oid1len; i++) {
441                         if (oid1[i] != oid2[i])
442                                 return 0;
443                 }
444                 return 1;
445         }
446 }
447
448         /* BB check for endian conversion issues here */
449
450 int
451 decode_negTokenInit(unsigned char *security_blob, int length,
452                     enum securityEnum *secType)
453 {
454         struct asn1_ctx ctx;
455         unsigned char *end;
456         unsigned char *sequence_end;
457         unsigned long *oid;
458         unsigned int cls, con, tag, oidlen, rc;
459         int use_ntlmssp = FALSE;
460
461     *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default */
462
463         /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
464
465         asn1_open(&ctx, security_blob, length);
466
467         if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
468                 cFYI(1, ("Error decoding negTokenInit header "));
469                 return 0;
470         } else if ((cls != ASN1_APL) || (con != ASN1_CON)
471                    || (tag != ASN1_EOC)) {
472                 cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
473                 return 0;
474         } else {
475                 /*      remember to free obj->oid */
476                 rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
477                 if (rc) {
478                         if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
479                                 rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
480                                 if (rc) {
481                                         rc = compare_oid(oid, oidlen,
482                                                          SPNEGO_OID,
483                                                          SPNEGO_OID_LEN);
484                                         kfree(oid);
485                                 }
486                         } else
487                                 rc = 0;
488                 }
489
490                 if (!rc) {
491                         cFYI(1, ("Error decoding negTokenInit header"));
492                         return 0;
493                 }
494
495                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
496                         cFYI(1, ("Error decoding negTokenInit "));
497                         return 0;
498                 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
499                            || (tag != ASN1_EOC)) {
500                         cFYI(1,("cls = %d con = %d tag = %d end = %p (%d) exit 0",
501                               cls, con, tag, end, *end));
502                         return 0;
503                 }
504
505                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
506                         cFYI(1, ("Error decoding negTokenInit "));
507                         return 0;
508                 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
509                            || (tag != ASN1_SEQ)) {
510                         cFYI(1,("cls = %d con = %d tag = %d end = %p (%d) exit 1",
511                               cls, con, tag, end, *end));
512                         return 0;
513                 }
514
515                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
516                         cFYI(1, ("Error decoding 2nd part of negTokenInit "));
517                         return 0;
518                 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
519                            || (tag != ASN1_EOC)) {
520                         cFYI(1,
521                              ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
522                               cls, con, tag, end, *end));
523                         return 0;
524                 }
525
526                 if (asn1_header_decode
527                     (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
528                         cFYI(1, ("Error decoding 2nd part of negTokenInit "));
529                         return 0;
530                 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
531                            || (tag != ASN1_SEQ)) {
532                         cFYI(1,
533                              ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
534                               cls, con, tag, end, *end));
535                         return 0;
536                 }
537
538                 while (!asn1_eoc_decode(&ctx, sequence_end)) {
539                         rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
540                         if (!rc) {
541                                 cFYI(1,
542                                      ("Error 1 decoding negTokenInit header exit 2"));
543                                 return 0;
544                         }
545                         if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
546                                 asn1_oid_decode(&ctx, end, &oid, &oidlen);
547                                 cFYI(1,
548                                      ("OID len = %d oid = 0x%lx 0x%lx 0x%lx 0x%lx",
549                                       oidlen, *oid, *(oid + 1), *(oid + 2),
550                                       *(oid + 3)));
551                                 rc = compare_oid(oid, oidlen, NTLMSSP_OID,
552                                                  NTLMSSP_OID_LEN);
553                                 kfree(oid);
554                                 if (rc)
555                                         use_ntlmssp = TRUE;
556                         } else {
557                                 cFYI(1,("This should be an oid what is going on? "));
558                         }
559                 }
560
561                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
562                         cFYI(1,
563                              ("Error decoding last part of negTokenInit exit 3"));
564                         return 0;
565                 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {    /* tag = 3 indicating mechListMIC */
566                         cFYI(1,
567                              ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
568                               cls, con, tag, end, *end));
569                         return 0;
570                 }
571                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
572                         cFYI(1,
573                              ("Error decoding last part of negTokenInit exit 5"));
574                         return 0;
575                 } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
576                            || (tag != ASN1_SEQ)) {
577                         cFYI(1,
578                              ("Exit 6 cls = %d con = %d tag = %d end = %p (%d)",
579                               cls, con, tag, end, *end));
580                 }
581
582                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
583                         cFYI(1,
584                              ("Error decoding last part of negTokenInit exit 7"));
585                         return 0;
586                 } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
587                         cFYI(1,
588                              ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
589                               cls, con, tag, end, *end));
590                         return 0;
591                 }
592                 if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
593                         cFYI(1,
594                              ("Error decoding last part of negTokenInit exit 9"));
595                         return 0;
596                 } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
597                            || (tag != ASN1_GENSTR)) {
598                         cFYI(1,
599                              ("Exit 10 cls = %d con = %d tag = %d end = %p (%d)",
600                               cls, con, tag, end, *end));
601                         return 0;
602                 }
603                 cFYI(1, ("Need to call asn1_octets_decode() function for this %s", ctx.pointer));       /* is this UTF-8 or ASCII? */
604         }
605
606         /* if (use_kerberos) 
607            *secType = Kerberos 
608            else */
609         if (use_ntlmssp) {
610                 *secType = NTLMSSP;
611         }
612
613         return 1;
614 }