1 /* crypto/pem/pem_all.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
62 #include <openssl/bio.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pkcs7.h>
66 #include <openssl/pem.h>
69 /* The X509 functions */
70 X509 *PEM_read_X509(FILE *fp, X509 **x, int (*cb)())
72 return((X509 *)PEM_ASN1_read((char *(*)())d2i_X509,
73 PEM_STRING_X509,fp,(char **)x,cb));
77 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, int (*cb)())
79 return((X509 *)PEM_ASN1_read_bio((char *(*)())d2i_X509,
80 PEM_STRING_X509,bp,(char **)x,cb));
84 int PEM_write_X509(FILE *fp, X509 *x)
86 return(PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp,
87 (char *)x, NULL,NULL,0,NULL));
91 int PEM_write_bio_X509(BIO *bp, X509 *x)
93 return(PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp,
94 (char *)x, NULL,NULL,0,NULL));
98 /* The X509_REQ functions */
99 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x, int (*cb)())
101 return((X509_REQ *)PEM_ASN1_read((char *(*)())d2i_X509_REQ,
102 PEM_STRING_X509_REQ,fp,(char **)x,cb));
106 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, int (*cb)())
108 return((X509_REQ *)PEM_ASN1_read_bio((char *(*)())d2i_X509_REQ,
109 PEM_STRING_X509_REQ,bp,(char **)x,cb));
113 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x)
115 return(PEM_ASN1_write((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,
116 (char *)x, NULL,NULL,0,NULL));
120 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x)
122 return(PEM_ASN1_write_bio((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,
123 bp,(char *)x, NULL,NULL,0,NULL));
127 /* The X509_CRL functions */
128 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x, int (*cb)())
130 return((X509_CRL *)PEM_ASN1_read((char *(*)())d2i_X509_CRL,
131 PEM_STRING_X509_CRL,fp,(char **)x,cb));
135 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, int (*cb)())
137 return((X509_CRL *)PEM_ASN1_read_bio((char *(*)())d2i_X509_CRL,
138 PEM_STRING_X509_CRL,bp,(char **)x,cb));
142 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x)
144 return(PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,fp,
145 (char *)x, NULL,NULL,0,NULL));
149 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x)
151 return(PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,
152 bp,(char *)x, NULL,NULL,0,NULL));
157 /* The RSAPrivateKey functions */
158 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x, int (*cb)())
160 return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPrivateKey,
161 PEM_STRING_RSA,fp,(char **)x,cb));
164 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x, int (*cb)())
166 return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPublicKey,
167 PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb));
171 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x, int (*cb)())
173 return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPrivateKey,
174 PEM_STRING_RSA,bp,(char **)x,cb));
177 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, int (*cb)())
179 return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPublicKey,
180 PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb));
184 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, EVP_CIPHER *enc,
185 unsigned char *kstr, int klen, int (*cb)())
187 return(PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,
188 (char *)x,enc,kstr,klen,cb));
191 int PEM_write_RSAPublicKey(FILE *fp, RSA *x)
193 return(PEM_ASN1_write((int (*)())i2d_RSAPublicKey,
194 PEM_STRING_RSA_PUBLIC,fp,
195 (char *)x,NULL,NULL,0,NULL));
199 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
200 unsigned char *kstr, int klen, int (*cb)())
202 return(PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,
203 bp,(char *)x,enc,kstr,klen,cb));
206 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x)
208 return(PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey,
209 PEM_STRING_RSA_PUBLIC,
210 bp,(char *)x,NULL,NULL,0,NULL));
216 /* The DSAPrivateKey functions */
217 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x, int (*cb)())
219 return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAPrivateKey,
220 PEM_STRING_DSA,fp,(char **)x,cb));
224 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x, int (*cb)())
226 return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAPrivateKey,
227 PEM_STRING_DSA,bp,(char **)x,cb));
231 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
232 unsigned char *kstr, int klen, int (*cb)())
234 return(PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,
235 (char *)x,enc,kstr,klen,cb));
239 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
240 unsigned char *kstr, int klen, int (*cb)())
242 return(PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,
243 bp,(char *)x,enc,kstr,klen,cb));
248 /* The PrivateKey functions */
249 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, int (*cb)())
251 return((EVP_PKEY *)PEM_ASN1_read((char *(*)())d2i_PrivateKey,
252 PEM_STRING_EVP_PKEY,fp,(char **)x,cb));
256 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, int (*cb)())
258 return((EVP_PKEY *)PEM_ASN1_read_bio((char *(*)())d2i_PrivateKey,
259 PEM_STRING_EVP_PKEY,bp,(char **)x,cb));
263 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, EVP_CIPHER *enc,
264 unsigned char *kstr, int klen, int (*cb)())
266 return(PEM_ASN1_write((int (*)())i2d_PrivateKey,
267 ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
268 fp,(char *)x,enc,kstr,klen,cb));
272 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, EVP_CIPHER *enc,
273 unsigned char *kstr, int klen, int (*cb)())
275 return(PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,
276 ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
277 bp,(char *)x,enc,kstr,klen,cb));
281 /* The PKCS7 functions */
282 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, int (*cb)())
284 return((PKCS7 *)PEM_ASN1_read((char *(*)())d2i_PKCS7,
285 PEM_STRING_PKCS7,fp,(char **)x,cb));
289 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, int (*cb)())
291 return((PKCS7 *)PEM_ASN1_read_bio((char *(*)())d2i_PKCS7,
292 PEM_STRING_PKCS7,bp,(char **)x,cb));
296 int PEM_write_PKCS7(FILE *fp, PKCS7 *x)
298 return(PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp,
299 (char *)x, NULL,NULL,0,NULL));
303 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x)
305 return(PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp,
306 (char *)x, NULL,NULL,0,NULL));
311 /* The DHparams functions */
312 DH *PEM_read_DHparams(FILE *fp, DH **x, int (*cb)())
314 return((DH *)PEM_ASN1_read((char *(*)())d2i_DHparams,
315 PEM_STRING_DHPARAMS,fp,(char **)x,cb));
319 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, int (*cb)())
321 return((DH *)PEM_ASN1_read_bio((char *(*)())d2i_DHparams,
322 PEM_STRING_DHPARAMS,bp,(char **)x,cb));
326 int PEM_write_DHparams(FILE *fp, DH *x)
328 return(PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,
329 (char *)x, NULL,NULL,0,NULL));
333 int PEM_write_bio_DHparams(BIO *bp, DH *x)
335 return(PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,
336 bp,(char *)x, NULL,NULL,0,NULL));
342 /* The DSAparams functions */
343 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, int (*cb)())
345 return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAparams,
346 PEM_STRING_DSAPARAMS,fp,(char **)x,cb));
350 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, int (*cb)())
352 return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAparams,
353 PEM_STRING_DSAPARAMS,bp,(char **)x,cb));
357 int PEM_write_DSAparams(FILE *fp, DSA *x)
359 return(PEM_ASN1_write((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,fp,
360 (char *)x, NULL,NULL,0,NULL));
364 int PEM_write_bio_DSAparams(BIO *bp, DSA *x)
366 return(PEM_ASN1_write_bio((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,
367 bp,(char *)x, NULL,NULL,0,NULL));
371 /* The Netscape Certificate sequence functions */
374 NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
375 NETSCAPE_CERT_SEQUENCE **x, int (*cb)())
377 return((NETSCAPE_CERT_SEQUENCE *)
378 PEM_ASN1_read((char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,
379 PEM_STRING_X509,fp,(char **)x,cb));
383 NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
384 NETSCAPE_CERT_SEQUENCE **x, int (*cb)())
386 return((NETSCAPE_CERT_SEQUENCE *)
387 PEM_ASN1_read_bio((char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,
388 PEM_STRING_X509,bp,(char **)x,cb));
392 int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x)
394 return(PEM_ASN1_write((int (*)())i2d_NETSCAPE_CERT_SEQUENCE,
395 PEM_STRING_X509,fp, (char *)x, NULL,NULL,0,NULL));
399 int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x)
401 return(PEM_ASN1_write_bio((int (*)())i2d_NETSCAPE_CERT_SEQUENCE,
402 PEM_STRING_X509,bp, (char *)x, NULL,NULL,0,NULL));