Merge branch 'vendor/GCC'
[dragonfly.git] / contrib / bind-9.3 / lib / dns / openssl_link.c
1 /*
2  * Portions Copyright (C) 2004, 2006  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2003  Internet Software Consortium.
4  * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
11  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
13  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 /*
20  * Principal Author: Brian Wellington
21  * $Id: openssl_link.c,v 1.1.4.3 2006/05/23 23:51:03 marka Exp $
22  */
23 #ifdef OPENSSL
24
25 #include <config.h>
26
27 #include <isc/entropy.h>
28 #include <isc/mem.h>
29 #include <isc/mutex.h>
30 #include <isc/mutexblock.h>
31 #include <isc/string.h>
32 #include <isc/thread.h>
33 #include <isc/util.h>
34
35 #include "dst_internal.h"
36 #include "dst_openssl.h"
37
38 #include <openssl/err.h>
39 #include <openssl/rand.h>
40 #include <openssl/crypto.h>
41
42 #if defined(CRYPTO_LOCK_ENGINE) && (OPENSSL_VERSION_NUMBER != 0x00907000L)
43 #define USE_ENGINE 1
44 #endif
45
46 #ifdef USE_ENGINE
47 #include <openssl/engine.h>
48 #endif
49
50 static RAND_METHOD *rm = NULL;
51 static isc_mutex_t *locks = NULL;
52 static int nlocks;
53
54 #ifdef USE_ENGINE
55 static ENGINE *e;
56 #endif
57
58
59 static int
60 entropy_get(unsigned char *buf, int num) {
61         isc_result_t result;
62         if (num < 0)
63                 return (-1);
64         result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
65         return (result == ISC_R_SUCCESS ? num : -1);
66 }
67
68 static int
69 entropy_getpseudo(unsigned char *buf, int num) {
70         isc_result_t result;
71         if (num < 0)
72                 return (-1);
73         result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
74         return (result == ISC_R_SUCCESS ? num : -1);
75 }
76
77 static void
78 entropy_add(const void *buf, int num, double entropy) {
79         /*
80          * Do nothing.  The only call to this provides no useful data anyway.
81          */
82         UNUSED(buf);
83         UNUSED(num);
84         UNUSED(entropy);
85 }
86
87 static void
88 lock_callback(int mode, int type, const char *file, int line) {
89         UNUSED(file);
90         UNUSED(line);
91         if ((mode & CRYPTO_LOCK) != 0)
92                 LOCK(&locks[type]);
93         else
94                 UNLOCK(&locks[type]);
95 }
96
97 static unsigned long
98 id_callback(void) {
99         return ((unsigned long)isc_thread_self());
100 }
101
102 static void *
103 mem_alloc(size_t size) {
104         INSIST(dst__memory_pool != NULL);
105         return (isc_mem_allocate(dst__memory_pool, size));
106 }
107
108 static void
109 mem_free(void *ptr) {
110         INSIST(dst__memory_pool != NULL);
111         if (ptr != NULL)
112                 isc_mem_free(dst__memory_pool, ptr);
113 }
114
115 static void *
116 mem_realloc(void *ptr, size_t size) {
117         void *p;
118
119         INSIST(dst__memory_pool != NULL);
120         p = NULL;
121         if (size > 0U) {
122                 p = mem_alloc(size);
123                 if (p != NULL && ptr != NULL)
124                         memcpy(p, ptr, size);
125         }
126         if (ptr != NULL)
127                 mem_free(ptr);
128         return (p);
129 }
130
131 isc_result_t
132 dst__openssl_init() {
133         isc_result_t result;
134
135         CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
136         nlocks = CRYPTO_num_locks();
137         locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
138         if (locks == NULL)
139                 return (ISC_R_NOMEMORY);
140         result = isc_mutexblock_init(locks, nlocks);
141         if (result != ISC_R_SUCCESS)
142                 goto cleanup_mutexalloc;
143         CRYPTO_set_locking_callback(lock_callback);
144         CRYPTO_set_id_callback(id_callback);
145         rm = mem_alloc(sizeof(RAND_METHOD));
146         if (rm == NULL) {
147                 result = ISC_R_NOMEMORY;
148                 goto cleanup_mutexinit;
149         }
150         rm->seed = NULL;
151         rm->bytes = entropy_get;
152         rm->cleanup = NULL;
153         rm->add = entropy_add;
154         rm->pseudorand = entropy_getpseudo;
155         rm->status = NULL;
156 #ifdef USE_ENGINE
157         e = ENGINE_new();
158         if (e == NULL) {
159                 result = ISC_R_NOMEMORY;
160                 goto cleanup_rm;
161         }
162         ENGINE_set_RAND(e, rm);
163         RAND_set_rand_method(rm);
164 #else
165         RAND_set_rand_method(rm);
166 #endif
167         return (ISC_R_SUCCESS);
168
169 #ifdef USE_ENGINE
170  cleanup_rm:
171         mem_free(rm);
172 #endif
173  cleanup_mutexinit:
174         DESTROYMUTEXBLOCK(locks, nlocks);
175  cleanup_mutexalloc:
176         mem_free(locks);
177         return (result);
178 }
179
180 void
181 dst__openssl_destroy() {
182         ERR_clear_error();
183 #ifdef USE_ENGINE
184         if (e != NULL) {
185                 ENGINE_free(e);
186                 e = NULL;
187         }
188 #endif
189         if (locks != NULL) {
190                 DESTROYMUTEXBLOCK(locks, nlocks);
191                 mem_free(locks);
192         }
193         if (rm != NULL)
194                 mem_free(rm);
195 }
196
197 isc_result_t
198 dst__openssl_toresult(isc_result_t fallback) {
199         isc_result_t result = fallback;
200         int err = ERR_get_error();
201
202         switch (ERR_GET_REASON(err)) {
203         case ERR_R_MALLOC_FAILURE:
204                 result = ISC_R_NOMEMORY;
205                 break;
206         default:
207                 break;
208         }
209         ERR_clear_error();
210         return (result);
211 }
212
213 #else /* OPENSSL */
214
215 #include <isc/util.h>
216
217 EMPTY_TRANSLATION_UNIT
218
219 #endif /* OPENSSL */