pem.pod 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. =pod
  2. =head1 NAME
  3. PEM, PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
  4. PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
  5. PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
  6. PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY,
  7. PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
  8. PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
  9. PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
  10. PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
  11. PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
  12. PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
  13. PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
  14. PEM_write_DSA_PUBKEY, PEM_read_bio_DSAparams, PEM_read_DSAparams,
  15. PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
  16. PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
  17. PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
  18. PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
  19. PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
  20. PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
  21. PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
  22. PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
  23. PEM_write_bio_PKCS7, PEM_write_PKCS7, PEM_read_bio_NETSCAPE_CERT_SEQUENCE,
  24. PEM_read_NETSCAPE_CERT_SEQUENCE, PEM_write_bio_NETSCAPE_CERT_SEQUENCE,
  25. PEM_write_NETSCAPE_CERT_SEQUENCE - PEM routines
  26. =head1 SYNOPSIS
  27. #include <openssl/pem.h>
  28. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
  29. pem_password_cb *cb, void *u);
  30. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
  31. pem_password_cb *cb, void *u);
  32. int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  33. unsigned char *kstr, int klen,
  34. pem_password_cb *cb, void *u);
  35. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  36. unsigned char *kstr, int klen,
  37. pem_password_cb *cb, void *u);
  38. int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  39. char *kstr, int klen,
  40. pem_password_cb *cb, void *u);
  41. int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  42. char *kstr, int klen,
  43. pem_password_cb *cb, void *u);
  44. int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
  45. char *kstr, int klen,
  46. pem_password_cb *cb, void *u);
  47. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
  48. char *kstr, int klen,
  49. pem_password_cb *cb, void *u);
  50. EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
  51. pem_password_cb *cb, void *u);
  52. EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
  53. pem_password_cb *cb, void *u);
  54. int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
  55. int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
  56. RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
  57. pem_password_cb *cb, void *u);
  58. RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
  59. pem_password_cb *cb, void *u);
  60. int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
  61. unsigned char *kstr, int klen,
  62. pem_password_cb *cb, void *u);
  63. int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
  64. unsigned char *kstr, int klen,
  65. pem_password_cb *cb, void *u);
  66. RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
  67. pem_password_cb *cb, void *u);
  68. RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
  69. pem_password_cb *cb, void *u);
  70. int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
  71. int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
  72. RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
  73. pem_password_cb *cb, void *u);
  74. RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
  75. pem_password_cb *cb, void *u);
  76. int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
  77. int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
  78. DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
  79. pem_password_cb *cb, void *u);
  80. DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
  81. pem_password_cb *cb, void *u);
  82. int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
  83. unsigned char *kstr, int klen,
  84. pem_password_cb *cb, void *u);
  85. int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
  86. unsigned char *kstr, int klen,
  87. pem_password_cb *cb, void *u);
  88. DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
  89. pem_password_cb *cb, void *u);
  90. DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
  91. pem_password_cb *cb, void *u);
  92. int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
  93. int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
  94. DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
  95. DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
  96. int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
  97. int PEM_write_DSAparams(FILE *fp, DSA *x);
  98. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
  99. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
  100. int PEM_write_bio_DHparams(BIO *bp, DH *x);
  101. int PEM_write_DHparams(FILE *fp, DH *x);
  102. X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  103. X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  104. int PEM_write_bio_X509(BIO *bp, X509 *x);
  105. int PEM_write_X509(FILE *fp, X509 *x);
  106. X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  107. X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  108. int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
  109. int PEM_write_X509_AUX(FILE *fp, X509 *x);
  110. X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
  111. pem_password_cb *cb, void *u);
  112. X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
  113. pem_password_cb *cb, void *u);
  114. int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
  115. int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
  116. int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
  117. int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
  118. X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
  119. pem_password_cb *cb, void *u);
  120. X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
  121. pem_password_cb *cb, void *u);
  122. int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
  123. int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
  124. PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
  125. PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
  126. int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
  127. int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
  128. NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
  129. NETSCAPE_CERT_SEQUENCE **x,
  130. pem_password_cb *cb, void *u);
  131. NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
  132. NETSCAPE_CERT_SEQUENCE **x,
  133. pem_password_cb *cb, void *u);
  134. int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x);
  135. int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x);
  136. =head1 DESCRIPTION
  137. The PEM functions read or write structures in PEM format. In
  138. this sense PEM format is simply base64 encoded data surrounded
  139. by header lines.
  140. For more details about the meaning of arguments see the
  141. B<PEM FUNCTION ARGUMENTS> section.
  142. Each operation has four functions associated with it. For
  143. clarity the term "B<foobar> functions" will be used to collectively
  144. refer to the PEM_read_bio_foobar(), PEM_read_foobar(),
  145. PEM_write_bio_foobar() and PEM_write_foobar() functions.
  146. The B<PrivateKey> functions read or write a private key in
  147. PEM format using an EVP_PKEY structure. The write routines use
  148. "traditional" private key format and can handle both RSA and DSA
  149. private keys. The read functions can additionally transparently
  150. handle PKCS#8 format encrypted and unencrypted keys too.
  151. PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey()
  152. write a private key in an EVP_PKEY structure in PKCS#8
  153. EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption
  154. algorithms. The B<cipher> argument specifies the encryption algorithm to
  155. use: unlike all other PEM routines the encryption is applied at the
  156. PKCS#8 level and not in the PEM headers. If B<cipher> is NULL then no
  157. encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.
  158. PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
  159. also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
  160. it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
  161. to use is specified in the B<nid> parameter and should be the NID of the
  162. corresponding OBJECT IDENTIFIER (see NOTES section).
  163. The B<PUBKEY> functions process a public key using an EVP_PKEY
  164. structure. The public key is encoded as a SubjectPublicKeyInfo
  165. structure.
  166. The B<RSAPrivateKey> functions process an RSA private key using an
  167. RSA structure. It handles the same formats as the B<PrivateKey>
  168. functions but an error occurs if the private key is not RSA.
  169. The B<RSAPublicKey> functions process an RSA public key using an
  170. RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
  171. structure.
  172. The B<RSA_PUBKEY> functions also process an RSA public key using
  173. an RSA structure. However the public key is encoded using a
  174. SubjectPublicKeyInfo structure and an error occurs if the public
  175. key is not RSA.
  176. The B<DSAPrivateKey> functions process a DSA private key using a
  177. DSA structure. It handles the same formats as the B<PrivateKey>
  178. functions but an error occurs if the private key is not DSA.
  179. The B<DSA_PUBKEY> functions process a DSA public key using
  180. a DSA structure. The public key is encoded using a
  181. SubjectPublicKeyInfo structure and an error occurs if the public
  182. key is not DSA.
  183. The B<DSAparams> functions process DSA parameters using a DSA
  184. structure. The parameters are encoded using a Dss-Parms structure
  185. as defined in RFC2459.
  186. The B<DHparams> functions process DH parameters using a DH
  187. structure. The parameters are encoded using a PKCS#3 DHparameter
  188. structure.
  189. The B<X509> functions process an X509 certificate using an X509
  190. structure. They will also process a trusted X509 certificate but
  191. any trust settings are discarded.
  192. The B<X509_AUX> functions process a trusted X509 certificate using
  193. an X509 structure.
  194. The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
  195. certificate request using an X509_REQ structure. The B<X509_REQ>
  196. write functions use B<CERTIFICATE REQUEST> in the header whereas
  197. the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
  198. (as required by some CAs). The B<X509_REQ> read functions will
  199. handle either form so there are no B<X509_REQ_NEW> read functions.
  200. The B<X509_CRL> functions process an X509 CRL using an X509_CRL
  201. structure.
  202. The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
  203. structure.
  204. The B<NETSCAPE_CERT_SEQUENCE> functions process a Netscape Certificate
  205. Sequence using a NETSCAPE_CERT_SEQUENCE structure.
  206. =head1 PEM FUNCTION ARGUMENTS
  207. The PEM functions have many common arguments.
  208. The B<bp> BIO parameter (if present) specifies the BIO to read from
  209. or write to.
  210. The B<fp> FILE parameter (if present) specifies the FILE pointer to
  211. read from or write to.
  212. The PEM read functions all take an argument B<TYPE **x> and return
  213. a B<TYPE *> pointer. Where B<TYPE> is whatever structure the function
  214. uses. If B<x> is NULL then the parameter is ignored. If B<x> is not
  215. NULL but B<*x> is NULL then the structure returned will be written
  216. to B<*x>. If neither B<x> nor B<*x> is NULL then an attempt is made
  217. to reuse the structure at B<*x> (but see BUGS and EXAMPLES sections).
  218. Irrespective of the value of B<x> a pointer to the structure is always
  219. returned (or NULL if an error occurred).
  220. The PEM functions which write private keys take an B<enc> parameter
  221. which specifies the encryption algorithm to use, encryption is done
  222. at the PEM level. If this parameter is set to NULL then the private
  223. key is written in unencrypted form.
  224. The B<cb> argument is the callback to use when querying for the pass
  225. phrase used for encrypted PEM structures (normally only private keys).
  226. For the PEM write routines if the B<kstr> parameter is not NULL then
  227. B<klen> bytes at B<kstr> are used as the passphrase and B<cb> is
  228. ignored.
  229. If the B<cb> parameters is set to NULL and the B<u> parameter is not
  230. NULL then the B<u> parameter is interpreted as a null terminated string
  231. to use as the passphrase. If both B<cb> and B<u> are NULL then the
  232. default callback routine is used which will typically prompt for the
  233. passphrase on the current terminal with echoing turned off.
  234. The default passphrase callback is sometimes inappropriate (for example
  235. in a GUI application) so an alternative can be supplied. The callback
  236. routine has the following form:
  237. int cb(char *buf, int size, int rwflag, void *u);
  238. B<buf> is the buffer to write the passphrase to. B<size> is the maximum
  239. length of the passphrase (i.e. the size of buf). B<rwflag> is a flag
  240. which is set to 0 when reading and 1 when writing. A typical routine
  241. will ask the user to verify the passphrase (for example by prompting
  242. for it twice) if B<rwflag> is 1. The B<u> parameter has the same
  243. value as the B<u> parameter passed to the PEM routine. It allows
  244. arbitrary data to be passed to the callback by the application
  245. (for example a window handle in a GUI application). The callback
  246. B<must> return the number of characters in the passphrase or 0 if
  247. an error occurred.
  248. =head1 EXAMPLES
  249. Although the PEM routines take several arguments in almost all applications
  250. most of them are set to 0 or NULL.
  251. Read a certificate in PEM format from a BIO:
  252. X509 *x;
  253. x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  254. if (x == NULL)
  255. {
  256. /* Error */
  257. }
  258. Alternative method:
  259. X509 *x = NULL;
  260. if (!PEM_read_bio_X509(bp, &x, 0, NULL))
  261. {
  262. /* Error */
  263. }
  264. Write a certificate to a BIO:
  265. if (!PEM_write_bio_X509(bp, x))
  266. {
  267. /* Error */
  268. }
  269. Write an unencrypted private key to a FILE pointer:
  270. if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL))
  271. {
  272. /* Error */
  273. }
  274. Write a private key (using traditional format) to a BIO using
  275. triple DES encryption, the pass phrase is prompted for:
  276. if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
  277. {
  278. /* Error */
  279. }
  280. Write a private key (using PKCS#8 format) to a BIO using triple
  281. DES encryption, using the pass phrase "hello":
  282. if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello"))
  283. {
  284. /* Error */
  285. }
  286. Read a private key from a BIO using the pass phrase "hello":
  287. key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
  288. if (key == NULL)
  289. {
  290. /* Error */
  291. }
  292. Read a private key from a BIO using a pass phrase callback:
  293. key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
  294. if (key == NULL)
  295. {
  296. /* Error */
  297. }
  298. Skeleton pass phrase callback:
  299. int pass_cb(char *buf, int size, int rwflag, void *u);
  300. {
  301. int len;
  302. char *tmp;
  303. /* We'd probably do something else if 'rwflag' is 1 */
  304. printf("Enter pass phrase for \"%s\"\n", u);
  305. /* get pass phrase, length 'len' into 'tmp' */
  306. tmp = "hello";
  307. len = strlen(tmp);
  308. if (len <= 0) return 0;
  309. /* if too long, truncate */
  310. if (len > size) len = size;
  311. memcpy(buf, tmp, len);
  312. return len;
  313. }
  314. =head1 NOTES
  315. The old B<PrivateKey> write routines are retained for compatibility.
  316. New applications should write private keys using the
  317. PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
  318. because they are more secure (they use an iteration count of 2048 whereas
  319. the traditional routines use a count of 1) unless compatibility with older
  320. versions of OpenSSL is important.
  321. The B<PrivateKey> read routines can be used in all applications because
  322. they handle all formats transparently.
  323. A frequent cause of problems is attempting to use the PEM routines like
  324. this:
  325. X509 *x;
  326. PEM_read_bio_X509(bp, &x, 0, NULL);
  327. this is a bug because an attempt will be made to reuse the data at B<x>
  328. which is an uninitialised pointer.
  329. =head1 PEM ENCRYPTION FORMAT
  330. This old B<PrivateKey> routines use a non standard technique for encryption.
  331. The private key (or other data) takes the following form:
  332. -----BEGIN RSA PRIVATE KEY-----
  333. Proc-Type: 4,ENCRYPTED
  334. DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
  335. ...base64 encoded data...
  336. -----END RSA PRIVATE KEY-----
  337. The line beginning DEK-Info contains two comma separated pieces of information:
  338. the encryption algorithm name as used by EVP_get_cipherbyname() and an 8
  339. byte B<salt> encoded as a set of hexadecimal digits.
  340. After this is the base64 encoded encrypted data.
  341. The encryption key is determined using EVP_BytesToKey(), using B<salt> and an
  342. iteration count of 1. The IV used is the value of B<salt> and *not* the IV
  343. returned by EVP_BytesToKey().
  344. =head1 BUGS
  345. The PEM read routines in some versions of OpenSSL will not correctly reuse
  346. an existing structure. Therefore the following:
  347. PEM_read_bio_X509(bp, &x, 0, NULL);
  348. where B<x> already contains a valid certificate, may not work, whereas:
  349. X509_free(x);
  350. x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  351. is guaranteed to work.
  352. =head1 RETURN CODES
  353. The read routines return either a pointer to the structure read or NULL
  354. if an error occurred.
  355. The write routines return 1 for success or 0 for failure.
  356. =head1 SEE ALSO
  357. L<EVP_get_cipherbyname(3)|EVP_get_cipherbyname>, L<EVP_BytesToKey(3)|EVP_BytesToKey(3)>