ASN1_ITEM versions of ASN1_d2i_{fp, bio} and replacement of
[openssl.git] / crypto / x509 / x_all.c
1 /* crypto/x509/x_all.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
59 #include <stdio.h>
60 #undef SSLEAY_MACROS
61 #include <openssl/stack.h>
62 #include "cryptlib.h"
63 #include <openssl/buffer.h>
64 #include <openssl/asn1.h>
65 #include <openssl/evp.h>
66 #include <openssl/x509.h>
67
68 int X509_verify(X509 *a, EVP_PKEY *r)
69         {
70         return(ASN1_item_verify(&X509_CINF_it,a->sig_alg,
71                 a->signature,a->cert_info,r));
72         }
73
74 int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
75         {
76         return( ASN1_item_verify(&X509_REQ_INFO_it,
77                 a->sig_alg,a->signature,a->req_info,r));
78         }
79
80 int X509_CRL_verify(X509_CRL *a, EVP_PKEY *r)
81         {
82         return(ASN1_item_verify(&X509_CRL_INFO_it,
83                 a->sig_alg, a->signature,a->crl,r));
84         }
85
86 int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
87         {
88         return(ASN1_item_verify(&NETSCAPE_SPKAC_it,
89                 a->sig_algor,a->signature,a->spkac,r));
90         }
91
92 int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
93         {
94         return(ASN1_item_sign(&X509_CINF_it, x->cert_info->signature,
95                 x->sig_alg, x->signature, x->cert_info,pkey,md));
96         }
97
98 int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
99         {
100         return(ASN1_item_sign(&X509_REQ_INFO_it,x->sig_alg, NULL,
101                 x->signature, x->req_info,pkey,md));
102         }
103
104 int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
105         {
106         return(ASN1_item_sign(&X509_CRL_INFO_it,x->crl->sig_alg,
107                 x->sig_alg, x->signature, x->crl,pkey,md));
108         }
109
110 int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
111         {
112         return(ASN1_item_sign(&NETSCAPE_SPKAC_it, x->sig_algor,NULL,
113                 x->signature, x->spkac,pkey,md));
114         }
115
116 X509_ATTRIBUTE *X509_ATTRIBUTE_dup(X509_ATTRIBUTE *xa)
117         {
118         return ASN1_item_dup(&X509_ATTRIBUTE_it,xa);
119         }
120
121 X509 *X509_dup(X509 *x509)
122         {
123         return ASN1_item_dup(&X509_it,x509);
124         }
125
126 X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *ex)
127         {
128         return ASN1_item_dup(&X509_EXTENSION_it,ex);
129         }
130
131 #ifndef NO_FP_API
132 X509 *d2i_X509_fp(FILE *fp, X509 **x509)
133         {
134         return ASN1_item_d2i_fp(&X509_it, fp, x509);
135         }
136
137 int i2d_X509_fp(FILE *fp, X509 *x509)
138         {
139         return ASN1_item_i2d_fp(&X509_it, fp, x509);
140         }
141 #endif
142
143 X509 *d2i_X509_bio(BIO *bp, X509 **x509)
144         {
145         return ASN1_item_d2i_bio(&X509_it, bp, x509);
146         }
147
148 int i2d_X509_bio(BIO *bp, X509 *x509)
149         {
150         return ASN1_item_i2d_bio(&X509_it, bp, x509);
151         }
152
153 X509_CRL *X509_CRL_dup(X509_CRL *crl)
154         {
155         return ASN1_item_dup(&X509_CRL_it, crl);
156         }
157
158 #ifndef NO_FP_API
159 X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
160         {
161         return ASN1_item_d2i_fp(&X509_CRL_it, fp, crl);
162         }
163
164 int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl)
165         {
166         return ASN1_item_i2d_fp(&X509_CRL_it, fp, crl);
167         }
168 #endif
169
170 X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
171         {
172         return ASN1_item_d2i_bio(&X509_CRL_it, bp, crl);
173         }
174
175 int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl)
176         {
177         return ASN1_item_i2d_bio(&X509_CRL_it, bp, crl);
178         }
179
180 PKCS7 *PKCS7_dup(PKCS7 *p7)
181         {
182         return ASN1_item_dup(&PKCS7_it, p7);
183         }
184
185 #ifndef NO_FP_API
186 PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
187         {
188         return ASN1_item_d2i_fp(&PKCS7_it, fp, p7);
189         }
190
191 int i2d_PKCS7_fp(FILE *fp, PKCS7 *p7)
192         {
193         return ASN1_item_i2d_fp(&PKCS7_it, fp, p7);
194         }
195 #endif
196
197 PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
198         {
199         return ASN1_item_d2i_bio(&PKCS7_it, bp, p7);
200         }
201
202 int i2d_PKCS7_bio(BIO *bp, PKCS7 *p7)
203         {
204         return ASN1_item_i2d_bio(&PKCS7_it, bp, p7);
205         }
206
207 X509_REQ *X509_REQ_dup(X509_REQ *req)
208         {
209         return ASN1_item_dup(&X509_REQ_it, req);
210         }
211
212 #ifndef NO_FP_API
213 X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
214         {
215         return ASN1_item_d2i_fp(&X509_REQ_it, fp, req);
216         }
217
218 int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req)
219         {
220         return ASN1_item_i2d_fp(&X509_REQ_it, fp, req);
221         }
222 #endif
223
224 X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
225         {
226         return ASN1_item_d2i_bio(&X509_REQ_it, bp, req);
227         }
228
229 int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req)
230         {
231         return ASN1_item_i2d_bio(&X509_REQ_it, bp, req);
232         }
233
234 #ifndef NO_RSA
235 RSA *RSAPublicKey_dup(RSA *rsa)
236         {
237         return ASN1_item_dup(&RSAPublicKey_it, rsa);
238         }
239
240 RSA *RSAPrivateKey_dup(RSA *rsa)
241         {
242         return ASN1_item_dup(&RSAPrivateKey_it, rsa);
243         }
244
245 #ifndef NO_FP_API
246 RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
247         {
248         return ASN1_item_d2i_fp(&RSAPrivateKey_it, fp, rsa);
249         }
250
251 int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa)
252         {
253         return ASN1_item_i2d_fp(&RSAPrivateKey_it, fp, rsa);
254         }
255
256 RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
257         {
258         return ASN1_item_d2i_fp(&RSAPublicKey_it, fp, rsa);
259         }
260
261
262 RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
263         {
264         return((RSA *)ASN1_d2i_fp((char *(*)())
265                 RSA_new,(char *(*)())d2i_RSA_PUBKEY, (fp),
266                 (unsigned char **)(rsa)));
267         }
268
269 int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa)
270         {
271         return ASN1_item_i2d_fp(&RSAPublicKey_it, fp, rsa);
272         }
273
274 int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa)
275         {
276         return(ASN1_i2d_fp(i2d_RSA_PUBKEY,fp,(unsigned char *)rsa));
277         }
278 #endif
279
280 RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
281         {
282         return ASN1_item_d2i_bio(&RSAPrivateKey_it, bp, rsa);
283         }
284
285 int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa)
286         {
287         return ASN1_item_i2d_bio(&RSAPrivateKey_it, bp, rsa);
288         }
289
290 RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
291         {
292         return ASN1_item_d2i_bio(&RSAPublicKey_it, bp, rsa);
293         }
294
295
296 RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
297         {
298         return((RSA *)ASN1_d2i_bio((char *(*)())
299                 RSA_new,(char *(*)())d2i_RSA_PUBKEY, (bp),
300                 (unsigned char **)(rsa)));
301         }
302
303 int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa)
304         {
305         return ASN1_item_i2d_bio(&RSAPublicKey_it, bp, rsa);
306         }
307
308 int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa)
309         {
310         return(ASN1_i2d_bio(i2d_RSA_PUBKEY,bp,(unsigned char *)rsa));
311         }
312 #endif
313
314 #ifndef NO_DSA
315 #ifndef NO_FP_API
316 DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
317         {
318         return((DSA *)ASN1_d2i_fp((char *(*)())
319                 DSA_new,(char *(*)())d2i_DSAPrivateKey, (fp),
320                 (unsigned char **)(dsa)));
321         }
322
323 int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa)
324         {
325         return(ASN1_i2d_fp(i2d_DSAPrivateKey,fp,(unsigned char *)dsa));
326         }
327
328 DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
329         {
330         return((DSA *)ASN1_d2i_fp((char *(*)())
331                 DSA_new,(char *(*)())d2i_DSA_PUBKEY, (fp),
332                 (unsigned char **)(dsa)));
333         }
334
335 int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa)
336         {
337         return(ASN1_i2d_fp(i2d_DSA_PUBKEY,fp,(unsigned char *)dsa));
338         }
339 #endif
340
341 DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
342         {
343         return((DSA *)ASN1_d2i_bio((char *(*)())
344                 DSA_new,(char *(*)())d2i_DSAPrivateKey, (bp),
345                 (unsigned char **)(dsa)));
346         }
347
348 int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa)
349         {
350         return(ASN1_i2d_bio(i2d_DSAPrivateKey,bp,(unsigned char *)dsa));
351         }
352
353 DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
354         {
355         return((DSA *)ASN1_d2i_bio((char *(*)())
356                 DSA_new,(char *(*)())d2i_DSA_PUBKEY, (bp),
357                 (unsigned char **)(dsa)));
358         }
359
360 int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa)
361         {
362         return(ASN1_i2d_bio(i2d_DSA_PUBKEY,bp,(unsigned char *)dsa));
363         }
364
365 #endif
366
367 X509_ALGOR *X509_ALGOR_dup(X509_ALGOR *xn)
368         {
369         return ASN1_item_dup(&X509_ALGOR_it, xn);
370         }
371
372 X509_NAME *X509_NAME_dup(X509_NAME *xn)
373         {
374         return ASN1_item_dup(&X509_NAME_it, xn);
375         }
376
377 X509_NAME_ENTRY *X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne)
378         {
379         return ASN1_item_dup(&X509_NAME_ENTRY_it, ne);
380         }
381
382 int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
383              unsigned int *len)
384         {
385         return(ASN1_item_digest(&X509_it,type,(char *)data,md,len));
386         }
387
388 int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md,
389              unsigned int *len)
390         {
391         return(ASN1_item_digest(&X509_CRL_it,type,(char *)data,md,len));
392         }
393
394 int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md,
395              unsigned int *len)
396         {
397         return(ASN1_item_digest(&X509_REQ_it,type,(char *)data,md,len));
398         }
399
400 int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, unsigned char *md,
401              unsigned int *len)
402         {
403         return(ASN1_item_digest(&X509_NAME_it,type,(char *)data,md,len));
404         }
405
406 int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data, const EVP_MD *type,
407              unsigned char *md, unsigned int *len)
408         {
409         return(ASN1_item_digest(&PKCS7_ISSUER_AND_SERIAL_it,type,
410                 (char *)data,md,len));
411         }
412
413
414 #ifndef NO_FP_API
415 X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
416         {
417         return((X509_SIG *)ASN1_d2i_fp((char *(*)())X509_SIG_new,
418                 (char *(*)())d2i_X509_SIG, (fp),(unsigned char **)(p8)));
419         }
420
421 int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8)
422         {
423         return(ASN1_i2d_fp(i2d_X509_SIG,fp,(unsigned char *)p8));
424         }
425 #endif
426
427 X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
428         {
429         return((X509_SIG *)ASN1_d2i_bio((char *(*)())X509_SIG_new,
430                 (char *(*)())d2i_X509_SIG, (bp),(unsigned char **)(p8)));
431         }
432
433 int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8)
434         {
435         return(ASN1_i2d_bio(i2d_X509_SIG,bp,(unsigned char *)p8));
436         }
437
438 #ifndef NO_FP_API
439 PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
440                                                  PKCS8_PRIV_KEY_INFO **p8inf)
441         {
442         return((PKCS8_PRIV_KEY_INFO *)ASN1_d2i_fp(
443                 (char *(*)())PKCS8_PRIV_KEY_INFO_new,
444                 (char *(*)())d2i_PKCS8_PRIV_KEY_INFO, (fp),
445                                 (unsigned char **)(p8inf)));
446         }
447
448 int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf)
449         {
450         return(ASN1_i2d_fp(i2d_PKCS8_PRIV_KEY_INFO,fp,(unsigned char *)p8inf));
451         }
452
453 int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key)
454         {
455         PKCS8_PRIV_KEY_INFO *p8inf;
456         int ret;
457         p8inf = EVP_PKEY2PKCS8(key);
458         if(!p8inf) return 0;
459         ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
460         PKCS8_PRIV_KEY_INFO_free(p8inf);
461         return ret;
462         }
463
464 int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey)
465         {
466         return(ASN1_i2d_fp(i2d_PrivateKey,fp,(unsigned char *)pkey));
467         }
468
469 EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
470 {
471         return((EVP_PKEY *)ASN1_d2i_fp((char *(*)())EVP_PKEY_new,
472                 (char *(*)())d2i_AutoPrivateKey, (fp),(unsigned char **)(a)));
473 }
474
475 int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey)
476         {
477         return(ASN1_i2d_fp(i2d_PUBKEY,fp,(unsigned char *)pkey));
478         }
479
480 EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a)
481 {
482         return((EVP_PKEY *)ASN1_d2i_fp((char *(*)())EVP_PKEY_new,
483                 (char *(*)())d2i_PUBKEY, (fp),(unsigned char **)(a)));
484 }
485
486 #endif
487
488 PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
489                                                  PKCS8_PRIV_KEY_INFO **p8inf)
490         {
491         return((PKCS8_PRIV_KEY_INFO *)ASN1_d2i_bio(
492                 (char *(*)())PKCS8_PRIV_KEY_INFO_new,
493                 (char *(*)())d2i_PKCS8_PRIV_KEY_INFO, (bp),
494                                 (unsigned char **)(p8inf)));
495         }
496
497 int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf)
498         {
499         return(ASN1_i2d_bio(i2d_PKCS8_PRIV_KEY_INFO,bp,(unsigned char *)p8inf));
500         }
501
502 int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key)
503         {
504         PKCS8_PRIV_KEY_INFO *p8inf;
505         int ret;
506         p8inf = EVP_PKEY2PKCS8(key);
507         if(!p8inf) return 0;
508         ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
509         PKCS8_PRIV_KEY_INFO_free(p8inf);
510         return ret;
511         }
512
513 int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey)
514         {
515         return(ASN1_i2d_bio(i2d_PrivateKey,bp,(unsigned char *)pkey));
516         }
517
518 EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
519         {
520         return((EVP_PKEY *)ASN1_d2i_bio((char *(*)())EVP_PKEY_new,
521                 (char *(*)())d2i_AutoPrivateKey, (bp),(unsigned char **)(a)));
522         }
523
524 int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey)
525         {
526         return(ASN1_i2d_bio(i2d_PUBKEY,bp,(unsigned char *)pkey));
527         }
528
529 EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a)
530         {
531         return((EVP_PKEY *)ASN1_d2i_bio((char *(*)())EVP_PKEY_new,
532                 (char *(*)())d2i_PUBKEY, (bp),(unsigned char **)(a)));
533         }