]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/utctime/der_decode_utctime.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / utctime / der_decode_utctime.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file der_decode_utctime.c
13   ASN.1 DER, decode a  UTCTIME, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17
18 static int _char_to_int(unsigned char x)
19 {
20    switch (x)  {
21       case '0': return 0;
22       case '1': return 1;
23       case '2': return 2;
24       case '3': return 3;
25       case '4': return 4;
26       case '5': return 5;
27       case '6': return 6;
28       case '7': return 7;
29       case '8': return 8;
30       case '9': return 9;
31       default:  return 100;
32    }
33 }
34
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;           \
38    x += 2;
39
40 /**
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
46 */
47 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
48                              ltc_utctime   *out)
49 {
50    unsigned char buf[32] = { 0 }; /* initialize as all zeroes */
51    unsigned long x;
52    int           y;
53
54    LTC_ARGCHK(in    != NULL);
55    LTC_ARGCHK(inlen != NULL);
56    LTC_ARGCHK(out   != NULL);
57
58    /* check header */
59    if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
60       return CRYPT_INVALID_PACKET;
61    }
62
63    /* decode the string */
64    for (x = 0; x < in[1]; x++) {
65        y = der_ia5_value_decode(in[x+2]);
66        if (y == -1) {
67           return CRYPT_INVALID_PACKET;
68        }
69        buf[x] = y;
70    }
71    *inlen = 2 + x;
72
73
74    /* possible encodings are
75 YYMMDDhhmmZ
76 YYMMDDhhmm+hh'mm'
77 YYMMDDhhmm-hh'mm'
78 YYMMDDhhmmssZ
79 YYMMDDhhmmss+hh'mm'
80 YYMMDDhhmmss-hh'mm'
81
82     So let's do a trivial decode upto [including] mm
83    */
84
85     x = 0;
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);
91
92     /* clear timezone and seconds info */
93     out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
94
95     /* now is it Z, +, - or 0-9 */
96     if (buf[x] == 'Z') {
97        return CRYPT_OK;
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);
102        return CRYPT_OK;
103     }
104
105     /* decode seconds */
106     DECODE_V(out->ss, 60);
107
108     /* now is it Z, +, - */
109     if (buf[x] == 'Z') {
110        return CRYPT_OK;
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);
115        return CRYPT_OK;
116     } else {
117        return CRYPT_INVALID_PACKET;
118     }
119 }
120
121 #endif
122
123 /* ref:         $Format:%D$ */
124 /* git commit:  $Format:%H$ */
125 /* commit time: $Format:%ai$ */