Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 17 | #include <string.h> |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 18 | #include <assert.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 19 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 20 | #include <openssl/ec_key.h> |
Adam Langley | 7ea8481 | 2014-10-09 16:26:09 -0700 | [diff] [blame] | 21 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/rsa.h> |
| 24 | #include <openssl/thread.h> |
| 25 | |
| 26 | |
| 27 | struct engine_st { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 28 | RSA_METHOD *rsa_method; |
| 29 | ECDSA_METHOD *ecdsa_method; |
| 30 | }; |
| 31 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 32 | ENGINE *ENGINE_new(void) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 33 | ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE)); |
| 34 | if (engine == NULL) { |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | memset(engine, 0, sizeof(ENGINE)); |
| 39 | return engine; |
| 40 | } |
| 41 | |
| 42 | void ENGINE_free(ENGINE *engine) { |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 43 | /* Methods are currently required to be static so are not unref'ed. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 44 | OPENSSL_free(engine); |
| 45 | } |
| 46 | |
| 47 | /* set_method takes a pointer to a method and its given size and sets |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 48 | * |*out_member| to point to it. This function might want to be extended in the |
| 49 | * future to support making a copy of the method so that a stable ABI for |
| 50 | * ENGINEs can be supported. But, for the moment, all *_METHODS must be |
| 51 | * static. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 52 | static int set_method(void **out_member, const void *method, size_t method_size, |
| 53 | size_t compiled_size) { |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 54 | const struct openssl_method_common_st *common = method; |
| 55 | if (method_size != compiled_size || !common->is_static) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 59 | *out_member = (void*) method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 63 | int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method, |
| 64 | size_t method_size) { |
| 65 | return set_method((void **)&engine->rsa_method, method, method_size, |
| 66 | sizeof(RSA_METHOD)); |
| 67 | } |
| 68 | |
| 69 | RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) { |
| 70 | return engine->rsa_method; |
| 71 | } |
| 72 | |
| 73 | int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method, |
| 74 | size_t method_size) { |
| 75 | return set_method((void **)&engine->ecdsa_method, method, method_size, |
| 76 | sizeof(ECDSA_METHOD)); |
| 77 | } |
| 78 | |
| 79 | ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) { |
| 80 | return engine->ecdsa_method; |
| 81 | } |
| 82 | |
| 83 | void METHOD_ref(void *method_in) { |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 84 | assert(((struct openssl_method_common_st*) method_in)->is_static); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void METHOD_unref(void *method_in) { |
| 88 | struct openssl_method_common_st *method = method_in; |
| 89 | |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 90 | if (method == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | return; |
| 92 | } |
Adam Langley | 4e04ee8 | 2015-02-03 17:20:06 -0800 | [diff] [blame] | 93 | assert(method->is_static); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | } |
Adam Langley | 7ea8481 | 2014-10-09 16:26:09 -0700 | [diff] [blame] | 95 | |
Alessandro Ghedini | 1fc7e9c | 2016-08-03 13:46:45 +0100 | [diff] [blame^] | 96 | OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED) |