Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_gentm.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_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) {
16 if (d->type != V_ASN1_GENERALIZEDTIME) {
17 return 0;
18 }
19 CBS cbs;
20 CBS_init(&cbs, d->data, (size_t)d->length);
21 if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
22 return 0;
23 }
24 return 1;
25 }
26
27 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
28 return asn1_generalizedtime_to_tm(NULL, d);
29 }
30
31 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
32 size_t len = strlen(str);
33 CBS cbs;
34 CBS_init(&cbs, (const uint8_t *)str, len);
35 if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/NULL,
36 /*allow_timezone_offset=*/0)) {
37 return 0;
38 }
39 if (s != NULL) {
40 if (!ASN1_STRING_set(s, str, len)) {
41 return 0;
42 }
43 s->type = V_ASN1_GENERALIZEDTIME;
44 }
45 return 1;
46 }
47
48 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
49 int64_t posix_time) {
50 return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
51 }
52
53 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
54 int64_t posix_time, int offset_day,
55 long offset_sec) {
56 struct tm data;
57 if (!OPENSSL_posix_to_tm(posix_time, &data)) {
58 return NULL;
59 }
60
61 if (offset_day || offset_sec) {
62 if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
63 return NULL;
64 }
65 }
66
67 if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
68 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
69 return NULL;
70 }
71
72 char buf[16];
73 int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
74 data.tm_year + 1900, data.tm_mon + 1, data.tm_mday,
75 data.tm_hour, data.tm_min, data.tm_sec);
76 if (ret != (int)(sizeof(buf) - 1)) {
77 abort(); // |snprintf| should neither truncate nor write fewer bytes.
78 }
79
80 int free_s = 0;
81 if (s == NULL) {
82 free_s = 1;
83 s = ASN1_UTCTIME_new();
84 if (s == NULL) {
85 return NULL;
86 }
87 }
88
89 if (!ASN1_STRING_set(s, buf, strlen(buf))) {
90 if (free_s) {
91 ASN1_UTCTIME_free(s);
92 }
93 return NULL;
94 }
95 s->type = V_ASN1_GENERALIZEDTIME;
96 return s;
97 }