pem_pkey.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /* We need to use some STORE deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/buffer.h>
  14. #include <openssl/objects.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/pkcs12.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/dh.h>
  21. #include <openssl/store.h>
  22. #include <openssl/ui.h>
  23. #include "crypto/store.h"
  24. #include "crypto/asn1.h"
  25. #include "crypto/evp.h"
  26. #include "pem_local.h"
  27. int pem_check_suffix(const char *pem_str, const char *suffix);
  28. static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
  29. pem_password_cb *cb, void *u,
  30. OPENSSL_CTX *libctx, const char *propq,
  31. int expected_store_info_type,
  32. int try_secure)
  33. {
  34. EVP_PKEY *ret = NULL;
  35. OSSL_STORE_CTX *ctx = NULL;
  36. OSSL_STORE_INFO *info = NULL;
  37. const UI_METHOD *ui_method = NULL;
  38. UI_METHOD *allocated_ui_method = NULL;
  39. if (expected_store_info_type != OSSL_STORE_INFO_PKEY
  40. && expected_store_info_type != OSSL_STORE_INFO_PUBKEY
  41. && expected_store_info_type != OSSL_STORE_INFO_PARAMS) {
  42. ERR_raise(ERR_LIB_PEM, ERR_R_PASSED_INVALID_ARGUMENT);
  43. return NULL;
  44. }
  45. if (u != NULL && cb == NULL)
  46. cb = PEM_def_callback;
  47. if (cb == NULL)
  48. ui_method = UI_null();
  49. else
  50. ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
  51. if (ui_method == NULL)
  52. return NULL;
  53. if ((ctx = OSSL_STORE_attach(bp, "file", libctx, propq, ui_method, u,
  54. NULL, NULL)) == NULL)
  55. goto err;
  56. #ifndef OPENSSL_NO_SECURE_HEAP
  57. # ifndef OPENSSL_NO_DEPRECATED_3_0
  58. if (try_secure) {
  59. int on = 1;
  60. if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
  61. goto err;
  62. }
  63. # endif
  64. #endif
  65. while (!OSSL_STORE_eof(ctx)
  66. && (info = OSSL_STORE_load(ctx)) != NULL) {
  67. if (OSSL_STORE_INFO_get_type(info) == expected_store_info_type) {
  68. switch (expected_store_info_type) {
  69. case OSSL_STORE_INFO_PKEY:
  70. ret = OSSL_STORE_INFO_get1_PKEY(info);
  71. break;
  72. case OSSL_STORE_INFO_PUBKEY:
  73. ret = OSSL_STORE_INFO_get1_PUBKEY(info);
  74. break;
  75. case OSSL_STORE_INFO_PARAMS:
  76. ret = OSSL_STORE_INFO_get1_PARAMS(info);
  77. break;
  78. }
  79. }
  80. OSSL_STORE_INFO_free(info);
  81. info = NULL;
  82. }
  83. if (ret != NULL && x != NULL)
  84. *x = ret;
  85. err:
  86. OSSL_STORE_close(ctx);
  87. UI_destroy_method(allocated_ui_method);
  88. OSSL_STORE_INFO_free(info);
  89. return ret;
  90. }
  91. EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
  92. pem_password_cb *cb, void *u,
  93. OPENSSL_CTX *libctx, const char *propq)
  94. {
  95. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  96. OSSL_STORE_INFO_PUBKEY, 0);
  97. }
  98. EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  99. void *u)
  100. {
  101. return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
  102. }
  103. #ifndef OPENSSL_NO_STDIO
  104. EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
  105. pem_password_cb *cb, void *u,
  106. OPENSSL_CTX *libctx, const char *propq)
  107. {
  108. BIO *b;
  109. EVP_PKEY *ret;
  110. if ((b = BIO_new(BIO_s_file())) == NULL) {
  111. PEMerr(0, ERR_R_BUF_LIB);
  112. return 0;
  113. }
  114. BIO_set_fp(b, fp, BIO_NOCLOSE);
  115. ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
  116. BIO_free(b);
  117. return ret;
  118. }
  119. EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  120. {
  121. return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
  122. }
  123. #endif
  124. EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
  125. pem_password_cb *cb, void *u,
  126. OPENSSL_CTX *libctx, const char *propq)
  127. {
  128. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  129. OSSL_STORE_INFO_PKEY, 1);
  130. }
  131. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  132. void *u)
  133. {
  134. return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
  135. }
  136. PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
  137. {
  138. IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
  139. IMPLEMENT_PEM_provided_write_body_pass();
  140. IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
  141. legacy:
  142. if (x->ameth == NULL || x->ameth->priv_encode != NULL)
  143. return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
  144. (const char *)kstr, klen, cb, u);
  145. return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
  146. }
  147. int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
  148. const EVP_CIPHER *enc,
  149. const unsigned char *kstr, int klen,
  150. pem_password_cb *cb, void *u)
  151. {
  152. char pem_str[80];
  153. if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
  154. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  155. return 0;
  156. }
  157. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  158. return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  159. pem_str, bp, x, enc, kstr, klen, cb, u);
  160. }
  161. EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
  162. OPENSSL_CTX *libctx, const char *propq)
  163. {
  164. return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
  165. OSSL_STORE_INFO_PARAMS, 0);
  166. }
  167. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  168. {
  169. return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
  170. }
  171. PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
  172. {
  173. char pem_str[80];
  174. IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
  175. IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
  176. legacy:
  177. if (!x->ameth || !x->ameth->param_encode)
  178. return 0;
  179. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  180. return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
  181. pem_str, out, x, NULL, NULL, 0, 0, NULL);
  182. }
  183. #ifndef OPENSSL_NO_STDIO
  184. EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  185. void *u, OPENSSL_CTX *libctx,
  186. const char *propq)
  187. {
  188. BIO *b;
  189. EVP_PKEY *ret;
  190. if ((b = BIO_new(BIO_s_file())) == NULL) {
  191. PEMerr(0, ERR_R_BUF_LIB);
  192. return 0;
  193. }
  194. BIO_set_fp(b, fp, BIO_NOCLOSE);
  195. ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
  196. BIO_free(b);
  197. return ret;
  198. }
  199. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  200. void *u)
  201. {
  202. return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
  203. }
  204. int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  205. const unsigned char *kstr, int klen,
  206. pem_password_cb *cb, void *u)
  207. {
  208. BIO *b;
  209. int ret;
  210. if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  211. PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
  212. return 0;
  213. }
  214. ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
  215. BIO_free(b);
  216. return ret;
  217. }
  218. #endif
  219. #ifndef OPENSSL_NO_DH
  220. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  221. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  222. {
  223. DH *ret = NULL;
  224. EVP_PKEY *pkey = NULL;
  225. OSSL_STORE_CTX *ctx = NULL;
  226. OSSL_STORE_INFO *info = NULL;
  227. UI_METHOD *ui_method = NULL;
  228. if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
  229. return NULL;
  230. if ((ctx = OSSL_STORE_attach(bp, "file", NULL, NULL, ui_method, u,
  231. NULL, NULL)) == NULL)
  232. goto err;
  233. while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
  234. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
  235. pkey = OSSL_STORE_INFO_get0_PARAMS(info);
  236. if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
  237. || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
  238. ret = EVP_PKEY_get1_DH(pkey);
  239. break;
  240. }
  241. }
  242. OSSL_STORE_INFO_free(info);
  243. info = NULL;
  244. }
  245. if (ret != NULL && x != NULL)
  246. *x = ret;
  247. err:
  248. OSSL_STORE_close(ctx);
  249. UI_destroy_method(ui_method);
  250. OSSL_STORE_INFO_free(info);
  251. return ret;
  252. }
  253. # ifndef OPENSSL_NO_STDIO
  254. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  255. {
  256. BIO *b;
  257. DH *ret;
  258. if ((b = BIO_new(BIO_s_file())) == NULL) {
  259. PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
  260. return 0;
  261. }
  262. BIO_set_fp(b, fp, BIO_NOCLOSE);
  263. ret = PEM_read_bio_DHparams(b, x, cb, u);
  264. BIO_free(b);
  265. return ret;
  266. }
  267. # endif
  268. #endif