Coverage Report

Created: 2026-10-28 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/x509_req.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/bn.h>
13
#include <openssl/evp.h>
14
#include <openssl/asn1.h>
15
#include <openssl/asn1t.h>
16
#include <openssl/x509.h>
17
#include "crypto/x509.h"
18
#include <openssl/objects.h>
19
#include <openssl/buffer.h>
20
#include <openssl/pem.h>
21
22
X509_REQ *X509_to_X509_REQ(const X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
23
0
{
24
0
    X509_REQ *ret;
25
0
    X509_REQ_INFO *ri;
26
0
    int i;
27
0
    EVP_PKEY *pktmp;
28
0
    OSSL_LIB_CTX *libctx;
29
0
    const char *propq;
30
31
0
    ossl_x509_get0_libctx(x, &libctx, &propq);
32
0
    ret = X509_REQ_new_ex(libctx, propq);
33
0
    if (ret == NULL) {
34
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
35
0
        goto err;
36
0
    }
37
38
0
    ri = &ret->req_info;
39
40
0
    ri->version->length = 1;
41
0
    ri->version->data = OPENSSL_malloc(1);
42
0
    if (ri->version->data == NULL)
43
0
        goto err;
44
0
    ri->version->data[0] = 0; /* version == 0 */
45
46
0
    if (!X509_REQ_set_subject_name(ret, X509_get_subject_name(x)))
47
0
        goto err;
48
49
0
    pktmp = X509_get0_pubkey(x);
50
0
    if (pktmp == NULL)
51
0
        goto err;
52
0
    i = X509_REQ_set_pubkey(ret, pktmp);
53
0
    if (!i)
54
0
        goto err;
55
56
0
    if (pkey != NULL) {
57
0
        if (!X509_REQ_sign(ret, pkey, md))
58
0
            goto err;
59
0
    }
60
0
    return ret;
61
0
err:
62
0
    X509_REQ_free(ret);
63
0
    return NULL;
64
0
}
65
66
EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
67
0
{
68
0
    if (req == NULL)
69
0
        return NULL;
70
0
    return X509_PUBKEY_get(req->req_info.pubkey);
71
0
}
72
73
EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req)
74
0
{
75
0
    if (req == NULL)
76
0
        return NULL;
77
0
    return X509_PUBKEY_get0(req->req_info.pubkey);
78
0
}
79
80
X509_PUBKEY *X509_REQ_get_X509_PUBKEY(const X509_REQ *req)
81
0
{
82
0
    return req->req_info.pubkey;
83
0
}
84
85
int X509_REQ_check_private_key(const X509_REQ *req, EVP_PKEY *pkey)
86
0
{
87
0
    return ossl_x509_check_private_key(X509_REQ_get0_pubkey(req), pkey);
88
0
}
89
90
/*
91
 * It seems several organisations had the same idea of including a list of
92
 * extensions in a certificate request. There are at least two OIDs that are
93
 * used and there may be more: so the list is configurable.
94
 */
95
96
static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef };
97
98
static int *ext_nids = ext_nid_list;
99
100
int X509_REQ_extension_nid(int req_nid)
101
0
{
102
0
    int i, nid;
103
104
0
    for (i = 0;; i++) {
105
0
        nid = ext_nids[i];
106
0
        if (nid == NID_undef)
107
0
            return 0;
108
0
        else if (req_nid == nid)
109
0
            return 1;
110
0
    }
111
0
}
112
113
int *X509_REQ_get_extension_nids(void)
114
0
{
115
0
    return ext_nids;
116
0
}
117
118
void X509_REQ_set_extension_nids(int *nids)
119
0
{
120
0
    ext_nids = nids;
121
0
}
122
123
STACK_OF(X509_EXTENSION) *
124
ossl_x509_req_get1_extensions_by_nid(const X509_REQ *req, int nid)
125
0
{
126
0
    X509_ATTRIBUTE *attr;
127
0
    const ASN1_TYPE *ext = NULL;
128
0
    const unsigned char *p;
129
0
    int idx = X509_REQ_get_attr_by_NID(req, nid, -1);
130
131
0
    if (idx < 0) /* no extensions is not an error */
132
0
        return sk_X509_EXTENSION_new_null();
133
0
    attr = X509_REQ_get_attr(req, idx);
134
0
    ext = X509_ATTRIBUTE_get0_type(attr, 0);
135
0
    if (ext == NULL || ext->type != V_ASN1_SEQUENCE) {
136
0
        ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
137
0
        return NULL;
138
0
    }
139
0
    p = ext->value.sequence->data;
140
0
    return (STACK_OF(X509_EXTENSION) *)
141
0
        ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
142
0
            ASN1_ITEM_rptr(X509_EXTENSIONS));
143
0
}
144
145
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req)
146
0
{
147
0
    STACK_OF(X509_EXTENSION) *exts = NULL;
148
0
    int *pnid;
149
150
0
    if (req == NULL || ext_nids == NULL)
151
0
        return NULL;
152
0
    for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
153
0
        exts = ossl_x509_req_get1_extensions_by_nid(req, *pnid);
154
0
        if (exts == NULL)
155
0
            return NULL;
156
0
        if (sk_X509_EXTENSION_num(exts) > 0)
157
0
            return exts;
158
0
        sk_X509_EXTENSION_free(exts);
159
0
    }
160
    /* no extensions is not an error */
161
0
    return sk_X509_EXTENSION_new_null();
162
0
}
163
164
/*
165
 * Add a STACK_OF extensions to a certificate request: allow alternative OIDs
166
 * in case we want to create a non standard one.
167
 */
168
int X509_REQ_add_extensions_nid(X509_REQ *req,
169
    const STACK_OF(X509_EXTENSION) *exts, int nid)
