Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/asn1/a_gentm.c
Line
Count
Source
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
/*
11
 * GENERALIZEDTIME implementation. Based on UTCTIME
12
 */
13
14
#include <stdio.h>
15
#include <time.h>
16
#include "internal/cryptlib.h"
17
#include <openssl/asn1.h>
18
#include "asn1_local.h"
19
#include <openssl/asn1t.h>
20
21
IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME)
22
23
/* This is the primary function used to parse ASN1_GENERALIZEDTIME */
24
static int asn1_generalizedtime_to_tm(struct tm *tm,
25
    const ASN1_GENERALIZEDTIME *d)
26
91.8k
{
27
    /* wrapper around ossl_asn1_time_to_tm */
28
91.8k
    if (d->type != V_ASN1_GENERALIZEDTIME)
29
0
        return 0;
30
91.8k
    return ossl_asn1_time_to_tm(tm, d);
31
91.8k
}
32
33
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
34
91.8k
{
35
91.8k
    return asn1_generalizedtime_to_tm(NULL, d);
36
91.8k
}
37
38
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
39
21.6k
{
40
21.6k
    ASN1_GENERALIZEDTIME t;
41
21.6k
    size_t len;
42
43
21.6k
    if ((len = strlen(str)) >= INT_MAX)
44
0
        return 0;
45
46
21.6k
    t.type = V_ASN1_GENERALIZEDTIME;
47
21.6k
    t.length = (int)len;
48
21.6k
    t.data = (unsigned char *)str;
49
21.6k
    t.flags = 0;
50
51
21.6k
    if (!ASN1_GENERALIZEDTIME_check(&t))
52
8.00k
        return 0;
53
54
13.6k
    if (s != NULL && !ASN1_STRING_copy(s, &t))
55
0
        return 0;
56
57
13.6k
    return 1;
58
13.6k
}
59
60
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
61
    time_t t)
62
46.0k
{
63
46.0k
    return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
64
46.0k
}
65
66
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
67
    time_t t, int offset_day,
68
    long offset_sec)
69
137k
{
70
137k
    struct tm *ts;
71
137k
    struct tm data;
72
73
137k
    ts = OPENSSL_gmtime(&t, &data);
74
137k
    if (ts == NULL)
75
0
        return NULL;
76
77
137k
    if (offset_day || offset_sec) {
78
76.1k
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
79
37.2k
            return NULL;
80
76.1k
    }
81
82
100k
    return ossl_asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
83
137k
}
84
85
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
86
194k
{
87
194k
    if (tm->type != V_ASN1_GENERALIZEDTIME)
88
99
        return 0;
89
194k
    return ASN1_TIME_print(bp, tm);
90
194k
}