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