]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 BM |
12 | #include <openssl/bn.h> |
13 | #include <openssl/evp.h> | |
14 | #include <openssl/asn1.h> | |
3c07d3a3 | 15 | #include <openssl/asn1t.h> |
ec577822 | 16 | #include <openssl/x509.h> |
25f2138b | 17 | #include "crypto/x509.h" |
ec577822 BM |
18 | #include <openssl/objects.h> |
19 | #include <openssl/buffer.h> | |
20 | #include <openssl/pem.h> | |
d02b48c6 | 21 | |
9868232a | 22 | X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) |
0f113f3e MC |
23 | { |
24 | X509_REQ *ret; | |
25 | X509_REQ_INFO *ri; | |
26 | int i; | |
27 | EVP_PKEY *pktmp; | |
28 | ||
29 | ret = X509_REQ_new(); | |
30 | if (ret == NULL) { | |
9311d0c4 | 31 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
32 | goto err; |
33 | } | |
34 | ||
95ed0e7c | 35 | ri = &ret->req_info; |
0f113f3e MC |
36 | |
37 | ri->version->length = 1; | |
b196e7d9 | 38 | ri->version->data = OPENSSL_malloc(1); |
0f113f3e MC |
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 | ||
8382fd3a | 46 | pktmp = X509_get0_pubkey(x); |
28a00bcd DSH |
47 | if (pktmp == NULL) |
48 | goto err; | |
0f113f3e | 49 | i = X509_REQ_set_pubkey(ret, pktmp); |
0f113f3e MC |
50 | if (!i) |
51 | goto err; | |
52 | ||
53 | if (pkey != NULL) { | |
54 | if (!X509_REQ_sign(ret, pkey, md)) | |
55 | goto err; | |
56 | } | |
26a7d938 | 57 | return ret; |
0f113f3e MC |
58 | err: |
59 | X509_REQ_free(ret); | |
26a7d938 | 60 | return NULL; |
0f113f3e | 61 | } |
d02b48c6 | 62 | |
6b691a5c | 63 | EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req) |
0f113f3e | 64 | { |
95ed0e7c | 65 | if (req == NULL) |
26a7d938 K |
66 | return NULL; |
67 | return X509_PUBKEY_get(req->req_info.pubkey); | |
0f113f3e | 68 | } |
d02b48c6 | 69 | |
97458daa F |
70 | EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *req) |
71 | { | |
72 | if (req == NULL) | |
73 | return NULL; | |
26a7d938 | 74 | return X509_PUBKEY_get0(req->req_info.pubkey); |
97458daa F |
75 | } |
76 | ||
1f143e08 DSH |
77 | X509_PUBKEY *X509_REQ_get_X509_PUBKEY(X509_REQ *req) |
78 | { | |
79 | return req->req_info.pubkey; | |
80 | } | |
81 | ||
e6526fbf | 82 | int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k) |
0f113f3e MC |
83 | { |
84 | EVP_PKEY *xk = NULL; | |
85 | int ok = 0; | |
86 | ||
87 | xk = X509_REQ_get_pubkey(x); | |
c74aaa39 | 88 | switch (EVP_PKEY_eq(xk, k)) { |
0f113f3e MC |
89 | case 1: |
90 | ok = 1; | |
91 | break; | |
92 | case 0: | |
9311d0c4 | 93 | ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH); |
0f113f3e MC |
94 | break; |
95 | case -1: | |
9311d0c4 | 96 | ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH); |
0f113f3e MC |
97 | break; |
98 | case -2: | |
9311d0c4 | 99 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE); |
0f113f3e | 100 | } |
e6526fbf | 101 | |
0f113f3e | 102 | EVP_PKEY_free(xk); |
26a7d938 | 103 | return ok; |
0f113f3e | 104 | } |
e6526fbf | 105 | |
0f113f3e MC |
106 | /* |
107 | * It seems several organisations had the same idea of including a list of | |
87c49f62 DSH |
108 | * extensions in a certificate request. There are at least two OIDs that are |
109 | * used and there may be more: so the list is configurable. | |
110 | */ | |
111 | ||
0f113f3e | 112 | static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef }; |
87c49f62 DSH |
113 | |
114 | static int *ext_nids = ext_nid_list; | |
115 | ||
116 | int X509_REQ_extension_nid(int req_nid) | |
117 | { | |
0f113f3e MC |
118 | int i, nid; |
119 | for (i = 0;; i++) { | |
120 | nid = ext_nids[i]; | |
121 | if (nid == NID_undef) | |
122 | return 0; | |
123 | else if (req_nid == nid) | |
124 | return 1; | |
125 | } | |
87c49f62 DSH |
126 | } |
127 | ||
657e60fa | 128 | int *X509_REQ_get_extension_nids(void) |
87c49f62 | 129 | { |
0f113f3e | 130 | return ext_nids; |
87c49f62 | 131 | } |
0f113f3e | 132 | |
87c49f62 DSH |
133 | void X509_REQ_set_extension_nids(int *nids) |
134 | { | |
0f113f3e | 135 | ext_nids = nids; |
87c49f62 DSH |
136 | } |
137 | ||
138 | STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) | |
0f113f3e MC |
139 | { |
140 | X509_ATTRIBUTE *attr; | |
141 | ASN1_TYPE *ext = NULL; | |
142 | int idx, *pnid; | |
143 | const unsigned char *p; | |
144 | ||
95ed0e7c | 145 | if ((req == NULL) || !ext_nids) |
26a7d938 | 146 | return NULL; |
0f113f3e MC |
147 | for (pnid = ext_nids; *pnid != NID_undef; pnid++) { |
148 | idx = X509_REQ_get_attr_by_NID(req, *pnid, -1); | |
149 | if (idx == -1) | |
150 | continue; | |
151 | attr = X509_REQ_get_attr(req, idx); | |
9b0a4531 | 152 | ext = X509_ATTRIBUTE_get0_type(attr, 0); |
0f113f3e MC |
153 | break; |
154 | } | |
155 | if (!ext || (ext->type != V_ASN1_SEQUENCE)) | |
156 | return NULL; | |
157 | p = ext->value.sequence->data; | |
158 | return (STACK_OF(X509_EXTENSION) *) | |
159 | ASN1_item_d2i(NULL, &p, ext->value.sequence->length, | |
160 | ASN1_ITEM_rptr(X509_EXTENSIONS)); | |
87c49f62 | 161 | } |
fd520577 | 162 | |
0f113f3e MC |
163 | /* |
164 | * Add a STACK_OF extensions to a certificate request: allow alternative OIDs | |
fd520577 DSH |
165 | * in case we want to create a non standard one. |
166 | */ | |
743975c7 DDO |
167 | int X509_REQ_add_extensions_nid(X509_REQ *req, |
168 | const STACK_OF(X509_EXTENSION) *exts, int nid) | |
fd520577 | 169 | { |
9b0a4531 DSH |
170 | int extlen; |
171 | int rv = 0; | |
172 | unsigned char *ext = NULL; | |
743975c7 | 173 | |
0f113f3e | 174 | /* Generate encoding of extensions */ |
743975c7 | 175 | extlen = ASN1_item_i2d((const ASN1_VALUE *)exts, &ext, |
9b0a4531 DSH |
176 | ASN1_ITEM_rptr(X509_EXTENSIONS)); |
177 | if (extlen <= 0) | |
178 | return 0; | |
179 | rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen); | |
180 | OPENSSL_free(ext); | |
181 | return rv; | |
fd520577 | 182 | } |
0f113f3e | 183 | |
fd520577 | 184 | /* This is the normal usage: use the "official" OID */ |
743975c7 | 185 | int X509_REQ_add_extensions(X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts) |
fd520577 | 186 | { |
0f113f3e | 187 | return X509_REQ_add_extensions_nid(req, exts, NID_ext_req); |
fd520577 | 188 | } |
77b47b90 DSH |
189 | |
190 | /* Request attribute functions */ | |
191 | ||
192 | int X509_REQ_get_attr_count(const X509_REQ *req) | |
193 | { | |
95ed0e7c | 194 | return X509at_get_attr_count(req->req_info.attributes); |
77b47b90 DSH |
195 | } |
196 | ||
0f113f3e | 197 | int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) |
77b47b90 | 198 | { |
95ed0e7c | 199 | return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos); |
77b47b90 DSH |
200 | } |
201 | ||
c47ba4e9 | 202 | int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj, |
0f113f3e | 203 | int lastpos) |
77b47b90 | 204 | { |
95ed0e7c | 205 | return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos); |
77b47b90 DSH |
206 | } |
207 | ||
208 | X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) | |
209 | { | |
95ed0e7c | 210 | return X509at_get_attr(req->req_info.attributes, loc); |
77b47b90 DSH |
211 | } |
212 | ||
213 | X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) | |
214 | { | |
95ed0e7c | 215 | return X509at_delete_attr(req->req_info.attributes, loc); |
77b47b90 DSH |
216 | } |
217 | ||
c7cb16a8 | 218 | int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr) |
77b47b90 | 219 | { |
95ed0e7c | 220 | if (X509at_add1_attr(&req->req_info.attributes, attr)) |
0f113f3e MC |
221 | return 1; |
222 | return 0; | |
77b47b90 DSH |
223 | } |
224 | ||
c7cb16a8 | 225 | int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, |
0f113f3e MC |
226 | const ASN1_OBJECT *obj, int type, |
227 | const unsigned char *bytes, int len) | |
77b47b90 | 228 | { |
95ed0e7c | 229 | if (X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj, |
0f113f3e MC |
230 | type, bytes, len)) |
231 | return 1; | |
232 | return 0; | |
77b47b90 DSH |
233 | } |
234 | ||
c7cb16a8 | 235 | int X509_REQ_add1_attr_by_NID(X509_REQ *req, |
0f113f3e MC |
236 | int nid, int type, |
237 | const unsigned char *bytes, int len) | |
77b47b90 | 238 | { |
95ed0e7c | 239 | if (X509at_add1_attr_by_NID(&req->req_info.attributes, nid, |
0f113f3e MC |
240 | type, bytes, len)) |
241 | return 1; | |
242 | return 0; | |
77b47b90 DSH |
243 | } |
244 | ||
c7cb16a8 | 245 | int X509_REQ_add1_attr_by_txt(X509_REQ *req, |
0f113f3e MC |
246 | const char *attrname, int type, |
247 | const unsigned char *bytes, int len) | |
77b47b90 | 248 | { |
95ed0e7c | 249 | if (X509at_add1_attr_by_txt(&req->req_info.attributes, attrname, |
0f113f3e MC |
250 | type, bytes, len)) |
251 | return 1; | |
252 | return 0; | |
77b47b90 | 253 | } |
124055a9 | 254 | |
b26ab17f | 255 | long X509_REQ_get_version(const X509_REQ *req) |
124055a9 | 256 | { |
95ed0e7c | 257 | return ASN1_INTEGER_get(req->req_info.version); |
124055a9 DSH |
258 | } |
259 | ||
b26ab17f | 260 | X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) |
124055a9 | 261 | { |
95ed0e7c | 262 | return req->req_info.subject; |
124055a9 | 263 | } |
a9732d04 | 264 | |
11222483 DSH |
265 | void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, |
266 | const X509_ALGOR **palg) | |
a9732d04 | 267 | { |
75ef61d7 | 268 | if (psig != NULL) |
a9732d04 | 269 | *psig = req->signature; |
75ef61d7 | 270 | if (palg != NULL) |
a9732d04 DSH |
271 | *palg = &req->sig_alg; |
272 | } | |
273 | ||
c72e5934 DWG |
274 | void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig) |
275 | { | |
276 | if (req->signature) | |
277 | ASN1_BIT_STRING_free(req->signature); | |
278 | req->signature = psig; | |
279 | } | |
280 | ||
281 | int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg) | |
282 | { | |
283 | return X509_ALGOR_copy(&req->sig_alg, palg); | |
284 | } | |
285 | ||
a9732d04 DSH |
286 | int X509_REQ_get_signature_nid(const X509_REQ *req) |
287 | { | |
288 | return OBJ_obj2nid(req->sig_alg.algorithm); | |
289 | } | |
290 | ||
291 | int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) | |
292 | { | |
293 | req->req_info.enc.modified = 1; | |
294 | return i2d_X509_REQ_INFO(&req->req_info, pp); | |
295 | } |