eng_pkey.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2001-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 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. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  51. ERR_R_PASSED_NULL_PARAMETER);
  52. return 0;
  53. }
  54. CRYPTO_THREAD_write_lock(global_engine_lock);
  55. if (e->funct_ref == 0) {
  56. CRYPTO_THREAD_unlock(global_engine_lock);
  57. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, ENGINE_R_NOT_INITIALISED);
  58. return 0;
  59. }
  60. CRYPTO_THREAD_unlock(global_engine_lock);
  61. if (!e->load_privkey) {
  62. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  63. ENGINE_R_NO_LOAD_FUNCTION);
  64. return 0;
  65. }
  66. pkey = e->load_privkey(e, key_id, ui_method, callback_data);
  67. if (pkey == NULL) {
  68. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  69. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  70. return 0;
  71. }
  72. return pkey;
  73. }
  74. EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
  75. UI_METHOD *ui_method, void *callback_data)
  76. {
  77. EVP_PKEY *pkey;
  78. if (e == NULL) {
  79. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
  80. ERR_R_PASSED_NULL_PARAMETER);
  81. return 0;
  82. }
  83. CRYPTO_THREAD_write_lock(global_engine_lock);
  84. if (e->funct_ref == 0) {
  85. CRYPTO_THREAD_unlock(global_engine_lock);
  86. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NOT_INITIALISED);
  87. return 0;
  88. }
  89. CRYPTO_THREAD_unlock(global_engine_lock);
  90. if (!e->load_pubkey) {
  91. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NO_LOAD_FUNCTION);
  92. return 0;
  93. }
  94. pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
  95. if (pkey == NULL) {
  96. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
  97. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  98. return 0;
  99. }
  100. return pkey;
  101. }
  102. int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
  103. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  104. EVP_PKEY **ppkey, STACK_OF(X509) **pother,
  105. UI_METHOD *ui_method, void *callback_data)
  106. {
  107. if (e == NULL) {
  108. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  109. ERR_R_PASSED_NULL_PARAMETER);
  110. return 0;
  111. }
  112. CRYPTO_THREAD_write_lock(global_engine_lock);
  113. if (e->funct_ref == 0) {
  114. CRYPTO_THREAD_unlock(global_engine_lock);
  115. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  116. ENGINE_R_NOT_INITIALISED);
  117. return 0;
  118. }
  119. CRYPTO_THREAD_unlock(global_engine_lock);
  120. if (!e->load_ssl_client_cert) {
  121. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  122. ENGINE_R_NO_LOAD_FUNCTION);
  123. return 0;
  124. }
  125. return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
  126. ui_method, callback_data);
  127. }