comparison libtomcrypt/src/pk/asn1/der/utctime/der_decode_utctime.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file der_decode_utctime.c
15 ASN.1 DER, decode a UTCTIME, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19
20 static int char_to_int(unsigned char x)
21 {
22 switch (x) {
23 case '0': return 0;
24 case '1': return 1;
25 case '2': return 2;
26 case '3': return 3;
27 case '4': return 4;
28 case '5': return 5;
29 case '6': return 6;
30 case '7': return 7;
31 case '8': return 8;
32 case '9': return 9;
33 }
34 return 100;
35 }
36
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; \
40 x += 2;
41
42 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
43 ltc_utctime *out)
44 {
45 unsigned char buf[32];
46 unsigned long x;
47 int y;
48
49 LTC_ARGCHK(in != NULL);
50 LTC_ARGCHK(inlen != NULL);
51 LTC_ARGCHK(out != NULL);
52
53 /* check header */
54 if (*inlen < 2UL || (in[1] >= sizeof(buf)) || ((in[1] + 2UL) > *inlen)) {
55 return CRYPT_INVALID_PACKET;
56 }
57
58 /* decode the string */
59 for (x = 0; x < in[1]; x++) {
60 y = der_ia5_value_decode(in[x+2]);
61 if (y == -1) {
62 return CRYPT_INVALID_PACKET;
63 }
64 buf[x] = y;
65 }
66 *inlen = 2 + x;
67
68
69 /* possible encodings are
70 YYMMDDhhmmZ
71 YYMMDDhhmm+hh'mm'
72 YYMMDDhhmm-hh'mm'
73 YYMMDDhhmmssZ
74 YYMMDDhhmmss+hh'mm'
75 YYMMDDhhmmss-hh'mm'
76
77 So let's do a trivial decode upto [including] mm
78 */
79
80 x = 0;
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);
86
87 /* clear timezone and seconds info */
88 out->off_dir = out->off_hh = out->off_mm = out->ss = 0;
89
90 /* now is it Z, +, - or 0-9 */
91 if (buf[x] == 'Z') {
92 return CRYPT_OK;
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);
97 return CRYPT_OK;
98 }
99
100 /* decode seconds */
101 DECODE_V(out->ss, 60);
102
103 /* now is it Z, +, - */
104 if (buf[x] == 'Z') {
105 return CRYPT_OK;
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);
110 return CRYPT_OK;
111 } else {
112 return CRYPT_INVALID_PACKET;
113 }
114 }
115
116 #endif
117
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 $ */