pem.3 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. .\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .if !\nF .nr F 0
  58. .if \nF>0 \{\
  59. . de IX
  60. . tm Index:\\$1\t\\n%\t"\\$2"
  61. ..
  62. . if !\nF==2 \{\
  63. . nr % 0
  64. . nr F 2
  65. . \}
  66. .\}
  67. .\"
  68. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  69. .\" Fear. Run. Save yourself. No user-serviceable parts.
  70. . \" fudge factors for nroff and troff
  71. .if n \{\
  72. . ds #H 0
  73. . ds #V .8m
  74. . ds #F .3m
  75. . ds #[ \f1
  76. . ds #] \fP
  77. .\}
  78. .if t \{\
  79. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  80. . ds #V .6m
  81. . ds #F 0
  82. . ds #[ \&
  83. . ds #] \&
  84. .\}
  85. . \" simple accents for nroff and troff
  86. .if n \{\
  87. . ds ' \&
  88. . ds ` \&
  89. . ds ^ \&
  90. . ds , \&
  91. . ds ~ ~
  92. . ds /
  93. .\}
  94. .if t \{\
  95. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  96. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  97. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  98. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  99. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  100. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  101. .\}
  102. . \" troff and (daisy-wheel) nroff accents
  103. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  104. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  105. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  106. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  107. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  108. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  109. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  110. .ds ae a\h'-(\w'a'u*4/10)'e
  111. .ds Ae A\h'-(\w'A'u*4/10)'E
  112. . \" corrections for vroff
  113. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  114. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  115. . \" for low resolution devices (crt and lpr)
  116. .if \n(.H>23 .if \n(.V>19 \
  117. \{\
  118. . ds : e
  119. . ds 8 ss
  120. . ds o a
  121. . ds d- d\h'-1'\(ga
  122. . ds D- D\h'-1'\(hy
  123. . ds th \o'bp'
  124. . ds Th \o'LP'
  125. . ds ae ae
  126. . ds Ae AE
  127. .\}
  128. .rm #[ #] #H #V #F C
  129. .\" ========================================================================
  130. .\"
  131. .IX Title "pem 3"
  132. .TH pem 3 "2019-09-12" "1.0.2g" "OpenSSL"
  133. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  134. .\" way too many mistakes in technical documents.
  135. .if n .ad l
  136. .nh
  137. .SH "NAME"
  138. PEM, PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
  139. PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
  140. PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
  141. PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY,
  142. PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
  143. PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
  144. PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
  145. PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
  146. PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
  147. PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
  148. PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
  149. PEM_write_DSA_PUBKEY, PEM_read_bio_DSAparams, PEM_read_DSAparams,
  150. PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
  151. PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
  152. PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
  153. PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
  154. PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
  155. PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
  156. PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
  157. PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
  158. PEM_write_bio_PKCS7, PEM_write_PKCS7, PEM_read_bio_NETSCAPE_CERT_SEQUENCE,
  159. PEM_read_NETSCAPE_CERT_SEQUENCE, PEM_write_bio_NETSCAPE_CERT_SEQUENCE,
  160. PEM_write_NETSCAPE_CERT_SEQUENCE \- PEM routines
  161. .SH "SYNOPSIS"
  162. .IX Header "SYNOPSIS"
  163. .Vb 1
  164. \& #include <openssl/pem.h>
  165. \&
  166. \& EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
  167. \& pem_password_cb *cb, void *u);
  168. \&
  169. \& EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
  170. \& pem_password_cb *cb, void *u);
  171. \&
  172. \& int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  173. \& unsigned char *kstr, int klen,
  174. \& pem_password_cb *cb, void *u);
  175. \&
  176. \& int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  177. \& unsigned char *kstr, int klen,
  178. \& pem_password_cb *cb, void *u);
  179. \&
  180. \& int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  181. \& char *kstr, int klen,
  182. \& pem_password_cb *cb, void *u);
  183. \&
  184. \& int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  185. \& char *kstr, int klen,
  186. \& pem_password_cb *cb, void *u);
  187. \&
  188. \& int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
  189. \& char *kstr, int klen,
  190. \& pem_password_cb *cb, void *u);
  191. \&
  192. \& int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
  193. \& char *kstr, int klen,
  194. \& pem_password_cb *cb, void *u);
  195. \&
  196. \& EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
  197. \& pem_password_cb *cb, void *u);
  198. \&
  199. \& EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
  200. \& pem_password_cb *cb, void *u);
  201. \&
  202. \& int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
  203. \& int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
  204. \&
  205. \& RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
  206. \& pem_password_cb *cb, void *u);
  207. \&
  208. \& RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
  209. \& pem_password_cb *cb, void *u);
  210. \&
  211. \& int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
  212. \& unsigned char *kstr, int klen,
  213. \& pem_password_cb *cb, void *u);
  214. \&
  215. \& int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
  216. \& unsigned char *kstr, int klen,
  217. \& pem_password_cb *cb, void *u);
  218. \&
  219. \& RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
  220. \& pem_password_cb *cb, void *u);
  221. \&
  222. \& RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
  223. \& pem_password_cb *cb, void *u);
  224. \&
  225. \& int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
  226. \&
  227. \& int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
  228. \&
  229. \& RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
  230. \& pem_password_cb *cb, void *u);
  231. \&
  232. \& RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
  233. \& pem_password_cb *cb, void *u);
  234. \&
  235. \& int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
  236. \&
  237. \& int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
  238. \&
  239. \& DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
  240. \& pem_password_cb *cb, void *u);
  241. \&
  242. \& DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
  243. \& pem_password_cb *cb, void *u);
  244. \&
  245. \& int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
  246. \& unsigned char *kstr, int klen,
  247. \& pem_password_cb *cb, void *u);
  248. \&
  249. \& int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
  250. \& unsigned char *kstr, int klen,
  251. \& pem_password_cb *cb, void *u);
  252. \&
  253. \& DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
  254. \& pem_password_cb *cb, void *u);
  255. \&
  256. \& DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
  257. \& pem_password_cb *cb, void *u);
  258. \&
  259. \& int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
  260. \&
  261. \& int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
  262. \&
  263. \& DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
  264. \&
  265. \& DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
  266. \&
  267. \& int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
  268. \&
  269. \& int PEM_write_DSAparams(FILE *fp, DSA *x);
  270. \&
  271. \& DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
  272. \&
  273. \& DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
  274. \&
  275. \& int PEM_write_bio_DHparams(BIO *bp, DH *x);
  276. \&
  277. \& int PEM_write_DHparams(FILE *fp, DH *x);
  278. \&
  279. \& X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  280. \&
  281. \& X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  282. \&
  283. \& int PEM_write_bio_X509(BIO *bp, X509 *x);
  284. \&
  285. \& int PEM_write_X509(FILE *fp, X509 *x);
  286. \&
  287. \& X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
  288. \&
  289. \& X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
  290. \&
  291. \& int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
  292. \&
  293. \& int PEM_write_X509_AUX(FILE *fp, X509 *x);
  294. \&
  295. \& X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
  296. \& pem_password_cb *cb, void *u);
  297. \&
  298. \& X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
  299. \& pem_password_cb *cb, void *u);
  300. \&
  301. \& int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
  302. \&
  303. \& int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
  304. \&
  305. \& int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
  306. \&
  307. \& int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
  308. \&
  309. \& X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
  310. \& pem_password_cb *cb, void *u);
  311. \& X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
  312. \& pem_password_cb *cb, void *u);
  313. \& int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
  314. \& int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
  315. \&
  316. \& PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
  317. \&
  318. \& PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
  319. \&
  320. \& int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
  321. \&
  322. \& int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
  323. \&
  324. \& NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
  325. \& NETSCAPE_CERT_SEQUENCE **x,
  326. \& pem_password_cb *cb, void *u);
  327. \&
  328. \& NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
  329. \& NETSCAPE_CERT_SEQUENCE **x,
  330. \& pem_password_cb *cb, void *u);
  331. \&
  332. \& int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x);
  333. \&
  334. \& int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x);
  335. .Ve
  336. .SH "DESCRIPTION"
  337. .IX Header "DESCRIPTION"
  338. The \s-1PEM\s0 functions read or write structures in \s-1PEM\s0 format. In
  339. this sense \s-1PEM\s0 format is simply base64 encoded data surrounded
  340. by header lines.
  341. .PP
  342. For more details about the meaning of arguments see the
  343. \&\fB\s-1PEM FUNCTION ARGUMENTS\s0\fR section.
  344. .PP
  345. Each operation has four functions associated with it. For
  346. clarity the term "\fBfoobar\fR functions" will be used to collectively
  347. refer to the \fIPEM_read_bio_foobar()\fR, \fIPEM_read_foobar()\fR,
  348. \&\fIPEM_write_bio_foobar()\fR and \fIPEM_write_foobar()\fR functions.
  349. .PP
  350. The \fBPrivateKey\fR functions read or write a private key in
  351. \&\s-1PEM\s0 format using an \s-1EVP_PKEY\s0 structure. The write routines use
  352. \&\*(L"traditional\*(R" private key format and can handle both \s-1RSA\s0 and \s-1DSA\s0
  353. private keys. The read functions can additionally transparently
  354. handle PKCS#8 format encrypted and unencrypted keys too.
  355. .PP
  356. \&\fIPEM_write_bio_PKCS8PrivateKey()\fR and \fIPEM_write_PKCS8PrivateKey()\fR
  357. write a private key in an \s-1EVP_PKEY\s0 structure in PKCS#8
  358. EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption
  359. algorithms. The \fBcipher\fR argument specifies the encryption algorithm to
  360. use: unlike all other \s-1PEM\s0 routines the encryption is applied at the
  361. PKCS#8 level and not in the \s-1PEM\s0 headers. If \fBcipher\fR is \s-1NULL\s0 then no
  362. encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.
  363. .PP
  364. \&\fIPEM_write_bio_PKCS8PrivateKey_nid()\fR and \fIPEM_write_PKCS8PrivateKey_nid()\fR
  365. also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
  366. it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
  367. to use is specified in the \fBnid\fR parameter and should be the \s-1NID\s0 of the
  368. corresponding \s-1OBJECT IDENTIFIER\s0 (see \s-1NOTES\s0 section).
  369. .PP
  370. The \fB\s-1PUBKEY\s0\fR functions process a public key using an \s-1EVP_PKEY\s0
  371. structure. The public key is encoded as a SubjectPublicKeyInfo
  372. structure.
  373. .PP
  374. The \fBRSAPrivateKey\fR functions process an \s-1RSA\s0 private key using an
  375. \&\s-1RSA\s0 structure. It handles the same formats as the \fBPrivateKey\fR
  376. functions but an error occurs if the private key is not \s-1RSA.\s0
  377. .PP
  378. The \fBRSAPublicKey\fR functions process an \s-1RSA\s0 public key using an
  379. \&\s-1RSA\s0 structure. The public key is encoded using a PKCS#1 RSAPublicKey
  380. structure.
  381. .PP
  382. The \fB\s-1RSA_PUBKEY\s0\fR functions also process an \s-1RSA\s0 public key using
  383. an \s-1RSA\s0 structure. However the public key is encoded using a
  384. SubjectPublicKeyInfo structure and an error occurs if the public
  385. key is not \s-1RSA.\s0
  386. .PP
  387. The \fBDSAPrivateKey\fR functions process a \s-1DSA\s0 private key using a
  388. \&\s-1DSA\s0 structure. It handles the same formats as the \fBPrivateKey\fR
  389. functions but an error occurs if the private key is not \s-1DSA.\s0
  390. .PP
  391. The \fB\s-1DSA_PUBKEY\s0\fR functions process a \s-1DSA\s0 public key using
  392. a \s-1DSA\s0 structure. The public key is encoded using a
  393. SubjectPublicKeyInfo structure and an error occurs if the public
  394. key is not \s-1DSA.\s0
  395. .PP
  396. The \fBDSAparams\fR functions process \s-1DSA\s0 parameters using a \s-1DSA\s0
  397. structure. The parameters are encoded using a Dss-Parms structure
  398. as defined in \s-1RFC2459.\s0
  399. .PP
  400. The \fBDHparams\fR functions process \s-1DH\s0 parameters using a \s-1DH\s0
  401. structure. The parameters are encoded using a PKCS#3 DHparameter
  402. structure.
  403. .PP
  404. The \fBX509\fR functions process an X509 certificate using an X509
  405. structure. They will also process a trusted X509 certificate but
  406. any trust settings are discarded.
  407. .PP
  408. The \fBX509_AUX\fR functions process a trusted X509 certificate using
  409. an X509 structure.
  410. .PP
  411. The \fBX509_REQ\fR and \fBX509_REQ_NEW\fR functions process a PKCS#10
  412. certificate request using an X509_REQ structure. The \fBX509_REQ\fR
  413. write functions use \fB\s-1CERTIFICATE REQUEST\s0\fR in the header whereas
  414. the \fBX509_REQ_NEW\fR functions use \fB\s-1NEW CERTIFICATE REQUEST\s0\fR
  415. (as required by some CAs). The \fBX509_REQ\fR read functions will
  416. handle either form so there are no \fBX509_REQ_NEW\fR read functions.
  417. .PP
  418. The \fBX509_CRL\fR functions process an X509 \s-1CRL\s0 using an X509_CRL
  419. structure.
  420. .PP
  421. The \fB\s-1PKCS7\s0\fR functions process a PKCS#7 ContentInfo using a \s-1PKCS7\s0
  422. structure.
  423. .PP
  424. The \fB\s-1NETSCAPE_CERT_SEQUENCE\s0\fR functions process a Netscape Certificate
  425. Sequence using a \s-1NETSCAPE_CERT_SEQUENCE\s0 structure.
  426. .SH "PEM FUNCTION ARGUMENTS"
  427. .IX Header "PEM FUNCTION ARGUMENTS"
  428. The \s-1PEM\s0 functions have many common arguments.
  429. .PP
  430. The \fBbp\fR \s-1BIO\s0 parameter (if present) specifies the \s-1BIO\s0 to read from
  431. or write to.
  432. .PP
  433. The \fBfp\fR \s-1FILE\s0 parameter (if present) specifies the \s-1FILE\s0 pointer to
  434. read from or write to.
  435. .PP
  436. The \s-1PEM\s0 read functions all take an argument \fB\s-1TYPE\s0 **x\fR and return
  437. a \fB\s-1TYPE\s0 *\fR pointer. Where \fB\s-1TYPE\s0\fR is whatever structure the function
  438. uses. If \fBx\fR is \s-1NULL\s0 then the parameter is ignored. If \fBx\fR is not
  439. \&\s-1NULL\s0 but \fB*x\fR is \s-1NULL\s0 then the structure returned will be written
  440. to \fB*x\fR. If neither \fBx\fR nor \fB*x\fR is \s-1NULL\s0 then an attempt is made
  441. to reuse the structure at \fB*x\fR (but see \s-1BUGS\s0 and \s-1EXAMPLES\s0 sections).
  442. Irrespective of the value of \fBx\fR a pointer to the structure is always
  443. returned (or \s-1NULL\s0 if an error occurred).
  444. .PP
  445. The \s-1PEM\s0 functions which write private keys take an \fBenc\fR parameter
  446. which specifies the encryption algorithm to use, encryption is done
  447. at the \s-1PEM\s0 level. If this parameter is set to \s-1NULL\s0 then the private
  448. key is written in unencrypted form.
  449. .PP
  450. The \fBcb\fR argument is the callback to use when querying for the pass
  451. phrase used for encrypted \s-1PEM\s0 structures (normally only private keys).
  452. .PP
  453. For the \s-1PEM\s0 write routines if the \fBkstr\fR parameter is not \s-1NULL\s0 then
  454. \&\fBklen\fR bytes at \fBkstr\fR are used as the passphrase and \fBcb\fR is
  455. ignored.
  456. .PP
  457. If the \fBcb\fR parameters is set to \s-1NULL\s0 and the \fBu\fR parameter is not
  458. \&\s-1NULL\s0 then the \fBu\fR parameter is interpreted as a null terminated string
  459. to use as the passphrase. If both \fBcb\fR and \fBu\fR are \s-1NULL\s0 then the
  460. default callback routine is used which will typically prompt for the
  461. passphrase on the current terminal with echoing turned off.
  462. .PP
  463. The default passphrase callback is sometimes inappropriate (for example
  464. in a \s-1GUI\s0 application) so an alternative can be supplied. The callback
  465. routine has the following form:
  466. .PP
  467. .Vb 1
  468. \& int cb(char *buf, int size, int rwflag, void *u);
  469. .Ve
  470. .PP
  471. \&\fBbuf\fR is the buffer to write the passphrase to. \fBsize\fR is the maximum
  472. length of the passphrase (i.e. the size of buf). \fBrwflag\fR is a flag
  473. which is set to 0 when reading and 1 when writing. A typical routine
  474. will ask the user to verify the passphrase (for example by prompting
  475. for it twice) if \fBrwflag\fR is 1. The \fBu\fR parameter has the same
  476. value as the \fBu\fR parameter passed to the \s-1PEM\s0 routine. It allows
  477. arbitrary data to be passed to the callback by the application
  478. (for example a window handle in a \s-1GUI\s0 application). The callback
  479. \&\fBmust\fR return the number of characters in the passphrase or 0 if
  480. an error occurred.
  481. .SH "EXAMPLES"
  482. .IX Header "EXAMPLES"
  483. Although the \s-1PEM\s0 routines take several arguments in almost all applications
  484. most of them are set to 0 or \s-1NULL.\s0
  485. .PP
  486. Read a certificate in \s-1PEM\s0 format from a \s-1BIO:\s0
  487. .PP
  488. .Vb 6
  489. \& X509 *x;
  490. \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  491. \& if (x == NULL)
  492. \& {
  493. \& /* Error */
  494. \& }
  495. .Ve
  496. .PP
  497. Alternative method:
  498. .PP
  499. .Vb 5
  500. \& X509 *x = NULL;
  501. \& if (!PEM_read_bio_X509(bp, &x, 0, NULL))
  502. \& {
  503. \& /* Error */
  504. \& }
  505. .Ve
  506. .PP
  507. Write a certificate to a \s-1BIO:\s0
  508. .PP
  509. .Vb 4
  510. \& if (!PEM_write_bio_X509(bp, x))
  511. \& {
  512. \& /* Error */
  513. \& }
  514. .Ve
  515. .PP
  516. Write an unencrypted private key to a \s-1FILE\s0 pointer:
  517. .PP
  518. .Vb 4
  519. \& if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL))
  520. \& {
  521. \& /* Error */
  522. \& }
  523. .Ve
  524. .PP
  525. Write a private key (using traditional format) to a \s-1BIO\s0 using
  526. triple \s-1DES\s0 encryption, the pass phrase is prompted for:
  527. .PP
  528. .Vb 4
  529. \& if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
  530. \& {
  531. \& /* Error */
  532. \& }
  533. .Ve
  534. .PP
  535. Write a private key (using PKCS#8 format) to a \s-1BIO\s0 using triple
  536. \&\s-1DES\s0 encryption, using the pass phrase \*(L"hello\*(R":
  537. .PP
  538. .Vb 4
  539. \& if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello"))
  540. \& {
  541. \& /* Error */
  542. \& }
  543. .Ve
  544. .PP
  545. Read a private key from a \s-1BIO\s0 using the pass phrase \*(L"hello\*(R":
  546. .PP
  547. .Vb 5
  548. \& key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
  549. \& if (key == NULL)
  550. \& {
  551. \& /* Error */
  552. \& }
  553. .Ve
  554. .PP
  555. Read a private key from a \s-1BIO\s0 using a pass phrase callback:
  556. .PP
  557. .Vb 5
  558. \& key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
  559. \& if (key == NULL)
  560. \& {
  561. \& /* Error */
  562. \& }
  563. .Ve
  564. .PP
  565. Skeleton pass phrase callback:
  566. .PP
  567. .Vb 6
  568. \& int pass_cb(char *buf, int size, int rwflag, void *u);
  569. \& {
  570. \& int len;
  571. \& char *tmp;
  572. \& /* We\*(Aqd probably do something else if \*(Aqrwflag\*(Aq is 1 */
  573. \& printf("Enter pass phrase for \e"%s\e"\en", u);
  574. \&
  575. \& /* get pass phrase, length \*(Aqlen\*(Aq into \*(Aqtmp\*(Aq */
  576. \& tmp = "hello";
  577. \& len = strlen(tmp);
  578. \&
  579. \& if (len <= 0) return 0;
  580. \& /* if too long, truncate */
  581. \& if (len > size) len = size;
  582. \& memcpy(buf, tmp, len);
  583. \& return len;
  584. \& }
  585. .Ve
  586. .SH "NOTES"
  587. .IX Header "NOTES"
  588. The old \fBPrivateKey\fR write routines are retained for compatibility.
  589. New applications should write private keys using the
  590. \&\fIPEM_write_bio_PKCS8PrivateKey()\fR or \fIPEM_write_PKCS8PrivateKey()\fR routines
  591. because they are more secure (they use an iteration count of 2048 whereas
  592. the traditional routines use a count of 1) unless compatibility with older
  593. versions of OpenSSL is important.
  594. .PP
  595. The \fBPrivateKey\fR read routines can be used in all applications because
  596. they handle all formats transparently.
  597. .PP
  598. A frequent cause of problems is attempting to use the \s-1PEM\s0 routines like
  599. this:
  600. .PP
  601. .Vb 2
  602. \& X509 *x;
  603. \& PEM_read_bio_X509(bp, &x, 0, NULL);
  604. .Ve
  605. .PP
  606. this is a bug because an attempt will be made to reuse the data at \fBx\fR
  607. which is an uninitialised pointer.
  608. .SH "PEM ENCRYPTION FORMAT"
  609. .IX Header "PEM ENCRYPTION FORMAT"
  610. This old \fBPrivateKey\fR routines use a non standard technique for encryption.
  611. .PP
  612. The private key (or other data) takes the following form:
  613. .PP
  614. .Vb 3
  615. \& \-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-
  616. \& Proc\-Type: 4,ENCRYPTED
  617. \& DEK\-Info: DES\-EDE3\-CBC,3F17F5316E2BAC89
  618. \&
  619. \& ...base64 encoded data...
  620. \& \-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-
  621. .Ve
  622. .PP
  623. The line beginning DEK-Info contains two comma separated pieces of information:
  624. the encryption algorithm name as used by \fIEVP_get_cipherbyname()\fR and an 8
  625. byte \fBsalt\fR encoded as a set of hexadecimal digits.
  626. .PP
  627. After this is the base64 encoded encrypted data.
  628. .PP
  629. The encryption key is determined using \fIEVP_BytesToKey()\fR, using \fBsalt\fR and an
  630. iteration count of 1. The \s-1IV\s0 used is the value of \fBsalt\fR and *not* the \s-1IV\s0
  631. returned by \fIEVP_BytesToKey()\fR.
  632. .SH "BUGS"
  633. .IX Header "BUGS"
  634. The \s-1PEM\s0 read routines in some versions of OpenSSL will not correctly reuse
  635. an existing structure. Therefore the following:
  636. .PP
  637. .Vb 1
  638. \& PEM_read_bio_X509(bp, &x, 0, NULL);
  639. .Ve
  640. .PP
  641. where \fBx\fR already contains a valid certificate, may not work, whereas:
  642. .PP
  643. .Vb 2
  644. \& X509_free(x);
  645. \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
  646. .Ve
  647. .PP
  648. is guaranteed to work.
  649. .SH "RETURN CODES"
  650. .IX Header "RETURN CODES"
  651. The read routines return either a pointer to the structure read or \s-1NULL\s0
  652. if an error occurred.
  653. .PP
  654. The write routines return 1 for success or 0 for failure.
  655. .SH "SEE ALSO"
  656. .IX Header "SEE ALSO"
  657. \&\fIEVP_get_cipherbyname\fR\|(3), \fIEVP_BytesToKey\fR\|(3)