summaryrefslogtreecommitdiff
path: root/usr.sbin/sasyncd/net_ssl.c
blob: 7cef23bf11215043bed1698443fe9c88597a9a86 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*	$OpenBSD: net_ssl.c,v 1.1 2005/03/30 18:44:49 ho Exp $	*/

/*
 * Copyright (c) 2005 Håkan Olsson.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This code was written under funding by Multicom Security AB.
 */


#include <sys/types.h>

#include <openssl/ssl.h>
#include <openssl/tls1.h>
#include <openssl/err.h>

#include <string.h>

#include "sasyncd.h"
#include "net.h"

/* Global SSL context. */
SSL_CTX	*ctx;

static void	net_SSL_dump_stack(int);
static void	net_SSL_print_error(int, int);

int
net_SSL_init(void)
{
	SSL_library_init();
	SSL_load_error_strings();

	ctx = SSL_CTX_new(TLSv1_method());
	if (!ctx)
		return -1;

	(void)SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv2);

	/* Load CA cert. */
	if (!SSL_CTX_load_verify_locations(ctx, cfgstate.cafile, NULL)) {
		net_SSL_dump_stack(0);
		fprintf(stderr, "cannot read \"%s\": %s\n", cfgstate.cafile,
		    strerror(errno));
		return -1;
	}

	/* Load our certificate. */
	if (!SSL_CTX_use_certificate_chain_file(ctx, cfgstate.certfile)) {
		net_SSL_dump_stack(0);
		fprintf(stderr, "cannot read \"%s\": %s\n", cfgstate.certfile,
		    strerror(errno));
		return -1;
	}

	/* Load and check private key. */
	if (!SSL_CTX_use_PrivateKey_file(ctx, cfgstate.privkeyfile,
	    SSL_FILETYPE_PEM)) {
		net_SSL_dump_stack(0);
		if (ERR_GET_REASON(ERR_peek_error() == EVP_R_BAD_DECRYPT)) {
			fprintf(stderr, "bad pass phrase\n");
			return -1;
		} else {
			fprintf(stderr, "cannot read \"%s\": %s\n",
			    cfgstate.privkeyfile, strerror(errno));
			return -1;
		}
	}
	if (!SSL_CTX_check_private_key(ctx)) {
		net_SSL_dump_stack(0);
		fprintf(stderr, "Private key does not match certificate\n");
		return -1;
	}
	return 0;
}

int
net_SSL_connect(struct syncpeer *p)
{
	int	r, err;

	p->ssl = SSL_new(ctx);
	if (!p->ssl)
		return -1;
	SSL_set_fd(p->ssl, p->socket);
	r = SSL_connect(p->ssl);
	if (r != 1) {
		err = SSL_get_error(p->ssl, r);
		net_SSL_print_error(err, r);
		return -1;
	}
	log_msg(2, "TLS connection established with peer "
	    "\"%s\"", p->name);
	return 0;
}

void
net_SSL_disconnect(struct syncpeer *p)
{
	if (p->ssl) {
		SSL_shutdown(p->ssl);
		SSL_free(p->ssl);
	}
	p->ssl = NULL;
}

static void
net_SSL_dump_stack(int level)
{
	int	err;

	while ((err = ERR_get_error()) != 0)
		log_msg(level, "%s", ERR_error_string(err, NULL));
}

static void
net_SSL_print_error(int r, int prev)
{
	char	*msg;

	switch (r) {
	case SSL_ERROR_NONE:
		msg = "SSL_ERROR_NONE";
		break;
	case SSL_ERROR_ZERO_RETURN:
		msg = "SSL_ERROR_ZERO_RETURN";
		break;
	case SSL_ERROR_WANT_READ:
		msg = "SSL_ERROR_WANT_READ";
		break;
	case SSL_ERROR_WANT_WRITE:
		msg = "SSL_ERROR_WANT_WRITE";
		break;
	case SSL_ERROR_WANT_CONNECT:
		msg = "SSL_ERROR_WANT_CONNECT";
		break;
	case SSL_ERROR_WANT_ACCEPT:
		msg = "SSL_ERROR_WANT_ACCEPT";
		break;
	case SSL_ERROR_WANT_X509_LOOKUP:
		msg = "SSL_ERROR_WANT_X509_LOOKUP";
		break;
	case SSL_ERROR_SYSCALL:
		msg = "SSL_ERROR_SYSCALL";
		break;
	case SSL_ERROR_SSL:
		msg = "SSL_ERROR_SSL";
		break;
	default:
		msg = "<unknown error>";
		break;
	}

	log_msg(3, "SSL: \"%s\" original code = %d", msg, prev);

	net_SSL_dump_stack(3);
}

static int
net_SSL_io(struct syncpeer *p, void *buf, u_int32_t len, int writeflag)
{
	int	ret, e;

  retry:
	if (writeflag)
		ret = SSL_write(p->ssl, buf, len);
	else
		ret = SSL_read(p->ssl, buf, len);
	if (ret == (int)len)
		return 0;

	e = SSL_get_error(p->ssl, ret);
	net_SSL_print_error(e, ret);

	if (e == SSL_ERROR_WANT_READ || e == SSL_ERROR_WANT_WRITE)
		goto retry; /* Enough to just retry here? XXX */

	log_msg(1, "peer \"%s\" disconnected", p->name);
	net_disconnect_peer(p);
	return 1;
}

int
net_SSL_read(struct syncpeer *p, void *buf, u_int32_t len)
{
	int r, err;

	if (!p->ssl) {
		p->ssl = SSL_new(ctx);
		if (!p->ssl) {
			log_msg(0, "SSL_new() failed");
			return NULL;
		}
		SSL_set_fd(p->ssl, p->socket);
		r = SSL_accept(p->ssl);
		if (r != 1) {
			err = SSL_get_error(p->ssl, r);
			net_SSL_print_error(err, r);
			return NULL;
		}
	}

	return net_SSL_io(p, buf, len, 0);
}

int
net_SSL_write(struct syncpeer *p, void *buf, u_int32_t len)
{
	return net_SSL_io(p, buf, len, 1);
}

void
net_SSL_shutdown(void)
{
	ERR_free_strings();
	SSL_CTX_free(ctx);
}