1 /* -*- mode: c; indent-tabs-mode: nil -*- */
3 * src/lib/krb5/asn.1/asn1_decode.c
5 * Copyright 1994, 2003 by the Massachusetts Institute of Technology.
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.
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.
28 /* ASN.1 primitive decoders */
29 #include "k5-int.h" /* for krb5int_gmt_mktime */
30 #include "asn1_decode.h"
33 #ifdef HAVE_SYS_TIME_H
35 #ifdef TIME_WITH_SYS_TIME
43 asn1_error_code retval;\
46 #define asn1class (tinfo.asn1class)
47 #define construction (tinfo.construction)
48 #define tagnum (tinfo.tagnum)
49 #define length (tinfo.length)
52 retval = asn1_get_tag_2(buf,&tinfo);\
53 if (retval) return retval;\
54 if (asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\
60 asn1_error_code asn1_decode_integer(asn1buf *buf, long int *val)
64 long n = 0; /* initialize to keep gcc happy */
69 for (i = 0; i < length; i++) {
70 retval = asn1buf_remove_octet(buf, &o);
71 if (retval) return retval;
73 n = (0x80 & o) ? -1 : 0; /* grab sign bit */
74 if (n < 0 && length > sizeof (long))
76 else if (length > sizeof (long) + 1) /* allow extra octet for positive */
85 asn1_error_code asn1_decode_unsigned_integer(asn1buf *buf, long unsigned int *val)
94 for (i = 0, n = 0; i < length; i++) {
95 retval = asn1buf_remove_octet(buf, &o);
96 if (retval) return retval;
100 else if (length > sizeof (long) + 1)
101 return ASN1_OVERFLOW;
110 * asn1_decode_maybe_unsigned
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.
119 asn1_error_code asn1_decode_maybe_unsigned(asn1buf *buf, unsigned long *val)
123 unsigned long n, bitsremain;
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) {
137 n = (o & 0x80) ? ~0UL : 0UL; /* grab sign bit */
139 * Skip leading zero or 0xFF octets to humor non-compliant encoders.
141 if (n == 0 && o == 0)
143 if (n == ~0UL && o == 0xff)
153 asn1_error_code asn1_decode_oid(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
156 tag(ASN1_OBJECTIDENTIFIER);
157 retval = asn1buf_remove_octetstring(buf, length, val);
158 if (retval) return retval;
163 asn1_error_code asn1_decode_octetstring(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
166 tag(ASN1_OCTETSTRING);
167 retval = asn1buf_remove_octetstring(buf,length,val);
168 if (retval) return retval;
173 asn1_error_code asn1_decode_charstring(asn1buf *buf, unsigned int *retlen, char **val)
176 tag(ASN1_OCTETSTRING);
177 retval = asn1buf_remove_charstring(buf,length,val);
178 if (retval) return retval;
184 asn1_error_code asn1_decode_generalstring(asn1buf *buf, unsigned int *retlen, char **val)
187 tag(ASN1_GENERALSTRING);
188 retval = asn1buf_remove_charstring(buf,length,val);
189 if (retval) return retval;
195 asn1_error_code asn1_decode_null(asn1buf *buf)
199 if (length != 0) return ASN1_BAD_LENGTH;
203 asn1_error_code asn1_decode_printablestring(asn1buf *buf, int *retlen, char **val)
206 tag(ASN1_PRINTABLESTRING);
207 retval = asn1buf_remove_charstring(buf,length,val);
208 if (retval) return retval;
213 asn1_error_code asn1_decode_ia5string(asn1buf *buf, int *retlen, char **val)
217 retval = asn1buf_remove_charstring(buf,length,val);
218 if (retval) return retval;
223 asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val)
230 tag(ASN1_GENERALTIME);
232 if (length != 15) return ASN1_BAD_LENGTH;
233 retval = asn1buf_remove_charstring(buf,15,&s);
234 /* Time encoding: YYYYMMDDhhmmssZ */
237 return ASN1_BAD_FORMAT;
239 if (s[0] == '1' && !memcmp("19700101000000Z", s, 15)) {
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])
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]);
253 t = krb5int_gmt_mktime(&ts);
256 if (t == -1) return ASN1_BAD_TIMEFORMAT;