1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci * GENERALIZEDTIME implementation. Based on UTCTIME 12e1051a39Sopenharmony_ci */ 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci#include <stdio.h> 15e1051a39Sopenharmony_ci#include <time.h> 16e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 17e1051a39Sopenharmony_ci#include <openssl/asn1.h> 18e1051a39Sopenharmony_ci#include "asn1_local.h" 19e1051a39Sopenharmony_ci#include <openssl/asn1t.h> 20e1051a39Sopenharmony_ci 21e1051a39Sopenharmony_ciIMPLEMENT_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME) 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ci/* This is the primary function used to parse ASN1_GENERALIZEDTIME */ 24e1051a39Sopenharmony_cistatic int asn1_generalizedtime_to_tm(struct tm *tm, 25e1051a39Sopenharmony_ci const ASN1_GENERALIZEDTIME *d) 26e1051a39Sopenharmony_ci{ 27e1051a39Sopenharmony_ci /* wrapper around ossl_asn1_time_to_tm */ 28e1051a39Sopenharmony_ci if (d->type != V_ASN1_GENERALIZEDTIME) 29e1051a39Sopenharmony_ci return 0; 30e1051a39Sopenharmony_ci return ossl_asn1_time_to_tm(tm, d); 31e1051a39Sopenharmony_ci} 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_ciint ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) 34e1051a39Sopenharmony_ci{ 35e1051a39Sopenharmony_ci return asn1_generalizedtime_to_tm(NULL, d); 36e1051a39Sopenharmony_ci} 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciint ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) 39e1051a39Sopenharmony_ci{ 40e1051a39Sopenharmony_ci ASN1_GENERALIZEDTIME t; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci t.type = V_ASN1_GENERALIZEDTIME; 43e1051a39Sopenharmony_ci t.length = strlen(str); 44e1051a39Sopenharmony_ci t.data = (unsigned char *)str; 45e1051a39Sopenharmony_ci t.flags = 0; 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_ci if (!ASN1_GENERALIZEDTIME_check(&t)) 48e1051a39Sopenharmony_ci return 0; 49e1051a39Sopenharmony_ci 50e1051a39Sopenharmony_ci if (s != NULL && !ASN1_STRING_copy(s, &t)) 51e1051a39Sopenharmony_ci return 0; 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci return 1; 54e1051a39Sopenharmony_ci} 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ciASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, 57e1051a39Sopenharmony_ci time_t t) 58e1051a39Sopenharmony_ci{ 59e1051a39Sopenharmony_ci return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); 60e1051a39Sopenharmony_ci} 61e1051a39Sopenharmony_ci 62e1051a39Sopenharmony_ciASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, 63e1051a39Sopenharmony_ci time_t t, int offset_day, 64e1051a39Sopenharmony_ci long offset_sec) 65e1051a39Sopenharmony_ci{ 66e1051a39Sopenharmony_ci struct tm *ts; 67e1051a39Sopenharmony_ci struct tm data; 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ci ts = OPENSSL_gmtime(&t, &data); 70e1051a39Sopenharmony_ci if (ts == NULL) 71e1051a39Sopenharmony_ci return NULL; 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_ci if (offset_day || offset_sec) { 74e1051a39Sopenharmony_ci if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) 75e1051a39Sopenharmony_ci return NULL; 76e1051a39Sopenharmony_ci } 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ci return ossl_asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME); 79e1051a39Sopenharmony_ci} 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_ciint ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) 82e1051a39Sopenharmony_ci{ 83e1051a39Sopenharmony_ci if (tm->type != V_ASN1_GENERALIZEDTIME) 84e1051a39Sopenharmony_ci return 0; 85e1051a39Sopenharmony_ci return ASN1_TIME_print(bp, tm); 86e1051a39Sopenharmony_ci} 87