選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

134 行
3.7 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 <openssl/dh.h>
  16. #include <openssl/dsa.h>
  17. #include <openssl/ec_key.h>
  18. #include <openssl/mem.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/thread.h>
  21. struct engine_st {
  22. DH_METHOD *dh_method;
  23. DSA_METHOD *dsa_method;
  24. RSA_METHOD *rsa_method;
  25. ECDSA_METHOD *ecdsa_method;
  26. };
  27. ENGINE *ENGINE_new() {
  28. ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
  29. if (engine == NULL) {
  30. return NULL;
  31. }
  32. memset(engine, 0, sizeof(ENGINE));
  33. return engine;
  34. }
  35. void ENGINE_free(ENGINE *engine) {
  36. if (engine->dh_method != NULL) {
  37. METHOD_unref(engine->dh_method);
  38. }
  39. OPENSSL_free(engine);
  40. }
  41. /* set_method takes a pointer to a method and its given size and sets
  42. * |*out_member| to point to a copy of it. The copy is |compiled_size| bytes
  43. * long and has zero padding if needed. */
  44. static int set_method(void **out_member, const void *method, size_t method_size,
  45. size_t compiled_size) {
  46. void *copy = OPENSSL_malloc(compiled_size);
  47. if (copy == NULL) {
  48. return 0;
  49. }
  50. memset(copy, 0, compiled_size);
  51. if (method_size > compiled_size) {
  52. method_size = compiled_size;
  53. }
  54. memcpy(copy, method, method_size);
  55. METHOD_unref(*out_member);
  56. *out_member = copy;
  57. return 1;
  58. }
  59. int ENGINE_set_DH_method(ENGINE *engine, const DH_METHOD *method,
  60. size_t method_size) {
  61. return set_method((void **)&engine->dh_method, method, method_size,
  62. sizeof(DH_METHOD));
  63. }
  64. DH_METHOD *ENGINE_get_DH_method(const ENGINE *engine) {
  65. return engine->dh_method;
  66. }
  67. int ENGINE_set_DSA_method(ENGINE *engine, const DSA_METHOD *method,
  68. size_t method_size) {
  69. return set_method((void **)&engine->dsa_method, method, method_size,
  70. sizeof(DSA_METHOD));
  71. }
  72. DSA_METHOD *ENGINE_get_DSA_method(const ENGINE *engine) {
  73. return engine->dsa_method;
  74. }
  75. int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
  76. size_t method_size) {
  77. return set_method((void **)&engine->rsa_method, method, method_size,
  78. sizeof(RSA_METHOD));
  79. }
  80. RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
  81. return engine->rsa_method;
  82. }
  83. int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
  84. size_t method_size) {
  85. return set_method((void **)&engine->ecdsa_method, method, method_size,
  86. sizeof(ECDSA_METHOD));
  87. }
  88. ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
  89. return engine->ecdsa_method;
  90. }
  91. void METHOD_ref(void *method_in) {
  92. struct openssl_method_common_st *method = method_in;
  93. if (method->is_static) {
  94. return;
  95. }
  96. CRYPTO_add(&method->references, 1, CRYPTO_LOCK_ENGINE);
  97. }
  98. void METHOD_unref(void *method_in) {
  99. struct openssl_method_common_st *method = method_in;
  100. if (method == NULL || method->is_static) {
  101. return;
  102. }
  103. if (CRYPTO_add(&method->references, -1, CRYPTO_LOCK_ENGINE) == 0) {
  104. OPENSSL_free(method);
  105. }
  106. }