1 /* $MirOS: src/lib/libssl/src/crypto/pem/pem.h,v 1.2 2006/03/27 19:30:36 tg Exp $ */
2 /* crypto/pem/pem.h */
3 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
4  * Copyright (c) 2006 Thorsten Glaser (tg@mirbsd.de)
5  * All rights reserved.
6  *
7  * This package is an SSL implementation written
8  * by Eric Young (eay@cryptsoft.com).
9  * The implementation was written so as to conform with Netscapes SSL.
10  *
11  * This library is free for commercial and non-commercial use as long as
12  * the following conditions are aheared to.  The following conditions
13  * apply to all code found in this distribution, be it the RC4, RSA,
14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15  * included with this distribution is covered by the same copyright terms
16  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17  *
18  * Copyright remains Eric Young's, and as such any Copyright notices in
19  * the code are not to be removed.
20  * If this package is used in a product, Eric Young should be given attribution
21  * as the author of the parts of the library used.
22  * This can be in the form of a textual message at program startup or
23  * in documentation (online or textual) provided with the package.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *    "This product includes cryptographic software written by
36  *     Eric Young (eay@cryptsoft.com)"
37  *    The word 'cryptographic' can be left out if the rouines from the library
38  *    being used are not cryptographic related :-).
39  * 4. If you include any Windows specific code (or a derivative thereof) from
40  *    the apps directory (application code) you must include an acknowledgement:
41  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42  *
43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  * The licence and distribution terms for any publically available version or
56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57  * copied and put under another distribution licence
58  * [including the GNU Public Licence.]
59  */
60 
61 #ifndef HEADER_PEM_H
62 #define HEADER_PEM_H
63 
64 #ifndef OPENSSL_NO_BIO
65 #include <openssl/bio.h>
66 #endif
67 #ifndef OPENSSL_NO_STACK
68 #include <openssl/stack.h>
69 #endif
70 #include <openssl/evp.h>
71 #include <openssl/x509.h>
72 #include <openssl/pem2.h>
73 #include <openssl/e_os2.h>
74 
75 #ifdef  __cplusplus
76 extern "C" {
77 #endif
78 
79 #define PEM_BUFSIZE		1024
80 
81 #define PEM_OBJ_UNDEF		0
82 #define PEM_OBJ_X509		1
83 #define PEM_OBJ_X509_REQ	2
84 #define PEM_OBJ_CRL		3
85 #define PEM_OBJ_SSL_SESSION	4
86 #define PEM_OBJ_PRIV_KEY	10
87 #define PEM_OBJ_PRIV_RSA	11
88 #define PEM_OBJ_PRIV_DSA	12
89 #define PEM_OBJ_PRIV_DH		13
90 #define PEM_OBJ_PUB_RSA		14
91 #define PEM_OBJ_PUB_DSA		15
92 #define PEM_OBJ_PUB_DH		16
93 #define PEM_OBJ_DHPARAMS	17
94 #define PEM_OBJ_DSAPARAMS	18
95 #define PEM_OBJ_PRIV_RSA_PUBLIC	19
96 
97 #define PEM_ERROR		30
98 #define PEM_DEK_DES_CBC         40
99 #define PEM_DEK_IDEA_CBC        45
100 #define PEM_DEK_DES_EDE         50
101 #define PEM_DEK_DES_ECB         60
102 #define PEM_DEK_RSA             70
103 #define PEM_DEK_RSA_MD2         80
104 #define PEM_DEK_RSA_MD5         90
105 
106 #define PEM_MD_MD2		NID_md2
107 #define PEM_MD_MD5		NID_md5
108 #define PEM_MD_SHA		NID_sha
109 #define PEM_MD_MD2_RSA		NID_md2WithRSAEncryption
110 #define PEM_MD_MD5_RSA		NID_md5WithRSAEncryption
111 #define PEM_MD_SHA_RSA		NID_sha1WithRSAEncryption
112 
113 #define PEM_STRING_X509_OLD	"X509 CERTIFICATE"
114 #define PEM_STRING_X509		"CERTIFICATE"
115 #define PEM_STRING_X509_TRUSTED	"TRUSTED CERTIFICATE"
116 #define PEM_STRING_X509_REQ_OLD	"NEW CERTIFICATE REQUEST"
117 #define PEM_STRING_X509_REQ	"CERTIFICATE REQUEST"
118 #define PEM_STRING_X509_CRL	"X509 CRL"
119 #define PEM_STRING_EVP_PKEY	"ANY PRIVATE KEY"
120 #define PEM_STRING_PUBLIC	"PUBLIC KEY"
121 #define PEM_STRING_RSA		"RSA PRIVATE KEY"
122 #define PEM_STRING_RSA_PUBLIC	"RSA PUBLIC KEY"
123 #define PEM_STRING_DSA		"DSA PRIVATE KEY"
124 #define PEM_STRING_DSA_PUBLIC	"DSA PUBLIC KEY"
125 #define PEM_STRING_PKCS7	"PKCS7"
126 #define PEM_STRING_PKCS8	"ENCRYPTED PRIVATE KEY"
127 #define PEM_STRING_PKCS8INF	"PRIVATE KEY"
128 #define PEM_STRING_DHPARAMS	"DH PARAMETERS"
129 #define PEM_STRING_SSL_SESSION	"SSL SESSION PARAMETERS"
130 #define PEM_STRING_DSAPARAMS	"DSA PARAMETERS"
131 #define PEM_STRING_ASN1_OCTET_STRING	"ASN1 OCTET STRING"
132 
133   /* Note that this structure is initialised by PEM_SealInit and cleaned up
134      by PEM_SealFinal (at least for now) */
135 typedef struct PEM_Encode_Seal_st
136 	{
137 	EVP_ENCODE_CTX encode;
138 	EVP_MD_CTX md;
139 	EVP_CIPHER_CTX cipher;
140 	} PEM_ENCODE_SEAL_CTX;
141 
142 /* enc_type is one off */
143 #define PEM_TYPE_ENCRYPTED      10
144 #define PEM_TYPE_MIC_ONLY       20
145 #define PEM_TYPE_MIC_CLEAR      30
146 #define PEM_TYPE_CLEAR		40
147 
148 typedef struct pem_recip_st
149 	{
150 	char *name;
151 	X509_NAME *dn;
152 
153 	int cipher;
154 	int key_enc;
155 	/*	char iv[8]; unused and wrong size */
156 	} PEM_USER;
157 
158 typedef struct pem_ctx_st
159 	{
160 	int type;		/* what type of object */
161 
162 	struct	{
163 		int version;
164 		int mode;
165 		} proc_type;
166 
167 	char *domain;
168 
169 	struct	{
170 		int cipher;
171 	/* unused, and wrong size
172 	   unsigned char iv[8]; */
173 		} DEK_info;
174 
175 	PEM_USER *originator;
176 
177 	int num_recipient;
178 	PEM_USER **recipient;
179 
180 #ifndef OPENSSL_NO_STACK
181 	STACK *x509_chain;	/* certificate chain */
182 #else
183 	char *x509_chain;	/* certificate chain */
184 #endif
185 	EVP_MD *md;		/* signature type */
186 
187 	int md_enc;		/* is the md encrypted or not? */
188 	int md_len;		/* length of md_data */
189 	char *md_data;		/* message digest, could be pkey encrypted */
190 
191 	EVP_CIPHER *dec;	/* date encryption cipher */
192 	int key_len;		/* key length */
193 	unsigned char *key;	/* key */
194 	/* unused, and wrong size
195 	   unsigned char iv[8]; */
196 
197 
198 	int  data_enc;		/* is the data encrypted */
199 	int data_len;
200 	unsigned char *data;
201 	} PEM_CTX;
202 
203 /* These macros make the PEM_read/PEM_write functions easier to maintain and
204  * write. Now they are all implemented with either:
205  * IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
206  */
207 
208 #ifdef OPENSSL_NO_FP_API
209 
210 #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
211 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
212 #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
213 
214 #else
215 
216 #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
217 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
218 { \
219 return((type *)PEM_ASN1_read((char *(*)())d2i_##asn1, str,fp,(char **)x,\
220 	cb,u)); \
221 } \
222 
223 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
224 int PEM_write_##name(FILE *fp, type *x) \
225 { \
226 return(PEM_ASN1_write((int (*)())i2d_##asn1,str,fp, (char *)x, \
227 							 NULL,NULL,0,NULL,NULL)); \
228 }
229 
230 #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
231 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
232 	     unsigned char *kstr, int klen, pem_password_cb *cb, \
233 		  void *u) \
234 	{ \
235 	return(PEM_ASN1_write((int (*)())i2d_##asn1,str,fp, \
236 		(char *)x,enc,kstr,klen,cb,u)); \
237 	}
238 
239 #endif
240 
241 #define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
242 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
243 { \
244 return((type *)PEM_ASN1_read_bio((char *(*)())d2i_##asn1, str,bp,\
245 							(char **)x,cb,u)); \
246 }
247 
248 #define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
249 int PEM_write_bio_##name(BIO *bp, type *x) \
250 { \
251 return(PEM_ASN1_write_bio((int (*)())i2d_##asn1,str,bp, (char *)x, \
252 							 NULL,NULL,0,NULL,NULL)); \
253 }
254 
255 #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
256 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
257 	     unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
258 	{ \
259 	return(PEM_ASN1_write_bio((int (*)())i2d_##asn1,str,bp, \
260 		(char *)x,enc,kstr,klen,cb,u)); \
261 	}
262 
263 #define IMPLEMENT_PEM_write(name, type, str, asn1) \
264 	IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
265 	IMPLEMENT_PEM_write_fp(name, type, str, asn1)
266 
267 #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
268 	IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
269 	IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
270 
271 #define IMPLEMENT_PEM_read(name, type, str, asn1) \
272 	IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
273 	IMPLEMENT_PEM_read_fp(name, type, str, asn1)
274 
275 #define IMPLEMENT_PEM_rw(name, type, str, asn1) \
276 	IMPLEMENT_PEM_read(name, type, str, asn1) \
277 	IMPLEMENT_PEM_write(name, type, str, asn1)
278 
279 #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
280 	IMPLEMENT_PEM_read(name, type, str, asn1) \
281 	IMPLEMENT_PEM_write_cb(name, type, str, asn1)
282 
283 /* These are the same except they are for the declarations */
284 
285 #if defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_NO_FP_API)
286 
287 #define DECLARE_PEM_read_fp(name, type) /**/
288 #define DECLARE_PEM_write_fp(name, type) /**/
289 #define DECLARE_PEM_write_cb_fp(name, type) /**/
290 
291 #else
292 
293 #define DECLARE_PEM_read_fp(name, type) \
294 	type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
295 
296 #define DECLARE_PEM_write_fp(name, type) \
297 	int PEM_write_##name(FILE *fp, type *x);
298 
299 #define DECLARE_PEM_write_cb_fp(name, type) \
300 	int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
301 	     unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
302 
303 #endif
304 
305 #ifndef OPENSSL_NO_BIO
306 #define DECLARE_PEM_read_bio(name, type) \
307 	type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
308 
309 #define DECLARE_PEM_write_bio(name, type) \
310 	int PEM_write_bio_##name(BIO *bp, type *x);
311 
312 #define DECLARE_PEM_write_cb_bio(name, type) \
313 	int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
314 	     unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
315 
316 #else
317 
318 #define DECLARE_PEM_read_bio(name, type) /**/
319 #define DECLARE_PEM_write_bio(name, type) /**/
320 #define DECLARE_PEM_write_cb_bio(name, type) /**/
321 
322 #endif
323 
324 #define DECLARE_PEM_write(name, type) \
325 	DECLARE_PEM_write_bio(name, type) \
326 	DECLARE_PEM_write_fp(name, type)
327 
328 #define DECLARE_PEM_write_cb(name, type) \
329 	DECLARE_PEM_write_cb_bio(name, type) \
330 	DECLARE_PEM_write_cb_fp(name, type)
331 
332 #define DECLARE_PEM_read(name, type) \
333 	DECLARE_PEM_read_bio(name, type) \
334 	DECLARE_PEM_read_fp(name, type)
335 
336 #define DECLARE_PEM_rw(name, type) \
337 	DECLARE_PEM_read(name, type) \
338 	DECLARE_PEM_write(name, type)
339 
340 #define DECLARE_PEM_rw_cb(name, type) \
341 	DECLARE_PEM_read(name, type) \
342 	DECLARE_PEM_write_cb(name, type)
343 
344 #ifdef SSLEAY_MACROS
345 
346 #define PEM_write_SSL_SESSION(fp,x) \
347 		PEM_ASN1_write((int (*)())i2d_SSL_SESSION, \
348 			PEM_STRING_SSL_SESSION,fp, (char *)x, NULL,NULL,0,NULL,NULL)
349 #define PEM_write_X509(fp,x) \
350 		PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp, \
351 			(char *)x, NULL,NULL,0,NULL,NULL)
352 #define PEM_write_X509_REQ(fp,x) PEM_ASN1_write( \
353 		(int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,(char *)x, \
354 			NULL,NULL,0,NULL,NULL)
355 #define PEM_write_X509_CRL(fp,x) \
356 		PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL, \
357 			fp,(char *)x, NULL,NULL,0,NULL,NULL)
358 #define	PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb,u) \
359 		PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,\
360 			(char *)x,enc,kstr,klen,cb,u)
361 #define	PEM_write_RSAPublicKey(fp,x) \
362 		PEM_ASN1_write((int (*)())i2d_RSAPublicKey,\
363 			PEM_STRING_RSA_PUBLIC,fp,(char *)x,NULL,NULL,0,NULL,NULL)
364 #define	PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb,u) \
365 		PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,\
366 			(char *)x,enc,kstr,klen,cb,u)
367 #define	PEM_write_PrivateKey(bp,x,enc,kstr,klen,cb,u) \
368 		PEM_ASN1_write((int (*)())i2d_PrivateKey,\
369 		(((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
370 			bp,(char *)x,enc,kstr,klen,cb,u)
371 #define PEM_write_PKCS7(fp,x) \
372 		PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp, \
373 			(char *)x, NULL,NULL,0,NULL,NULL)
374 #define PEM_write_DHparams(fp,x) \
375 		PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,\
376 			(char *)x,NULL,NULL,0,NULL,NULL)
377 
378 #define PEM_write_NETSCAPE_CERT_SEQUENCE(fp,x) \
379                 PEM_ASN1_write((int (*)())i2d_NETSCAPE_CERT_SEQUENCE, \
380 			PEM_STRING_X509,fp, \
381                         (char *)x, NULL,NULL,0,NULL,NULL)
382 
383 #define	PEM_write_ASN1_OCTET_STRING(fp,x,enc,kstr,klen,cb,u) \
384 		PEM_ASN1_write((int (*)())i2d_ASN1_OCTET_STRING,PEM_STRING_ASN1_OCTET_STRING,fp,\
385 			(char *)x,enc,kstr,klen,cb,u)
386 
387 #define	PEM_read_SSL_SESSION(fp,x,cb,u) (SSL_SESSION *)PEM_ASN1_read( \
388 	(char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,fp,(char **)x,cb,u)
389 #define	PEM_read_X509(fp,x,cb,u) (X509 *)PEM_ASN1_read( \
390 	(char *(*)())d2i_X509,PEM_STRING_X509,fp,(char **)x,cb,u)
391 #define	PEM_read_X509_REQ(fp,x,cb,u) (X509_REQ *)PEM_ASN1_read( \
392 	(char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,fp,(char **)x,cb,u)
393 #define	PEM_read_X509_CRL(fp,x,cb,u) (X509_CRL *)PEM_ASN1_read( \
394 	(char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,fp,(char **)x,cb,u)
395 #define	PEM_read_RSAPrivateKey(fp,x,cb,u) (RSA *)PEM_ASN1_read( \
396 	(char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,fp,(char **)x,cb,u)
397 #define	PEM_read_RSAPublicKey(fp,x,cb,u) (RSA *)PEM_ASN1_read( \
398 	(char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb,u)
399 #define	PEM_read_DSAPrivateKey(fp,x,cb,u) (DSA *)PEM_ASN1_read( \
400 	(char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,fp,(char **)x,cb,u)
401 #define	PEM_read_PrivateKey(fp,x,cb,u) (EVP_PKEY *)PEM_ASN1_read( \
402 	(char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,fp,(char **)x,cb,u)
403 #define	PEM_read_PKCS7(fp,x,cb,u) (PKCS7 *)PEM_ASN1_read( \
404 	(char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,fp,(char **)x,cb,u)
405 #define	PEM_read_DHparams(fp,x,cb,u) (DH *)PEM_ASN1_read( \
406 	(char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,fp,(char **)x,cb,u)
407 
408 #define PEM_read_NETSCAPE_CERT_SEQUENCE(fp,x,cb,u) \
409 		(NETSCAPE_CERT_SEQUENCE *)PEM_ASN1_read( \
410         (char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,PEM_STRING_X509,fp,\
411 							(char **)x,cb,u)
412 
413 #define	PEM_read_ASN1_OCTET_STRING(fp,x,cb,u) (ASN1_OCTET_STRING *)PEM_ASN1_read( \
414 	(char *(*)())d2i_ASN1_OCTET_STRING,PEM_STRING_ASN1_OCTET_STRING,fp,(char **)x,cb,u)
415 
416 #define PEM_write_bio_SSL_SESSION(bp,x) \
417 		PEM_ASN1_write_bio((int (*)())i2d_SSL_SESSION, \
418 			PEM_STRING_SSL_SESSION,bp, (char *)x, NULL,NULL,0,NULL,NULL)
419 #define PEM_write_bio_X509(bp,x) \
420 		PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp, \
421 			(char *)x, NULL,NULL,0,NULL,NULL)
422 #define PEM_write_bio_X509_REQ(bp,x) PEM_ASN1_write_bio( \
423 		(int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,bp,(char *)x, \
424 			NULL,NULL,0,NULL,NULL)
425 #define PEM_write_bio_X509_CRL(bp,x) \
426 		PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,\
427 			bp,(char *)x, NULL,NULL,0,NULL,NULL)
428 #define	PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb,u) \
429 		PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,\
430 			bp,(char *)x,enc,kstr,klen,cb,u)
431 #define	PEM_write_bio_RSAPublicKey(bp,x) \
432 		PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey, \
433 			PEM_STRING_RSA_PUBLIC,\
434 			bp,(char *)x,NULL,NULL,0,NULL,NULL)
435 #define	PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb,u) \
436 		PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,\
437 			bp,(char *)x,enc,kstr,klen,cb,u)
438 #define	PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb,u) \
439 		PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,\
440 		(((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
441 			bp,(char *)x,enc,kstr,klen,cb,u)
442 #define PEM_write_bio_PKCS7(bp,x) \
443 		PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp, \
444 			(char *)x, NULL,NULL,0,NULL,NULL)
445 #define PEM_write_bio_DHparams(bp,x) \
446 		PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,\
447 			bp,(char *)x,NULL,NULL,0,NULL,NULL)
448 #define PEM_write_bio_DSAparams(bp,x) \
449 		PEM_ASN1_write_bio((int (*)())i2d_DSAparams, \
450 			PEM_STRING_DSAPARAMS,bp,(char *)x,NULL,NULL,0,NULL,NULL)
451 
452 #define PEM_write_bio_NETSCAPE_CERT_SEQUENCE(bp,x) \
453                 PEM_ASN1_write_bio((int (*)())i2d_NETSCAPE_CERT_SEQUENCE, \
454 			PEM_STRING_X509,bp, \
455                         (char *)x, NULL,NULL,0,NULL,NULL)
456 
457 #define	PEM_write_bio_ASN1_OCTET_STRING(bp,x,enc,kstr,klen,cb,u) \
458 		PEM_ASN1_write_bio((int (*)())i2d_ASN1_OCTET_STRING,PEM_STRING_ASN1_OCTET_STRING,\
459 			bp,(char *)x,enc,kstr,klen,cb,u)
460 
461 #define	PEM_read_bio_SSL_SESSION(bp,x,cb,u) (SSL_SESSION *)PEM_ASN1_read_bio( \
462 	(char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,bp,(char **)x,cb,u)
463 #define	PEM_read_bio_X509(bp,x,cb,u) (X509 *)PEM_ASN1_read_bio( \
464 	(char *(*)())d2i_X509,PEM_STRING_X509,bp,(char **)x,cb,u)
465 #define	PEM_read_bio_X509_REQ(bp,x,cb,u) (X509_REQ *)PEM_ASN1_read_bio( \
466 	(char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,bp,(char **)x,cb,u)
467 #define	PEM_read_bio_X509_CRL(bp,x,cb,u) (X509_CRL *)PEM_ASN1_read_bio( \
468 	(char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,bp,(char **)x,cb,u)
469 #define	PEM_read_bio_RSAPrivateKey(bp,x,cb,u) (RSA *)PEM_ASN1_read_bio( \
470 	(char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,bp,(char **)x,cb,u)
471 #define	PEM_read_bio_RSAPublicKey(bp,x,cb,u) (RSA *)PEM_ASN1_read_bio( \
472 	(char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb,u)
473 #define	PEM_read_bio_DSAPrivateKey(bp,x,cb,u) (DSA *)PEM_ASN1_read_bio( \
474 	(char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,bp,(char **)x,cb,u)
475 #define	PEM_read_bio_PrivateKey(bp,x,cb,u) (EVP_PKEY *)PEM_ASN1_read_bio( \
476 	(char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,bp,(char **)x,cb,u)
477 
478 #define	PEM_read_bio_PKCS7(bp,x,cb,u) (PKCS7 *)PEM_ASN1_read_bio( \
479 	(char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,bp,(char **)x,cb,u)
480 #define	PEM_read_bio_DHparams(bp,x,cb,u) (DH *)PEM_ASN1_read_bio( \
481 	(char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,bp,(char **)x,cb,u)
482 #define	PEM_read_bio_DSAparams(bp,x,cb,u) (DSA *)PEM_ASN1_read_bio( \
483 	(char *(*)())d2i_DSAparams,PEM_STRING_DSAPARAMS,bp,(char **)x,cb,u)
484 
485 #define PEM_read_bio_NETSCAPE_CERT_SEQUENCE(bp,x,cb,u) \
486 		(NETSCAPE_CERT_SEQUENCE *)PEM_ASN1_read_bio( \
487         (char *(*)())d2i_NETSCAPE_CERT_SEQUENCE,PEM_STRING_X509,bp,\
488 							(char **)x,cb,u)
489 
490 #define	PEM_read_bio_ASN1_OCTET_STRING(bp,x,cb,u) (ASN1_OCTET_STRING *)PEM_ASN1_read_bio( \
491 	(char *(*)())d2i_ASN1_OCTET_STRING,PEM_STRING_ASN1_OCTET_STRING,bp,(char **)x,cb,u)
492 
493 #endif
494 
495 #if 1
496 /* "userdata": new with OpenSSL 0.9.4 */
497 typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
498 #else
499 /* OpenSSL 0.9.3, 0.9.3a */
500 typedef int pem_password_cb(char *buf, int size, int rwflag);
501 #endif
502 
503 int	PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
504 int	PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data,long *len,
505 	pem_password_cb *callback,void *u);
506 
507 #ifndef OPENSSL_NO_BIO
508 int	PEM_read_bio(BIO *bp, char **name, char **header,
509 		unsigned char **data,long *len);
510 int	PEM_write_bio(BIO *bp,const char *name,char *hdr,unsigned char *data,
511 		long len);
512 int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp,
513 	     pem_password_cb *cb, void *u);
514 char *	PEM_ASN1_read_bio(char *(*d2i)(),const char *name,BIO *bp,char **x,
515 		pem_password_cb *cb, void *u);
516 int	PEM_ASN1_write_bio(int (*i2d)(),const char *name,BIO *bp,char *x,
517 			   const EVP_CIPHER *enc,unsigned char *kstr,int klen,
518 			   pem_password_cb *cb, void *u);
519 STACK_OF(X509_INFO) *	PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
520 int	PEM_X509_INFO_write_bio(BIO *bp,X509_INFO *xi, EVP_CIPHER *enc,
521 		unsigned char *kstr, int klen, pem_password_cb *cd, void *u);
522 #endif
523 
524 #ifndef OPENSSL_SYS_WIN16
525 int	PEM_read(FILE *fp, char **name, char **header,
526 		unsigned char **data,long *len);
527 int	PEM_write(FILE *fp,char *name,char *hdr,unsigned char *data,long len);
528 char *	PEM_ASN1_read(char *(*d2i)(),const char *name,FILE *fp,char **x,
529 	pem_password_cb *cb, void *u);
530 int	PEM_ASN1_write(int (*i2d)(),const char *name,FILE *fp,char *x,
531 		       const EVP_CIPHER *enc,unsigned char *kstr,int klen,
532 		       pem_password_cb *callback, void *u);
533 STACK_OF(X509_INFO) *	PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
534 	pem_password_cb *cb, void *u);
535 #endif
536 
537 int	PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type,
538 		EVP_MD *md_type, unsigned char **ek, int *ekl,
539 		unsigned char *iv, EVP_PKEY **pubk, int npubk);
540 void	PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
541 		unsigned char *in, int inl);
542 int	PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig,int *sigl,
543 		unsigned char *out, int *outl, EVP_PKEY *priv);
544 
545 void    PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
546 void    PEM_SignUpdate(EVP_MD_CTX *ctx,unsigned char *d,unsigned int cnt);
547 int	PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
548 		unsigned int *siglen, EVP_PKEY *pkey);
549 
550 int	PEM_def_callback(char *buf, int num, int w, void *key);
551 void	PEM_proc_type(char *buf, int type);
552 void	PEM_dek_info(char *buf, const char *type, int len, char *str);
553 
554 #ifndef SSLEAY_MACROS
555 
556 #include <openssl/symhacks.h>
557 
558 DECLARE_PEM_rw(X509, X509)
559 
560 DECLARE_PEM_rw(X509_AUX, X509)
561 
562 DECLARE_PEM_rw(X509_REQ, X509_REQ)
563 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
564 
565 DECLARE_PEM_rw(X509_CRL, X509_CRL)
566 
567 DECLARE_PEM_rw(PKCS7, PKCS7)
568 
569 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
570 
571 DECLARE_PEM_rw(PKCS8, X509_SIG)
572 
573 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
574 
575 #ifndef OPENSSL_NO_RSA
576 
577 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
578 
579 DECLARE_PEM_rw(RSAPublicKey, RSA)
580 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
581 
582 #endif
583 
584 #ifndef OPENSSL_NO_DSA
585 
586 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
587 
588 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
589 
590 DECLARE_PEM_rw(DSAparams, DSA)
591 
592 #endif
593 
594 #ifndef OPENSSL_NO_DH
595 
596 DECLARE_PEM_rw(DHparams, DH)
597 
598 #endif
599 
600 DECLARE_PEM_rw_cb(ASN1_OCTET_STRING, ASN1_OCTET_STRING)
601 
602 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
603 
604 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
605 
606 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
607 				  char *kstr, int klen,
608 				  pem_password_cb *cb, void *u);
609 int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *,
610                                   char *, int, pem_password_cb *, void *);
611 int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
612 				  char *kstr, int klen,
613 				  pem_password_cb *cb, void *u);
614 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
615 				  char *kstr, int klen,
616 				  pem_password_cb *cb, void *u);
617 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u);
618 
619 int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
620 				  char *kstr, int klen,
621 				  pem_password_cb *cb, void *u);
622 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
623 				  char *kstr, int klen,
624 				  pem_password_cb *cb, void *u);
625 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
626 				  char *kstr, int klen,
627 				  pem_password_cb *cb, void *u);
628 
629 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u);
630 
631 int PEM_write_PKCS8PrivateKey(FILE *fp,EVP_PKEY *x,const EVP_CIPHER *enc,
632 			      char *kstr,int klen, pem_password_cb *cd, void *u);
633 
634 #endif /* SSLEAY_MACROS */
635 
636 
637 /* BEGIN ERROR CODES */
638 /* The following lines are auto generated by the script mkerr.pl. Any changes
639  * made after this point may be overwritten when the script is next run.
640  */
641 void ERR_load_PEM_strings(void);
642 
643 /* Error codes for the PEM functions. */
644 
645 /* Function codes. */
646 #define PEM_F_D2I_PKCS8PRIVATEKEY_BIO			 120
647 #define PEM_F_D2I_PKCS8PRIVATEKEY_FP			 121
648 #define PEM_F_DEF_CALLBACK				 100
649 #define PEM_F_LOAD_IV					 101
650 #define PEM_F_PEM_ASN1_READ				 102
651 #define PEM_F_PEM_ASN1_READ_BIO				 103
652 #define PEM_F_PEM_ASN1_WRITE				 104
653 #define PEM_F_PEM_ASN1_WRITE_BIO			 105
654 #define PEM_F_PEM_DO_HEADER				 106
655 #define PEM_F_PEM_F_DO_PK8KEY_FP			 122
656 #define PEM_F_PEM_F_PEM_WRITE_PKCS8PRIVATEKEY		 118
657 #define PEM_F_PEM_GET_EVP_CIPHER_INFO			 107
658 #define PEM_F_PEM_READ					 108
659 #define PEM_F_PEM_READ_BIO				 109
660 #define PEM_F_PEM_SEALFINAL				 110
661 #define PEM_F_PEM_SEALINIT				 111
662 #define PEM_F_PEM_SIGNFINAL				 112
663 #define PEM_F_PEM_WRITE					 113
664 #define PEM_F_PEM_WRITE_BIO				 114
665 #define PEM_F_PEM_WRITE_BIO_PKCS8PRIVATEKEY		 119
666 #define PEM_F_PEM_X509_INFO_READ			 115
667 #define PEM_F_PEM_X509_INFO_READ_BIO			 116
668 #define PEM_F_PEM_X509_INFO_WRITE_BIO			 117
669 
670 /* Reason codes. */
671 #define PEM_R_BAD_BASE64_DECODE				 100
672 #define PEM_R_BAD_DECRYPT				 101
673 #define PEM_R_BAD_END_LINE				 102
674 #define PEM_R_BAD_IV_CHARS				 103
675 #define PEM_R_BAD_PASSWORD_READ				 104
676 #define PEM_R_ERROR_CONVERTING_PRIVATE_KEY		 115
677 #define PEM_R_NOT_DEK_INFO				 105
678 #define PEM_R_NOT_ENCRYPTED				 106
679 #define PEM_R_NOT_PROC_TYPE				 107
680 #define PEM_R_NO_START_LINE				 108
681 #define PEM_R_PROBLEMS_GETTING_PASSWORD			 109
682 #define PEM_R_PUBLIC_KEY_NO_RSA				 110
683 #define PEM_R_READ_KEY					 111
684 #define PEM_R_SHORT_HEADER				 112
685 #define PEM_R_UNSUPPORTED_CIPHER			 113
686 #define PEM_R_UNSUPPORTED_ENCRYPTION			 114
687 
688 #ifdef  __cplusplus
689 }
690 #endif
691 #endif
692