Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/x_algor.c
Line
Count
Source
1
/*
2
 * Copyright 1998-2026 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
0
} ASN1_SEQUENCE_END(X509_ALGOR)
22
0
23
0
ASN1_ITEM_TEMPLATE(X509_ALGORS) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR)
24
0
ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
25
26
IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
27
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
28
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
29
30
int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval)
31
0
{
32
0
    if (alg == NULL)
33
0
        return 0;
34
35
0
    if (ptype != V_ASN1_UNDEF && alg->parameter == NULL
36
0
        && (alg->parameter = ASN1_TYPE_new()) == NULL)
37
0
        return 0;
38
39
0
    ASN1_OBJECT_free(alg->algorithm);
40
0
    alg->algorithm = aobj;
41
42
0
    if (ptype == V_ASN1_EOC)
43
0
        return 1;
44
0
    if (ptype == V_ASN1_UNDEF) {
45
0
        ASN1_TYPE_free(alg->parameter);
46
0
        alg->parameter = NULL;
47
0
    } else
48
0
        ASN1_TYPE_set(alg->parameter, ptype, pval);
49
0
    return 1;
50
0
}
51
52
X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval)
53
0
{
54
0
    ASN1_OBJECT *algo = OBJ_nid2obj(nid);
55
0
    X509_ALGOR *alg = NULL;
56
57
0
    if (algo == NULL)
58
0
        return NULL;
59
0
    if ((alg = X509_ALGOR_new()) == NULL)
60
0
        goto err;
61
0
    if (X509_ALGOR_set0(alg, algo, ptype, pval))
62
0
        return alg;
63
0
    alg->algorithm = NULL; /* precaution to prevent double free */
64
65
0
err:
66
0
    X509_ALGOR_free(alg);
67
    /* ASN1_OBJECT_free(algo) is not needed due to OBJ_nid2obj() */
68
0
    return NULL;
69
0
}
70
71
void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
72
    const void **ppval, const X509_ALGOR *algor)
73
0
{
74
0
    if (paobj)
75
0
        *paobj = algor->algorithm;
76
0
    if (pptype) {
77
0
        if (algor->parameter == NULL) {
78
0
            *pptype = V_ASN1_UNDEF;
79
0
            return;
80
0
        } else
81
0
            *pptype = algor->parameter->type;
82
0
        if (ppval)
83
0
            *ppval = algor->parameter->value.ptr;
84
0
    }
85
0
}
86
87
/* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
88
int X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md)
89
0
{
90
0
    int type, md_type;
91
0
    ASN1_OBJECT *obj;
92
93
0
    if (alg == NULL || md == NULL)
94
0
        return 0;
95
96
0
    type = md->flags & EVP_MD_FLAG_DIGALGID_ABSENT ? V_ASN1_UNDEF
97
0
                                                   : V_ASN1_NULL;
98
0
    md_type = EVP_MD_type(md);
99
100
0
    obj = (md_type == NID_undef) ? OBJ_txt2obj(EVP_MD_get0_name(md), 0)
101
0
                                 : OBJ_nid2obj(md_type);
102
0
    if (obj == NULL)
103
0
        return 0;
104
0
    if (!X509_ALGOR_set0(alg, obj, type, NULL)) {
105
0
        ASN1_OBJECT_free(obj);
106
0
        return 0;
107
0
    }
108
0
    return 1;
109
0
}
110
111
int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b)
112
0
{
113
0
    int rv;
114
0
    rv = OBJ_cmp(a->algorithm, b->algorithm);
115
0
    if (rv)
116
0
        return rv;
117
0
    if (!a->parameter && !b->parameter)
118
0
        return 0;
119
0
    return ASN1_TYPE_cmp(a->parameter, b->parameter);
120
0
}
121
122
int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src)
123
0
{
124
0
    if (src == NULL || dest == NULL)
125
0
        return 0;
126
127
0
    if (dest->algorithm)
128
0
        ASN1_OBJECT_free(dest->algorithm);
129
0
    dest->algorithm = NULL;
130
131
0
    if (dest->parameter)
132
0
        ASN1_TYPE_free(dest->parameter);
133
0
    dest->parameter = NULL;
134
135
0
    if (src->algorithm)
136
0
        if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL)
137
0
            return 0;
138
139
0
    if (src->parameter != NULL) {
140
0
        dest->parameter = ASN1_TYPE_new();
141
0
        if (dest->parameter == NULL)
142
0
            return 0;
143
144
        /* Assuming this is also correct for a BOOL.
145
         * set does copy as a side effect.
146
         */
147
0
        if (ASN1_TYPE_set1(dest->parameter, src->parameter->type,
148
0
                src->parameter->value.ptr)
149
0
            == 0)
150
0
            return 0;
151
0
    }
152
153
0
    return 1;
154
0
}
155
156
/* allocate and set algorithm ID from EVP_MD, default SHA1 */
157
int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md)
158
0
{
159
0
    X509_ALGOR *alg;
160
161
    /* Default is SHA1 so no need to create it - still success */
162
0
    if (md == NULL || EVP_MD_is_a(md, "SHA1"))
163
0
        return 1;
164
0
    if ((alg = X509_ALGOR_new()) == NULL)
165
0
        return 0;
166
0
    if (!X509_ALGOR_set_md(alg, md)) {
167
0
        X509_ALGOR_free(alg);
168
0
        return 0;
169
0
    }
170
0
    *palg = alg;
171
0
    return 1;
172
0
}
173
174
/* convert algorithm ID to EVP_MD, default SHA1 */
175
const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg)
176
0
{
177
0
    const EVP_MD *md;
178
179
0
    if (alg == NULL)
180
0
        return EVP_sha1();
181
0
    md = EVP_get_digestbyobj(alg->algorithm);
182
0
    if (md == NULL)
183
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_DIGEST);
184
0
    return md;
185
0
}
186
187
X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg)
188
0
{
189
0
    if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
190
0
        return NULL;
191
0
    return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
192
0
        alg->parameter);
193
0
}
194
195
/* Allocate and set MGF1 algorithm ID from EVP_MD */
196
int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
197
0
{
198
0
    X509_ALGOR *algtmp = NULL;
199
0
    ASN1_STRING *stmp = NULL;
200
201
0
    *palg = NULL;
202
0
    if (mgf1md == NULL || EVP_MD_is_a(mgf1md, "SHA1"))
203
0
        return 1;
204
    /* need to embed algorithm ID inside another */
205
0
    if (!ossl_x509_algor_new_from_md(&algtmp, mgf1md))
206
0
        goto err;
207
0
    if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
208
0
        goto err;
209
0
    *palg = ossl_X509_ALGOR_from_nid(NID_mgf1, V_ASN1_SEQUENCE, stmp);
210
0
    if (*palg == NULL)
211
0
        goto err;
212
0
    stmp = NULL;
213
0
err:
214
0
    ASN1_STRING_free(stmp);
215
0
    X509_ALGOR_free(algtmp);
216
    return *palg != NULL;
217
0
}