version 0.3.33
[fms.git] / libs / libtomcrypt / 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  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12
13 /**
14   @file der_encode_utctime.c
15   ASN.1 DER, encode a  UTCTIME, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19
20 static const char *baseten = "0123456789";
21
22 #define STORE_V(y) \
23     out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \
24     out[x++] = der_ia5_char_encode(baseten[y % 10]);
25
26 /**
27   Encodes a UTC time structure in DER format
28   @param utctime      The UTC time structure to encode
29   @param out          The destination of the DER encoding of the UTC time structure
30   @param outlen       [in/out] The length of the DER encoding
31   @return CRYPT_OK if successful
32 */
33 int der_encode_utctime(ltc_utctime *utctime, 
34                        unsigned char *out,   unsigned long *outlen)
35 {
36     unsigned long x, tmplen;
37     int           err;
38  
39     LTC_ARGCHK(utctime != NULL);
40     LTC_ARGCHK(out     != NULL);
41     LTC_ARGCHK(outlen  != NULL);
42
43     if ((err = der_length_utctime(utctime, &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] = 0x17;
53
54     /* store values */
55     x = 2;
56     STORE_V(utctime->YY);
57     STORE_V(utctime->MM);
58     STORE_V(utctime->DD);
59     STORE_V(utctime->hh);
60     STORE_V(utctime->mm);
61     STORE_V(utctime->ss);
62
63     if (utctime->off_mm || utctime->off_hh) {
64        out[x++] = der_ia5_char_encode(utctime->off_dir ? '-' : '+');
65        STORE_V(utctime->off_hh);
66        STORE_V(utctime->off_mm);
67     } else {
68        out[x++] = der_ia5_char_encode('Z');
69     }
70
71     /* store length */
72     out[1] = (unsigned char)(x - 2);
73    
74     /* all good let's return */
75     *outlen = x;
76     return CRYPT_OK;
77 }
78
79 #endif
80
81 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/utctime/der_encode_utctime.c,v $ */
82 /* $Revision: 1.9 $ */
83 /* $Date: 2006/12/04 21:34:03 $ */