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
12 @file der_decode_generalizedtime.c
13 ASN.1 DER, decode a GeneralizedTime, Steffen Jaeckel
14 Based on der_decode_utctime.c
19 static int _char_to_int(unsigned char x)
36 #define DECODE_V(y, max) do {\
37 y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
38 if (y >= max) return CRYPT_INVALID_PACKET; \
42 #define DECODE_V4(y, max) do {\
43 y = _char_to_int(buf[x])*1000 + _char_to_int(buf[x+1])*100 + _char_to_int(buf[x+2])*10 + _char_to_int(buf[x+3]); \
44 if (y >= max) return CRYPT_INVALID_PACKET; \
49 Decodes a Generalized time structure in DER format (reads all 6 valid encoding formats)
50 @param in Input buffer
51 @param inlen Length of input buffer in octets
52 @param out [out] Destination of Generalized time structure
53 @return CRYPT_OK if successful
55 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
56 ltc_generalizedtime *out)
58 unsigned char buf[32];
62 LTC_ARGCHK(in != NULL);
63 LTC_ARGCHK(inlen != NULL);
64 LTC_ARGCHK(out != NULL);
67 if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
68 return CRYPT_INVALID_PACKET;
71 /* decode the string */
72 for (x = 0; x < in[1]; x++) {
73 y = der_ia5_value_decode(in[x+2]);
75 return CRYPT_INVALID_PACKET;
77 if (!((y >= '0' && y <= '9')
78 || y == 'Z' || y == '.'
79 || y == '+' || y == '-')) {
80 return CRYPT_INVALID_PACKET;
87 return CRYPT_INVALID_PACKET;
90 /* possible encodings are
95 YYYYMMDDhhmmss.fs+hh'mm'
96 YYYYMMDDhhmmss.fs-hh'mm'
98 So let's do a trivial decode upto [including] ss
102 DECODE_V4(out->YYYY, 10000);
103 DECODE_V(out->MM, 13);
104 DECODE_V(out->DD, 32);
105 DECODE_V(out->hh, 24);
106 DECODE_V(out->mm, 60);
107 DECODE_V(out->ss, 60);
109 /* clear fractional seconds info */
112 /* now is it Z or . */
115 } else if (buf[x] == '.') {
117 while (buf[x] >= '0' && buf[x] <= '9') {
118 unsigned fs = out->fs;
119 if (x >= sizeof(buf)) return CRYPT_INVALID_PACKET;
121 out->fs += _char_to_int(buf[x]);
122 if (fs > out->fs) return CRYPT_OVERFLOW;
127 /* now is it Z, +, - */
130 } else if (buf[x] == '+' || buf[x] == '-') {
131 out->off_dir = (buf[x++] == '+') ? 0 : 1;
132 DECODE_V(out->off_hh, 24);
133 DECODE_V(out->off_mm, 60);
136 return CRYPT_INVALID_PACKET;
142 /* ref: $Format:%D$ */
143 /* git commit: $Format:%H$ */
144 /* commit time: $Format:%ai$ */