/src/boringssl/crypto/asn1/a_utctm.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
27 | | int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d, |
28 | 0 | int allow_timezone_offset) { |
29 | 0 | if (d->type != V_ASN1_UTCTIME) { |
30 | 0 | return 0; |
31 | 0 | } |
32 | 0 | CBS cbs; |
33 | 0 | CBS_init(&cbs, d->data, (size_t)d->length); |
34 | 0 | if (!CBS_parse_utc_time(&cbs, tm, allow_timezone_offset)) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | 0 | return 1; |
38 | 0 | } |
39 | | |
40 | 0 | int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) { |
41 | 0 | return asn1_utctime_to_tm(NULL, d, /*allow_timezone_offset=*/1); |
42 | 0 | } |
43 | | |
44 | 0 | 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 | 0 | size_t len = strlen(str); |
49 | 0 | CBS cbs; |
50 | 0 | CBS_init(&cbs, (const uint8_t *)str, len); |
51 | 0 | if (!CBS_parse_utc_time(&cbs, /*out_tm=*/NULL, |
52 | 0 | /*allow_timezone_offset=*/0)) { |
53 | 0 | return 0; |
54 | 0 | } |
55 | 0 | if (s != NULL) { |
56 | 0 | if (!ASN1_STRING_set(s, str, len)) { |
57 | 0 | return 0; |
58 | 0 | } |
59 | 0 | s->type = V_ASN1_UTCTIME; |
60 | 0 | } |
61 | 0 | return 1; |
62 | 0 | } |
63 | | |
64 | 0 | ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) { |
65 | 0 | return ASN1_UTCTIME_adj(s, posix_time, 0, 0); |
66 | 0 | } |
67 | | |
68 | | ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, |
69 | 0 | int offset_day, long offset_sec) { |
70 | 0 | struct tm data; |
71 | 0 | if (!OPENSSL_posix_to_tm(posix_time, &data)) { |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 0 | if (offset_day || offset_sec) { |
76 | 0 | if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) { |
77 | 0 | return NULL; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | 0 | if (data.tm_year < 50 || data.tm_year >= 150) { |
82 | 0 | return NULL; |
83 | 0 | } |
84 | | |
85 | 0 | char buf[14]; |
86 | 0 | int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ", |
87 | 0 | data.tm_year % 100, data.tm_mon + 1, data.tm_mday, |
88 | 0 | data.tm_hour, data.tm_min, data.tm_sec); |
89 | | // |snprintf| must write exactly 15 bytes (plus the NUL) to the buffer. |
90 | 0 | BSSL_CHECK(ret == static_cast<int>(sizeof(buf) - 1)); |
91 | |
|
92 | 0 | int free_s = 0; |
93 | 0 | if (s == NULL) { |
94 | 0 | free_s = 1; |
95 | 0 | s = ASN1_UTCTIME_new(); |
96 | 0 | if (s == NULL) { |
97 | 0 | return NULL; |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | 0 | if (!ASN1_STRING_set(s, buf, strlen(buf))) { |
102 | 0 | if (free_s) { |
103 | 0 | ASN1_UTCTIME_free(s); |
104 | 0 | } |
105 | 0 | return NULL; |
106 | 0 | } |
107 | 0 | s->type = V_ASN1_UTCTIME; |
108 | 0 | return s; |
109 | 0 | } |