62 #include <openssl/e_os2.h>
63 #ifndef OPENSSL_NO_BIO
64 #include <openssl/bio.h>
66 #ifndef OPENSSL_NO_STACK
67 #include <openssl/stack.h>
69 #include <openssl/evp.h>
70 #include <openssl/x509.h>
71 #include <openssl/pem2.h>
77 #define PEM_BUFSIZE 1024
79 #define PEM_OBJ_UNDEF 0
80 #define PEM_OBJ_X509 1
81 #define PEM_OBJ_X509_REQ 2
83 #define PEM_OBJ_SSL_SESSION 4
84 #define PEM_OBJ_PRIV_KEY 10
85 #define PEM_OBJ_PRIV_RSA 11
86 #define PEM_OBJ_PRIV_DSA 12
87 #define PEM_OBJ_PRIV_DH 13
88 #define PEM_OBJ_PUB_RSA 14
89 #define PEM_OBJ_PUB_DSA 15
90 #define PEM_OBJ_PUB_DH 16
91 #define PEM_OBJ_DHPARAMS 17
92 #define PEM_OBJ_DSAPARAMS 18
93 #define PEM_OBJ_PRIV_RSA_PUBLIC 19
94 #define PEM_OBJ_PRIV_ECDSA 20
95 #define PEM_OBJ_PUB_ECDSA 21
96 #define PEM_OBJ_ECPARAMETERS 22
99 #define PEM_DEK_DES_CBC 40
100 #define PEM_DEK_IDEA_CBC 45
101 #define PEM_DEK_DES_EDE 50
102 #define PEM_DEK_DES_ECB 60
103 #define PEM_DEK_RSA 70
104 #define PEM_DEK_RSA_MD2 80
105 #define PEM_DEK_RSA_MD5 90
107 #define PEM_MD_MD2 NID_md2
108 #define PEM_MD_MD5 NID_md5
109 #define PEM_MD_SHA NID_sha
110 #define PEM_MD_MD2_RSA NID_md2WithRSAEncryption
111 #define PEM_MD_MD5_RSA NID_md5WithRSAEncryption
112 #define PEM_MD_SHA_RSA NID_sha1WithRSAEncryption
114 #define PEM_STRING_X509_OLD "X509 CERTIFICATE"
115 #define PEM_STRING_X509 "CERTIFICATE"
116 #define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
117 #define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
118 #define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
119 #define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
120 #define PEM_STRING_X509_CRL "X509 CRL"
121 #define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
122 #define PEM_STRING_PUBLIC "PUBLIC KEY"
123 #define PEM_STRING_RSA "RSA PRIVATE KEY"
124 #define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
125 #define PEM_STRING_DSA "DSA PRIVATE KEY"
126 #define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
127 #define PEM_STRING_PKCS7 "PKCS7"
128 #define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
129 #define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
130 #define PEM_STRING_PKCS8INF "PRIVATE KEY"
131 #define PEM_STRING_DHPARAMS "DH PARAMETERS"
132 #define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
133 #define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
134 #define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
135 #define PEM_STRING_ECPARAMETERS "EC PARAMETERS"
136 #define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
137 #define PEM_STRING_PARAMETERS "PARAMETERS"
138 #define PEM_STRING_CMS "CMS"
150 #define PEM_TYPE_ENCRYPTED 10
151 #define PEM_TYPE_MIC_ONLY 20
152 #define PEM_TYPE_MIC_CLEAR 30
153 #define PEM_TYPE_CLEAR 40
212 #ifdef OPENSSL_NO_FP_API
214 #define IMPLEMENT_PEM_read_fp(name, type, str, asn1)
215 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1)
216 #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
217 #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
218 #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
222 #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
223 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
225 return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
228 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
229 int PEM_write_##name(FILE *fp, type *x) \
231 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
234 #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
235 int PEM_write_##name(FILE *fp, const type *x) \
237 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
240 #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
241 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
242 unsigned char *kstr, int klen, pem_password_cb *cb, \
245 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
248 #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
249 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
250 unsigned char *kstr, int klen, pem_password_cb *cb, \
253 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
258 #define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
259 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
261 return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
264 #define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
265 int PEM_write_bio_##name(BIO *bp, type *x) \
267 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
270 #define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
271 int PEM_write_bio_##name(BIO *bp, const type *x) \
273 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
276 #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
277 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
278 unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
280 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
283 #define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
284 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
285 unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
287 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
290 #define IMPLEMENT_PEM_write(name, type, str, asn1) \
291 IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
292 IMPLEMENT_PEM_write_fp(name, type, str, asn1)
294 #define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
295 IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
296 IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
298 #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
299 IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
300 IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
302 #define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
303 IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
304 IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
306 #define IMPLEMENT_PEM_read(name, type, str, asn1) \
307 IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
308 IMPLEMENT_PEM_read_fp(name, type, str, asn1)
310 #define IMPLEMENT_PEM_rw(name, type, str, asn1) \
311 IMPLEMENT_PEM_read(name, type, str, asn1) \
312 IMPLEMENT_PEM_write(name, type, str, asn1)
314 #define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
315 IMPLEMENT_PEM_read(name, type, str, asn1) \
316 IMPLEMENT_PEM_write_const(name, type, str, asn1)
318 #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
319 IMPLEMENT_PEM_read(name, type, str, asn1) \
320 IMPLEMENT_PEM_write_cb(name, type, str, asn1)
324 #if defined(OPENSSL_NO_FP_API)
326 #define DECLARE_PEM_read_fp(name, type)
327 #define DECLARE_PEM_write_fp(name, type)
328 #define DECLARE_PEM_write_cb_fp(name, type)
332 #define DECLARE_PEM_read_fp(name, type) \
333 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
335 #define DECLARE_PEM_write_fp(name, type) \
336 int PEM_write_##name(FILE *fp, type *x);
338 #define DECLARE_PEM_write_fp_const(name, type) \
339 int PEM_write_##name(FILE *fp, const type *x);
341 #define DECLARE_PEM_write_cb_fp(name, type) \
342 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
343 unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
347 #ifndef OPENSSL_NO_BIO
348 #define DECLARE_PEM_read_bio(name, type) \
349 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
351 #define DECLARE_PEM_write_bio(name, type) \
352 int PEM_write_bio_##name(BIO *bp, type *x);
354 #define DECLARE_PEM_write_bio_const(name, type) \
355 int PEM_write_bio_##name(BIO *bp, const type *x);
357 #define DECLARE_PEM_write_cb_bio(name, type) \
358 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
359 unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
363 #define DECLARE_PEM_read_bio(name, type)
364 #define DECLARE_PEM_write_bio(name, type)
365 #define DECLARE_PEM_write_bio_const(name, type)
366 #define DECLARE_PEM_write_cb_bio(name, type)
370 #define DECLARE_PEM_write(name, type) \
371 DECLARE_PEM_write_bio(name, type) \
372 DECLARE_PEM_write_fp(name, type)
374 #define DECLARE_PEM_write_const(name, type) \
375 DECLARE_PEM_write_bio_const(name, type) \
376 DECLARE_PEM_write_fp_const(name, type)
378 #define DECLARE_PEM_write_cb(name, type) \
379 DECLARE_PEM_write_cb_bio(name, type) \
380 DECLARE_PEM_write_cb_fp(name, type)
382 #define DECLARE_PEM_read(name, type) \
383 DECLARE_PEM_read_bio(name, type) \
384 DECLARE_PEM_read_fp(name, type)
386 #define DECLARE_PEM_rw(name, type) \
387 DECLARE_PEM_read(name, type) \
388 DECLARE_PEM_write(name, type)
390 #define DECLARE_PEM_rw_const(name, type) \
391 DECLARE_PEM_read(name, type) \
392 DECLARE_PEM_write_const(name, type)
394 #define DECLARE_PEM_rw_cb(name, type) \
395 DECLARE_PEM_read(name, type) \
396 DECLARE_PEM_write_cb(name, type)
400 typedef int pem_password_cb(
char *buf,
int size,
int rwflag,
void *userdata);
403 typedef int pem_password_cb(
char *buf,
int size,
int rwflag);
407 int PEM_do_header (
EVP_CIPHER_INFO *cipher,
unsigned char *data,
long *len,
408 pem_password_cb *callback,
void *u);
410 #ifndef OPENSSL_NO_BIO
411 int PEM_read_bio(
BIO *bp,
char **name,
char **header,
412 unsigned char **data,
long *len);
413 int PEM_write_bio(
BIO *bp,
const char *name,
char *hdr,
unsigned char *data,
415 int PEM_bytes_read_bio(
unsigned char **pdata,
long *plen,
char **pnm,
const char *name,
BIO *bp,
416 pem_password_cb *cb,
void *u);
417 void * PEM_ASN1_read_bio(d2i_of_void *d2i,
const char *name,
BIO *bp,
418 void **x, pem_password_cb *cb,
void *u);
419 int PEM_ASN1_write_bio(i2d_of_void *i2d,
const char *name,
BIO *bp,
void *x,
420 const EVP_CIPHER *enc,
unsigned char *kstr,
int klen,
421 pem_password_cb *cb,
void *u);
423 STACK_OF(
X509_INFO) * PEM_X509_INFO_read_bio(
BIO *bp, STACK_OF(
X509_INFO) *sk, pem_password_cb *cb,
void *u);
425 unsigned char *kstr,
int klen, pem_password_cb *cd,
void *u);
428 int PEM_read(FILE *fp,
char **name,
char **header,
429 unsigned char **data,
long *len);
430 int PEM_write(FILE *fp,
char *name,
char *hdr,
unsigned char *data,
long len);
431 void * PEM_ASN1_read(d2i_of_void *d2i,
const char *name, FILE *fp,
void **x,
432 pem_password_cb *cb,
void *u);
433 int PEM_ASN1_write(i2d_of_void *i2d,
const char *name,FILE *fp,
434 void *x,
const EVP_CIPHER *enc,
unsigned char *kstr,
435 int klen,pem_password_cb *callback,
void *u);
437 pem_password_cb *cb,
void *u);
440 EVP_MD *md_type,
unsigned char **ek,
int *ekl,
441 unsigned char *iv,
EVP_PKEY **pubk,
int npubk);
443 unsigned char *in,
int inl);
445 unsigned char *out,
int *outl,
EVP_PKEY *priv);
448 void PEM_SignUpdate(
EVP_MD_CTX *ctx,
unsigned char *d,
unsigned int cnt);
449 int PEM_SignFinal(
EVP_MD_CTX *ctx,
unsigned char *sigret,
450 unsigned int *siglen,
EVP_PKEY *pkey);
452 int PEM_def_callback(
char *buf,
int num,
int w,
void *key);
453 void PEM_proc_type(
char *buf,
int type);
454 void PEM_dek_info(
char *buf,
const char *type,
int len,
char *str);
457 #include <openssl/symhacks.h>
461 DECLARE_PEM_rw(X509_AUX,
X509)
466 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
470 DECLARE_PEM_rw(
PKCS7, PKCS7)
478 #ifndef OPENSSL_NO_RSA
480 DECLARE_PEM_rw_cb(RSAPrivateKey,
RSA)
482 DECLARE_PEM_rw_const(RSAPublicKey,
RSA)
483 DECLARE_PEM_rw(RSA_PUBKEY,
RSA)
487 #ifndef OPENSSL_NO_DSA
489 DECLARE_PEM_rw_cb(DSAPrivateKey,
DSA)
491 DECLARE_PEM_rw(DSA_PUBKEY,
DSA)
493 DECLARE_PEM_rw_const(DSAparams,
DSA)
497 #ifndef OPENSSL_NO_EC
498 DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP)
499 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
500 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
503 #ifndef OPENSSL_NO_DH
505 DECLARE_PEM_rw_const(DHparams,
DH)
509 DECLARE_PEM_rw_cb(PrivateKey,
EVP_PKEY)
513 int PEM_write_bio_PKCS8PrivateKey_nid(
BIO *bp,
EVP_PKEY *x,
int nid,
514 char *kstr,
int klen,
515 pem_password_cb *cb,
void *u);
517 char *,
int, pem_password_cb *,
void *);
518 int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
519 char *kstr,
int klen,
520 pem_password_cb *cb,
void *u);
521 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x,
int nid,
522 char *kstr,
int klen,
523 pem_password_cb *cb,
void *u);
524 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
void *u);
526 int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
527 char *kstr,
int klen,
528 pem_password_cb *cb,
void *u);
529 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x,
int nid,
530 char *kstr,
int klen,
531 pem_password_cb *cb,
void *u);
532 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x,
int nid,
533 char *kstr,
int klen,
534 pem_password_cb *cb,
void *u);
536 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
void *u);
538 int PEM_write_PKCS8PrivateKey(FILE *fp,EVP_PKEY *x,const EVP_CIPHER *enc,
539 char *kstr,
int klen, pem_password_cb *cd,
void *u);
541 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
542 int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x);
545 EVP_PKEY *b2i_PrivateKey(const
unsigned char **in,
long length);
546 EVP_PKEY *b2i_PublicKey(const
unsigned char **in,
long length);
547 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
548 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
549 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk);
550 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk);
551 #ifndef OPENSSL_NO_RC4
552 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb,
void *u);
553 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk,
int enclevel,
554 pem_password_cb *cb,
void *u);
562 void ERR_load_PEM_strings(
void);
567 #define PEM_F_B2I_DSS 127
568 #define PEM_F_B2I_PVK_BIO 128
569 #define PEM_F_B2I_RSA 129
570 #define PEM_F_CHECK_BITLEN_DSA 130
571 #define PEM_F_CHECK_BITLEN_RSA 131
572 #define PEM_F_D2I_PKCS8PRIVATEKEY_BIO 120
573 #define PEM_F_D2I_PKCS8PRIVATEKEY_FP 121
574 #define PEM_F_DO_B2I 132
575 #define PEM_F_DO_B2I_BIO 133
576 #define PEM_F_DO_BLOB_HEADER 134
577 #define PEM_F_DO_PK8PKEY 126
578 #define PEM_F_DO_PK8PKEY_FP 125
579 #define PEM_F_DO_PVK_BODY 135
580 #define PEM_F_DO_PVK_HEADER 136
581 #define PEM_F_I2B_PVK 137
582 #define PEM_F_I2B_PVK_BIO 138
583 #define PEM_F_LOAD_IV 101
584 #define PEM_F_PEM_ASN1_READ 102
585 #define PEM_F_PEM_ASN1_READ_BIO 103
586 #define PEM_F_PEM_ASN1_WRITE 104
587 #define PEM_F_PEM_ASN1_WRITE_BIO 105
588 #define PEM_F_PEM_DEF_CALLBACK 100
589 #define PEM_F_PEM_DO_HEADER 106
590 #define PEM_F_PEM_F_PEM_WRITE_PKCS8PRIVATEKEY 118
591 #define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
592 #define PEM_F_PEM_PK8PKEY 119
593 #define PEM_F_PEM_READ 108
594 #define PEM_F_PEM_READ_BIO 109
595 #define PEM_F_PEM_READ_BIO_PARAMETERS 140
596 #define PEM_F_PEM_READ_BIO_PRIVATEKEY 123
597 #define PEM_F_PEM_READ_PRIVATEKEY 124
598 #define PEM_F_PEM_SEALFINAL 110
599 #define PEM_F_PEM_SEALINIT 111
600 #define PEM_F_PEM_SIGNFINAL 112
601 #define PEM_F_PEM_WRITE 113
602 #define PEM_F_PEM_WRITE_BIO 114
603 #define PEM_F_PEM_WRITE_PRIVATEKEY 139
604 #define PEM_F_PEM_X509_INFO_READ 115
605 #define PEM_F_PEM_X509_INFO_READ_BIO 116
606 #define PEM_F_PEM_X509_INFO_WRITE_BIO 117
609 #define PEM_R_BAD_BASE64_DECODE 100
610 #define PEM_R_BAD_DECRYPT 101
611 #define PEM_R_BAD_END_LINE 102
612 #define PEM_R_BAD_IV_CHARS 103
613 #define PEM_R_BAD_MAGIC_NUMBER 116
614 #define PEM_R_BAD_PASSWORD_READ 104
615 #define PEM_R_BAD_VERSION_NUMBER 117
616 #define PEM_R_BIO_WRITE_FAILURE 118
617 #define PEM_R_CIPHER_IS_NULL 127
618 #define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 115
619 #define PEM_R_EXPECTING_PRIVATE_KEY_BLOB 119
620 #define PEM_R_EXPECTING_PUBLIC_KEY_BLOB 120
621 #define PEM_R_INCONSISTENT_HEADER 121
622 #define PEM_R_KEYBLOB_HEADER_PARSE_ERROR 122
623 #define PEM_R_KEYBLOB_TOO_SHORT 123
624 #define PEM_R_NOT_DEK_INFO 105
625 #define PEM_R_NOT_ENCRYPTED 106
626 #define PEM_R_NOT_PROC_TYPE 107
627 #define PEM_R_NO_START_LINE 108
628 #define PEM_R_PROBLEMS_GETTING_PASSWORD 109
629 #define PEM_R_PUBLIC_KEY_NO_RSA 110
630 #define PEM_R_PVK_DATA_TOO_SHORT 124
631 #define PEM_R_PVK_TOO_SHORT 125
632 #define PEM_R_READ_KEY 111
633 #define PEM_R_SHORT_HEADER 112
634 #define PEM_R_UNSUPPORTED_CIPHER 113
635 #define PEM_R_UNSUPPORTED_ENCRYPTION 114
636 #define PEM_R_UNSUPPORTED_KEY_COMPONENTS 126