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 UTCTIME, Tom St Denis 8 */ 9 10 #ifdef LTC_DER 11 12 static const char * const baseten = "0123456789"; 13 14 #define STORE_V(y) \ 15 out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ 16 out[x++] = der_ia5_char_encode(baseten[y % 10]); 17 18 /** 19 Encodes a UTC time structure in DER format 20 @param utctime The UTC time structure to encode 21 @param out The destination of the DER encoding of the UTC time structure 22 @param outlen [in/out] The length of the DER encoding 23 @return CRYPT_OK if successful 24 */ der_encode_utctime(const ltc_utctime * utctime,unsigned char * out,unsigned long * outlen)25int der_encode_utctime(const ltc_utctime *utctime, 26 unsigned char *out, unsigned long *outlen) 27 { 28 unsigned long x, tmplen; 29 int err; 30 31 LTC_ARGCHK(utctime != NULL); 32 LTC_ARGCHK(out != NULL); 33 LTC_ARGCHK(outlen != NULL); 34 35 if ((err = der_length_utctime(utctime, &tmplen)) != CRYPT_OK) { 36 return err; 37 } 38 if (tmplen > *outlen) { 39 *outlen = tmplen; 40 return CRYPT_BUFFER_OVERFLOW; 41 } 42 43 /* store header */ 44 out[0] = 0x17; 45 46 /* store values */ 47 x = 2; 48 STORE_V(utctime->YY); 49 STORE_V(utctime->MM); 50 STORE_V(utctime->DD); 51 STORE_V(utctime->hh); 52 STORE_V(utctime->mm); 53 STORE_V(utctime->ss); 54 55 if (utctime->off_mm || utctime->off_hh) { 56 out[x++] = der_ia5_char_encode(utctime->off_dir ? '-' : '+'); 57 STORE_V(utctime->off_hh); 58 STORE_V(utctime->off_mm); 59 } else { 60 out[x++] = der_ia5_char_encode('Z'); 61 } 62 63 /* store length */ 64 out[1] = (unsigned char)(x - 2); 65 66 /* all good let's return */ 67 *outlen = x; 68 return CRYPT_OK; 69 } 70 71 #endif 72