You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 lines
2.9 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/engine.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <openssl/ec_key.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/rsa.h>
  21. #include <openssl/thread.h>
  22. #include "../internal.h"
  23. struct engine_st {
  24. RSA_METHOD *rsa_method;
  25. ECDSA_METHOD *ecdsa_method;
  26. };
  27. ENGINE *ENGINE_new(void) {
  28. ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
  29. if (engine == NULL) {
  30. return NULL;
  31. }
  32. OPENSSL_memset(engine, 0, sizeof(ENGINE));
  33. return engine;
  34. }
  35. void ENGINE_free(ENGINE *engine) {
  36. // Methods are currently required to be static so are not unref'ed.
  37. OPENSSL_free(engine);
  38. }
  39. // set_method takes a pointer to a method and its given size and sets
  40. // |*out_member| to point to it. This function might want to be extended in the
  41. // future to support making a copy of the method so that a stable ABI for
  42. // ENGINEs can be supported. But, for the moment, all *_METHODS must be
  43. // static.
  44. static int set_method(void **out_member, const void *method, size_t method_size,
  45. size_t compiled_size) {
  46. const struct openssl_method_common_st *common = method;
  47. if (method_size != compiled_size || !common->is_static) {
  48. return 0;
  49. }
  50. *out_member = (void*) method;
  51. return 1;
  52. }
  53. int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
  54. size_t method_size) {
  55. return set_method((void **)&engine->rsa_method, method, method_size,
  56. sizeof(RSA_METHOD));
  57. }
  58. RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
  59. return engine->rsa_method;
  60. }
  61. int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
  62. size_t method_size) {
  63. return set_method((void **)&engine->ecdsa_method, method, method_size,
  64. sizeof(ECDSA_METHOD));
  65. }
  66. ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
  67. return engine->ecdsa_method;
  68. }
  69. void METHOD_ref(void *method_in) {
  70. assert(((struct openssl_method_common_st*) method_in)->is_static);
  71. }
  72. void METHOD_unref(void *method_in) {
  73. struct openssl_method_common_st *method = method_in;
  74. if (method == NULL) {
  75. return;
  76. }
  77. assert(method->is_static);
  78. }
  79. OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED)