2
0

pem.pod 17 KB

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