2 * src/lib/krb5/asn.1/asn1_decode.c
4 * Copyright 1994 by the Massachusetts Institute of Technology.
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.
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. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
25 /* ASN.1 primitive decoders */
26 #include "asn1_decode.h"
28 #ifdef HAVE_SYS_TIME_H
30 #ifdef TIME_WITH_SYS_TIME
38 asn1_error_code retval;\
40 asn1_construction construction;\
45 retval = asn1_get_tag(buf,&class,&construction,&tagnum,&length);\
46 if(retval) return retval;\
47 if(class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\
53 time_t gmt_mktime PROTOTYPE((struct tm *));
55 asn1_error_code INTERFACE asn1_decode_integer(buf, val)
65 for(n=0; length > 0; length--){
66 retval = asn1buf_remove_octet(buf,&o);
67 if(retval) return retval;
68 n = (n<<8) + (unsigned int)o;
74 asn1_error_code INTERFACE asn1_decode_unsigned_integer(buf, val)
84 for(n=0; length > 0; length--){
85 retval = asn1buf_remove_octet(buf,&o);
86 if(retval) return retval;
87 n = (n<<8) + (unsigned int)o;
93 asn1_error_code INTERFACE asn1_decode_octetstring(buf, retlen, val)
99 tag(ASN1_OCTETSTRING);
100 retval = asn1buf_remove_octetstring(buf,length,val);
101 if(retval) return retval;
106 asn1_error_code INTERFACE asn1_decode_charstring(buf, retlen, val)
112 tag(ASN1_OCTETSTRING);
113 retval = asn1buf_remove_charstring(buf,length,val);
114 if(retval) return retval;
120 asn1_error_code INTERFACE asn1_decode_generalstring(buf, retlen, val)
126 tag(ASN1_GENERALSTRING);
127 retval = asn1buf_remove_charstring(buf,length,val);
128 if(retval) return retval;
134 asn1_error_code INTERFACE asn1_decode_null(buf)
139 if(length != 0) return ASN1_BAD_LENGTH;
143 asn1_error_code INTERFACE asn1_decode_printablestring(buf, retlen, val)
149 tag(ASN1_PRINTABLESTRING);
150 retval = asn1buf_remove_charstring(buf,length,val);
151 if(retval) return retval;
156 asn1_error_code INTERFACE asn1_decode_ia5string(buf, retlen, val)
163 retval = asn1buf_remove_charstring(buf,length,val);
164 if(retval) return retval;
169 asn1_error_code INTERFACE asn1_decode_generaltime(buf, val)
178 tag(ASN1_GENERALTIME);
180 if(length != 15) return ASN1_BAD_LENGTH;
181 retval = asn1buf_remove_charstring(buf,15,&s);
182 /* Time encoding: YYYYMMDDhhmmssZ */
185 return ASN1_BAD_FORMAT;
187 #define c2i(c) ((c)-'0')
188 ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3])
190 ts.tm_mon = 10*c2i(s[4]) + c2i(s[5]) - 1;
191 ts.tm_mday = 10*c2i(s[6]) + c2i(s[7]);
192 ts.tm_hour = 10*c2i(s[8]) + c2i(s[9]);
193 ts.tm_min = 10*c2i(s[10]) + c2i(s[11]);
194 ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]);
199 if(t == -1) return ASN1_BAD_TIMEFORMAT;