1*e71b7053SJung-uk Kim=pod 2*e71b7053SJung-uk Kim 3*e71b7053SJung-uk Kim=head1 NAME 4*e71b7053SJung-uk Kim 5*e71b7053SJung-uk KimPEM_write, PEM_write_bio, 6*e71b7053SJung-uk KimPEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO 7*e71b7053SJung-uk Kim- PEM encoding routines 8*e71b7053SJung-uk Kim 9*e71b7053SJung-uk Kim=head1 SYNOPSIS 10*e71b7053SJung-uk Kim 11*e71b7053SJung-uk Kim #include <openssl/pem.h> 12*e71b7053SJung-uk Kim 13*e71b7053SJung-uk Kim int PEM_write(FILE *fp, const char *name, const char *header, 14*e71b7053SJung-uk Kim const unsigned char *data, long len) 15*e71b7053SJung-uk Kim int PEM_write_bio(BIO *bp, const char *name, const char *header, 16*e71b7053SJung-uk Kim const unsigned char *data, long len) 17*e71b7053SJung-uk Kim 18*e71b7053SJung-uk Kim int PEM_read(FILE *fp, char **name, char **header, 19*e71b7053SJung-uk Kim unsigned char **data, long *len); 20*e71b7053SJung-uk Kim int PEM_read_bio(BIO *bp, char **name, char **header, 21*e71b7053SJung-uk Kim unsigned char **data, long *len); 22*e71b7053SJung-uk Kim 23*e71b7053SJung-uk Kim int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo); 24*e71b7053SJung-uk Kim int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len, 25*e71b7053SJung-uk Kim pem_password_cb *cb, void *u); 26*e71b7053SJung-uk Kim 27*e71b7053SJung-uk Kim=head1 DESCRIPTION 28*e71b7053SJung-uk Kim 29*e71b7053SJung-uk KimThese functions read and write PEM-encoded objects, using the PEM 30*e71b7053SJung-uk Kimtype B<name>, any additional B<header> information, and the raw 31*e71b7053SJung-uk KimB<data> of length B<len>. 32*e71b7053SJung-uk Kim 33*e71b7053SJung-uk KimPEM is the term used for binary content encoding first defined in IETF 34*e71b7053SJung-uk KimRFC 1421. The content is a series of base64-encoded lines, surrounded 35*e71b7053SJung-uk Kimby begin/end markers each on their own line. For example: 36*e71b7053SJung-uk Kim 37*e71b7053SJung-uk Kim -----BEGIN PRIVATE KEY----- 38*e71b7053SJung-uk Kim MIICdg.... 39*e71b7053SJung-uk Kim ... bhTQ== 40*e71b7053SJung-uk Kim -----END PRIVATE KEY----- 41*e71b7053SJung-uk Kim 42*e71b7053SJung-uk KimOptional header line(s) may appear after the begin line, and their 43*e71b7053SJung-uk Kimexistence depends on the type of object being written or read. 44*e71b7053SJung-uk Kim 45*e71b7053SJung-uk KimPEM_write() writes to the file B<fp>, while PEM_write_bio() writes to 46*e71b7053SJung-uk Kimthe BIO B<bp>. The B<name> is the name to use in the marker, the 47*e71b7053SJung-uk KimB<header> is the header value or NULL, and B<data> and B<len> specify 48*e71b7053SJung-uk Kimthe data and its length. 49*e71b7053SJung-uk Kim 50*e71b7053SJung-uk KimThe final B<data> buffer is typically an ASN.1 object which can be decoded with 51*e71b7053SJung-uk Kimthe B<d2i> function appropriate to the type B<name>; see L<d2i_X509(3)> 52*e71b7053SJung-uk Kimfor examples. 53*e71b7053SJung-uk Kim 54*e71b7053SJung-uk KimPEM_read() reads from the file B<fp>, while PEM_read_bio() reads 55*e71b7053SJung-uk Kimfrom the BIO B<bp>. 56*e71b7053SJung-uk KimBoth skip any non-PEM data that precedes the start of the next PEM object. 57*e71b7053SJung-uk KimWhen an object is successfully retrieved, the type name from the "----BEGIN 58*e71b7053SJung-uk Kim<type>-----" is returned via the B<name> argument, any encapsulation headers 59*e71b7053SJung-uk Kimare returned in B<header> and the base64-decoded content and its length are 60*e71b7053SJung-uk Kimreturned via B<data> and B<len> respectively. 61*e71b7053SJung-uk KimThe B<name>, B<header> and B<data> pointers are allocated via OPENSSL_malloc() 62*e71b7053SJung-uk Kimand should be freed by the caller via OPENSSL_free() when no longer needed. 63*e71b7053SJung-uk Kim 64*e71b7053SJung-uk KimPEM_get_EVP_CIPHER_INFO() can be used to determine the B<data> returned by 65*e71b7053SJung-uk KimPEM_read() or PEM_read_bio() is encrypted and to retrieve the associated cipher 66*e71b7053SJung-uk Kimand IV. 67*e71b7053SJung-uk KimThe caller passes a pointer to structure of type B<EVP_CIPHER_INFO> via the 68*e71b7053SJung-uk KimB<cinfo> argument and the B<header> returned via PEM_read() or PEM_read_bio(). 69*e71b7053SJung-uk KimIf the call is successful 1 is returned and the cipher and IV are stored at the 70*e71b7053SJung-uk Kimaddress pointed to by B<cinfo>. 71*e71b7053SJung-uk KimWhen the header is malformed, or not supported or when the cipher is unknown 72*e71b7053SJung-uk Kimor some internal error happens 0 is returned. 73*e71b7053SJung-uk KimThis function is deprecated, see B<NOTES> below. 74*e71b7053SJung-uk Kim 75*e71b7053SJung-uk KimPEM_do_header() can then be used to decrypt the data if the header 76*e71b7053SJung-uk Kimindicates encryption. 77*e71b7053SJung-uk KimThe B<cinfo> argument is a pointer to the structure initialized by the previous 78*e71b7053SJung-uk Kimcall to PEM_get_EVP_CIPHER_INFO(). 79*e71b7053SJung-uk KimThe B<data> and B<len> arguments are those returned by the previous call to 80*e71b7053SJung-uk KimPEM_read() or PEM_read_bio(). 81*e71b7053SJung-uk KimThe B<cb> and B<u> arguments make it possible to override the default password 82*e71b7053SJung-uk Kimprompt function as described in L<PEM_read_PrivateKey(3)>. 83*e71b7053SJung-uk KimOn successful completion the B<data> is decrypted in place, and B<len> is 84*e71b7053SJung-uk Kimupdated to indicate the plaintext length. 85*e71b7053SJung-uk KimThis function is deprecated, see B<NOTES> below. 86*e71b7053SJung-uk Kim 87*e71b7053SJung-uk KimIf the data is a priori known to not be encrypted, then neither PEM_do_header() 88*e71b7053SJung-uk Kimnor PEM_get_EVP_CIPHER_INFO() need be called. 89*e71b7053SJung-uk Kim 90*e71b7053SJung-uk Kim=head1 RETURN VALUES 91*e71b7053SJung-uk Kim 92*e71b7053SJung-uk KimPEM_read() and PEM_read_bio() return 1 on success and 0 on failure, the latter 93*e71b7053SJung-uk Kimincludes the case when no more PEM objects remain in the input file. 94*e71b7053SJung-uk KimTo distinguish end of file from more serious errors the caller must peek at the 95*e71b7053SJung-uk Kimerror stack and check for B<PEM_R_NO_START_LINE>, which indicates that no more 96*e71b7053SJung-uk KimPEM objects were found. See L<ERR_peek_last_error(3)>, L<ERR_GET_REASON(3)>. 97*e71b7053SJung-uk Kim 98*e71b7053SJung-uk KimPEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on 99*e71b7053SJung-uk Kimfailure. 100*e71b7053SJung-uk KimThe B<data> is likely meaningless if these functions fail. 101*e71b7053SJung-uk Kim 102*e71b7053SJung-uk Kim=head1 NOTES 103*e71b7053SJung-uk Kim 104*e71b7053SJung-uk KimThe PEM_get_EVP_CIPHER_INFO() and PEM_do_header() functions are deprecated. 105*e71b7053SJung-uk KimThis is because the underlying PEM encryption format is obsolete, and should 106*e71b7053SJung-uk Kimbe avoided. 107*e71b7053SJung-uk KimIt uses an encryption format with an OpenSSL-specific key-derivation function, 108*e71b7053SJung-uk Kimwhich employs MD5 with an iteration count of 1! 109*e71b7053SJung-uk KimInstead, private keys should be stored in PKCS#8 form, with a strong PKCS#5 110*e71b7053SJung-uk Kimv2.0 PBE. 111*e71b7053SJung-uk KimSee L<PEM_write_PrivateKey(3)> and L<d2i_PKCS8PrivateKey_bio(3)>. 112*e71b7053SJung-uk Kim 113*e71b7053SJung-uk KimPEM_do_header() makes no assumption regarding the pass phrase received from the 114*e71b7053SJung-uk Kimpassword callback. 115*e71b7053SJung-uk KimIt will simply be treated as a byte sequence. 116*e71b7053SJung-uk Kim 117*e71b7053SJung-uk Kim=head1 SEE ALSO 118*e71b7053SJung-uk Kim 119*e71b7053SJung-uk KimL<ERR_peek_last_error(3)>, L<ERR_GET_LIB(3)>, 120*e71b7053SJung-uk KimL<d2i_PKCS8PrivateKey_bio(3)>, 121*e71b7053SJung-uk KimL<passphrase-encoding(7)> 122*e71b7053SJung-uk Kim 123*e71b7053SJung-uk Kim=head1 COPYRIGHT 124*e71b7053SJung-uk Kim 125*e71b7053SJung-uk KimCopyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved. 126*e71b7053SJung-uk Kim 127*e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 128*e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 129*e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 130*e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 131*e71b7053SJung-uk Kim 132*e71b7053SJung-uk Kim=cut 133