/src/openssl111/crypto/x509/x509_req.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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(); |
30 | 0 | if (ret == NULL) { |
31 | 0 | X509err(X509_F_X509_TO_X509_REQ, ERR_R_MALLOC_FAILURE); |
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(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(X509_REQ *x, EVP_PKEY *k) |
83 | 0 | { |
84 | 0 | EVP_PKEY *xk = NULL; |
85 | 0 | int ok = 0; |
86 | |
|
87 | 0 | xk = X509_REQ_get_pubkey(x); |
88 | 0 | switch (EVP_PKEY_cmp(xk, k)) { |
89 | 0 | case 1: |
90 | 0 | ok = 1; |
91 | 0 | break; |
92 | 0 | case 0: |
93 | 0 | X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, |
94 | 0 | X509_R_KEY_VALUES_MISMATCH); |
95 | 0 | break; |
96 | 0 | case -1: |
97 | 0 | X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH); |
98 | 0 | break; |
99 | 0 | case -2: |
100 | 0 | #ifndef OPENSSL_NO_EC |
101 | 0 | if (EVP_PKEY_id(k) == EVP_PKEY_EC) { |
102 | 0 | X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB); |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | #endif |
106 | 0 | #ifndef OPENSSL_NO_DH |
107 | 0 | if (EVP_PKEY_id(k) == EVP_PKEY_DH) { |
108 | | /* No idea */ |
109 | 0 | X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, |
110 | 0 | X509_R_CANT_CHECK_DH_KEY); |
111 | 0 | break; |
112 | 0 | } |
113 | 0 | #endif |
114 | 0 | X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE); |
115 | 0 | } |
116 | | |
117 | 0 | EVP_PKEY_free(xk); |
118 | 0 | return ok; |
119 | 0 | } |
120 | | |
121 | | /* |
122 | | * It seems several organisations had the same idea of including a list of |
123 | | * extensions in a certificate request. There are at least two OIDs that are |
124 | | * used and there may be more: so the list is configurable. |
125 | | */ |
126 | | |
127 | | static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef }; |
128 | | |
129 | | static int *ext_nids = ext_nid_list; |
130 | | |
131 | | int X509_REQ_extension_nid(int req_nid) |
132 | 0 | { |
133 | 0 | int i, nid; |
134 | 0 | for (i = 0;; i++) { |
135 | 0 | nid = ext_nids[i]; |
136 | 0 | if (nid == NID_undef) |
137 | 0 | return 0; |
138 | 0 | else if (req_nid == nid) |
139 | 0 | return 1; |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | int *X509_REQ_get_extension_nids(void) |
144 | 0 | { |
145 | 0 | return ext_nids; |
146 | 0 | } |
147 | | |
148 | | void X509_REQ_set_extension_nids(int *nids) |
149 | 0 | { |
150 | 0 | ext_nids = nids; |
151 | 0 | } |
152 | | |
153 | | STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) |
154 | 0 | { |
155 | 0 | X509_ATTRIBUTE *attr; |
156 | 0 | ASN1_TYPE *ext = NULL; |
157 | 0 | int idx, *pnid; |
158 | 0 | const unsigned char *p; |
159 | |
|
160 | 0 | if ((req == NULL) || !ext_nids) |
161 | 0 | return NULL; |
162 | 0 | for (pnid = ext_nids; *pnid != NID_undef; pnid++) { |
163 | 0 | idx = X509_REQ_get_attr_by_NID(req, *pnid, -1); |
164 | 0 | if (idx == -1) |
165 | 0 | continue; |
166 | 0 | attr = X509_REQ_get_attr(req, idx); |
167 | 0 | ext = X509_ATTRIBUTE_get0_type(attr, 0); |
168 | 0 | break; |
169 | 0 | } |
170 | 0 | if (ext == NULL) /* no extensions is not an error */ |
171 | 0 | return sk_X509_EXTENSION_new_null(); |
172 | 0 | if (ext->type != V_ASN1_SEQUENCE) |
173 | 0 | return NULL; |
174 | 0 | p = ext->value.sequence->data; |
175 | 0 | return (STACK_OF(X509_EXTENSION) *) |
176 | 0 | ASN1_item_d2i(NULL, &p, ext->value.sequence->length, |
177 | 0 | ASN1_ITEM_rptr(X509_EXTENSIONS)); |
178 | 0 | } |
179 | | |
180 | | /* |
181 | | * Add a STACK_OF extensions to a certificate request: allow alternative OIDs |
182 | | * in case we want to create a non standard one. |
183 | | */ |
184 | | |
185 | | int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts, |
186 | | int nid) |
187 | 0 | { |
188 | 0 | int extlen; |
189 | 0 | int rv = 0; |
190 | 0 | unsigned char *ext = NULL; |
191 | | /* Generate encoding of extensions */ |
192 | 0 | extlen = ASN1_item_i2d((ASN1_VALUE *)exts, &ext, |
193 | 0 | ASN1_ITEM_rptr(X509_EXTENSIONS)); |
194 | 0 | if (extlen <= 0) |
195 | 0 | return 0; |
196 | 0 | rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen); |
197 | 0 | OPENSSL_free(ext); |
198 | 0 | return rv; |
199 | 0 | } |
200 | | |
201 | | /* This is the normal usage: use the "official" OID */ |
202 | | int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts) |
203 | 0 | { |
204 | 0 | return X509_REQ_add_extensions_nid(req, exts, NID_ext_req); |
205 | 0 | } |
206 | | |
207 | | /* Request attribute functions */ |
208 | | |
209 | | int X509_REQ_get_attr_count(const X509_REQ *req) |
210 | 0 | { |
211 | 0 | return X509at_get_attr_count(req->req_info.attributes); |
212 | 0 | } |
213 | | |
214 | | int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) |
215 | 0 | { |
216 | 0 | return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos); |
217 | 0 | } |
218 | | |
219 | | int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj, |
220 | | int lastpos) |
221 | 0 | { |
222 | 0 | return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos); |
223 | 0 | } |
224 | | |
225 | | X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) |
226 | 0 | { |
227 | 0 | return X509at_get_attr(req->req_info.attributes, loc); |
228 | 0 | } |
229 | | |
230 | | X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) |
231 | 0 | { |
232 | 0 | X509_ATTRIBUTE *attr = X509at_delete_attr(req->req_info.attributes, loc); |
233 | |
|
234 | 0 | if (attr != NULL) |
235 | 0 | req->req_info.enc.modified = 1; |
236 | 0 | return attr; |
237 | 0 | } |
238 | | |
239 | | int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr) |
240 | 0 | { |
241 | 0 | if (!X509at_add1_attr(&req->req_info.attributes, attr)) |
242 | 0 | return 0; |
243 | 0 | req->req_info.enc.modified = 1; |
244 | 0 | return 1; |
245 | 0 | } |
246 | | |
247 | | int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, |
248 | | const ASN1_OBJECT *obj, int type, |
249 | | const unsigned char *bytes, int len) |
250 | 0 | { |
251 | 0 | if (!X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj, |
252 | 0 | type, bytes, len)) |
253 | 0 | return 0; |
254 | 0 | req->req_info.enc.modified = 1; |
255 | 0 | return 1; |
256 | 0 | } |
257 | | |
258 | | int X509_REQ_add1_attr_by_NID(X509_REQ *req, |
259 | | int nid, int type, |
260 | | const unsigned char *bytes, int len) |
261 | 0 | { |
262 | 0 | if (!X509at_add1_attr_by_NID(&req->req_info.attributes, nid, |
263 | 0 | type, bytes, len)) |
264 | 0 | return 0; |
265 | 0 | req->req_info.enc.modified = 1; |
266 | 0 | return 1; |
267 | 0 | } |
268 | | |
269 | | int X509_REQ_add1_attr_by_txt(X509_REQ *req, |
270 | | const char *attrname, int type, |
271 | | const unsigned char *bytes, int len) |
272 | 0 | { |
273 | 0 | if (!X509at_add1_attr_by_txt(&req->req_info.attributes, attrname, |
274 | 0 | type, bytes, len)) |
275 | 0 | return 0; |
276 | 0 | req->req_info.enc.modified = 1; |
277 | 0 | return 1; |
278 | 0 | } |
279 | | |
280 | | long X509_REQ_get_version(const X509_REQ *req) |
281 | 0 | { |
282 | 0 | return ASN1_INTEGER_get(req->req_info.version); |
283 | 0 | } |
284 | | |
285 | | X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) |
286 | 0 | { |
287 | 0 | return req->req_info.subject; |
288 | 0 | } |
289 | | |
290 | | void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, |
291 | | const X509_ALGOR **palg) |
292 | 0 | { |
293 | 0 | if (psig != NULL) |
294 | 0 | *psig = req->signature; |
295 | 0 | if (palg != NULL) |
296 | 0 | *palg = &req->sig_alg; |
297 | 0 | } |
298 | | |
299 | | void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig) |
300 | 0 | { |
301 | 0 | if (req->signature) |
302 | 0 | ASN1_BIT_STRING_free(req->signature); |
303 | 0 | req->signature = psig; |
304 | 0 | } |
305 | | |
306 | | int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg) |
307 | 0 | { |
308 | 0 | return X509_ALGOR_copy(&req->sig_alg, palg); |
309 | 0 | } |
310 | | |
311 | | int X509_REQ_get_signature_nid(const X509_REQ *req) |
312 | 0 | { |
313 | 0 | return OBJ_obj2nid(req->sig_alg.algorithm); |
314 | 0 | } |
315 | | |
316 | | int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) |
317 | 0 | { |
318 | 0 | req->req_info.enc.modified = 1; |
319 | 0 | return i2d_X509_REQ_INFO(&req->req_info, pp); |
320 | 0 | } |