| 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 |
|
| 6 |
#include <string.h> |
| 7 |
#include <time.h> |
| 8 |
|
| 9 |
#include <openssl/asn1t.h> |
| 10 |
#include <openssl/bytestring.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
// This is an implementation of the ASN1 Time structure which is: Time ::= |
| 17 |
// CHOICE { utcTime UTCTime, generalTime GeneralizedTime } written by Steve |
| 18 |
// Henson. |
| 19 |
|
| 20 |
IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME) |
| 21 |
|
| 22 |
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME) |
| 23 |
|
| 24 |
ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time) { |
| 25 |
return ASN1_TIME_adj(s, posix_time, 0, 0); |
| 26 |
} |
| 27 |
|
| 28 |
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time) { |
| 29 |
return ASN1_TIME_adj(s, time, 0, 0); |
| 30 |
} |
| 31 |
|
| 32 |
static int fits_in_utc_time(const struct tm *tm) { |
| 33 |
return 50 <= tm->tm_year && tm->tm_year < 150; |
| 34 |
} |
| 35 |
|
| 36 |
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, int offset_day, |
| 37 |
long offset_sec) { |
| 38 |
struct tm tm; |
| 39 |
|
| 40 |
if (!OPENSSL_posix_to_tm(posix_time, &tm)) { |
| 41 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
if (offset_day || offset_sec) { |
| 45 |
if (!OPENSSL_gmtime_adj(&tm, offset_day, offset_sec)) { |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
} |
| 49 |
if (fits_in_utc_time(&tm)) { |
| 50 |
return ASN1_UTCTIME_adj(s, posix_time, offset_day, offset_sec); |
| 51 |
} |
| 52 |
return ASN1_GENERALIZEDTIME_adj(s, posix_time, offset_day, offset_sec); |
| 53 |
} |
| 54 |
|
| 55 |
int ASN1_TIME_check(const ASN1_TIME *t) { |
| 56 |
if (t->type == V_ASN1_GENERALIZEDTIME) { |
| 57 |
return ASN1_GENERALIZEDTIME_check(t); |
| 58 |
} else if (t->type == V_ASN1_UTCTIME) { |
| 59 |
return ASN1_UTCTIME_check(t); |
| 60 |
} |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
// Convert an ASN1_TIME structure to GeneralizedTime |
| 65 |
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t, |
| 66 |
ASN1_GENERALIZEDTIME **out) { |
| 67 |
ASN1_GENERALIZEDTIME *ret = NULL; |
| 68 |
char *str; |
| 69 |
int newlen; |
| 70 |
|
| 71 |
if (!ASN1_TIME_check(t)) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
if (!out || !*out) { |
| 76 |
if (!(ret = ASN1_GENERALIZEDTIME_new())) { |
| 77 |
goto err; |
| 78 |
} |
| 79 |
} else { |
| 80 |
ret = *out; |
| 81 |
} |
| 82 |
|
| 83 |
// If already GeneralizedTime just copy across |
| 84 |
if (t->type == V_ASN1_GENERALIZEDTIME) { |
| 85 |
if (!ASN1_STRING_set(ret, t->data, t->length)) { |
| 86 |
goto err; |
| 87 |
} |
| 88 |
goto done; |
| 89 |
} |
| 90 |
|
| 91 |
// grow the string |
| 92 |
if (!ASN1_STRING_set(ret, NULL, t->length + 2)) { |
| 93 |
goto err; |
| 94 |
} |
| 95 |
// ASN1_STRING_set() allocated 'len + 1' bytes. |
| 96 |
newlen = t->length + 2 + 1; |
| 97 |
str = (char *)ret->data; |
| 98 |
// Work out the century and prepend |
| 99 |
if (t->data[0] >= '5') { |
| 100 |
OPENSSL_strlcpy(str, "19", newlen); |
| 101 |
} else { |
| 102 |
OPENSSL_strlcpy(str, "20", newlen); |
| 103 |
} |
| 104 |
|
| 105 |
OPENSSL_strlcat(str, (char *)t->data, newlen); |
| 106 |
|
| 107 |
done: |
| 108 |
if (out != NULL && *out == NULL) { |
| 109 |
*out = ret; |
| 110 |
} |
| 111 |
return ret; |
| 112 |
|
| 113 |
err: |
| 114 |
if (out == NULL || *out != ret) { |
| 115 |
ASN1_GENERALIZEDTIME_free(ret); |
| 116 |
} |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) { |
| 121 |
return ASN1_UTCTIME_set_string(s, str) || |
| 122 |
ASN1_GENERALIZEDTIME_set_string(s, str); |
| 123 |
} |
| 124 |
|
| 125 |
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str) { |
| 126 |
CBS cbs; |
| 127 |
CBS_init(&cbs, (const uint8_t*)str, strlen(str)); |
| 128 |
int type; |
| 129 |
struct tm tm; |
| 130 |
if (CBS_parse_utc_time(&cbs, /*out_tm=*/NULL, |
| 131 |
/*allow_timezone_offset=*/0)) { |
| 132 |
type = V_ASN1_UTCTIME; |
| 133 |
} else if (CBS_parse_generalized_time(&cbs, &tm, |
| 134 |
/*allow_timezone_offset=*/0)) { |
| 135 |
type = V_ASN1_GENERALIZEDTIME; |
| 136 |
if (fits_in_utc_time(&tm)) { |
| 137 |
type = V_ASN1_UTCTIME; |
| 138 |
CBS_skip(&cbs, 2); |
| 139 |
} |
| 140 |
} else { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
if (s != NULL) { |
| 145 |
if (!ASN1_STRING_set(s, CBS_data(&cbs), CBS_len(&cbs))) { |
| 146 |
return 0; |
| 147 |
} |
| 148 |
s->type = type; |
| 149 |
} |
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t, |
| 154 |
int allow_timezone_offset) { |
| 155 |
if (t == NULL) { |
| 156 |
if (OPENSSL_posix_to_tm(time(NULL), tm)) { |
| 157 |
return 1; |
| 158 |
} |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
if (t->type == V_ASN1_UTCTIME) { |
| 163 |
return asn1_utctime_to_tm(tm, t, allow_timezone_offset); |
| 164 |
} else if (t->type == V_ASN1_GENERALIZEDTIME) { |
| 165 |
return asn1_generalizedtime_to_tm(tm, t); |
| 166 |
} |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
int ASN1_TIME_diff(int *out_days, int *out_seconds, const ASN1_TIME *from, |
| 172 |
const ASN1_TIME *to) { |
| 173 |
struct tm tm_from, tm_to; |
| 174 |
if (!asn1_time_to_tm(&tm_from, from, /*allow_timezone_offset=*/1)) { |
| 175 |
return 0; |
| 176 |
} |
| 177 |
if (!asn1_time_to_tm(&tm_to, to, /*allow_timezone_offset=*/1)) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
return OPENSSL_gmtime_diff(out_days, out_seconds, &tm_from, &tm_to); |
| 181 |
} |
| 182 |
|
| 183 |
// The functions below do *not* permissively allow the use of four digit |
| 184 |
// timezone offsets in UTC times, as is done elsewhere in the code. They are |
| 185 |
// both new API, and used internally to X509_cmp_time. This is to discourage the |
| 186 |
// use of nonstandard times in new code, and to ensure that this code behaves |
| 187 |
// correctly in X509_cmp_time which historically did its own time validations |
| 188 |
// slightly different than the many other copies of X.509 time validation |
| 189 |
// sprinkled through the codebase. The custom checks in X509_cmp_time meant that |
| 190 |
// it did not allow four digit timezone offsets in UTC times. |
| 191 |
int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm) { |
| 192 |
return asn1_time_to_tm(tm, s, /*allow_timezone_offset=*/0); |
| 193 |
} |
| 194 |
|
| 195 |
int ASN1_TIME_to_time_t(const ASN1_TIME *t, time_t *out_time) { |
| 196 |
struct tm tm; |
| 197 |
if (!asn1_time_to_tm(&tm, t, /*allow_timezone_offset=*/0)) { |
| 198 |
return 0; |
| 199 |
} |
| 200 |
return OPENSSL_timegm(&tm, out_time); |
| 201 |
} |
| 202 |
|
| 203 |
int ASN1_TIME_to_posix(const ASN1_TIME *t, int64_t *out_time) { |
| 204 |
struct tm tm; |
| 205 |
if (!asn1_time_to_tm(&tm, t, /*allow_timezone_offset=*/0)) { |
| 206 |
return 0; |
| 207 |
} |
| 208 |
return OPENSSL_tm_to_posix(&tm, out_time); |
| 209 |
} |