PEM_read.3ossl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "PEM_READ 3ossl"
  58. .TH PEM_READ 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. PEM_write, PEM_write_bio,
  65. PEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO
  66. \&\- PEM encoding routines
  67. .SH SYNOPSIS
  68. .IX Header "SYNOPSIS"
  69. .Vb 1
  70. \& #include <openssl/pem.h>
  71. \&
  72. \& int PEM_write(FILE *fp, const char *name, const char *header,
  73. \& const unsigned char *data, long len);
  74. \& int PEM_write_bio(BIO *bp, const char *name, const char *header,
  75. \& const unsigned char *data, long len);
  76. \&
  77. \& int PEM_read(FILE *fp, char **name, char **header,
  78. \& unsigned char **data, long *len);
  79. \& int PEM_read_bio(BIO *bp, char **name, char **header,
  80. \& unsigned char **data, long *len);
  81. \&
  82. \& int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
  83. \& int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
  84. \& pem_password_cb *cb, void *u);
  85. .Ve
  86. .SH DESCRIPTION
  87. .IX Header "DESCRIPTION"
  88. These functions read and write PEM-encoded objects, using the PEM
  89. type \fBname\fR, any additional \fBheader\fR information, and the raw
  90. \&\fBdata\fR of length \fBlen\fR.
  91. .PP
  92. PEM is the term used for binary content encoding first defined in IETF
  93. RFC 1421. The content is a series of base64\-encoded lines, surrounded
  94. by begin/end markers each on their own line. For example:
  95. .PP
  96. .Vb 4
  97. \& \-\-\-\-\-BEGIN PRIVATE KEY\-\-\-\-\-
  98. \& MIICdg....
  99. \& ... bhTQ==
  100. \& \-\-\-\-\-END PRIVATE KEY\-\-\-\-\-
  101. .Ve
  102. .PP
  103. Optional header line(s) may appear after the begin line, and their
  104. existence depends on the type of object being written or read.
  105. .PP
  106. \&\fBPEM_write()\fR writes to the file \fBfp\fR, while \fBPEM_write_bio()\fR writes to
  107. the BIO \fBbp\fR. The \fBname\fR is the name to use in the marker, the
  108. \&\fBheader\fR is the header value or NULL, and \fBdata\fR and \fBlen\fR specify
  109. the data and its length.
  110. .PP
  111. The final \fBdata\fR buffer is typically an ASN.1 object which can be decoded with
  112. the \fBd2i\fR function appropriate to the type \fBname\fR; see \fBd2i_X509\fR\|(3)
  113. for examples.
  114. .PP
  115. \&\fBPEM_read()\fR reads from the file \fBfp\fR, while \fBPEM_read_bio()\fR reads
  116. from the BIO \fBbp\fR.
  117. Both skip any non-PEM data that precedes the start of the next PEM object.
  118. When an object is successfully retrieved, the type name from the "\-\-\-\-BEGIN
  119. <type>\-\-\-\-\-" is returned via the \fBname\fR argument, any encapsulation headers
  120. are returned in \fBheader\fR and the base64\-decoded content and its length are
  121. returned via \fBdata\fR and \fBlen\fR respectively.
  122. The \fBname\fR, \fBheader\fR and \fBdata\fR pointers are allocated via \fBOPENSSL_malloc()\fR
  123. and should be freed by the caller via \fBOPENSSL_free()\fR when no longer needed.
  124. .PP
  125. \&\fBPEM_get_EVP_CIPHER_INFO()\fR can be used to determine the \fBdata\fR returned by
  126. \&\fBPEM_read()\fR or \fBPEM_read_bio()\fR is encrypted and to retrieve the associated cipher
  127. and IV.
  128. The caller passes a pointer to structure of type \fBEVP_CIPHER_INFO\fR via the
  129. \&\fBcinfo\fR argument and the \fBheader\fR returned via \fBPEM_read()\fR or \fBPEM_read_bio()\fR.
  130. If the call is successful 1 is returned and the cipher and IV are stored at the
  131. address pointed to by \fBcinfo\fR.
  132. When the header is malformed, or not supported or when the cipher is unknown
  133. or some internal error happens 0 is returned.
  134. This function is deprecated, see \fBNOTES\fR below.
  135. .PP
  136. \&\fBPEM_do_header()\fR can then be used to decrypt the data if the header
  137. indicates encryption.
  138. The \fBcinfo\fR argument is a pointer to the structure initialized by the previous
  139. call to \fBPEM_get_EVP_CIPHER_INFO()\fR.
  140. The \fBdata\fR and \fBlen\fR arguments are those returned by the previous call to
  141. \&\fBPEM_read()\fR or \fBPEM_read_bio()\fR.
  142. The \fBcb\fR and \fBu\fR arguments make it possible to override the default password
  143. prompt function as described in \fBPEM_read_PrivateKey\fR\|(3).
  144. On successful completion the \fBdata\fR is decrypted in place, and \fBlen\fR is
  145. updated to indicate the plaintext length.
  146. This function is deprecated, see \fBNOTES\fR below.
  147. .PP
  148. If the data is a priori known to not be encrypted, then neither \fBPEM_do_header()\fR
  149. nor \fBPEM_get_EVP_CIPHER_INFO()\fR need be called.
  150. .SH "RETURN VALUES"
  151. .IX Header "RETURN VALUES"
  152. \&\fBPEM_read()\fR and \fBPEM_read_bio()\fR return 1 on success and 0 on failure, the latter
  153. includes the case when no more PEM objects remain in the input file.
  154. To distinguish end of file from more serious errors the caller must peek at the
  155. error stack and check for \fBPEM_R_NO_START_LINE\fR, which indicates that no more
  156. PEM objects were found. See \fBERR_peek_last_error\fR\|(3), \fBERR_GET_REASON\fR\|(3).
  157. .PP
  158. \&\fBPEM_get_EVP_CIPHER_INFO()\fR and \fBPEM_do_header()\fR return 1 on success, and 0 on
  159. failure.
  160. The \fBdata\fR is likely meaningless if these functions fail.
  161. .SH NOTES
  162. .IX Header "NOTES"
  163. The \fBPEM_get_EVP_CIPHER_INFO()\fR and \fBPEM_do_header()\fR functions are deprecated.
  164. This is because the underlying PEM encryption format is obsolete, and should
  165. be avoided.
  166. It uses an encryption format with an OpenSSL-specific key-derivation function,
  167. which employs MD5 with an iteration count of 1!
  168. Instead, private keys should be stored in PKCS#8 form, with a strong PKCS#5
  169. v2.0 PBE.
  170. See \fBPEM_write_PrivateKey\fR\|(3) and \fBd2i_PKCS8PrivateKey_bio\fR\|(3).
  171. .PP
  172. \&\fBPEM_do_header()\fR makes no assumption regarding the pass phrase received from the
  173. password callback.
  174. It will simply be treated as a byte sequence.
  175. .SH "SEE ALSO"
  176. .IX Header "SEE ALSO"
  177. \&\fBERR_peek_last_error\fR\|(3), \fBERR_GET_LIB\fR\|(3),
  178. \&\fBd2i_PKCS8PrivateKey_bio\fR\|(3),
  179. \&\fBpassphrase\-encoding\fR\|(7)
  180. .SH COPYRIGHT
  181. .IX Header "COPYRIGHT"
  182. Copyright 1998\-2020 The OpenSSL Project Authors. All Rights Reserved.
  183. .PP
  184. Licensed under the Apache License 2.0 (the "License"). You may not use
  185. this file except in compliance with the License. You can obtain a copy
  186. in the file LICENSE in the source distribution or at
  187. <https://www.openssl.org/source/license.html>.