Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_utctm.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5 #include <openssl/bytestring.h>
6 #include <openssl/err.h>
7 #include <openssl/mem.h>
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12
13 #include "internal.h"
14
15 int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
16 int allow_timezone_offset) {
17 if (d->type != V_ASN1_UTCTIME) {
18 return 0;
19 }
20 CBS cbs;
21 CBS_init(&cbs, d->data, (size_t)d->length);
22 if (!CBS_parse_utc_time(&cbs, tm, allow_timezone_offset)) {
23 return 0;
24 }
25 return 1;
26 }
27
28 int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
29 return asn1_utctime_to_tm(NULL, d, /*allow_timezone_offset=*/1);
30 }
31
32 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
33 // Although elsewhere we allow timezone offsets with UTCTime, to be compatible
34 // with some existing misissued certificates, this function is used to
35 // construct new certificates and can be stricter.
36 size_t len = strlen(str);
37 CBS cbs;
38 CBS_init(&cbs, (const uint8_t *)str, len);
39 if (!CBS_parse_utc_time(&cbs, /*out_tm=*/NULL,
40 /*allow_timezone_offset=*/0)) {
41 return 0;
42 }
43 if (s != NULL) {
44 if (!ASN1_STRING_set(s, str, len)) {
45 return 0;
46 }
47 s->type = V_ASN1_UTCTIME;
48 }
49 return 1;
50 }
51
52 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) {
53 return ASN1_UTCTIME_adj(s, posix_time, 0, 0);
54 }
55
56 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int offset_day,
57 long offset_sec) {
58 struct tm data;
59 if (!OPENSSL_posix_to_tm(posix_time, &data)) {
60 return NULL;
61 }
62
63 if (offset_day || offset_sec) {
64 if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
65 return NULL;
66 }
67 }
68
69 if (data.tm_year < 50 || data.tm_year >= 150) {
70 return NULL;
71 }
72
73 char buf[14];
74 int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
75 data.tm_year % 100, data.tm_mon + 1, data.tm_mday,
76 data.tm_hour, data.tm_min, data.tm_sec);
77 if (ret != (int)(sizeof(buf) - 1)) {
78 abort(); // |snprintf| should neither truncate nor write fewer bytes.
79 }
80
81 int free_s = 0;
82 if (s == NULL) {
83 free_s = 1;
84 s = ASN1_UTCTIME_new();
85 if (s == NULL) {
86 return NULL;
87 }
88 }
89
90 if (!ASN1_STRING_set(s, buf, strlen(buf))) {
91 if (free_s) {
92 ASN1_UTCTIME_free(s);
93 }
94 return NULL;
95 }
96 s->type = V_ASN1_UTCTIME;
97 return s;
98 }
99
100 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) {
101 struct tm stm, ttm;
102 int day, sec;
103
104 if (!asn1_utctime_to_tm(&stm, s, /*allow_timezone_offset=*/1)) {
105 return -2;
106 }
107
108 if (!OPENSSL_posix_to_tm(t, &ttm)) {
109 return -2;
110 }
111
112 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) {
113 return -2;
114 }
115
116 if (day > 0) {
117 return 1;
118 }
119 if (day < 0) {
120 return -1;
121 }
122 if (sec > 0) {
123 return 1;
124 }
125 if (sec < 0) {
126 return -1;
127 }
128 return 0;
129 }