1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948PEM - PEM routines 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 #include <openssl/pem.h> 10*2175Sjp161948 11*2175Sjp161948 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, 12*2175Sjp161948 pem_password_cb *cb, void *u); 13*2175Sjp161948 14*2175Sjp161948 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, 15*2175Sjp161948 pem_password_cb *cb, void *u); 16*2175Sjp161948 17*2175Sjp161948 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, 18*2175Sjp161948 unsigned char *kstr, int klen, 19*2175Sjp161948 pem_password_cb *cb, void *u); 20*2175Sjp161948 21*2175Sjp161948 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, 22*2175Sjp161948 unsigned char *kstr, int klen, 23*2175Sjp161948 pem_password_cb *cb, void *u); 24*2175Sjp161948 25*2175Sjp161948 int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, 26*2175Sjp161948 char *kstr, int klen, 27*2175Sjp161948 pem_password_cb *cb, void *u); 28*2175Sjp161948 29*2175Sjp161948 int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, 30*2175Sjp161948 char *kstr, int klen, 31*2175Sjp161948 pem_password_cb *cb, void *u); 32*2175Sjp161948 33*2175Sjp161948 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid, 34*2175Sjp161948 char *kstr, int klen, 35*2175Sjp161948 pem_password_cb *cb, void *u); 36*2175Sjp161948 37*2175Sjp161948 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, 38*2175Sjp161948 char *kstr, int klen, 39*2175Sjp161948 pem_password_cb *cb, void *u); 40*2175Sjp161948 41*2175Sjp161948 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, 42*2175Sjp161948 pem_password_cb *cb, void *u); 43*2175Sjp161948 44*2175Sjp161948 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, 45*2175Sjp161948 pem_password_cb *cb, void *u); 46*2175Sjp161948 47*2175Sjp161948 int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x); 48*2175Sjp161948 int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x); 49*2175Sjp161948 50*2175Sjp161948 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x, 51*2175Sjp161948 pem_password_cb *cb, void *u); 52*2175Sjp161948 53*2175Sjp161948 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x, 54*2175Sjp161948 pem_password_cb *cb, void *u); 55*2175Sjp161948 56*2175Sjp161948 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc, 57*2175Sjp161948 unsigned char *kstr, int klen, 58*2175Sjp161948 pem_password_cb *cb, void *u); 59*2175Sjp161948 60*2175Sjp161948 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc, 61*2175Sjp161948 unsigned char *kstr, int klen, 62*2175Sjp161948 pem_password_cb *cb, void *u); 63*2175Sjp161948 64*2175Sjp161948 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, 65*2175Sjp161948 pem_password_cb *cb, void *u); 66*2175Sjp161948 67*2175Sjp161948 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x, 68*2175Sjp161948 pem_password_cb *cb, void *u); 69*2175Sjp161948 70*2175Sjp161948 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x); 71*2175Sjp161948 72*2175Sjp161948 int PEM_write_RSAPublicKey(FILE *fp, RSA *x); 73*2175Sjp161948 74*2175Sjp161948 RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x, 75*2175Sjp161948 pem_password_cb *cb, void *u); 76*2175Sjp161948 77*2175Sjp161948 RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x, 78*2175Sjp161948 pem_password_cb *cb, void *u); 79*2175Sjp161948 80*2175Sjp161948 int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x); 81*2175Sjp161948 82*2175Sjp161948 int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x); 83*2175Sjp161948 84*2175Sjp161948 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x, 85*2175Sjp161948 pem_password_cb *cb, void *u); 86*2175Sjp161948 87*2175Sjp161948 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x, 88*2175Sjp161948 pem_password_cb *cb, void *u); 89*2175Sjp161948 90*2175Sjp161948 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc, 91*2175Sjp161948 unsigned char *kstr, int klen, 92*2175Sjp161948 pem_password_cb *cb, void *u); 93*2175Sjp161948 94*2175Sjp161948 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc, 95*2175Sjp161948 unsigned char *kstr, int klen, 96*2175Sjp161948 pem_password_cb *cb, void *u); 97*2175Sjp161948 98*2175Sjp161948 DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x, 99*2175Sjp161948 pem_password_cb *cb, void *u); 100*2175Sjp161948 101*2175Sjp161948 DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x, 102*2175Sjp161948 pem_password_cb *cb, void *u); 103*2175Sjp161948 104*2175Sjp161948 int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x); 105*2175Sjp161948 106*2175Sjp161948 int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x); 107*2175Sjp161948 108*2175Sjp161948 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u); 109*2175Sjp161948 110*2175Sjp161948 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u); 111*2175Sjp161948 112*2175Sjp161948 int PEM_write_bio_DSAparams(BIO *bp, DSA *x); 113*2175Sjp161948 114*2175Sjp161948 int PEM_write_DSAparams(FILE *fp, DSA *x); 115*2175Sjp161948 116*2175Sjp161948 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u); 117*2175Sjp161948 118*2175Sjp161948 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u); 119*2175Sjp161948 120*2175Sjp161948 int PEM_write_bio_DHparams(BIO *bp, DH *x); 121*2175Sjp161948 122*2175Sjp161948 int PEM_write_DHparams(FILE *fp, DH *x); 123*2175Sjp161948 124*2175Sjp161948 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u); 125*2175Sjp161948 126*2175Sjp161948 X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u); 127*2175Sjp161948 128*2175Sjp161948 int PEM_write_bio_X509(BIO *bp, X509 *x); 129*2175Sjp161948 130*2175Sjp161948 int PEM_write_X509(FILE *fp, X509 *x); 131*2175Sjp161948 132*2175Sjp161948 X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u); 133*2175Sjp161948 134*2175Sjp161948 X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u); 135*2175Sjp161948 136*2175Sjp161948 int PEM_write_bio_X509_AUX(BIO *bp, X509 *x); 137*2175Sjp161948 138*2175Sjp161948 int PEM_write_X509_AUX(FILE *fp, X509 *x); 139*2175Sjp161948 140*2175Sjp161948 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, 141*2175Sjp161948 pem_password_cb *cb, void *u); 142*2175Sjp161948 143*2175Sjp161948 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x, 144*2175Sjp161948 pem_password_cb *cb, void *u); 145*2175Sjp161948 146*2175Sjp161948 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x); 147*2175Sjp161948 148*2175Sjp161948 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x); 149*2175Sjp161948 150*2175Sjp161948 int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x); 151*2175Sjp161948 152*2175Sjp161948 int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x); 153*2175Sjp161948 154*2175Sjp161948 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, 155*2175Sjp161948 pem_password_cb *cb, void *u); 156*2175Sjp161948 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x, 157*2175Sjp161948 pem_password_cb *cb, void *u); 158*2175Sjp161948 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x); 159*2175Sjp161948 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x); 160*2175Sjp161948 161*2175Sjp161948 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u); 162*2175Sjp161948 163*2175Sjp161948 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u); 164*2175Sjp161948 165*2175Sjp161948 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x); 166*2175Sjp161948 167*2175Sjp161948 int PEM_write_PKCS7(FILE *fp, PKCS7 *x); 168*2175Sjp161948 169*2175Sjp161948 NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, 170*2175Sjp161948 NETSCAPE_CERT_SEQUENCE **x, 171*2175Sjp161948 pem_password_cb *cb, void *u); 172*2175Sjp161948 173*2175Sjp161948 NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp, 174*2175Sjp161948 NETSCAPE_CERT_SEQUENCE **x, 175*2175Sjp161948 pem_password_cb *cb, void *u); 176*2175Sjp161948 177*2175Sjp161948 int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x); 178*2175Sjp161948 179*2175Sjp161948 int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x); 180*2175Sjp161948 181*2175Sjp161948=head1 DESCRIPTION 182*2175Sjp161948 183*2175Sjp161948The PEM functions read or write structures in PEM format. In 184*2175Sjp161948this sense PEM format is simply base64 encoded data surrounded 185*2175Sjp161948by header lines. 186*2175Sjp161948 187*2175Sjp161948For more details about the meaning of arguments see the 188*2175Sjp161948B<PEM FUNCTION ARGUMENTS> section. 189*2175Sjp161948 190*2175Sjp161948Each operation has four functions associated with it. For 191*2175Sjp161948clarity the term "B<foobar> functions" will be used to collectively 192*2175Sjp161948refer to the PEM_read_bio_foobar(), PEM_read_foobar(), 193*2175Sjp161948PEM_write_bio_foobar() and PEM_write_foobar() functions. 194*2175Sjp161948 195*2175Sjp161948The B<PrivateKey> functions read or write a private key in 196*2175Sjp161948PEM format using an EVP_PKEY structure. The write routines use 197*2175Sjp161948"traditional" private key format and can handle both RSA and DSA 198*2175Sjp161948private keys. The read functions can additionally transparently 199*2175Sjp161948handle PKCS#8 format encrypted and unencrypted keys too. 200*2175Sjp161948 201*2175Sjp161948PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() 202*2175Sjp161948write a private key in an EVP_PKEY structure in PKCS#8 203*2175Sjp161948EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption 204*2175Sjp161948algorithms. The B<cipher> argument specifies the encryption algoritm to 205*2175Sjp161948use: unlike all other PEM routines the encryption is applied at the 206*2175Sjp161948PKCS#8 level and not in the PEM headers. If B<cipher> is NULL then no 207*2175Sjp161948encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead. 208*2175Sjp161948 209*2175Sjp161948PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid() 210*2175Sjp161948also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however 211*2175Sjp161948it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm 212*2175Sjp161948to use is specified in the B<nid> parameter and should be the NID of the 213*2175Sjp161948corresponding OBJECT IDENTIFIER (see NOTES section). 214*2175Sjp161948 215*2175Sjp161948The B<PUBKEY> functions process a public key using an EVP_PKEY 216*2175Sjp161948structure. The public key is encoded as a SubjectPublicKeyInfo 217*2175Sjp161948structure. 218*2175Sjp161948 219*2175Sjp161948The B<RSAPrivateKey> functions process an RSA private key using an 220*2175Sjp161948RSA structure. It handles the same formats as the B<PrivateKey> 221*2175Sjp161948functions but an error occurs if the private key is not RSA. 222*2175Sjp161948 223*2175Sjp161948The B<RSAPublicKey> functions process an RSA public key using an 224*2175Sjp161948RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey 225*2175Sjp161948structure. 226*2175Sjp161948 227*2175Sjp161948The B<RSA_PUBKEY> functions also process an RSA public key using 228*2175Sjp161948an RSA structure. However the public key is encoded using a 229*2175Sjp161948SubjectPublicKeyInfo structure and an error occurs if the public 230*2175Sjp161948key is not RSA. 231*2175Sjp161948 232*2175Sjp161948The B<DSAPrivateKey> functions process a DSA private key using a 233*2175Sjp161948DSA structure. It handles the same formats as the B<PrivateKey> 234*2175Sjp161948functions but an error occurs if the private key is not DSA. 235*2175Sjp161948 236*2175Sjp161948The B<DSA_PUBKEY> functions process a DSA public key using 237*2175Sjp161948a DSA structure. The public key is encoded using a 238*2175Sjp161948SubjectPublicKeyInfo structure and an error occurs if the public 239*2175Sjp161948key is not DSA. 240*2175Sjp161948 241*2175Sjp161948The B<DSAparams> functions process DSA parameters using a DSA 242*2175Sjp161948structure. The parameters are encoded using a foobar structure. 243*2175Sjp161948 244*2175Sjp161948The B<DHparams> functions process DH parameters using a DH 245*2175Sjp161948structure. The parameters are encoded using a PKCS#3 DHparameter 246*2175Sjp161948structure. 247*2175Sjp161948 248*2175Sjp161948The B<X509> functions process an X509 certificate using an X509 249*2175Sjp161948structure. They will also process a trusted X509 certificate but 250*2175Sjp161948any trust settings are discarded. 251*2175Sjp161948 252*2175Sjp161948The B<X509_AUX> functions process a trusted X509 certificate using 253*2175Sjp161948an X509 structure. 254*2175Sjp161948 255*2175Sjp161948The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10 256*2175Sjp161948certificate request using an X509_REQ structure. The B<X509_REQ> 257*2175Sjp161948write functions use B<CERTIFICATE REQUEST> in the header whereas 258*2175Sjp161948the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST> 259*2175Sjp161948(as required by some CAs). The B<X509_REQ> read functions will 260*2175Sjp161948handle either form so there are no B<X509_REQ_NEW> read functions. 261*2175Sjp161948 262*2175Sjp161948The B<X509_CRL> functions process an X509 CRL using an X509_CRL 263*2175Sjp161948structure. 264*2175Sjp161948 265*2175Sjp161948The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7 266*2175Sjp161948structure. 267*2175Sjp161948 268*2175Sjp161948The B<NETSCAPE_CERT_SEQUENCE> functions process a Netscape Certificate 269*2175Sjp161948Sequence using a NETSCAPE_CERT_SEQUENCE structure. 270*2175Sjp161948 271*2175Sjp161948=head1 PEM FUNCTION ARGUMENTS 272*2175Sjp161948 273*2175Sjp161948The PEM functions have many common arguments. 274*2175Sjp161948 275*2175Sjp161948The B<bp> BIO parameter (if present) specifies the BIO to read from 276*2175Sjp161948or write to. 277*2175Sjp161948 278*2175Sjp161948The B<fp> FILE parameter (if present) specifies the FILE pointer to 279*2175Sjp161948read from or write to. 280*2175Sjp161948 281*2175Sjp161948The PEM read functions all take an argument B<TYPE **x> and return 282*2175Sjp161948a B<TYPE *> pointer. Where B<TYPE> is whatever structure the function 283*2175Sjp161948uses. If B<x> is NULL then the parameter is ignored. If B<x> is not 284*2175Sjp161948NULL but B<*x> is NULL then the structure returned will be written 285*2175Sjp161948to B<*x>. If neither B<x> nor B<*x> is NULL then an attempt is made 286*2175Sjp161948to reuse the structure at B<*x> (but see BUGS and EXAMPLES sections). 287*2175Sjp161948Irrespective of the value of B<x> a pointer to the structure is always 288*2175Sjp161948returned (or NULL if an error occurred). 289*2175Sjp161948 290*2175Sjp161948The PEM functions which write private keys take an B<enc> parameter 291*2175Sjp161948which specifies the encryption algorithm to use, encryption is done 292*2175Sjp161948at the PEM level. If this parameter is set to NULL then the private 293*2175Sjp161948key is written in unencrypted form. 294*2175Sjp161948 295*2175Sjp161948The B<cb> argument is the callback to use when querying for the pass 296*2175Sjp161948phrase used for encrypted PEM structures (normally only private keys). 297*2175Sjp161948 298*2175Sjp161948For the PEM write routines if the B<kstr> parameter is not NULL then 299*2175Sjp161948B<klen> bytes at B<kstr> are used as the passphrase and B<cb> is 300*2175Sjp161948ignored. 301*2175Sjp161948 302*2175Sjp161948If the B<cb> parameters is set to NULL and the B<u> parameter is not 303*2175Sjp161948NULL then the B<u> parameter is interpreted as a null terminated string 304*2175Sjp161948to use as the passphrase. If both B<cb> and B<u> are NULL then the 305*2175Sjp161948default callback routine is used which will typically prompt for the 306*2175Sjp161948passphrase on the current terminal with echoing turned off. 307*2175Sjp161948 308*2175Sjp161948The default passphrase callback is sometimes inappropriate (for example 309*2175Sjp161948in a GUI application) so an alternative can be supplied. The callback 310*2175Sjp161948routine has the following form: 311*2175Sjp161948 312*2175Sjp161948 int cb(char *buf, int size, int rwflag, void *u); 313*2175Sjp161948 314*2175Sjp161948B<buf> is the buffer to write the passphrase to. B<size> is the maximum 315*2175Sjp161948length of the passphrase (i.e. the size of buf). B<rwflag> is a flag 316*2175Sjp161948which is set to 0 when reading and 1 when writing. A typical routine 317*2175Sjp161948will ask the user to verify the passphrase (for example by prompting 318*2175Sjp161948for it twice) if B<rwflag> is 1. The B<u> parameter has the same 319*2175Sjp161948value as the B<u> parameter passed to the PEM routine. It allows 320*2175Sjp161948arbitrary data to be passed to the callback by the application 321*2175Sjp161948(for example a window handle in a GUI application). The callback 322*2175Sjp161948B<must> return the number of characters in the passphrase or 0 if 323*2175Sjp161948an error occurred. 324*2175Sjp161948 325*2175Sjp161948=head1 EXAMPLES 326*2175Sjp161948 327*2175Sjp161948Although the PEM routines take several arguments in almost all applications 328*2175Sjp161948most of them are set to 0 or NULL. 329*2175Sjp161948 330*2175Sjp161948Read a certificate in PEM format from a BIO: 331*2175Sjp161948 332*2175Sjp161948 X509 *x; 333*2175Sjp161948 x = PEM_read_bio_X509(bp, NULL, 0, NULL); 334*2175Sjp161948 if (x == NULL) 335*2175Sjp161948 { 336*2175Sjp161948 /* Error */ 337*2175Sjp161948 } 338*2175Sjp161948 339*2175Sjp161948Alternative method: 340*2175Sjp161948 341*2175Sjp161948 X509 *x = NULL; 342*2175Sjp161948 if (!PEM_read_bio_X509(bp, &x, 0, NULL)) 343*2175Sjp161948 { 344*2175Sjp161948 /* Error */ 345*2175Sjp161948 } 346*2175Sjp161948 347*2175Sjp161948Write a certificate to a BIO: 348*2175Sjp161948 349*2175Sjp161948 if (!PEM_write_bio_X509(bp, x)) 350*2175Sjp161948 { 351*2175Sjp161948 /* Error */ 352*2175Sjp161948 } 353*2175Sjp161948 354*2175Sjp161948Write an unencrypted private key to a FILE pointer: 355*2175Sjp161948 356*2175Sjp161948 if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL)) 357*2175Sjp161948 { 358*2175Sjp161948 /* Error */ 359*2175Sjp161948 } 360*2175Sjp161948 361*2175Sjp161948Write a private key (using traditional format) to a BIO using 362*2175Sjp161948triple DES encryption, the pass phrase is prompted for: 363*2175Sjp161948 364*2175Sjp161948 if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL)) 365*2175Sjp161948 { 366*2175Sjp161948 /* Error */ 367*2175Sjp161948 } 368*2175Sjp161948 369*2175Sjp161948Write a private key (using PKCS#8 format) to a BIO using triple 370*2175Sjp161948DES encryption, using the pass phrase "hello": 371*2175Sjp161948 372*2175Sjp161948 if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello")) 373*2175Sjp161948 { 374*2175Sjp161948 /* Error */ 375*2175Sjp161948 } 376*2175Sjp161948 377*2175Sjp161948Read a private key from a BIO using the pass phrase "hello": 378*2175Sjp161948 379*2175Sjp161948 key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello"); 380*2175Sjp161948 if (key == NULL) 381*2175Sjp161948 { 382*2175Sjp161948 /* Error */ 383*2175Sjp161948 } 384*2175Sjp161948 385*2175Sjp161948Read a private key from a BIO using a pass phrase callback: 386*2175Sjp161948 387*2175Sjp161948 key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key"); 388*2175Sjp161948 if (key == NULL) 389*2175Sjp161948 { 390*2175Sjp161948 /* Error */ 391*2175Sjp161948 } 392*2175Sjp161948 393*2175Sjp161948Skeleton pass phrase callback: 394*2175Sjp161948 395*2175Sjp161948 int pass_cb(char *buf, int size, int rwflag, void *u); 396*2175Sjp161948 { 397*2175Sjp161948 int len; 398*2175Sjp161948 char *tmp; 399*2175Sjp161948 /* We'd probably do something else if 'rwflag' is 1 */ 400*2175Sjp161948 printf("Enter pass phrase for \"%s\"\n", u); 401*2175Sjp161948 402*2175Sjp161948 /* get pass phrase, length 'len' into 'tmp' */ 403*2175Sjp161948 tmp = "hello"; 404*2175Sjp161948 len = strlen(tmp); 405*2175Sjp161948 406*2175Sjp161948 if (len <= 0) return 0; 407*2175Sjp161948 /* if too long, truncate */ 408*2175Sjp161948 if (len > size) len = size; 409*2175Sjp161948 memcpy(buf, tmp, len); 410*2175Sjp161948 return len; 411*2175Sjp161948 } 412*2175Sjp161948 413*2175Sjp161948=head1 NOTES 414*2175Sjp161948 415*2175Sjp161948The old B<PrivateKey> write routines are retained for compatibility. 416*2175Sjp161948New applications should write private keys using the 417*2175Sjp161948PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines 418*2175Sjp161948because they are more secure (they use an iteration count of 2048 whereas 419*2175Sjp161948the traditional routines use a count of 1) unless compatibility with older 420*2175Sjp161948versions of OpenSSL is important. 421*2175Sjp161948 422*2175Sjp161948The B<PrivateKey> read routines can be used in all applications because 423*2175Sjp161948they handle all formats transparently. 424*2175Sjp161948 425*2175Sjp161948A frequent cause of problems is attempting to use the PEM routines like 426*2175Sjp161948this: 427*2175Sjp161948 428*2175Sjp161948 X509 *x; 429*2175Sjp161948 PEM_read_bio_X509(bp, &x, 0, NULL); 430*2175Sjp161948 431*2175Sjp161948this is a bug because an attempt will be made to reuse the data at B<x> 432*2175Sjp161948which is an uninitialised pointer. 433*2175Sjp161948 434*2175Sjp161948=head1 PEM ENCRYPTION FORMAT 435*2175Sjp161948 436*2175Sjp161948This old B<PrivateKey> routines use a non standard technique for encryption. 437*2175Sjp161948 438*2175Sjp161948The private key (or other data) takes the following form: 439*2175Sjp161948 440*2175Sjp161948 -----BEGIN RSA PRIVATE KEY----- 441*2175Sjp161948 Proc-Type: 4,ENCRYPTED 442*2175Sjp161948 DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89 443*2175Sjp161948 444*2175Sjp161948 ...base64 encoded data... 445*2175Sjp161948 -----END RSA PRIVATE KEY----- 446*2175Sjp161948 447*2175Sjp161948The line beginning DEK-Info contains two comma separated pieces of information: 448*2175Sjp161948the encryption algorithm name as used by EVP_get_cipherbyname() and an 8 449*2175Sjp161948byte B<salt> encoded as a set of hexadecimal digits. 450*2175Sjp161948 451*2175Sjp161948After this is the base64 encoded encrypted data. 452*2175Sjp161948 453*2175Sjp161948The encryption key is determined using EVP_bytestokey(), using B<salt> and an 454*2175Sjp161948iteration count of 1. The IV used is the value of B<salt> and *not* the IV 455*2175Sjp161948returned by EVP_bytestokey(). 456*2175Sjp161948 457*2175Sjp161948=head1 BUGS 458*2175Sjp161948 459*2175Sjp161948The PEM read routines in some versions of OpenSSL will not correctly reuse 460*2175Sjp161948an existing structure. Therefore the following: 461*2175Sjp161948 462*2175Sjp161948 PEM_read_bio_X509(bp, &x, 0, NULL); 463*2175Sjp161948 464*2175Sjp161948where B<x> already contains a valid certificate, may not work, whereas: 465*2175Sjp161948 466*2175Sjp161948 X509_free(x); 467*2175Sjp161948 x = PEM_read_bio_X509(bp, NULL, 0, NULL); 468*2175Sjp161948 469*2175Sjp161948is guaranteed to work. 470*2175Sjp161948 471*2175Sjp161948=head1 RETURN CODES 472*2175Sjp161948 473*2175Sjp161948The read routines return either a pointer to the structure read or NULL 474*2175Sjp161948if an error occurred. 475*2175Sjp161948 476*2175Sjp161948The write routines return 1 for success or 0 for failure. 477