1 /* Copyright 2014 The BoringSSL Authors
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 <assert.h>
18 #include <string.h>
19
20 #include <openssl/ec_key.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 #include <openssl/rsa.h>
24 #include <openssl/thread.h>
25
26 #include "../internal.h"
27
28
29 struct engine_st {
30 RSA_METHOD *rsa_method;
31 ECDSA_METHOD *ecdsa_method;
32 };
33
ENGINE_new(void)34 ENGINE *ENGINE_new(void) {
35 return reinterpret_cast<ENGINE *>(OPENSSL_zalloc(sizeof(ENGINE)));
36 }
37
ENGINE_free(ENGINE * engine)38 int ENGINE_free(ENGINE *engine) {
39 // Methods are currently required to be static so are not unref'ed.
40 OPENSSL_free(engine);
41 return 1;
42 }
43
44 // set_method takes a pointer to a method and its given size and sets
45 // |*out_member| to point to it. This function might want to be extended in the
46 // future to support making a copy of the method so that a stable ABI for
47 // ENGINEs can be supported. But, for the moment, all *_METHODS must be
48 // static.
set_method(void ** out_member,const void * method,size_t method_size,size_t compiled_size)49 static int set_method(void **out_member, const void *method, size_t method_size,
50 size_t compiled_size) {
51 const struct openssl_method_common_st *common =
52 reinterpret_cast<const openssl_method_common_st *>(method);
53 if (method_size != compiled_size || !common->is_static) {
54 return 0;
55 }
56
57 *out_member = (void *)method;
58 return 1;
59 }
60
ENGINE_set_RSA_method(ENGINE * engine,const RSA_METHOD * method,size_t method_size)61 int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
62 size_t method_size) {
63 return set_method((void **)&engine->rsa_method, method, method_size,
64 sizeof(RSA_METHOD));
65 }
66
ENGINE_get_RSA_method(const ENGINE * engine)67 RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
68 return engine->rsa_method;
69 }
70
ENGINE_set_ECDSA_method(ENGINE * engine,const ECDSA_METHOD * method,size_t method_size)71 int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
72 size_t method_size) {
73 return set_method((void **)&engine->ecdsa_method, method, method_size,
74 sizeof(ECDSA_METHOD));
75 }
76
ENGINE_get_ECDSA_method(const ENGINE * engine)77 ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
78 return engine->ecdsa_method;
79 }
80
METHOD_ref(void * method_in)81 void METHOD_ref(void *method_in) {
82 assert(((struct openssl_method_common_st *)method_in)->is_static);
83 }
84
METHOD_unref(void * method_in)85 void METHOD_unref(void *method_in) {
86 struct openssl_method_common_st *method =
87 reinterpret_cast<openssl_method_common_st *>(method_in);
88
89 if (method == NULL) {
90 return;
91 }
92 assert(method->is_static);
93 }
94
95 OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED)
96