Refactor ASN1_TIME_print functions
[openssl.git] / crypto / asn1 / a_gentm.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * GENERALIZEDTIME implementation. Based on UTCTIME
12  */
13
14 #include <stdio.h>
15 #include <time.h>
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1.h>
18 #include "asn1_locl.h"
19
20 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
21 {
22     /* wrapper around asn1_time_to_tm */
23     if (d->type != V_ASN1_GENERALIZEDTIME)
24         return 0;
25     return asn1_time_to_tm(tm, d);
26 }
27
28 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
29 {
30     return asn1_generalizedtime_to_tm(NULL, d);
31 }
32
33 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
34 {
35     ASN1_GENERALIZEDTIME t;
36
37     t.type = V_ASN1_GENERALIZEDTIME;
38     t.length = strlen(str);
39     t.data = (unsigned char *)str;
40     t.flags = 0;
41
42     if (ASN1_GENERALIZEDTIME_check(&t)) {
43         if (s != NULL) {
44             if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
45                 return 0;
46             s->type = V_ASN1_GENERALIZEDTIME;
47         }
48         return 1;
49     }
50     return 0;
51 }
52
53 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
54                                                time_t t)
55 {
56     return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
57 }
58
59 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
60                                                time_t t, int offset_day,
61                                                long offset_sec)
62 {
63     char *p;
64     struct tm *ts;
65     struct tm data;
66     const size_t len = 20;
67     ASN1_GENERALIZEDTIME *tmps = NULL;
68
69     if (s == NULL)
70         tmps = ASN1_GENERALIZEDTIME_new();
71     else
72         tmps = s;
73     if (tmps == NULL)
74         return NULL;
75
76     ts = OPENSSL_gmtime(&t, &data);
77     if (ts == NULL)
78         goto err;
79
80     if (offset_day || offset_sec) {
81         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
82             goto err;
83     }
84
85     p = (char *)tmps->data;
86     if ((p == NULL) || ((size_t)tmps->length < len)) {
87         p = OPENSSL_malloc(len);
88         if (p == NULL) {
89             ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
90             goto err;
91         }
92         OPENSSL_free(tmps->data);
93         tmps->data = (unsigned char *)p;
94     }
95
96     tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
97                                 ts->tm_year + 1900, ts->tm_mon + 1,
98                                 ts->tm_mday, ts->tm_hour, ts->tm_min,
99                                 ts->tm_sec);
100     tmps->type = V_ASN1_GENERALIZEDTIME;
101 #ifdef CHARSET_EBCDIC_not
102     ebcdic2ascii(tmps->data, tmps->data, tmps->length);
103 #endif
104     return tmps;
105  err:
106     if (s == NULL)
107         ASN1_GENERALIZEDTIME_free(tmps);
108     return NULL;
109 }
110
111 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
112 {
113     if (tm->type != V_ASN1_GENERALIZEDTIME)
114         return 0;
115     return ASN1_TIME_print(bp, tm);
116 }