PEM_read.pod 5.2 KB

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