You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

228 line
7.6 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. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  69. void *u)
  70. {
  71. char *nm = NULL;
  72. const unsigned char *p = NULL;
  73. unsigned char *data = NULL;
  74. long len;
  75. EVP_PKEY *ret = NULL;
  76. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
  77. return NULL;
  78. p = data;
  79. if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
  80. PKCS8_PRIV_KEY_INFO *p8inf;
  81. p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  82. if (!p8inf)
  83. goto p8err;
  84. ret = EVP_PKCS82PKEY(p8inf);
  85. if (x) {
  86. if (*x)
  87. 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)
  98. goto p8err;
  99. klen = 0;
  100. if (!cb)
  101. cb = PEM_def_callback;
  102. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  103. if (klen <= 0) {
  104. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ);
  105. X509_SIG_free(p8);
  106. goto err;
  107. }
  108. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  109. X509_SIG_free(p8);
  110. OPENSSL_cleanse(psbuf, klen);
  111. if (!p8inf)
  112. goto p8err;
  113. ret = EVP_PKCS82PKEY(p8inf);
  114. if (x) {
  115. if (*x)
  116. EVP_PKEY_free((EVP_PKEY *)*x);
  117. *x = ret;
  118. }
  119. PKCS8_PRIV_KEY_INFO_free(p8inf);
  120. } else if (strcmp(nm, PEM_STRING_RSA) == 0) {
  121. /* TODO(davidben): d2i_PrivateKey parses PKCS#8 along with the
  122. * standalone format. This and the cases below probably should not
  123. * accept PKCS#8. */
  124. ret = d2i_PrivateKey(EVP_PKEY_RSA, x, &p, len);
  125. } else if (strcmp(nm, PEM_STRING_EC) == 0) {
  126. ret = d2i_PrivateKey(EVP_PKEY_EC, x, &p, len);
  127. } else if (strcmp(nm, PEM_STRING_DSA) == 0) {
  128. ret = d2i_PrivateKey(EVP_PKEY_DSA, x, &p, len);
  129. }
  130. p8err:
  131. if (ret == NULL)
  132. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  133. err:
  134. OPENSSL_free(nm);
  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. return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, (char *)kstr, klen, cb, u);
  143. }
  144. #ifndef OPENSSL_NO_FP_API
  145. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  146. void *u)
  147. {
  148. BIO *b;
  149. EVP_PKEY *ret;
  150. if ((b = BIO_new(BIO_s_file())) == NULL) {
  151. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  152. return (0);
  153. }
  154. BIO_set_fp(b, fp, BIO_NOCLOSE);
  155. ret = PEM_read_bio_PrivateKey(b, x, cb, u);
  156. BIO_free(b);
  157. return (ret);
  158. }
  159. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  160. unsigned char *kstr, int klen,
  161. pem_password_cb *cb, void *u)
  162. {
  163. BIO *b;
  164. int ret;
  165. if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  166. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  167. return 0;
  168. }
  169. ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  170. BIO_free(b);
  171. return ret;
  172. }
  173. #endif
  174. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  175. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  176. {
  177. char *nm = NULL;
  178. const unsigned char *p = NULL;
  179. unsigned char *data = NULL;
  180. long len;
  181. DH *ret = NULL;
  182. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
  183. return NULL;
  184. p = data;
  185. ret = d2i_DHparams(x, &p, len);
  186. if (ret == NULL)
  187. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  188. OPENSSL_free(nm);
  189. OPENSSL_free(data);
  190. return ret;
  191. }
  192. #ifndef OPENSSL_NO_FP_API
  193. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  194. {
  195. BIO *b;
  196. DH *ret;
  197. if ((b = BIO_new(BIO_s_file())) == NULL) {
  198. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  199. return (0);
  200. }
  201. BIO_set_fp(b, fp, BIO_NOCLOSE);
  202. ret = PEM_read_bio_DHparams(b, x, cb, u);
  203. BIO_free(b);
  204. return (ret);
  205. }
  206. #endif