pem_pkey.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* crypto/pem/pem_pkey.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/buffer.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/rand.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/pkcs12.h>
  66. #include <openssl/pem.h>
  67. #include "asn1_locl.h"
  68. int pem_check_suffix(const char *pem_str, const char *suffix);
  69. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  70. {
  71. char *nm=NULL;
  72. const unsigned char *p=NULL;
  73. unsigned char *data=NULL;
  74. long len;
  75. int slen;
  76. EVP_PKEY *ret=NULL;
  77. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
  78. return NULL;
  79. p = data;
  80. if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
  81. PKCS8_PRIV_KEY_INFO *p8inf;
  82. p8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  83. if(!p8inf) goto p8err;
  84. ret = EVP_PKCS82PKEY(p8inf);
  85. if(x) {
  86. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  87. *x = ret;
  88. }
  89. PKCS8_PRIV_KEY_INFO_free(p8inf);
  90. } else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {
  91. PKCS8_PRIV_KEY_INFO *p8inf;
  92. X509_SIG *p8;
  93. int klen;
  94. char psbuf[PEM_BUFSIZE];
  95. p8 = d2i_X509_SIG(NULL, &p, len);
  96. if(!p8) goto p8err;
  97. if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);
  98. else klen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u);
  99. if (klen <= 0) {
  100. PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,
  101. PEM_R_BAD_PASSWORD_READ);
  102. X509_SIG_free(p8);
  103. goto err;
  104. }
  105. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  106. X509_SIG_free(p8);
  107. if(!p8inf) goto p8err;
  108. ret = EVP_PKCS82PKEY(p8inf);
  109. if(x) {
  110. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  111. *x = ret;
  112. }
  113. PKCS8_PRIV_KEY_INFO_free(p8inf);
  114. } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)
  115. {
  116. const EVP_PKEY_ASN1_METHOD *ameth;
  117. ameth = EVP_PKEY_asn1_find_str(nm, slen);
  118. if (!ameth || !ameth->old_priv_decode)
  119. goto p8err;
  120. ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);
  121. }
  122. p8err:
  123. if (ret == NULL)
  124. PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,ERR_R_ASN1_LIB);
  125. err:
  126. OPENSSL_free(nm);
  127. OPENSSL_free(data);
  128. return(ret);
  129. }
  130. int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  131. unsigned char *kstr, int klen,
  132. pem_password_cb *cb, void *u)
  133. {
  134. char pem_str[80];
  135. if (!x->ameth || !x->ameth->old_priv_encode)
  136. return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
  137. (char *)kstr, klen,
  138. cb, u);
  139. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  140. return PEM_ASN1_write_bio((i2d_of_void *)openssl_fcast(i2d_PrivateKey),
  141. pem_str,bp,(char *)x,enc,kstr,klen,cb,u);
  142. }
  143. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  144. {
  145. char *nm=NULL;
  146. const unsigned char *p=NULL;
  147. unsigned char *data=NULL;
  148. long len;
  149. int slen;
  150. EVP_PKEY *ret=NULL;
  151. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
  152. bp, 0, NULL))
  153. return NULL;
  154. p = data;
  155. if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0)
  156. {
  157. const EVP_PKEY_ASN1_METHOD *ameth;
  158. ameth = EVP_PKEY_asn1_find_str(nm, slen);
  159. if (!ameth || !ameth->param_decode)
  160. goto err;
  161. ret = EVP_PKEY_new();
  162. if (!ret)
  163. goto err;
  164. if (!ameth->param_decode(ret, &p, len))
  165. {
  166. EVP_PKEY_free(ret);
  167. ret = NULL;
  168. goto err;
  169. }
  170. if(x)
  171. {
  172. if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
  173. *x = ret;
  174. }
  175. }
  176. err:
  177. if (ret == NULL)
  178. PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS,ERR_R_ASN1_LIB);
  179. OPENSSL_free(nm);
  180. OPENSSL_free(data);
  181. return(ret);
  182. }
  183. int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
  184. {
  185. char pem_str[80];
  186. if (!x->ameth || !x->ameth->param_encode)
  187. return 0;
  188. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  189. return PEM_ASN1_write_bio(
  190. (i2d_of_void *)openssl_fcast(x->ameth->param_encode),
  191. pem_str,bp,(char *)x,NULL,NULL,0,0,NULL);
  192. }
  193. #ifndef OPENSSL_NO_FP_API
  194. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  195. {
  196. BIO *b;
  197. EVP_PKEY *ret;
  198. if ((b=BIO_new(BIO_s_file())) == NULL)
  199. {
  200. PEMerr(PEM_F_PEM_READ_PRIVATEKEY,ERR_R_BUF_LIB);
  201. return(0);
  202. }
  203. BIO_set_fp(b,fp,BIO_NOCLOSE);
  204. ret=PEM_read_bio_PrivateKey(b,x,cb,u);
  205. BIO_free(b);
  206. return(ret);
  207. }
  208. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  209. unsigned char *kstr, int klen,
  210. pem_password_cb *cb, void *u)
  211. {
  212. BIO *b;
  213. int ret;
  214. if ((b=BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
  215. {
  216. PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY,ERR_R_BUF_LIB);
  217. return 0;
  218. }
  219. ret=PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  220. BIO_free(b);
  221. return ret;
  222. }
  223. #endif