170
0
{
171
0
    int extlen;
172
0
    int rv = 0;
173
0
    unsigned char *ext = NULL;
174
0
    STACK_OF(X509_EXTENSION) *mod_exts = NULL;
175
0
    int loc;
176
177
0
    if (sk_X509_EXTENSION_num(exts) <= 0)
178
0
        return 1; /* adding NULL or empty list of exts is a no-op */
179
180
0
    loc = X509at_get_attr_by_NID(req->req_info.attributes, nid, -1);
181
0
    if (loc != -1) {
182
0
        if ((mod_exts = ossl_x509_req_get1_extensions_by_nid(req, nid)) == NULL)
183
0
            return 0;
184
0
        if (X509v3_add_extensions(&mod_exts, exts) == NULL)
185
0
            goto end;
186
0
    }
187
188
    /* Generate encoding of extensions */
189
0
    extlen = ASN1_item_i2d((const ASN1_VALUE *)(mod_exts == NULL ? exts : mod_exts),
190
0
        &ext, ASN1_ITEM_rptr(X509_EXTENSIONS));
191
0
    if (extlen <= 0)
192
0
        goto end;
193
0
    if (mod_exts != NULL) {
194
0
        X509_ATTRIBUTE *att = X509at_delete_attr(req->req_info.attributes, loc);
195
196
0
        if (att == NULL)
197
0
            goto end;
198
0
        X509_ATTRIBUTE_free(att);
199
0
    }
200
201
0
    rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen);
202
0
    OPENSSL_free(ext);
203
204
0
end:
205
0
    sk_X509_EXTENSION_pop_free(mod_exts, X509_EXTENSION_free);
206
0
    return rv;
207
0
}
208
209
/* This is the normal usage: use the "official" OID */
210
int X509_REQ_add_extensions(X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts)
211
0
{
212
0
    return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
213
0
}
214
215
/* Request attribute functions */
216
217
int X509_REQ_get_attr_count(const X509_REQ *req)
218
0
{
219
0
    return X509at_get_attr_count(req->req_info.attributes);
220
0
}
221
222
int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
223
0
{
224
0
    return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
225
0
}
226
227
int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
228
    int lastpos)
229
0
{
230
0
    return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
231
0
}
232
233
X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
234
0
{
235
0
    return X509at_get_attr(req->req_info.attributes, loc);
236
0
}
237
238
X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
239
0
{
240
0
    X509_ATTRIBUTE *attr;
241
242
0
    if (req == NULL) {
243
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
244
0
        return NULL;
245
0
    }
246
0
    attr = X509at_delete_attr(req->req_info.attributes, loc);
247
0
    if (attr != NULL)
248
0
        req->req_info.enc.modified = 1;
249
0
    return attr;
250
0
}
251
252
int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
253
0
{
254
0
    if (req == NULL) {
255
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
256
0
        return 0;
257
0
    }
258
0
    if (!X509at_add1_attr(&req->req_info.attributes, attr))
259
0
        return 0;
260
0
    req->req_info.enc.modified = 1;
261
0
    return 1;
262
0
}
263
264
int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
265
    const ASN1_OBJECT *obj, int type,
266
    const unsigned char *bytes, int len)
267
0
{
268
0
    if (req == NULL) {
269
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
270
0
        return 0;
271
0
    }
272
0
    if (!X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
273
0
            type, bytes, len))
274
0
        return 0;
275
0
    req->req_info.enc.modified = 1;
276
0
    return 1;
277
0
}
278
279
int X509_REQ_add1_attr_by_NID(X509_REQ *req,
280
    int nid, int type,
281
    const unsigned char *bytes, int len)
282
0
{
283
0
    if (req == NULL) {
284
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
285
0
        return 0;
286
0
    }
287
0
    if (!X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
288
0
            type, bytes, len))
289
0
        return 0;
290
0
    req->req_info.enc.modified = 1;
291
0
    return 1;
292
0
}
293
294
int X509_REQ_add1_attr_by_txt(X509_REQ *req,
295
    const char *attrname, int type,
296
    const unsigned char *bytes, int len)
297
0
{
298
0
    if (req == NULL) {
299
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
300
0
        return 0;
301
0
    }
302
0
    if (!X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
303
0
            type, bytes, len))
304
0
        return 0;
305
0
    req->req_info.enc.modified = 1;
306
0
    return 1;
307
0
}
308
309
long X509_REQ_get_version(const X509_REQ *req)
310
0
{
311
0
    return ASN1_INTEGER_get(req->req_info.version);
312
0
}
313
314
const X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
315
0
{
316
0
    return req->req_info.subject;
317
0
}
318
319
void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
320
    const X509_ALGOR **palg)
321
0
{
322
0
    if (psig != NULL)
323
0
        *psig = req->signature;
324
0
    if (palg != NULL)
325
0
        *palg = &req->sig_alg;
326
0
}
327
328
void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig)
329
0
{
330
0
    if (req->signature)
331
0
        ASN1_BIT_STRING_free(req->signature);
332
0
    req->signature = psig;
333
0
}
334
335
int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg)
336
0
{
337
0
    return X509_ALGOR_copy(&req->sig_alg, palg);
338
0
}
339
340
int X509_REQ_get_signature_nid(const X509_REQ *req)
341
0
{
342
0
    return OBJ_obj2nid(req->sig_alg.algorithm);
343
0
}
344
345
int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
346
0
{
347
0
    if (req == NULL) {
348
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
349
0
        return 0;
350
0
    }
351
0
    req->req_info.enc.modified = 1;
352
0
    return i2d_X509_REQ_INFO(&req->req_info, pp);
353
0
}