1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <openssl/asn1.h>
16 #include <openssl/bytestring.h>
17 #include <openssl/err.h>
18 #include <openssl/mem.h>
19 #include <openssl/posix_time.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24
25 #include "internal.h"
26
asn1_generalizedtime_to_tm(struct tm * tm,const ASN1_GENERALIZEDTIME * d)27 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) {
28 if (d->type != V_ASN1_GENERALIZEDTIME) {
29 return 0;
30 }
31 CBS cbs;
32 CBS_init(&cbs, d->data, (size_t)d->length);
33 if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
34 return 0;
35 }
36 return 1;
37 }
38
ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME * d)39 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
40 return asn1_generalizedtime_to_tm(NULL, d);
41 }
42
ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME * s,const char * str)43 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
44 size_t len = strlen(str);
45 CBS cbs;
46 CBS_init(&cbs, (const uint8_t *)str, len);
47 if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/NULL,
48 /*allow_timezone_offset=*/0)) {
49 return 0;
50 }
51 if (s != NULL) {
52 if (!ASN1_STRING_set(s, str, len)) {
53 return 0;
54 }
55 s->type = V_ASN1_GENERALIZEDTIME;
56 }
57 return 1;
58 }
59
ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME * s,int64_t posix_time)60 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
61 int64_t posix_time) {
62 return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
63 }
64
ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME * s,int64_t posix_time,int offset_day,long offset_sec)65 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
66 int64_t posix_time,
67 int offset_day,
68 long offset_sec) {
69 struct tm data;
70 if (!OPENSSL_posix_to_tm(posix_time, &data)) {
71 return NULL;
72 }
73
74 if (offset_day || offset_sec) {
75 if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
76 return NULL;
77 }
78 }
79
80 if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
81 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
82 return NULL;
83 }
84
85 char buf[16];
86 int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
87 data.tm_year + 1900, data.tm_mon + 1, data.tm_mday,
88 data.tm_hour, data.tm_min, data.tm_sec);
89 // |snprintf| must write exactly 15 bytes (plus the NUL) to the buffer.
90 BSSL_CHECK(ret == static_cast<int>(sizeof(buf) - 1));
91
92 int free_s = 0;
93 if (s == NULL) {
94 free_s = 1;
95 s = ASN1_UTCTIME_new();
96 if (s == NULL) {
97 return NULL;
98 }
99 }
100
101 if (!ASN1_STRING_set(s, buf, strlen(buf))) {
102 if (free_s) {
103 ASN1_UTCTIME_free(s);
104 }
105 return NULL;
106 }
107 s->type = V_ASN1_GENERALIZEDTIME;
108 return s;
109 }
110