Coverage Report

Created: 2026-10-28 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/x_x509.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/evp.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/x509.h>
15
#include <openssl/x509v3.h>
16
#include "crypto/x509.h"
17
18
ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
19
    ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
20
    ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER),
21
    ASN1_EMBED(X509_CINF, signature, X509_ALGOR),
22
    ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
23
    ASN1_EMBED(X509_CINF, validity, X509_VAL),
24
    ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
25
    ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
26
    ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
27
    ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
28
    ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3)
29
} ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
30
31
IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
32
/* X509 top level structure needs a bit of customisation */
33
34
extern void ossl_policy_cache_free(X509_POLICY_CACHE *cache);
35
36
static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
37
    void *exarg)
38
0
{
39
0
    X509 *ret = (X509 *)*pval;
40
41
0
    switch (operation) {
42
43
0
    case ASN1_OP_D2I_PRE:
44
0
        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
45
0
        X509_CERT_AUX_free(ret->aux);
46
0
        ASN1_OCTET_STRING_free(ret->skid);
47
0
        AUTHORITY_KEYID_free(ret->akid);
48
0
        CRL_DIST_POINTS_free(ret->crldp);
49
0
        ossl_policy_cache_free(ret->policy_cache);
50
0
        GENERAL_NAMES_free(ret->altname);
51
0
        NAME_CONSTRAINTS_free(ret->nc);
52
0
#ifndef OPENSSL_NO_RFC3779
53
0
        sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
54
0
        ASIdentifiers_free(ret->rfc3779_asid);
55
0
#endif
56
0
        ASN1_OCTET_STRING_free(ret->distinguishing_id);
57
58
        /* fall through */
59
60
0
    case ASN1_OP_NEW_POST:
61
0
        ret->ex_cached = 0;
62
0
        ret->ex_kusage = 0;
63
0
        ret->ex_xkusage = 0;
64
0
        ret->ex_nscert = 0;
65
0
        ret->ex_flags = 0;
66
0
        ret->ex_pathlen = -1;
67
0
        ret->ex_pcpathlen = -1;
68
0
        ret->skid = NULL;
69
0
        ret->akid = NULL;
70
0
        ret->policy_cache = NULL;
71
0
        ret->altname = NULL;
72
0
        ret->nc = NULL;
73
0
#ifndef OPENSSL_NO_RFC3779
74
0
        ret->rfc3779_addr = NULL;
75
0
        ret->rfc3779_asid = NULL;
76
0
#endif
77
0
        ret->distinguishing_id = NULL;
78
0
        ret->aux = NULL;
79
0
        ret->crldp = NULL;
80
0
        if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
81
0
            return 0;
82
0
        break;
83
84
0
    case ASN1_OP_FREE_POST:
85
0
        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
86
0
        X509_CERT_AUX_free(ret->aux);
87
0
        ASN1_OCTET_STRING_free(ret->skid);
88
0
        AUTHORITY_KEYID_free(ret->akid);
89
0
        CRL_DIST_POINTS_free(ret->crldp);
90
0
        ossl_policy_cache_free(ret->policy_cache);
91
0
        GENERAL_NAMES_free(ret->altname);
92
0
        NAME_CONSTRAINTS_free(ret->nc);
93
0
#ifndef OPENSSL_NO_RFC3779
94
0
        sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
95
0
        ASIdentifiers_free(ret->rfc3779_asid);
96
0
#endif
97
0
        ASN1_OCTET_STRING_free(ret->distinguishing_id);
98
0
        break;
99
100
0
    case ASN1_OP_GET0_LIBCTX:
101
0
        ossl_x509_get0_libctx(ret, exarg, NULL);
102
0
        break;
103
104
0
    case ASN1_OP_GET0_PROPQ:
105
0
        ossl_x509_get0_libctx(ret, NULL, exarg);
106
0
        break;
107
108
0
    default:
109
0
        break;
110
0
    }
111
112
0
    return 1;
113
0
}
114
115
ASN1_SEQUENCE_ref(X509, x509_cb) = {
116
    ASN1_EMBED(X509, cert_info, X509_CINF),
117
    ASN1_EMBED(X509, sig_alg, X509_ALGOR),
118
    ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
119
} ASN1_SEQUENCE_END_ref(X509, X509)
120
121
IMPLEMENT_ASN1_FUNCTIONS(X509)
122
IMPLEMENT_ASN1_DUP_FUNCTION(X509)
123
124
void ossl_x509_get0_libctx(const X509 *x, OSSL_LIB_CTX **libctx,
125
    const char **propq)
