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_utctime.c
13 ASN.1 DER, decode a UTCTIME, Tom St Denis
18 static int _char_to_int(unsigned char x)
35 #define DECODE_V(y, max) \
36 y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
37 if (y >= max) return CRYPT_INVALID_PACKET; \
41 Decodes a UTC time structure in DER format (reads all 6 valid encoding formats)
42 @param in Input buffer
43 @param inlen Length of input buffer in octets
44 @param out [out] Destination of UTC time structure
45 @return CRYPT_OK if successful
47 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
50 unsigned char buf[32] = { 0 }; /* initialize as all zeroes */
54 LTC_ARGCHK(in != NULL);
55 LTC_ARGCHK(inlen != NULL);
56 LTC_ARGCHK(out != NULL);
59 if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
60 return CRYPT_INVALID_PACKET;
63 /* decode the string */
64 for (x = 0; x < in[1]; x++) {
65 y = der_ia5_value_decode(in[x+2]);
67 return CRYPT_INVALID_PACKET;
74 /* possible encodings are
82 So let's do a trivial decode upto [including] mm
86 DECODE_V(out->YY, 100);
87 DECODE_V(out->MM, 13);
88 DECODE_V(out->DD, 32);
89 DECODE_V(out->hh, 24);
90 DECODE_V(out->mm, 60);
92 /* clear timezone and seconds info */
93 out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
95 /* now is it Z, +, - or 0-9 */
98 } else if (buf[x] == '+' || buf[x] == '-') {
99 out->off_dir = (buf[x++] == '+') ? 0 : 1;
100 DECODE_V(out->off_hh, 24);
101 DECODE_V(out->off_mm, 60);
106 DECODE_V(out->ss, 60);
108 /* now is it Z, +, - */
111 } else if (buf[x] == '+' || buf[x] == '-') {
112 out->off_dir = (buf[x++] == '+') ? 0 : 1;
113 DECODE_V(out->off_hh, 24);
114 DECODE_V(out->off_mm, 60);
117 return CRYPT_INVALID_PACKET;
123 /* ref: $Format:%D$ */
124 /* git commit: $Format:%H$ */
125 /* commit time: $Format:%ai$ */