1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_decode_utctime.c
14   ASN.1 DER, decode a  UTCTIME, Tom St Denis
15 */
16 
17 #ifdef LTC_DER
18 
_char_to_int(unsigned char x)19 static int _char_to_int(unsigned char x)
20 {
21    switch (x)  {
22       case '0': return 0;
23       case '1': return 1;
24       case '2': return 2;
25       case '3': return 3;
26       case '4': return 4;
27       case '5': return 5;
28       case '6': return 6;
29       case '7': return 7;
30       case '8': return 8;
31       case '9': return 9;
32       default:  return 100;
33    }
34 }
35 
36 #define DECODE_V(y, max) \
37    y  = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
38    if (y >= max) return CRYPT_INVALID_PACKET;           \
39    x += 2;
40 
41 /**
42   Decodes a UTC time structure in DER format (reads all 6 valid encoding formats)
43   @param in     Input buffer
44   @param inlen  Length of input buffer in octets
45   @param out    [out] Destination of UTC time structure
46   @return CRYPT_OK   if successful
47 */
der_decode_utctime(const unsigned char * in,unsigned long * inlen,ltc_utctime * out)48 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
49                              ltc_utctime   *out)
50 {
51    unsigned char buf[32] = { 0 }; /* initialize as all zeroes */
52    unsigned long x;
53    int           y;
54 
55    LTC_ARGCHK(in    != NULL);
56    LTC_ARGCHK(inlen != NULL);
57    LTC_ARGCHK(out   != NULL);
58 
59    /* check header */
60    if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
61       return CRYPT_INVALID_PACKET;
62    }
63 
64    /* decode the string */
65    for (x = 0; x < in[1]; x++) {
66        y = der_ia5_value_decode(in[x+2]);
67        if (y == -1) {
68           return CRYPT_INVALID_PACKET;
69        }
70        buf[x] = y;
71    }
72    *inlen = 2 + x;
73 
74 
75    /* possible encodings are
76 YYMMDDhhmmZ
77 YYMMDDhhmm+hh'mm'
78 YYMMDDhhmm-hh'mm'
79 YYMMDDhhmmssZ
80 YYMMDDhhmmss+hh'mm'
81 YYMMDDhhmmss-hh'mm'
82 
83     So let's do a trivial decode upto [including] mm
84    */
85 
86     x = 0;
87     DECODE_V(out->YY, 100);
88     DECODE_V(out->MM, 13);
89     DECODE_V(out->DD, 32);
90     DECODE_V(out->hh, 24);
91     DECODE_V(out->mm, 60);
92 
93     /* clear timezone and seconds info */
94     out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
95 
96     /* now is it Z, +, - or 0-9 */
97     if (buf[x] == 'Z') {
98        return CRYPT_OK;
99     }
100     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);
104        return CRYPT_OK;
105     }
106 
107     /* decode seconds */
108     DECODE_V(out->ss, 60);
109 
110     /* now is it Z, +, - */
111     if (buf[x] == 'Z') {
112        return CRYPT_OK;
113     }
114     if (buf[x] == '+' || buf[x] == '-') {
115        out->off_dir = (buf[x++] == '+') ? 0 : 1;
116        DECODE_V(out->off_hh, 24);
117        DECODE_V(out->off_mm, 60);
118        return CRYPT_OK;
119     }
120     return CRYPT_INVALID_PACKET;
121 }
122 
123 #endif
124 
125 /* ref:         $Format:%D$ */
126 /* git commit:  $Format:%H$ */
127 /* commit time: $Format:%ai$ */
128