| 1 |
/* |
| 2 |
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <time.h> |
| 12 |
#include "internal/cryptlib.h" |
| 13 |
#include <openssl/asn1.h> |
| 14 |
#include "asn1_local.h" |
| 15 |
#include <openssl/asn1t.h> |
| 16 |
|
| 17 |
IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME) |
| 18 |
|
| 19 |
/* This is the primary function used to parse ASN1_UTCTIME */ |
| 20 |
int ossl_asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d) |
| 21 |
{ |
| 22 |
/* wrapper around ossl_asn1_time_to_tm */ |
| 23 |
if (d->type != V_ASN1_UTCTIME) |
| 24 |
return 0; |
| 25 |
return ossl_asn1_time_to_tm(tm, d); |
| 26 |
} |
| 27 |
|
| 28 |
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) |
| 29 |
{ |
| 30 |
return ossl_asn1_utctime_to_tm(NULL, d); |
| 31 |
} |
| 32 |
|
| 33 |
/* Sets the string via simple copy without cleaning it up */ |
| 34 |
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) |
| 35 |
{ |
| 36 |
ASN1_UTCTIME t; |
| 37 |
size_t len; |
| 38 |
|
| 39 |
if ((len = strlen(str)) >= INT_MAX) |
| 40 |
return 0; |
| 41 |
t.type = V_ASN1_UTCTIME; |
| 42 |
t.length = (int)len; |
| 43 |
t.data = (unsigned char *)str; |
| 44 |
t.flags = 0; |
| 45 |
|
| 46 |
if (!ASN1_UTCTIME_check(&t)) |
| 47 |
return 0; |
| 48 |
|
| 49 |
if (s != NULL && !ASN1_STRING_copy(s, &t)) |
| 50 |
return 0; |
| 51 |
|
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) |
| 56 |
{ |
| 57 |
return ASN1_UTCTIME_adj(s, t, 0, 0); |
| 58 |
} |
| 59 |
|
| 60 |
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, |
| 61 |
int offset_day, long offset_sec) |
| 62 |
{ |
| 63 |
struct tm *ts; |
| 64 |
struct tm data; |
| 65 |
|
| 66 |
ts = OPENSSL_gmtime(&t, &data); |
| 67 |
if (ts == NULL) |
| 68 |
return NULL; |
| 69 |
|
| 70 |
if (offset_day || offset_sec) { |
| 71 |
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
return ossl_asn1_time_from_tm(s, ts, V_ASN1_UTCTIME); |
| 76 |
} |
| 77 |
|
| 78 |
int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) |
| 79 |
{ |
| 80 |
struct tm stm, ttm; |
| 81 |
int day, sec; |
| 82 |
|
| 83 |
if (!ossl_asn1_utctime_to_tm(&stm, s)) |
| 84 |
return -2; |
| 85 |
|
| 86 |
if (OPENSSL_gmtime(&t, &ttm) == NULL) |
| 87 |
return -2; |
| 88 |
|
| 89 |
if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) |
| 90 |
return -2; |
| 91 |
|
| 92 |
if (day > 0 || sec > 0) |
| 93 |
return 1; |
| 94 |
if (day < 0 || sec < 0) |
| 95 |
return -1; |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) |
| 100 |
{ |
| 101 |
if (tm->type != V_ASN1_UTCTIME) |
| 102 |
return 0; |
| 103 |
return ASN1_TIME_print(bp, tm); |
| 104 |
} |