• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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(X509_REQ * req)79 EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req) {
80   if ((req == NULL) || (req->req_info == NULL)) {
81     return NULL;
82   }
83   return (X509_PUBKEY_get(req->req_info->pubkey));
84 }
85 
X509_REQ_check_private_key(X509_REQ * x,EVP_PKEY * k)86 int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k) {
87   EVP_PKEY *xk = NULL;
88   int ok = 0;
89 
90   xk = X509_REQ_get_pubkey(x);
91   switch (EVP_PKEY_cmp(xk, k)) {
92     case 1:
93       ok = 1;
94       break;
95     case 0:
96       OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
97       break;
98     case -1:
99       OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
100       break;
101     case -2:
102       if (EVP_PKEY_id(k) == EVP_PKEY_EC) {
103         OPENSSL_PUT_ERROR(X509, ERR_R_EC_LIB);
104         break;
105       }
106       OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
107   }
108 
109   EVP_PKEY_free(xk);
110   return ok;
111 }
112 
X509_REQ_extension_nid(int req_nid)113 int X509_REQ_extension_nid(int req_nid) {
114   return req_nid == NID_ext_req || req_nid == NID_ms_ext_req;
115 }
116 
STACK_OF(X509_EXTENSION)117 STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) {
118   if (req == NULL || req->req_info == NULL) {
119     return NULL;
120   }
121 
122   int idx = X509_REQ_get_attr_by_NID(req, NID_ext_req, -1);
123   if (idx == -1) {
124     idx = X509_REQ_get_attr_by_NID(req, NID_ms_ext_req, -1);
125   }
126   if (idx == -1) {
127     return NULL;
128   }
129 
130   X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx);
131   ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type(attr, 0);
132   if (!ext || ext->type != V_ASN1_SEQUENCE) {
133     return NULL;
134   }
135   const unsigned char *p = ext->value.sequence->data;
136   return (STACK_OF(X509_EXTENSION) *)ASN1_item_d2i(
137       NULL, &p, ext->value.sequence->length, ASN1_ITEM_rptr(X509_EXTENSIONS));
138 }
139 
140 // Add a STACK_OF extensions to a certificate request: allow alternative OIDs
141 // in case we want to create a non standard one.
142 
X509_REQ_add_extensions_nid(X509_REQ * req,const STACK_OF (X509_EXTENSION)* exts,int nid)143 int X509_REQ_add_extensions_nid(X509_REQ *req,
144                                 const STACK_OF(X509_EXTENSION) *exts, int nid) {
145   // Generate encoding of extensions
146   unsigned char *ext = NULL;
147   int ext_len =
148       ASN1_item_i2d((ASN1_VALUE *)exts, &ext, ASN1_ITEM_rptr(X509_EXTENSIONS));
149   if (ext_len <= 0) {
150     return 0;
151   }
152   int ret = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, ext_len);
153   OPENSSL_free(ext);
154   return ret;
155 }
156 
157 // This is the normal usage: use the "official" OID
X509_REQ_add_extensions(X509_REQ * req,const STACK_OF (X509_EXTENSION)* exts)158 int X509_REQ_add_extensions(X509_REQ *req,
159                             const STACK_OF(X509_EXTENSION) *exts) {
160   return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
161 }
162 
X509_REQ_get_attr_count(const X509_REQ * req)163 int X509_REQ_get_attr_count(const X509_REQ *req) {
164   return sk_X509_ATTRIBUTE_num(req->req_info->attributes);
165 }
166 
X509_REQ_get_attr_by_NID(const X509_REQ * req,int nid,int lastpos)167 int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) {
168   const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
169   if (obj == NULL) {
170     return -1;
171   }
172   return X509_REQ_get_attr_by_OBJ(req, obj, lastpos);
173 }
174 
X509_REQ_get_attr_by_OBJ(const X509_REQ * req,const ASN1_OBJECT * obj,int lastpos)175 int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
176                              int lastpos) {
177   if (req->req_info->attributes == NULL) {
178     return -1;
179   }
180   lastpos++;
181   if (lastpos < 0) {
182     lastpos = 0;
183   }
184   int n = sk_X509_ATTRIBUTE_num(req->req_info->attributes);
185   for (; lastpos < n; lastpos++) {
186     const X509_ATTRIBUTE *attr =
187         sk_X509_ATTRIBUTE_value(req->req_info->attributes, lastpos);
188     if (OBJ_cmp(attr->object, obj) == 0) {
189       return lastpos;
190     }
191   }
192   return -1;
193 }
194 
X509_REQ_get_attr(const X509_REQ * req,int loc)195 X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) {
196   if (req->req_info->attributes == NULL || loc < 0 ||
197       sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
198     return NULL;
199   }
200   return sk_X509_ATTRIBUTE_value(req->req_info->attributes, loc);
201 }
202 
X509_REQ_delete_attr(X509_REQ * req,int loc)203 X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) {
204   if (req->req_info->attributes == NULL || loc < 0 ||
205       sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
206     return NULL;
207   }
208   return sk_X509_ATTRIBUTE_delete(req->req_info->attributes, loc);
209 }
210 
X509_REQ_add0_attr(X509_REQ * req,X509_ATTRIBUTE * attr)211 static int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) {
212   if (req->req_info->attributes == NULL) {
213     req->req_info->attributes = sk_X509_ATTRIBUTE_new_null();
214   }
215   if (req->req_info->attributes == NULL ||
216       !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) {
217     return 0;
218   }
219 
220   return 1;
221 }
222 
X509_REQ_add1_attr(X509_REQ * req,const X509_ATTRIBUTE * attr)223 int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr) {
224   X509_ATTRIBUTE *new_attr = X509_ATTRIBUTE_dup(attr);
225   if (new_attr == NULL || !X509_REQ_add0_attr(req, new_attr)) {
226     X509_ATTRIBUTE_free(new_attr);
227     return 0;
228   }
229 
230   return 1;
231 }
232 
X509_REQ_add1_attr_by_OBJ(X509_REQ * req,const ASN1_OBJECT * obj,int attrtype,const unsigned char * data,int len)233 int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj,
234                               int attrtype, const unsigned char *data,
235                               int len) {
236   X509_ATTRIBUTE *attr =
237       X509_ATTRIBUTE_create_by_OBJ(NULL, obj, attrtype, data, len);
238   if (attr == NULL || !X509_REQ_add0_attr(req, attr)) {
239     X509_ATTRIBUTE_free(attr);
240     return 0;
241   }
242 
243   return 1;
244 }
245 
X509_REQ_add1_attr_by_NID(X509_REQ * req,int nid,int attrtype,const unsigned char * data,int len)246 int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int attrtype,
247                               const unsigned char *data, int len) {
248   X509_ATTRIBUTE *attr =
249       X509_ATTRIBUTE_create_by_NID(NULL, nid, 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_txt(X509_REQ * req,const char * attrname,int attrtype,const unsigned char * data,int len)258 int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int attrtype,
259                               const unsigned char *data, int len) {
260   X509_ATTRIBUTE *attr =
261       X509_ATTRIBUTE_create_by_txt(NULL, attrname, 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_get0_signature(const X509_REQ * req,const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg)270 void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
271                              const X509_ALGOR **palg) {
272   if (psig != NULL) {
273     *psig = req->signature;
274   }
275   if (palg != NULL) {
276     *palg = req->sig_alg;
277   }
278 }
279 
X509_REQ_get_signature_nid(const X509_REQ * req)280 int X509_REQ_get_signature_nid(const X509_REQ *req) {
281   return OBJ_obj2nid(req->sig_alg->algorithm);
282 }
283 
i2d_re_X509_REQ_tbs(X509_REQ * req,unsigned char ** pp)284 int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) {
285   asn1_encoding_clear(&req->req_info->enc);
286   return i2d_X509_REQ_INFO(req->req_info, pp);
287 }
288