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