Mercurial > dropbear
comparison libtomcrypt/src/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
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_encode_utctime.c | |
13 ASN.1 DER, encode a GeneralizedTime, Steffen Jaeckel | |
14 Based on der_encode_utctime.c | |
15 */ | |
16 | |
17 #ifdef LTC_DER | |
18 | |
19 static const char * const baseten = "0123456789"; | |
20 | |
21 #define STORE_V(y) do {\ | |
22 out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ | |
23 out[x++] = der_ia5_char_encode(baseten[y % 10]); \ | |
24 } while(0) | |
25 | |
26 #define STORE_V4(y) do {\ | |
27 out[x++] = der_ia5_char_encode(baseten[(y/1000) % 10]); \ | |
28 out[x++] = der_ia5_char_encode(baseten[(y/100) % 10]); \ | |
29 out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ | |
30 out[x++] = der_ia5_char_encode(baseten[y % 10]); \ | |
31 } while(0) | |
32 | |
33 /** | |
34 Encodes a Generalized time structure in DER format | |
35 @param gtime The GeneralizedTime structure to encode | |
36 @param out The destination of the DER encoding of the GeneralizedTime structure | |
37 @param outlen [in/out] The length of the DER encoding | |
38 @return CRYPT_OK if successful | |
39 */ | |
40 int der_encode_generalizedtime(ltc_generalizedtime *gtime, | |
41 unsigned char *out, unsigned long *outlen) | |
42 { | |
43 unsigned long x, tmplen; | |
44 int err; | |
45 | |
46 LTC_ARGCHK(gtime != NULL); | |
47 LTC_ARGCHK(out != NULL); | |
48 LTC_ARGCHK(outlen != NULL); | |
49 | |
50 if ((err = der_length_generalizedtime(gtime, &tmplen)) != CRYPT_OK) { | |
51 return err; | |
52 } | |
53 if (tmplen > *outlen) { | |
54 *outlen = tmplen; | |
55 return CRYPT_BUFFER_OVERFLOW; | |
56 } | |
57 | |
58 /* store header */ | |
59 out[0] = 0x18; | |
60 | |
61 /* store values */ | |
62 x = 2; | |
63 STORE_V4(gtime->YYYY); | |
64 STORE_V(gtime->MM); | |
65 STORE_V(gtime->DD); | |
66 STORE_V(gtime->hh); | |
67 STORE_V(gtime->mm); | |
68 STORE_V(gtime->ss); | |
69 | |
70 if (gtime->fs) { | |
71 unsigned long divisor; | |
72 unsigned fs = gtime->fs; | |
73 unsigned len = 0; | |
74 out[x++] = der_ia5_char_encode('.'); | |
75 divisor = 1; | |
76 do { | |
77 fs /= 10; | |
78 divisor *= 10; | |
79 len++; | |
80 } while(fs != 0); | |
81 while (len-- > 1) { | |
82 divisor /= 10; | |
83 out[x++] = der_ia5_char_encode(baseten[(gtime->fs/divisor) % 10]); | |
84 } | |
85 out[x++] = der_ia5_char_encode(baseten[gtime->fs % 10]); | |
86 } | |
87 | |
88 if (gtime->off_mm || gtime->off_hh) { | |
89 out[x++] = der_ia5_char_encode(gtime->off_dir ? '-' : '+'); | |
90 STORE_V(gtime->off_hh); | |
91 STORE_V(gtime->off_mm); | |
92 } else { | |
93 out[x++] = der_ia5_char_encode('Z'); | |
94 } | |
95 | |
96 /* store length */ | |
97 out[1] = (unsigned char)(x - 2); | |
98 | |
99 /* all good let's return */ | |
100 *outlen = x; | |
101 return CRYPT_OK; | |
102 } | |
103 | |
104 #endif | |
105 | |
106 /* ref: $Format:%D$ */ | |
107 /* git commit: $Format:%H$ */ | |
108 /* commit time: $Format:%ai$ */ |