1 /* netsec.c -- Network security routines for handling protocols that
2 * require SASL and/or TLS.
4 * This code is Copyright (c) 2016, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
14 #include <sys/select.h>
18 #include <sasl/sasl.h>
19 #include <sasl/saslutil.h>
20 # if SASL_VERSION_FULL < 0x020125
21 /* Cyrus SASL 2.1.25 introduced the sasl_callback_ft prototype,
22 which has an explicit void parameter list, according to best
23 practice. So we need to cast to avoid compile warnings.
24 Provide this prototype for earlier versions. */
25 typedef int (*sasl_callback_ft
)();
26 # endif /* SASL_VERSION_FULL < 0x020125 */
28 static int netsec_get_user(void *context
, int id
, const char **result
,
30 static int netsec_get_password(sasl_conn_t
*conn
, void *context
, int id
,
31 sasl_secret_t
**psecret
);
33 static int sasl_initialized
= 0;
35 #define SASL_MAXRECVBUF 65536
36 #endif /* CYRUS_SASL */
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
42 static int tls_initialized
= 0;
43 static SSL_CTX
*sslctx
= NULL
; /* SSL Context */
45 #endif /* TLS_SUPPORT */
47 /* I'm going to hardcode this for now; maybe make it adjustable later? */
48 #define NETSEC_BUFSIZE 65536
51 * Our context structure, which holds all of the relevant information
55 struct _netsec_context
{
56 int ns_readfd
; /* Read descriptor for network connection */
57 int ns_writefd
; /* Write descriptor for network connection */
58 int ns_noclose
; /* Do not close file descriptors if set */
59 int ns_snoop
; /* If true, display network data */
60 int ns_snoop_noend
; /* If true, didn't get a CR/LF on last line */
61 netsec_snoop_callback
*ns_snoop_cb
; /* Snoop output callback */
62 void *ns_snoop_context
; /* Context data for snoop function */
63 int ns_timeout
; /* Network read timeout, in seconds */
64 char *ns_userid
; /* Userid for authentication */
65 char *ns_hostname
; /* Hostname we've connected to */
66 unsigned char *ns_inbuffer
; /* Our read input buffer */
67 unsigned char *ns_inptr
; /* Our read buffer input pointer */
68 unsigned int ns_inbuflen
; /* Length of data in input buffer */
69 unsigned int ns_inbufsize
; /* Size of input buffer */
70 unsigned char *ns_outbuffer
;/* Output buffer */
71 unsigned char *ns_outptr
; /* Output buffer pointer */
72 unsigned int ns_outbuflen
; /* Output buffer data length */
73 unsigned int ns_outbufsize
; /* Output buffer size */
74 char *sasl_mech
; /* User-requested mechanism */
75 char *sasl_chosen_mech
; /* Mechanism chosen by SASL */
76 netsec_sasl_callback sasl_proto_cb
; /* SASL callback we use */
78 char *oauth_service
; /* OAuth2 service name */
79 #endif /* OAUTH_SUPPORT */
81 sasl_conn_t
*sasl_conn
; /* SASL connection context */
82 sasl_ssf_t sasl_ssf
; /* SASL Security Strength Factor */
83 sasl_callback_t
*sasl_cbs
; /* Callbacks used by SASL */
84 nmh_creds_t sasl_creds
; /* Credentials (username/password) */
85 sasl_secret_t
*sasl_secret
; /* SASL password structure */
86 int sasl_seclayer
; /* If true, SASL security layer is enabled */
87 char *sasl_tmpbuf
; /* Temporary read buffer for decodes */
88 size_t sasl_maxbufsize
; /* Maximum negotiated SASL buffer size */
89 #endif /* CYRUS_SASL */
91 BIO
*ssl_io
; /* BIO used for connection I/O */
92 int tls_active
; /* If true, TLS is running */
93 #endif /* TLS_SUPPORT */
97 * Function to read data from the actual network socket
100 static int netsec_fillread(netsec_context
*ns_context
, char **errstr
);
103 * Code to check the ASCII content of a byte array.
106 static int checkascii(const unsigned char *byte
, size_t len
);
109 * How this code works, in general.
111 * _If_ we are using no encryption then we buffer the network data
112 * through ns_inbuffer and ns_outbuffer. That should be relatively
115 * If we use encryption, then ns_inbuffer and ns_outbuffer contain the
116 * cleartext data. When it comes time to send the encrypted data on the
117 * (either from a flush or the buffer is full) we either use BIO_write()
118 * for TLS or sasl_encode() (followed by a write() for Cyrus-SASL. For
119 * reads we either use BIO_read() (TLS) or do a network read into a
120 * temporary buffer and use sasl_decode() (Cyrus-SASL). Note that if
121 * negotiate TLS then we disable SASL encryption.
123 * We used to use a buffering BIO for the reads/writes for TLS, but it
124 * ended up being complicated to special-case the buffering for everything
125 * except TLS, so the buffering is now unified, no matter which encryption
126 * method is being used (even none).
128 * For SASL authentication, we make use of (for now) the Cyrus-SASL
129 * library. For some mechanisms, we implement those mechanisms directly
130 * since the Cyrus SASL library doesn't support them (like OAuth).
134 * Allocate and initialize our security context
144 nsc
->ns_writefd
= -1;
147 nsc
->ns_snoop_noend
= 0;
148 nsc
->ns_snoop_cb
= NULL
;
149 nsc
->ns_snoop_context
= NULL
;
150 nsc
->ns_userid
= NULL
;
151 nsc
->ns_hostname
= NULL
;
152 nsc
->ns_timeout
= 60; /* Our default */
153 nsc
->ns_inbufsize
= NETSEC_BUFSIZE
;
154 nsc
->ns_inbuffer
= mh_xmalloc(nsc
->ns_inbufsize
);
155 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
156 nsc
->ns_inbuflen
= 0;
157 nsc
->ns_outbufsize
= NETSEC_BUFSIZE
;
158 nsc
->ns_outbuffer
= mh_xmalloc(nsc
->ns_outbufsize
);
159 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
160 nsc
->ns_outbuflen
= 0;
161 nsc
->sasl_mech
= NULL
;
162 nsc
->sasl_chosen_mech
= NULL
;
163 nsc
->sasl_proto_cb
= NULL
;
165 nsc
->oauth_service
= NULL
;
166 #endif /* OAUTH_SUPPORT */
168 nsc
->sasl_conn
= NULL
;
169 nsc
->sasl_cbs
= NULL
;
170 nsc
->sasl_creds
= NULL
;
171 nsc
->sasl_secret
= NULL
;
173 nsc
->sasl_seclayer
= 0;
174 nsc
->sasl_tmpbuf
= NULL
;
175 nsc
->sasl_maxbufsize
= 0;
176 #endif /* CYRUS_SASL */
180 #endif /* TLS_SUPPORT */
185 * Shutdown the connection completely and free all resources.
189 netsec_shutdown(netsec_context
*nsc
)
191 free(nsc
->ns_userid
);
192 free(nsc
->ns_hostname
);
193 free(nsc
->ns_inbuffer
);
194 free(nsc
->ns_outbuffer
);
195 free(nsc
->sasl_mech
);
196 free(nsc
->sasl_chosen_mech
);
198 free(nsc
->oauth_service
);
199 #endif /* OAUTH_SERVICE */
202 sasl_dispose(&nsc
->sasl_conn
);
205 nmh_credentials_free(nsc
->sasl_creds
);
206 if (nsc
->sasl_secret
) {
207 if (nsc
->sasl_secret
->len
> 0) {
208 memset(nsc
->sasl_secret
->data
, 0, nsc
->sasl_secret
->len
);
210 free(nsc
->sasl_secret
);
212 free(nsc
->sasl_tmpbuf
);
213 #endif /* CYRUS_SASL */
217 * I checked; BIO_free_all() will cause SSL_shutdown to be called
218 * on the SSL object in the chain.
220 BIO_free_all(nsc
->ssl_io
);
221 #endif /* TLS_SUPPORT */
223 if (! nsc
->ns_noclose
) {
224 if (nsc
->ns_readfd
!= -1)
225 close(nsc
->ns_readfd
);
226 if (nsc
->ns_writefd
!= -1 && nsc
->ns_writefd
!= nsc
->ns_readfd
)
227 close(nsc
->ns_writefd
);
234 * Set the file descriptor for our context
238 netsec_set_fd(netsec_context
*nsc
, int readfd
, int writefd
)
240 nsc
->ns_readfd
= readfd
;
241 nsc
->ns_writefd
= writefd
;
245 * Set the userid used for authentication for this context
249 netsec_set_userid(netsec_context
*nsc
, const char *userid
)
251 nsc
->ns_userid
= getcpy(userid
);
255 * Set the hostname of the remote host we're connecting to.
259 netsec_set_hostname(netsec_context
*nsc
, const char *hostname
)
261 nsc
->ns_hostname
= mh_xstrdup(hostname
);
265 * Get the snoop flag for this connection
269 netsec_get_snoop(netsec_context
*nsc
)
271 return nsc
->ns_snoop
;
275 * Set the snoop flag for this connection
279 netsec_set_snoop(netsec_context
*nsc
, int snoop
)
281 nsc
->ns_snoop
= snoop
;
285 * Set the snoop callback for this connection.
288 void netsec_set_snoop_callback(netsec_context
*nsc
,
289 netsec_snoop_callback callback
, void *context
)
291 nsc
->ns_snoop_cb
= callback
;
292 nsc
->ns_snoop_context
= context
;
296 * A base64-decoding snoop callback
300 netsec_b64_snoop_decoder(netsec_context
*nsc
, const char *string
, size_t len
,
303 unsigned char *decoded
;
308 offset
= context
? *((int *) context
) : 0;
312 * Output non-base64 data first.
314 fprintf(stderr
, "%.*s", offset
, string
);
319 if (decodeBase64(string
, &decoded
, &decodedlen
, 1, NULL
) == OK
) {
321 * Some mechanisms produce large binary tokens, which aren't really
322 * readable. So let's do a simple heuristic. If the token is greater
323 * than 100 characters _and_ the first 100 bytes are more than 50%
324 * non-ASCII, then don't print the decoded buffer, just the
327 if (decodedlen
> 100 && !checkascii(decoded
, 100)) {
328 fprintf(stderr
, "%.*s\n", (int) len
, string
);
331 hexify(decoded
, decodedlen
, &hexified
);
332 fprintf(stderr
, "b64<%s>\n", hexified
);
337 fprintf(stderr
, "%.*s\n", (int) len
, string
);
342 * If the ASCII content is > 50%, return 1
346 checkascii(const unsigned char *bytes
, size_t len
)
348 size_t count
= 0, half
= len
/ 2;
351 if (isascii(*bytes
) && isprint(*bytes
) && ++count
> half
)
354 /* No chance by this point */
355 if (count
+ len
< half
)
363 * Set the read timeout for this connection
367 netsec_set_timeout(netsec_context
*nsc
, int timeout
)
369 nsc
->ns_timeout
= timeout
;
373 * Read data from the network. Basically, return anything in our buffer,
374 * otherwise fill from the network.
378 netsec_read(netsec_context
*nsc
, void *buffer
, size_t size
, char **errstr
)
383 * If our buffer is empty, then we should fill it now
386 if (nsc
->ns_inbuflen
== 0) {
387 if (netsec_fillread(nsc
, errstr
) != OK
)
392 * netsec_fillread only returns if the buffer is full, so we can
393 * assume here that this has something in it.
396 retlen
= min(size
, nsc
->ns_inbuflen
);
398 memcpy(buffer
, nsc
->ns_inptr
, retlen
);
400 if (retlen
== (int) nsc
->ns_inbuflen
) {
402 * We've emptied our buffer, so reset everything.
404 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
405 nsc
->ns_inbuflen
= 0;
407 nsc
->ns_inptr
+= size
;
408 nsc
->ns_inbuflen
-= size
;
415 * Get a "line" (CR/LF) terminated from the network.
417 * Okay, we play some games here, so pay attention:
419 * - Unlike every other function, we return a pointer to the
420 * existing buffer. This pointer is valid until you call another
421 * read function again.
422 * - We NUL-terminate the buffer right at the end, before the CR-LF terminator.
423 * - Technically we look for a LF; if we find a CR right before it, then
425 * - If your data may contain embedded NULs, this won't work. You should
426 * be using netsec_read() in that case.
430 netsec_readline(netsec_context
*nsc
, size_t *len
, char **errstr
)
432 unsigned char *ptr
= nsc
->ns_inptr
;
433 size_t count
= 0, offset
;
437 * Search through our existing buffer for a LF
440 while (count
< nsc
->ns_inbuflen
) {
442 if (*ptr
++ == '\n') {
443 char *sptr
= (char *) nsc
->ns_inptr
;
444 if (count
> 1 && *(ptr
- 2) == '\r')
448 *len
= ptr
- nsc
->ns_inptr
;
449 nsc
->ns_inptr
+= count
;
450 nsc
->ns_inbuflen
-= count
;
453 if (nsc
->sasl_seclayer
)
454 fprintf(stderr
, "(sasl-decrypted) ");
455 #endif /* CYRUS_SASL */
458 fprintf(stderr
, "(tls-decrypted) ");
459 #endif /* TLS_SUPPORT */
460 fprintf(stderr
, "<= ");
461 if (nsc
->ns_snoop_cb
)
462 nsc
->ns_snoop_cb(nsc
, sptr
, strlen(sptr
),
463 nsc
->ns_snoop_context
);
474 * Hm, we didn't find a \n. If we've already searched half of the input
475 * buffer, return an error.
478 if (count
>= nsc
->ns_inbufsize
/ 2) {
479 netsec_err(errstr
, "Unable to find a line terminator after %zu bytes",
485 * Okay, get some more network data. This may move inptr, so regenerate
489 offset
= ptr
- nsc
->ns_inptr
;
491 if (netsec_fillread(nsc
, errstr
) != OK
)
494 ptr
= nsc
->ns_inptr
+ offset
;
498 return NULL
; /* Should never reach this */
502 * Fill our read buffer with some data from the network.
506 netsec_fillread(netsec_context
*nsc
, char **errstr
)
510 size_t readbufsize
, remaining
, startoffset
;
514 * If inbuflen is zero, that means the buffer has been emptied
515 * completely. In that case move inptr back to the start.
518 if (nsc
->ns_inbuflen
== 0) {
519 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
522 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
524 #endif /* CYRUS_SASL || TLS_SUPPORT */
526 * If we are using TLS and there's anything pending, then skip the
530 if (!nsc
->tls_active
|| BIO_pending(nsc
->ssl_io
) == 0)
531 #endif /* TLS_SUPPORT */
537 FD_SET(nsc
->ns_readfd
, &rfds
);
539 tv
.tv_sec
= nsc
->ns_timeout
;
542 rc
= select(nsc
->ns_readfd
+ 1, &rfds
, NULL
, NULL
, &tv
);
545 netsec_err(errstr
, "select() while reading failed: %s",
551 netsec_err(errstr
, "read() timed out after %d seconds",
557 * At this point, we know that rc is 1, so there's not even any
558 * point to check to see if our descriptor is set in rfds.
565 * startoffset is the offset from the beginning of the input
566 * buffer to data that is in our input buffer, but has not yet
567 * been consumed. This can be non-zero if functions like
568 * netsec_readline() leave leftover data.
570 * remaining is the remaining amount of unconsumed data in the input
573 * end is a pointer to the end of the valid data + 1; it's where
574 * the next read should go.
577 startoffset
= nsc
->ns_inptr
- nsc
->ns_inbuffer
;
578 remaining
= nsc
->ns_inbufsize
- (startoffset
+ nsc
->ns_inbuflen
);
579 end
= nsc
->ns_inptr
+ nsc
->ns_inbuflen
;
582 * If we're past the halfway point in our read buffers, shuffle everything
583 * back to the beginning.
586 if (startoffset
> nsc
->ns_inbufsize
/ 2) {
587 memmove(nsc
->ns_inbuffer
, nsc
->ns_inptr
, nsc
->ns_inbuflen
);
588 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
590 remaining
= nsc
->ns_inbufsize
- nsc
->ns_inbuflen
;
591 end
= nsc
->ns_inptr
+ nsc
->ns_inbuflen
;
595 * If we are using TLS, then just read via the BIO. But we still
596 * use our local buffer.
599 if (nsc
->tls_active
) {
600 rc
= BIO_read(nsc
->ssl_io
, end
, remaining
);
606 * Check to see if we're supposed to retry; if so,
607 * then go back and read again.
610 if (BIO_should_retry(nsc
->ssl_io
))
614 * Okay, fine. Get the real error out of the SSL context.
617 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
618 netsec_err(errstr
, "SSL_read() returned 0, but cannot "
619 "retrieve SSL context");
623 errcode
= SSL_get_error(ssl
, rc
);
624 if (errcode
== SSL_ERROR_ZERO_RETURN
) {
625 netsec_err(errstr
, "TLS peer closed remote connection");
627 netsec_err(errstr
, "TLS network read failed: %s",
628 ERR_error_string(ERR_peek_last_error(), NULL
));
631 ERR_print_errors_fp(stderr
);
635 /* Definitely an error */
636 netsec_err(errstr
, "Read on TLS connection failed: %s",
637 ERR_error_string(ERR_get_error(), NULL
));
641 nsc
->ns_inbuflen
+= rc
;
645 #endif /* TLS_SUPPORT */
648 * Okay, time to read some data. Either we're just doing it straight
649 * or we're passing it through sasl_decode() first.
653 if (nsc
->sasl_seclayer
) {
654 readbuf
= nsc
->sasl_tmpbuf
;
655 readbufsize
= nsc
->sasl_maxbufsize
;
657 #endif /* CYRUS_SASL */
659 readbuf
= (char *) end
;
660 readbufsize
= remaining
;
664 * At this point, we should have active data on the connection (see
665 * select() above) so this read SHOULDN'T block. Hopefully.
668 rc
= read(nsc
->ns_readfd
, readbuf
, readbufsize
);
671 netsec_err(errstr
, "Received EOF on network read");
676 netsec_err(errstr
, "Network read failed: %s", strerror(errno
));
681 * Okay, so we've had a successful read. If we are doing SASL security
682 * layers, pass this through sasl_decode(). sasl_decode() can return
683 * 0 bytes decoded; if that happens, jump back to the beginning. Otherwise
684 * we can just update our length pointer.
688 if (nsc
->sasl_seclayer
) {
690 unsigned int tmpoutlen
;
692 rc
= sasl_decode(nsc
->sasl_conn
, nsc
->sasl_tmpbuf
, rc
,
693 &tmpout
, &tmpoutlen
);
696 netsec_err(errstr
, "Unable to decode SASL network data: %s",
697 sasl_errdetail(nsc
->sasl_conn
));
708 if (tmpoutlen
> remaining
) {
709 netsec_err(errstr
, "Internal error: SASL decode buffer overflow!");
713 memcpy(end
, tmpout
, tmpoutlen
);
715 nsc
->ns_inbuflen
+= tmpoutlen
;
717 #endif /* CYRUS_SASL */
718 nsc
->ns_inbuflen
+= rc
;
724 * Write data to our network connection. Really, fill up the buffer as
725 * much as we can, and flush it out if necessary. netsec_flush() does
730 netsec_write(netsec_context
*nsc
, const void *buffer
, size_t size
,
733 const unsigned char *bufptr
= buffer
;
742 * Run a loop copying in data to our local buffer; when we're done with
743 * any buffer overflows then just copy any remaining data in.
746 while ((int) size
>= (remaining
= nsc
->ns_outbufsize
- nsc
->ns_outbuflen
)) {
747 memcpy(nsc
->ns_outptr
, bufptr
, remaining
);
750 * In theory I should increment outptr, but netsec_flush just resets
753 nsc
->ns_outbuflen
= nsc
->ns_outbufsize
;
755 rc
= netsec_flush(nsc
, errstr
);
765 * Copy any leftover data into the buffer.
769 memcpy(nsc
->ns_outptr
, bufptr
, size
);
770 nsc
->ns_outptr
+= size
;
771 nsc
->ns_outbuflen
+= size
;
778 * Our network printf() routine, which really just calls netsec_vprintf().
782 netsec_printf(netsec_context
*nsc
, char **errstr
, const char *format
, ...)
787 va_start(ap
, format
);
788 rc
= netsec_vprintf(nsc
, errstr
, format
, ap
);
795 * Write bytes to the network using printf()-style formatting.
797 * Again, for the most part copy stuff into our buffer to be flushed
802 netsec_vprintf(netsec_context
*nsc
, char **errstr
, const char *format
,
808 * Cheat a little. If we can fit the data into our outgoing buffer,
809 * great! If not, generate a flush and retry once.
813 rc
= vsnprintf((char *) nsc
->ns_outptr
,
814 nsc
->ns_outbufsize
- nsc
->ns_outbuflen
, format
, ap
);
816 if (rc
>= (int) (nsc
->ns_outbufsize
- nsc
->ns_outbuflen
)) {
818 * This means we have an overflow. Note that we don't actually
819 * make use of the terminating NUL, but according to the spec
820 * vsnprintf() won't write to the last byte in the string; that's
821 * why we have to use >= in the comparison above.
823 if (nsc
->ns_outbuffer
== nsc
->ns_outptr
) {
825 * Whoops, if the buffer pointer was the same as the start of the
826 * buffer, that means we overflowed the internal buffer.
827 * At that point, just give up.
829 netsec_err(errstr
, "Internal error: wanted to printf() a total of "
830 "%d bytes, but our buffer size was only %d bytes",
831 rc
, nsc
->ns_outbufsize
);
835 * Generate a flush (which may be inefficient, but hopefully
836 * it isn't) and then try again.
838 if (netsec_flush(nsc
, errstr
) != OK
)
841 * After this, outbuffer should == outptr, so we shouldn't
842 * hit this next time around.
849 if (outlen
> 0 && nsc
->ns_outptr
[outlen
- 1] == '\n') {
851 if (outlen
> 0 && nsc
->ns_outptr
[outlen
- 1] == '\r')
854 nsc
->ns_snoop_noend
= 1;
856 if (outlen
> 0 || nsc
->ns_snoop_noend
== 0) {
858 if (nsc
->sasl_seclayer
)
859 fprintf(stderr
, "(sasl-encrypted) ");
860 #endif /* CYRUS_SASL */
863 fprintf(stderr
, "(tls-encrypted) ");
864 #endif /* TLS_SUPPORT */
865 fprintf(stderr
, "=> ");
866 if (nsc
->ns_snoop_cb
)
867 nsc
->ns_snoop_cb(nsc
, (char *) nsc
->ns_outptr
, outlen
,
868 nsc
->ns_snoop_context
);
870 fprintf(stderr
, "%.*s\n", outlen
, nsc
->ns_outptr
);
872 nsc
->ns_snoop_noend
= 0;
876 nsc
->ns_outptr
+= rc
;
877 nsc
->ns_outbuflen
+= rc
;
883 * Flush out any buffered data in our output buffers. This routine is
884 * actually where the real network writes take place.
888 netsec_flush(netsec_context
*nsc
, char **errstr
)
890 const char *netoutbuf
= (const char *) nsc
->ns_outbuffer
;
891 unsigned int netoutlen
= nsc
->ns_outbuflen
;
902 * If SASL security layers are in effect, run the data through
903 * sasl_encode() first.
906 if (nsc
->sasl_seclayer
) {
907 rc
= sasl_encode(nsc
->sasl_conn
, (const char *) nsc
->ns_outbuffer
,
908 nsc
->ns_outbuflen
, &netoutbuf
, &netoutlen
);
911 netsec_err(errstr
, "SASL data encoding failed: %s",
912 sasl_errdetail(nsc
->sasl_conn
));
917 #endif /* CYRUS_SASL */
920 * If TLS is active, then use those functions to write out the
924 if (nsc
->tls_active
) {
925 if (BIO_write(nsc
->ssl_io
, netoutbuf
, netoutlen
) <= 0) {
926 netsec_err(errstr
, "Error writing to TLS connection: %s",
927 ERR_error_string(ERR_get_error(), NULL
));
931 #endif /* TLS_SUPPORT */
933 rc
= write(nsc
->ns_writefd
, netoutbuf
, netoutlen
);
936 netsec_err(errstr
, "write() failed: %s", strerror(errno
));
941 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
942 nsc
->ns_outbuflen
= 0;
948 * Set various SASL protocol parameters
952 netsec_set_sasl_params(netsec_context
*nsc
, const char *service
,
953 const char *mechanism
, netsec_sasl_callback callback
,
957 sasl_callback_t
*sasl_cbs
;
960 if (!nsc
->ns_hostname
) {
961 netsec_err(errstr
, "Internal error: ns_hostname is NULL");
965 if (! sasl_initialized
) {
966 retval
= sasl_client_init(NULL
);
967 if (retval
!= SASL_OK
) {
968 netsec_err(errstr
, "SASL client initialization failed: %s",
969 sasl_errstring(retval
, NULL
, NULL
));
976 * Allocate an array of SASL callbacks for this connection.
977 * Right now we just allocate an array of four callbacks.
980 sasl_cbs
= mh_xmalloc(sizeof(*sasl_cbs
) * 4);
982 sasl_cbs
[0].id
= SASL_CB_USER
;
983 sasl_cbs
[0].proc
= (sasl_callback_ft
) netsec_get_user
;
984 sasl_cbs
[0].context
= nsc
;
986 sasl_cbs
[1].id
= SASL_CB_AUTHNAME
;
987 sasl_cbs
[1].proc
= (sasl_callback_ft
) netsec_get_user
;
988 sasl_cbs
[1].context
= nsc
;
990 sasl_cbs
[2].id
= SASL_CB_PASS
;
991 sasl_cbs
[2].proc
= (sasl_callback_ft
) netsec_get_password
;
992 sasl_cbs
[2].context
= nsc
;
994 sasl_cbs
[3].id
= SASL_CB_LIST_END
;
995 sasl_cbs
[3].proc
= NULL
;
996 sasl_cbs
[3].context
= NULL
;
998 nsc
->sasl_cbs
= sasl_cbs
;
1000 retval
= sasl_client_new(service
, nsc
->ns_hostname
, NULL
, NULL
,
1001 nsc
->sasl_cbs
, 0, &nsc
->sasl_conn
);
1004 netsec_err(errstr
, "SASL new client allocation failed: %s",
1005 sasl_errstring(retval
, NULL
, NULL
));
1010 * Set up our credentials
1013 nsc
->sasl_creds
= nmh_get_credentials(nsc
->ns_hostname
, nsc
->ns_userid
);
1015 #else /* CYRUS_SASL */
1016 NMH_UNUSED(service
);
1018 #endif /* CYRUS_SASL */
1021 * According to the RFC, mechanisms can only be uppercase letter, numbers,
1022 * and a hyphen or underscore. So make sure we uppercase any letters
1023 * in case the user passed in lowercase.
1028 nsc
->sasl_mech
= mh_xstrdup(mechanism
);
1030 for (p
= nsc
->sasl_mech
; *p
; p
++)
1031 if (isascii((unsigned char) *p
)) /* Leave non-ASCII lower alone. */
1032 *p
= toupper((unsigned char) *p
);
1035 nsc
->sasl_proto_cb
= callback
;
1042 * Our userid callback; return the specified username to the SASL
1043 * library when asked.
1046 int netsec_get_user(void *context
, int id
, const char **result
,
1049 netsec_context
*nsc
= (netsec_context
*) context
;
1051 if (! result
|| (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
))
1052 return SASL_BADPARAM
;
1054 *result
= nmh_cred_get_user(nsc
->sasl_creds
);
1057 *len
= strlen(*result
);
1063 * Retrieve a password and return it to SASL
1067 netsec_get_password(sasl_conn_t
*conn
, void *context
, int id
,
1068 sasl_secret_t
**psecret
)
1070 netsec_context
*nsc
= (netsec_context
*) context
;
1071 const char *password
;
1076 if (! psecret
|| id
!= SASL_CB_PASS
)
1077 return SASL_BADPARAM
;
1079 password
= nmh_cred_get_password(nsc
->sasl_creds
);
1081 len
= strlen(password
);
1084 * sasl_secret_t includes 1 bytes for "data" already, so that leaves
1085 * us room for a terminating NUL
1088 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
1093 (*psecret
)->len
= len
;
1094 strcpy((char *) (*psecret
)->data
, password
);
1096 nsc
->sasl_secret
= *psecret
;
1100 #endif /* CYRUS_SASL */
1103 * Negotiate SASL on this connection
1107 netsec_negotiate_sasl(netsec_context
*nsc
, const char *mechlist
, char **errstr
)
1110 sasl_security_properties_t secprops
;
1111 const char *chosen_mech
;
1112 const unsigned char *saslbuf
;
1113 unsigned char *outbuf
;
1114 unsigned int saslbuflen
, outbuflen
;
1118 #ifdef OAUTH_SUPPORT
1119 unsigned char *xoauth_client_res
;
1120 size_t xoauth_client_res_len
;
1121 #endif /* OAUTH_SUPPORT */
1122 #if defined CYRUS_SASL || defined OAUTH_SUPPORT
1124 #endif /* CYRUS_SASL || OAUTH_SUPPORT */
1127 * If we've been passed a requested mechanism, check our mechanism
1128 * list from the protocol. If it's not supported, return an error.
1131 if (nsc
->sasl_mech
) {
1132 char **str
, *mlist
= getcpy(mechlist
);
1135 str
= brkstring(mlist
, " ", NULL
);
1137 for (i
= 0; str
[i
] != NULL
; i
++) {
1138 if (strcasecmp(nsc
->sasl_mech
, str
[i
]) == 0) {
1143 i
= (str
[i
] == NULL
);
1148 netsec_err(errstr
, "Chosen mechanism %s not supported by server",
1154 #ifdef OAUTH_SUPPORT
1155 if (nsc
->sasl_mech
&& strcasecmp(nsc
->sasl_mech
, "XOAUTH2") == 0) {
1157 * This should be relatively straightforward, but requires some
1158 * help from the plugin. Basically, if XOAUTH2 is a success,
1159 * the callback has to return success, but no output data. If
1160 * there is output data, it will be assumed that it is the JSON
1164 if (! nsc
->oauth_service
) {
1165 netsec_err(errstr
, "Internal error: OAuth2 service name not given");
1169 nsc
->sasl_chosen_mech
= mh_xstrdup(nsc
->sasl_mech
);
1171 if (mh_oauth_do_xoauth(nsc
->ns_userid
, nsc
->oauth_service
,
1172 &xoauth_client_res
, &xoauth_client_res_len
,
1173 nsc
->ns_snoop
? stderr
: NULL
) != OK
) {
1174 netsec_err(errstr
, "Internal error: Unable to get OAuth2 "
1179 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_START
, xoauth_client_res
,
1180 xoauth_client_res_len
, NULL
, 0, errstr
);
1181 free(xoauth_client_res
);
1187 * Okay, we need to do a NETSEC_SASL_FINISH now. If we return
1188 * success, we indicate that with no output data. But if we
1189 * fail, then send a blank message and get the resulting
1193 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0, errstr
);
1197 * We're going to assume the error here is a JSON response;
1198 * we ignore it and send a blank message in response. We should
1199 * then get a failure messages with a useful error. We should
1200 * NOT get a success message at this point.
1203 nsc
->sasl_proto_cb(NETSEC_SASL_WRITE
, NULL
, 0, NULL
, 0, NULL
);
1204 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0,
1207 netsec_err(errstr
, "Unexpected success after OAuth failure!");
1213 #endif /* OAUTH_SUPPORT */
1217 * In netsec_set_sasl_params, we've already done all of our setup with
1218 * sasl_client_init() and sasl_client_new(). So time to set security
1219 * properties, call sasl_client_start(), and generate the protocol
1224 secprops
.maxbufsize
= SASL_MAXRECVBUF
;
1227 * If we're using TLS, do not negotiate a security layer
1232 nsc
->tls_active
? 0 :
1233 #endif /* TLS_SUPPORT */
1236 rc
= sasl_setprop(nsc
->sasl_conn
, SASL_SEC_PROPS
, &secprops
);
1238 if (rc
!= SASL_OK
) {
1239 netsec_err(errstr
, "SASL security property initialization failed: %s",
1240 sasl_errstring(rc
, NULL
, NULL
));
1245 * Start the actual protocol negotiation, and go through the
1246 * sasl_client_step() loop (after sasl_client_start, of course).
1249 rc
= sasl_client_start(nsc
->sasl_conn
,
1250 nsc
->sasl_mech
? nsc
->sasl_mech
: mechlist
, NULL
,
1251 (const char **) &saslbuf
, &saslbuflen
,
1254 if (rc
!= SASL_OK
&& rc
!= SASL_CONTINUE
) {
1255 netsec_err(errstr
, "SASL client start failed: %s",
1256 sasl_errdetail(nsc
->sasl_conn
));
1260 nsc
->sasl_chosen_mech
= getcpy(chosen_mech
);
1262 if (nsc
->sasl_proto_cb(NETSEC_SASL_START
, saslbuf
, saslbuflen
, NULL
, 0,
1267 * We've written out our first message; enter in the step loop
1270 while (rc
== SASL_CONTINUE
) {
1272 * Call our SASL callback, which will handle the details of
1273 * reading data from the network.
1276 if (nsc
->sasl_proto_cb(NETSEC_SASL_READ
, NULL
, 0, &outbuf
, &outbuflen
,
1278 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0, NULL
);
1282 rc
= sasl_client_step(nsc
->sasl_conn
, (char *) outbuf
, outbuflen
, NULL
,
1283 (const char **) &saslbuf
, &saslbuflen
);
1287 if (rc
!= SASL_OK
&& rc
!= SASL_CONTINUE
) {
1288 netsec_err(errstr
, "SASL client negotiation failed: %s",
1289 sasl_errdetail(nsc
->sasl_conn
));
1290 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0, NULL
);
1294 if (nsc
->sasl_proto_cb(NETSEC_SASL_WRITE
, saslbuf
, saslbuflen
,
1295 NULL
, 0, errstr
) != OK
) {
1296 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0, NULL
);
1302 * SASL exchanges should be complete, process the final response message
1306 if (nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0,
1309 * At this point we can't really send an abort since the SASL dialog
1310 * has completed, so just bubble back up the error message.
1317 * At this point, SASL should be complete. Get a few properties
1318 * from the authentication exchange.
1321 rc
= sasl_getprop(nsc
->sasl_conn
, SASL_SSF
, (const void **) &ssf
);
1323 if (rc
!= SASL_OK
) {
1324 netsec_err(errstr
, "Cannot retrieve SASL negotiated security "
1325 "strength factor: %s", sasl_errstring(rc
, NULL
, NULL
));
1329 nsc
->sasl_ssf
= *ssf
;
1331 if (nsc
->sasl_ssf
> 0) {
1332 rc
= sasl_getprop(nsc
->sasl_conn
, SASL_MAXOUTBUF
,
1333 (const void **) &outbufmax
);
1335 if (rc
!= SASL_OK
) {
1336 netsec_err(errstr
, "Cannot retrieve SASL negotiated output "
1337 "buffer size: %s", sasl_errstring(rc
, NULL
, NULL
));
1342 * If our output buffer isn't the same size as the input buffer,
1343 * reallocate it and set the new size (since we won't encode any
1344 * data larger than that).
1347 nsc
->sasl_maxbufsize
= *outbufmax
;
1349 if (nsc
->ns_outbufsize
!= nsc
->sasl_maxbufsize
) {
1350 nsc
->ns_outbufsize
= nsc
->sasl_maxbufsize
;
1351 nsc
->ns_outbuffer
= mh_xrealloc(nsc
->ns_outbuffer
,
1352 nsc
->ns_outbufsize
);
1354 * There shouldn't be any data in the buffer, but for
1355 * consistency's sake discard it.
1357 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
1358 nsc
->ns_outbuflen
= 0;
1362 * Allocate a buffer to do temporary reads into, before we
1363 * call sasl_decode()
1366 nsc
->sasl_tmpbuf
= mh_xmalloc(nsc
->sasl_maxbufsize
);
1369 * Okay, this is a bit weird. Make sure that the input buffer
1370 * is at least TWICE the size of the max buffer size. That's
1371 * because if we're consuming data but want to extend the current
1372 * buffer, we want to be sure there's room for another full buffer's
1376 if (nsc
->ns_inbufsize
< nsc
->sasl_maxbufsize
* 2) {
1377 size_t offset
= nsc
->ns_inptr
- nsc
->ns_inbuffer
;
1378 nsc
->ns_inbufsize
= nsc
->sasl_maxbufsize
* 2;
1379 nsc
->ns_inbuffer
= mh_xrealloc(nsc
->ns_inbuffer
, nsc
->ns_inbufsize
);
1380 nsc
->ns_inptr
= nsc
->ns_inbuffer
+ offset
;
1383 nsc
->sasl_seclayer
= 1;
1389 * If we're at this point, then either we have NEITHER OAuth2 or
1390 * Cyrus-SASL compiled in, or have OAuth2 but didn't give the XOAUTH2
1391 * mechanism on the command line.
1394 if (! nsc
->sasl_mech
)
1395 netsec_err(errstr
, "SASL library support not available; please "
1396 "specify a SASL mechanism to use");
1398 netsec_err(errstr
, "No support for the %s SASL mechanism",
1402 #endif /* CYRUS_SASL */
1406 * Retrieve our chosen SASL mechanism
1410 netsec_get_sasl_mechanism(netsec_context
*nsc
)
1412 return nsc
->sasl_chosen_mech
;
1416 * Set an OAuth2 service name, if we support it.
1420 netsec_set_oauth_service(netsec_context
*nsc
, const char *service
)
1422 #ifdef OAUTH_SUPPORT
1423 nsc
->oauth_service
= getcpy(service
);
1425 #else /* OAUTH_SUPPORT */
1427 NMH_UNUSED(service
);
1429 #endif /* OAUTH_SUPPORT */
1433 * Initialize (and enable) TLS for this connection
1437 netsec_set_tls(netsec_context
*nsc
, int tls
, int noverify
, char **errstr
)
1442 BIO
*rbio
, *wbio
, *ssl_bio
;
1444 if (! tls_initialized
) {
1446 SSL_load_error_strings();
1449 * Create the SSL context; this has the properties for all
1450 * SSL connections (we are only doing one), though. Make sure
1451 * we only support secure (known as of now) TLS protocols.
1454 sslctx
= SSL_CTX_new(SSLv23_client_method());
1457 netsec_err(errstr
, "Unable to initialize OpenSSL context: %s",
1458 ERR_error_string(ERR_get_error(), NULL
));
1462 SSL_CTX_set_options(sslctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1465 if (!SSL_CTX_set_default_verify_paths(sslctx
)) {
1466 netsec_err(errstr
, "Unable to set default certificate "
1467 "verification paths: %s",
1468 ERR_error_string(ERR_get_error(), NULL
));
1475 if (nsc
->ns_readfd
== -1 || nsc
->ns_writefd
== -1) {
1476 netsec_err(errstr
, "Invalid file descriptor in netsec context");
1481 * Create the SSL structure which holds the data for a single
1485 ssl
= SSL_new(sslctx
);
1488 netsec_err(errstr
, "Unable to create SSL connection: %s",
1489 ERR_error_string(ERR_get_error(), NULL
));
1494 * Never bother us, since we are using blocking sockets.
1497 SSL_set_mode(ssl
, SSL_MODE_AUTO_RETRY
);
1500 * This is a bit weird, so pay attention.
1502 * We create a socket BIO, and bind it to our SSL connection.
1503 * That means reads and writes to the SSL connection will use our
1506 * Then we create an SSL BIO, and assign our current SSL connection
1507 * to it. This is done so our code stays simple if we want to use
1508 * any buffering BIOs (right now we do our own buffering).
1509 * So the chain looks like:
1511 * SSL BIO -> socket BIO.
1514 rbio
= BIO_new_socket(nsc
->ns_readfd
, BIO_CLOSE
);
1517 netsec_err(errstr
, "Unable to create a read socket BIO: %s",
1518 ERR_error_string(ERR_get_error(), NULL
));
1523 wbio
= BIO_new_socket(nsc
->ns_writefd
, BIO_CLOSE
);
1526 netsec_err(errstr
, "Unable to create a write socket BIO: %s",
1527 ERR_error_string(ERR_get_error(), NULL
));
1533 SSL_set_bio(ssl
, rbio
, wbio
);
1534 SSL_set_connect_state(ssl
);
1537 * If noverify is NOT set, then do certificate validation.
1538 * Turning on SSL_VERIFY_PEER will verify the certificate chain
1539 * against locally stored root certificates (the locations are
1540 * set using SSL_CTX_set_default_verify_paths()), and we put
1541 * the hostname in the X509 verification parameters so the OpenSSL
1542 * code will verify that the hostname appears in the server
1547 #ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
1548 X509_VERIFY_PARAM
*param
;
1549 #endif /* HAVE_X509_VERIFY_PARAM_SET1_HOST */
1551 SSL_set_verify(ssl
, SSL_VERIFY_PEER
, NULL
);
1552 if (! nsc
->ns_hostname
) {
1553 netsec_err(errstr
, "Internal error: hostname not set and "
1554 "certification verification enabled");
1559 #ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
1560 param
= SSL_get0_param(ssl
);
1562 if (! X509_VERIFY_PARAM_set1_host(param
, nsc
->ns_hostname
, 0)) {
1563 netsec_err(errstr
, "Unable to add hostname %s to cert "
1564 "verification parameters: %s", nsc
->ns_hostname
,
1565 ERR_error_string(ERR_get_error(), NULL
));
1569 #endif /* HAVE_X509_VERIFY_PARAM_SET1_HOST */
1572 ssl_bio
= BIO_new(BIO_f_ssl());
1575 netsec_err(errstr
, "Unable to create a SSL BIO: %s",
1576 ERR_error_string(ERR_get_error(), NULL
));
1581 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
1582 nsc
->ssl_io
= ssl_bio
;
1585 * Since SSL now owns these file descriptors, have it handle the
1586 * closing of them instead of netsec_shutdown().
1589 nsc
->ns_noclose
= 1;
1593 BIO_free_all(nsc
->ssl_io
);
1596 #else /* TLS_SUPPORT */
1598 NMH_UNUSED(noverify
);
1601 netsec_err(errstr
, "TLS is not supported");
1604 #endif /* TLS_SUPPORT */
1610 * Start TLS negotiation on this connection
1614 netsec_negotiate_tls(netsec_context
*nsc
, char **errstr
)
1617 if (! nsc
->ssl_io
) {
1618 netsec_err(errstr
, "TLS has not been configured for this connection");
1622 if (BIO_do_handshake(nsc
->ssl_io
) < 1) {
1623 unsigned long errcode
= ERR_get_error();
1626 * Print a more detailed message if it was certificate verification
1630 if (ERR_GET_LIB(errcode
) == ERR_LIB_SSL
&&
1631 ERR_GET_REASON(errcode
) == SSL_R_CERTIFICATE_VERIFY_FAILED
) {
1634 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
1635 netsec_err(errstr
, "Certificate verification failed, but "
1636 "cannot retrieve SSL handle: %s",
1637 ERR_error_string(ERR_get_error(), NULL
));
1639 netsec_err(errstr
, "Server certificate verification failed: %s",
1640 X509_verify_cert_error_string(
1641 SSL_get_verify_result(ssl
)));
1644 netsec_err(errstr
, "TLS negotiation failed: %s",
1645 ERR_error_string(errcode
, NULL
));
1649 * Because negotiation failed, shut down TLS so we don't get any
1650 * garbage on the connection. Because of weirdness with SSL_shutdown,
1651 * we end up calling it twice: once explicitly, once as part of
1655 BIO_ssl_shutdown(nsc
->ssl_io
);
1656 BIO_free_all(nsc
->ssl_io
);
1662 if (nsc
->ns_snoop
) {
1665 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
1666 fprintf(stderr
, "WARNING: cannot determine SSL ciphers\n");
1668 const SSL_CIPHER
*cipher
= SSL_get_current_cipher(ssl
);
1669 fprintf(stderr
, "TLS negotiation successful: %s(%d) %s\n",
1670 SSL_CIPHER_get_name(cipher
),
1671 SSL_CIPHER_get_bits(cipher
, NULL
),
1672 SSL_CIPHER_get_version(cipher
));
1673 SSL_SESSION_print_fp(stderr
, SSL_get_session(ssl
));
1677 nsc
->tls_active
= 1;
1680 #else /* TLS_SUPPORT */
1682 netsec_err(errstr
, "TLS not supported");
1685 #endif /* TLS_SUPPORT */
1689 * Generate an (allocated) error string
1693 netsec_err(char **errstr
, const char *fmt
, ...)
1697 char *errbuf
= NULL
;
1704 errbufsize
= rc
+ 1;
1705 errbuf
= mh_xrealloc(errbuf
, errbufsize
);
1707 rc
= vsnprintf(errbuf
, errbufsize
, fmt
, ap
);
1709 } while (rc
>= (int) errbufsize
);