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