PEM_read_bio_PrivateKey.pod 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. =pod
  2. =head1 NAME
  3. pem_password_cb,
  4. PEM_read_bio_PrivateKey_ex, PEM_read_bio_PrivateKey,
  5. PEM_read_PrivateKey_ex, PEM_read_PrivateKey,
  6. PEM_write_bio_PrivateKey_ex, PEM_write_bio_PrivateKey,
  7. PEM_write_bio_PrivateKey_traditional,
  8. PEM_write_PrivateKey_ex, PEM_write_PrivateKey,
  9. PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
  10. PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
  11. PEM_read_bio_PUBKEY_ex, PEM_read_bio_PUBKEY,
  12. PEM_read_PUBKEY_ex, PEM_read_PUBKEY,
  13. PEM_write_bio_PUBKEY_ex, PEM_write_bio_PUBKEY,
  14. PEM_write_PUBKEY_ex, PEM_write_PUBKEY,
  15. PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
  16. PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
  17. PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
  18. PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
  19. PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
  20. PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
  21. PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
  22. PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters_ex, PEM_read_bio_Parameters,
  23. PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams,
  24. PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
  25. PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
  26. PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
  27. PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
  28. PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
  29. PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
  30. PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
  31. PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
  32. PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
  33. =head1 SYNOPSIS
  34. #include <openssl/pem.h>
  35. typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
  36. EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
  37. pem_password_cb *cb, void *u,
  38. OSSL_LIB_CTX *libctx, const char *propq);
  39. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
  40. pem_password_cb *cb, void *u);
  41. EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  42. void *u, OSSL_LIB_CTX *libctx,
  43. const char *propq);
  44. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
  45. pem_password_cb *cb, void *u);
  46. int PEM_write_bio_PrivateKey_ex(BIO *bp, const EVP_PKEY *x,
  47. const EVP_CIPHER *enc,
  48. unsigned char *kstr, int klen,
  49. pem_password_cb *cb, void *u,
  50. OSSL_LIB_CTX *libctx, const char *propq);
  51. int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  52. unsigned char *kstr, int klen,
  53. pem_password_cb *cb, void *u);
  54. int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
  55. const EVP_CIPHER *enc,
  56. unsigned char *kstr, int klen,
  57. pem_password_cb *cb, void *u);
  58. int PEM_write_PrivateKey_ex(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  59. unsigned char *kstr, int klen,
  60. pem_password_cb *cb, void *u,
  61. OSSL_LIB_CTX *libctx, const char *propq);
  62. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  63. unsigned char *kstr, int klen,
  64. pem_password_cb *cb, void *u);
  65. int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  66. char *kstr, int klen,
  67. pem_password_cb *cb, void *u);
  68. int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  69. char *kstr, int klen,
  70. pem_password_cb *cb, void *u);
  71. int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
  72. char *kstr, int klen,
  73. pem_password_cb *cb, void *u);
  74. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
  75. char *kstr, int klen,
  76. pem_password_cb *cb, void *u);
  77. EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
  78. pem_password_cb *cb, void *u,
  79. OSSL_LIB_CTX *libctx, const char *propq);
  80. EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
  81. pem_password_cb *cb, void *u);
  82. EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
  83. pem_password_cb *cb, void *u,
  84. OSSL_LIB_CTX *libctx, const char *propq);
  85. EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
  86. pem_password_cb *cb, void *u);
  87. int PEM_write_bio_PUBKEY_ex(BIO *bp, EVP_PKEY *x,
  88. OSSL_LIB_CTX *libctx, const char *propq);
  89. int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
  90. int PEM_write_PUBKEY_ex(FILE *fp, EVP_PKEY *x,
  91. OSSL_LIB_CTX *libctx, const char *propq);
  92. int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
  93. EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
  94. OSSL_LIB_CTX *libctx, const char *propq);
  95. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
  96. int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
  97. X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  98. X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  99. int PEM_write_bio_X509(BIO *bp, X509 *x);
  100. int PEM_write_X509(FILE *fp, X509 *x);
  101. X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  102. X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  103. int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
  104. int PEM_write_X509_AUX(FILE *fp, X509 *x);
  105. X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
  106. pem_password_cb *cb, void *u);
  107. X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
  108. pem_password_cb *cb, void *u);
  109. int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
  110. int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
  111. int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
  112. int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
  113. X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
  114. pem_password_cb *cb, void *u);
  115. X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
  116. pem_password_cb *cb, void *u);
  117. int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
  118. int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
  119. PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
  120. PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
  121. int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
  122. int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
  123. The following functions have been deprecated since OpenSSL 3.0, and can be
  124. hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
  125. see L<openssl_user_macros(7)>:
  126. RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
  127. pem_password_cb *cb, void *u);
  128. RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
  129. pem_password_cb *cb, void *u);
  130. int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
  131. unsigned char *kstr, int klen,
  132. pem_password_cb *cb, void *u);
  133. int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
  134. unsigned char *kstr, int klen,
  135. pem_password_cb *cb, void *u);
  136. RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
  137. pem_password_cb *cb, void *u);
  138. RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
  139. pem_password_cb *cb, void *u);
  140. int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
  141. int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
  142. RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
  143. pem_password_cb *cb, void *u);
  144. RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
  145. pem_password_cb *cb, void *u);
  146. int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
  147. int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
  148. DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
  149. pem_password_cb *cb, void *u);
  150. DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
  151. pem_password_cb *cb, void *u);
  152. int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
  153. unsigned char *kstr, int klen,
  154. pem_password_cb *cb, void *u);
  155. int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
  156. unsigned char *kstr, int klen,
  157. pem_password_cb *cb, void *u);
  158. DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
  159. pem_password_cb *cb, void *u);
  160. DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
  161. pem_password_cb *cb, void *u);
  162. int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
  163. int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
  164. DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
  165. DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
  166. int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
  167. int PEM_write_DSAparams(FILE *fp, DSA *x);
  168. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
  169. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
  170. int PEM_write_bio_DHparams(BIO *bp, DH *x);
  171. int PEM_write_DHparams(FILE *fp, DH *x);
  172. =head1 DESCRIPTION
  173. All of the functions described on this page that have a I<TYPE> of B<DH>, B<DSA>
  174. and B<RSA> are deprecated. Applications should use L<OSSL_ENCODER_to_bio(3)> and
  175. L<OSSL_DECODER_from_bio(3)> instead.
  176. The PEM functions read or write structures in PEM format. In
  177. this sense PEM format is simply base64 encoded data surrounded
  178. by header lines.
  179. For more details about the meaning of arguments see the
  180. B<PEM FUNCTION ARGUMENTS> section.
  181. Each operation has four functions associated with it. For
  182. brevity the term "B<I<TYPE>> functions" will be used below to collectively
  183. refer to the B<PEM_read_bio_I<TYPE>>(), B<PEM_read_I<TYPE>>(),
  184. B<PEM_write_bio_I<TYPE>>(), and B<PEM_write_I<TYPE>>() functions.
  185. Some operations have additional variants that take a library context I<libctx>
  186. and a property query string I<propq>. The B<X509>, B<X509_REQ> and B<X509_CRL>
  187. objects may have an associated library context or property query string but
  188. there are no variants of these functions that take a library context or property
  189. query string parameter. In this case it is possible to set the appropriate
  190. library context or property query string by creating an empty B<X509>,
  191. B<X509_REQ> or B<X509_CRL> object using L<X509_new_ex(3)>, L<X509_REQ_new_ex(3)>
  192. or L<X509_CRL_new_ex(3)> respectively. Then pass the empty object as a parameter
  193. to the relevant PEM function. See the L</EXAMPLES> section below.
  194. The B<PrivateKey> functions read or write a private key in PEM format using
  195. an EVP_PKEY structure. The write routines use PKCS#8 private key format and are
  196. equivalent to PEM_write_bio_PKCS8PrivateKey(). The read functions transparently
  197. handle traditional and PKCS#8 format encrypted and unencrypted keys.
  198. PEM_write_bio_PrivateKey_traditional() writes out a private key in the
  199. "traditional" format with a simple private key marker and should only
  200. be used for compatibility with legacy programs.
  201. PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private
  202. key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using
  203. PKCS#5 v2.0 password based encryption algorithms. The I<cipher> argument
  204. specifies the encryption algorithm to use: unlike some other PEM routines the
  205. encryption is applied at the PKCS#8 level and not in the PEM headers. If
  206. I<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
  207. structure is used instead.
  208. PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
  209. also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
  210. it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
  211. to use is specified in the I<nid> parameter and should be the NID of the
  212. corresponding OBJECT IDENTIFIER (see NOTES section).
  213. The B<PUBKEY> functions process a public key using an EVP_PKEY
  214. structure. The public key is encoded as a SubjectPublicKeyInfo
  215. structure.
  216. The B<RSAPrivateKey> functions process an RSA private key using an
  217. RSA structure. The write routines uses traditional format. The read
  218. routines handles the same formats as the B<PrivateKey>
  219. functions but an error occurs if the private key is not RSA.
  220. The B<RSAPublicKey> functions process an RSA public key using an
  221. RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
  222. structure.
  223. The B<RSA_PUBKEY> functions also process an RSA public key using
  224. an RSA structure. However, the public key is encoded using a
  225. SubjectPublicKeyInfo structure and an error occurs if the public
  226. key is not RSA.
  227. The B<DSAPrivateKey> functions process a DSA private key using a
  228. DSA structure. The write routines uses traditional format. The read
  229. routines handles the same formats as the B<PrivateKey>
  230. functions but an error occurs if the private key is not DSA.
  231. The B<DSA_PUBKEY> functions process a DSA public key using
  232. a DSA structure. The public key is encoded using a
  233. SubjectPublicKeyInfo structure and an error occurs if the public
  234. key is not DSA.
  235. The B<Parameters> functions read or write key parameters in PEM format using
  236. an EVP_PKEY structure. The encoding depends on the type of key; for DSA key
  237. parameters, it will be a Dss-Parms structure as defined in RFC2459, and for DH
  238. key parameters, it will be a PKCS#3 DHparameter structure. I<These functions
  239. only exist for the B<BIO> type>.
  240. The B<DSAparams> functions process DSA parameters using a DSA
  241. structure. The parameters are encoded using a Dss-Parms structure
  242. as defined in RFC2459.
  243. The B<DHparams> functions process DH parameters using a DH
  244. structure. The parameters are encoded using a PKCS#3 DHparameter
  245. structure.
  246. The B<X509> functions process an X509 certificate using an X509
  247. structure. They will also process a trusted X509 certificate but
  248. any trust settings are discarded.
  249. The B<X509_AUX> functions process a trusted X509 certificate using
  250. an X509 structure.
  251. The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
  252. certificate request using an X509_REQ structure. The B<X509_REQ>
  253. write functions use B<CERTIFICATE REQUEST> in the header whereas
  254. the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
  255. (as required by some CAs). The B<X509_REQ> read functions will
  256. handle either form so there are no B<X509_REQ_NEW> read functions.
  257. The B<X509_CRL> functions process an X509 CRL using an X509_CRL
  258. structure.
  259. The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
  260. structure.
  261. =head1 PEM FUNCTION ARGUMENTS
  262. The PEM functions have many common arguments.
  263. The I<bp> BIO parameter (if present) specifies the BIO to read from
  264. or write to.
  265. The I<fp> FILE parameter (if present) specifies the FILE pointer to
  266. read from or write to.
  267. The PEM read functions all take an argument I<B<TYPE> **x> and return
  268. a I<B<TYPE> *> pointer. Where I<B<TYPE>> is whatever structure the function
  269. uses. If I<x> is NULL then the parameter is ignored. If I<x> is not
  270. NULL but I<*x> is NULL then the structure returned will be written
  271. to I<*x>. If neither I<x> nor I<*x> is NULL then an attempt is made
  272. to reuse the structure at I<*x> (but see BUGS and EXAMPLES sections).
  273. Irrespective of the value of I<x> a pointer to the structure is always
  274. returned (or NULL if an error occurred).
  275. The PEM functions which write private keys take an I<enc> parameter
  276. which specifies the encryption algorithm to use, encryption is done
  277. at the PEM level. If this parameter is set to NULL then the private
  278. key is written in unencrypted form.
  279. The I<cb> argument is the callback to use when querying for the pass
  280. phrase used for encrypted PEM structures (normally only private keys).
  281. For the PEM write routines if the I<kstr> parameter is not NULL then
  282. I<klen> bytes at I<kstr> are used as the passphrase and I<cb> is
  283. ignored.
  284. If the I<cb> parameters is set to NULL and the I<u> parameter is not
  285. NULL then the I<u> parameter is interpreted as a NUL terminated string
  286. to use as the passphrase. If both I<cb> and I<u> are NULL then the
  287. default callback routine is used which will typically prompt for the
  288. passphrase on the current terminal with echoing turned off.
  289. The default passphrase callback is sometimes inappropriate (for example
  290. in a GUI application) so an alternative can be supplied. The callback
  291. routine has the following form:
  292. int cb(char *buf, int size, int rwflag, void *u);
  293. I<buf> is the buffer to write the passphrase to. I<size> is the maximum
  294. length of the passphrase (i.e. the size of buf). I<rwflag> is a flag
  295. which is set to 0 when reading and 1 when writing. A typical routine
  296. will ask the user to verify the passphrase (for example by prompting
  297. for it twice) if I<rwflag> is 1. The I<u> parameter has the same
  298. value as the I<u> parameter passed to the PEM routine. It allows
  299. arbitrary data to be passed to the callback by the application
  300. (for example a window handle in a GUI application). The callback
  301. I<must> return the number of characters in the passphrase or -1 if
  302. an error occurred. The passphrase can be arbitrary data; in the case where it
  303. is a string, it is not NUL terminated. See the L</EXAMPLES> section below.
  304. Some implementations may need to use cryptographic algorithms during their
  305. operation. If this is the case and I<libctx> and I<propq> parameters have been
  306. passed then any algorithm fetches will use that library context and property
  307. query string. Otherwise the default library context and property query string
  308. will be used.
  309. =head1 NOTES
  310. The PEM reading functions will skip any extraneous content or PEM data of
  311. a different type than they expect. This allows for example having a certificate
  312. (or multiple certificates) and a key in the PEM format in a single file.
  313. The old B<PrivateKey> write routines are retained for compatibility.
  314. New applications should write private keys using the
  315. PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
  316. because they are more secure (they use an iteration count of 2048 whereas
  317. the traditional routines use a count of 1) unless compatibility with older
  318. versions of OpenSSL is important.
  319. The B<PrivateKey> read routines can be used in all applications because
  320. they handle all formats transparently.
  321. A frequent cause of problems is attempting to use the PEM routines like
  322. this:
  323. X509 *x;
  324. PEM_read_bio_X509(bp, &x, 0, NULL);
  325. this is a bug because an attempt will be made to reuse the data at I<x>
  326. which is an uninitialised pointer.
  327. These functions make no assumption regarding the pass phrase received from the
  328. password callback.
  329. It will simply be treated as a byte sequence.
  330. =head1 PEM ENCRYPTION FORMAT
  331. These old B<PrivateKey> routines use a non standard technique for encryption.
  332. The private key (or other data) takes the following form:
  333. -----BEGIN RSA PRIVATE KEY-----
  334. Proc-Type: 4,ENCRYPTED
  335. DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
  336. ...base64 encoded data...
  337. -----END RSA PRIVATE KEY-----
  338. The line beginning with I<Proc-Type> contains the version and the
  339. protection on the encapsulated data. The line beginning I<DEK-Info>
  340. contains two comma separated values: the encryption algorithm name as
  341. used by EVP_get_cipherbyname() and an initialization vector used by the
  342. cipher encoded as a set of hexadecimal digits. After those two lines is
  343. the base64-encoded encrypted data.
  344. The encryption key is derived using EVP_BytesToKey(). The cipher's
  345. initialization vector is passed to EVP_BytesToKey() as the I<salt>
  346. parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
  347. (regardless of the size of the initialization vector). The user's
  348. password is passed to EVP_BytesToKey() using the I<data> and I<datal>
  349. parameters. Finally, the library uses an iteration count of 1 for
  350. EVP_BytesToKey().
  351. The I<key> derived by EVP_BytesToKey() along with the original initialization
  352. vector is then used to decrypt the encrypted data. The I<iv> produced by
  353. EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
  354. the function.
  355. The pseudo code to derive the key would look similar to:
  356. EVP_CIPHER* cipher = EVP_des_ede3_cbc();
  357. EVP_MD* md = EVP_md5();
  358. unsigned int nkey = EVP_CIPHER_get_key_length(cipher);
  359. unsigned int niv = EVP_CIPHER_get_iv_length(cipher);
  360. unsigned char key[nkey];
  361. unsigned char iv[niv];
  362. memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
  363. rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
  364. if (rc != nkey)
  365. /* Error */
  366. /* On success, use key and iv to initialize the cipher */
  367. =head1 BUGS
  368. The PEM read routines in some versions of OpenSSL will not correctly reuse
  369. an existing structure. Therefore, the following:
  370. PEM_read_bio_X509(bp, &x, 0, NULL);
  371. where I<x> already contains a valid certificate, may not work, whereas:
  372. X509_free(x);
  373. x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  374. is guaranteed to work. It is always acceptable for I<x> to contain a newly
  375. allocated, empty B<X509> object (for example allocated via L<X509_new_ex(3)>).
  376. =head1 RETURN VALUES
  377. The read routines return either a pointer to the structure read or NULL
  378. if an error occurred.
  379. The write routines return 1 for success or 0 for failure.
  380. =head1 EXAMPLES
  381. Although the PEM routines take several arguments in almost all applications
  382. most of them are set to 0 or NULL.
  383. To read a certificate with a library context in PEM format from a BIO:
  384. X509 *x = X509_new_ex(libctx, NULL);
  385. if (x == NULL)
  386. /* Error */
  387. if (PEM_read_bio_X509(bp, &x, 0, NULL) == NULL)
  388. /* Error */
  389. Read a certificate in PEM format from a BIO:
  390. X509 *x;
  391. x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  392. if (x == NULL)
  393. /* Error */
  394. Alternative method:
  395. X509 *x = NULL;
  396. if (!PEM_read_bio_X509(bp, &x, 0, NULL))
  397. /* Error */
  398. Write a certificate to a BIO:
  399. if (!PEM_write_bio_X509(bp, x))
  400. /* Error */
  401. Write a private key (using traditional format) to a BIO using
  402. triple DES encryption, the pass phrase is prompted for:
  403. if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
  404. /* Error */
  405. Write a private key (using PKCS#8 format) to a BIO using triple
  406. DES encryption, using the pass phrase "hello":
  407. if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
  408. NULL, 0, 0, "hello"))
  409. /* Error */
  410. Read a private key from a BIO using a pass phrase callback:
  411. key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
  412. if (key == NULL)
  413. /* Error */
  414. Skeleton pass phrase callback:
  415. int pass_cb(char *buf, int size, int rwflag, void *u)
  416. {
  417. /* We'd probably do something else if 'rwflag' is 1 */
  418. printf("Enter pass phrase for \"%s\"\n", (char *)u);
  419. /* get pass phrase, length 'len' into 'tmp' */
  420. char *tmp = "hello";
  421. if (tmp == NULL) /* An error occurred */
  422. return -1;
  423. size_t len = strlen(tmp);
  424. if (len > size)
  425. len = size;
  426. memcpy(buf, tmp, len);
  427. return len;
  428. }
  429. =head1 SEE ALSO
  430. L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>,
  431. L<passphrase-encoding(7)>
  432. =head1 HISTORY
  433. The old Netscape certificate sequences were no longer documented
  434. in OpenSSL 1.1.0; applications should use the PKCS7 standard instead
  435. as they will be formally deprecated in a future releases.
  436. PEM_read_bio_PrivateKey_ex(), PEM_read_PrivateKey_ex(),
  437. PEM_read_bio_PUBKEY_ex(), PEM_read_PUBKEY_ex() and
  438. PEM_read_bio_Parameters_ex() were introduced in OpenSSL 3.0.
  439. The functions PEM_read_bio_RSAPrivateKey(), PEM_read_RSAPrivateKey(),
  440. PEM_write_bio_RSAPrivateKey(), PEM_write_RSAPrivateKey(),
  441. PEM_read_bio_RSAPublicKey(), PEM_read_RSAPublicKey(),
  442. PEM_write_bio_RSAPublicKey(), PEM_write_RSAPublicKey(),
  443. PEM_read_bio_RSA_PUBKEY(), PEM_read_RSA_PUBKEY(),
  444. PEM_write_bio_RSA_PUBKEY(), PEM_write_RSA_PUBKEY(),
  445. PEM_read_bio_DSAPrivateKey(), PEM_read_DSAPrivateKey(),
  446. PEM_write_bio_DSAPrivateKey(), PEM_write_DSAPrivateKey(),
  447. PEM_read_bio_DSA_PUBKEY(), PEM_read_DSA_PUBKEY(),
  448. PEM_write_bio_DSA_PUBKEY(), PEM_write_DSA_PUBKEY();
  449. PEM_read_bio_DSAparams(), PEM_read_DSAparams(),
  450. PEM_write_bio_DSAparams(), PEM_write_DSAparams(),
  451. PEM_read_bio_DHparams(), PEM_read_DHparams(),
  452. PEM_write_bio_DHparams() and PEM_write_DHparams() were deprecated in 3.0.
  453. =head1 COPYRIGHT
  454. Copyright 2001-2022 The OpenSSL Project Authors. All Rights Reserved.
  455. Licensed under the Apache License 2.0 (the "License"). You may not use
  456. this file except in compliance with the License. You can obtain a copy
  457. in the file LICENSE in the source distribution or at
  458. L<https://www.openssl.org/source/license.html>.
  459. =cut