pem_pkey.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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,
  71. void *u)
  72. {
  73. char *nm = NULL;
  74. const unsigned char *p = NULL;
  75. unsigned char *data = NULL;
  76. long len;
  77. int slen;
  78. EVP_PKEY *ret = NULL;
  79. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
  80. return NULL;
  81. p = data;
  82. if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
  83. PKCS8_PRIV_KEY_INFO *p8inf;
  84. p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  85. if (!p8inf)
  86. goto p8err;
  87. ret = EVP_PKCS82PKEY(p8inf);
  88. if (x) {
  89. if (*x)
  90. EVP_PKEY_free((EVP_PKEY *)*x);
  91. *x = ret;
  92. }
  93. PKCS8_PRIV_KEY_INFO_free(p8inf);
  94. } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
  95. PKCS8_PRIV_KEY_INFO *p8inf;
  96. X509_SIG *p8;
  97. int klen;
  98. char psbuf[PEM_BUFSIZE];
  99. p8 = d2i_X509_SIG(NULL, &p, len);
  100. if (!p8)
  101. goto p8err;
  102. klen = 0;
  103. if (!cb)
  104. cb = PEM_def_callback;
  105. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  106. if (klen <= 0) {
  107. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ);
  108. X509_SIG_free(p8);
  109. goto err;
  110. }
  111. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  112. X509_SIG_free(p8);
  113. if (!p8inf)
  114. goto p8err;
  115. ret = EVP_PKCS82PKEY(p8inf);
  116. if (x) {
  117. if (*x)
  118. EVP_PKEY_free((EVP_PKEY *)*x);
  119. *x = ret;
  120. }
  121. PKCS8_PRIV_KEY_INFO_free(p8inf);
  122. } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
  123. const EVP_PKEY_ASN1_METHOD *ameth;
  124. ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
  125. if (!ameth || !ameth->old_priv_decode)
  126. goto p8err;
  127. ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
  128. }
  129. p8err:
  130. if (ret == NULL)
  131. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  132. err:
  133. OPENSSL_free(nm);
  134. OPENSSL_cleanse(data, len);
  135. OPENSSL_free(data);
  136. return (ret);
  137. }
  138. int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  139. unsigned char *kstr, int klen,
  140. pem_password_cb *cb, void *u)
  141. {
  142. char pem_str[80];
  143. if (!x->ameth || x->ameth->priv_encode)
  144. return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
  145. (char *)kstr, klen, cb, u);
  146. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  147. return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  148. pem_str, bp, x, enc, kstr, klen, cb, u);
  149. }
  150. #ifndef OPENSSL_NO_FP_API
  151. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  152. void *u)
  153. {
  154. BIO *b;
  155. EVP_PKEY *ret;
  156. if ((b = BIO_new(BIO_s_file())) == NULL) {
  157. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  158. return (0);
  159. }
  160. BIO_set_fp(b, fp, BIO_NOCLOSE);
  161. ret = PEM_read_bio_PrivateKey(b, x, cb, u);
  162. BIO_free(b);
  163. return (ret);
  164. }
  165. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  166. unsigned char *kstr, int klen,
  167. pem_password_cb *cb, void *u)
  168. {
  169. BIO *b;
  170. int ret;
  171. if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  172. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  173. return 0;
  174. }
  175. ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  176. BIO_free(b);
  177. return ret;
  178. }
  179. #endif
  180. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  181. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  182. {
  183. char *nm = NULL;
  184. const unsigned char *p = NULL;
  185. unsigned char *data = NULL;
  186. long len;
  187. DH *ret = NULL;
  188. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
  189. return NULL;
  190. p = data;
  191. ret = d2i_DHparams(x, &p, len);
  192. if (ret == NULL)
  193. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  194. OPENSSL_free(nm);
  195. OPENSSL_free(data);
  196. return ret;
  197. }
  198. #ifndef OPENSSL_NO_FP_API
  199. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  200. {
  201. BIO *b;
  202. DH *ret;
  203. if ((b = BIO_new(BIO_s_file())) == NULL) {
  204. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  205. return (0);
  206. }
  207. BIO_set_fp(b, fp, BIO_NOCLOSE);
  208. ret = PEM_read_bio_DHparams(b, x, cb, u);
  209. BIO_free(b);
  210. return (ret);
  211. }
  212. #endif