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.org
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; \
42
int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
45
unsigned char buf[32];
49
LTC_ARGCHK(in != NULL);
50
LTC_ARGCHK(inlen != NULL);
51
LTC_ARGCHK(out != NULL);
54
if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
55
return CRYPT_INVALID_PACKET;
58
/* decode the string */
59
for (x = 0; x < in[1]; x++) {
60
y = der_ia5_value_decode(in[x+2]);
62
return CRYPT_INVALID_PACKET;
69
/* possible encodings are
77
So let's do a trivial decode upto [including] mm
81
DECODE_V(out->YY, 100);
82
DECODE_V(out->MM, 13);
83
DECODE_V(out->DD, 32);
84
DECODE_V(out->hh, 24);
85
DECODE_V(out->mm, 60);
87
/* clear timezone and seconds info */
88
out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
90
/* now is it Z, +, - or 0-9 */
93
} else if (buf[x] == '+' || buf[x] == '-') {
94
out->off_dir = (buf[x++] == '+') ? 0 : 1;
95
DECODE_V(out->off_hh, 24);
96
DECODE_V(out->off_mm, 60);
101
DECODE_V(out->ss, 60);
103
/* now is it Z, +, - */
106
} else if (buf[x] == '+' || buf[x] == '-') {
107
out->off_dir = (buf[x++] == '+') ? 0 : 1;
108
DECODE_V(out->off_hh, 24);
109
DECODE_V(out->off_mm, 60);
112
return CRYPT_INVALID_PACKET;
118
/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/utctime/der_decode_utctime.c,v $ */
119
/* $Revision: 1.6 $ */
120
/* $Date: 2005/06/19 12:07:00 $ */