xref: /titanic_51/usr/src/lib/gss_mechs/mech_krb5/krb5/asn.1/asn1_decode.c (revision 5caf44512708d13394a092c67a06333261406209)
1 /*
2  * src/lib/krb5/asn.1/asn1_decode.c
3  *
4  * Copyright 1994, 2003 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /* ASN.1 primitive decoders */
28 #include "asn1_decode.h"
29 #include "asn1_get.h"
30 #include <stdio.h>
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #ifdef TIME_WITH_SYS_TIME
34 #include <time.h>
35 #endif
36 #else
37 #include <time.h>
38 #endif
39 
40 #define setup()\
41 asn1_error_code retval;\
42 taginfo tinfo
43 
44 #define asn1class	(tinfo.asn1class)
45 #define construction	(tinfo.construction)
46 #define tagnum		(tinfo.tagnum)
47 #define length		(tinfo.length)
48 
49 #define tag(type)\
50 retval = asn1_get_tag_2(buf,&tinfo);\
51 if(retval) return retval;\
52 if(asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\
53   return ASN1_BAD_ID
54 
55 #define cleanup()\
56 return 0
57 
58 extern time_t krb5int_gmt_mktime (struct tm *);
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   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   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   if (retval) return retval;
235   /* Time encoding: YYYYMMDDhhmmssZ */
236   if(s[14] != 'Z') {
237       free(s);
238       return ASN1_BAD_FORMAT;
239   }
240   if(s[0] == '1' && !memcmp("19700101000000Z", s, 15)) {
241       t = 0;
242       free(s);
243       goto done;
244   }
245 #define c2i(c) ((c)-'0')
246   ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3])
247     - 1900;
248   ts.tm_mon = 10*c2i(s[4]) + c2i(s[5]) - 1;
249   ts.tm_mday = 10*c2i(s[6]) + c2i(s[7]);
250   ts.tm_hour = 10*c2i(s[8]) + c2i(s[9]);
251   ts.tm_min = 10*c2i(s[10]) + c2i(s[11]);
252   ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]);
253   ts.tm_isdst = -1;
254   t = krb5int_gmt_mktime(&ts);
255   free(s);
256 
257   if(t == -1) return ASN1_BAD_TIMEFORMAT;
258 
259 done:
260   *val = t;
261   cleanup();
262 }
263