pem_pkey.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/pkcs12.h>
  16. #include <openssl/pem.h>
  17. #include <openssl/engine.h>
  18. #include <openssl/dh.h>
  19. #include "crypto/asn1.h"
  20. #include "crypto/evp.h"
  21. int pem_check_suffix(const char *pem_str, const char *suffix);
  22. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  23. void *u)
  24. {
  25. char *nm = NULL;
  26. const unsigned char *p = NULL;
  27. unsigned char *data = NULL;
  28. long len;
  29. int slen;
  30. EVP_PKEY *ret = NULL;
  31. if (!PEM_bytes_read_bio_secmem(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp,
  32. cb, u))
  33. return NULL;
  34. p = data;
  35. if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
  36. PKCS8_PRIV_KEY_INFO *p8inf;
  37. p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
  38. if (!p8inf)
  39. goto p8err;
  40. ret = EVP_PKCS82PKEY(p8inf);
  41. if (x) {
  42. EVP_PKEY_free((EVP_PKEY *)*x);
  43. *x = ret;
  44. }
  45. PKCS8_PRIV_KEY_INFO_free(p8inf);
  46. } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
  47. PKCS8_PRIV_KEY_INFO *p8inf;
  48. X509_SIG *p8;
  49. int klen;
  50. char psbuf[PEM_BUFSIZE];
  51. p8 = d2i_X509_SIG(NULL, &p, len);
  52. if (!p8)
  53. goto p8err;
  54. if (cb)
  55. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  56. else
  57. klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  58. if (klen < 0) {
  59. PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ);
  60. X509_SIG_free(p8);
  61. goto err;
  62. }
  63. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  64. X509_SIG_free(p8);
  65. OPENSSL_cleanse(psbuf, klen);
  66. if (!p8inf)
  67. goto p8err;
  68. ret = EVP_PKCS82PKEY(p8inf);
  69. if (x) {
  70. EVP_PKEY_free((EVP_PKEY *)*x);
  71. *x = ret;
  72. }
  73. PKCS8_PRIV_KEY_INFO_free(p8inf);
  74. } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
  75. const EVP_PKEY_ASN1_METHOD *ameth;
  76. ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
  77. if (!ameth || !ameth->old_priv_decode)
  78. goto p8err;
  79. ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
  80. }
  81. p8err:
  82. if (ret == NULL)
  83. PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
  84. err:
  85. OPENSSL_secure_free(nm);
  86. OPENSSL_secure_clear_free(data, len);
  87. return ret;
  88. }
  89. int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  90. unsigned char *kstr, int klen,
  91. pem_password_cb *cb, void *u)
  92. {
  93. if (x->ameth == NULL || x->ameth->priv_encode != NULL)
  94. return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
  95. (char *)kstr, klen, cb, u);
  96. return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
  97. }
  98. int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
  99. const EVP_CIPHER *enc,
  100. unsigned char *kstr, int klen,
  101. pem_password_cb *cb, void *u)
  102. {
  103. char pem_str[80];
  104. if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
  105. PEMerr(PEM_F_PEM_WRITE_BIO_PRIVATEKEY_TRADITIONAL,
  106. PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  107. return 0;
  108. }
  109. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  110. return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  111. pem_str, bp, x, enc, kstr, klen, cb, u);
  112. }
  113. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  114. {
  115. char *nm = NULL;
  116. const unsigned char *p = NULL;
  117. unsigned char *data = NULL;
  118. long len;
  119. int slen;
  120. EVP_PKEY *ret = NULL;
  121. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
  122. bp, 0, NULL))
  123. return NULL;
  124. p = data;
  125. if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) {
  126. ret = EVP_PKEY_new();
  127. if (ret == NULL)
  128. goto err;
  129. if (!EVP_PKEY_set_type_str(ret, nm, slen)
  130. || !ret->ameth->param_decode
  131. || !ret->ameth->param_decode(ret, &p, len)) {
  132. EVP_PKEY_free(ret);
  133. ret = NULL;
  134. goto err;
  135. }
  136. if (x) {
  137. EVP_PKEY_free((EVP_PKEY *)*x);
  138. *x = ret;
  139. }
  140. }
  141. err:
  142. if (ret == NULL)
  143. PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
  144. OPENSSL_free(nm);
  145. OPENSSL_free(data);
  146. return ret;
  147. }
  148. int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
  149. {
  150. char pem_str[80];
  151. if (!x->ameth || !x->ameth->param_encode)
  152. return 0;
  153. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  154. return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
  155. pem_str, bp, x, NULL, NULL, 0, 0, NULL);
  156. }
  157. #ifndef OPENSSL_NO_STDIO
  158. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  159. void *u)
  160. {
  161. BIO *b;
  162. EVP_PKEY *ret;
  163. if ((b = BIO_new(BIO_s_file())) == NULL) {
  164. PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
  165. return 0;
  166. }
  167. BIO_set_fp(b, fp, BIO_NOCLOSE);
  168. ret = PEM_read_bio_PrivateKey(b, x, cb, u);
  169. BIO_free(b);
  170. return ret;
  171. }
  172. int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  173. unsigned char *kstr, int klen,
  174. pem_password_cb *cb, void *u)
  175. {
  176. BIO *b;
  177. int ret;
  178. if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  179. PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
  180. return 0;
  181. }
  182. ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  183. BIO_free(b);
  184. return ret;
  185. }
  186. #endif
  187. #ifndef OPENSSL_NO_DH
  188. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  189. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  190. {
  191. char *nm = NULL;
  192. const unsigned char *p = NULL;
  193. unsigned char *data = NULL;
  194. long len;
  195. DH *ret = NULL;
  196. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
  197. return NULL;
  198. p = data;
  199. if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0)
  200. ret = d2i_DHxparams(x, &p, len);
  201. else
  202. ret = d2i_DHparams(x, &p, len);
  203. if (ret == NULL)
  204. PEMerr(PEM_F_PEM_READ_BIO_DHPARAMS, ERR_R_ASN1_LIB);
  205. OPENSSL_free(nm);
  206. OPENSSL_free(data);
  207. return ret;
  208. }
  209. # ifndef OPENSSL_NO_STDIO
  210. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  211. {
  212. BIO *b;
  213. DH *ret;
  214. if ((b = BIO_new(BIO_s_file())) == NULL) {
  215. PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
  216. return 0;
  217. }
  218. BIO_set_fp(b, fp, BIO_NOCLOSE);
  219. ret = PEM_read_bio_DHparams(b, x, cb, u);
  220. BIO_free(b);
  221. return ret;
  222. }
  223. # endif
  224. #endif