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_utctime_to_tm(struct tm * tm,const ASN1_UTCTIME * d,int allow_timezone_offset)27 int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
28 int allow_timezone_offset) {
29 if (d->type != V_ASN1_UTCTIME) {
30 return 0;
31 }
32 CBS cbs;
33 CBS_init(&cbs, d->data, (size_t)d->length);
34 if (!CBS_parse_utc_time(&cbs, tm, allow_timezone_offset)) {
35 return 0;
36 }
37 return 1;
38 }
39
ASN1_UTCTIME_check(const ASN1_UTCTIME * d)40 int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
41 return asn1_utctime_to_tm(NULL, d, /*allow_timezone_offset=*/1);
42 }
43
ASN1_UTCTIME_set_string(ASN1_UTCTIME * s,const char * str)44 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
45 // Although elsewhere we allow timezone offsets with UTCTime, to be compatible
46 // with some existing misissued certificates, this function is used to
47 // construct new certificates and can be stricter.
48 size_t len = strlen(str);
49 CBS cbs;
50 CBS_init(&cbs, (const uint8_t *)str, len);
51 if (!CBS_parse_utc_time(&cbs, /*out_tm=*/NULL,
52 /*allow_timezone_offset=*/0)) {
53 return 0;
54 }
55 if (s != NULL) {
56 if (!ASN1_STRING_set(s, str, len)) {
57 return 0;
58 }
59 s->type = V_ASN1_UTCTIME;
60 }
61 return 1;
62 }
63
ASN1_UTCTIME_set(ASN1_UTCTIME * s,int64_t posix_time)64 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) {
65 return ASN1_UTCTIME_adj(s, posix_time, 0, 0);
66 }
67
ASN1_UTCTIME_adj(ASN1_UTCTIME * s,int64_t posix_time,int offset_day,long offset_sec)68 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time,
69 int offset_day, long offset_sec) {
70 struct tm data;
71 if (!OPENSSL_posix_to_tm(posix_time, &data)) {
72 return NULL;
73 }
74
75 if (offset_day || offset_sec) {
76 if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
77 return NULL;
78 }
79 }
80
81 if (data.tm_year < 50 || data.tm_year >= 150) {
82 return NULL;
83 }
84
85 char buf[14];
86 int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
87 data.tm_year % 100, 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_UTCTIME;
108 return s;
109 }
110