ssl-proxy-openssl.c revision fa02962b74d39e8d74c4c307c0210791b2f0a1ca
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "login-common.h"
80c1d98d3638b71e57a39cafa88b9122bf8169c6Timo Sirainen#include "array.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ioloop.h"
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen#include "network.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ostream.h"
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen#include "read-full.h"
50031a6b36a6051512bd18f39e4bbabe54acf565Timo Sirainen#include "safe-memset.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "hash.h"
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen#include "llist.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "master-interface.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "client-common.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ssl-proxy.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <fcntl.h>
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include <unistd.h>
8887bf3757d51d73887dd20b1db3334d867d3817Timo Sirainen#include <sys/stat.h>
8887bf3757d51d73887dd20b1db3334d867d3817Timo Sirainen
369a1084c500a9df7448ffa9409ce32e42060bc2Timo Sirainen#ifdef HAVE_OPENSSL
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen#include "iostream-openssl.h"
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen#include <openssl/crypto.h>
c53e8ee216904ffe6de4f6518d9f9f5107b7610eTimo Sirainen#include <openssl/engine.h>
589a9c6e8ee22071c14171c04bfc6bfe17121871Timo Sirainen#include <openssl/x509.h>
589a9c6e8ee22071c14171c04bfc6bfe17121871Timo Sirainen#include <openssl/pem.h>
ef5fb27361cc5e15766e85e28355750ff04b13c9Timo Sirainen#include <openssl/ssl.h>
ef5fb27361cc5e15766e85e28355750ff04b13c9Timo Sirainen#include <openssl/err.h>
ef5fb27361cc5e15766e85e28355750ff04b13c9Timo Sirainen#include <openssl/rand.h>
ef5fb27361cc5e15766e85e28355750ff04b13c9Timo Sirainen
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen/* Check every 30 minutes if parameters file has been updated */
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen#define SSL_PARAMFILE_CHECK_INTERVAL (60*30)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define SSL_PARAMETERS_PATH "ssl-params"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME /* FIXME: this may be unnecessary.. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# undef HAVE_SSL_GET_SERVERNAME
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen#endif
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
6ec7cf71ccd0eed1f9cc1b0bda8960796b04160bTimo Sirainenenum ssl_io_action {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen SSL_ADD_INPUT,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen SSL_REMOVE_INPUT,
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen SSL_ADD_OUTPUT,
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainen SSL_REMOVE_OUTPUT
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainen};
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainen
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainenstruct ssl_proxy {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int refcount;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct ssl_proxy *prev, *next;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen SSL *ssl;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct client *client;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct ip_addr ip;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct login_settings *set;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int fd_ssl, fd_plain;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct io *io_ssl_read, *io_ssl_write, *io_plain_read, *io_plain_write;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen unsigned char plainout_buf[1024];
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen unsigned int plainout_size;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen unsigned char sslout_buf[1024];
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int sslout_size;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen ssl_handshake_callback_t *handshake_callback;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen void *handshake_context;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char *last_error;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int handshaked:1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int destroyed:1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int cert_received:1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int cert_broken:1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int client_proxy:1;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct ssl_parameters {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *path;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen time_t last_refresh;
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainen int fd;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DH *dh_512, *dh_1024;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainenstruct ssl_server_context {
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen SSL_CTX *ctx;
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen pool_t pool;
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *cert;
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen const char *key;
2a734f36105e33ab452d057df6bc7a2b7d9f96f0Timo Sirainen const char *ca;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *cipher_list;
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen const char *protocols;
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen bool verify_client_cert;
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int extdata_index;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct hash_table *ssl_servers;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic SSL_CTX *ssl_client_ctx;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic unsigned int ssl_proxy_count;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct ssl_proxy *ssl_proxies;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct ssl_parameters ssl_params;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic int ssl_username_nid;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic ENGINE *ssl_engine;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainenstatic void plain_read(struct ssl_proxy *proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void ssl_read(struct ssl_proxy *proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void ssl_write(struct ssl_proxy *proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void ssl_step(struct ssl_proxy *proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void ssl_proxy_destroy(struct ssl_proxy *proxy);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void ssl_proxy_unref(struct ssl_proxy *proxy);
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen
b9c44feadade0481b957f2978640afb3317bd1dfTimo Sirainenstatic struct ssl_server_context *
b9c44feadade0481b957f2978640afb3317bd1dfTimo Sirainenssl_server_context_init(const struct login_settings *set);
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainenstatic void ssl_server_context_deinit(struct ssl_server_context **_ctx);
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainenstatic unsigned int ssl_server_context_hash(const void *p)
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen{
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen const struct ssl_server_context *ctx = p;
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen unsigned int i, g, h = 0;
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen /* checking for different certs is typically good enough,
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen and it should be enough to check only the first few bytes. */
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen for (i = 0; i < 16 && ctx->cert[i] != '\0'; i++) {
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen h = (h << 4) + ctx->cert[i];
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen if ((g = h & 0xf0000000UL)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen h = h ^ (g >> 24);
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen h = h ^ g;
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen }
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen }
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen return h;
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen}
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int ssl_server_context_cmp(const void *p1, const void *p2)
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen{
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen const struct ssl_server_context *ctx1 = p1, *ctx2 = p2;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen if (strcmp(ctx1->cert, ctx2->cert) != 0)
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen return 1;
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen if (strcmp(ctx1->key, ctx2->key) != 0)
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen return 1;
c8adec8db635f5efb13b9879a5f3fb523abdc969Timo Sirainen if (null_strcmp(ctx1->cipher_list, ctx2->cipher_list) != 0)
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen return 1;
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (null_strcmp(ctx1->protocols, ctx2->protocols) != 0)
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen return 1;
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen
807b48fe1f6a57b01ed2cc20247d5b5e3facc562Timo Sirainen return ctx1->verify_client_cert == ctx2->verify_client_cert ? 0 : 1;
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen}
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen
7761758f43d6150be4b07f4c54457ce662f78c4cTimo Sirainenstatic void ssl_params_corrupted(void)
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen{
62f4a199b5c9a0862f486cbf18e195cc621bbe25Timo Sirainen i_fatal("Corrupted SSL parameters file: "
62f4a199b5c9a0862f486cbf18e195cc621bbe25Timo Sirainen PKG_STATEDIR"/ssl-parameters.dat");
62f4a199b5c9a0862f486cbf18e195cc621bbe25Timo Sirainen}
62f4a199b5c9a0862f486cbf18e195cc621bbe25Timo Sirainen
62f4a199b5c9a0862f486cbf18e195cc621bbe25Timo Sirainenstatic void read_next(struct ssl_parameters *params, void *data, size_t size)
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if ((ret = read_full(params->fd, data, size)) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_fatal("read(%s) failed: %m", params->path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ssl_params_corrupted();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainenstatic bool read_dh_parameters_next(struct ssl_parameters *params)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen unsigned char *buf;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const unsigned char *cbuf;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int len;
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen int bits;
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen /* read bit size. 0 ends the DH parameters list. */
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen read_next(params, &bits, sizeof(bits));
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen
020a39a395d2adb768e0179631b37bc78ecd9471Timo Sirainen if (bits == 0)
020a39a395d2adb768e0179631b37bc78ecd9471Timo Sirainen return FALSE;
1f43c8ac132c153c224c4fffe34b2c3075d87ef7Timo Sirainen
b9c44feadade0481b957f2978640afb3317bd1dfTimo Sirainen /* read data size. */
b9c44feadade0481b957f2978640afb3317bd1dfTimo Sirainen read_next(params, &len, sizeof(len));
b9c44feadade0481b957f2978640afb3317bd1dfTimo Sirainen if (len > 1024*100) /* should be enough? */
020a39a395d2adb768e0179631b37bc78ecd9471Timo Sirainen ssl_params_corrupted();
020a39a395d2adb768e0179631b37bc78ecd9471Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen buf = i_malloc(len);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen read_next(params, buf, len);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen cbuf = buf;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (bits) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case 512:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen params->dh_512 = d2i_DHparams(NULL, &cbuf, len);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen case 1024:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen params->dh_1024 = d2i_DHparams(NULL, &cbuf, len);
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen break;
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen default:
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen ssl_params_corrupted();
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen }
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen i_free(buf);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen return TRUE;
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen}
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainenstatic void ssl_free_parameters(struct ssl_parameters *params)
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen{
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen if (params->dh_512 != NULL) {
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen DH_free(params->dh_512);
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen params->dh_512 = NULL;
41e6eb07b411ea58352ba9d2cc8cf340325d49f3Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (params->dh_1024 != NULL) {
4d4d585520538a752e9f0a4a1c019a2918f52e56Timo Sirainen DH_free(params->dh_1024);
4d4d585520538a752e9f0a4a1c019a2918f52e56Timo Sirainen params->dh_1024 = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
15cc66ca72982a43e3bfa58f307adc57e9caa52dTimo Sirainen}
15cc66ca72982a43e3bfa58f307adc57e9caa52dTimo Sirainen
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainenstatic void ssl_refresh_parameters(struct ssl_parameters *params)
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen{
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen char c;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (params->last_refresh > ioloop_time - SSL_PARAMFILE_CHECK_INTERVAL)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen params->last_refresh = ioloop_time;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen params->fd = net_connect_unix(params->path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (params->fd == -1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_error("connect(%s) failed: %m", params->path);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen return;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen net_set_nonblock(params->fd, FALSE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ssl_free_parameters(params);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (read_dh_parameters_next(params)) ;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen if ((ret = read_full(params->fd, &c, 1)) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_fatal("read(%s) failed: %m", params->path);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen else if (ret != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* more data than expected */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ssl_params_corrupted();
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen }
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen if (close(params->fd) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_error("close(%s) failed: %m", params->path);
7c95b03620a03a43dd72d39608cea5fc77393ad6Timo Sirainen params->fd = -1;
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen}
9404a7b90dcb80d31bd37ee2493f03751acdb1bdTimo Sirainen
9404a7b90dcb80d31bd37ee2493f03751acdb1bdTimo Sirainenstatic void ssl_set_io(struct ssl_proxy *proxy, enum ssl_io_action action)
9404a7b90dcb80d31bd37ee2493f03751acdb1bdTimo Sirainen{
9404a7b90dcb80d31bd37ee2493f03751acdb1bdTimo Sirainen switch (action) {
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen case SSL_ADD_INPUT:
eac3948d67eff8623d51aeaea9eca582f3aec677Timo Sirainen if (proxy->io_ssl_read != NULL)
2674b4f0cf8f3c203d8e56b29735f5e267038dafTimo Sirainen break;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen proxy->io_ssl_read = io_add(proxy->fd_ssl, IO_READ,
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen ssl_step, proxy);
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen break;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen case SSL_REMOVE_INPUT:
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen if (proxy->io_ssl_read != NULL)
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen io_remove(&proxy->io_ssl_read);
e376693bfa3985232c41df99c7010fca22612c89Timo Sirainen break;
48136ae5a0eb49daa44e343553f3688a500307e2Timo Sirainen case SSL_ADD_OUTPUT:
48136ae5a0eb49daa44e343553f3688a500307e2Timo Sirainen if (proxy->io_ssl_write != NULL)
2674b4f0cf8f3c203d8e56b29735f5e267038dafTimo Sirainen break;
e376693bfa3985232c41df99c7010fca22612c89Timo Sirainen proxy->io_ssl_write = io_add(proxy->fd_ssl, IO_WRITE,
e376693bfa3985232c41df99c7010fca22612c89Timo Sirainen ssl_step, proxy);
e376693bfa3985232c41df99c7010fca22612c89Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SSL_REMOVE_OUTPUT:
97735b96d442568f65efa20f8c292a496498c17bTimo Sirainen if (proxy->io_ssl_write != NULL)
97735b96d442568f65efa20f8c292a496498c17bTimo Sirainen io_remove(&proxy->io_ssl_write);
97735b96d442568f65efa20f8c292a496498c17bTimo Sirainen break;
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen }
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen}
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainenstatic void plain_block_input(struct ssl_proxy *proxy, bool block)
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen{
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen if (block) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (proxy->io_plain_read != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen io_remove(&proxy->io_plain_read);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen if (proxy->io_plain_read == NULL) {
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen proxy->io_plain_read = io_add(proxy->fd_plain, IO_READ,
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen plain_read, proxy);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen }
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainenstatic void plain_read(struct ssl_proxy *proxy)
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainen{
db5164c9a1129af0cfb11fc18d88da361a8011fbTimo Sirainen ssize_t ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen bool corked = FALSE;
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen if (proxy->sslout_size == sizeof(proxy->sslout_buf)) {
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen /* buffer full, block input until it's written */
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen plain_block_input(proxy, TRUE);
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen return;
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen }
d152ccd0d29fae1bc6092bf198ee7eb843202f96Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen proxy->refcount++;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (proxy->sslout_size < sizeof(proxy->sslout_buf) &&
aa0647f2debf0d48d504a321186f66c85596aaf4Timo Sirainen !proxy->destroyed) {
2d39dc1a453546892109b35c0d9770369011a13dTimo Sirainen ret = net_receive(proxy->fd_plain,
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen proxy->sslout_buf + proxy->sslout_size,
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen sizeof(proxy->sslout_buf) -
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen proxy->sslout_size);
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen if (ret <= 0) {
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen if (ret < 0)
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen ssl_proxy_destroy(proxy);
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen break;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen } else {
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen proxy->sslout_size += ret;
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen if (!corked) {
8f8315e4b4e27ead12dd1c3da65bf4dee3762f18Timo Sirainen net_set_cork(proxy->fd_ssl, TRUE);
768b7f5783c8de119d7386321e5d0c72d5c2d9f6Timo Sirainen corked = TRUE;
768b7f5783c8de119d7386321e5d0c72d5c2d9f6Timo Sirainen }
768b7f5783c8de119d7386321e5d0c72d5c2d9f6Timo Sirainen ssl_write(proxy);
079f54c97145a0a5daa36c37eead3eae91b67a1eTimo Sirainen }
079f54c97145a0a5daa36c37eead3eae91b67a1eTimo Sirainen }
079f54c97145a0a5daa36c37eead3eae91b67a1eTimo Sirainen
079f54c97145a0a5daa36c37eead3eae91b67a1eTimo Sirainen if (corked)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen net_set_cork(proxy->fd_ssl, FALSE);
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen
b08b33d1f5ce3721dc2d83586c9cb0ca141331fdTimo Sirainen ssl_proxy_unref(proxy);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen}
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
89caf81340a4da959ef18c5f9b9c99824a53066bTimo Sirainenstatic void plain_write(struct ssl_proxy *proxy)
89caf81340a4da959ef18c5f9b9c99824a53066bTimo Sirainen{
89caf81340a4da959ef18c5f9b9c99824a53066bTimo Sirainen ssize_t ret;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen proxy->refcount++;
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen ret = net_transmit(proxy->fd_plain, proxy->plainout_buf,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen proxy->plainout_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret < 0)
685393de106e55b61f754d420e378d05bd462ebbTimo Sirainen ssl_proxy_destroy(proxy);
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen else {
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen proxy->plainout_size -= ret;
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen memmove(proxy->plainout_buf, proxy->plainout_buf + ret,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen proxy->plainout_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (proxy->plainout_size > 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (proxy->io_plain_write == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen proxy->io_plain_write =
5a86309a6c58bdeb9921bf2989aaabaaa04a29abTimo Sirainen io_add(proxy->fd_plain, IO_WRITE,
5a86309a6c58bdeb9921bf2989aaabaaa04a29abTimo Sirainen plain_write, proxy);
5a86309a6c58bdeb9921bf2989aaabaaa04a29abTimo Sirainen }
c979eeda1f46483d9c963e265786b701d7683d77Timo Sirainen } else {
c979eeda1f46483d9c963e265786b701d7683d77Timo Sirainen if (proxy->io_plain_write != NULL)
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen io_remove(&proxy->io_plain_write);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen
17da42c31202b1b3e7e308121ea17d922c24da1bTimo Sirainen ssl_set_io(proxy, SSL_ADD_INPUT);
17da42c31202b1b3e7e308121ea17d922c24da1bTimo Sirainen if (SSL_pending(proxy->ssl) > 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ssl_read(proxy);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
b20fb5b1df9d604a7541f5118fc5b4b466d211efTimo Sirainen
b20fb5b1df9d604a7541f5118fc5b4b466d211efTimo Sirainen ssl_proxy_unref(proxy);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen}
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic const char *ssl_err2str(unsigned long err, const char *data, int flags)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen{
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen const char *ret;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen char *buf;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen size_t err_size = 256;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
bb26f09873c18f342cd1ab2d0ee0b9018e6546d9Timo Sirainen buf = t_malloc(err_size);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen buf[err_size-1] = '\0';
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen ERR_error_string_n(err, buf, err_size-1);
c4db5f0fb7cea8160dc10b5f0ab57ab7c02bf8a5Timo Sirainen ret = buf;
c4db5f0fb7cea8160dc10b5f0ab57ab7c02bf8a5Timo Sirainen
c4db5f0fb7cea8160dc10b5f0ab57ab7c02bf8a5Timo Sirainen if ((flags & ERR_TXT_STRING) != 0)
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen ret = t_strdup_printf("%s: %s", buf, data);
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen return ret;
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen}
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainenstatic const char *ssl_last_error(void)
dd9712b013e5a14939deed84b2e391d89897d2cfTimo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned long err;
b5ea11802f2bafbec06282a7b3b6704dc5fae584Timo Sirainen const char *data;
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen int flags;
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen err = ERR_get_error_line_data(NULL, NULL, &data, &flags);
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen while (err != 0 && ERR_peek_error() != 0) {
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen i_error("SSL: Stacked error: %s",
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen ssl_err2str(err, data, flags));
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen err = ERR_get_error();
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen }
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen if (err == 0) {
3da614c39dd29f536c485089e67839b4cf89fed3Timo Sirainen if (errno != 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return strerror(errno);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return "Unknown error";
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen }
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen return ssl_err2str(err, data, flags);
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void ssl_handle_error(struct ssl_proxy *proxy, int ret,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *func_name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen const char *errstr = NULL;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen int err;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen proxy->refcount++;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_free_and_null(proxy->last_error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen err = SSL_get_error(proxy->ssl, ret);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (err) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case SSL_ERROR_WANT_READ:
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ssl_set_io(proxy, SSL_ADD_INPUT);
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen break;
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen case SSL_ERROR_WANT_WRITE:
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen ssl_set_io(proxy, SSL_ADD_OUTPUT);
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen break;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen case SSL_ERROR_SYSCALL:
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen /* eat up the error queue */
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen if (ERR_peek_error() != 0)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen errstr = ssl_last_error();
6825360d446542046757b06064282301c4c6b27cTimo Sirainen else if (ret != 0)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen errstr = strerror(errno);
6825360d446542046757b06064282301c4c6b27cTimo Sirainen else {
6825360d446542046757b06064282301c4c6b27cTimo Sirainen /* EOF. */
6825360d446542046757b06064282301c4c6b27cTimo Sirainen errstr = "Disconnected";
6825360d446542046757b06064282301c4c6b27cTimo Sirainen break;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen errstr = t_strdup_printf("%s syscall failed: %s",
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen func_name, errstr);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen break;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen case SSL_ERROR_ZERO_RETURN:
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen /* clean connection closing */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ssl_proxy_destroy(proxy);
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen break;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen case SSL_ERROR_SSL:
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE) {
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen i_error("OpenSSL malloc() failed. "
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen "You may need to increase login_process_size");
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen }
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen errstr = t_strdup_printf("%s failed: %s",
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen func_name, ssl_last_error());
61f5256ef248d35459b53534ae428bf6d016e1c5Timo Sirainen break;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen default:
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen errstr = t_strdup_printf("%s failed: unknown failure %d (%s)",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen func_name, err, ssl_last_error());
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen break;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen if (errstr != NULL) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen proxy->last_error = i_strdup(errstr);
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen ssl_proxy_destroy(proxy);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ssl_proxy_unref(proxy);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void ssl_handshake(struct ssl_proxy *proxy)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (proxy->client_proxy) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ret = SSL_connect(proxy->ssl);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret != 1) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ssl_handle_error(proxy, ret, "SSL_connect()");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen } else {
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen ret = SSL_accept(proxy->ssl);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret != 1) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ssl_handle_error(proxy, ret, "SSL_accept()");
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_free_and_null(proxy->last_error);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen proxy->handshaked = TRUE;
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen ssl_set_io(proxy, SSL_ADD_INPUT);
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen plain_block_input(proxy, FALSE);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (proxy->handshake_callback != NULL) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (proxy->handshake_callback(proxy->handshake_context) < 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ssl_proxy_destroy(proxy);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainenstatic void ssl_read(struct ssl_proxy *proxy)
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen{
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen int ret;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen while (proxy->plainout_size < sizeof(proxy->plainout_buf) &&
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen !proxy->destroyed) {
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen ret = SSL_read(proxy->ssl,
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen proxy->plainout_buf + proxy->plainout_size,
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen sizeof(proxy->plainout_buf) -
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen proxy->plainout_size);
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen if (ret <= 0) {
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen ssl_handle_error(proxy, ret, "SSL_read()");
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen break;
6e07b4251bf6a3cf34019c351a32a65c08392e58Timo Sirainen } else {
893e5bbd5184ec5c21f47c67c8ea6efbea41f7d0Timo Sirainen i_free_and_null(proxy->last_error);
893e5bbd5184ec5c21f47c67c8ea6efbea41f7d0Timo Sirainen proxy->plainout_size += ret;
5278c93bd7105c32ac7ec37f36015d5950f6cbcaTimo Sirainen plain_write(proxy);
5278c93bd7105c32ac7ec37f36015d5950f6cbcaTimo Sirainen }
44c5e644cb413a6559bf2d4179cbe48f9a82f366Timo Sirainen }
5278c93bd7105c32ac7ec37f36015d5950f6cbcaTimo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic void ssl_write(struct ssl_proxy *proxy)
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen{
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen int ret;
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen ret = SSL_write(proxy->ssl, proxy->sslout_buf, proxy->sslout_size);
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen if (ret <= 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ssl_handle_error(proxy, ret, "SSL_write()");
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen else {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen i_free_and_null(proxy->last_error);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen proxy->sslout_size -= ret;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen memmove(proxy->sslout_buf, proxy->sslout_buf + ret,
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen proxy->sslout_size);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen ssl_set_io(proxy, proxy->sslout_size > 0 ?
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen SSL_ADD_OUTPUT : SSL_REMOVE_OUTPUT);
bb2b91b4c5363348b737237893d414639510a561Timo Sirainen plain_block_input(proxy, FALSE);
bb2b91b4c5363348b737237893d414639510a561Timo Sirainen }
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen}
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic void ssl_step(struct ssl_proxy *proxy)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen proxy->refcount++;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!proxy->handshaked)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ssl_handshake(proxy);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (proxy->handshaked) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (proxy->plainout_size == sizeof(proxy->plainout_buf))
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ssl_set_io(proxy, SSL_REMOVE_INPUT);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen else
b7a5580abb62dd82e82e58ed81dcb307ed338144Timo Sirainen ssl_read(proxy);
b7a5580abb62dd82e82e58ed81dcb307ed338144Timo Sirainen
b7a5580abb62dd82e82e58ed81dcb307ed338144Timo Sirainen if (proxy->sslout_size == 0)
b7a5580abb62dd82e82e58ed81dcb307ed338144Timo Sirainen ssl_set_io(proxy, SSL_REMOVE_OUTPUT);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen net_set_cork(proxy->fd_ssl, TRUE);
ssl_write(proxy);
net_set_cork(proxy->fd_ssl, FALSE);
}
}
ssl_proxy_unref(proxy);
}
static int
ssl_proxy_alloc_common(SSL_CTX *ssl_ctx, int fd, const struct ip_addr *ip,
const struct login_settings *set,
struct ssl_proxy **proxy_r)
{
struct ssl_proxy *proxy;
SSL *ssl;
int sfd[2];
i_assert(fd != -1);
*proxy_r = NULL;
if (!ssl_initialized) {
i_error("SSL support not enabled in configuration");
return -1;
}
ssl_refresh_parameters(&ssl_params);
ssl = SSL_new(ssl_ctx);
if (ssl == NULL) {
i_error("SSL_new() failed: %s", ssl_last_error());
return -1;
}
if (SSL_set_fd(ssl, fd) != 1) {
i_error("SSL_set_fd() failed: %s", ssl_last_error());
SSL_free(ssl);
return -1;
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0) {
i_error("socketpair() failed: %m");
SSL_free(ssl);
return -1;
}
net_set_nonblock(sfd[0], TRUE);
net_set_nonblock(sfd[1], TRUE);
net_set_nonblock(fd, TRUE);
proxy = i_new(struct ssl_proxy, 1);
proxy->refcount = 2;
proxy->ssl = ssl;
proxy->set = set;
proxy->fd_ssl = fd;
proxy->fd_plain = sfd[0];
proxy->ip = *ip;
SSL_set_ex_data(ssl, extdata_index, proxy);
ssl_proxy_count++;
DLLIST_PREPEND(&ssl_proxies, proxy);
*proxy_r = proxy;
return sfd[1];
}
static struct ssl_server_context *
ssl_server_context_get(const struct login_settings *set)
{
struct ssl_server_context *ctx, lookup_ctx;
memset(&lookup_ctx, 0, sizeof(lookup_ctx));
lookup_ctx.cert = set->ssl_cert;
lookup_ctx.key = set->ssl_key;
lookup_ctx.ca = set->ssl_ca;
lookup_ctx.cipher_list = set->ssl_cipher_list;
lookup_ctx.protocols = set->ssl_protocols;
lookup_ctx.verify_client_cert = set->ssl_verify_client_cert;
ctx = hash_table_lookup(ssl_servers, &lookup_ctx);
if (ctx == NULL)
ctx = ssl_server_context_init(set);
return ctx;
}
int ssl_proxy_alloc(int fd, const struct ip_addr *ip,
const struct login_settings *set,
struct ssl_proxy **proxy_r)
{
struct ssl_server_context *ctx;
ctx = ssl_server_context_get(set);
return ssl_proxy_alloc_common(ctx->ctx, fd, ip, set, proxy_r);
}
int ssl_proxy_client_alloc(int fd, struct ip_addr *ip,
const struct login_settings *set,
ssl_handshake_callback_t *callback, void *context,
struct ssl_proxy **proxy_r)
{
int ret;
ret = ssl_proxy_alloc_common(ssl_client_ctx, fd, ip, set, proxy_r);
if (ret < 0)
return -1;
(*proxy_r)->handshake_callback = callback;
(*proxy_r)->handshake_context = context;
(*proxy_r)->client_proxy = TRUE;
return ret;
}
void ssl_proxy_start(struct ssl_proxy *proxy)
{
ssl_step(proxy);
}
void ssl_proxy_set_client(struct ssl_proxy *proxy, struct client *client)
{
i_assert(proxy->client == NULL);
proxy->client = client;
}
void ssl_proxy_unset_client(struct ssl_proxy *proxy)
{
proxy->client = NULL;
}
bool ssl_proxy_has_valid_client_cert(const struct ssl_proxy *proxy)
{
return proxy->cert_received && !proxy->cert_broken;
}
bool ssl_proxy_has_broken_client_cert(struct ssl_proxy *proxy)
{
return proxy->cert_received && proxy->cert_broken;
}
int ssl_proxy_cert_match_name(struct ssl_proxy *proxy, const char *verify_name)
{
return openssl_cert_match_name(proxy->ssl, verify_name);
}
const char *ssl_proxy_get_peer_name(struct ssl_proxy *proxy)
{
X509 *x509;
char *name;
int len;
if (!ssl_proxy_has_valid_client_cert(proxy))
return NULL;
x509 = SSL_get_peer_certificate(proxy->ssl);
if (x509 == NULL)
return NULL; /* we should have had it.. */
len = X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
ssl_username_nid, NULL, 0);
if (len < 0)
name = "";
else {
name = t_malloc(len + 1);
if (X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
ssl_username_nid, name, len + 1) < 0)
name = "";
else if (strlen(name) != (size_t)len) {
/* NUL characters in name. Someone's trying to fake
being another user? Don't allow it. */
name = "";
}
}
X509_free(x509);
return *name == '\0' ? NULL : name;
}
bool ssl_proxy_is_handshaked(const struct ssl_proxy *proxy)
{
return proxy->handshaked;
}
const char *ssl_proxy_get_last_error(const struct ssl_proxy *proxy)
{
return proxy->last_error;
}
const char *ssl_proxy_get_security_string(struct ssl_proxy *proxy)
{
const SSL_CIPHER *cipher;
int bits, alg_bits;
const char *comp_str;
if (!proxy->handshaked)
return "";
cipher = SSL_get_current_cipher(proxy->ssl);
bits = SSL_CIPHER_get_bits(cipher, &alg_bits);
comp_str = ssl_proxy_get_compression(proxy);
comp_str = comp_str == NULL ? "" : t_strconcat(" ", comp_str, NULL);
return t_strdup_printf("%s with cipher %s (%d/%d bits)%s",
SSL_get_version(proxy->ssl),
SSL_CIPHER_get_name(cipher),
bits, alg_bits, comp_str);
}
const char *ssl_proxy_get_compression(struct ssl_proxy *proxy ATTR_UNUSED)
{
#ifdef HAVE_SSL_COMPRESSION
const COMP_METHOD *comp;
comp = SSL_get_current_compression(proxy->ssl);
return comp == NULL ? NULL : SSL_COMP_get_name(comp);
#else
return NULL;
#endif
}
void ssl_proxy_free(struct ssl_proxy **_proxy)
{
struct ssl_proxy *proxy = *_proxy;
*_proxy = NULL;
ssl_proxy_unref(proxy);
}
static void ssl_proxy_unref(struct ssl_proxy *proxy)
{
if (--proxy->refcount > 0)
return;
i_assert(proxy->refcount == 0);
SSL_free(proxy->ssl);
i_free(proxy->last_error);
i_free(proxy);
}
static void ssl_proxy_destroy(struct ssl_proxy *proxy)
{
if (proxy->destroyed)
return;
proxy->destroyed = TRUE;
ssl_proxy_count--;
DLLIST_REMOVE(&ssl_proxies, proxy);
if (proxy->io_ssl_read != NULL)
io_remove(&proxy->io_ssl_read);
if (proxy->io_ssl_write != NULL)
io_remove(&proxy->io_ssl_write);
if (proxy->io_plain_read != NULL)
io_remove(&proxy->io_plain_read);
if (proxy->io_plain_write != NULL)
io_remove(&proxy->io_plain_write);
(void)SSL_shutdown(proxy->ssl);
(void)net_disconnect(proxy->fd_ssl);
(void)net_disconnect(proxy->fd_plain);
ssl_proxy_unref(proxy);
}
static RSA *ssl_gen_rsa_key(SSL *ssl ATTR_UNUSED,
int is_export ATTR_UNUSED, int keylength)
{
return RSA_generate_key(keylength, RSA_F4, NULL, NULL);
}
static DH *ssl_tmp_dh_callback(SSL *ssl ATTR_UNUSED,
int is_export, int keylength)
{
/* Well, I'm not exactly sure why the logic in here is this.
It's the same as in Postfix, so it can't be too wrong. */
if (is_export && keylength == 512 && ssl_params.dh_512 != NULL)
return ssl_params.dh_512;
return ssl_params.dh_1024;
}
static void ssl_info_callback(const SSL *ssl, int where, int ret)
{
struct ssl_proxy *proxy;
proxy = SSL_get_ex_data(ssl, extdata_index);
if (!proxy->set->verbose_ssl)
return;
if ((where & SSL_CB_ALERT) != 0) {
i_warning("SSL alert: where=0x%x, ret=%d: %s %s [%s]",
where, ret, SSL_alert_type_string_long(ret),
SSL_alert_desc_string_long(ret),
net_ip2addr(&proxy->ip));
} else if (ret == 0) {
i_warning("SSL failed: where=0x%x: %s [%s]",
where, SSL_state_string_long(ssl),
net_ip2addr(&proxy->ip));
} else {
i_warning("SSL: where=0x%x, ret=%d: %s [%s]",
where, ret, SSL_state_string_long(ssl),
net_ip2addr(&proxy->ip));
}
}
static int ssl_verify_client_cert(int preverify_ok, X509_STORE_CTX *ctx)
{
SSL *ssl;
struct ssl_proxy *proxy;
ssl = X509_STORE_CTX_get_ex_data(ctx,
SSL_get_ex_data_X509_STORE_CTX_idx());
proxy = SSL_get_ex_data(ssl, extdata_index);
proxy->cert_received = TRUE;
if (proxy->set->verbose_ssl ||
(proxy->set->auth_verbose && !preverify_ok)) {
char buf[1024];
X509_NAME *subject;
subject = X509_get_subject_name(ctx->current_cert);
(void)X509_NAME_oneline(subject, buf, sizeof(buf));
buf[sizeof(buf)-1] = '\0'; /* just in case.. */
if (!preverify_ok)
i_info("Invalid certificate: %s: %s", X509_verify_cert_error_string(ctx->error),buf);
else
i_info("Valid certificate: %s", buf);
}
if (ctx->error == X509_V_ERR_UNABLE_TO_GET_CRL && proxy->client_proxy) {
/* no CRL given with the CA list. don't worry about it. */
preverify_ok = 1;
}
if (!preverify_ok)
proxy->cert_broken = TRUE;
/* Return success anyway, because if ssl_require_client_cert=no we
could still allow authentication. */
return 1;
}
static int
pem_password_callback(char *buf, int size, int rwflag ATTR_UNUSED,
void *userdata)
{
if (userdata == NULL) {
i_error("SSL private key file is password protected, "
"but password isn't given");
return 0;
}
if (i_strocpy(buf, userdata, size) < 0)
return 0;
return strlen(buf);
}
unsigned int ssl_proxy_get_count(void)
{
return ssl_proxy_count;
}
static bool is_pem_key(const char *cert)
{
return strstr(cert, "PRIVATE KEY---") != NULL;
}
static STACK_OF(X509_NAME) *load_ca(X509_STORE *store, const char *ca)
{
/* mostly just copy&pasted from X509_load_cert_crl_file() */
STACK_OF(X509_INFO) *inf;
STACK_OF(X509_NAME) *xnames;
X509_INFO *itmp;
X509_NAME *xname;
BIO *bio;
int i;
bio = BIO_new_mem_buf(t_strdup_noconst(ca), strlen(ca));
if (bio == NULL)
i_fatal("BIO_new_mem_buf() failed");
inf = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
if (inf == NULL)
i_fatal("Couldn't parse ssl_ca: %s", ssl_last_error());
BIO_free(bio);
xnames = sk_X509_NAME_new_null();
if (xnames == NULL)
i_fatal("sk_X509_NAME_new_null() failed");
for(i = 0; i < sk_X509_INFO_num(inf); i++) {
itmp = sk_X509_INFO_value(inf, i);
if(itmp->x509) {
X509_STORE_add_cert(store, itmp->x509);
xname = X509_get_subject_name(itmp->x509);
if (xname != NULL)
xname = X509_NAME_dup(xname);
if (xname != NULL)
sk_X509_NAME_push(xnames, xname);
}
if(itmp->crl)
X509_STORE_add_crl(store, itmp->crl);
}
sk_X509_INFO_pop_free(inf, X509_INFO_free);
return xnames;
}
static STACK_OF(X509_NAME) *
ssl_proxy_ctx_init(SSL_CTX *ssl_ctx, const struct login_settings *set)
{
X509_STORE *store;
STACK_OF(X509_NAME) *xnames = NULL;
/* enable all SSL workarounds, except empty fragments as it
makes SSL more vulnerable against attacks */
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL &
~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
#ifdef SSL_MODE_RELEASE_BUFFERS
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
#endif
if (*set->ssl_ca != '\0') {
/* set trusted CA certs */
store = SSL_CTX_get_cert_store(ssl_ctx);
xnames = load_ca(store, set->ssl_ca);
}
SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
if (SSL_CTX_need_tmp_RSA(ssl_ctx))
SSL_CTX_set_tmp_rsa_callback(ssl_ctx, ssl_gen_rsa_key);
SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_tmp_dh_callback);
return xnames;
}
static void
ssl_proxy_ctx_verify_client(SSL_CTX *ssl_ctx, STACK_OF(X509_NAME) *ca_names)
{
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
X509_STORE *store;
store = SSL_CTX_get_cert_store(ssl_ctx);
X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK |
X509_V_FLAG_CRL_CHECK_ALL);
#endif
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
ssl_verify_client_cert);
/* set list of CA names that are sent to client */
SSL_CTX_set_client_CA_list(ssl_ctx, ca_names);
}
static const char *ssl_proxy_get_use_certificate_error(const char *cert)
{
unsigned long err;
err = ERR_peek_error();
if (ERR_GET_LIB(err) != ERR_LIB_PEM ||
ERR_GET_REASON(err) != PEM_R_NO_START_LINE)
return ssl_last_error();
else if (is_pem_key(cert)) {
return "The file contains a private key "
"(you've mixed ssl_cert and ssl_key settings)";
} else if (strchr(cert, '\n') == NULL) {
return t_strdup_printf("There is no valid PEM certificate. "
"(You probably forgot '<' from ssl_cert=<%s)", cert);
} else {
return "There is no valid PEM certificate.";
}
}
static EVP_PKEY *
ssl_proxy_load_key(const char *key, const char *password)
{
EVP_PKEY *pkey;
BIO *bio;
char *dup_password;
bio = BIO_new_mem_buf(t_strdup_noconst(key), strlen(key));
if (bio == NULL)
i_fatal("BIO_new_mem_buf() failed");
dup_password = t_strdup_noconst(password);
pkey = PEM_read_bio_PrivateKey(bio, NULL, pem_password_callback,
dup_password);
if (pkey == NULL)
i_fatal("Couldn't parse private ssl_key");
BIO_free(bio);
return pkey;
}
static const char *ssl_key_load_error(void)
{
unsigned long err = ERR_peek_error();
if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
ERR_GET_REASON(err) == X509_R_KEY_VALUES_MISMATCH)
return "Key is for a different cert than ssl_cert";
else
return ssl_last_error();
}
static void ssl_proxy_ctx_use_key(SSL_CTX *ctx, const struct login_settings *set)
{
EVP_PKEY *pkey;
const char *password;
password = *set->ssl_key_password != '\0' ? set->ssl_key_password :
getenv(MASTER_SSL_KEY_PASSWORD_ENV);
pkey = ssl_proxy_load_key(set->ssl_key, password);
if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1)
i_fatal("Can't load private ssl_key: %s", ssl_key_load_error());
EVP_PKEY_free(pkey);
}
static int
ssl_proxy_ctx_use_certificate_chain(SSL_CTX *ctx, const char *cert)
{
/* mostly just copy&pasted from SSL_CTX_use_certificate_chain_file() */
BIO *in;
X509 *x;
int ret = 0;
in = BIO_new_mem_buf(t_strdup_noconst(cert), strlen(cert));
if (in == NULL)
i_fatal("BIO_new_mem_buf() failed");
x = PEM_read_bio_X509(in, NULL, NULL, NULL);
if (x == NULL)
goto end;
ret = SSL_CTX_use_certificate(ctx, x);
#if 0
/* This is in OpenSSL code, but it seems to cause failures.. */
if (ERR_peek_error() != 0)
ret = 0;
#endif
if (ret != 0) {
/* If we could set up our certificate, now proceed to
* the CA certificates.
*/
X509 *ca;
int r;
unsigned long err;
while ((ca = PEM_read_bio_X509(in,NULL,NULL,NULL)) != NULL) {
r = SSL_CTX_add_extra_chain_cert(ctx, ca);
if (!r) {
X509_free(ca);
ret = 0;
goto end;
}
}
/* When the while loop ends, it's usually just EOF. */
err = ERR_peek_last_error();
if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
ERR_clear_error();
else
ret = 0; /* some real error */
}
end:
if (x != NULL) X509_free(x);
BIO_free(in);
return ret;
}
#ifdef HAVE_SSL_GET_SERVERNAME
static void ssl_servername_callback(SSL *ssl, int *al ATTR_UNUSED,
void *context ATTR_UNUSED)
{
struct ssl_server_context *ctx;
struct ssl_proxy *proxy;
struct client *client;
const char *host;
void **other_sets;
proxy = SSL_get_ex_data(ssl, extdata_index);
host = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
client = proxy->client;
if (!client->ssl_servername_settings_read) {
client->ssl_servername_settings_read = TRUE;
client->set = login_settings_read(client->pool,
&client->local_ip,
&client->ip, host,
&other_sets);
}
ctx = ssl_server_context_get(client->set);
SSL_set_SSL_CTX(ssl, ctx->ctx);
}
#endif
enum {
DOVECOT_SSL_PROTO_SSLv2 = 0x01,
DOVECOT_SSL_PROTO_SSLv3 = 0x02,
DOVECOT_SSL_PROTO_TLSv1 = 0x04,
DOVECOT_SSL_PROTO_ALL = 0x07
};
static void
ssl_proxy_ctx_set_protocols(struct ssl_server_context *ssl_ctx,
const char *protocols)
{
const char *const *tmp;
int proto, op = 0, include = 0, exclude = 0;
bool neg;
tmp = t_strsplit_spaces(protocols, " ");
for (; *tmp != NULL; tmp++) {
const char *name = *tmp;
if (*name != '!')
neg = FALSE;
else {
name++;
neg = TRUE;
}
if (strcasecmp(name, SSL_TXT_SSLV2) == 0)
proto = DOVECOT_SSL_PROTO_SSLv2;
else if (strcasecmp(name, SSL_TXT_SSLV3) == 0)
proto = DOVECOT_SSL_PROTO_SSLv3;
else if (strcasecmp(name, SSL_TXT_TLSV1) == 0)
proto = DOVECOT_SSL_PROTO_TLSv1;
else {
i_fatal("Invalid ssl_protocols setting: "
"Unknown protocol '%s'", name);
}
if (neg)
exclude |= proto;
else
include |= proto;
}
if (include != 0) {
/* exclude everything, except those that are included
(and let excludes still override those) */
exclude |= DOVECOT_SSL_PROTO_ALL & ~include;
}
if ((exclude & DOVECOT_SSL_PROTO_SSLv2) != 0) op |= SSL_OP_NO_SSLv2;
if ((exclude & DOVECOT_SSL_PROTO_SSLv3) != 0) op |= SSL_OP_NO_SSLv3;
if ((exclude & DOVECOT_SSL_PROTO_TLSv1) != 0) op |= SSL_OP_NO_TLSv1;
SSL_CTX_set_options(ssl_ctx->ctx, op);
}
static struct ssl_server_context *
ssl_server_context_init(const struct login_settings *set)
{
struct ssl_server_context *ctx;
SSL_CTX *ssl_ctx;
pool_t pool;
STACK_OF(X509_NAME) *xnames;
pool = pool_alloconly_create("ssl server context", 4096);
ctx = p_new(pool, struct ssl_server_context, 1);
ctx->pool = pool;
ctx->cert = p_strdup(pool, set->ssl_cert);
ctx->key = p_strdup(pool, set->ssl_key);
ctx->ca = p_strdup(pool, set->ssl_ca);
ctx->cipher_list = p_strdup(pool, set->ssl_cipher_list);
ctx->protocols = p_strdup(pool, set->ssl_protocols);
ctx->verify_client_cert = set->ssl_verify_client_cert;
ctx->ctx = ssl_ctx = SSL_CTX_new(SSLv23_server_method());
if (ssl_ctx == NULL)
i_fatal("SSL_CTX_new() failed");
xnames = ssl_proxy_ctx_init(ssl_ctx, set);
if (SSL_CTX_set_cipher_list(ssl_ctx, ctx->cipher_list) != 1) {
i_fatal("Can't set cipher list to '%s': %s",
ctx->cipher_list, ssl_last_error());
}
ssl_proxy_ctx_set_protocols(ctx, ctx->protocols);
if (ssl_proxy_ctx_use_certificate_chain(ctx->ctx, ctx->cert) != 1) {
i_fatal("Can't load ssl_cert: %s",
ssl_proxy_get_use_certificate_error(ctx->cert));
}
#ifdef HAVE_SSL_GET_SERVERNAME
if (SSL_CTX_set_tlsext_servername_callback(ctx->ctx,
ssl_servername_callback) != 1) {
if (set->verbose_ssl)
i_debug("OpenSSL library doesn't support SNI");
}
#endif
ssl_proxy_ctx_use_key(ctx->ctx, set);
SSL_CTX_set_info_callback(ctx->ctx, ssl_info_callback);
if (ctx->verify_client_cert)
ssl_proxy_ctx_verify_client(ctx->ctx, xnames);
hash_table_insert(ssl_servers, ctx, ctx);
return ctx;
}
static void ssl_server_context_deinit(struct ssl_server_context **_ctx)
{
struct ssl_server_context *ctx = *_ctx;
SSL_CTX_free(ctx->ctx);
pool_unref(&ctx->pool);
}
static void
ssl_proxy_client_ctx_set_client_cert(SSL_CTX *ctx,
const struct login_settings *set)
{
EVP_PKEY *pkey;
if (*set->ssl_client_cert == '\0')
return;
if (ssl_proxy_ctx_use_certificate_chain(ctx, set->ssl_client_cert) != 1) {
i_fatal("Can't load ssl_client_cert: %s",
ssl_proxy_get_use_certificate_error(set->ssl_client_cert));
}
pkey = ssl_proxy_load_key(set->ssl_client_key, NULL);
if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
i_fatal("Can't load private ssl_client_key: %s",
ssl_key_load_error());
}
EVP_PKEY_free(pkey);
}
static void ssl_proxy_init_client(const struct login_settings *set)
{
STACK_OF(X509_NAME) *xnames;
if ((ssl_client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
i_fatal("SSL_CTX_new() failed");
xnames = ssl_proxy_ctx_init(ssl_client_ctx, set);
ssl_proxy_ctx_verify_client(ssl_client_ctx, xnames);
ssl_proxy_client_ctx_set_client_cert(ssl_client_ctx, set);
}
void ssl_proxy_init(void)
{
const struct login_settings *set = global_login_settings;
static char dovecot[] = "dovecot";
unsigned char buf;
if (strcmp(set->ssl, "no") == 0)
return;
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
if (*set->ssl_crypto_device != '\0') {
ENGINE_load_builtin_engines();
ssl_engine = ENGINE_by_id(set->ssl_crypto_device);
if (ssl_engine == NULL) {
i_fatal("Unknown ssl_crypto_device: %s",
set->ssl_crypto_device);
}
ENGINE_init(ssl_engine);
ENGINE_set_default_RSA(ssl_engine);
ENGINE_set_default_DSA(ssl_engine);
ENGINE_set_default_ciphers(ssl_engine);
}
extdata_index = SSL_get_ex_new_index(0, dovecot, NULL, NULL, NULL);
ssl_servers = hash_table_create(default_pool, default_pool, 0,
ssl_server_context_hash,
ssl_server_context_cmp);
(void)ssl_server_context_init(set);
ssl_proxy_init_client(set);
ssl_username_nid = OBJ_txt2nid(set->ssl_cert_username_field);
if (ssl_username_nid == NID_undef) {
i_fatal("Invalid ssl_cert_username_field: %s",
set->ssl_cert_username_field);
}
/* PRNG initialization might want to use /dev/urandom, make sure it
does it before chrooting. We might not have enough entropy at
the first try, so this function may fail. It's still been
initialized though. */
(void)RAND_bytes(&buf, 1);
memset(&ssl_params, 0, sizeof(ssl_params));
ssl_params.path = SSL_PARAMETERS_PATH;
ssl_proxy_count = 0;
ssl_proxies = NULL;
ssl_initialized = TRUE;
}
void ssl_proxy_deinit(void)
{
struct hash_iterate_context *iter;
void *key, *value;
if (!ssl_initialized)
return;
while (ssl_proxies != NULL)
ssl_proxy_destroy(ssl_proxies);
iter = hash_table_iterate_init(ssl_servers);
while (hash_table_iterate(iter, &key, &value)) {
struct ssl_server_context *ctx = value;
ssl_server_context_deinit(&ctx);
}
hash_table_iterate_deinit(&iter);
hash_table_destroy(&ssl_servers);
ssl_free_parameters(&ssl_params);
SSL_CTX_free(ssl_client_ctx);
if (ssl_engine != NULL) {
ENGINE_finish(ssl_engine);
ENGINE_cleanup();
}
EVP_cleanup();
ERR_free_strings();
}
#endif