eng_pkey.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2001-2016 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. #include "eng_local.h"
  10. /* Basic get/set stuff */
  11. int ENGINE_set_load_privkey_function(ENGINE *e,
  12. ENGINE_LOAD_KEY_PTR loadpriv_f)
  13. {
  14. e->load_privkey = loadpriv_f;
  15. return 1;
  16. }
  17. int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
  18. {
  19. e->load_pubkey = loadpub_f;
  20. return 1;
  21. }
  22. int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
  23. ENGINE_SSL_CLIENT_CERT_PTR
  24. loadssl_f)
  25. {
  26. e->load_ssl_client_cert = loadssl_f;
  27. return 1;
  28. }
  29. ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
  30. {
  31. return e->load_privkey;
  32. }
  33. ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
  34. {
  35. return e->load_pubkey;
  36. }
  37. ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE
  38. *e)
  39. {
  40. return e->load_ssl_client_cert;
  41. }
  42. /* API functions to load public/private keys */
  43. EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
  44. UI_METHOD *ui_method, void *callback_data)
  45. {
  46. EVP_PKEY *pkey;
  47. if (e == NULL) {
  48. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  49. ERR_R_PASSED_NULL_PARAMETER);
  50. return 0;
  51. }
  52. CRYPTO_THREAD_write_lock(global_engine_lock);
  53. if (e->funct_ref == 0) {
  54. CRYPTO_THREAD_unlock(global_engine_lock);
  55. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, ENGINE_R_NOT_INITIALISED);
  56. return 0;
  57. }
  58. CRYPTO_THREAD_unlock(global_engine_lock);
  59. if (!e->load_privkey) {
  60. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  61. ENGINE_R_NO_LOAD_FUNCTION);
  62. return 0;
  63. }
  64. pkey = e->load_privkey(e, key_id, ui_method, callback_data);
  65. if (pkey == NULL) {
  66. ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
  67. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  68. return 0;
  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. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
  78. ERR_R_PASSED_NULL_PARAMETER);
  79. return 0;
  80. }
  81. CRYPTO_THREAD_write_lock(global_engine_lock);
  82. if (e->funct_ref == 0) {
  83. CRYPTO_THREAD_unlock(global_engine_lock);
  84. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NOT_INITIALISED);
  85. return 0;
  86. }
  87. CRYPTO_THREAD_unlock(global_engine_lock);
  88. if (!e->load_pubkey) {
  89. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NO_LOAD_FUNCTION);
  90. return 0;
  91. }
  92. pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
  93. if (pkey == NULL) {
  94. ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
  95. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  96. return 0;
  97. }
  98. return pkey;
  99. }
  100. int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
  101. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  102. EVP_PKEY **ppkey, STACK_OF(X509) **pother,
  103. UI_METHOD *ui_method, void *callback_data)
  104. {
  105. if (e == NULL) {
  106. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  107. ERR_R_PASSED_NULL_PARAMETER);
  108. return 0;
  109. }
  110. CRYPTO_THREAD_write_lock(global_engine_lock);
  111. if (e->funct_ref == 0) {
  112. CRYPTO_THREAD_unlock(global_engine_lock);
  113. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  114. ENGINE_R_NOT_INITIALISED);
  115. return 0;
  116. }
  117. CRYPTO_THREAD_unlock(global_engine_lock);
  118. if (!e->load_ssl_client_cert) {
  119. ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
  120. ENGINE_R_NO_LOAD_FUNCTION);
  121. return 0;
  122. }
  123. return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
  124. ui_method, callback_data);
  125. }