Debian Codegraph @

src/ltc/pk/asn1/der/utctime/der_encode_utctime.c

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