126
0
{
127
0
    if (libctx != NULL)
128
0
        *libctx = NULL;
129
0
    if (propq != NULL)
130
0
        *propq = NULL;
131
0
    if (x->cert_info.key != NULL)
132
0
        (void)ossl_x509_PUBKEY_get0_libctx(libctx, propq, x->cert_info.key);
133
0
}
134
135
X509 *X509_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
136
0
{
137
0
    return (X509 *)ASN1_item_new_ex(ASN1_ITEM_rptr(X509), libctx, propq);
138
0
}
139
140
int X509_set_ex_data(X509 *r, int idx, void *arg)
141
0
{
142
0
    return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
143
0
}
144
145
void *X509_get_ex_data(const X509 *r, int idx)
146
0
{
147
0
    return CRYPTO_get_ex_data(&r->ex_data, idx);
148
0
}
149
150
/*
151
 * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
152
 * extra info tagged on the end. Since these functions set how a certificate
153
 * is trusted they should only be used when the certificate comes from a
154
 * reliable source such as local storage.
155
 */
156
157
X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
158
0
{
159
0
    const unsigned char *q;
160
0
    X509 *ret;
161
0
    int freeret = 0;
162
163
    /* Save start position */
164
0
    q = *pp;
165
166
0
    if (a == NULL || *a == NULL)
167
0
        freeret = 1;
168
0
    ret = d2i_X509(a, &q, length);
169
    /* If certificate unreadable then forget it */
170
0
    if (ret == NULL)
171
0
        return NULL;
172
    /* update length */
173
0
    length -= (long)(q - *pp);
174
0
    if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
175
0
        goto err;
176
0
    *pp = q;
177
0
    return ret;
178
0
err:
179
0
    if (freeret) {
180
0
        X509_free(ret);
181
0
        if (a)
182
0
            *a = NULL;
183
0
    }
184
0
    return NULL;
185
0
}
186
187
/*
188
 * Serialize trusted certificate to *pp or just return the required buffer
189
 * length if pp == NULL.  We ultimately want to avoid modifying *pp in the
190
 * error path, but that depends on similar hygiene in lower-level functions.
191
 * Here we avoid compounding the problem.
192
 */
193
static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp)
194
0
{
195
0
    int length, tmplen;
196
0
    unsigned char *start = pp != NULL ? *pp : NULL;
197
198
    /*
199
     * This might perturb *pp on error, but fixing that belongs in i2d_X509()
200
     * not here.  It should be that if a == NULL length is zero, but we check
201
     * both just in case.
202
     */
203
0
    length = i2d_X509(a, pp);
204
0
    if (length <= 0 || a == NULL)
205
0
        return length;
206
207
0
    tmplen = i2d_X509_CERT_AUX(a->aux, pp);
208
0
    if (tmplen < 0) {
209
0
        if (start != NULL)
210
0
            *pp = start;
211
0
        return tmplen;
212
0
    }
213
0
    length += tmplen;
214
215
0
    return length;
216
0
}
217
218
/*
219
 * Serialize trusted certificate to *pp, or just return the required buffer
220
 * length if pp == NULL.
221
 *
222
 * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
223
 * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
224
 * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
225
 * allocated buffer.
226
 */
227
int i2d_X509_AUX(const X509 *a, unsigned char **pp)
228
0
{
229
0
    int length;
230
0
    unsigned char *tmp;
231
232
    /* Buffer provided by caller */
233
0
    if (pp == NULL || *pp != NULL)
234
0
        return i2d_x509_aux_internal(a, pp);
235
236
    /* Obtain the combined length */
237
0
    if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
238
0
        return length;
239
240
    /* Allocate requisite combined storage */
241
0
    *pp = tmp = OPENSSL_malloc(length);
242
0
    if (tmp == NULL)
243
0
        return -1;
244
245
    /* Encode, but keep *pp at the originally malloced pointer */
246
0
    length = i2d_x509_aux_internal(a, &tmp);
247
0
    if (length <= 0) {
248
0
        OPENSSL_free(*pp);
249
0
        *pp = NULL;
250
0
    }
251
0
    return length;
252
0
}
253
254
int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
255
0
{
256
0
    x->cert_info.enc.modified = 1;
257
0
    return i2d_X509_CINF(&x->cert_info, pp);
258
0
}
259
260
void X509_get0_signature(const ASN1_BIT_STRING **psig,
261
    const X509_ALGOR **palg, const X509 *x)
262
0
{
263
0
    if (psig)
264
0
        *psig = &x->signature;
265
0
    if (palg)
266
0
        *palg = &x->sig_alg;
267
0
}
268
269
int X509_get_signature_nid(const X509 *x)
270
0
{
271
0
    return OBJ_obj2nid(x->sig_alg.algorithm);
272
0
}
273
274
void X509_set0_distinguishing_id(X509 *x, ASN1_OCTET_STRING *d_id)
275
0
{
276
0
    ASN1_OCTET_STRING_free(x->distinguishing_id);
277
0
    x->distinguishing_id = d_id;
278
0
}
279
280
const ASN1_OCTET_STRING *X509_get0_distinguishing_id(const X509 *x)
281
0
{
282
0
    return x->distinguishing_id;
283
0
}