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