Coverage Report

Created: 2025-06-13 06:56

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