Coverage Report

Created: 2025-04-22 06:18

/src/openssl/crypto/ts/ts_verify_ctx.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2021 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 "internal/cryptlib.h"
11
#include <openssl/objects.h>
12
#include <openssl/ts.h>
13
#include "ts_local.h"
14
15
TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
16
0
{
17
0
    TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
18
19
0
    if (ctx == NULL)
20
0
        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
21
0
    return ctx;
22
0
}
23
24
void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
25
0
{
26
0
    OPENSSL_assert(ctx != NULL);
27
0
    memset(ctx, 0, sizeof(*ctx));
28
0
}
29
30
void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
31
0
{
32
0
    if (!ctx)
33
0
        return;
34
35
0
    TS_VERIFY_CTX_cleanup(ctx);
36
0
    OPENSSL_free(ctx);
37
0
}
38
39
int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
40
0
{
41
0
    ctx->flags |= f;
42
0
    return ctx->flags;
43
0
}
44
45
int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
46
0
{
47
0
    ctx->flags = f;
48
0
    return ctx->flags;
49
0
}
50
51
BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
52
0
{
53
0
    ctx->data = b;
54
0
    return ctx->data;
55
0
}
56
57
X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
58
0
{
59
0
    ctx->store = s;
60
0
    return ctx->store;
61
0
}
62
63
STACK_OF(X509) *TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx,
64
                                        STACK_OF(X509) *certs)
65
0
{
66
0
    ctx->certs = certs;
67
0
    return ctx->certs;
68
0
}
69
70
unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
71
                                         unsigned char *hexstr, long len)
72
0
{
73
0
    OPENSSL_free(ctx->imprint);
74
0
    ctx->imprint = hexstr;
75
0
    ctx->imprint_len = len;
76
0
    return ctx->imprint;
77
0
}
78
79
void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
80
0
{
81
0
    if (!ctx)
82
0
        return;
83
84
0
    X509_STORE_free(ctx->store);
85
0
    sk_X509_pop_free(ctx->certs, X509_free);
86
87
0
    ASN1_OBJECT_free(ctx->policy);
88
89
0
    X509_ALGOR_free(ctx->md_alg);
90
0
    OPENSSL_free(ctx->imprint);
91
92
0
    BIO_free_all(ctx->data);
93
94
0
    ASN1_INTEGER_free(ctx->nonce);
95
96
0
    GENERAL_NAME_free(ctx->tsa_name);
97
98
0
    TS_VERIFY_CTX_init(ctx);
99
0
}
100
101
TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
102
0
{
103
0
    TS_VERIFY_CTX *ret = ctx;
104
0
    ASN1_OBJECT *policy;
105
0
    TS_MSG_IMPRINT *imprint;
106
0
    X509_ALGOR *md_alg;
107
0
    ASN1_OCTET_STRING *msg;
108
0
    const ASN1_INTEGER *nonce;
109
110
0
    OPENSSL_assert(req != NULL);
111
0
    if (ret)
112
0
        TS_VERIFY_CTX_cleanup(ret);
113
0
    else if ((ret = TS_VERIFY_CTX_new()) == NULL)
114
0
        return NULL;
115
116
0
    ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
117
118
0
    if ((policy = req->policy_id) != NULL) {
119
0
        if ((ret->policy = OBJ_dup(policy)) == NULL)
120
0
            goto err;
121
0
    } else
122
0
        ret->flags &= ~TS_VFY_POLICY;
123
124
0
    imprint = req->msg_imprint;
125
0
    md_alg = imprint->hash_algo;
126
0
    if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
127
0
        goto err;
128
0
    msg = imprint->hashed_msg;
129
0
    ret->imprint_len = ASN1_STRING_length(msg);
130
0
    if (ret->imprint_len <= 0)
131
0
        goto err;
132
0
    if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
133
0
        goto err;
134
0
    memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
135
136
0
    if ((nonce = req->nonce) != NULL) {
137
0
        if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
138
0
            goto err;
139
0
    } else
140
0
        ret->flags &= ~TS_VFY_NONCE;
141
142
0
    return ret;
143
0
 err:
144
0
    if (ctx)
145
0
        TS_VERIFY_CTX_cleanup(ctx);
146
0
    else
147
0
        TS_VERIFY_CTX_free(ret);
148
0
    return NULL;
149
0
}