pem_pkey.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. OSSL_LIB_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 (cb == NULL)
  46. cb = PEM_def_callback;
  47. ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
  48. if (ui_method == NULL)
  49. return NULL;
  50. if ((ctx = OSSL_STORE_attach(bp, "file", libctx, propq, ui_method, u,
  51. NULL, NULL)) == NULL)
  52. goto err;
  53. #ifndef OPENSSL_NO_SECURE_HEAP
  54. # ifndef OPENSSL_NO_DEPRECATED_3_0
  55. if (try_secure) {
  56. int on = 1;
  57. if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
  58. goto err;
  59. }
  60. # endif
  61. #endif
  62. if (!OSSL_STORE_expect(ctx, expected_store_info_type))
  63. goto err;
  64. while (!OSSL_STORE_eof(ctx)
  65. && (info = OSSL_STORE_load(ctx)) != NULL) {
  66. if (OSSL_STORE_INFO_get_type(info) == expected_store_info_type) {
  67. switch (expected_store_info_type) {
  68. case OSSL_STORE_INFO_PKEY:
  69. ret = OSSL_STORE_INFO_get1_PKEY(info);
  70. break;
  71. case OSSL_STORE_INFO_PUBKEY:
  72. ret = OSSL_STORE_INFO_get1_PUBKEY(info);
  73. break;
  74. case OSSL_STORE_INFO_PARAMS:
  75. ret = OSSL_STORE_INFO_get1_PARAMS(info);
  76. break;
  77. }
  78. }
  79. OSSL_STORE_INFO_free(info);
  80. info = NULL;
  81. }
  82. if (ret != NULL && x != NULL)
  83. *x = ret;
  84. err:
  85. OSSL_STORE_close(ctx);
  86. UI_destroy_method(allocated_ui_method);
  87. OSSL_STORE_INFO_free(info);
  88. return ret;
  89. }
  90. EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
  91. pem_password_cb *cb, void *u,
  92. OSSL_LIB_CTX *libctx, const char *propq)
  93. {
  94. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  95. OSSL_STORE_INFO_PUBKEY, 0);
  96. }
  97. EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  98. void *u)
  99. {
  100. return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
  101. }
  102. #ifndef OPENSSL_NO_STDIO
  103. EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
  104. pem_password_cb *cb, void *u,
  105. OSSL_LIB_CTX *libctx, const char *propq)
  106. {
  107. BIO *b;
  108. EVP_PKEY *ret;
  109. if ((b = BIO_new(BIO_s_file())) == NULL) {
  110. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  111. return 0;
  112. }
  113. BIO_set_fp(b, fp, BIO_NOCLOSE);
  114. ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
  115. BIO_free(b);
  116. return ret;
  117. }
  118. EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
  119. {
  120. return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
  121. }
  122. #endif
  123. EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
  124. pem_password_cb *cb, void *u,
  125. OSSL_LIB_CTX *libctx, const char *propq)
  126. {
  127. return pem_read_bio_key(bp, x, cb, u, libctx, propq,
  128. OSSL_STORE_INFO_PKEY, 1);
  129. }
  130. EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  131. void *u)
  132. {
  133. return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
  134. }
  135. PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
  136. {
  137. IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey, propq);
  138. IMPLEMENT_PEM_provided_write_body_pass();
  139. IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
  140. legacy:
  141. if (x->ameth == NULL || x->ameth->priv_encode != NULL)
  142. return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
  143. (const char *)kstr, klen, cb, u);
  144. return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
  145. }
  146. PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
  147. {
  148. return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u,
  149. NULL, NULL);
  150. }
  151. /*
  152. * Note: there is no way to tell a provided pkey encoder to use "traditional"
  153. * encoding. Therefore, if the pkey is provided, we try to take a copy
  154. * TODO: when #legacy keys are gone, this function will not be possible any
  155. * more and should be removed.
  156. */
  157. int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
  158. const EVP_CIPHER *enc,
  159. const unsigned char *kstr, int klen,
  160. pem_password_cb *cb, void *u)
  161. {
  162. char pem_str[80];
  163. EVP_PKEY *copy = NULL;
  164. int ret;
  165. if (evp_pkey_is_assigned(x)
  166. && evp_pkey_is_provided(x)
  167. && evp_pkey_copy_downgraded(&copy, x))
  168. x = copy;
  169. if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
  170. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  171. return 0;
  172. }
  173. BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
  174. ret = PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
  175. pem_str, bp, x, enc, kstr, klen, cb, u);
  176. EVP_PKEY_free(copy);
  177. return ret;
  178. }
  179. EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
  180. OSSL_LIB_CTX *libctx, const char *propq)
  181. {
  182. return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
  183. OSSL_STORE_INFO_PARAMS, 0);
  184. }
  185. EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
  186. {
  187. return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
  188. }
  189. PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
  190. {
  191. char pem_str[80];
  192. IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters, NULL);
  193. IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
  194. legacy:
  195. if (!x->ameth || !x->ameth->param_encode)
  196. return 0;
  197. BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
  198. return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
  199. pem_str, out, x, NULL, NULL, 0, 0, NULL);
  200. }
  201. #ifndef OPENSSL_NO_STDIO
  202. EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  203. void *u, OSSL_LIB_CTX *libctx,
  204. const char *propq)
  205. {
  206. BIO *b;
  207. EVP_PKEY *ret;
  208. if ((b = BIO_new(BIO_s_file())) == NULL) {
  209. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  210. return 0;
  211. }
  212. BIO_set_fp(b, fp, BIO_NOCLOSE);
  213. ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
  214. BIO_free(b);
  215. return ret;
  216. }
  217. EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  218. void *u)
  219. {
  220. return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
  221. }
  222. PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write)
  223. {
  224. BIO *b;
  225. int ret;
  226. if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  227. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  228. return 0;
  229. }
  230. ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u,
  231. libctx, propq);
  232. BIO_free(b);
  233. return ret;
  234. }
  235. PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write)
  236. {
  237. return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL);
  238. }
  239. #endif