]> diplodocus.org Git - nmh/blob - sbr/netsec.c
cd33df9d902f2cae52528b9bbc2197d688c3b4f1
[nmh] / sbr / netsec.c
1
2 /*
3 * netsec.c -- Network security routines for handling protocols that
4 * require SASL and/or TLS.
5 *
6 * This code is Copyright (c) 2016, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13 #include <h/netsec.h>
14 #include <stdarg.h>
15
16 #ifdef CYRUS_SASL
17 #include <sasl/sasl.h>
18 #include <sasl/saslutil.h>
19 # if SASL_VERSION_FULL < 0x020125
20 /* Cyrus SASL 2.1.25 introduced the sasl_callback_ft prototype,
21 which has an explicit void parameter list, according to best
22 practice. So we need to cast to avoid compile warnings.
23 Provide this prototype for earlier versions. */
24 typedef int (*sasl_callback_ft)();
25 # endif /* SASL_VERSION_FULL < 0x020125 */
26 #endif /* CYRUS_SASL */
27
28 #ifdef TLS_SUPPORT
29 #include <openssl/ssl.h>
30 #include <openssl/err.h>
31
32 static int tls_initialized = 0;
33 static SSL_CTX *sslctx = NULL; /* SSL Context */
34
35 #endif /* TLS_SUPPORT */
36
37 /*
38 * Our context structure, which holds all of the relevant information
39 * about a connection.
40 */
41
42 struct _netsec_context {
43 int ns_fd; /* Descriptor for network connection */
44 int ns_snoop; /* If true, display network data */
45 unsigned char *ns_inbuffer; /* Our read input buffer */
46 unsigned char *ns_inptr; /* Our read buffer input pointer */
47 unsigned int ns_inbuflen; /* Length of data in input buffer */
48 unsigned int ns_inbufsize; /* Size of input buffer */
49 unsigned char *ns_outbuffer;/* Output buffer */
50 unsigned char *ns_outptr; /* Output buffer pointer */
51 unsigned int ns_outbuflen; /* Output buffer data length */
52 unsigned int ns_outbufsize; /* Output buffer size */
53 #ifdef CYRUS_SASL
54 char *sasl_service; /* SASL service name */
55 char *sasl_mech; /* User-requested mechanism */
56 sasl_conn_t *sasl_conn; /* SASL connection context */
57 sasl_ssf_t sasl_ssf; /* SASL Security Strength Factor */
58 netsec_sasl_callback sasl_cb; /* SASL callback */
59 #endif /* CYRUS_SASL */
60 #ifdef TLS_SUPPORT
61 BIO *ssl_io; /* BIO used for connection I/O */
62 int tls_active; /* If true, TLS is running */
63 #endif /* TLS_SUPPORT */
64 };
65
66 /*
67 * Function to allocate error message strings
68 */
69
70 static char *netsec_errstr(const char *format, ...);
71
72 /*
73 * How this code works, in general.
74 *
75 * _If_ we are using no encryption or SASL encryption, then we buffer the
76 * network data through ns_inbuffer and ns_outbuffer. That should be
77 * relatively self-explanatory.
78 *
79 * If we are using SSL for encryption, then use a buffering BIO for output
80 * (that just easier). Still do buffering for reads; when we need more
81 * data we call the BIO_read() function to fill our local buffer.
82 *
83 * For SASL, we make use of (for now) the Cyrus-SASL library. For some
84 * mechanisms, we implement those mechanisms directly since the Cyrus SASL
85 * library doesn't support them (like OAuth).
86 */
87
88 /*
89 * Allocate and initialize our security context
90 */
91
92 netsec_context *
93 netsec_init(void)
94 {
95 netsec_context *nsc = mh_xmalloc(sizeof(*nsc));
96
97 nsc->ns_fd = -1;
98 nsc->ns_snoop = 0;
99 nsc->ns_inbuffer = nsc->ns_inptr = NULL;
100 nsc->ns_inbuflen = nsc->ns_inbufsize = 0;
101 nsc->ns_outbuffer = nsc->ns_outptr = NULL;
102 nsc->ns_outbuflen = nsc->ns_outbufsize = 0;
103 #ifdef CYRUS_SASL
104 nsc->sasl_conn = NULL;
105 nsc->sasl_service = nsc->sasl_mech = NULL;
106 #endif /* CYRUS_SASL */
107 #ifdef TLS_SUPPORT
108 nsc->ssl_io = NULL;
109 nsc->tls_active = 0;
110 #endif /* TLS_SUPPORT */
111 return nsc;
112 }
113
114 /*
115 * Set the file descriptor for our context
116 */
117
118 void
119 netsec_set_fd(netsec_context *nsc, int fd)
120 {
121 nsc->ns_fd = fd;
122 }
123
124 /*
125 * Set the snoop flag for this connection
126 */
127
128 void
129 netsec_set_snoop(netsec_context *nsc, int snoop)
130 {
131 nsc->ns_snoop = snoop;
132 }
133
134 /*
135 * Initialize (and enable) TLS for this connection
136 */
137
138 int
139 netsec_set_tls(netsec_context *nsc, int tls, char **errstr)
140 {
141 if (tls) {
142 #ifdef TLS_SUPPORT
143 SSL *ssl;
144 BIO *sbio, *ssl_bio;;
145
146 if (! tls_initialized) {
147 SSL_library_init();
148 SSL_load_error_strings();
149
150 /*
151 * Create the SSL context; this has the properties for all
152 * SSL connections (we are only doing one), though. Make sure
153 * we only support secure (known as of now) TLS protocols.
154 */
155
156 sslctx = SSL_CTX_new(SSLv23_client_method());
157
158 if (! sslctx) {
159 *errstr = netsec_errstr("Unable to initialize OpenSSL "
160 "context: %s",
161 ERR_error_string(ERR_get_error(), NULL));
162 return NOTOK;
163 }
164
165 SSL_CTX_set_options(sslctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
166 SSL_OP_NO_TLSv1);
167
168 tls_initialized++;
169 }
170
171 if (nsc->ns_fd == -1) {
172 *errstr = netsec_errstr("Invalid file descriptor in netsec "
173 "context");
174 return NOTOK;
175 }
176
177 /*
178 * Create the SSL structure which holds the data for a single
179 * TLS connection.
180 */
181
182 ssl = SSL_new(sslctx);
183
184 if (! ssl) {
185 *errstr = netsec_errstr("Unable to create SSL connection: %s",
186 ERR_error_string(ERR_get_error(), NULL));
187 return NOTOK;
188 }
189
190 /*
191 * This is a bit weird, so pay attention.
192 *
193 * We create a socket BIO, and bind it to our SSL connection.
194 * That means reads and writes to the SSL connection will use our
195 * supplied socket.
196 *
197 * Then we create an SSL BIO, and assign our current SSL connection
198 * to it. We then create a buffer BIO and push it in front of our
199 * SSL BIO. So the chain looks like:
200 *
201 * buffer BIO -> SSL BIO -> socket BIO.
202 *
203 * So writes and reads are buffered (we mostly care about writes).
204 */
205
206 sbio = BIO_new_socket(nsc->ns_fd, BIO_NOCLOSE);
207
208 if (! sbio) {
209 *errstr = netsec_errstr("Unable to create a socket BIO: %s",
210 ERR_error_string(ERR_get_error(), NULL));
211 SSL_free(ssl);
212 return NOTOK;
213 }
214
215 SSL_set_bio(ssl, sbio, sbio);
216 SSL_set_connect_state(ssl);
217
218 nsc->ssl_io = BIO_new(BIO_f_buffer());
219
220 if (! nsc->ssl_io) {
221 *errstr = netsec_errstr("Unable to create a buffer BIO: %s",
222 ERR_error_string(ERR_get_error(), NULL));
223 SSL_free(ssl);
224 return NOTOK;
225 }
226
227 ssl_bio = BIO_new(BIO_f_ssl());
228
229 if (! ssl_bio) {
230 *errstr = netsec_errstr("Unable to create a SSL BIO: %s",
231 ERR_error_string(ERR_get_error(), NULL));
232 SSL_free(ssl);
233 return NOTOK;
234 }
235
236 BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
237 BIO_push(nsc->ssl_io, ssl_bio);
238
239 return OK;
240 } else {
241 BIO_free_all(nsc->ssl_io);
242 nsc->ssl_io = NULL;
243
244 return OK;
245 }
246 #else /* TLS_SUPPORT */
247 *errstr = netsec_errstr("TLS is not supported");
248
249 return NOTOK;
250 }
251 #endif /* TLS_SUPPORT */
252 }
253
254 /*
255 * Generate an (allocated) error string
256 */
257
258 static char *
259 netsec_errstr(const char *fmt, ...)
260 {
261 va_list ap;
262 size_t errbufsize;
263 char *errbuf = NULL;
264 int rc = 127;
265
266 do {
267 errbufsize = rc + 1;
268 errbuf = mh_xrealloc(errbuf, errbufsize);
269 va_start(ap, fmt);
270 rc = vsnprintf(errbuf, errbufsize, fmt, ap);
271 va_end(ap);
272 } while (rc >= (int) errbufsize);
273
274 return errbuf;
275 }