2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2001, 2003 Internet Software Consortium.
4 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
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.
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.
20 * Principal Author: Brian Wellington
21 * $Id: openssl_link.c,v 1.46.2.3 2004/03/09 06:11:41 marka Exp $
27 #include <isc/entropy.h>
29 #include <isc/mutex.h>
30 #include <isc/mutexblock.h>
31 #include <isc/string.h>
32 #include <isc/thread.h>
35 #include "dst_internal.h"
37 #include <openssl/rand.h>
38 #include <openssl/crypto.h>
40 #if defined(CRYPTO_LOCK_ENGINE) && (OPENSSL_VERSION_NUMBER < 0x00907000L)
45 #include <openssl/engine.h>
48 static RAND_METHOD
*rm
= NULL
;
49 static isc_mutex_t
*locks
= NULL
;
58 entropy_get(unsigned char *buf
, int num
) {
62 result
= dst__entropy_getdata(buf
, (unsigned int) num
, ISC_FALSE
);
63 return (result
== ISC_R_SUCCESS
? num
: -1);
67 entropy_getpseudo(unsigned char *buf
, int num
) {
71 result
= dst__entropy_getdata(buf
, (unsigned int) num
, ISC_TRUE
);
72 return (result
== ISC_R_SUCCESS
? num
: -1);
76 entropy_add(const void *buf
, int num
, double entropy
) {
78 * Do nothing. The only call to this provides no useful data anyway.
86 lock_callback(int mode
, int type
, const char *file
, int line
) {
89 if ((mode
& CRYPTO_LOCK
) != 0)
97 return ((unsigned long)isc_thread_self());
101 dst__openssl_init() {
104 CRYPTO_set_mem_functions(dst__mem_alloc
, dst__mem_realloc
,
106 nlocks
= CRYPTO_num_locks();
107 locks
= dst__mem_alloc(sizeof(isc_mutex_t
) * nlocks
);
109 return (ISC_R_NOMEMORY
);
110 result
= isc_mutexblock_init(locks
, nlocks
);
111 if (result
!= ISC_R_SUCCESS
)
112 goto cleanup_mutexalloc
;
113 CRYPTO_set_locking_callback(lock_callback
);
114 CRYPTO_set_id_callback(id_callback
);
115 rm
= dst__mem_alloc(sizeof(RAND_METHOD
));
117 result
= ISC_R_NOMEMORY
;
118 goto cleanup_mutexinit
;
121 rm
->bytes
= entropy_get
;
123 rm
->add
= entropy_add
;
124 rm
->pseudorand
= entropy_getpseudo
;
129 result
= ISC_R_NOMEMORY
;
132 ENGINE_set_RAND(e
, rm
);
133 RAND_set_rand_method(e
);
135 RAND_set_rand_method(rm
);
137 return (ISC_R_SUCCESS
);
144 RUNTIME_CHECK(isc_mutexblock_destroy(locks
, nlocks
) == ISC_R_SUCCESS
);
146 dst__mem_free(locks
);
151 dst__openssl_destroy() {
159 RUNTIME_CHECK(isc_mutexblock_destroy(locks
, nlocks
) ==
161 dst__mem_free(locks
);