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