1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file der_encode_utctime.c
13 ASN.1 DER, encode a GeneralizedTime, Steffen Jaeckel
14 Based on der_encode_utctime.c
19 static const char * const baseten = "0123456789";
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]); \
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]); \
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
40 int der_encode_generalizedtime(ltc_generalizedtime *gtime,
41 unsigned char *out, unsigned long *outlen)
43 unsigned long x, tmplen;
46 LTC_ARGCHK(gtime != NULL);
47 LTC_ARGCHK(out != NULL);
48 LTC_ARGCHK(outlen != NULL);
50 if ((err = der_length_generalizedtime(gtime, &tmplen)) != CRYPT_OK) {
53 if (tmplen > *outlen) {
55 return CRYPT_BUFFER_OVERFLOW;
63 STORE_V4(gtime->YYYY);
71 unsigned long divisor;
72 unsigned fs = gtime->fs;
74 out[x++] = der_ia5_char_encode('.');
83 out[x++] = der_ia5_char_encode(baseten[(gtime->fs/divisor) % 10]);
85 out[x++] = der_ia5_char_encode(baseten[gtime->fs % 10]);
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);
93 out[x++] = der_ia5_char_encode('Z');
97 out[1] = (unsigned char)(x - 2);
99 /* all good let's return */
106 /* ref: $Format:%D$ */
107 /* git commit: $Format:%H$ */
108 /* commit time: $Format:%ai$ */