2 * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
60 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
61 * Use is subject to license terms.
65 * Thread setup portions of this code derived from
66 * OpenSSL 0.9.x file mt/mttest.c examples
77 #include <openssl/lhash.h>
78 #include <openssl/crypto.h>
79 #include <openssl/ssl.h>
80 #include <openssl/err.h>
81 #include "ipsec_util.h"
83 /* OpenSSL function pointers */
84 static X509_NAME
*(*d2i_X509_NAME_fn
)() = NULL
;
85 static int (*X509_NAME_print_ex_fp_fn
)() = NULL
;
86 static char *(*ERR_get_error_fn
)() = NULL
;
87 static char *(*ERR_error_string_fn
)() = NULL
;
88 static void (*SSL_load_error_strings_fn
)() = NULL
;
89 static void (*ERR_free_strings_fn
)() = NULL
;
90 static void (*CRYPTO_set_locking_callback_fn
)() = NULL
;
91 static void (*CRYPTO_set_id_callback_fn
)() = NULL
;
92 static void (*X509_NAME_free_fn
)() = NULL
;
93 static int (*CRYPTO_num_locks_fn
)() = NULL
;
94 static void *(*OPENSSL_malloc_fn
)() = NULL
;
95 static void (*OPENSSL_free_fn
)() = NULL
;
97 static void solaris_locking_callback(int, int, char *, int);
98 static unsigned long solaris_thread_id(void);
99 static boolean_t
thread_setup(void);
100 /* LINTED E_STATIC_UNUSED */
101 static void thread_cleanup(void);
103 mutex_t init_lock
= DEFAULTMUTEX
;
104 static mutex_t
*lock_cs
;
105 static long *lock_count
;
107 static boolean_t libssl_loaded
= B_FALSE
;
108 static boolean_t libcrypto_loaded
= B_FALSE
;
115 (void) mutex_lock(&init_lock
);
117 (void) mutex_unlock(&init_lock
);
121 dldesc
= dlopen(LIBSSL
, RTLD_LAZY
);
122 if (dldesc
!= NULL
) {
123 d2i_X509_NAME_fn
= (X509_NAME
*(*)())dlsym(dldesc
,
125 if (d2i_X509_NAME_fn
== NULL
)
128 X509_NAME_print_ex_fp_fn
= (int(*)())dlsym(dldesc
,
129 "X509_NAME_print_ex_fp");
130 if (X509_NAME_print_ex_fp_fn
== NULL
)
133 ERR_get_error_fn
= (char *(*)())dlsym(dldesc
,
135 if (ERR_get_error_fn
== NULL
)
138 ERR_error_string_fn
= (char *(*)())dlsym(dldesc
,
140 if (ERR_error_string_fn
== NULL
)
143 SSL_load_error_strings_fn
= (void(*)())dlsym(dldesc
,
144 "SSL_load_error_strings");
145 if (SSL_load_error_strings_fn
== NULL
)
148 ERR_free_strings_fn
= (void(*)())dlsym(dldesc
,
150 if (ERR_free_strings_fn
== NULL
)
153 CRYPTO_set_locking_callback_fn
= (void(*)())dlsym(dldesc
,
154 "CRYPTO_set_locking_callback");
155 if (CRYPTO_set_locking_callback_fn
== NULL
)
158 CRYPTO_set_id_callback_fn
= (void(*)())dlsym(dldesc
,
159 "CRYPTO_set_id_callback");
160 if (CRYPTO_set_id_callback_fn
== NULL
)
163 X509_NAME_free_fn
= (void(*)())dlsym(dldesc
,
165 if (X509_NAME_free_fn
== NULL
)
168 if (thread_setup() == B_FALSE
)
171 libssl_loaded
= B_TRUE
;
173 (void) mutex_unlock(&init_lock
);
176 (void) dlclose(dldesc
);
177 (void) mutex_unlock(&init_lock
);
185 (void) mutex_lock(&init_lock
);
186 if (libcrypto_loaded
) {
187 (void) mutex_unlock(&init_lock
);
191 dldesc
= dlopen(LIBCRYPTO
, RTLD_LAZY
);
192 if (dldesc
!= NULL
) {
193 CRYPTO_num_locks_fn
= (int(*)())dlsym(dldesc
,
195 if (CRYPTO_num_locks_fn
== NULL
)
199 * OPENSSL_free is really a macro, so we
200 * need to reference the actual symbol,
201 * which is CRYPTO_free.
203 OPENSSL_free_fn
= (void(*)())dlsym(dldesc
,
205 if (OPENSSL_free_fn
== NULL
)
209 * OPENSSL_malloc is really a macro, so we
210 * need to reference the actual symbol,
211 * which is CRYPTO_malloc.
213 OPENSSL_malloc_fn
= (void *(*)())dlsym(dldesc
,
215 if (OPENSSL_malloc_fn
== NULL
)
218 libcrypto_loaded
= B_TRUE
;
220 (void) mutex_unlock(&init_lock
);
223 (void) dlclose(dldesc
);
224 (void) mutex_unlock(&init_lock
);
232 if ((lock_cs
= OPENSSL_malloc_fn(CRYPTO_num_locks_fn() *
233 sizeof (mutex_t
))) == NULL
)
235 if ((lock_count
= OPENSSL_malloc_fn(CRYPTO_num_locks_fn() *
236 sizeof (long))) == NULL
) {
237 OPENSSL_free_fn(lock_cs
);
241 for (i
= 0; i
< CRYPTO_num_locks_fn(); i
++) {
243 (void) mutex_init(&(lock_cs
[i
]), USYNC_THREAD
, NULL
);
246 CRYPTO_set_id_callback_fn((unsigned long (*)())solaris_thread_id
);
247 CRYPTO_set_locking_callback_fn((void (*)())solaris_locking_callback
);
256 (void) mutex_lock(&init_lock
);
257 CRYPTO_set_locking_callback_fn(NULL
);
258 CRYPTO_set_id_callback_fn(NULL
);
259 for (i
= 0; i
< CRYPTO_num_locks_fn(); i
++)
260 (void) mutex_destroy(&(lock_cs
[i
]));
261 OPENSSL_free_fn(lock_cs
);
262 OPENSSL_free_fn(lock_count
);
263 (void) mutex_unlock(&init_lock
);
268 solaris_locking_callback(int mode
, int type
, char *file
, int line
)
270 if (mode
& CRYPTO_LOCK
) {
271 (void) mutex_lock(&(lock_cs
[type
]));
274 (void) mutex_unlock(&(lock_cs
[type
]));
279 solaris_thread_id(void)
283 ret
= (unsigned long)thr_self();
288 print_asn1_name(FILE *file
, const unsigned char *buf
, long buflen
)
291 if (libcrypto_loaded
)
294 if (libssl_loaded
&& libcrypto_loaded
) {
295 X509_NAME
*x509name
= NULL
;
296 const unsigned char *p
;
298 /* Make an effort to decode the ASN1 encoded name */
299 SSL_load_error_strings_fn();
302 * Temporary variable is mandatory per d2i_X509(3). Upcoming
303 * call to d2i_X509_NAME_fn() will change the 'p' pointer.
307 x509name
= d2i_X509_NAME_fn(NULL
, &p
, buflen
);
308 if (x509name
!= NULL
) {
309 (void) X509_NAME_print_ex_fp_fn(file
, x509name
, 0,
310 (ASN1_STRFLGS_RFC2253
| ASN1_STRFLGS_ESC_QUOTE
|
311 XN_FLAG_SEP_CPLUS_SPC
| XN_FLAG_FN_SN
));
312 X509_NAME_free_fn(x509name
);
313 (void) fprintf(file
, "\n");
317 (void) fprintf(file
, "\n# %s\n",
318 ERR_error_string_fn(ERR_get_error_fn(), errbuf
));
319 (void) fprintf(file
, dgettext(TEXT_DOMAIN
,
320 "<cannot interpret>\n"));
322 ERR_free_strings_fn();
324 (void) fprintf(file
, dgettext(TEXT_DOMAIN
, "<cannot print>\n"));