1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58 #include <openssl/asn1t.h>
59 #include <openssl/bn.h>
60 #include <openssl/err.h>
61 #include <openssl/evp.h>
62 #include <openssl/mem.h>
63 #include <openssl/obj.h>
64 #include <openssl/pem.h>
65 #include <openssl/x509.h>
66
67 #include "../asn1/internal.h"
68 #include "internal.h"
69
70
X509_REQ_get_version(const X509_REQ * req)71 long X509_REQ_get_version(const X509_REQ *req) {
72 return ASN1_INTEGER_get(req->req_info->version);
73 }
74
X509_REQ_get_subject_name(const X509_REQ * req)75 X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) {
76 return req->req_info->subject;
77 }
78
X509_REQ_get_pubkey(const X509_REQ * req)79 EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req) {
80 if (req == NULL) {
81 return NULL;
82 }
83 return X509_PUBKEY_get(req->req_info->pubkey);
84 }
85
X509_REQ_get0_pubkey(const X509_REQ * req)86 EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req) {
87 if (req == NULL) {
88 return NULL;
89 }
90 return X509_PUBKEY_get0(req->req_info->pubkey);
91 }
92
X509_REQ_check_private_key(const X509_REQ * x,const EVP_PKEY * k)93 int X509_REQ_check_private_key(const X509_REQ *x, const EVP_PKEY *k) {
94 const EVP_PKEY *xk = X509_REQ_get0_pubkey(x);
95 if (xk == NULL) {
96 return 0;
97 }
98
99 int ret = EVP_PKEY_cmp(xk, k);
100 if (ret > 0) {
101 return 1;
102 }
103
104 switch (ret) {
105 case 0:
106 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
107 return 0;
108 case -1:
109 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
110 return 0;
111 case -2:
112 if (EVP_PKEY_id(k) == EVP_PKEY_EC) {
113 OPENSSL_PUT_ERROR(X509, ERR_R_EC_LIB);
114 } else {
115 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
116 }
117 return 0;
118 }
119
120 return 0;
121 }
122
X509_REQ_extension_nid(int req_nid)123 int X509_REQ_extension_nid(int req_nid) {
124 return req_nid == NID_ext_req || req_nid == NID_ms_ext_req;
125 }
126
STACK_OF(X509_EXTENSION)127 STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req) {
128 if (req == NULL || req->req_info == NULL) {
129 return NULL;
130 }
131
132 int idx = X509_REQ_get_attr_by_NID(req, NID_ext_req, -1);
133 if (idx == -1) {
134 idx = X509_REQ_get_attr_by_NID(req, NID_ms_ext_req, -1);
135 }
136 if (idx == -1) {
137 return NULL;
138 }
139
140 const X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx);
141 // TODO(davidben): |X509_ATTRIBUTE_get0_type| is not const-correct. It should
142 // take and return a const pointer.
143 const ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type((X509_ATTRIBUTE *)attr, 0);
144 if (!ext || ext->type != V_ASN1_SEQUENCE) {
145 return NULL;
146 }
147 const unsigned char *p = ext->value.sequence->data;
148 return (STACK_OF(X509_EXTENSION) *)ASN1_item_d2i(
149 NULL, &p, ext->value.sequence->length, ASN1_ITEM_rptr(X509_EXTENSIONS));
150 }
151
152 // Add a STACK_OF extensions to a certificate request: allow alternative OIDs
153 // in case we want to create a non standard one.
154
X509_REQ_add_extensions_nid(X509_REQ * req,const STACK_OF (X509_EXTENSION)* exts,int nid)155 int X509_REQ_add_extensions_nid(X509_REQ *req,
156 const STACK_OF(X509_EXTENSION) *exts, int nid) {
157 // Generate encoding of extensions
158 unsigned char *ext = NULL;
159 int ext_len =
160 ASN1_item_i2d((ASN1_VALUE *)exts, &ext, ASN1_ITEM_rptr(X509_EXTENSIONS));
161 if (ext_len <= 0) {
162 return 0;
163 }
164 int ret = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, ext_len);
165 OPENSSL_free(ext);
166 return ret;
167 }
168
169 // This is the normal usage: use the "official" OID
X509_REQ_add_extensions(X509_REQ * req,const STACK_OF (X509_EXTENSION)* exts)170 int X509_REQ_add_extensions(X509_REQ *req,
171 const STACK_OF(X509_EXTENSION) *exts) {
172 return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
173 }
174
X509_REQ_get_attr_count(const X509_REQ * req)175 int X509_REQ_get_attr_count(const X509_REQ *req) {
176 return (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes);
177 }
178
X509_REQ_get_attr_by_NID(const X509_REQ * req,int nid,int lastpos)179 int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) {
180 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
181 if (obj == NULL) {
182 return -1;
183 }
184 return X509_REQ_get_attr_by_OBJ(req, obj, lastpos);
185 }
186
X509_REQ_get_attr_by_OBJ(const X509_REQ * req,const ASN1_OBJECT * obj,int lastpos)187 int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
188 int lastpos) {
189 if (req->req_info->attributes == NULL) {
190 return -1;
191 }
192 lastpos++;
193 if (lastpos < 0) {
194 lastpos = 0;
195 }
196 int n = (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes);
197 for (; lastpos < n; lastpos++) {
198 const X509_ATTRIBUTE *attr =
199 sk_X509_ATTRIBUTE_value(req->req_info->attributes, lastpos);
200 if (OBJ_cmp(attr->object, obj) == 0) {
201 return lastpos;
202 }
203 }
204 return -1;
205 }
206
X509_REQ_get_attr(const X509_REQ * req,int loc)207 X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) {
208 if (req->req_info->attributes == NULL || loc < 0 ||
209 sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
210 return NULL;
211 }
212 return sk_X509_ATTRIBUTE_value(req->req_info->attributes, loc);
213 }
214
X509_REQ_delete_attr(X509_REQ * req,int loc)215 X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) {
216 if (req->req_info->attributes == NULL || loc < 0 ||
217 sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
218 return NULL;
219 }
220 return sk_X509_ATTRIBUTE_delete(req->req_info->attributes, loc);
221 }
222
X509_REQ_add0_attr(X509_REQ * req,X509_ATTRIBUTE * attr)223 static int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) {
224 if (req->req_info->attributes == NULL) {
225 req->req_info->attributes = sk_X509_ATTRIBUTE_new_null();
226 }
227 if (req->req_info->attributes == NULL ||
228 !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) {
229 return 0;
230 }
231
232 return 1;
233 }
234
X509_REQ_add1_attr(X509_REQ * req,const X509_ATTRIBUTE * attr)235 int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr) {
236 X509_ATTRIBUTE *new_attr = X509_ATTRIBUTE_dup(attr);
237 if (new_attr == NULL || !X509_REQ_add0_attr(req, new_attr)) {
238 X509_ATTRIBUTE_free(new_attr);
239 return 0;
240 }
241
242 return 1;
243 }
244
X509_REQ_add1_attr_by_OBJ(X509_REQ * req,const ASN1_OBJECT * obj,int attrtype,const unsigned char * data,int len)245 int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj,
246 int attrtype, const unsigned char *data,
247 int len) {
248 X509_ATTRIBUTE *attr =
249 X509_ATTRIBUTE_create_by_OBJ(NULL, obj, attrtype, data, len);
250 if (attr == NULL || !X509_REQ_add0_attr(req, attr)) {
251 X509_ATTRIBUTE_free(attr);
252 return 0;
253 }
254
255 return 1;
256 }
257
X509_REQ_add1_attr_by_NID(X509_REQ * req,int nid,int attrtype,const unsigned char * data,int len)258 int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int attrtype,
259 const unsigned char *data, int len) {
260 X509_ATTRIBUTE *attr =
261 X509_ATTRIBUTE_create_by_NID(NULL, nid, attrtype, data, len);
262 if (attr == NULL || !X509_REQ_add0_attr(req, attr)) {
263 X509_ATTRIBUTE_free(attr);
264 return 0;
265 }
266
267 return 1;
268 }
269
X509_REQ_add1_attr_by_txt(X509_REQ * req,const char * attrname,int attrtype,const unsigned char * data,int len)270 int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int attrtype,
271 const unsigned char *data, int len) {
272 X509_ATTRIBUTE *attr =
273 X509_ATTRIBUTE_create_by_txt(NULL, attrname, attrtype, data, len);
274 if (attr == NULL || !X509_REQ_add0_attr(req, attr)) {
275 X509_ATTRIBUTE_free(attr);
276 return 0;
277 }
278
279 return 1;
280 }
281
X509_REQ_get0_signature(const X509_REQ * req,const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg)282 void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
283 const X509_ALGOR **palg) {
284 if (psig != NULL) {
285 *psig = req->signature;
286 }
287 if (palg != NULL) {
288 *palg = req->sig_alg;
289 }
290 }
291
X509_REQ_get_signature_nid(const X509_REQ * req)292 int X509_REQ_get_signature_nid(const X509_REQ *req) {
293 return OBJ_obj2nid(req->sig_alg->algorithm);
294 }
295
i2d_re_X509_REQ_tbs(X509_REQ * req,unsigned char ** pp)296 int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) {
297 asn1_encoding_clear(&req->req_info->enc);
298 return i2d_X509_REQ_INFO(req->req_info, pp);
299 }
300