Debian Codegraph @

src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c

raw
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file der_decode_generalizedtime.c
7 ASN.1 DER, decode a GeneralizedTime, Steffen Jaeckel
8 Based on der_decode_utctime.c
9 */
10
11 #ifdef LTC_DER
12
13 #ifndef LTC_DER_CHAR_TO_INT
14 #define LTC_DER_CHAR_TO_INT
15 static LTC_INLINE int s_char_to_int(unsigned char x)
16 {
17 switch (x) {
18 case '0': return 0;
19 case '1': return 1;
20 case '2': return 2;
21 case '3': return 3;
22 case '4': return 4;
23 case '5': return 5;
24 case '6': return 6;
25 case '7': return 7;
26 case '8': return 8;
27 case '9': return 9;
28 default: return 100;
29 }
30 }
31 #endif
32
33 #define DECODE_V(y, max) do {\
34 y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \
35 if (y >= max) return CRYPT_INVALID_PACKET; \
36 x += 2; \
37 } while(0)
38
39 #define DECODE_V4(y, max) do {\
40 y = s_char_to_int(buf[x])*1000 + s_char_to_int(buf[x+1])*100 + s_char_to_int(buf[x+2])*10 + s_char_to_int(buf[x+3]); \
41 if (y >= max) return CRYPT_INVALID_PACKET; \
42 x += 4; \
43 } while(0)
44
45 /**
46 Decodes a Generalized time structure in DER format (reads all 6 valid encoding formats)
47 @param in Input buffer
48 @param inlen Length of input buffer in octets
49 @param out [out] Destination of Generalized time structure
50 @return CRYPT_OK if successful
51 */
52 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
53 ltc_generalizedtime *out)
54 {
55 unsigned char buf[32];
56 unsigned long x;
57 int y;
58
59 LTC_ARGCHK(in != NULL);
60 LTC_ARGCHK(inlen != NULL);
61 LTC_ARGCHK(out != NULL);
62
63 /* check header */
64 if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
65 return CRYPT_INVALID_PACKET;
66 }
67
68 /* decode the string */
69 for (x = 0; x < in[1]; x++) {
70 y = der_ia5_value_decode(in[x+2]);
71 if (y == -1) {
72 return CRYPT_INVALID_PACKET;
73 }
74 if (!((y >= '0' && y <= '9')
75 || y == 'Z' || y == '.'
76 || y == '+' || y == '-')) {
77 return CRYPT_INVALID_PACKET;
78 }
79 buf[x] = y;
80 }
81 *inlen = 2 + x;
82
83 if (x < 15) {
84 return CRYPT_INVALID_PACKET;
85 }
86
87 /* possible encodings are
88 YYYYMMDDhhmmssZ
89 YYYYMMDDhhmmss+hh'mm'
90 YYYYMMDDhhmmss-hh'mm'
91 YYYYMMDDhhmmss.fsZ
92 YYYYMMDDhhmmss.fs+hh'mm'
93 YYYYMMDDhhmmss.fs-hh'mm'
94
95 So let's do a trivial decode upto [including] ss
96 */
97
98 x = 0;
99 DECODE_V4(out->YYYY, 10000);
100 DECODE_V(out->MM, 13);
101 DECODE_V(out->DD, 32);
102 DECODE_V(out->hh, 24);
103 DECODE_V(out->mm, 60);
104 DECODE_V(out->ss, 60);
105
106 /* clear fractional seconds info */
107 out->fs = 0;
108
109 /* now is it Z or . */
110 if (buf[x] == 'Z') {
111 return CRYPT_OK;
112 }
113 if (buf[x] == '.') {
114 x++;
115 while (buf[x] >= '0' && buf[x] <= '9') {
116 unsigned fs = out->fs;
117 if (x >= sizeof(buf)) return CRYPT_INVALID_PACKET;
118 out->fs *= 10;
119 out->fs += s_char_to_int(buf[x]);
120 if (fs > out->fs) return CRYPT_OVERFLOW;
121 x++;
122 }
123 }
124
125 /* now is it Z, +, - */
126 if (buf[x] == 'Z') {
127 return CRYPT_OK;
128 }
129 if (buf[x] == '+' || buf[x] == '-') {
130 out->off_dir = (buf[x++] == '+') ? 0 : 1;
131 DECODE_V(out->off_hh, 24);
132 DECODE_V(out->off_mm, 60);
133 return CRYPT_OK;
134 }
135 return CRYPT_INVALID_PACKET;
136 }
137
138 #undef DECODE_V
139 #undef DECODE_V4
140
141 #endif