Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

313 rindas
9.1 KiB

  1. /* Copyright (C) 1995-1998 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. #include <openssl/pem.h>
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/dh.h>
  61. #include <openssl/err.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/pkcs8.h>
  66. #include <openssl/rand.h>
  67. #include <openssl/x509.h>
  68. #include "../evp/internal.h"
  69. int pem_check_suffix(const char *pem_str, const char *suffix);
  70. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  71. {
  72. char *nm=NULL;
  73. const unsigned char *p=NULL;
  74. unsigned char *data=NULL;
  75. long len;
  76. int slen;
  77. EVP_PKEY *ret=NULL;
  78. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
  79. return NULL;
  80. p = data;
  81. if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
  82. PKCS8_PRIV_KEY_INFO *p8inf;
  83. p8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  84. if(!p8inf) goto p8err;
  85. ret = EVP_PKCS82PKEY(p8inf);
  86. if(x) {
  87. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  88. *x = ret;
  89. }
  90. PKCS8_PRIV_KEY_INFO_free(p8inf);
  91. } else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {
  92. PKCS8_PRIV_KEY_INFO *p8inf;
  93. X509_SIG *p8;
  94. int klen;
  95. char psbuf[PEM_BUFSIZE];
  96. p8 = d2i_X509_SIG(NULL, &p, len);
  97. if(!p8) goto p8err;
  98. klen = 0;
  99. if (!cb) cb = PEM_def_callback;
  100. klen=cb(psbuf,PEM_BUFSIZE,0,u);
  101. if (klen <= 0) {
  102. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ);
  103. X509_SIG_free(p8);
  104. goto err;
  105. }
  106. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  107. X509_SIG_free(p8);
  108. if(!p8inf) goto p8err;
  109. ret = EVP_PKCS82PKEY(p8inf);
  110. if(x) {
  111. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  112. *x = ret;
  113. }
  114. PKCS8_PRIV_KEY_INFO_free(p8inf);
  115. } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)
  116. {
  117. const EVP_PKEY_ASN1_METHOD *ameth;
  118. ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
  119. if (!ameth || !ameth->old_priv_decode)
  120. goto p8err;
  121. ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);
  122. }
  123. p8err:
  124. if (ret == NULL)
  125. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  126. err:
  127. OPENSSL_free(nm);
  128. OPENSSL_cleanse(data, len);
  129. OPENSSL_free(data);
  130. return(ret);
  131. }
  132. int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  133. unsigned char *kstr, int klen,
  134. pem_password_cb *cb, void *u)
  135. {
  136. char pem_str[80];
  137. if (!x->ameth || x->ameth->priv_encode)
  138. return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
  139. (char *)kstr, klen,
  140. cb, u);
  141. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  142. return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  143. pem_str,bp,x,enc,kstr,klen,cb,u);
  144. }
  145. static int public_key_type_from_str(const char *name, size_t len) {
  146. if (len == 3 && memcmp(name, "RSA", 3) == 0) {
  147. return EVP_PKEY_RSA;
  148. } else if (len == 2 && memcmp(name, "DH", 2) == 0) {
  149. return EVP_PKEY_DH;
  150. } else if (len == 2 && memcmp(name, "EC", 2) == 0) {
  151. return EVP_PKEY_EC;
  152. }
  153. return NID_undef;
  154. }
  155. static int set_pkey_type_from_str(EVP_PKEY *pkey, const char *name, size_t len) {
  156. int nid = public_key_type_from_str(name, len);
  157. if (nid == NID_undef) {
  158. return 0;
  159. }
  160. return EVP_PKEY_set_type(pkey, nid);
  161. }
  162. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  163. {
  164. char *nm=NULL;
  165. const unsigned char *p=NULL;
  166. unsigned char *data=NULL;
  167. long len;
  168. int slen;
  169. EVP_PKEY *ret=NULL;
  170. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
  171. bp, 0, NULL))
  172. return NULL;
  173. p = data;
  174. if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0)
  175. {
  176. ret = EVP_PKEY_new();
  177. if (!ret)
  178. goto err;
  179. if (!set_pkey_type_from_str(ret, nm, slen)
  180. || !ret->ameth->param_decode
  181. || !ret->ameth->param_decode(ret, &p, len))
  182. {
  183. EVP_PKEY_free(ret);
  184. ret = NULL;
  185. goto err;
  186. }
  187. if(x)
  188. {
  189. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  190. *x = ret;
  191. }
  192. }
  193. err:
  194. if (ret == NULL)
  195. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  196. OPENSSL_free(nm);
  197. OPENSSL_free(data);
  198. return(ret);
  199. }
  200. int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
  201. {
  202. char pem_str[80];
  203. if (!x->ameth || !x->ameth->param_encode)
  204. return 0;
  205. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  206. return PEM_ASN1_write_bio(
  207. (i2d_of_void *)x->ameth->param_encode,
  208. pem_str,bp,x,NULL,NULL,0,0,NULL);
  209. }
  210. #ifndef OPENSSL_NO_FP_API
  211. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  212. {
  213. BIO *b;
  214. EVP_PKEY *ret;
  215. if ((b=BIO_new(BIO_s_file())) == NULL)
  216. {
  217. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  218. return(0);
  219. }
  220. BIO_set_fp(b,fp,BIO_NOCLOSE);
  221. ret=PEM_read_bio_PrivateKey(b,x,cb,u);
  222. BIO_free(b);
  223. return(ret);
  224. }
  225. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  226. unsigned char *kstr, int klen,
  227. pem_password_cb *cb, void *u)
  228. {
  229. BIO *b;
  230. int ret;
  231. if ((b=BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
  232. {
  233. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  234. return 0;
  235. }
  236. ret=PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  237. BIO_free(b);
  238. return ret;
  239. }
  240. #endif
  241. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  242. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  243. {
  244. char *nm=NULL;
  245. const unsigned char *p=NULL;
  246. unsigned char *data=NULL;
  247. long len;
  248. DH *ret=NULL;
  249. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS,
  250. bp, cb, u))
  251. return NULL;
  252. p = data;
  253. /* TODO(fork): remove? */
  254. /*if (!strcmp(nm, PEM_STRING_DHXPARAMS))
  255. ret = d2i_DHxparams(x, &p, len);
  256. else */
  257. ret = d2i_DHparams(x, &p, len);
  258. if (ret == NULL)
  259. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  260. OPENSSL_free(nm);
  261. OPENSSL_free(data);
  262. return ret;
  263. }
  264. #ifndef OPENSSL_NO_FP_API
  265. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  266. {
  267. BIO *b;
  268. DH *ret;
  269. if ((b=BIO_new(BIO_s_file())) == NULL)
  270. {
  271. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  272. return(0);
  273. }
  274. BIO_set_fp(b,fp,BIO_NOCLOSE);
  275. ret=PEM_read_bio_DHparams(b,x,cb,u);
  276. BIO_free(b);
  277. return(ret);
  278. }
  279. #endif