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/x509.h>
58
59 #include <limits.h>
60
61 #include <openssl/asn1.h>
62 #include <openssl/digest.h>
63 #include <openssl/dsa.h>
64 #include <openssl/evp.h>
65 #include <openssl/mem.h>
66 #include <openssl/rsa.h>
67 #include <openssl/stack.h>
68
69 #include "internal.h"
70
71
X509_verify(X509 * x509,EVP_PKEY * pkey)72 int X509_verify(X509 *x509, EVP_PKEY *pkey)
73 {
74 if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) {
75 OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
76 return 0;
77 }
78 return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg,
79 x509->signature, x509->cert_info, pkey);
80 }
81
X509_REQ_verify(X509_REQ * req,EVP_PKEY * pkey)82 int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey)
83 {
84 return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO),
85 req->sig_alg, req->signature, req->req_info, pkey);
86 }
87
X509_sign(X509 * x,EVP_PKEY * pkey,const EVP_MD * md)88 int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
89 {
90 x->cert_info->enc.modified = 1;
91 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
92 x->sig_alg, x->signature, x->cert_info, pkey, md));
93 }
94
X509_sign_ctx(X509 * x,EVP_MD_CTX * ctx)95 int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
96 {
97 x->cert_info->enc.modified = 1;
98 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
99 x->cert_info->signature,
100 x->sig_alg, x->signature, x->cert_info, ctx);
101 }
102
X509_REQ_sign(X509_REQ * x,EVP_PKEY * pkey,const EVP_MD * md)103 int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
104 {
105 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
106 x->signature, x->req_info, pkey, md));
107 }
108
X509_REQ_sign_ctx(X509_REQ * x,EVP_MD_CTX * ctx)109 int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
110 {
111 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO),
112 x->sig_alg, NULL, x->signature, x->req_info,
113 ctx);
114 }
115
X509_CRL_sign(X509_CRL * x,EVP_PKEY * pkey,const EVP_MD * md)116 int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
117 {
118 x->crl->enc.modified = 1;
119 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
120 x->sig_alg, x->signature, x->crl, pkey, md));
121 }
122
X509_CRL_sign_ctx(X509_CRL * x,EVP_MD_CTX * ctx)123 int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
124 {
125 x->crl->enc.modified = 1;
126 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
127 x->crl->sig_alg, x->sig_alg, x->signature,
128 x->crl, ctx);
129 }
130
NETSCAPE_SPKI_sign(NETSCAPE_SPKI * x,EVP_PKEY * pkey,const EVP_MD * md)131 int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
132 {
133 return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL,
134 x->signature, x->spkac, pkey, md));
135 }
136
NETSCAPE_SPKI_verify(NETSCAPE_SPKI * spki,EVP_PKEY * pkey)137 int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey)
138 {
139 return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor,
140 spki->signature, spki->spkac, pkey));
141 }
142
d2i_X509_fp(FILE * fp,X509 ** x509)143 X509 *d2i_X509_fp(FILE *fp, X509 **x509)
144 {
145 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
146 }
147
i2d_X509_fp(FILE * fp,X509 * x509)148 int i2d_X509_fp(FILE *fp, X509 *x509)
149 {
150 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
151 }
152
d2i_X509_bio(BIO * bp,X509 ** x509)153 X509 *d2i_X509_bio(BIO *bp, X509 **x509)
154 {
155 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
156 }
157
i2d_X509_bio(BIO * bp,X509 * x509)158 int i2d_X509_bio(BIO *bp, X509 *x509)
159 {
160 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
161 }
162
d2i_X509_CRL_fp(FILE * fp,X509_CRL ** crl)163 X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
164 {
165 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
166 }
167
i2d_X509_CRL_fp(FILE * fp,X509_CRL * crl)168 int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl)
169 {
170 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
171 }
172
d2i_X509_CRL_bio(BIO * bp,X509_CRL ** crl)173 X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
174 {
175 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
176 }
177
i2d_X509_CRL_bio(BIO * bp,X509_CRL * crl)178 int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl)
179 {
180 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
181 }
182
d2i_X509_REQ_fp(FILE * fp,X509_REQ ** req)183 X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
184 {
185 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
186 }
187
i2d_X509_REQ_fp(FILE * fp,X509_REQ * req)188 int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req)
189 {
190 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
191 }
192
d2i_X509_REQ_bio(BIO * bp,X509_REQ ** req)193 X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
194 {
195 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
196 }
197
i2d_X509_REQ_bio(BIO * bp,X509_REQ * req)198 int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req)
199 {
200 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
201 }
202
203
204 #define IMPLEMENT_D2I_FP(type, name, bio_func) \
205 type *name(FILE *fp, type **obj) { \
206 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
207 if (bio == NULL) { \
208 return NULL; \
209 } \
210 type *ret = bio_func(bio, obj); \
211 BIO_free(bio); \
212 return ret; \
213 }
214
215 #define IMPLEMENT_I2D_FP(type, name, bio_func) \
216 int name(FILE *fp, type *obj) { \
217 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
218 if (bio == NULL) { \
219 return 0; \
220 } \
221 int ret = bio_func(bio, obj); \
222 BIO_free(bio); \
223 return ret; \
224 }
225
IMPLEMENT_D2I_FP(RSA,d2i_RSAPrivateKey_fp,d2i_RSAPrivateKey_bio)226 IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio)
227 IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio)
228
229 IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio)
230 IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio)
231
232 IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio)
233 IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio)
234
235 #define IMPLEMENT_D2I_BIO(type, name, d2i_func) \
236 type *name(BIO *bio, type **obj) { \
237 uint8_t *data; \
238 size_t len; \
239 if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \
240 return NULL; \
241 } \
242 const uint8_t *ptr = data; \
243 type *ret = d2i_func(obj, &ptr, (long)len); \
244 OPENSSL_free(data); \
245 return ret; \
246 }
247
248 #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \
249 int name(BIO *bio, type *obj) { \
250 uint8_t *data = NULL; \
251 int len = i2d_func(obj, &data); \
252 if (len < 0) { \
253 return 0; \
254 } \
255 int ret = BIO_write_all(bio, data, len); \
256 OPENSSL_free(data); \
257 return ret; \
258 }
259
260 IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey)
261 IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey)
262
263 IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey)
264 IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey)
265
266 IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY)
267 IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY)
268
269 #ifndef OPENSSL_NO_DSA
270 IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio)
271 IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio)
272
273 IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio)
274 IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio)
275
276 IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey)
277 IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey)
278
279 IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY)
280 IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY)
281 #endif
282
283 IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio)
284 IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio)
285
286 IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio)
287 IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio)
288
289 IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey)
290 IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey)
291
292 IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY)
293 IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY)
294
295 int X509_pubkey_digest(const X509 *data, const EVP_MD *type,
296 unsigned char *md, unsigned int *len)
297 {
298 ASN1_BIT_STRING *key;
299 key = X509_get0_pubkey_bitstr(data);
300 if (!key)
301 return 0;
302 return EVP_Digest(key->data, key->length, md, len, type, NULL);
303 }
304
X509_digest(const X509 * data,const EVP_MD * type,unsigned char * md,unsigned int * len)305 int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
306 unsigned int *len)
307 {
308 return (ASN1_item_digest
309 (ASN1_ITEM_rptr(X509), type, (char *)data, md, len));
310 }
311
X509_CRL_digest(const X509_CRL * data,const EVP_MD * type,unsigned char * md,unsigned int * len)312 int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type,
313 unsigned char *md, unsigned int *len)
314 {
315 return (ASN1_item_digest
316 (ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len));
317 }
318
X509_REQ_digest(const X509_REQ * data,const EVP_MD * type,unsigned char * md,unsigned int * len)319 int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type,
320 unsigned char *md, unsigned int *len)
321 {
322 return (ASN1_item_digest
323 (ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len));
324 }
325
X509_NAME_digest(const X509_NAME * data,const EVP_MD * type,unsigned char * md,unsigned int * len)326 int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
327 unsigned char *md, unsigned int *len)
328 {
329 return (ASN1_item_digest
330 (ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len));
331 }
332
IMPLEMENT_D2I_FP(X509_SIG,d2i_PKCS8_fp,d2i_PKCS8_bio)333 IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio)
334 IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio)
335
336 IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG)
337 IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG)
338
339 IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp,
340 d2i_PKCS8_PRIV_KEY_INFO_bio)
341 IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp,
342 i2d_PKCS8_PRIV_KEY_INFO_bio)
343
344 int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key)
345 {
346 PKCS8_PRIV_KEY_INFO *p8inf;
347 int ret;
348 p8inf = EVP_PKEY2PKCS8(key);
349 if (!p8inf)
350 return 0;
351 ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
352 PKCS8_PRIV_KEY_INFO_free(p8inf);
353 return ret;
354 }
355
IMPLEMENT_D2I_FP(EVP_PKEY,d2i_PrivateKey_fp,d2i_PrivateKey_bio)356 IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio)
357 IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio)
358
359 IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio)
360 IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio)
361
362 IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio,
363 d2i_PKCS8_PRIV_KEY_INFO)
364 IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio,
365 i2d_PKCS8_PRIV_KEY_INFO)
366
367 int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key)
368 {
369 PKCS8_PRIV_KEY_INFO *p8inf;
370 int ret;
371 p8inf = EVP_PKEY2PKCS8(key);
372 if (!p8inf)
373 return 0;
374 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
375 PKCS8_PRIV_KEY_INFO_free(p8inf);
376 return ret;
377 }
378
379 IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey)
380 IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey)
381
382 IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY)
383 IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY)
384
385 IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams)
386 IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams)
387