eng_pkey.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2001-2024 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. return pkey;
  71. }
  72. EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
  73. UI_METHOD *ui_method, void *callback_data)
  74. {
  75. EVP_PKEY *pkey;
  76. if (e == NULL) {
  77. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  78. return NULL;
  79. }
  80. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  81. return NULL;
  82. if (e->funct_ref == 0) {
  83. CRYPTO_THREAD_unlock(global_engine_lock);
  84. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
  85. return NULL;
  86. }
  87. CRYPTO_THREAD_unlock(global_engine_lock);
  88. if (!e->load_pubkey) {
  89. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
  90. return NULL;
  91. }
  92. pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
  93. if (pkey == NULL) {
  94. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  95. return NULL;
  96. }
  97. return pkey;
  98. }
  99. int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
  100. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  101. EVP_PKEY **ppkey, STACK_OF(X509) **pother,
  102. UI_METHOD *ui_method, void *callback_data)
  103. {
  104. if (e == NULL) {
  105. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  106. return 0;
  107. }
  108. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  109. return 0;
  110. if (e->funct_ref == 0) {
  111. CRYPTO_THREAD_unlock(global_engine_lock);
  112. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
  113. return 0;
  114. }
  115. CRYPTO_THREAD_unlock(global_engine_lock);
  116. if (!e->load_ssl_client_cert) {
  117. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
  118. return 0;
  119. }
  120. return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
  121. ui_method, callback_data);
  122. }