1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_decode_utctime.c
15 ASN.1 DER, decode a UTCTIME, Tom St Denis
20 static int char_to_int(unsigned char x)
37 #define DECODE_V(y, max) \
38 y = char_to_int(buf[x])*10 + char_to_int(buf[x+1]); \
39 if (y >= max) return CRYPT_INVALID_PACKET; \
43 Decodes a UTC time structure in DER format (reads all 6 valid encoding formats)
44 @param in Input buffer
45 @param inlen Length of input buffer in octets
46 @param out [out] Destination of UTC time structure
47 @return CRYPT_OK if successful
49 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
52 unsigned char buf[32];
56 LTC_ARGCHK(in != NULL);
57 LTC_ARGCHK(inlen != NULL);
58 LTC_ARGCHK(out != NULL);
61 if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
62 return CRYPT_INVALID_PACKET;
65 /* decode the string */
66 for (x = 0; x < in[1]; x++) {
67 y = der_ia5_value_decode(in[x+2]);
69 return CRYPT_INVALID_PACKET;
76 /* possible encodings are
84 So let's do a trivial decode upto [including] mm
88 DECODE_V(out->YY, 100);
89 DECODE_V(out->MM, 13);
90 DECODE_V(out->DD, 32);
91 DECODE_V(out->hh, 24);
92 DECODE_V(out->mm, 60);
94 /* clear timezone and seconds info */
95 out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
97 /* now is it Z, +, - or 0-9 */
100 } else if (buf[x] == '+' || buf[x] == '-') {
101 out->off_dir = (buf[x++] == '+') ? 0 : 1;
102 DECODE_V(out->off_hh, 24);
103 DECODE_V(out->off_mm, 60);
108 DECODE_V(out->ss, 60);
110 /* now is it Z, +, - */
113 } else if (buf[x] == '+' || buf[x] == '-') {
114 out->off_dir = (buf[x++] == '+') ? 0 : 1;
115 DECODE_V(out->off_hh, 24);
116 DECODE_V(out->off_mm, 60);
119 return CRYPT_INVALID_PACKET;
125 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/utctime/der_decode_utctime.c,v $ */
126 /* $Revision: 1.8 $ */
127 /* $Date: 2006/03/31 14:15:35 $ */