pem.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef HEADER_PEM_H
  10. # define HEADER_PEM_H
  11. # include <openssl/e_os2.h>
  12. # include <openssl/bio.h>
  13. # include <openssl/safestack.h>
  14. # include <openssl/evp.h>
  15. # include <openssl/x509.h>
  16. # include <openssl/pemerr.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. # define PEM_BUFSIZE 1024
  21. # define PEM_STRING_X509_OLD "X509 CERTIFICATE"
  22. # define PEM_STRING_X509 "CERTIFICATE"
  23. # define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
  24. # define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
  25. # define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
  26. # define PEM_STRING_X509_CRL "X509 CRL"
  27. # define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
  28. # define PEM_STRING_PUBLIC "PUBLIC KEY"
  29. # define PEM_STRING_RSA "RSA PRIVATE KEY"
  30. # define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
  31. # define PEM_STRING_DSA "DSA PRIVATE KEY"
  32. # define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
  33. # define PEM_STRING_PKCS7 "PKCS7"
  34. # define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
  35. # define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
  36. # define PEM_STRING_PKCS8INF "PRIVATE KEY"
  37. # define PEM_STRING_DHPARAMS "DH PARAMETERS"
  38. # define PEM_STRING_DHXPARAMS "X9.42 DH PARAMETERS"
  39. # define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
  40. # define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
  41. # define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
  42. # define PEM_STRING_ECPARAMETERS "EC PARAMETERS"
  43. # define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
  44. # define PEM_STRING_PARAMETERS "PARAMETERS"
  45. # define PEM_STRING_CMS "CMS"
  46. # define PEM_TYPE_ENCRYPTED 10
  47. # define PEM_TYPE_MIC_ONLY 20
  48. # define PEM_TYPE_MIC_CLEAR 30
  49. # define PEM_TYPE_CLEAR 40
  50. /*
  51. * These macros make the PEM_read/PEM_write functions easier to maintain and
  52. * write. Now they are all implemented with either: IMPLEMENT_PEM_rw(...) or
  53. * IMPLEMENT_PEM_rw_cb(...)
  54. */
  55. # ifdef OPENSSL_NO_STDIO
  56. # define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
  57. # define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
  58. # define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/
  59. # define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
  60. # define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/
  61. # else
  62. # define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
  63. type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
  64. { \
  65. return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
  66. }
  67. # define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
  68. int PEM_write_##name(FILE *fp, type *x) \
  69. { \
  70. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
  71. }
  72. # define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
  73. int PEM_write_##name(FILE *fp, const type *x) \
  74. { \
  75. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
  76. }
  77. # define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
  78. int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  79. unsigned char *kstr, int klen, pem_password_cb *cb, \
  80. void *u) \
  81. { \
  82. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
  83. }
  84. # define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
  85. int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  86. unsigned char *kstr, int klen, pem_password_cb *cb, \
  87. void *u) \
  88. { \
  89. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
  90. }
  91. # endif
  92. # define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
  93. type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
  94. { \
  95. return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
  96. }
  97. # define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
  98. int PEM_write_bio_##name(BIO *bp, type *x) \
  99. { \
  100. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
  101. }
  102. # define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
  103. int PEM_write_bio_##name(BIO *bp, const type *x) \
  104. { \
  105. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
  106. }
  107. # define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
  108. int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  109. unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
  110. { \
  111. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
  112. }
  113. # define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
  114. int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  115. unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
  116. { \
  117. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
  118. }
  119. # define IMPLEMENT_PEM_write(name, type, str, asn1) \
  120. IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
  121. IMPLEMENT_PEM_write_fp(name, type, str, asn1)
  122. # define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
  123. IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
  124. IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
  125. # define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
  126. IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
  127. IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
  128. # define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
  129. IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
  130. IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
  131. # define IMPLEMENT_PEM_read(name, type, str, asn1) \
  132. IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
  133. IMPLEMENT_PEM_read_fp(name, type, str, asn1)
  134. # define IMPLEMENT_PEM_rw(name, type, str, asn1) \
  135. IMPLEMENT_PEM_read(name, type, str, asn1) \
  136. IMPLEMENT_PEM_write(name, type, str, asn1)
  137. # define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
  138. IMPLEMENT_PEM_read(name, type, str, asn1) \
  139. IMPLEMENT_PEM_write_const(name, type, str, asn1)
  140. # define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
  141. IMPLEMENT_PEM_read(name, type, str, asn1) \
  142. IMPLEMENT_PEM_write_cb(name, type, str, asn1)
  143. /* These are the same except they are for the declarations */
  144. # if defined(OPENSSL_NO_STDIO)
  145. # define DECLARE_PEM_read_fp(name, type) /**/
  146. # define DECLARE_PEM_write_fp(name, type) /**/
  147. # define DECLARE_PEM_write_fp_const(name, type) /**/
  148. # define DECLARE_PEM_write_cb_fp(name, type) /**/
  149. # else
  150. # define DECLARE_PEM_read_fp(name, type) \
  151. type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
  152. # define DECLARE_PEM_write_fp(name, type) \
  153. int PEM_write_##name(FILE *fp, type *x);
  154. # define DECLARE_PEM_write_fp_const(name, type) \
  155. int PEM_write_##name(FILE *fp, const type *x);
  156. # define DECLARE_PEM_write_cb_fp(name, type) \
  157. int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  158. unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
  159. # endif
  160. # define DECLARE_PEM_read_bio(name, type) \
  161. type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
  162. # define DECLARE_PEM_write_bio(name, type) \
  163. int PEM_write_bio_##name(BIO *bp, type *x);
  164. # define DECLARE_PEM_write_bio_const(name, type) \
  165. int PEM_write_bio_##name(BIO *bp, const type *x);
  166. # define DECLARE_PEM_write_cb_bio(name, type) \
  167. int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  168. unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
  169. # define DECLARE_PEM_write(name, type) \
  170. DECLARE_PEM_write_bio(name, type) \
  171. DECLARE_PEM_write_fp(name, type)
  172. # define DECLARE_PEM_write_const(name, type) \
  173. DECLARE_PEM_write_bio_const(name, type) \
  174. DECLARE_PEM_write_fp_const(name, type)
  175. # define DECLARE_PEM_write_cb(name, type) \
  176. DECLARE_PEM_write_cb_bio(name, type) \
  177. DECLARE_PEM_write_cb_fp(name, type)
  178. # define DECLARE_PEM_read(name, type) \
  179. DECLARE_PEM_read_bio(name, type) \
  180. DECLARE_PEM_read_fp(name, type)
  181. # define DECLARE_PEM_rw(name, type) \
  182. DECLARE_PEM_read(name, type) \
  183. DECLARE_PEM_write(name, type)
  184. # define DECLARE_PEM_rw_const(name, type) \
  185. DECLARE_PEM_read(name, type) \
  186. DECLARE_PEM_write_const(name, type)
  187. # define DECLARE_PEM_rw_cb(name, type) \
  188. DECLARE_PEM_read(name, type) \
  189. DECLARE_PEM_write_cb(name, type)
  190. typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata);
  191. int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
  192. int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len,
  193. pem_password_cb *callback, void *u);
  194. int PEM_read_bio(BIO *bp, char **name, char **header,
  195. unsigned char **data, long *len);
  196. # define PEM_FLAG_SECURE 0x1
  197. # define PEM_FLAG_EAY_COMPATIBLE 0x2
  198. # define PEM_FLAG_ONLY_B64 0x4
  199. int PEM_read_bio_ex(BIO *bp, char **name, char **header,
  200. unsigned char **data, long *len, unsigned int flags);
  201. int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
  202. const char *name, BIO *bp, pem_password_cb *cb,
  203. void *u);
  204. int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
  205. const unsigned char *data, long len);
  206. int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
  207. const char *name, BIO *bp, pem_password_cb *cb,
  208. void *u);
  209. void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
  210. pem_password_cb *cb, void *u);
  211. int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
  212. const EVP_CIPHER *enc, unsigned char *kstr, int klen,
  213. pem_password_cb *cb, void *u);
  214. STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
  215. pem_password_cb *cb, void *u);
  216. int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
  217. unsigned char *kstr, int klen,
  218. pem_password_cb *cd, void *u);
  219. #ifndef OPENSSL_NO_STDIO
  220. int PEM_read(FILE *fp, char **name, char **header,
  221. unsigned char **data, long *len);
  222. int PEM_write(FILE *fp, const char *name, const char *hdr,
  223. const unsigned char *data, long len);
  224. void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
  225. pem_password_cb *cb, void *u);
  226. int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
  227. void *x, const EVP_CIPHER *enc, unsigned char *kstr,
  228. int klen, pem_password_cb *callback, void *u);
  229. STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
  230. pem_password_cb *cb, void *u);
  231. #endif
  232. int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
  233. int PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *d, unsigned int cnt);
  234. int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
  235. unsigned int *siglen, EVP_PKEY *pkey);
  236. /* The default pem_password_cb that's used internally */
  237. int PEM_def_callback(char *buf, int num, int rwflag, void *userdata);
  238. void PEM_proc_type(char *buf, int type);
  239. void PEM_dek_info(char *buf, const char *type, int len, char *str);
  240. # include <openssl/symhacks.h>
  241. DECLARE_PEM_rw(X509, X509)
  242. DECLARE_PEM_rw(X509_AUX, X509)
  243. DECLARE_PEM_rw(X509_REQ, X509_REQ)
  244. DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
  245. DECLARE_PEM_rw(X509_CRL, X509_CRL)
  246. DECLARE_PEM_rw(PKCS7, PKCS7)
  247. DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
  248. DECLARE_PEM_rw(PKCS8, X509_SIG)
  249. DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
  250. # ifndef OPENSSL_NO_RSA
  251. DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
  252. DECLARE_PEM_rw_const(RSAPublicKey, RSA)
  253. DECLARE_PEM_rw(RSA_PUBKEY, RSA)
  254. # endif
  255. # ifndef OPENSSL_NO_DSA
  256. DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
  257. DECLARE_PEM_rw(DSA_PUBKEY, DSA)
  258. DECLARE_PEM_rw_const(DSAparams, DSA)
  259. # endif
  260. # ifndef OPENSSL_NO_EC
  261. DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP)
  262. DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
  263. DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
  264. # endif
  265. # ifndef OPENSSL_NO_DH
  266. DECLARE_PEM_rw_const(DHparams, DH)
  267. DECLARE_PEM_write_const(DHxparams, DH)
  268. # endif
  269. DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
  270. DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
  271. int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
  272. const EVP_CIPHER *enc,
  273. unsigned char *kstr, int klen,
  274. pem_password_cb *cb, void *u);
  275. int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
  276. char *kstr, int klen,
  277. pem_password_cb *cb, void *u);
  278. int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *,
  279. char *, int, pem_password_cb *, void *);
  280. int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  281. char *kstr, int klen,
  282. pem_password_cb *cb, void *u);
  283. int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
  284. char *kstr, int klen,
  285. pem_password_cb *cb, void *u);
  286. EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  287. void *u);
  288. # ifndef OPENSSL_NO_STDIO
  289. int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  290. char *kstr, int klen,
  291. pem_password_cb *cb, void *u);
  292. int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
  293. char *kstr, int klen,
  294. pem_password_cb *cb, void *u);
  295. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
  296. char *kstr, int klen,
  297. pem_password_cb *cb, void *u);
  298. EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  299. void *u);
  300. int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  301. char *kstr, int klen, pem_password_cb *cd,
  302. void *u);
  303. # endif
  304. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
  305. int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x);
  306. # ifndef OPENSSL_NO_DSA
  307. EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
  308. EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
  309. EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
  310. EVP_PKEY *b2i_PublicKey_bio(BIO *in);
  311. int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk);
  312. int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk);
  313. # ifndef OPENSSL_NO_RC4
  314. EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
  315. int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
  316. pem_password_cb *cb, void *u);
  317. # endif
  318. # endif
  319. # ifdef __cplusplus
  320. }
  321. # endif
  322. #endif