summaryrefslogtreecommitdiff
path: root/usr/src/lib/libkmsagent/common/k_setupssl.c
blob: de24e499f04243a81b0dbd8fec3fafbafc8a79a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*---------------------------------------------------------------------------
 * Module:            k_setupssl.c
 * Operating System:  Linux, Win32
 *
 * Description:
 * This is the C Implementation file for setting up OpenSSL muti-threading environment
 *
 *-------------------------------------------------------------------------*/

#ifndef WIN32
#include <signal.h>
#include <openssl/evp.h>	/* UNIX */
#include <openssl/engine.h>
#endif

#include "k_setupssl.h"
#include "stdsoap2.h"
#include <openssl/crypto.h>

#if defined(WIN32)

#include <windows.h>
#define MUTEX_TYPE              HANDLE
#define MUTEX_SETUP(x)          (x) = CreateMutex(NULL, FALSE, NULL)
#define MUTEX_CLEANUP(x)        CloseHandle(x)
#define MUTEX_LOCK(x)           WaitForSingleObject((x), INFINITE)
#define MUTEX_UNLOCK(x)         ReleaseMutex(x)
#define THREAD_ID               GetCurrentThreadId()

#else

#include <pthread.h>

# define MUTEX_TYPE             pthread_mutex_t
# define MUTEX_SETUP(x)         pthread_mutex_init(&(x), NULL)
# define MUTEX_CLEANUP(x)       pthread_mutex_destroy(&(x))
# define MUTEX_LOCK(x)          pthread_mutex_lock(&(x))
# define MUTEX_UNLOCK(x)        pthread_mutex_unlock(&(x))
# define THREAD_ID              pthread_self()

#ifdef K_SOLARIS_PLATFORM
MUTEX_TYPE	init_ssl_mutex = PTHREAD_MUTEX_INITIALIZER;
static		int	ssl_initialized = 0;
#endif
#endif

struct CRYPTO_dynlock_value
{ MUTEX_TYPE mutex;
};

void sigpipe_handle(int x)
{
}

static MUTEX_TYPE *mutex_buf;

static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
{ struct CRYPTO_dynlock_value *value;
  value = (struct CRYPTO_dynlock_value*)malloc(sizeof(struct CRYPTO_dynlock_value));
  if (value)
    MUTEX_SETUP(value->mutex);
  return value;
}

static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
{ if (mode & CRYPTO_LOCK)
    MUTEX_LOCK(l->mutex);
  else
    MUTEX_UNLOCK(l->mutex);
}

static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
{ MUTEX_CLEANUP(l->mutex);
  free(l);
}

void kms_locking_function(int mode, int n, const char *file, int line)
{ if (mode & CRYPTO_LOCK)
    MUTEX_LOCK(mutex_buf[n]);
  else
    MUTEX_UNLOCK(mutex_buf[n]);
}


unsigned long id_function(void )
{ return (unsigned long)THREAD_ID;
}

#ifdef WIN32
void OpenSSL_add_all_ciphers(void);	// UNIX
void OpenSSL_add_all_digests(void);
#endif

#ifdef K_HPUX_PLATFORM
extern void allow_unaligned_data_access();
#endif

// gSOAP 2.7e:
//   The function ssl_init is defined in stdsoap2.cpp and is not exported by
//   default by gSOAP.
// gSOAP 2.7.12:
//   The function soap_ssl_init is defined in stdsoap2.cpp.  It replaces
//   ssl_init and is exported by gSOAP.  gSOAP 2.7.13 also supports a new
//   SOAP_SSL_SKIP_HOST_CHECK flag.
#ifndef SOAP_SSL_SKIP_HOST_CHECK
extern int ssl_init();
#endif

int K_SetupSSL()
{ int i;
#ifdef K_SOLARIS_PLATFORM
	if (ssl_initialized)
		return 1;
	MUTEX_LOCK(init_ssl_mutex);
#endif
  mutex_buf = (MUTEX_TYPE*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
  if (!mutex_buf) {
#ifdef K_SOLARIS_PLATFORM
	MUTEX_UNLOCK(init_ssl_mutex);
#endif
    return 0;
  }
  for (i = 0; i < CRYPTO_num_locks(); i++)
    MUTEX_SETUP(mutex_buf[i]);
  if (CRYPTO_get_id_callback() == NULL)
	CRYPTO_set_id_callback(id_function);
  if (CRYPTO_get_locking_callback() == NULL)
	CRYPTO_set_locking_callback(kms_locking_function);

  CRYPTO_set_dynlock_create_callback(dyn_create_function);
  CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
  CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);

#ifndef WIN32
  /* Need SIGPIPE handler on Unix/Linux systems to catch broken pipes: */
  signal(SIGPIPE, sigpipe_handle);
#endif
#ifdef K_HPUX_PLATFORM
//  signal(SIGBUS, sigpipe_handle);
    allow_unaligned_data_access();
#endif
  OpenSSL_add_all_ciphers();
  OpenSSL_add_all_digests();

  // call gSOAP's OpenSSL initialization, which initializes SSL algorithms and seeds RAND

  // gSOAP 2.7e:
  //   The function ssl_init is defined in stdsoap2.cpp and is not exported by
  //   default by gSOAP.
  // gSOAP 2.7.13:
  //   The function soap_ssl_init is defined in stdsoap2.cpp.  It replaces
  //   ssl_init and is exported by gSOAP.  gSOAP 2.7.13 also supports a new
  //   SOAP_SSL_SKIP_HOST_CHECK flag.
#ifdef SOAP_SSL_SKIP_HOST_CHECK
  soap_ssl_init();
#else
  ssl_init();
#endif

#ifdef K_SOLARIS_PLATFORM
	ssl_initialized = 1;
	MUTEX_UNLOCK(init_ssl_mutex);
#endif

  return 1;
}

void K_CleanupSSL()
{ int i;
  if (!mutex_buf)
    return;
#ifdef K_SOLARIS_PLATFORM
  {
	unsigned long (*id_func)();

	if ((id_func = CRYPTO_get_id_callback()) == id_function) {
  		ENGINE_cleanup();
		/* EVP_cleanup(); */
		ERR_free_strings();
		CRYPTO_set_id_callback(NULL);
		CRYPTO_set_locking_callback(NULL);
	}
  }
#endif
  CRYPTO_set_dynlock_create_callback(NULL);
  CRYPTO_set_dynlock_lock_callback(NULL);
  CRYPTO_set_dynlock_destroy_callback(NULL);
  for (i = 0; i < CRYPTO_num_locks(); i++)
    MUTEX_CLEANUP(mutex_buf[i]);
  OPENSSL_free(mutex_buf);
  mutex_buf = NULL;
}

// TODO: what should 'struct soap' really be?
int K_SetupCallbacks( struct soap *i_pSoap )
{
    return 1;
}