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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-openssl.c: Asyncronous Callback-based SOAP Request Queue.
*
* Authors:
* Alex Graveley (alex@ximian.com)
*
* Copyright (C) 2001, Ximian, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_OPENSSL_SSL_H
#include <unistd.h>
#include <glib.h>
#include <sys/time.h>
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include "soup-openssl.h"
static gboolean server_mode = FALSE;
typedef struct {
GIOChannel channel;
gint fd;
GIOChannel *real_sock;
SSL *ssl;
} SoupOpenSSLChannel;
static void
soup_openssl_free (GIOChannel *channel)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
g_io_channel_unref (chan->real_sock);
SSL_free (chan->ssl);
g_free (chan);
}
static GIOStatus
soup_openssl_read (GIOChannel *channel,
gchar *buf,
guint count,
guint *bytes_read,
GError **err)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
gint result;
result = SSL_read (chan->ssl, buf, count);
if (result < 0) {
/* This occurs when a re-handshake is required */
*bytes_read = 0;
if (SSL_get_error (chan->ssl, result) == SSL_ERROR_WANT_READ)
return G_IO_STATUS_AGAIN;
switch (errno) {
case EINVAL:
#if 0
return G_IO_ERROR_INVAL;
#else
return G_IO_STATUS_ERROR;
#endif
case EAGAIN:
case EINTR:
return G_IO_STATUS_AGAIN;
default:
return G_IO_STATUS_ERROR;
}
} else {
*bytes_read = result;
return G_IO_STATUS_NORMAL;
}
}
static GIOStatus
soup_openssl_write (GIOChannel *channel,
const gchar *buf,
gsize count,
gsize *bytes_written,
GError **err)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
gint result;
result = SSL_write (chan->ssl, buf, count);
if (result < 0) {
*bytes_written = 0;
if (SSL_get_error (chan->ssl, result) == SSL_ERROR_WANT_WRITE)
return G_IO_STATUS_AGAIN;
switch (errno) {
case EINVAL:
#if 0
return G_IO_ERROR_INVAL;
#else
return G_IO_STATUS_ERROR;
#endif
case EAGAIN:
case EINTR:
return G_IO_STATUS_AGAIN;
default:
return G_IO_STATUS_ERROR;
}
} else {
*bytes_written = result;
return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
}
}
static GIOStatus
soup_openssl_seek (GIOChannel *channel,
gint64 offset,
GSeekType type,
GError **err)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
GIOError e;
e = g_io_channel_seek (chan->real_sock, offset, type);
if (e != G_IO_ERROR_NONE)
return G_IO_STATUS_ERROR;
else
return G_IO_STATUS_NORMAL;
}
static GIOStatus
soup_openssl_close (GIOChannel *channel,
GError **err)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
g_io_channel_close (chan->real_sock);
return G_IO_STATUS_NORMAL;
}
typedef struct {
GSource source;
GPollFD pollfd;
GIOChannel *channel;
GIOCondition condition;
} SoupOpenSSLWatch;
static gboolean
soup_openssl_prepare (GSource *source,
gint *timeout)
{
SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
watch->channel);
*timeout = -1;
/* Only return TRUE here if _all_ bits in watch->condition will be set
*/
return ((watch->condition & buffer_condition) == watch->condition);
}
static gboolean
soup_openssl_check (GSource *source)
{
SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
watch->channel);
GIOCondition poll_condition = watch->pollfd.revents;
return ((poll_condition | buffer_condition) & watch->condition);
}
static gboolean
soup_openssl_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
{
GIOFunc func = (GIOFunc) callback;
SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) watch->channel;
GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
watch->channel);
GIOCondition cond;
if (!func) {
g_warning ("IO watch dispatched without callback\n"
"You must call g_source_connect().");
return FALSE;
}
cond = (watch->pollfd.revents | buffer_condition) & watch->condition;
if (cond & G_IO_IN) {
do {
if (!(*func) (watch->channel, cond, user_data))
return FALSE;
} while (SSL_pending (chan->ssl));
return TRUE;
} else
return (*func) (watch->channel, cond, user_data);
}
static void
soup_openssl_finalize (GSource *source)
{
SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
g_io_channel_unref (watch->channel);
}
/* All of these functions were basically cut-and-pasted from glib */
GSourceFuncs soup_openssl_watch_funcs = {
soup_openssl_prepare,
soup_openssl_check,
soup_openssl_dispatch,
soup_openssl_finalize
};
static GSource *
soup_openssl_create_watch (GIOChannel *channel,
GIOCondition condition)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
if (condition & G_IO_IN) {
GSource *source;
SoupOpenSSLWatch *watch;
source = g_source_new (&soup_openssl_watch_funcs,
sizeof (SoupOpenSSLWatch));
watch = (SoupOpenSSLWatch *) source;
watch->channel = channel;
g_io_channel_ref (channel);
watch->condition = condition;
watch->pollfd.fd = chan->fd;
watch->pollfd.events = condition;
g_source_add_poll (source, &watch->pollfd);
return source;
}
else {
return chan->real_sock->funcs->io_create_watch (channel,
condition);
}
}
static GIOStatus
soup_openssl_set_flags (GIOChannel *channel,
GIOFlags flags,
GError **err)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
return chan->real_sock->funcs->io_set_flags (channel, flags, err);
}
static GIOFlags
soup_openssl_get_flags (GIOChannel *channel)
{
SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
return chan->real_sock->funcs->io_get_flags (channel);
}
GIOFuncs soup_openssl_channel_funcs = {
soup_openssl_read,
soup_openssl_write,
soup_openssl_seek,
soup_openssl_close,
soup_openssl_create_watch,
soup_openssl_free,
soup_openssl_set_flags,
soup_openssl_get_flags
};
static SSL_CTX *ssl_context = NULL;
#if SSL_LIBRARY_VERSION >= 0x00905100
# define CHECK_OPENSSL_SEEDED RAND_status()
# define CHECK_OPENSSL_SEEDED_FINAL RAND_status()
#else
# define CHECK_OPENSSL_SEEDED FALSE
# define CHECK_OPENSSL_SEEDED_FINAL TRUE
#endif
static gboolean
soup_openssl_seed (void)
{
pid_t pid;
struct timeval tv;
guchar stack [1024], *heap;
if (!CHECK_OPENSSL_SEEDED) {
/* Seed with pid */
pid = getpid ();
RAND_seed ((guchar *) &pid, sizeof (pid_t));
/* Seed with current time */
if (gettimeofday (&tv, NULL) == 0)
RAND_seed ((guchar *) &tv, sizeof (struct timeval));
/* Seed with untouched stack (1024) */
RAND_seed (stack, sizeof (stack));
/* Quit now if we are adequately seeded */
if (CHECK_OPENSSL_SEEDED)
return TRUE;
/* Seed with untouched heap (1024) */
heap = g_malloc (1024);
if (heap)
RAND_seed (heap, 1024);
g_free (heap);
return CHECK_OPENSSL_SEEDED_FINAL;
} else
return TRUE;
}
GIOChannel *
soup_openssl_get_iochannel (GIOChannel *sock)
{
SoupOpenSSLChannel *chan;
GIOChannel *gchan;
int bits, alg_bits, err, sockfd;
SSL *ssl;
X509 *cert;
gchar *ccert_file, *ckey_file;
g_return_val_if_fail (sock != NULL, NULL);
if (!ssl_context && !soup_openssl_init (server_mode))
goto THROW_CREATE_ERROR;
if (!soup_openssl_seed ())
g_warning ("SSL random number seed failed.");
sockfd = g_io_channel_unix_get_fd (sock);
if (!sockfd)
goto THROW_CREATE_ERROR;
ssl = SSL_new (ssl_context);
if (!ssl) {
g_warning ("SSL object creation failure.");
goto THROW_CREATE_ERROR;
}
ccert_file = getenv ("HTTPS_CERT_FILE");
ckey_file = getenv ("HTTPS_KEY_FILE");
if (ccert_file) {
if (!ckey_file) {
g_warning ("SSL key file not specified.");
goto THROW_CREATE_ERROR;
}
if (!SSL_use_RSAPrivateKey_file (ssl, ckey_file, SSL_FILETYPE_PEM)) {
g_warning ("Unable to use private key file.");
ERR_print_errors_fp(stderr);
goto THROW_CREATE_ERROR;
}
if (!SSL_use_certificate_file (ssl, ccert_file, SSL_FILETYPE_PEM)) {
g_warning ("Unable to use certificate file.");
goto THROW_CREATE_ERROR;
}
if (!SSL_check_private_key (ssl)) {
g_warning ("Can't verify correct private key.");
goto THROW_CREATE_ERROR;
}
} else if (ckey_file) {
g_warning ("SSL certificate file not specified.");
}
err = SSL_set_fd (ssl, sockfd);
if (err == 0) {
g_warning ("Unable to set SSL file descriptor.");
goto THROW_CREATE_ERROR;
}
do {
fd_set ssl_fdset;
if (server_mode)
err = SSL_accept (ssl);
else
err = SSL_connect (ssl);
err = SSL_get_error (ssl, err);
if (err == SSL_ERROR_WANT_READ) {
FD_ZERO (&ssl_fdset);
FD_SET (sockfd, &ssl_fdset);
select (sockfd + 1, &ssl_fdset, NULL, NULL, NULL);
}
else if (err == SSL_ERROR_WANT_WRITE) {
FD_ZERO (&ssl_fdset);
FD_SET (sockfd, &ssl_fdset);
select (sockfd + 1, NULL, &ssl_fdset, NULL, NULL);
}
else if (err != SSL_ERROR_NONE) {
g_warning ("Could not establish secure connection.");
goto THROW_CREATE_ERROR;
}
} while (err != SSL_ERROR_NONE);
bits = SSL_get_cipher_bits (ssl, &alg_bits);
if (bits == 0) {
g_warning ("Server requested unsecure tranfer.");
goto THROW_CREATE_ERROR;
}
if (!server_mode) {
cert = SSL_get_peer_certificate (ssl);
if (!cert) {
g_warning ("Server certificate unavailable");
goto THROW_CREATE_ERROR;
}
else
X509_free (cert);
}
chan = g_new0 (SoupOpenSSLChannel, 1);
chan->fd = sockfd;
chan->real_sock = sock;
chan->ssl = ssl;
g_io_channel_ref (sock);
gchan = (GIOChannel *) chan;
gchan->funcs = &soup_openssl_channel_funcs;
g_io_channel_init (gchan);
return gchan;
THROW_CREATE_ERROR:
return NULL;
}
static int
verify_cb (int verified, X509_STORE_CTX *x509_ctx)
{
if (!verified)
g_warning ("Unable to verify server's CA");
return verified;
}
gboolean
soup_openssl_init (gboolean server)
{
static gchar *ssl_ca_file = NULL;
static gchar *ssl_ca_dir = NULL;
SSL_library_init ();
SSL_load_error_strings ();
server_mode = server;
if (server_mode)
ssl_context = SSL_CTX_new (SSLv23_server_method ());
else
ssl_context = SSL_CTX_new (SSLv23_client_method ());
if (!ssl_context) {
g_warning ("Unable to initialize OpenSSL library");
return FALSE;
}
ssl_ca_file = getenv ("HTTPS_CA_FILE");
ssl_ca_dir = getenv ("HTTPS_CA_DIR");
SSL_CTX_set_default_verify_paths (ssl_context);
if (ssl_ca_file || ssl_ca_dir) {
SSL_CTX_load_verify_locations (ssl_context,
ssl_ca_file,
ssl_ca_dir);
SSL_CTX_set_verify (ssl_context, SSL_VERIFY_PEER, verify_cb);
}
return TRUE;
}
void
soup_openssl_set_security_policy (SoupSecurityPolicy policy)
{
switch (policy) {
case SOUP_SECURITY_DOMESTIC:
break;
case SOUP_SECURITY_EXPORT:
break;
case SOUP_SECURITY_FRANCE:
break;
}
}
#endif /*HAVE_OPENSSL_SSL_H*/
|