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