25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

398 satır
16 KiB

  1. /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_PEM_H
  57. #define OPENSSL_HEADER_PEM_H
  58. #include <openssl/base64.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/cipher.h>
  61. #include <openssl/digest.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/stack.h>
  64. #include <openssl/x509.h>
  65. /* For compatibility with open-iscsi, which assumes that it can get
  66. * |OPENSSL_malloc| from pem.h or err.h */
  67. #include <openssl/crypto.h>
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. #define PEM_BUFSIZE 1024
  72. #define PEM_STRING_X509_OLD "X509 CERTIFICATE"
  73. #define PEM_STRING_X509 "CERTIFICATE"
  74. #define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
  75. #define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
  76. #define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
  77. #define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
  78. #define PEM_STRING_X509_CRL "X509 CRL"
  79. #define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
  80. #define PEM_STRING_PUBLIC "PUBLIC KEY"
  81. #define PEM_STRING_RSA "RSA PRIVATE KEY"
  82. #define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
  83. #define PEM_STRING_DSA "DSA PRIVATE KEY"
  84. #define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
  85. #define PEM_STRING_EC "EC PRIVATE KEY"
  86. #define PEM_STRING_PKCS7 "PKCS7"
  87. #define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
  88. #define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
  89. #define PEM_STRING_PKCS8INF "PRIVATE KEY"
  90. #define PEM_STRING_DHPARAMS "DH PARAMETERS"
  91. #define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
  92. #define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
  93. #define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
  94. #define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
  95. #define PEM_STRING_CMS "CMS"
  96. /* enc_type is one off */
  97. #define PEM_TYPE_ENCRYPTED 10
  98. #define PEM_TYPE_MIC_ONLY 20
  99. #define PEM_TYPE_MIC_CLEAR 30
  100. #define PEM_TYPE_CLEAR 40
  101. /* These macros make the PEM_read/PEM_write functions easier to maintain and
  102. * write. Now they are all implemented with either:
  103. * IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
  104. */
  105. #ifdef OPENSSL_NO_FP_API
  106. #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
  107. #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
  108. #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/
  109. #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
  110. #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/
  111. #else
  112. #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
  113. OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
  114. { \
  115. return (type *)PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
  116. }
  117. #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
  118. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) \
  119. { \
  120. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
  121. }
  122. #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
  123. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) \
  124. { \
  125. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
  126. }
  127. #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
  128. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  129. unsigned char *kstr, int klen, pem_password_cb *cb, \
  130. void *u) \
  131. { \
  132. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
  133. }
  134. #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
  135. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  136. unsigned char *kstr, int klen, pem_password_cb *cb, \
  137. void *u) \
  138. { \
  139. return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
  140. }
  141. #endif
  142. #define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
  143. OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
  144. { \
  145. return (type *)PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
  146. }
  147. #define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
  148. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) \
  149. { \
  150. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
  151. }
  152. #define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
  153. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) \
  154. { \
  155. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
  156. }
  157. #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
  158. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  159. unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
  160. { \
  161. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
  162. }
  163. #define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
  164. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  165. unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
  166. { \
  167. return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
  168. }
  169. #define IMPLEMENT_PEM_write(name, type, str, asn1) \
  170. IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
  171. IMPLEMENT_PEM_write_fp(name, type, str, asn1)
  172. #define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
  173. IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
  174. IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
  175. #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
  176. IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
  177. IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
  178. #define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
  179. IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
  180. IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
  181. #define IMPLEMENT_PEM_read(name, type, str, asn1) \
  182. IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
  183. IMPLEMENT_PEM_read_fp(name, type, str, asn1)
  184. #define IMPLEMENT_PEM_rw(name, type, str, asn1) \
  185. IMPLEMENT_PEM_read(name, type, str, asn1) \
  186. IMPLEMENT_PEM_write(name, type, str, asn1)
  187. #define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
  188. IMPLEMENT_PEM_read(name, type, str, asn1) \
  189. IMPLEMENT_PEM_write_const(name, type, str, asn1)
  190. #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
  191. IMPLEMENT_PEM_read(name, type, str, asn1) \
  192. IMPLEMENT_PEM_write_cb(name, type, str, asn1)
  193. /* These are the same except they are for the declarations */
  194. #if defined(OPENSSL_NO_FP_API)
  195. #define DECLARE_PEM_read_fp(name, type) /**/
  196. #define DECLARE_PEM_write_fp(name, type) /**/
  197. #define DECLARE_PEM_write_cb_fp(name, type) /**/
  198. #else
  199. #define DECLARE_PEM_read_fp(name, type) \
  200. OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
  201. #define DECLARE_PEM_write_fp(name, type) \
  202. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x);
  203. #define DECLARE_PEM_write_fp_const(name, type) \
  204. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x);
  205. #define DECLARE_PEM_write_cb_fp(name, type) \
  206. OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
  207. unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
  208. #endif
  209. #define DECLARE_PEM_read_bio(name, type) \
  210. OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
  211. #define DECLARE_PEM_write_bio(name, type) \
  212. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x);
  213. #define DECLARE_PEM_write_bio_const(name, type) \
  214. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x);
  215. #define DECLARE_PEM_write_cb_bio(name, type) \
  216. OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
  217. unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
  218. #define DECLARE_PEM_write(name, type) \
  219. DECLARE_PEM_write_bio(name, type) \
  220. DECLARE_PEM_write_fp(name, type)
  221. #define DECLARE_PEM_write_const(name, type) \
  222. DECLARE_PEM_write_bio_const(name, type) \
  223. DECLARE_PEM_write_fp_const(name, type)
  224. #define DECLARE_PEM_write_cb(name, type) \
  225. DECLARE_PEM_write_cb_bio(name, type) \
  226. DECLARE_PEM_write_cb_fp(name, type)
  227. #define DECLARE_PEM_read(name, type) \
  228. DECLARE_PEM_read_bio(name, type) \
  229. DECLARE_PEM_read_fp(name, type)
  230. #define DECLARE_PEM_rw(name, type) \
  231. DECLARE_PEM_read(name, type) \
  232. DECLARE_PEM_write(name, type)
  233. #define DECLARE_PEM_rw_const(name, type) \
  234. DECLARE_PEM_read(name, type) \
  235. DECLARE_PEM_write_const(name, type)
  236. #define DECLARE_PEM_rw_cb(name, type) \
  237. DECLARE_PEM_read(name, type) \
  238. DECLARE_PEM_write_cb(name, type)
  239. /* "userdata": new with OpenSSL 0.9.4 */
  240. typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
  241. OPENSSL_EXPORT int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
  242. OPENSSL_EXPORT int PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data,long *len, pem_password_cb *callback,void *u);
  243. OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,long *len);
  244. OPENSSL_EXPORT int PEM_write_bio(BIO *bp,const char *name, const char *hdr, const unsigned char *data, long len);
  245. OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp, pem_password_cb *cb, void *u);
  246. OPENSSL_EXPORT void * PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x, pem_password_cb *cb, void *u);
  247. OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d,const char *name,BIO *bp, void *x, const EVP_CIPHER *enc,unsigned char *kstr,int klen, pem_password_cb *cb, void *u);
  248. OPENSSL_EXPORT STACK_OF(X509_INFO) * PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
  249. OPENSSL_EXPORT int PEM_X509_INFO_write_bio(BIO *bp,X509_INFO *xi, EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cd, void *u);
  250. OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,long *len);
  251. OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr, const unsigned char *data, long len);
  252. OPENSSL_EXPORT void * PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, pem_password_cb *cb, void *u);
  253. OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d,const char *name,FILE *fp, void *x,const EVP_CIPHER *enc,unsigned char *kstr, int klen,pem_password_cb *callback, void *u);
  254. OPENSSL_EXPORT STACK_OF(X509_INFO) * PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
  255. /* PEM_def_callback treats |userdata| as a string and copies it into |buf|,
  256. * assuming its |size| is sufficient. Returns the length of the string, or 0
  257. * if there is not enough room. If either |buf| or |userdata| is NULL, 0 is
  258. * returned. Note that this is different from OpenSSL, which prompts for a
  259. * password. */
  260. OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag, void *userdata);
  261. OPENSSL_EXPORT void PEM_proc_type(char *buf, int type);
  262. OPENSSL_EXPORT void PEM_dek_info(char *buf, const char *type, int len, char *str);
  263. DECLARE_PEM_rw(X509, X509)
  264. DECLARE_PEM_rw(X509_AUX, X509)
  265. DECLARE_PEM_rw(X509_REQ, X509_REQ)
  266. DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
  267. DECLARE_PEM_rw(X509_CRL, X509_CRL)
  268. DECLARE_PEM_rw(PKCS8, X509_SIG)
  269. DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
  270. DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
  271. DECLARE_PEM_rw_const(RSAPublicKey, RSA)
  272. DECLARE_PEM_rw(RSA_PUBKEY, RSA)
  273. #ifndef OPENSSL_NO_DSA
  274. DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
  275. DECLARE_PEM_rw(DSA_PUBKEY, DSA)
  276. DECLARE_PEM_rw_const(DSAparams, DSA)
  277. #endif
  278. DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
  279. DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
  280. DECLARE_PEM_rw_const(DHparams, DH)
  281. DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
  282. DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
  283. OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u);
  284. OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *, char *, int, pem_password_cb *, void *);
  285. OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
  286. OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u);
  287. OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u);
  288. OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
  289. OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u);
  290. OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u);
  291. OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u);
  292. OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp,EVP_PKEY *x,const EVP_CIPHER *enc, char *kstr,int klen, pem_password_cb *cd, void *u);
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #define PEM_R_BAD_BASE64_DECODE 100
  297. #define PEM_R_BAD_DECRYPT 101
  298. #define PEM_R_BAD_END_LINE 102
  299. #define PEM_R_BAD_IV_CHARS 103
  300. #define PEM_R_BAD_PASSWORD_READ 104
  301. #define PEM_R_CIPHER_IS_NULL 105
  302. #define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106
  303. #define PEM_R_NOT_DEK_INFO 107
  304. #define PEM_R_NOT_ENCRYPTED 108
  305. #define PEM_R_NOT_PROC_TYPE 109
  306. #define PEM_R_NO_START_LINE 110
  307. #define PEM_R_READ_KEY 111
  308. #define PEM_R_SHORT_HEADER 112
  309. #define PEM_R_UNSUPPORTED_CIPHER 113
  310. #define PEM_R_UNSUPPORTED_ENCRYPTION 114
  311. #endif /* OPENSSL_HEADER_PEM_H */