| 1 |
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 |
/* SPDX-License-Identifier: Unlicense */ |
| 3 |
#include "tomcrypt_private.h" |
| 4 |
|
| 5 |
/** |
| 6 |
@file der_encode_utctime.c |
| 7 |
ASN.1 DER, encode a GeneralizedTime, Steffen Jaeckel |
| 8 |
Based on der_encode_utctime.c |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifdef LTC_DER |
| 12 |
|
| 13 |
#define STORE_V(y) do {\ |
| 14 |
out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ |
| 15 |
out[x++] = der_ia5_char_encode(baseten[y % 10]); \ |
| 16 |
} while(0) |
| 17 |
|
| 18 |
#define STORE_V4(y) do {\ |
| 19 |
out[x++] = der_ia5_char_encode(baseten[(y/1000) % 10]); \ |
| 20 |
out[x++] = der_ia5_char_encode(baseten[(y/100) % 10]); \ |
| 21 |
out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ |
| 22 |
out[x++] = der_ia5_char_encode(baseten[y % 10]); \ |
| 23 |
} while(0) |
| 24 |
|
| 25 |
/** |
| 26 |
Encodes a Generalized time structure in DER format |
| 27 |
@param gtime The GeneralizedTime structure to encode |
| 28 |
@param out The destination of the DER encoding of the GeneralizedTime structure |
| 29 |
@param outlen [in/out] The length of the DER encoding |
| 30 |
@return CRYPT_OK if successful |
| 31 |
*/ |
| 32 |
int der_encode_generalizedtime(const ltc_generalizedtime *gtime, |
| 33 |
unsigned char *out, unsigned long *outlen) |
| 34 |
{ |
| 35 |
const char * const baseten = "0123456789"; |
| 36 |
unsigned long x, tmplen; |
| 37 |
int err; |
| 38 |
|
| 39 |
LTC_ARGCHK(gtime != NULL); |
| 40 |
LTC_ARGCHK(out != NULL); |
| 41 |
LTC_ARGCHK(outlen != NULL); |
| 42 |
|
| 43 |
if ((err = der_length_generalizedtime(gtime, &tmplen)) != CRYPT_OK) { |
| 44 |
return err; |
| 45 |
} |
| 46 |
if (tmplen > *outlen) { |
| 47 |
*outlen = tmplen; |
| 48 |
return CRYPT_BUFFER_OVERFLOW; |
| 49 |
} |
| 50 |
|
| 51 |
/* store header */ |
| 52 |
out[0] = 0x18; |
| 53 |
|
| 54 |
/* store values */ |
| 55 |
x = 2; |
| 56 |
STORE_V4(gtime->YYYY); |
| 57 |
STORE_V(gtime->MM); |
| 58 |
STORE_V(gtime->DD); |
| 59 |
STORE_V(gtime->hh); |
| 60 |
STORE_V(gtime->mm); |
| 61 |
STORE_V(gtime->ss); |
| 62 |
|
| 63 |
if (gtime->fs) { |
| 64 |
unsigned long divisor; |
| 65 |
unsigned fs = gtime->fs; |
| 66 |
unsigned len = 0; |
| 67 |
out[x++] = der_ia5_char_encode('.'); |
| 68 |
divisor = 1; |
| 69 |
do { |
| 70 |
fs /= 10; |
| 71 |
divisor *= 10; |
| 72 |
len++; |
| 73 |
} while(fs != 0); |
| 74 |
while (len-- > 1) { |
| 75 |
divisor /= 10; |
| 76 |
out[x++] = der_ia5_char_encode(baseten[(gtime->fs/divisor) % 10]); |
| 77 |
} |
| 78 |
out[x++] = der_ia5_char_encode(baseten[gtime->fs % 10]); |
| 79 |
} |
| 80 |
|
| 81 |
if (gtime->off_mm || gtime->off_hh) { |
| 82 |
out[x++] = der_ia5_char_encode(gtime->off_dir ? '-' : '+'); |
| 83 |
STORE_V(gtime->off_hh); |
| 84 |
STORE_V(gtime->off_mm); |
| 85 |
} else { |
| 86 |
out[x++] = der_ia5_char_encode('Z'); |
| 87 |
} |
| 88 |
|
| 89 |
/* store length */ |
| 90 |
out[1] = (unsigned char)(x - 2); |
| 91 |
|
| 92 |
/* all good let's return */ |
| 93 |
*outlen = x; |
| 94 |
return CRYPT_OK; |
| 95 |
} |
| 96 |
|
| 97 |
#undef STORE_V |
| 98 |
#undef STORE_V4 |
| 99 |
|
| 100 |
#endif |