1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include <openssl/engine.h>
17 #include <openssl/dh.h>
18 #include <openssl/dsa.h>
19 #include <openssl/ec_key.h>
20 #include <openssl/err.h>
21 #include <openssl/mem.h>
22 #include <openssl/rsa.h>
23 #include <openssl/thread.h>
28 DSA_METHOD *dsa_method;
29 RSA_METHOD *rsa_method;
30 ECDSA_METHOD *ecdsa_method;
33 ENGINE *ENGINE_new(void) {
34 ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
39 memset(engine, 0, sizeof(ENGINE));
43 void ENGINE_free(ENGINE *engine) {
44 if (engine->dh_method != NULL) {
45 METHOD_unref(engine->dh_method);
51 /* set_method takes a pointer to a method and its given size and sets
52 * |*out_member| to point to a copy of it. The copy is |compiled_size| bytes
53 * long and has zero padding if needed. */
54 static int set_method(void **out_member, const void *method, size_t method_size,
55 size_t compiled_size) {
56 void *copy = OPENSSL_malloc(compiled_size);
61 memset(copy, 0, compiled_size);
63 if (method_size > compiled_size) {
64 method_size = compiled_size;
66 memcpy(copy, method, method_size);
68 METHOD_unref(*out_member);
74 int ENGINE_set_DH_method(ENGINE *engine, const DH_METHOD *method,
76 return set_method((void **)&engine->dh_method, method, method_size,
80 DH_METHOD *ENGINE_get_DH_method(const ENGINE *engine) {
81 return engine->dh_method;
84 int ENGINE_set_DSA_method(ENGINE *engine, const DSA_METHOD *method,
86 return set_method((void **)&engine->dsa_method, method, method_size,
90 DSA_METHOD *ENGINE_get_DSA_method(const ENGINE *engine) {
91 return engine->dsa_method;
94 int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
96 return set_method((void **)&engine->rsa_method, method, method_size,
100 RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
101 return engine->rsa_method;
104 int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
105 size_t method_size) {
106 return set_method((void **)&engine->ecdsa_method, method, method_size,
107 sizeof(ECDSA_METHOD));
110 ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
111 return engine->ecdsa_method;
114 void METHOD_ref(void *method_in) {
115 struct openssl_method_common_st *method = method_in;
117 if (method->is_static) {
121 CRYPTO_add(&method->references, 1, CRYPTO_LOCK_ENGINE);
124 void METHOD_unref(void *method_in) {
125 struct openssl_method_common_st *method = method_in;
127 if (method == NULL || method->is_static) {
131 if (CRYPTO_add(&method->references, -1, CRYPTO_LOCK_ENGINE) == 0) {
132 OPENSSL_free(method);
136 OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED);