eng_pkey.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2001-2021 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 engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "eng_local.h"
  12. /* Basic get/set stuff */
  13. int ENGINE_set_load_privkey_function(ENGINE *e,
  14. ENGINE_LOAD_KEY_PTR loadpriv_f)
  15. {
  16. e->load_privkey = loadpriv_f;
  17. return 1;
  18. }
  19. int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
  20. {
  21. e->load_pubkey = loadpub_f;
  22. return 1;
  23. }
  24. int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
  25. ENGINE_SSL_CLIENT_CERT_PTR
  26. loadssl_f)
  27. {
  28. e->load_ssl_client_cert = loadssl_f;
  29. return 1;
  30. }
  31. ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
  32. {
  33. return e->load_privkey;
  34. }
  35. ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
  36. {
  37. return e->load_pubkey;
  38. }
  39. ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE
  40. *e)
  41. {
  42. return e->load_ssl_client_cert;
  43. }
  44. /* API functions to load public/private keys */
  45. EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
  46. UI_METHOD *ui_method, void *callback_data)
  47. {
  48. EVP_PKEY *pkey;
  49. if (e == NULL) {
  50. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  51. return NULL;
  52. }
  53. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  54. return NULL;
  55. if (e->funct_ref == 0) {
  56. CRYPTO_THREAD_unlock(global_engine_lock);
  57. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
  58. return NULL;
  59. }
  60. CRYPTO_THREAD_unlock(global_engine_lock);
  61. if (!e->load_privkey) {
  62. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
  63. return NULL;
  64. }
  65. pkey = e->load_privkey(e, key_id, ui_method, callback_data);
  66. if (pkey == NULL) {
  67. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  68. return NULL;
  69. }
  70. /* We enforce check for legacy key */
  71. switch (EVP_PKEY_get_id(pkey)) {
  72. case EVP_PKEY_RSA:
  73. {
  74. RSA *rsa = EVP_PKEY_get1_RSA(pkey);
  75. EVP_PKEY_set1_RSA(pkey, rsa);
  76. RSA_free(rsa);
  77. }
  78. break;
  79. # ifndef OPENSSL_NO_EC
  80. case EVP_PKEY_SM2:
  81. case EVP_PKEY_EC:
  82. {
  83. EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey);
  84. EVP_PKEY_set1_EC_KEY(pkey, ec);
  85. EC_KEY_free(ec);
  86. }
  87. break;
  88. # endif
  89. # ifndef OPENSSL_NO_DSA
  90. case EVP_PKEY_DSA:
  91. {
  92. DSA *dsa = EVP_PKEY_get1_DSA(pkey);
  93. EVP_PKEY_set1_DSA(pkey, dsa);
  94. DSA_free(dsa);
  95. }
  96. break;
  97. #endif
  98. # ifndef OPENSSL_NO_DH
  99. case EVP_PKEY_DH:
  100. {
  101. DH *dh = EVP_PKEY_get1_DH(pkey);
  102. EVP_PKEY_set1_DH(pkey, dh);
  103. DH_free(dh);
  104. }
  105. break;
  106. #endif
  107. default:
  108. /*Do nothing */
  109. break;
  110. }
  111. return pkey;
  112. }
  113. EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
  114. UI_METHOD *ui_method, void *callback_data)
  115. {
  116. EVP_PKEY *pkey;
  117. if (e == NULL) {
  118. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  119. return NULL;
  120. }
  121. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  122. return NULL;
  123. if (e->funct_ref == 0) {
  124. CRYPTO_THREAD_unlock(global_engine_lock);
  125. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
  126. return NULL;
  127. }
  128. CRYPTO_THREAD_unlock(global_engine_lock);
  129. if (!e->load_pubkey) {
  130. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
  131. return NULL;
  132. }
  133. pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
  134. if (pkey == NULL) {
  135. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  136. return NULL;
  137. }
  138. return pkey;
  139. }
  140. int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
  141. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  142. EVP_PKEY **ppkey, STACK_OF(X509) **pother,
  143. UI_METHOD *ui_method, void *callback_data)
  144. {
  145. if (e == NULL) {
  146. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  147. return 0;
  148. }
  149. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  150. return 0;
  151. if (e->funct_ref == 0) {
  152. CRYPTO_THREAD_unlock(global_engine_lock);
  153. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
  154. return 0;
  155. }
  156. CRYPTO_THREAD_unlock(global_engine_lock);
  157. if (!e->load_ssl_client_cert) {
  158. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
  159. return 0;
  160. }
  161. return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
  162. ui_method, callback_data);
  163. }