File: ssl-bozo.c

package info (click to toggle)
bozohttpd 20060517-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 256 kB
  • ctags: 220
  • sloc: ansic: 2,481; makefile: 82; sh: 44; perl: 4
file content (163 lines) | stat: -rw-r--r-- 4,256 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
/*	$eterna: ssl-bozo.c,v 1.6 2006/05/17 08:19:10 mrg Exp $	*/

/*
 * Copyright (c) 1997-2006 Matthew R. Green
 * 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 and
 *    dedication in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * 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 implements SSL for bozohttpd */

#ifndef NO_SSL_SUPPORT

#include <unistd.h>

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

#include "bozohttpd.h"

static	SSL_CTX		*ssl_context;
static	SSL_METHOD	*ssl_method;
static	SSL		*bozossl;
static	char		*certificate_file;
static	char		*privatekey_file;

static	int	ssl_printf(const char *, ...);
static	ssize_t	ssl_read(int, void *, size_t);
static	ssize_t	ssl_write(int, const void *, size_t);
static	int	ssl_flush(FILE *);

void
ssl_init(void)
{
	if (!certificate_file)
		return;
	SSL_library_init();
	SSL_load_error_strings();

	ssl_method = SSLv23_server_method();
	ssl_context = SSL_CTX_new(ssl_method);

	/* XXX we need to learn how to check the SSL stack for more info */
	if (ssl_context == NULL)
		error(1, "SSL context initialization failed.");

	SSL_CTX_use_certificate_file(ssl_context, certificate_file,
	    SSL_FILETYPE_PEM);
	SSL_CTX_use_PrivateKey_file(ssl_context, privatekey_file,
	    SSL_FILETYPE_PEM);

	/* check consistency of key vs certificate */
	if (!SSL_CTX_check_private_key(ssl_context))
		error(1, "check private key failed");
}

void
ssl_accept()
{
	if (ssl_context) {
		bozossl = SSL_new(ssl_context); /* XXX global sucks */
		SSL_set_rfd(bozossl, 0);
		SSL_set_wfd(bozossl, 1);
		SSL_accept(bozossl);
	}
}

void
ssl_destroy()
{
	if (bozossl)
		SSL_free(bozossl);
}

void
ssl_set_opts(char *cert, char *priv)
{
	certificate_file = cert;
	privatekey_file = priv;
	debug((DEBUG_NORMAL, "using cert/priv files: %s & %s", certificate_file,
	    privatekey_file));
	if (Iflag_set == 0)
		Iflag = "https";
	bozoprintf = ssl_printf;
	bozoread = ssl_read;
	bozowrite = ssl_write;
	bozoflush = ssl_flush;
}

static int
ssl_printf(const char * fmt, ...)
{
	int nbytes;
	char *buf;
	va_list ap;

	/* XXX we need more elegant/proper handling of SSL_write return */
	va_start(ap, fmt);
	if ((nbytes = vasprintf(&buf, fmt, ap)) != -1) 
		SSL_write(bozossl, buf, nbytes);
	va_end(ap);

	return nbytes;
}

static ssize_t
ssl_read(int fd, void *buf, size_t nbytes)
{
	ssize_t rbytes;

	/* XXX we need elegant/proper handling of SSL_read return */
	rbytes = (ssize_t)SSL_read(bozossl, buf, nbytes);
	if (1 > rbytes) {
		if (SSL_get_error(bozossl, rbytes) == SSL_ERROR_WANT_READ)
			warning("SSL_ERROR_WANT_READ");
		else
			warning("SSL_ERROR OTHER");
	}

	return rbytes;
}

static ssize_t
ssl_write(int fd, const void *buf, size_t nbytes)
{
	ssize_t wbytes;

	/* XXX we need elegant/proper handling of SSL_write return */
	wbytes = (ssize_t)SSL_write(bozossl, buf, nbytes);

	return wbytes;
}

static int
ssl_flush(FILE *fp)
{
	/* nothing to see here, move right along */
	return 0;
}
#endif /* NO_SSL_SUPPORT */