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
15 #include <openssl/engine.h>
16
17 #include <string.h>
18 #include <assert.h>
19
20 #include <openssl/dh.h>
21 #include <openssl/dsa.h>
22 #include <openssl/ec_key.h>
23 #include <openssl/err.h>
24 #include <openssl/mem.h>
25 #include <openssl/rsa.h>
26 #include <openssl/thread.h>
27
28
29 struct engine_st {
30 DH_METHOD *dh_method;
31 DSA_METHOD *dsa_method;
32 RSA_METHOD *rsa_method;
33 ECDSA_METHOD *ecdsa_method;
34 };
35
ENGINE_new(void)36 ENGINE *ENGINE_new(void) {
37 ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
38 if (engine == NULL) {
39 return NULL;
40 }
41
42 memset(engine, 0, sizeof(ENGINE));
43 return engine;
44 }
45
ENGINE_free(ENGINE * engine)46 void ENGINE_free(ENGINE *engine) {
47 /* Methods are currently required to be static so are not unref'ed. */
48 OPENSSL_free(engine);
49 }
50
51 /* set_method takes a pointer to a method and its given size and sets
52 * |*out_member| to point to it. This function might want to be extended in the
53 * future to support making a copy of the method so that a stable ABI for
54 * ENGINEs can be supported. But, for the moment, all *_METHODS must be
55 * static. */
set_method(void ** out_member,const void * method,size_t method_size,size_t compiled_size)56 static int set_method(void **out_member, const void *method, size_t method_size,
57 size_t compiled_size) {
58 const struct openssl_method_common_st *common = method;
59 if (method_size != compiled_size || !common->is_static) {
60 return 0;
61 }
62
63 *out_member = (void*) method;
64 return 1;
65 }
66
ENGINE_set_DH_method(ENGINE * engine,const DH_METHOD * method,size_t method_size)67 int ENGINE_set_DH_method(ENGINE *engine, const DH_METHOD *method,
68 size_t method_size) {
69 return set_method((void **)&engine->dh_method, method, method_size,
70 sizeof(DH_METHOD));
71 }
72
ENGINE_get_DH_method(const ENGINE * engine)73 DH_METHOD *ENGINE_get_DH_method(const ENGINE *engine) {
74 return engine->dh_method;
75 }
76
ENGINE_set_DSA_method(ENGINE * engine,const DSA_METHOD * method,size_t method_size)77 int ENGINE_set_DSA_method(ENGINE *engine, const DSA_METHOD *method,
78 size_t method_size) {
79 return set_method((void **)&engine->dsa_method, method, method_size,
80 sizeof(DSA_METHOD));
81 }
82
ENGINE_get_DSA_method(const ENGINE * engine)83 DSA_METHOD *ENGINE_get_DSA_method(const ENGINE *engine) {
84 return engine->dsa_method;
85 }
86
ENGINE_set_RSA_method(ENGINE * engine,const RSA_METHOD * method,size_t method_size)87 int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
88 size_t method_size) {
89 return set_method((void **)&engine->rsa_method, method, method_size,
90 sizeof(RSA_METHOD));
91 }
92
ENGINE_get_RSA_method(const ENGINE * engine)93 RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
94 return engine->rsa_method;
95 }
96
ENGINE_set_ECDSA_method(ENGINE * engine,const ECDSA_METHOD * method,size_t method_size)97 int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
98 size_t method_size) {
99 return set_method((void **)&engine->ecdsa_method, method, method_size,
100 sizeof(ECDSA_METHOD));
101 }
102
ENGINE_get_ECDSA_method(const ENGINE * engine)103 ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
104 return engine->ecdsa_method;
105 }
106
METHOD_ref(void * method_in)107 void METHOD_ref(void *method_in) {
108 assert(((struct openssl_method_common_st*) method_in)->is_static);
109 }
110
METHOD_unref(void * method_in)111 void METHOD_unref(void *method_in) {
112 struct openssl_method_common_st *method = method_in;
113
114 if (method == NULL) {
115 return;
116 }
117 assert(method->is_static);
118 }
119
120 OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED);
121