Introduce and use function typedef pem_password_cb so that we don't call
[openssl.git] / crypto / pem / pem_all.c
1 /* crypto/pem/pem_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 "cryptlib.h"
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>
67
68 #ifndef NO_FP_API
69 /* The X509 functions */
70 X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb)
71         {
72         return((X509 *)PEM_ASN1_read((char *(*)())d2i_X509,
73                 PEM_STRING_X509,fp,(char **)x,cb));
74         }
75 #endif
76
77 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb)
78         {
79         return((X509 *)PEM_ASN1_read_bio((char *(*)())d2i_X509,
80                 PEM_STRING_X509,bp,(char **)x,cb));
81         }
82
83 #ifndef NO_FP_API
84 int PEM_write_X509(FILE *fp, X509 *x)
85         {
86         return(PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp,
87                 (char *)x, NULL,NULL,0,NULL));
88         }
89 #endif
90
91 int PEM_write_bio_X509(BIO *bp, X509 *x)
92         {
93         return(PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp,
94                 (char *)x, NULL,NULL,0,NULL));
95         }
96
97 #ifndef NO_FP_API
98 /* The X509_REQ functions */
99 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x, pem_password_cb *cb)
100         {
101         return((X509_REQ *)PEM_ASN1_read((char *(*)())d2i_X509_REQ,
102                 PEM_STRING_X509_REQ,fp,(char **)x,cb));
103         }
104 #endif
105
106 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, pem_password_cb *cb)
107         {
108         return((X509_REQ *)PEM_ASN1_read_bio((char *(*)())d2i_X509_REQ,
109                 PEM_STRING_X509_REQ,bp,(char **)x,cb));
110         }
111
112 #ifndef NO_FP_API
113 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x)
114         {
115         return(PEM_ASN1_write((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,
116                 (char *)x, NULL,NULL,0,NULL));
117         }
118 #endif
119
120 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x)
121         {
122         return(PEM_ASN1_write_bio((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,
123                 bp,(char *)x, NULL,NULL,0,NULL));
124         }
125
126 #ifndef NO_FP_API
127 /* The X509_CRL functions */
128 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x, pem_password_cb *cb)
129         {
130         return((X509_CRL *)PEM_ASN1_read((char *(*)())d2i_X509_CRL,
131                 PEM_STRING_X509_CRL,fp,(char **)x,cb));
132         }
133 #endif
134
135 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, pem_password_cb *cb)
136         {
137         return((X509_CRL *)PEM_ASN1_read_bio((char *(*)())d2i_X509_CRL,
138                 PEM_STRING_X509_CRL,bp,(char **)x,cb));
139         }
140
141 #ifndef NO_FP_API
142 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x)
143         {
144         return(PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,fp,
145                 (char *)x, NULL,NULL,0,NULL));
146         }
147 #endif
148
149 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x)
150         {
151         return(PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,
152                 bp,(char *)x, NULL,NULL,0,NULL));
153         }
154
155 #ifndef NO_RSA
156 #ifndef NO_FP_API
157 /* The RSAPrivateKey functions */
158 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x, pem_password_cb *cb)
159         {
160         return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPrivateKey,
161                 PEM_STRING_RSA,fp,(char **)x,cb));
162         }
163
164 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x, pem_password_cb *cb)
165         {
166         return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPublicKey,
167                 PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb));
168         }
169 #endif
170
171 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x, pem_password_cb *cb)
172         {
173         return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPrivateKey,
174                 PEM_STRING_RSA,bp,(char **)x,cb));
175         }
176
177 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, pem_password_cb *cb)
178         {
179         return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPublicKey,
180                 PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb));
181         }
182
183 #ifndef NO_FP_API
184 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, EVP_CIPHER *enc,
185              unsigned char *kstr, int klen, pem_password_cb *cb)
186         {
187         return(PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,
188                 (char *)x,enc,kstr,klen,cb));
189         }
190
191 int PEM_write_RSAPublicKey(FILE *fp, RSA *x)
192         {
193         return(PEM_ASN1_write((int (*)())i2d_RSAPublicKey,
194                 PEM_STRING_RSA_PUBLIC,fp,
195                 (char *)x,NULL,NULL,0,NULL));
196         }
197 #endif
198
199 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
200              unsigned char *kstr, int klen, pem_password_cb *cb)
201         {
202         return(PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,
203                 bp,(char *)x,enc,kstr,klen,cb));
204         }
205
206 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x)
207         {
208         return(PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey,
209                 PEM_STRING_RSA_PUBLIC,
210                 bp,(char *)x,NULL,NULL,0,NULL));
211         }
212 #endif /* !NO_RSA */
213
214 #ifndef NO_DSA
215 #ifndef NO_FP_API
216 /* The DSAPrivateKey functions */
217 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x, pem_password_cb *cb)
218         {
219         return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAPrivateKey,
220                 PEM_STRING_DSA,fp,(char **)x,cb));
221         }
222 #endif
223
224 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x, pem_password_cb *cb)
225         {
226         return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAPrivateKey,
227                 PEM_STRING_DSA,bp,(char **)x,cb));
228         }
229
230 #ifndef NO_FP_API
231 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
232              unsigned char *kstr, int klen, pem_password_cb *cb)
233         {
234         return(PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,
235                 (char *)x,enc,kstr,klen,cb));
236         }
237 #endif
238
239 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
240              unsigned char *kstr, int klen, pem_password_cb *cb)
241         {
242         return(PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,
243                 bp,(char *)x,enc,kstr,klen,cb));
244         }
245 #endif
246
247 #ifndef NO_FP_API
248 /* The PrivateKey functions */
249 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb)
250         {
251         return((EVP_PKEY *)PEM_ASN1_read((char *(*)())d2i_PrivateKey,
252                 PEM_STRING_EVP_PKEY,fp,(char **)x,cb));
253         }
254 #endif
255
256 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb)
257         {
258         return((EVP_PKEY *)PEM_ASN1_read_bio((char *(*)())d2i_PrivateKey,
259                 PEM_STRING_EVP_PKEY,bp,(char **)x,cb));
260         }
261
262 #ifndef NO_FP_API
263 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, EVP_CIPHER *enc,
264              unsigned char *kstr, int klen, pem_password_cb *cb)
265         {
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));
269         }
270 #endif
271
272 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, EVP_CIPHER *enc,
273              unsigned char *kstr, int klen, pem_password_cb *cb)
274         {
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));
278         }
279
280 #ifndef NO_FP_API
281 /* The PKCS7 functions */
282 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb)
283         {
284         return((PKCS7 *)PEM_ASN1_read((char *(*)())d2i_PKCS7,
285                 PEM_STRING_PKCS7,fp,(char **)x,cb));
286         }
287 #endif
288
289 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb)
290         {
291         return((PKCS7 *)PEM_ASN1_read_bio((char *(*)())d2i_PKCS7,
292                 PEM_STRING_PKCS7,bp,(char **)x,cb));
293         }
294
295 #ifndef NO_FP_API
296 int PEM_write_PKCS7(FILE *fp, PKCS7 *x)
297         {
298         return(PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp,
299                 (char *)x, NULL,NULL,0,NULL));
300         }
301 #endif
302
303 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x)
304         {
305         return(PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp,
306                 (char *)x, NULL,NULL,0,NULL));
307         }
308
309 #ifndef NO_DH
310 #ifndef NO_FP_API
311 /* The DHparams functions */
312 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb)
313         {
314         return((DH *)PEM_ASN1_read((char *(*)())d2i_DHparams,
315                 PEM_STRING_DHPARAMS,fp,(char **)x,cb));
316         }
317 #endif
318
319 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb)
320         {
321         return((DH *)PEM_ASN1_read_bio((char *(*)())d2i_DHparams,
322                 PEM_STRING_DHPARAMS,bp,(char **)x,cb));
323         }
324
325 #ifndef NO_FP_API
326 int PEM_write_DHparams(FILE *fp, DH *x)
327         {
328         return(PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,
329                 (char *)x, NULL,NULL,0,NULL));
330         }
331 #endif
332
333 int PEM_write_bio_DHparams(BIO *bp, DH *x)
334         {
335         return(PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,
336                 bp,(char *)x, NULL,NULL,0,NULL));
337         }
338 #endif
339
340 #ifndef NO_DSA
341 #ifndef NO_FP_API
342 /* The DSAparams functions */
343 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb)
344         {
345         return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAparams,
346                 PEM_STRING_DSAPARAMS,fp,(char **)x,cb));
347         }
348 #endif
349
350 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb)
351         {
352         return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAparams,
353                 PEM_STRING_DSAPARAMS,bp,(char **)x,cb));
354         }
355
356 #ifndef NO_FP_API
357 int PEM_write_DSAparams(FILE *fp, DSA *x)
358         {
359         return(PEM_ASN1_write((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,fp,
360                 (char *)x, NULL,NULL,0,NULL));
361         }
362 #endif
363
364 int PEM_write_bio_DSAparams(BIO *bp, DSA *x)
365         {
366         return(PEM_ASN1_write_bio((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,
367                 bp,(char *)x, NULL,NULL,0,NULL));
368         }
369 #endif
370
371 /* The Netscape Certificate sequence functions */
372
373 #ifndef NO_FP_API
374 NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
375              NETSCAPE_CERT_SEQUENCE **x, pem_password_cb *cb)
376         {
377         return((NETSCAPE_CERT_SEQUENCE *)
378                 PEM_ASN1_read((char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,
379                 PEM_STRING_X509,fp,(char **)x,cb));
380         }
381 #endif
382
383 NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
384              NETSCAPE_CERT_SEQUENCE **x, pem_password_cb *cb)
385         {
386         return((NETSCAPE_CERT_SEQUENCE *)
387                 PEM_ASN1_read_bio((char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,
388                 PEM_STRING_X509,bp,(char **)x,cb));
389         }
390
391 #ifndef NO_FP_API
392 int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x)
393         {
394         return(PEM_ASN1_write((int (*)())i2d_NETSCAPE_CERT_SEQUENCE,
395                 PEM_STRING_X509,fp, (char *)x, NULL,NULL,0,NULL));
396         }
397 #endif
398
399 int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x)
400         {
401         return(PEM_ASN1_write_bio((int (*)())i2d_NETSCAPE_CERT_SEQUENCE,
402                 PEM_STRING_X509,bp, (char *)x, NULL,NULL,0,NULL));
403         }
404