blob: 141ed230f44cc15ec449bb644e14170d93a93be3 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
Adam Langley2b2d66d2015-01-30 17:08:37 -080017#include <string.h>
Adam Langley4e04ee82015-02-03 17:20:06 -080018#include <assert.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080019
Adam Langley95c29f32014-06-20 12:00:00 -070020#include <openssl/ec_key.h>
Adam Langley7ea84812014-10-09 16:26:09 -070021#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -070022#include <openssl/mem.h>
23#include <openssl/rsa.h>
24#include <openssl/thread.h>
25
David Benjamin17cf2cb2016-12-13 01:07:13 -050026#include "../internal.h"
27
Adam Langley95c29f32014-06-20 12:00:00 -070028
29struct engine_st {
Adam Langley95c29f32014-06-20 12:00:00 -070030 RSA_METHOD *rsa_method;
31 ECDSA_METHOD *ecdsa_method;
32};
33
David Benjaminc44d2f42014-08-20 16:24:00 -040034ENGINE *ENGINE_new(void) {
Adam Langley95c29f32014-06-20 12:00:00 -070035 ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
36 if (engine == NULL) {
37 return NULL;
38 }
39
David Benjamin17cf2cb2016-12-13 01:07:13 -050040 OPENSSL_memset(engine, 0, sizeof(ENGINE));
Adam Langley95c29f32014-06-20 12:00:00 -070041 return engine;
42}
43
44void ENGINE_free(ENGINE *engine) {
Adam Langley4e04ee82015-02-03 17:20:06 -080045 /* Methods are currently required to be static so are not unref'ed. */
Adam Langley95c29f32014-06-20 12:00:00 -070046 OPENSSL_free(engine);
47}
48
49/* set_method takes a pointer to a method and its given size and sets
Adam Langley4e04ee82015-02-03 17:20:06 -080050 * |*out_member| to point to it. This function might want to be extended in the
51 * future to support making a copy of the method so that a stable ABI for
52 * ENGINEs can be supported. But, for the moment, all *_METHODS must be
53 * static. */
Adam Langley95c29f32014-06-20 12:00:00 -070054static int set_method(void **out_member, const void *method, size_t method_size,
55 size_t compiled_size) {
Adam Langley4e04ee82015-02-03 17:20:06 -080056 const struct openssl_method_common_st *common = method;
57 if (method_size != compiled_size || !common->is_static) {
Adam Langley95c29f32014-06-20 12:00:00 -070058 return 0;
59 }
60
Adam Langley4e04ee82015-02-03 17:20:06 -080061 *out_member = (void*) method;
Adam Langley95c29f32014-06-20 12:00:00 -070062 return 1;
63}
64
Adam Langley95c29f32014-06-20 12:00:00 -070065int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
66 size_t method_size) {
67 return set_method((void **)&engine->rsa_method, method, method_size,
68 sizeof(RSA_METHOD));
69}
70
71RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
72 return engine->rsa_method;
73}
74
75int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
76 size_t method_size) {
77 return set_method((void **)&engine->ecdsa_method, method, method_size,
78 sizeof(ECDSA_METHOD));
79}
80
81ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
82 return engine->ecdsa_method;
83}
84
85void METHOD_ref(void *method_in) {
Adam Langley4e04ee82015-02-03 17:20:06 -080086 assert(((struct openssl_method_common_st*) method_in)->is_static);
Adam Langley95c29f32014-06-20 12:00:00 -070087}
88
89void METHOD_unref(void *method_in) {
90 struct openssl_method_common_st *method = method_in;
91
Adam Langley4e04ee82015-02-03 17:20:06 -080092 if (method == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -070093 return;
94 }
Adam Langley4e04ee82015-02-03 17:20:06 -080095 assert(method->is_static);
Adam Langley95c29f32014-06-20 12:00:00 -070096}
Adam Langley7ea84812014-10-09 16:26:09 -070097
Alessandro Ghedini1fc7e9c2016-08-03 13:46:45 +010098OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED)