author | Peter H. Ezetta <peterezetta@porch.com> |
Tue, 05 Jan 2021 23:48:34 -0800 | |
changeset 44 | cd0efa84de3f |
parent 43 | 6903f7870c4c |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (c) 2013 Sunil Nimmagadda <sunil@nimmagadda.net> |
|
3 |
* Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> |
|
4 |
* |
|
5 |
* Permission to use, copy, modify, and distribute this software for any |
|
6 |
* purpose with or without fee is hereby granted, provided that the above |
|
7 |
* copyright notice and this permission notice appear in all copies. |
|
8 |
* |
|
9 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
10 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
11 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
12 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
13 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
14 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
15 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
16 |
*/ |
|
17 |
||
18 |
#include <sys/types.h> |
|
19 |
#include <sys/socket.h> |
|
20 |
#include <sys/uio.h> |
|
21 |
#include <sys/stat.h> |
|
22 |
||
23 |
#include <fcntl.h> |
|
24 |
#include <syslog.h> |
|
25 |
#include <unistd.h> |
|
26 |
||
27 |
#include <openssl/ssl.h> |
|
28 |
#include <openssl/engine.h> |
|
29 |
#include <openssl/err.h> |
|
30 |
||
31 |
#include "pop3d.h" |
|
32 |
#include "ssl.h" |
|
33 |
||
34 |
#define SSL_CIPHERS "HIGH" |
|
35 |
#define SSL_SESSION_TIMEOUT 300 |
|
36 |
||
37 |
static char *ssl_load_file(const char *, off_t *); |
|
38 |
||
39 |
void |
|
40 |
ssl_init(void) |
|
41 |
{ |
|
42 |
/* SSL init */ |
|
43 |
SSL_library_init(); |
|
44 |
SSL_load_error_strings(); |
|
45 |
OpenSSL_add_all_algorithms(); |
|
46 |
||
47 |
/* Init hardware cryto engines. */ |
|
48 |
ENGINE_load_builtin_engines(); |
|
49 |
ENGINE_register_all_complete(); |
|
50 |
} |
|
51 |
||
52 |
void * |
|
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
0
diff
changeset
|
53 |
ssl_setup(const char *certfile, const char *keyfile) |
0 | 54 |
{ |
55 |
SSL_CTX *ctx = NULL; |
|
56 |
char *cert, *key; |
|
57 |
off_t cert_len, key_len; |
|
58 |
||
59 |
/* SSL context creation */ |
|
60 |
ctx = SSL_CTX_new(SSLv23_server_method()); |
|
61 |
if (ctx == NULL) { |
|
62 |
ssl_error("ssl_ctx_create"); |
|
63 |
fatal("ssl_ctx_create: could not create SSL context"); |
|
64 |
} |
|
65 |
||
66 |
SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
|
67 |
SSL_CTX_set_timeout(ctx, SSL_SESSION_TIMEOUT); |
|
68 |
SSL_CTX_set_options(ctx, |
|
69 |
SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_TICKET); |
|
70 |
SSL_CTX_set_options(ctx, |
|
71 |
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); |
|
72 |
||
73 |
/* SSL certificate, key loading */ |
|
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
0
diff
changeset
|
74 |
cert = ssl_load_file(certfile, &cert_len); |
0 | 75 |
if (cert == NULL) |
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
0
diff
changeset
|
76 |
fatal("ssl_load_file: certificate"); |
0 | 77 |
|
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
0
diff
changeset
|
78 |
key = ssl_load_file(keyfile, &key_len); |
0 | 79 |
if (key == NULL) |
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
0
diff
changeset
|
80 |
fatal("ssl_load_file: key"); |
0 | 81 |
|
82 |
if (!SSL_CTX_set_cipher_list(ctx, SSL_CIPHERS)) |
|
83 |
goto err; |
|
84 |
||
85 |
if (!ssl_ctx_use_certificate_chain(ctx, cert, cert_len)) |
|
86 |
goto err; |
|
87 |
||
88 |
else if (!ssl_ctx_use_private_key(ctx, key, key_len)) |
|
89 |
goto err; |
|
90 |
||
91 |
else if (!SSL_CTX_check_private_key(ctx)) |
|
92 |
goto err; |
|
93 |
||
94 |
return (ctx); |
|
95 |
||
96 |
err: |
|
97 |
if (ctx != NULL) |
|
98 |
SSL_CTX_free(ctx); |
|
99 |
ssl_error("ssl_setup"); |
|
100 |
fatal("ssl_setup: cannot set SSL up"); |
|
101 |
return (NULL); |
|
102 |
} |
|
103 |
||
104 |
void * |
|
105 |
pop3s_init(SSL_CTX *ctx, int fd) |
|
106 |
{ |
|
107 |
SSL *ssl; |
|
108 |
||
109 |
if ((ssl = SSL_new(ctx)) == NULL) |
|
110 |
fatal("SSL_new"); |
|
111 |
||
112 |
if (SSL_set_fd(ssl, fd) == 0) |
|
113 |
fatal("SSL_set_fd"); |
|
114 |
||
115 |
return (ssl); |
|
116 |
} |
|
117 |
||
118 |
static char * |
|
119 |
ssl_load_file(const char *name, off_t *len) |
|
120 |
{ |
|
121 |
struct stat st; |
|
122 |
off_t size; |
|
123 |
char *buf = NULL; |
|
124 |
int fd; |
|
125 |
||
126 |
if ((fd = open(name, O_RDONLY)) == -1) |
|
127 |
return (NULL); |
|
128 |
||
129 |
if (fstat(fd, &st) != 0) |
|
130 |
goto fail; |
|
131 |
||
132 |
size = st.st_size; |
|
133 |
if ((buf = calloc(1, size + 1)) == NULL) |
|
134 |
goto fail; |
|
135 |
if (read(fd, buf, size) != size) |
|
136 |
goto fail; |
|
137 |
||
138 |
close(fd); |
|
139 |
||
140 |
*len = size; |
|
141 |
return (buf); |
|
142 |
||
143 |
fail: |
|
144 |
if (buf != NULL) |
|
145 |
free(buf); |
|
146 |
||
147 |
close(fd); |
|
148 |
return (NULL); |
|
149 |
} |
|
150 |
||
151 |
void |
|
152 |
ssl_error(const char *where) |
|
153 |
{ |
|
154 |
unsigned long code; |
|
155 |
char errbuf[128]; |
|
156 |
extern int debug; |
|
157 |
||
158 |
if (!debug) |
|
159 |
return; |
|
160 |
||
161 |
for (; (code = ERR_get_error()) != 0 ;) { |
|
162 |
ERR_error_string_n(code, errbuf, sizeof(errbuf)); |
|
163 |
logit(LOG_DEBUG, "SSL library error: %s: %s", where, errbuf); |
|
164 |
} |
|
165 |
} |
|
166 |