Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / engine / engine.c
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 <openssl/dh.h>
18 #include <openssl/dsa.h>
19 #include <openssl/ec_key.h>
20 #include <openssl/err.h>
21 #include <openssl/mem.h>
22 #include <openssl/rsa.h>
23 #include <openssl/thread.h>
24
25
26 struct engine_st {
27   DH_METHOD *dh_method;
28   DSA_METHOD *dsa_method;
29   RSA_METHOD *rsa_method;
30   ECDSA_METHOD *ecdsa_method;
31 };
32
33 ENGINE *ENGINE_new(void) {
34   ENGINE *engine = OPENSSL_malloc(sizeof(ENGINE));
35   if (engine == NULL) {
36     return NULL;
37   }
38
39   memset(engine, 0, sizeof(ENGINE));
40   return engine;
41 }
42
43 void ENGINE_free(ENGINE *engine) {
44   if (engine->dh_method != NULL) {
45     METHOD_unref(engine->dh_method);
46   }
47
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 a copy of it. The copy is |compiled_size| bytes
53  * long and has zero padding if needed. */
54 static int set_method(void **out_member, const void *method, size_t method_size,
55                       size_t compiled_size) {
56   void *copy = OPENSSL_malloc(compiled_size);
57   if (copy == NULL) {
58     return 0;
59   }
60
61   memset(copy, 0, compiled_size);
62
63   if (method_size > compiled_size) {
64     method_size = compiled_size;
65   }
66   memcpy(copy, method, method_size);
67
68   METHOD_unref(*out_member);
69   *out_member = copy;
70
71   return 1;
72 }
73
74 int ENGINE_set_DH_method(ENGINE *engine, const DH_METHOD *method,
75                          size_t method_size) {
76   return set_method((void **)&engine->dh_method, method, method_size,
77                     sizeof(DH_METHOD));
78 }
79
80 DH_METHOD *ENGINE_get_DH_method(const ENGINE *engine) {
81   return engine->dh_method;
82 }
83
84 int ENGINE_set_DSA_method(ENGINE *engine, const DSA_METHOD *method,
85                          size_t method_size) {
86   return set_method((void **)&engine->dsa_method, method, method_size,
87                     sizeof(DSA_METHOD));
88 }
89
90 DSA_METHOD *ENGINE_get_DSA_method(const ENGINE *engine) {
91   return engine->dsa_method;
92 }
93
94 int ENGINE_set_RSA_method(ENGINE *engine, const RSA_METHOD *method,
95                          size_t method_size) {
96   return set_method((void **)&engine->rsa_method, method, method_size,
97                     sizeof(RSA_METHOD));
98 }
99
100 RSA_METHOD *ENGINE_get_RSA_method(const ENGINE *engine) {
101   return engine->rsa_method;
102 }
103
104 int ENGINE_set_ECDSA_method(ENGINE *engine, const ECDSA_METHOD *method,
105                             size_t method_size) {
106   return set_method((void **)&engine->ecdsa_method, method, method_size,
107                     sizeof(ECDSA_METHOD));
108 }
109
110 ECDSA_METHOD *ENGINE_get_ECDSA_method(const ENGINE *engine) {
111   return engine->ecdsa_method;
112 }
113
114 void METHOD_ref(void *method_in) {
115   struct openssl_method_common_st *method = method_in;
116
117   if (method->is_static) {
118     return;
119   }
120
121   CRYPTO_add(&method->references, 1, CRYPTO_LOCK_ENGINE);
122 }
123
124 void METHOD_unref(void *method_in) {
125   struct openssl_method_common_st *method = method_in;
126
127   if (method == NULL || method->is_static) {
128     return;
129   }
130
131   if (CRYPTO_add(&method->references, -1, CRYPTO_LOCK_ENGINE) == 0) {
132     OPENSSL_free(method);
133   }
134 }
135
136 OPENSSL_DECLARE_ERROR_REASON(ENGINE, OPERATION_NOT_SUPPORTED);