Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/x_algor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-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 <stddef.h>
11
#include <openssl/x509.h>
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/err.h>
15
#include "crypto/asn1.h"
16
#include "crypto/evp.h"
17
18
ASN1_SEQUENCE(X509_ALGOR) = {
19
        ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT),
20
        ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY)
21
} ASN1_SEQUENCE_END(X509_ALGOR)
22
23
ASN1_ITEM_TEMPLATE(X509_ALGORS) =
24
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
25
ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
26
27
IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
28
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
29
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
30
31
int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
32
63
{
33
63
    if (alg == NULL)
34
0
        return 0;
35
36
63
    if (ptype != V_ASN1_UNDEF) {
37
39
        if (alg->parameter == NULL)
38
39
            alg->parameter = ASN1_TYPE_new();
39
39
        if (alg->parameter == NULL)
40
0
            return 0;
41
39
    }
42
43
63
    ASN1_OBJECT_free(alg->algorithm);
44
63
    alg->algorithm = aobj;
45
46
63
    if (ptype == 0)
47
0
        return 1;
48
63
    if (ptype == V_ASN1_UNDEF) {
49
24
        ASN1_TYPE_free(alg->parameter);
50
24
        alg->parameter = NULL;
51
24
    } else
52
39
        ASN1_TYPE_set(alg->parameter, ptype, pval);
53
63
    return 1;
54
63
}
55
56
void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
57
                     const void **ppval, const X509_ALGOR *algor)
58
1.94M
{
59
1.94M
    if (paobj)
60
866k
        *paobj = algor->algorithm;
61
1.94M
    if (pptype) {
62
1.18M
        if (algor->parameter == NULL) {
63
76.8k
            *pptype = V_ASN1_UNDEF;
64
76.8k
            return;
65
76.8k
        } else
66
1.11M
            *pptype = algor->parameter->type;
67
1.11M
        if (ppval)
68
1.10M
            *ppval = algor->parameter->value.ptr;
69
1.11M
    }
70
1.94M
}
71
72
/* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
73
74
void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
75
0
{
76
0
    int param_type;
77
78
0
    if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
79
0
        param_type = V_ASN1_UNDEF;
80
0
    else
81
0
        param_type = V_ASN1_NULL;
82
83
0
    X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), param_type, NULL);
84
85
0
}
86
87
int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
88
6.70k
{
89
6.70k
    int rv;
90
6.70k
    rv = OBJ_cmp(a->algorithm, b->algorithm);
91
6.70k
    if (rv)
92
4.38k
        return rv;
93
2.32k
    if (!a->parameter && !b->parameter)
94
10
        return 0;
95
2.31k
    return ASN1_TYPE_cmp(a->parameter, b->parameter);
96
2.32k
}
97
98
int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)
99
0
{
100
0
    if (src == NULL || dest == NULL)
101
0
        return 0;
102
103
0
    if (dest->algorithm)
104
0
         ASN1_OBJECT_free(dest->algorithm);
105
0
    dest->algorithm = NULL;
106
107
0
    if (dest->parameter)
108
0
        ASN1_TYPE_free(dest->parameter);
109
0
    dest->parameter = NULL;
110
111
0
    if (src->algorithm)
112
0
        if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)
113
0
            return 0;
114
115
0
    if (src->parameter != NULL) {
116
0
        dest->parameter = ASN1_TYPE_new();
117
0
        if (dest->parameter == NULL)
118
0
            return 0;
119
120
        /* Assuming this is also correct for a BOOL.
121
         * set does copy as a side effect.
122
         */
123
0
        if (ASN1_TYPE_set1(dest->parameter, src->parameter->type,
124
0
                           src->parameter->value.ptr) == 0)
125
0
            return 0;
126
0
    }
127
128
0
    return 1;
129
0
}
130
131
/* allocate and set algorithm ID from EVP_MD, default SHA1 */
132
int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md)
133
0
{
134
    /* Default is SHA1 so no need to create it - still success */
135
0
    if (md == NULL || EVP_MD_is_a(md, "SHA1"))
136
0
        return 1;
137
0
    *palg = X509_ALGOR_new();
138
0
    if (*palg == NULL)
139
0
        return 0;
140
0
    X509_ALGOR_set_md(*palg, md);
141
0
    return 1;
142
0
}
143
144
/* convert algorithm ID to EVP_MD, default SHA1 */
145
const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg)
146
54.5k
{
147
54.5k
    const EVP_MD *md;
148
149
54.5k
    if (alg == NULL)
150
54.4k
        return EVP_sha1();
151
114
    md = EVP_get_digestbyobj(alg->algorithm);
152
114
    if (md == NULL)
153
114
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_DIGEST);
154
114
    return md;
155
54.5k
}
156
157
X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg)
158
835
{
159
835
    if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
160
138
        return NULL;
161
697
    return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
162
697
                                     alg->parameter);
163
835
}
164
165
/* Allocate and set MGF1 algorithm ID from EVP_MD */
166
int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
167
0
{
168
0
    X509_ALGOR *algtmp = NULL;
169
0
    ASN1_STRING *stmp = NULL;
170
171
0
    *palg = NULL;
172
0
    if (mgf1md == NULL || EVP_MD_is_a(mgf1md, "SHA1"))
173
0
        return 1;
174
    /* need to embed algorithm ID inside another */
175
0
    if (!ossl_x509_algor_new_from_md(&algtmp, mgf1md))
176
0
        goto err;
177
0
    if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
178
0
         goto err;
179
0
    *palg = X509_ALGOR_new();
180
0
    if (*palg == NULL)
181
0
        goto err;
182
0
    if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) {
183
0
        X509_ALGOR_free(*palg);
184
0
        *palg = NULL;
185
0
        goto err;
186
0
    }
187
0
    stmp = NULL;
188
0
 err:
189
0
    ASN1_STRING_free(stmp);
190
0
    X509_ALGOR_free(algtmp);
191
0
    if (*palg != NULL)
192
0
        return 1;
193
0
    return 0;
194
0
}