File: ssl.c

package info (click to toggle)
ayttm 0.6.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 8,372 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 564; yacc: 294; lex: 53; sed: 16
file content (197 lines) | stat: -rw-r--r-- 4,403 bytes parent folder | download | duplicates (2)
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
/*
 * Ayttm
 *
 * Copyright (C) 2003,2009, the Ayttm team
 *
 * this code is derivative of Sylpheed-claws
 * Copyright (C) 1999-2003, Hiroyuki Yamamoto
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "config.h"

#ifdef HAVE_OPENSSL
#include "ssl.h"
#include "ssl_certificate.h"

#include "debug.h"
#include <string.h>
#include <glib.h>
#include "globals.h"
#include "networking.h"

static gboolean ssl_inited = FALSE;

static GMutex **ssl_mutex = NULL;

static void locking_function(int mode, int n, const char *file, int line)
{
	if (mode & CRYPTO_LOCK)
		g_mutex_lock(ssl_mutex[n]);
	else
		g_mutex_unlock(ssl_mutex[n]);
}

static unsigned long id_function(void)
{
	return (unsigned long)g_thread_self();
}

/* Global system initialization */
void ssl_init(void)
{
	int num_locks = 0, i = 0;
	static GStaticMutex ssl_init_lock = G_STATIC_MUTEX_INIT;

	g_static_mutex_lock(&ssl_init_lock);

	if (ssl_inited)
		return;

	SSL_library_init();
	SSL_load_error_strings();

	num_locks = CRYPTO_num_locks();

	ssl_mutex = g_new0(GMutex *, num_locks);

	for (i = 0; i < num_locks; i++)
		ssl_mutex[i] = g_mutex_new();

	CRYPTO_set_locking_callback(locking_function);
	CRYPTO_set_id_callback(id_function);

	ssl_inited = TRUE;

	g_static_mutex_unlock(&ssl_init_lock);
}

SSL *ssl_get_socket(int sock, const char *host, int port, void *data)
{
	SSL_CTX *ssl_ctx = NULL;
	SSL_METHOD *meth;

	if (!ssl_inited)
		ssl_init();

	/* Create our context */
	meth = SSLv23_client_method();
	ssl_ctx = SSL_CTX_new(meth);

	/* Set default certificate paths */
	SSL_CTX_set_default_verify_paths(ssl_ctx);

#if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
	SSL_CTX_set_verify_depth(ssl_ctx, 1);
#endif

	return ssl_init_socket_with_method(sock, host, port, ssl_ctx,
		SSL_METHOD_SSLv23, data);
}

SSL *ssl_init_socket_with_method(int sock, const char *host, int port,
	SSL_CTX *ssl_ctx, SSLMethod method, void *data)
{
	X509 *server_cert;
	SSL *ssl;
	int res = 0;

	ssl = SSL_new(ssl_ctx);

	if (ssl == NULL) {
		eb_debug(DBG_CORE, "Error creating ssl context\n");
		return NULL;
	}

	switch (method) {
	case SSL_METHOD_SSLv23:
		eb_debug(DBG_CORE, "Setting SSLv23 client method\n");
		SSL_set_ssl_method(ssl, SSLv23_client_method());
		break;
	case SSL_METHOD_TLSv1:
		eb_debug(DBG_CORE, "Setting TLSv1 client method\n");
		SSL_set_ssl_method(ssl, TLSv1_client_method());
		break;
	default:
		break;
	}

	SSL_set_fd(ssl, sock);
	if ((res = SSL_connect(ssl)) == -1) {
		eb_debug(DBG_CORE, ("SSL connect failed (%s)\n"),
			ERR_error_string(SSL_get_error(ssl, res), NULL));
		res = SSL_get_error(ssl, res);
		switch (res) {
		case SSL_ERROR_NONE:
			printf("SSL_ERROR_NONE\n");
			break;
		case SSL_ERROR_WANT_READ:
			printf("SSL_ERROR_WANT_READ\n");
			break;
		case SSL_ERROR_WANT_WRITE:
			printf("SSL_ERROR_WANT_WRITE\n");
			break;
		case SSL_ERROR_ZERO_RETURN:
			printf("SSL_ERROR_ZERO_RETURN\n");
			break;
		default:
			printf("DEFAULT\n");
			break;
		}
		SSL_free(ssl);
		return NULL;
	}

	/* Get the cipher */

	eb_debug(DBG_CORE, "SSL connection using %s\n", SSL_get_cipher(ssl));

	/* Get server's certificate (note: beware of dynamic allocation) */
	if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
		eb_debug(DBG_CORE,
			"server_cert is NULL ! this _should_not_ happen !\n");
		SSL_free(ssl);
		return NULL;
	}

	if (!ssl_certificate_check(server_cert, host, port, data)) {
		X509_free(server_cert);
		SSL_free(ssl);
		return NULL;
	}

	X509_free(server_cert);

	return ssl;
}

void ssl_done_socket(SSL *ssl)
{
	if (ssl)
		SSL_free(ssl);
}

int ssl_read(SSL *ssl, char *buf, int len)
{
	return SSL_read(ssl, buf, len);
}

int ssl_write(SSL *ssl, const char *buf, int len)
{
	return SSL_write(ssl, buf, len);
}
#endif