3 * netsec.c -- Network security routines for handling protocols that
4 * require SASL and/or TLS.
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.
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 */
29 #include <openssl/ssl.h>
30 #include <openssl/err.h>
32 static int tls_initialized
= 0;
33 static SSL_CTX
*sslctx
= NULL
; /* SSL Context */
35 #endif /* TLS_SUPPORT */
38 * Our context structure, which holds all of the relevant information
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 */
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 */
61 BIO
*ssl_io
; /* BIO used for connection I/O */
62 int tls_active
; /* If true, TLS is running */
63 #endif /* TLS_SUPPORT */
67 * Function to allocate error message strings
70 static char *netsec_errstr(const char *format
, ...);
73 * How this code works, in general.
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.
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.
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).
89 * Allocate and initialize our security context
95 netsec_context
*nsc
= mh_xmalloc(sizeof(*nsc
));
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;
104 nsc
->sasl_conn
= NULL
;
105 nsc
->sasl_service
= nsc
->sasl_mech
= NULL
;
106 #endif /* CYRUS_SASL */
110 #endif /* TLS_SUPPORT */
115 * Set the file descriptor for our context
119 netsec_set_fd(netsec_context
*nsc
, int fd
)
125 * Set the snoop flag for this connection
129 netsec_set_snoop(netsec_context
*nsc
, int snoop
)
131 nsc
->ns_snoop
= snoop
;
135 * Initialize (and enable) TLS for this connection
139 netsec_set_tls(netsec_context
*nsc
, int tls
, char **errstr
)
144 BIO
*sbio
, *ssl_bio
;;
146 if (! tls_initialized
) {
148 SSL_load_error_strings();
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.
156 sslctx
= SSL_CTX_new(SSLv23_client_method());
159 *errstr
= netsec_errstr("Unable to initialize OpenSSL "
161 ERR_error_string(ERR_get_error(), NULL
));
165 SSL_CTX_set_options(sslctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
171 if (nsc
->ns_fd
== -1) {
172 *errstr
= netsec_errstr("Invalid file descriptor in netsec "
178 * Create the SSL structure which holds the data for a single
182 ssl
= SSL_new(sslctx
);
185 *errstr
= netsec_errstr("Unable to create SSL connection: %s",
186 ERR_error_string(ERR_get_error(), NULL
));
191 * This is a bit weird, so pay attention.
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
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:
201 * buffer BIO -> SSL BIO -> socket BIO.
203 * So writes and reads are buffered (we mostly care about writes).
206 sbio
= BIO_new_socket(nsc
->ns_fd
, BIO_NOCLOSE
);
209 *errstr
= netsec_errstr("Unable to create a socket BIO: %s",
210 ERR_error_string(ERR_get_error(), NULL
));
215 SSL_set_bio(ssl
, sbio
, sbio
);
216 SSL_set_connect_state(ssl
);
218 nsc
->ssl_io
= BIO_new(BIO_f_buffer());
221 *errstr
= netsec_errstr("Unable to create a buffer BIO: %s",
222 ERR_error_string(ERR_get_error(), NULL
));
227 ssl_bio
= BIO_new(BIO_f_ssl());
230 *errstr
= netsec_errstr("Unable to create a SSL BIO: %s",
231 ERR_error_string(ERR_get_error(), NULL
));
236 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
237 BIO_push(nsc
->ssl_io
, ssl_bio
);
241 BIO_free_all(nsc
->ssl_io
);
246 #else /* TLS_SUPPORT */
247 *errstr
= netsec_errstr("TLS is not supported");
251 #endif /* TLS_SUPPORT */
255 * Generate an (allocated) error string
259 netsec_errstr(const char *fmt
, ...)
268 errbuf
= mh_xrealloc(errbuf
, errbufsize
);
270 rc
= vsnprintf(errbuf
, errbufsize
, fmt
, ap
);
272 } while (rc
>= (int) errbufsize
);