62c04270614c5d0df526f3614a2d6590246d025a
[krb5.git] / src / lib / krb5 / asn.1 / asn1_decode.c
1 /* -*- mode: c; indent-tabs-mode: nil -*- */
2 /*
3  * src/lib/krb5/asn.1/asn1_decode.c
4  *
5  * Copyright 1994, 2003 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  */
27
28 /* ASN.1 primitive decoders */
29 #include "k5-int.h" /* for krb5int_gmt_mktime */
30 #include "asn1_decode.h"
31 #include "asn1_get.h"
32 #include <stdio.h>
33 #ifdef HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #ifdef TIME_WITH_SYS_TIME
36 #include <time.h>
37 #endif
38 #else
39 #include <time.h>
40 #endif
41
42 #define setup()\
43 asn1_error_code retval;\
44 taginfo tinfo
45
46 #define asn1class       (tinfo.asn1class)
47 #define construction    (tinfo.construction)
48 #define tagnum          (tinfo.tagnum)
49 #define length          (tinfo.length)
50
51 #define tag(type)\
52 retval = asn1_get_tag_2(buf,&tinfo);\
53 if (retval) return retval;\
54 if (asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\
55   return ASN1_BAD_ID
56
57 #define cleanup()\
58 return 0
59
60 asn1_error_code asn1_decode_integer(asn1buf *buf, long int *val)
61 {
62     setup();
63     asn1_octet o;
64     long n = 0; /* initialize to keep gcc happy */
65     unsigned int i;
66
67     tag(ASN1_INTEGER);
68
69     for (i = 0; i < length; i++) {
70         retval = asn1buf_remove_octet(buf, &o);
71         if (retval) return retval;
72         if (!i) {
73             n = (0x80 & o) ? -1 : 0;    /* grab sign bit */
74             if (n < 0 && length > sizeof (long))
75                 return ASN1_OVERFLOW;
76             else if (length > sizeof (long) + 1) /* allow extra octet for positive */
77                 return ASN1_OVERFLOW;
78         }
79         n = (n << 8) | o;
80     }
81     *val = n;
82     cleanup();
83 }
84
85 asn1_error_code asn1_decode_unsigned_integer(asn1buf *buf, long unsigned int *val)
86 {
87     setup();
88     asn1_octet o;
89     unsigned long n;
90     unsigned int i;
91
92     tag(ASN1_INTEGER);
93
94     for (i = 0, n = 0; i < length; i++) {
95         retval = asn1buf_remove_octet(buf, &o);
96         if (retval) return retval;
97         if (!i) {
98             if (0x80 & o)
99                 return ASN1_OVERFLOW;
100             else if (length > sizeof (long) + 1)
101                 return ASN1_OVERFLOW;
102         }
103         n = (n << 8) | o;
104     }
105     *val = n;
106     cleanup();
107 }
108
109 /*
110  * asn1_decode_maybe_unsigned
111  *
112  * This is needed because older releases of MIT krb5 have signed
113  * sequence numbers.  We want to accept both signed and unsigned
114  * sequence numbers, in the range -2^31..2^32-1, mapping negative
115  * numbers into their positive equivalents in the same way that C's
116  * normal integer conversions do, i.e., would preserve bits on a
117  * two's-complement architecture.
118  */
119 asn1_error_code asn1_decode_maybe_unsigned(asn1buf *buf, unsigned long *val)
120 {
121     setup();
122     asn1_octet o;
123     unsigned long n, bitsremain;
124     unsigned int i;
125
126     tag(ASN1_INTEGER);
127     o = 0;
128     n = 0;
129     bitsremain = ~0UL;
130     for (i = 0; i < length; i++) {
131         /* Accounts for u_long width not being a multiple of 8. */
132         if (bitsremain < 0xff) return ASN1_OVERFLOW;
133         retval = asn1buf_remove_octet(buf, &o);
134         if (retval) return retval;
135         if (bitsremain == ~0UL) {
136             if (i == 0)
137                 n = (o & 0x80) ? ~0UL : 0UL; /* grab sign bit */
138             /*
139              * Skip leading zero or 0xFF octets to humor non-compliant encoders.
140              */
141             if (n == 0 && o == 0)
142                 continue;
143             if (n == ~0UL && o == 0xff)
144                 continue;
145         }
146         n = (n << 8) | o;
147         bitsremain >>= 8;
148     }
149     *val = n;
150     cleanup();
151 }
152
153 asn1_error_code asn1_decode_oid(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
154 {
155     setup();
156     tag(ASN1_OBJECTIDENTIFIER);
157     retval = asn1buf_remove_octetstring(buf, length, val);
158     if (retval) return retval;
159     *retlen = length;
160     cleanup();
161 }
162
163 asn1_error_code asn1_decode_octetstring(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
164 {
165     setup();
166     tag(ASN1_OCTETSTRING);
167     retval = asn1buf_remove_octetstring(buf,length,val);
168     if (retval) return retval;
169     *retlen = length;
170     cleanup();
171 }
172
173 asn1_error_code asn1_decode_charstring(asn1buf *buf, unsigned int *retlen, char **val)
174 {
175     setup();
176     tag(ASN1_OCTETSTRING);
177     retval = asn1buf_remove_charstring(buf,length,val);
178     if (retval) return retval;
179     *retlen = length;
180     cleanup();
181 }
182
183
184 asn1_error_code asn1_decode_generalstring(asn1buf *buf, unsigned int *retlen, char **val)
185 {
186     setup();
187     tag(ASN1_GENERALSTRING);
188     retval = asn1buf_remove_charstring(buf,length,val);
189     if (retval) return retval;
190     *retlen = length;
191     cleanup();
192 }
193
194
195 asn1_error_code asn1_decode_null(asn1buf *buf)
196 {
197     setup();
198     tag(ASN1_NULL);
199     if (length != 0) return ASN1_BAD_LENGTH;
200     cleanup();
201 }
202
203 asn1_error_code asn1_decode_printablestring(asn1buf *buf, int *retlen, char **val)
204 {
205     setup();
206     tag(ASN1_PRINTABLESTRING);
207     retval = asn1buf_remove_charstring(buf,length,val);
208     if (retval) return retval;
209     *retlen = length;
210     cleanup();
211 }
212
213 asn1_error_code asn1_decode_ia5string(asn1buf *buf, int *retlen, char **val)
214 {
215     setup();
216     tag(ASN1_IA5STRING);
217     retval = asn1buf_remove_charstring(buf,length,val);
218     if (retval) return retval;
219     *retlen = length;
220     cleanup();
221 }
222
223 asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val)
224 {
225     setup();
226     char *s;
227     struct tm ts;
228     time_t t;
229
230     tag(ASN1_GENERALTIME);
231
232     if (length != 15) return ASN1_BAD_LENGTH;
233     retval = asn1buf_remove_charstring(buf,15,&s);
234     /* Time encoding: YYYYMMDDhhmmssZ */
235     if (s[14] != 'Z') {
236         free(s);
237         return ASN1_BAD_FORMAT;
238     }
239     if (s[0] == '1' && !memcmp("19700101000000Z", s, 15)) {
240         t = 0;
241         free(s);
242         goto done;
243     }
244 #define c2i(c) ((c)-'0')
245     ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3])
246         - 1900;
247     ts.tm_mon = 10*c2i(s[4]) + c2i(s[5]) - 1;
248     ts.tm_mday = 10*c2i(s[6]) + c2i(s[7]);
249     ts.tm_hour = 10*c2i(s[8]) + c2i(s[9]);
250     ts.tm_min = 10*c2i(s[10]) + c2i(s[11]);
251     ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]);
252     ts.tm_isdst = -1;
253     t = krb5int_gmt_mktime(&ts);
254     free(s);
255
256     if (t == -1) return ASN1_BAD_TIMEFORMAT;
257
258 done:
259     *val = t;
260     cleanup();
261 }