yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 12 | * Acknowledgement: |
| 13 | * We'd like to specially thank the Stud project authors for a very clean |
| 14 | * and well documented code which helped us understand how the OpenSSL API |
| 15 | * ought to be used in non-blocking mode. This is one difficult part which |
| 16 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 17 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 18 | * the good works, guys ! |
| 19 | * |
| 20 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 21 | * particularly well with haproxy. For more info about this project, visit : |
| 22 | * https://github.com/bumptech/stud |
| 23 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Willy Tarreau | 8d164dc | 2019-05-10 09:35:00 +0200 | [diff] [blame] | 26 | /* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 27 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 36 | |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 40 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 41 | #include <netinet/tcp.h> |
| 42 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 43 | #include <import/lru.h> |
| 44 | #include <import/xxhash.h> |
| 45 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 46 | #include <common/buffer.h> |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 47 | #include <common/chunk.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 48 | #include <common/compat.h> |
| 49 | #include <common/config.h> |
| 50 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 51 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 52 | #include <common/initcall.h> |
Willy Tarreau | 5599456 | 2019-05-09 14:52:44 +0200 | [diff] [blame] | 53 | #include <common/openssl-compat.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 54 | #include <common/standard.h> |
| 55 | #include <common/ticks.h> |
| 56 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 57 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 58 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 60 | #include <ebsttree.h> |
| 61 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 62 | #include <types/applet.h> |
| 63 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 64 | #include <types/global.h> |
| 65 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 66 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 68 | #include <proto/acl.h> |
| 69 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 70 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 71 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 72 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 73 | #include <proto/fd.h> |
| 74 | #include <proto/freq_ctr.h> |
| 75 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 76 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 77 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 78 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 79 | #include <proto/proto_tcp.h> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 80 | #include <proto/proto_http.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 81 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 82 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 83 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 84 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 85 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 86 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 87 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/task.h> |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 89 | #include <proto/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 91 | /* ***** READ THIS before adding code here! ***** |
| 92 | * |
| 93 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 94 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 95 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 96 | * exclusively so that the whole code consistently uses the same macros. |
| 97 | * |
| 98 | * Whenever possible if a macro is missing in certain versions, it's better |
| 99 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 100 | */ |
| 101 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 102 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 103 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 104 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 105 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 106 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 107 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 108 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 109 | |
| 110 | /* Verify errors macros */ |
| 111 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 112 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 113 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 114 | |
| 115 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 116 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 117 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 118 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 119 | /* ssl_methods flags for ssl options */ |
| 120 | #define MC_SSL_O_ALL 0x0000 |
| 121 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 122 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 123 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 124 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 125 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 126 | |
| 127 | /* ssl_methods versions */ |
| 128 | enum { |
| 129 | CONF_TLSV_NONE = 0, |
| 130 | CONF_TLSV_MIN = 1, |
| 131 | CONF_SSLV3 = 1, |
| 132 | CONF_TLSV10 = 2, |
| 133 | CONF_TLSV11 = 3, |
| 134 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 135 | CONF_TLSV13 = 5, |
| 136 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 139 | /* server and bind verify method, it uses a global value as default */ |
| 140 | enum { |
| 141 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 142 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 143 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 144 | SSL_SOCK_VERIFY_NONE = 3, |
| 145 | }; |
| 146 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 148 | int sslconns = 0; |
| 149 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 150 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 151 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 153 | static struct { |
| 154 | char *crt_base; /* base directory path for certificates */ |
| 155 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 156 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 157 | |
| 158 | char *listen_default_ciphers; |
| 159 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 160 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 161 | char *listen_default_ciphersuites; |
| 162 | char *connect_default_ciphersuites; |
| 163 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 164 | int listen_default_ssloptions; |
| 165 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 166 | struct tls_version_filter listen_default_sslmethods; |
| 167 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 168 | |
| 169 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 170 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 171 | unsigned int max_record; /* SSL max record size */ |
| 172 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 173 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 174 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 175 | } global_ssl = { |
| 176 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 177 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 178 | #endif |
| 179 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 180 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 181 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 182 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 183 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 184 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 185 | #endif |
| 186 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 187 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 188 | #endif |
| 189 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 190 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 191 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 192 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 193 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 194 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 195 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 196 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 197 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 198 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 199 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 200 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 201 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 202 | #endif |
| 203 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 204 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 205 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 206 | }; |
| 207 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 208 | static BIO_METHOD *ha_meth; |
| 209 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 210 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 211 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 212 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 213 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 214 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 215 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 216 | struct wait_event wait_event; |
| 217 | struct wait_event *recv_wait; |
| 218 | struct wait_event *send_wait; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 219 | int xprt_st; /* transport layer state, initialized to zero */ |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 220 | struct buffer early_buf; /* buffer to store the early data received */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 221 | int sent_early_data; /* Amount of early data we sent so far */ |
| 222 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 226 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 227 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 228 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 229 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 230 | /* Methods to implement OpenSSL BIO */ |
| 231 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 232 | { |
| 233 | struct buffer tmpbuf; |
| 234 | struct ssl_sock_ctx *ctx; |
| 235 | int ret; |
| 236 | |
| 237 | ctx = BIO_get_data(h); |
| 238 | tmpbuf.size = num; |
| 239 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 240 | tmpbuf.data = num; |
| 241 | tmpbuf.head = 0; |
| 242 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 243 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 244 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 245 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 246 | } else if (ret == 0) |
| 247 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 252 | { |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int ha_ssl_puts(BIO *h, const char *str) |
| 258 | { |
| 259 | |
| 260 | return ha_ssl_write(h, str, strlen(str)); |
| 261 | } |
| 262 | |
| 263 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 264 | { |
| 265 | struct buffer tmpbuf; |
| 266 | struct ssl_sock_ctx *ctx; |
| 267 | int ret; |
| 268 | |
| 269 | ctx = BIO_get_data(h); |
| 270 | tmpbuf.size = size; |
| 271 | tmpbuf.area = buf; |
| 272 | tmpbuf.data = 0; |
| 273 | tmpbuf.head = 0; |
| 274 | ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 275 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 276 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 277 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 278 | } else if (ret == 0) |
| 279 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 285 | { |
| 286 | int ret = 0; |
| 287 | switch (cmd) { |
| 288 | case BIO_CTRL_DUP: |
| 289 | case BIO_CTRL_FLUSH: |
| 290 | ret = 1; |
| 291 | break; |
| 292 | } |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int ha_ssl_new(BIO *h) |
| 297 | { |
| 298 | BIO_set_init(h, 1); |
| 299 | BIO_set_data(h, NULL); |
| 300 | BIO_clear_flags(h, ~0); |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | static int ha_ssl_free(BIO *data) |
| 305 | { |
| 306 | |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 311 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 312 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 313 | static HA_RWLOCK_T *ssl_rwlocks; |
| 314 | |
| 315 | |
| 316 | unsigned long ssl_id_function(void) |
| 317 | { |
| 318 | return (unsigned long)tid; |
| 319 | } |
| 320 | |
| 321 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 322 | { |
| 323 | if (mode & CRYPTO_LOCK) { |
| 324 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 325 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 326 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 327 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 328 | } |
| 329 | else { |
| 330 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 331 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 332 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 333 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | static int ssl_locking_init(void) |
| 338 | { |
| 339 | int i; |
| 340 | |
| 341 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 342 | if (!ssl_rwlocks) |
| 343 | return -1; |
| 344 | |
| 345 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 346 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 347 | |
| 348 | CRYPTO_set_id_callback(ssl_id_function); |
| 349 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 350 | |
| 351 | return 0; |
| 352 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 353 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 354 | #endif |
| 355 | |
| 356 | |
| 357 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 358 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 359 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 360 | unsigned long long int xxh64; |
| 361 | unsigned char ciphersuite_len; |
| 362 | char ciphersuite[0]; |
| 363 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 364 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 365 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 366 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 367 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 368 | static int ssl_pkey_info_index = -1; |
| 369 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 370 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 371 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 372 | #endif |
| 373 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 374 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 375 | static unsigned int openssl_engines_initialized; |
| 376 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 377 | struct ssl_engine_list { |
| 378 | struct list list; |
| 379 | ENGINE *e; |
| 380 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 381 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 382 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 383 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 384 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 385 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 386 | static DH *local_dh_1024 = NULL; |
| 387 | static DH *local_dh_2048 = NULL; |
| 388 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 389 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 390 | #endif /* OPENSSL_NO_DH */ |
| 391 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 392 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 393 | /* X509V3 Extensions that will be added on generated certificates */ |
| 394 | #define X509V3_EXT_SIZE 5 |
| 395 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 396 | "basicConstraints", |
| 397 | "nsComment", |
| 398 | "subjectKeyIdentifier", |
| 399 | "authorityKeyIdentifier", |
| 400 | "keyUsage", |
| 401 | }; |
| 402 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 403 | "CA:FALSE", |
| 404 | "\"OpenSSL Generated Certificate\"", |
| 405 | "hash", |
| 406 | "keyid,issuer:always", |
| 407 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 408 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 409 | /* LRU cache to store generated certificate */ |
| 410 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 411 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 412 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 413 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 414 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 415 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 416 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 417 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 418 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 419 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 420 | /* The order here matters for picking a default context, |
| 421 | * keep the most common keytype at the bottom of the list |
| 422 | */ |
| 423 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 424 | "dsa", |
| 425 | "ecdsa", |
| 426 | "rsa" |
| 427 | }; |
| 428 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 429 | #else |
| 430 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 431 | #endif |
| 432 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 433 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 434 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 435 | |
| 436 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 437 | |
| 438 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 439 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 440 | |
| 441 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 442 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 443 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 444 | /* |
| 445 | * This function gives the detail of the SSL error. It is used only |
| 446 | * if the debug mode and the verbose mode are activated. It dump all |
| 447 | * the SSL error until the stack was empty. |
| 448 | */ |
| 449 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 450 | { |
| 451 | unsigned long ret; |
| 452 | |
| 453 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 454 | while(1) { |
| 455 | ret = ERR_get_error(); |
| 456 | if (ret == 0) |
| 457 | return; |
| 458 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 459 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 460 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 465 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 466 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 467 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 468 | { |
| 469 | int err_code = ERR_ABORT; |
| 470 | ENGINE *engine; |
| 471 | struct ssl_engine_list *el; |
| 472 | |
| 473 | /* grab the structural reference to the engine */ |
| 474 | engine = ENGINE_by_id(engine_id); |
| 475 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 476 | ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 477 | goto fail_get; |
| 478 | } |
| 479 | |
| 480 | if (!ENGINE_init(engine)) { |
| 481 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 482 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 483 | goto fail_init; |
| 484 | } |
| 485 | |
| 486 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 487 | ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 488 | goto fail_set_method; |
| 489 | } |
| 490 | |
| 491 | el = calloc(1, sizeof(*el)); |
| 492 | el->e = engine; |
| 493 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 494 | nb_engines++; |
| 495 | if (global_ssl.async) |
| 496 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | |
| 499 | fail_set_method: |
| 500 | /* release the functional reference from ENGINE_init() */ |
| 501 | ENGINE_finish(engine); |
| 502 | |
| 503 | fail_init: |
| 504 | /* release the structural reference from ENGINE_by_id() */ |
| 505 | ENGINE_free(engine); |
| 506 | |
| 507 | fail_get: |
| 508 | return err_code; |
| 509 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 510 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 511 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 512 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 513 | /* |
| 514 | * openssl async fd handler |
| 515 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 516 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 517 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 518 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 519 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 520 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 521 | * to poll this fd until it is requested |
| 522 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 523 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 524 | fd_cant_recv(fd); |
| 525 | |
| 526 | /* crypto engine is available, let's notify the associated |
| 527 | * connection that it can pursue its processing. |
| 528 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 529 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 532 | /* |
| 533 | * openssl async delayed SSL_free handler |
| 534 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 535 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 536 | { |
| 537 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 538 | OSSL_ASYNC_FD all_fd[32]; |
| 539 | size_t num_all_fds = 0; |
| 540 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 541 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 542 | /* We suppose that the async job for a same SSL * |
| 543 | * are serialized. So if we are awake it is |
| 544 | * because the running job has just finished |
| 545 | * and we can remove all async fds safely |
| 546 | */ |
| 547 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 548 | if (num_all_fds > 32) { |
| 549 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 554 | for (i=0 ; i < num_all_fds ; i++) |
| 555 | fd_remove(all_fd[i]); |
| 556 | |
| 557 | /* Now we can safely call SSL_free, no more pending job in engines */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 558 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 559 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 560 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 561 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 562 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 563 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 564 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 565 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 566 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 567 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 568 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 569 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 570 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 571 | size_t num_add_fds = 0; |
| 572 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 573 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 574 | |
| 575 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 576 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 577 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 578 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 582 | SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 583 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 584 | /* We remove unused fds from the fdtab */ |
| 585 | for (i=0 ; i < num_del_fds ; i++) |
| 586 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 587 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 588 | /* We add new fds to the fdtab */ |
| 589 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 590 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 593 | num_add_fds = 0; |
| 594 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 595 | if (num_add_fds > 32) { |
| 596 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 597 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 598 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 599 | |
| 600 | /* We activate the polling for all known async fds */ |
| 601 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 602 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 603 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 604 | /* To ensure that the fd cache won't be used |
| 605 | * We'll prefer to catch a real RD event |
| 606 | * because handling an EAGAIN on this fd will |
| 607 | * result in a context switch and also |
| 608 | * some engines uses a fd in blocking mode. |
| 609 | */ |
| 610 | fd_cant_recv(add_fd[i]); |
| 611 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 612 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 613 | } |
| 614 | #endif |
| 615 | |
William Lallemand | c33d83d | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 616 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 617 | /* |
| 618 | * This function returns the number of seconds elapsed |
| 619 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 620 | * date presented un ASN1_GENERALIZEDTIME. |
| 621 | * |
| 622 | * In parsing error case, it returns -1. |
| 623 | */ |
| 624 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 625 | { |
| 626 | long epoch; |
| 627 | char *p, *end; |
| 628 | const unsigned short month_offset[12] = { |
| 629 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 630 | }; |
| 631 | int year, month; |
| 632 | |
| 633 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 634 | |
| 635 | p = (char *)d->data; |
| 636 | end = p + d->length; |
| 637 | |
| 638 | if (end - p < 4) return -1; |
| 639 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 640 | p += 4; |
| 641 | if (end - p < 2) return -1; |
| 642 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 643 | if (month < 1 || month > 12) return -1; |
| 644 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 645 | We consider leap years and the current month (<marsh or not) */ |
| 646 | epoch = ( ((year - 1970) * 365) |
| 647 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 648 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 649 | + month_offset[month-1] |
| 650 | ) * 24 * 60 * 60; |
| 651 | p += 2; |
| 652 | if (end - p < 2) return -1; |
| 653 | /* Add the number of seconds of completed days of current month */ |
| 654 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 655 | p += 2; |
| 656 | if (end - p < 2) return -1; |
| 657 | /* Add the completed hours of the current day */ |
| 658 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 659 | p += 2; |
| 660 | if (end - p < 2) return -1; |
| 661 | /* Add the completed minutes of the current hour */ |
| 662 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 663 | p += 2; |
| 664 | if (p == end) return -1; |
| 665 | /* Test if there is available seconds */ |
| 666 | if (p[0] < '0' || p[0] > '9') |
| 667 | goto nosec; |
| 668 | if (end - p < 2) return -1; |
| 669 | /* Add the seconds of the current minute */ |
| 670 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 671 | p += 2; |
| 672 | if (p == end) return -1; |
| 673 | /* Ignore seconds float part if present */ |
| 674 | if (p[0] == '.') { |
| 675 | do { |
| 676 | if (++p == end) return -1; |
| 677 | } while (p[0] >= '0' && p[0] <= '9'); |
| 678 | } |
| 679 | |
| 680 | nosec: |
| 681 | if (p[0] == 'Z') { |
| 682 | if (end - p != 1) return -1; |
| 683 | return epoch; |
| 684 | } |
| 685 | else if (p[0] == '+') { |
| 686 | if (end - p != 5) return -1; |
| 687 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 688 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 689 | } |
| 690 | else if (p[0] == '-') { |
| 691 | if (end - p != 5) return -1; |
| 692 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 693 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return -1; |
| 697 | } |
| 698 | |
William Lallemand | c33d83d | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 699 | /* |
| 700 | * struct alignment works here such that the key.key is the same as key_data |
| 701 | * Do not change the placement of key_data |
| 702 | */ |
| 703 | struct certificate_ocsp { |
| 704 | struct ebmb_node key; |
| 705 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 706 | struct buffer response; |
| 707 | long expire; |
| 708 | }; |
| 709 | |
| 710 | struct ocsp_cbk_arg { |
| 711 | int is_single; |
| 712 | int single_kt; |
| 713 | union { |
| 714 | struct certificate_ocsp *s_ocsp; |
| 715 | /* |
| 716 | * m_ocsp will have multiple entries dependent on key type |
| 717 | * Entry 0 - DSA |
| 718 | * Entry 1 - ECDSA |
| 719 | * Entry 2 - RSA |
| 720 | */ |
| 721 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 722 | }; |
| 723 | }; |
| 724 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 725 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 726 | |
| 727 | /* This function starts to check if the OCSP response (in DER format) contained |
| 728 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 729 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 730 | * contained in the OCSP Response and exits on error if no match. |
| 731 | * If it's a valid OCSP Response: |
| 732 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 733 | * pointed by 'ocsp'. |
| 734 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 735 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 736 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 737 | * already present in the container, it will be overwritten. |
| 738 | * |
| 739 | * Note: OCSP response containing more than one OCSP Single response is not |
| 740 | * considered valid. |
| 741 | * |
| 742 | * Returns 0 on success, 1 in error case. |
| 743 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 744 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 745 | struct certificate_ocsp *ocsp, |
| 746 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 747 | { |
| 748 | OCSP_RESPONSE *resp; |
| 749 | OCSP_BASICRESP *bs = NULL; |
| 750 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 751 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 752 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 753 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 754 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 755 | int reason; |
| 756 | int ret = 1; |
| 757 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 758 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 759 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 760 | if (!resp) { |
| 761 | memprintf(err, "Unable to parse OCSP response"); |
| 762 | goto out; |
| 763 | } |
| 764 | |
| 765 | rc = OCSP_response_status(resp); |
| 766 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 767 | memprintf(err, "OCSP response status not successful"); |
| 768 | goto out; |
| 769 | } |
| 770 | |
| 771 | bs = OCSP_response_get1_basic(resp); |
| 772 | if (!bs) { |
| 773 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 774 | goto out; |
| 775 | } |
| 776 | |
| 777 | count_sr = OCSP_resp_count(bs); |
| 778 | if (count_sr > 1) { |
| 779 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 780 | goto out; |
| 781 | } |
| 782 | |
| 783 | sr = OCSP_resp_get0(bs, 0); |
| 784 | if (!sr) { |
| 785 | memprintf(err, "Failed to get OCSP single response"); |
| 786 | goto out; |
| 787 | } |
| 788 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 789 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 790 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 791 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 792 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 793 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 794 | goto out; |
| 795 | } |
| 796 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 797 | if (!nextupd) { |
| 798 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 799 | goto out; |
| 800 | } |
| 801 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 802 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 803 | if (!rc) { |
| 804 | memprintf(err, "OCSP single response: no longer valid."); |
| 805 | goto out; |
| 806 | } |
| 807 | |
| 808 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 809 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 810 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 811 | goto out; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | if (!ocsp) { |
| 816 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 817 | unsigned char *p; |
| 818 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 819 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 820 | if (!rc) { |
| 821 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 826 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 827 | goto out; |
| 828 | } |
| 829 | |
| 830 | p = key; |
| 831 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 832 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 833 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 834 | if (!ocsp) { |
| 835 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 836 | goto out; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* According to comments on "chunk_dup", the |
| 841 | previous chunk buffer will be freed */ |
| 842 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 843 | memprintf(err, "OCSP response: Memory allocation error"); |
| 844 | goto out; |
| 845 | } |
| 846 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 847 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 848 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 849 | ret = 0; |
| 850 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 851 | ERR_clear_error(); |
| 852 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 853 | if (bs) |
| 854 | OCSP_BASICRESP_free(bs); |
| 855 | |
| 856 | if (resp) |
| 857 | OCSP_RESPONSE_free(resp); |
| 858 | |
| 859 | return ret; |
| 860 | } |
| 861 | /* |
| 862 | * External function use to update the OCSP response in the OCSP response's |
| 863 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 864 | * to update in DER format. |
| 865 | * |
| 866 | * Returns 0 on success, 1 in error case. |
| 867 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 868 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 869 | { |
| 870 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * This function load the OCSP Resonse in DER format contained in file at |
| 875 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 876 | * |
| 877 | * Returns 0 on success, 1 in error case. |
| 878 | */ |
| 879 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 880 | { |
| 881 | int fd = -1; |
| 882 | int r = 0; |
| 883 | int ret = 1; |
| 884 | |
| 885 | fd = open(ocsp_path, O_RDONLY); |
| 886 | if (fd == -1) { |
| 887 | memprintf(err, "Error opening OCSP response file"); |
| 888 | goto end; |
| 889 | } |
| 890 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 891 | trash.data = 0; |
| 892 | while (trash.data < trash.size) { |
| 893 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 894 | if (r < 0) { |
| 895 | if (errno == EINTR) |
| 896 | continue; |
| 897 | |
| 898 | memprintf(err, "Error reading OCSP response from file"); |
| 899 | goto end; |
| 900 | } |
| 901 | else if (r == 0) { |
| 902 | break; |
| 903 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 904 | trash.data += r; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | close(fd); |
| 908 | fd = -1; |
| 909 | |
| 910 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 911 | end: |
| 912 | if (fd != -1) |
| 913 | close(fd); |
| 914 | |
| 915 | return ret; |
| 916 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 917 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 918 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 919 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 920 | static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) |
| 921 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 922 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 923 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 924 | struct connection *conn; |
| 925 | int head; |
| 926 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 927 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 928 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 929 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 930 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 931 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 932 | |
| 933 | keys = ref->tlskeys; |
| 934 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 935 | |
| 936 | if (enc) { |
| 937 | memcpy(key_name, keys[head].name, 16); |
| 938 | |
| 939 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 940 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 941 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 942 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 943 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 944 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 945 | goto end; |
| 946 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 947 | HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 948 | ret = 1; |
| 949 | } |
| 950 | else if (ref->key_size_bits == 256 ) { |
| 951 | |
| 952 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 953 | goto end; |
| 954 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 955 | HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 956 | ret = 1; |
| 957 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 958 | } else { |
| 959 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 960 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 961 | goto found; |
| 962 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 963 | ret = 0; |
| 964 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 965 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 966 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 967 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 968 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 969 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 970 | goto end; |
| 971 | /* 2 for key renewal, 1 if current key is still valid */ |
| 972 | ret = i ? 2 : 1; |
| 973 | } |
| 974 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 975 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 976 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 977 | goto end; |
| 978 | /* 2 for key renewal, 1 if current key is still valid */ |
| 979 | ret = i ? 2 : 1; |
| 980 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 981 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 982 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 983 | end: |
| 984 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 985 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 989 | { |
| 990 | struct tls_keys_ref *ref; |
| 991 | |
| 992 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 993 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 994 | return ref; |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 999 | { |
| 1000 | struct tls_keys_ref *ref; |
| 1001 | |
| 1002 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1003 | if (ref->unique_id == unique_id) |
| 1004 | return ref; |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1008 | /* Update the key into ref: if keysize doesnt |
| 1009 | * match existing ones, this function returns -1 |
| 1010 | * else it returns 0 on success. |
| 1011 | */ |
| 1012 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1013 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1014 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1015 | if (ref->key_size_bits == 128) { |
| 1016 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1017 | return -1; |
| 1018 | } |
| 1019 | else if (ref->key_size_bits == 256) { |
| 1020 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1021 | return -1; |
| 1022 | } |
| 1023 | else |
| 1024 | return -1; |
| 1025 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1026 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1027 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1028 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1029 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1030 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1031 | |
| 1032 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1035 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1036 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1037 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1038 | |
| 1039 | if(!ref) { |
| 1040 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1041 | return 1; |
| 1042 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1043 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1044 | memprintf(err, "Invalid key size"); |
| 1045 | return 1; |
| 1046 | } |
| 1047 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1048 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1049 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1050 | |
| 1051 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1052 | * automatic ids. It's called just after the basic checks. It returns |
| 1053 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1054 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1055 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1056 | { |
| 1057 | int i = 0; |
| 1058 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1059 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1060 | |
| 1061 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1062 | if (ref->unique_id == -1) { |
| 1063 | /* Look for the first free id. */ |
| 1064 | while (1) { |
| 1065 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1066 | if (ref2->unique_id == i) { |
| 1067 | i++; |
| 1068 | break; |
| 1069 | } |
| 1070 | } |
| 1071 | if (&ref2->list == &tlskeys_reference) |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | /* Uses the unique id and increment it for the next entry. */ |
| 1076 | ref->unique_id = i; |
| 1077 | i++; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* This sort the reference list by id. */ |
| 1082 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1083 | LIST_DEL(&ref->list); |
| 1084 | list_for_each_entry(ref3, &tkr, list) { |
| 1085 | if (ref->unique_id < ref3->unique_id) { |
| 1086 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | if (&ref3->list == &tkr) |
| 1091 | LIST_ADDQ(&tkr, &ref->list); |
| 1092 | } |
| 1093 | |
| 1094 | /* swap root */ |
| 1095 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1096 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1097 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1098 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1099 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1100 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1101 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1102 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1103 | { |
| 1104 | switch (evp_keytype) { |
| 1105 | case EVP_PKEY_RSA: |
| 1106 | return 2; |
| 1107 | case EVP_PKEY_DSA: |
| 1108 | return 0; |
| 1109 | case EVP_PKEY_EC: |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1116 | /* |
| 1117 | * Callback used to set OCSP status extension content in server hello. |
| 1118 | */ |
| 1119 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1120 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1121 | struct certificate_ocsp *ocsp; |
| 1122 | struct ocsp_cbk_arg *ocsp_arg; |
| 1123 | char *ssl_buf; |
| 1124 | EVP_PKEY *ssl_pkey; |
| 1125 | int key_type; |
| 1126 | int index; |
| 1127 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1128 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1129 | |
| 1130 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1131 | if (!ssl_pkey) |
| 1132 | return SSL_TLSEXT_ERR_NOACK; |
| 1133 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1134 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1135 | |
| 1136 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1137 | ocsp = ocsp_arg->s_ocsp; |
| 1138 | else { |
| 1139 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1140 | * the certificate type |
| 1141 | */ |
| 1142 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1143 | |
| 1144 | if (index < 0) |
| 1145 | return SSL_TLSEXT_ERR_NOACK; |
| 1146 | |
| 1147 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1148 | |
| 1149 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1150 | |
| 1151 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1152 | !ocsp->response.area || |
| 1153 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1154 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1155 | return SSL_TLSEXT_ERR_NOACK; |
| 1156 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1157 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1158 | if (!ssl_buf) |
| 1159 | return SSL_TLSEXT_ERR_NOACK; |
| 1160 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1161 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1162 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1163 | |
| 1164 | return SSL_TLSEXT_ERR_OK; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1169 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1170 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1171 | * It should be present in the certificate's extra chain builded from file |
| 1172 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1173 | * named 'cert_path' suffixed using '.issuer'. |
| 1174 | * |
| 1175 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1176 | * response. If file is empty or content is not a valid OCSP response, |
| 1177 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1178 | * is displayed). |
| 1179 | * |
| 1180 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1181 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1182 | */ |
| 1183 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1184 | { |
| 1185 | |
| 1186 | BIO *in = NULL; |
| 1187 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1188 | STACK_OF(X509) *chain = NULL; |
| 1189 | OCSP_CERTID *cid = NULL; |
| 1190 | SSL *ssl; |
| 1191 | char ocsp_path[MAXPATHLEN+1]; |
| 1192 | int i, ret = -1; |
| 1193 | struct stat st; |
| 1194 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1195 | char *warn = NULL; |
| 1196 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1197 | pem_password_cb *passwd_cb; |
| 1198 | void *passwd_cb_userdata; |
| 1199 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1200 | |
| 1201 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1202 | |
| 1203 | if (stat(ocsp_path, &st)) |
| 1204 | return 1; |
| 1205 | |
| 1206 | ssl = SSL_new(ctx); |
| 1207 | if (!ssl) |
| 1208 | goto out; |
| 1209 | |
| 1210 | x = SSL_get_certificate(ssl); |
| 1211 | if (!x) |
| 1212 | goto out; |
| 1213 | |
| 1214 | /* Try to lookup for issuer in certificate extra chain */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1215 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1216 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1217 | issuer = sk_X509_value(chain, i); |
| 1218 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1219 | break; |
| 1220 | else |
| 1221 | issuer = NULL; |
| 1222 | } |
| 1223 | |
| 1224 | /* If not found try to load issuer from a suffixed file */ |
| 1225 | if (!issuer) { |
| 1226 | char issuer_path[MAXPATHLEN+1]; |
| 1227 | |
| 1228 | in = BIO_new(BIO_s_file()); |
| 1229 | if (!in) |
| 1230 | goto out; |
| 1231 | |
| 1232 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1233 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1234 | goto out; |
| 1235 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1236 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1237 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1238 | |
| 1239 | xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1240 | if (!xi) |
| 1241 | goto out; |
| 1242 | |
| 1243 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1244 | goto out; |
| 1245 | |
| 1246 | issuer = xi; |
| 1247 | } |
| 1248 | |
| 1249 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1250 | if (!cid) |
| 1251 | goto out; |
| 1252 | |
| 1253 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1254 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1255 | goto out; |
| 1256 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1257 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1258 | if (!ocsp) |
| 1259 | goto out; |
| 1260 | |
| 1261 | p = ocsp->key_data; |
| 1262 | i2d_OCSP_CERTID(cid, &p); |
| 1263 | |
| 1264 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1265 | if (iocsp == ocsp) |
| 1266 | ocsp = NULL; |
| 1267 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1268 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1269 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1270 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1271 | #endif |
| 1272 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1273 | |
| 1274 | if (!callback) { |
William Lallemand | a486449 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1275 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1276 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1277 | |
William Lallemand | a486449 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1278 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1279 | if (!cb_arg) |
| 1280 | goto out; |
| 1281 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1282 | cb_arg->is_single = 1; |
| 1283 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1284 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1285 | pkey = X509_get_pubkey(x); |
| 1286 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1287 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1288 | |
| 1289 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1290 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1291 | } else { |
| 1292 | /* |
| 1293 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1294 | * Update that cb_arg with the new cert's staple |
| 1295 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1296 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1297 | struct certificate_ocsp *tmp_ocsp; |
| 1298 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1299 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1300 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1301 | |
| 1302 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1303 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1304 | #else |
| 1305 | cb_arg = ctx->tlsext_status_arg; |
| 1306 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1307 | |
| 1308 | /* |
| 1309 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1310 | * the order of operations below matter, take care when changing it |
| 1311 | */ |
| 1312 | tmp_ocsp = cb_arg->s_ocsp; |
| 1313 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1314 | cb_arg->s_ocsp = NULL; |
| 1315 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1316 | cb_arg->is_single = 0; |
| 1317 | cb_arg->single_kt = 0; |
| 1318 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1319 | pkey = X509_get_pubkey(x); |
| 1320 | key_type = EVP_PKEY_base_id(pkey); |
| 1321 | EVP_PKEY_free(pkey); |
| 1322 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1323 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1324 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1325 | cb_arg->m_ocsp[index] = iocsp; |
| 1326 | |
| 1327 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1328 | |
| 1329 | ret = 0; |
| 1330 | |
| 1331 | warn = NULL; |
| 1332 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1333 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1334 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | out: |
| 1338 | if (ssl) |
| 1339 | SSL_free(ssl); |
| 1340 | |
| 1341 | if (in) |
| 1342 | BIO_free(in); |
| 1343 | |
| 1344 | if (xi) |
| 1345 | X509_free(xi); |
| 1346 | |
| 1347 | if (cid) |
| 1348 | OCSP_CERTID_free(cid); |
| 1349 | |
| 1350 | if (ocsp) |
| 1351 | free(ocsp); |
| 1352 | |
| 1353 | if (warn) |
| 1354 | free(warn); |
| 1355 | |
| 1356 | |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
| 1360 | #endif |
| 1361 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1362 | #ifdef OPENSSL_IS_BORINGSSL |
| 1363 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1364 | { |
| 1365 | char ocsp_path[MAXPATHLEN+1]; |
| 1366 | struct stat st; |
| 1367 | int fd = -1, r = 0; |
| 1368 | |
| 1369 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1370 | if (stat(ocsp_path, &st)) |
| 1371 | return 0; |
| 1372 | |
| 1373 | fd = open(ocsp_path, O_RDONLY); |
| 1374 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1375 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1376 | return -1; |
| 1377 | } |
| 1378 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1379 | trash.data = 0; |
| 1380 | while (trash.data < trash.size) { |
| 1381 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1382 | if (r < 0) { |
| 1383 | if (errno == EINTR) |
| 1384 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1385 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1386 | close(fd); |
| 1387 | return -1; |
| 1388 | } |
| 1389 | else if (r == 0) { |
| 1390 | break; |
| 1391 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1392 | trash.data += r; |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1393 | } |
| 1394 | close(fd); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1395 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area, |
| 1396 | trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1397 | } |
| 1398 | #endif |
| 1399 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1400 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1401 | |
| 1402 | #define CT_EXTENSION_TYPE 18 |
| 1403 | |
| 1404 | static int sctl_ex_index = -1; |
| 1405 | |
| 1406 | /* |
| 1407 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1408 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1409 | * is performed. |
| 1410 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1411 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1412 | { |
| 1413 | int ret = 1; |
| 1414 | int len, pos, sct_len; |
| 1415 | unsigned char *data; |
| 1416 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1417 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1418 | goto out; |
| 1419 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1420 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1421 | len = (data[0] << 8) | data[1]; |
| 1422 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1423 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1424 | goto out; |
| 1425 | |
| 1426 | data = data + 2; |
| 1427 | pos = 0; |
| 1428 | while (pos < len) { |
| 1429 | if (len - pos < 2) |
| 1430 | goto out; |
| 1431 | |
| 1432 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1433 | if (pos + sct_len + 2 > len) |
| 1434 | goto out; |
| 1435 | |
| 1436 | pos += sct_len + 2; |
| 1437 | } |
| 1438 | |
| 1439 | ret = 0; |
| 1440 | |
| 1441 | out: |
| 1442 | return ret; |
| 1443 | } |
| 1444 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1445 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, |
| 1446 | struct buffer **sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1447 | { |
| 1448 | int fd = -1; |
| 1449 | int r = 0; |
| 1450 | int ret = 1; |
| 1451 | |
| 1452 | *sctl = NULL; |
| 1453 | |
| 1454 | fd = open(sctl_path, O_RDONLY); |
| 1455 | if (fd == -1) |
| 1456 | goto end; |
| 1457 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1458 | trash.data = 0; |
| 1459 | while (trash.data < trash.size) { |
| 1460 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1461 | if (r < 0) { |
| 1462 | if (errno == EINTR) |
| 1463 | continue; |
| 1464 | |
| 1465 | goto end; |
| 1466 | } |
| 1467 | else if (r == 0) { |
| 1468 | break; |
| 1469 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1470 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | ret = ssl_sock_parse_sctl(&trash); |
| 1474 | if (ret) |
| 1475 | goto end; |
| 1476 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1477 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1478 | if (!chunk_dup(*sctl, &trash)) { |
| 1479 | free(*sctl); |
| 1480 | *sctl = NULL; |
| 1481 | goto end; |
| 1482 | } |
| 1483 | |
| 1484 | end: |
| 1485 | if (fd != -1) |
| 1486 | close(fd); |
| 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1492 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1493 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1494 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1495 | *out = (unsigned char *) sctl->area; |
| 1496 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1497 | |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
| 1501 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1502 | { |
| 1503 | return 1; |
| 1504 | } |
| 1505 | |
| 1506 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1507 | { |
| 1508 | char sctl_path[MAXPATHLEN+1]; |
| 1509 | int ret = -1; |
| 1510 | struct stat st; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1511 | struct buffer *sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1512 | |
| 1513 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1514 | |
| 1515 | if (stat(sctl_path, &st)) |
| 1516 | return 1; |
| 1517 | |
| 1518 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1519 | goto out; |
| 1520 | |
| 1521 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1522 | free(sctl); |
| 1523 | goto out; |
| 1524 | } |
| 1525 | |
| 1526 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1527 | |
| 1528 | ret = 0; |
| 1529 | |
| 1530 | out: |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | #endif |
| 1535 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1536 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1537 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1538 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1539 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1540 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1541 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1542 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1543 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1544 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1545 | * change this to #if and do not assign a default value to this macro! |
| 1546 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1547 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1548 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1549 | if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1550 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1551 | conn->err_code = CO_ER_SSL_RENEG; |
| 1552 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1553 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1554 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1555 | |
| 1556 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1557 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1558 | /* Long certificate chains optimz |
| 1559 | If write and read bios are differents, we |
| 1560 | consider that the buffering was activated, |
| 1561 | so we rise the output buffer size from 4k |
| 1562 | to 16k */ |
| 1563 | write_bio = SSL_get_wbio(ssl); |
| 1564 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1565 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1566 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1572 | /* Callback is called for each certificate of the chain during a verify |
| 1573 | ok is set to 1 if preverify detect no error on current certificate. |
| 1574 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1575 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1576 | { |
| 1577 | SSL *ssl; |
| 1578 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1579 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1580 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1581 | |
| 1582 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1583 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1584 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1585 | ctx = conn->xprt_ctx; |
| 1586 | |
| 1587 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1588 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1589 | if (ok) /* no errors */ |
| 1590 | return ok; |
| 1591 | |
| 1592 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1593 | err = X509_STORE_CTX_get_error(x_store); |
| 1594 | |
| 1595 | /* check if CA error needs to be ignored */ |
| 1596 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1597 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1598 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1599 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Willy Tarreau | a9d299f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1602 | if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1603 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1604 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1605 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1606 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1607 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1608 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1612 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1613 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1614 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1615 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | a9d299f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1616 | if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1617 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1618 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1619 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1620 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1622 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1623 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1626 | static inline |
| 1627 | void ssl_sock_parse_clienthello(int write_p, int version, int content_type, |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1628 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1629 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1630 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1631 | unsigned char *msg; |
| 1632 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1633 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1634 | |
| 1635 | /* This function is called for "from client" and "to server" |
| 1636 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1637 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1638 | */ |
| 1639 | |
| 1640 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1641 | * otherwise it is set to 1. |
| 1642 | */ |
| 1643 | if (write_p != 0) |
| 1644 | return; |
| 1645 | |
| 1646 | /* content_type contains the type of message received or sent |
| 1647 | * according with the SSL/TLS protocol spec. This message is |
| 1648 | * encoded with one byte. The value 256 (two bytes) is used |
| 1649 | * for designing the SSL/TLS record layer. According with the |
| 1650 | * rfc6101, the expected message (other than 256) are: |
| 1651 | * - change_cipher_spec(20) |
| 1652 | * - alert(21) |
| 1653 | * - handshake(22) |
| 1654 | * - application_data(23) |
| 1655 | * - (255) |
| 1656 | * We are interessed by the handshake and specially the client |
| 1657 | * hello. |
| 1658 | */ |
| 1659 | if (content_type != 22) |
| 1660 | return; |
| 1661 | |
| 1662 | /* The message length is at least 4 bytes, containing the |
| 1663 | * message type and the message length. |
| 1664 | */ |
| 1665 | if (len < 4) |
| 1666 | return; |
| 1667 | |
| 1668 | /* First byte of the handshake message id the type of |
| 1669 | * message. The konwn types are: |
| 1670 | * - hello_request(0) |
| 1671 | * - client_hello(1) |
| 1672 | * - server_hello(2) |
| 1673 | * - certificate(11) |
| 1674 | * - server_key_exchange (12) |
| 1675 | * - certificate_request(13) |
| 1676 | * - server_hello_done(14) |
| 1677 | * We are interested by the client hello. |
| 1678 | */ |
| 1679 | msg = (unsigned char *)buf; |
| 1680 | if (msg[0] != 1) |
| 1681 | return; |
| 1682 | |
| 1683 | /* Next three bytes are the length of the message. The total length |
| 1684 | * must be this decoded length + 4. If the length given as argument |
| 1685 | * is not the same, we abort the protocol dissector. |
| 1686 | */ |
| 1687 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1688 | if (len < rec_len + 4) |
| 1689 | return; |
| 1690 | msg += 4; |
| 1691 | end = msg + rec_len; |
| 1692 | if (end < msg) |
| 1693 | return; |
| 1694 | |
| 1695 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1696 | * for minor, the random, composed by 4 bytes for the unix time and |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1697 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1698 | */ |
| 1699 | msg += 1 + 1 + 4 + 28; |
| 1700 | if (msg > end) |
| 1701 | return; |
| 1702 | |
| 1703 | /* Next, is session id: |
| 1704 | * if present, we have to jump by length + 1 for the size information |
| 1705 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1706 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1707 | if (msg[0] > 0) |
| 1708 | msg += msg[0]; |
| 1709 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1710 | if (msg > end) |
| 1711 | return; |
| 1712 | |
| 1713 | /* Next two bytes are the ciphersuite length. */ |
| 1714 | if (msg + 2 > end) |
| 1715 | return; |
| 1716 | rec_len = (msg[0] << 8) + msg[1]; |
| 1717 | msg += 2; |
| 1718 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1719 | return; |
| 1720 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1721 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1722 | if (!capture) |
| 1723 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1724 | /* Compute the xxh64 of the ciphersuite. */ |
| 1725 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1726 | |
| 1727 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1728 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1729 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1730 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1731 | |
| 1732 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1733 | } |
| 1734 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1735 | /* Callback is called for ssl protocol analyse */ |
| 1736 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1737 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1738 | #ifdef TLS1_RT_HEARTBEAT |
| 1739 | /* test heartbeat received (write_p is set to 0 |
| 1740 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1741 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1742 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1743 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1744 | const unsigned char *p = buf; |
| 1745 | unsigned int payload; |
| 1746 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1747 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1750 | if (*p != TLS1_HB_REQUEST) |
| 1751 | return; |
| 1752 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1753 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1754 | goto kill_it; |
| 1755 | |
| 1756 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1757 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1758 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1759 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1760 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1761 | * advertised payload is larger than the advertised packet |
| 1762 | * length, so we have garbage in the buffer between the |
| 1763 | * payload and the end of the buffer (p+len). We can't know |
| 1764 | * if the SSL stack is patched, and we don't know if we can |
| 1765 | * safely wipe out the area between p+3+len and payload. |
| 1766 | * So instead, we prevent the response from being sent by |
| 1767 | * setting the max_send_fragment to 0 and we report an SSL |
| 1768 | * error, which will kill this connection. It will be reported |
| 1769 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1770 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1771 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1772 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1773 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1774 | return; |
| 1775 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1776 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1777 | if (global_ssl.capture_cipherlist > 0) |
| 1778 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1781 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1782 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1783 | const unsigned char *in, unsigned int inlen, |
| 1784 | void *arg) |
| 1785 | { |
| 1786 | struct server *srv = arg; |
| 1787 | |
| 1788 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1789 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1790 | return SSL_TLSEXT_ERR_OK; |
| 1791 | return SSL_TLSEXT_ERR_NOACK; |
| 1792 | } |
| 1793 | #endif |
| 1794 | |
| 1795 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1796 | /* This callback is used so that the server advertises the list of |
| 1797 | * negociable protocols for NPN. |
| 1798 | */ |
| 1799 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1800 | unsigned int *len, void *arg) |
| 1801 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1802 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1803 | |
| 1804 | *data = (const unsigned char *)conf->npn_str; |
| 1805 | *len = conf->npn_len; |
| 1806 | return SSL_TLSEXT_ERR_OK; |
| 1807 | } |
| 1808 | #endif |
| 1809 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1810 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1811 | /* This callback is used so that the server advertises the list of |
| 1812 | * negociable protocols for ALPN. |
| 1813 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1814 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1815 | unsigned char *outlen, |
| 1816 | const unsigned char *server, |
| 1817 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1818 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1819 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1820 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1821 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1822 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1823 | return SSL_TLSEXT_ERR_NOACK; |
| 1824 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1825 | return SSL_TLSEXT_ERR_OK; |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1829 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1830 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1831 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1832 | /* Create a X509 certificate with the specified servername and serial. This |
| 1833 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1834 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1835 | ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1836 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1837 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1838 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1839 | SSL_CTX *ssl_ctx = NULL; |
| 1840 | X509 *newcrt = NULL; |
| 1841 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1842 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1843 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1844 | X509_NAME *name; |
| 1845 | const EVP_MD *digest; |
| 1846 | X509V3_CTX ctx; |
| 1847 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1848 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1849 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1850 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1851 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1852 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1853 | #else |
| 1854 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1855 | if (tmp_ssl) |
| 1856 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1857 | #endif |
| 1858 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1859 | goto mkcert_error; |
| 1860 | |
| 1861 | /* Create the certificate */ |
| 1862 | if (!(newcrt = X509_new())) |
| 1863 | goto mkcert_error; |
| 1864 | |
| 1865 | /* Set version number for the certificate (X509v3) and the serial |
| 1866 | * number */ |
| 1867 | if (X509_set_version(newcrt, 2L) != 1) |
| 1868 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1869 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1870 | |
| 1871 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1872 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1873 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1874 | goto mkcert_error; |
| 1875 | |
| 1876 | /* set public key in the certificate */ |
| 1877 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1878 | goto mkcert_error; |
| 1879 | |
| 1880 | /* Set issuer name from the CA */ |
| 1881 | if (!(name = X509_get_subject_name(cacert))) |
| 1882 | goto mkcert_error; |
| 1883 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1884 | goto mkcert_error; |
| 1885 | |
| 1886 | /* Set the subject name using the same, but the CN */ |
| 1887 | name = X509_NAME_dup(name); |
| 1888 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1889 | (const unsigned char *)servername, |
| 1890 | -1, -1, 0) != 1) { |
| 1891 | X509_NAME_free(name); |
| 1892 | goto mkcert_error; |
| 1893 | } |
| 1894 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1895 | X509_NAME_free(name); |
| 1896 | goto mkcert_error; |
| 1897 | } |
| 1898 | X509_NAME_free(name); |
| 1899 | |
| 1900 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1901 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1902 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1903 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1904 | X509_EXTENSION *ext; |
| 1905 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1906 | if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1907 | goto mkcert_error; |
| 1908 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1909 | X509_EXTENSION_free(ext); |
| 1910 | goto mkcert_error; |
| 1911 | } |
| 1912 | X509_EXTENSION_free(ext); |
| 1913 | } |
| 1914 | |
| 1915 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1916 | |
| 1917 | key_type = EVP_PKEY_base_id(capkey); |
| 1918 | |
| 1919 | if (key_type == EVP_PKEY_DSA) |
| 1920 | digest = EVP_sha1(); |
| 1921 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1922 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1923 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1924 | digest = EVP_sha256(); |
| 1925 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1926 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1927 | int nid; |
| 1928 | |
| 1929 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1930 | goto mkcert_error; |
| 1931 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1932 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1933 | #else |
| 1934 | goto mkcert_error; |
| 1935 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1936 | } |
| 1937 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1938 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1939 | goto mkcert_error; |
| 1940 | |
| 1941 | /* Create and set the new SSL_CTX */ |
| 1942 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1943 | goto mkcert_error; |
| 1944 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1945 | goto mkcert_error; |
| 1946 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1947 | goto mkcert_error; |
| 1948 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1949 | goto mkcert_error; |
| 1950 | |
| 1951 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1952 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1953 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1954 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1955 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1956 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1957 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1958 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1959 | EC_KEY *ecc; |
| 1960 | int nid; |
| 1961 | |
| 1962 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1963 | goto end; |
| 1964 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1965 | goto end; |
| 1966 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1967 | EC_KEY_free(ecc); |
| 1968 | } |
| 1969 | #endif |
| 1970 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1971 | return ssl_ctx; |
| 1972 | |
| 1973 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1974 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1975 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1976 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1977 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1978 | return NULL; |
| 1979 | } |
| 1980 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1981 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1982 | ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1983 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1984 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1985 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1986 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1987 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1988 | } |
| 1989 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1990 | /* Do a lookup for a certificate in the LRU cache used to store generated |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1991 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1992 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1993 | ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1994 | { |
| 1995 | struct lru64 *lru = NULL; |
| 1996 | |
| 1997 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1998 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1999 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2000 | if (lru && lru->domain) { |
| 2001 | if (ssl) |
| 2002 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2003 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2004 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2005 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2006 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2007 | } |
| 2008 | return NULL; |
| 2009 | } |
| 2010 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2011 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2012 | * function is not thread-safe, it should only be used to check if a certificate |
| 2013 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2014 | * thread). It is kept for backward compatibility. */ |
| 2015 | SSL_CTX * |
| 2016 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2017 | { |
| 2018 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2019 | } |
| 2020 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2021 | /* Set a certificate int the LRU cache used to store generated |
| 2022 | * certificate. Return 0 on success, otherwise -1 */ |
| 2023 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2024 | ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2025 | { |
| 2026 | struct lru64 *lru = NULL; |
| 2027 | |
| 2028 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2029 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2030 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2031 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2032 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2033 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2034 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2035 | if (lru->domain && lru->data) |
| 2036 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2037 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2038 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2039 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2040 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2041 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2044 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2045 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2046 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2047 | { |
| 2048 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2049 | } |
| 2050 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2051 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2052 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2053 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2054 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2055 | ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2056 | { |
| 2057 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2058 | SSL_CTX *ssl_ctx = NULL; |
| 2059 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2060 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2061 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2062 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2063 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2064 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2065 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2066 | if (lru && lru->domain) |
| 2067 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2068 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2069 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2070 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2071 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2072 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2073 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2074 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2075 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2076 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2077 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2078 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2079 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2080 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2081 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2082 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2083 | return 0; |
| 2084 | } |
| 2085 | static int |
| 2086 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2087 | { |
| 2088 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2089 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2090 | |
| 2091 | conn_get_to_addr(conn); |
| 2092 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 2093 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2094 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2095 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2096 | } |
| 2097 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2098 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2099 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2100 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2101 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2102 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2103 | |
| 2104 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2105 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2106 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2107 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2108 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2109 | #endif |
| 2110 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2111 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2112 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2113 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2114 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2115 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2116 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2117 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2118 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2119 | #endif |
| 2120 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2121 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2122 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2123 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2124 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2125 | #endif |
| 2126 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2127 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2128 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2129 | /* Unusable in this context. */ |
| 2130 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2131 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2132 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2133 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2134 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2135 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2136 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2137 | |
| 2138 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2139 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2140 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2141 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2142 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2143 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2144 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2145 | } |
| 2146 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2147 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2148 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2149 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2150 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2151 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2152 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2153 | } |
| 2154 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2155 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2156 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2157 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2158 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2159 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2160 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2161 | } |
| 2162 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2163 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2164 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2165 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2166 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2167 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2168 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2169 | } |
| 2170 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2171 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2172 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2173 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2174 | #endif |
| 2175 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2176 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2177 | #if SSL_OP_NO_TLSv1_3 |
| 2178 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2179 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2180 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2181 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2182 | #endif |
| 2183 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2184 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2185 | |
| 2186 | static struct { |
| 2187 | int option; |
| 2188 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2189 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2190 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2191 | const char *name; |
| 2192 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2193 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2194 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2195 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2196 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2197 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2198 | {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2199 | }; |
| 2200 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2201 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2202 | { |
| 2203 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2204 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2205 | SSL_set_SSL_CTX(ssl, ctx); |
| 2206 | } |
| 2207 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2208 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2209 | |
| 2210 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2211 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2212 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2213 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2214 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2215 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2216 | return SSL_TLSEXT_ERR_OK; |
| 2217 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2220 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2221 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2222 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2223 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2224 | #else |
| 2225 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2226 | { |
| 2227 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2228 | struct connection *conn; |
| 2229 | struct bind_conf *s; |
| 2230 | const uint8_t *extension_data; |
| 2231 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2232 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2233 | |
| 2234 | char *wildp = NULL; |
| 2235 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2236 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2237 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2238 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2239 | int i; |
| 2240 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2241 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2242 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2243 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2244 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2245 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2246 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2247 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2248 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2249 | #else |
| 2250 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2251 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2252 | /* |
| 2253 | * The server_name extension was given too much extensibility when it |
| 2254 | * was written, so parsing the normal case is a bit complex. |
| 2255 | */ |
| 2256 | size_t len; |
| 2257 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2258 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2259 | /* Extract the length of the supplied list of names. */ |
| 2260 | len = (*extension_data++) << 8; |
| 2261 | len |= *extension_data++; |
| 2262 | if (len + 2 != extension_len) |
| 2263 | goto abort; |
| 2264 | /* |
| 2265 | * The list in practice only has a single element, so we only consider |
| 2266 | * the first one. |
| 2267 | */ |
| 2268 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2269 | goto abort; |
| 2270 | extension_len = len - 1; |
| 2271 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2272 | if (extension_len <= 2) |
| 2273 | goto abort; |
| 2274 | len = (*extension_data++) << 8; |
| 2275 | len |= *extension_data++; |
| 2276 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2277 | || memchr(extension_data, 0, len) != NULL) |
| 2278 | goto abort; |
| 2279 | servername = extension_data; |
| 2280 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2281 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2282 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2283 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2284 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2285 | } |
| 2286 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2287 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2288 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2289 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2290 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2291 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2292 | goto abort; |
| 2293 | } |
| 2294 | |
| 2295 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2296 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2297 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2298 | #else |
| 2299 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2300 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2301 | uint8_t sign; |
| 2302 | size_t len; |
| 2303 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2304 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2305 | len = (*extension_data++) << 8; |
| 2306 | len |= *extension_data++; |
| 2307 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2308 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2309 | if (len % 2 != 0) |
| 2310 | goto abort; |
| 2311 | for (; len > 0; len -= 2) { |
| 2312 | extension_data++; /* hash */ |
| 2313 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2314 | switch (sign) { |
| 2315 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2316 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2317 | break; |
| 2318 | case TLSEXT_signature_ecdsa: |
| 2319 | has_ecdsa_sig = 1; |
| 2320 | break; |
| 2321 | default: |
| 2322 | continue; |
| 2323 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2324 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2328 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2329 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2330 | } |
| 2331 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2332 | const SSL_CIPHER *cipher; |
| 2333 | size_t len; |
| 2334 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2335 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2336 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2337 | len = ctx->cipher_suites_len; |
| 2338 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2339 | #else |
| 2340 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2341 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2342 | if (len % 2 != 0) |
| 2343 | goto abort; |
| 2344 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2345 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2346 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2347 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2348 | #else |
| 2349 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2350 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2351 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2352 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2353 | break; |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2358 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2359 | trash.area[i] = tolower(servername[i]); |
| 2360 | if (!wildp && (trash.area[i] == '.')) |
| 2361 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2362 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2363 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2364 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | |
William Lallemand | 6c98f49 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2366 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2367 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | cc957c3 | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2368 | for (i = 0; i < 2; i++) { |
| 2369 | if (i == 0) /* lookup in full qualified names */ |
| 2370 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2371 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2372 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2373 | else |
| 2374 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2375 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | cc957c3 | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2376 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2377 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2378 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2379 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2380 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2381 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2382 | break; |
| 2383 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2384 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2385 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2386 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2387 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2388 | if (!node_anonymous) |
| 2389 | node_anonymous = n; |
| 2390 | break; |
| 2391 | } |
| 2392 | } |
| 2393 | } |
William Lallemand | 6c98f49 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2394 | } |
| 2395 | /* Once the certificates are found, select them depending on what is |
| 2396 | * supported in the client and by key_signature priority order: EDSA > |
| 2397 | * RSA > DSA */ |
| 2398 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2399 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2400 | : (node_anonymous ? node_anonymous |
| 2401 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2402 | : node_rsa /* no rsa signature case (far far away) */ |
| 2403 | ))); |
| 2404 | if (node) { |
| 2405 | /* switch ctx */ |
| 2406 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2407 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2408 | if (conf) { |
| 2409 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2410 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2411 | if (conf->early_data) |
| 2412 | allow_early = 1; |
Emmanuel Hocdet | cc957c3 | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2413 | } |
William Lallemand | 6c98f49 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2414 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2415 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2416 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2417 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2418 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2419 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2420 | } |
| 2421 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2422 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2423 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2424 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2425 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2426 | allow_early: |
| 2427 | #ifdef OPENSSL_IS_BORINGSSL |
| 2428 | if (allow_early) |
| 2429 | SSL_set_early_data_enabled(ssl, 1); |
| 2430 | #else |
| 2431 | if (!allow_early) |
| 2432 | SSL_set_max_early_data(ssl, 0); |
| 2433 | #endif |
| 2434 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2435 | abort: |
| 2436 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2437 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2438 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2439 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2440 | #else |
| 2441 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2442 | return 0; |
| 2443 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2447 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2448 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2449 | * warning when no match is found, which implies the default (first) cert |
| 2450 | * will keep being used. |
| 2451 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2452 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2453 | { |
| 2454 | const char *servername; |
| 2455 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2456 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2457 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2458 | int i; |
| 2459 | (void)al; /* shut gcc stupid warning */ |
| 2460 | |
| 2461 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2462 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2463 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2464 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2465 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2466 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2467 | if (s->strict_sni) |
| 2468 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2469 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2470 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2471 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2472 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2473 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2474 | if (!servername[i]) |
| 2475 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2476 | trash.area[i] = tolower(servername[i]); |
| 2477 | if (!wildp && (trash.area[i] == '.')) |
| 2478 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2479 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2480 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2481 | |
Emmanuel Hocdet | 26b7b80 | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2482 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2483 | /* lookup in full qualified names */ |
Emmanuel Hocdet | 26b7b80 | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2484 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2485 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2486 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2487 | node = n; |
| 2488 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2489 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2490 | } |
| 2491 | if (!node && wildp) { |
| 2492 | /* lookup in wildcards names */ |
Emmanuel Hocdet | 26b7b80 | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2493 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2494 | /* lookup a not neg filter */ |
| 2495 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2496 | node = n; |
| 2497 | break; |
| 2498 | } |
| 2499 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2500 | } |
Emmanuel Hocdet | 26b7b80 | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2501 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2502 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2503 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2504 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2505 | return SSL_TLSEXT_ERR_OK; |
| 2506 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2507 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2508 | if (s->strict_sni) |
| 2509 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2510 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2511 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2515 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2516 | return SSL_TLSEXT_ERR_OK; |
| 2517 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2518 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2519 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2520 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2521 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2522 | |
| 2523 | static DH * ssl_get_dh_1024(void) |
| 2524 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2525 | static unsigned char dh1024_p[]={ |
| 2526 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2527 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2528 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2529 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2530 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2531 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2532 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2533 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2534 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2535 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2536 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2537 | }; |
| 2538 | static unsigned char dh1024_g[]={ |
| 2539 | 0x02, |
| 2540 | }; |
| 2541 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2542 | BIGNUM *p; |
| 2543 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2544 | DH *dh = DH_new(); |
| 2545 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2546 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2547 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2548 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2549 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2550 | DH_free(dh); |
| 2551 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2552 | } else { |
| 2553 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | return dh; |
| 2557 | } |
| 2558 | |
| 2559 | static DH *ssl_get_dh_2048(void) |
| 2560 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2561 | static unsigned char dh2048_p[]={ |
| 2562 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2563 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2564 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2565 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2566 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2567 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2568 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2569 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2570 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2571 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2572 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2573 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2574 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2575 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2576 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2577 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2578 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2579 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2580 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2581 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2582 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2583 | 0xB7,0x1F,0x77,0xF3, |
| 2584 | }; |
| 2585 | static unsigned char dh2048_g[]={ |
| 2586 | 0x02, |
| 2587 | }; |
| 2588 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2589 | BIGNUM *p; |
| 2590 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2591 | DH *dh = DH_new(); |
| 2592 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2593 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2594 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2595 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2596 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2597 | DH_free(dh); |
| 2598 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2599 | } else { |
| 2600 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | return dh; |
| 2604 | } |
| 2605 | |
| 2606 | static DH *ssl_get_dh_4096(void) |
| 2607 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2608 | static unsigned char dh4096_p[]={ |
| 2609 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2610 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2611 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2612 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2613 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2614 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2615 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2616 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2617 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2618 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2619 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2620 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2621 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2622 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2623 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2624 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2625 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2626 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2627 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2628 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2629 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2630 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2631 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2632 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2633 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2634 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2635 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2636 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2637 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2638 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2639 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2640 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2641 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2642 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2643 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2644 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2645 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2646 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2647 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2648 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2649 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2650 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2651 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2652 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2653 | static unsigned char dh4096_g[]={ |
| 2654 | 0x02, |
| 2655 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2656 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2657 | BIGNUM *p; |
| 2658 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2659 | DH *dh = DH_new(); |
| 2660 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2661 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2662 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2663 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2664 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2665 | DH_free(dh); |
| 2666 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2667 | } else { |
| 2668 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | return dh; |
| 2672 | } |
| 2673 | |
| 2674 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2675 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2676 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2677 | { |
| 2678 | DH *dh = NULL; |
| 2679 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2680 | int type; |
| 2681 | |
| 2682 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2683 | |
| 2684 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2685 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2686 | */ |
| 2687 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2688 | keylen = EVP_PKEY_bits(pkey); |
| 2689 | } |
| 2690 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2691 | if (keylen > global_ssl.default_dh_param) { |
| 2692 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2693 | } |
| 2694 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2695 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2696 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2697 | } |
| 2698 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2699 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2700 | } |
| 2701 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2702 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | return dh; |
| 2706 | } |
| 2707 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2708 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2709 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2710 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2711 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2712 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2713 | if (in == NULL) |
| 2714 | goto end; |
| 2715 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2716 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2717 | goto end; |
| 2718 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2719 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2720 | |
| 2721 | end: |
| 2722 | if (in) |
| 2723 | BIO_free(in); |
| 2724 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2725 | ERR_clear_error(); |
| 2726 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2727 | return dh; |
| 2728 | } |
| 2729 | |
| 2730 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2731 | { |
| 2732 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2733 | |
| 2734 | if (global_dh) { |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | return -1; |
| 2739 | } |
| 2740 | |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2741 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
| 2742 | * If there is no DH paramater availaible in the ckchs, the global |
| 2743 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2744 | * DH parameter available in ckchs nor in global, the default |
| 2745 | * DH parameters are applied on the SSL_CTX. |
| 2746 | * Returns a bitfield containing the flags: |
| 2747 | * ERR_FATAL in any fatal error case |
| 2748 | * ERR_ALERT if a reason of the error is availabine in err |
| 2749 | * ERR_WARN if a warning is available into err |
| 2750 | * The value 0 means there is no error nor warning and |
| 2751 | * the operation succeed. |
| 2752 | */ |
| 2753 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file, char **err) |
| 2754 | { |
| 2755 | int ret = 0; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2756 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2757 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2758 | if (dh) { |
Emeric Brun | d6de151 | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2759 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2760 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2761 | err && *err ? *err : "", file); |
| 2762 | #if defined(SSL_CTX_set_dh_auto) |
| 2763 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2764 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2765 | err && *err ? *err : ""); |
| 2766 | #else |
| 2767 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2768 | err && *err ? *err : ""); |
| 2769 | #endif |
| 2770 | ret |= ERR_WARN; |
| 2771 | goto end; |
| 2772 | } |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2773 | |
| 2774 | if (ssl_dh_ptr_index >= 0) { |
| 2775 | /* store a pointer to the DH params to avoid complaining about |
| 2776 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2777 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2778 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2779 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2780 | else if (global_dh) { |
Emeric Brun | d6de151 | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2781 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2782 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2783 | err && *err ? *err : "", file); |
| 2784 | #if defined(SSL_CTX_set_dh_auto) |
| 2785 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2786 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2787 | err && *err ? *err : ""); |
| 2788 | #else |
| 2789 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2790 | err && *err ? *err : ""); |
| 2791 | #endif |
| 2792 | ret |= ERR_WARN; |
| 2793 | goto end; |
| 2794 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2795 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2796 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2797 | /* Clear openssl global errors stack */ |
| 2798 | ERR_clear_error(); |
| 2799 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2800 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2801 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2802 | if (local_dh_1024 == NULL) |
| 2803 | local_dh_1024 = ssl_get_dh_1024(); |
| 2804 | |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2805 | if (local_dh_1024 == NULL) { |
| 2806 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2807 | err && *err ? *err : "", file); |
| 2808 | ret |= ERR_ALERT | ERR_FATAL; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2809 | goto end; |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2810 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2811 | |
Emeric Brun | d6de151 | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2812 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 2813 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2814 | err && *err ? *err : "", file); |
| 2815 | #if defined(SSL_CTX_set_dh_auto) |
| 2816 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2817 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2818 | err && *err ? *err : ""); |
| 2819 | #else |
| 2820 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2821 | err && *err ? *err : ""); |
| 2822 | #endif |
| 2823 | ret |= ERR_WARN; |
| 2824 | goto end; |
| 2825 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2826 | } |
| 2827 | else { |
| 2828 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2829 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2830 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2831 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2832 | end: |
| 2833 | if (dh) |
| 2834 | DH_free(dh); |
| 2835 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2836 | return ret; |
| 2837 | } |
| 2838 | #endif |
| 2839 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2840 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2841 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2842 | { |
| 2843 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2844 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2845 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2846 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2847 | if (*name == '!') { |
| 2848 | neg = 1; |
| 2849 | name++; |
| 2850 | } |
| 2851 | if (*name == '*') { |
| 2852 | wild = 1; |
| 2853 | name++; |
| 2854 | } |
| 2855 | /* !* filter is a nop */ |
| 2856 | if (neg && wild) |
| 2857 | return order; |
| 2858 | if (*name) { |
| 2859 | int j, len; |
| 2860 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2861 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2862 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2863 | if (j >= trash.size) |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2864 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2865 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* Check for duplicates. */ |
| 2868 | if (wild) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2869 | node = ebst_lookup(&s->sni_w_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2870 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2871 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2872 | for (; node; node = ebmb_next_dup(node)) { |
| 2873 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2874 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2875 | return order; |
| 2876 | } |
| 2877 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2878 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2879 | if (!sc) |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2880 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2881 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2882 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2883 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2884 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2885 | sc->order = order++; |
| 2886 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2887 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2888 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2889 | if (wild) |
| 2890 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2891 | else |
| 2892 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2893 | } |
| 2894 | return order; |
| 2895 | } |
| 2896 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2897 | |
| 2898 | /* The following code is used for loading multiple crt files into |
| 2899 | * SSL_CTX's based on CN/SAN |
| 2900 | */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2901 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2902 | /* This is used to preload the certifcate, private key |
| 2903 | * and Cert Chain of a file passed in via the crt |
| 2904 | * argument |
| 2905 | * |
| 2906 | * This way, we do not have to read the file multiple times |
| 2907 | */ |
| 2908 | struct cert_key_and_chain { |
| 2909 | X509 *cert; |
| 2910 | EVP_PKEY *key; |
| 2911 | unsigned int num_chain_certs; |
| 2912 | /* This is an array of X509 pointers */ |
| 2913 | X509 **chain_certs; |
| 2914 | }; |
| 2915 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2916 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2917 | |
| 2918 | struct key_combo_ctx { |
| 2919 | SSL_CTX *ctx; |
| 2920 | int order; |
| 2921 | }; |
| 2922 | |
| 2923 | /* Map used for processing multiple keypairs for a single purpose |
| 2924 | * |
| 2925 | * This maps CN/SNI name to certificate type |
| 2926 | */ |
| 2927 | struct sni_keytype { |
| 2928 | int keytypes; /* BITMASK for keytypes */ |
| 2929 | struct ebmb_node name; /* node holding the servername value */ |
| 2930 | }; |
| 2931 | |
| 2932 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2933 | /* Frees the contents of a cert_key_and_chain |
| 2934 | */ |
| 2935 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2936 | { |
| 2937 | int i; |
| 2938 | |
| 2939 | if (!ckch) |
| 2940 | return; |
| 2941 | |
| 2942 | /* Free the certificate and set pointer to NULL */ |
| 2943 | if (ckch->cert) |
| 2944 | X509_free(ckch->cert); |
| 2945 | ckch->cert = NULL; |
| 2946 | |
| 2947 | /* Free the key and set pointer to NULL */ |
| 2948 | if (ckch->key) |
| 2949 | EVP_PKEY_free(ckch->key); |
| 2950 | ckch->key = NULL; |
| 2951 | |
| 2952 | /* Free each certificate in the chain */ |
| 2953 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2954 | if (ckch->chain_certs[i]) |
| 2955 | X509_free(ckch->chain_certs[i]); |
| 2956 | } |
| 2957 | |
| 2958 | /* Free the chain obj itself and set to NULL */ |
| 2959 | if (ckch->num_chain_certs > 0) { |
| 2960 | free(ckch->chain_certs); |
| 2961 | ckch->num_chain_certs = 0; |
| 2962 | ckch->chain_certs = NULL; |
| 2963 | } |
| 2964 | |
| 2965 | } |
| 2966 | |
| 2967 | /* checks if a key and cert exists in the ckch |
| 2968 | */ |
| 2969 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2970 | { |
| 2971 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2972 | } |
| 2973 | |
| 2974 | |
| 2975 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2976 | * This allows us to carry the contents of the file without having to |
| 2977 | * read the file multiple times. |
| 2978 | * |
| 2979 | * returns: |
| 2980 | * 0 on Success |
| 2981 | * 1 on SSL Failure |
| 2982 | * 2 on file not found |
| 2983 | */ |
| 2984 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2985 | { |
| 2986 | |
| 2987 | BIO *in; |
| 2988 | X509 *ca = NULL; |
| 2989 | int ret = 1; |
| 2990 | |
| 2991 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2992 | |
| 2993 | in = BIO_new(BIO_s_file()); |
| 2994 | if (in == NULL) |
| 2995 | goto end; |
| 2996 | |
| 2997 | if (BIO_read_filename(in, path) <= 0) |
| 2998 | goto end; |
| 2999 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3000 | /* Read Private Key */ |
| 3001 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3002 | if (ckch->key == NULL) { |
| 3003 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3004 | err && *err ? *err : "", path); |
| 3005 | goto end; |
| 3006 | } |
| 3007 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3008 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3009 | if (BIO_reset(in) == -1) { |
| 3010 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3011 | err && *err ? *err : "", path); |
| 3012 | goto end; |
| 3013 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3014 | |
| 3015 | /* Read Certificate */ |
| 3016 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3017 | if (ckch->cert == NULL) { |
| 3018 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 3019 | err && *err ? *err : "", path); |
| 3020 | goto end; |
| 3021 | } |
| 3022 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3023 | /* Read Certificate Chain */ |
| 3024 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3025 | /* Grow the chain certs */ |
| 3026 | ckch->num_chain_certs++; |
| 3027 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 3028 | |
| 3029 | /* use - 1 here since we just incremented it above */ |
| 3030 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 3031 | } |
| 3032 | ret = ERR_get_error(); |
| 3033 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3034 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 3035 | err && *err ? *err : "", path); |
| 3036 | ret = 1; |
| 3037 | goto end; |
| 3038 | } |
| 3039 | |
| 3040 | ret = 0; |
| 3041 | |
| 3042 | end: |
| 3043 | |
| 3044 | ERR_clear_error(); |
| 3045 | if (in) |
| 3046 | BIO_free(in); |
| 3047 | |
| 3048 | /* Something went wrong in one of the reads */ |
| 3049 | if (ret != 0) |
| 3050 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3051 | |
| 3052 | return ret; |
| 3053 | } |
| 3054 | |
| 3055 | /* Loads the info in ckch into ctx |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3056 | * Returns a bitfield containing the flags: |
| 3057 | * ERR_FATAL in any fatal error case |
| 3058 | * ERR_ALERT if the reason of the error is available in err |
| 3059 | * ERR_WARN if a warning is available into err |
| 3060 | * The value 0 means there is no error nor warning and |
| 3061 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3062 | */ |
| 3063 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3064 | { |
| 3065 | int i = 0; |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3066 | int errcode = 0; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3067 | |
| 3068 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3069 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3070 | err && *err ? *err : "", path); |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3071 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3072 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3076 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3077 | err && *err ? *err : "", path); |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3078 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3079 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3080 | } |
| 3081 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3082 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 3083 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 3084 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3085 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3086 | err && *err ? *err : "", (i+1), path); |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3087 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3088 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3093 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3094 | err && *err ? *err : "", path); |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3095 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3096 | } |
| 3097 | |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3098 | end: |
| 3099 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3100 | } |
| 3101 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3102 | |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3103 | static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3104 | { |
| 3105 | struct sni_keytype *s_kt = NULL; |
| 3106 | struct ebmb_node *node; |
| 3107 | int i; |
| 3108 | |
| 3109 | for (i = 0; i < trash.size; i++) { |
| 3110 | if (!str[i]) |
| 3111 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3112 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3113 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3114 | trash.area[i] = 0; |
| 3115 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3116 | if (!node) { |
| 3117 | /* CN not found in tree */ |
| 3118 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3119 | /* Using memcpy here instead of strncpy. |
| 3120 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3121 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3122 | */ |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3123 | if (!s_kt) |
| 3124 | return -1; |
| 3125 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3126 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3127 | s_kt->keytypes = 0; |
| 3128 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3129 | } else { |
| 3130 | /* CN found in tree */ |
| 3131 | s_kt = container_of(node, struct sni_keytype, name); |
| 3132 | } |
| 3133 | |
| 3134 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3135 | s_kt->keytypes |= 1<<key_index; |
| 3136 | |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3137 | return 0; |
| 3138 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | |
| 3142 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 3143 | * If any are found, group these files into a set of SSL_CTX* |
| 3144 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3145 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3146 | * This will allow the user to explicitly group multiple cert/keys for a single purpose |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3147 | * |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3148 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3149 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3150 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3151 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3152 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3153 | { |
| 3154 | char fp[MAXPATHLEN+1] = {0}; |
| 3155 | int n = 0; |
| 3156 | int i = 0; |
| 3157 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 3158 | struct eb_root sni_keytypes_map = { {0} }; |
| 3159 | struct ebmb_node *node; |
| 3160 | struct ebmb_node *next; |
| 3161 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3162 | * of keytypes |
| 3163 | */ |
| 3164 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3165 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3166 | X509_NAME *xname = NULL; |
| 3167 | char *str = NULL; |
| 3168 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3169 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3170 | #endif |
| 3171 | |
| 3172 | /* Load all possible certs and keys */ |
| 3173 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3174 | struct stat buf; |
| 3175 | |
| 3176 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3177 | if (stat(fp, &buf) == 0) { |
| 3178 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3179 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3180 | goto end; |
| 3181 | } |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | /* Process each ckch and update keytypes for each CN/SAN |
| 3186 | * for example, if CN/SAN www.a.com is associated with |
| 3187 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3188 | * www.a.com will have: |
| 3189 | * keyindex = 0 | 1 | 4 = 5 |
| 3190 | */ |
| 3191 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3192 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3193 | |
| 3194 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3195 | continue; |
| 3196 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3197 | if (fcount) { |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3198 | for (i = 0; i < fcount; i++) { |
| 3199 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3200 | if (ret < 0) { |
| 3201 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3202 | err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3203 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3204 | goto end; |
| 3205 | } |
| 3206 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3207 | } else { |
| 3208 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3209 | * so the line that contains logic is marked via comments |
| 3210 | */ |
| 3211 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3212 | i = -1; |
| 3213 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3214 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3215 | ASN1_STRING *value; |
| 3216 | value = X509_NAME_ENTRY_get_data(entry); |
| 3217 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3218 | /* Important line is here */ |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3219 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3220 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3221 | OPENSSL_free(str); |
| 3222 | str = NULL; |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3223 | if (ret < 0) { |
| 3224 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3225 | err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3226 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3227 | goto end; |
| 3228 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3229 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3230 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3231 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3232 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3233 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3234 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3235 | if (names) { |
| 3236 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3237 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3238 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3239 | if (name->type == GEN_DNS) { |
| 3240 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3241 | /* Important line is here */ |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3242 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3243 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3244 | OPENSSL_free(str); |
| 3245 | str = NULL; |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3246 | if (ret < 0) { |
| 3247 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3248 | err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3249 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 4801c70 | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3250 | goto end; |
| 3251 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3252 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | } |
| 3256 | } |
| 3257 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3258 | } |
| 3259 | |
| 3260 | /* If no files found, return error */ |
| 3261 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3262 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3263 | err && *err ? *err : "", path); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3264 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3265 | goto end; |
| 3266 | } |
| 3267 | |
| 3268 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3269 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3270 | * and add each CTX to the SNI tree |
| 3271 | * |
| 3272 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3273 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3274 | * combination is denoted by the key in the map. Each key |
| 3275 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3276 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3277 | * entry in the array to correspond to the unique combo (key) |
| 3278 | * associated with i. This unique key combo (i) will be associated |
| 3279 | * with combos[i-1] |
| 3280 | */ |
| 3281 | |
| 3282 | node = ebmb_first(&sni_keytypes_map); |
| 3283 | while (node) { |
| 3284 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3285 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3286 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3287 | |
| 3288 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3289 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3290 | cur_ctx = key_combos[i-1].ctx; |
| 3291 | |
| 3292 | if (cur_ctx == NULL) { |
| 3293 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3294 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3295 | if (cur_ctx == NULL) { |
| 3296 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3297 | err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3298 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3299 | goto end; |
| 3300 | } |
| 3301 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3302 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3303 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3304 | if (i & (1<<n)) { |
| 3305 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3306 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
Emeric Brun | 394701d | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3307 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 3308 | if (errcode & ERR_CODE) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3309 | SSL_CTX_free(cur_ctx); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3310 | goto end; |
| 3311 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3312 | |
| 3313 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3314 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3315 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3316 | if (err) |
| 3317 | memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n", |
Bertrand Jacquin | 5424ee0 | 2016-11-13 16:37:14 +0000 | [diff] [blame] | 3318 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3319 | SSL_CTX_free(cur_ctx); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3320 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3321 | goto end; |
| 3322 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3323 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3324 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3325 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | /* Load DH params into the ctx to support DHE keys */ |
| 3330 | #ifndef OPENSSL_NO_DH |
| 3331 | if (ssl_dh_ptr_index >= 0) |
| 3332 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3333 | |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3334 | errcode |= ssl_sock_load_dh_params(cur_ctx, NULL, err); |
| 3335 | if (errcode & ERR_CODE) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3336 | if (err) |
| 3337 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3338 | *err ? *err : "", path); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3339 | goto end; |
| 3340 | } |
| 3341 | #endif |
| 3342 | |
| 3343 | /* Update key_combos */ |
| 3344 | key_combos[i-1].ctx = cur_ctx; |
| 3345 | } |
| 3346 | |
| 3347 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3348 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3349 | kinfo, str, key_combos[i-1].order); |
| 3350 | if (key_combos[i-1].order < 0) { |
| 3351 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3352 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3353 | goto end; |
| 3354 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3355 | node = ebmb_next(node); |
| 3356 | } |
| 3357 | |
| 3358 | |
| 3359 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3360 | if (!bind_conf->default_ctx) { |
| 3361 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3362 | if (key_combos[i].ctx) { |
| 3363 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3364 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3365 | break; |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | end: |
| 3371 | |
| 3372 | if (names) |
| 3373 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3374 | |
| 3375 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3376 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3377 | |
| 3378 | node = ebmb_first(&sni_keytypes_map); |
| 3379 | while (node) { |
| 3380 | next = ebmb_next(node); |
| 3381 | ebmb_delete(node); |
William Lallemand | e92c030 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 3382 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3383 | node = next; |
| 3384 | } |
| 3385 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3386 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3387 | } |
| 3388 | #else |
| 3389 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3390 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3391 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3392 | { |
| 3393 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3394 | err && *err ? *err : "", path, strerror(errno)); |
| 3395 | return 1; |
| 3396 | } |
| 3397 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3398 | #endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3399 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3400 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3401 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3402 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3403 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3404 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3405 | { |
| 3406 | BIO *in; |
| 3407 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3408 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3409 | int ret = -1; |
| 3410 | int order = 0; |
| 3411 | X509_NAME *xname; |
| 3412 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3413 | pem_password_cb *passwd_cb; |
| 3414 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3415 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3416 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3417 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3418 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3419 | STACK_OF(GENERAL_NAME) *names; |
| 3420 | #endif |
| 3421 | |
| 3422 | in = BIO_new(BIO_s_file()); |
| 3423 | if (in == NULL) |
| 3424 | goto end; |
| 3425 | |
| 3426 | if (BIO_read_filename(in, file) <= 0) |
| 3427 | goto end; |
| 3428 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3429 | |
| 3430 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3431 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3432 | |
| 3433 | x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3434 | if (x == NULL) |
| 3435 | goto end; |
| 3436 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3437 | pkey = X509_get_pubkey(x); |
| 3438 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3439 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3440 | switch(EVP_PKEY_base_id(pkey)) { |
| 3441 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3442 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3443 | break; |
| 3444 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3445 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3446 | break; |
| 3447 | case EVP_PKEY_DSA: |
| 3448 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3449 | break; |
| 3450 | } |
| 3451 | EVP_PKEY_free(pkey); |
| 3452 | } |
| 3453 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3454 | if (fcount) { |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3455 | while (fcount--) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3456 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order); |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3457 | if (order < 0) |
| 3458 | goto end; |
| 3459 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3460 | } |
| 3461 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3462 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3463 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3464 | if (names) { |
| 3465 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3466 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3467 | if (name->type == GEN_DNS) { |
| 3468 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3469 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3470 | OPENSSL_free(str); |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3471 | if (order < 0) |
| 3472 | goto end; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3473 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3474 | } |
| 3475 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3476 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3477 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3478 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3479 | xname = X509_get_subject_name(x); |
| 3480 | i = -1; |
| 3481 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3482 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3483 | ASN1_STRING *value; |
| 3484 | |
| 3485 | value = X509_NAME_ENTRY_get_data(entry); |
| 3486 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3487 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3488 | OPENSSL_free(str); |
William Lallemand | 24e292c | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3489 | if (order < 0) |
| 3490 | goto end; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3491 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3496 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3497 | goto end; |
| 3498 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3499 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3500 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3501 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3502 | if (ctx->extra_certs != NULL) { |
| 3503 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3504 | ctx->extra_certs = NULL; |
| 3505 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3506 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3507 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3508 | while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3509 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3510 | X509_free(ca); |
| 3511 | goto end; |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | err = ERR_get_error(); |
| 3516 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3517 | /* we successfully reached the last cert in the file */ |
| 3518 | ret = 1; |
| 3519 | } |
| 3520 | ERR_clear_error(); |
| 3521 | |
| 3522 | end: |
| 3523 | if (x) |
| 3524 | X509_free(x); |
| 3525 | |
| 3526 | if (in) |
| 3527 | BIO_free(in); |
| 3528 | |
| 3529 | return ret; |
| 3530 | } |
| 3531 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3532 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3533 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3534 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3535 | { |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3536 | int errcode = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3537 | int ret; |
| 3538 | SSL_CTX *ctx; |
| 3539 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3540 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3541 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3542 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3543 | err && *err ? *err : "", path); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3544 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3548 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3549 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3550 | SSL_CTX_free(ctx); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3551 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3552 | } |
| 3553 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3554 | ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3555 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3556 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3557 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3558 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3559 | SSL_CTX_free(ctx); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3560 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3561 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3562 | |
| 3563 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3564 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3565 | err && *err ? *err : "", path); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3566 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3569 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3570 | * the tree, so it will be discovered and cleaned in time. |
| 3571 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3572 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3573 | /* store a NULL pointer to indicate we have not yet loaded |
| 3574 | a custom DH param file */ |
| 3575 | if (ssl_dh_ptr_index >= 0) { |
| 3576 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3577 | } |
| 3578 | |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3579 | errcode |= ssl_sock_load_dh_params(ctx, path, err); |
| 3580 | if (errcode & ERR_CODE) { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3581 | if (err) |
| 3582 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3583 | *err ? *err : "", path); |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3584 | return errcode; |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3585 | } |
| 3586 | #endif |
| 3587 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3588 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3589 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3590 | if (ret < 0) { |
| 3591 | if (err) |
| 3592 | memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n", |
| 3593 | *err ? *err : "", path); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3594 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3595 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3596 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3597 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3598 | #endif |
| 3599 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3600 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 3601 | if (sctl_ex_index >= 0) { |
| 3602 | ret = ssl_sock_load_sctl(ctx, path); |
| 3603 | if (ret < 0) { |
| 3604 | if (err) |
| 3605 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3606 | *err ? *err : "", path); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3607 | return ERR_ALERT | ERR_FATAL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | #endif |
| 3611 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3612 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3613 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3614 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3615 | err && *err ? *err : ""); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3616 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3617 | } |
| 3618 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3619 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3620 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3621 | bind_conf->default_ssl_conf = ssl_conf; |
| 3622 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3623 | |
Emeric Brun | cfc1afe | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3624 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3627 | |
| 3628 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3629 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3630 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3631 | struct dirent **de_list; |
| 3632 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3633 | DIR *dir; |
| 3634 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3635 | char *end; |
| 3636 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3637 | int cfgerr = 0; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3638 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3639 | int is_bundle; |
| 3640 | int j; |
| 3641 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3642 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3643 | if (stat(path, &buf) == 0) { |
| 3644 | dir = opendir(path); |
| 3645 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3646 | return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3647 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3648 | /* strip trailing slashes, including first one */ |
| 3649 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3650 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3651 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3652 | n = scandir(path, &de_list, 0, alphasort); |
| 3653 | if (n < 0) { |
| 3654 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3655 | err && *err ? *err : "", path, strerror(errno)); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3656 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3657 | } |
| 3658 | else { |
| 3659 | for (i = 0; i < n; i++) { |
| 3660 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3661 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3662 | end = strrchr(de->d_name, '.'); |
| 3663 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3664 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3665 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3666 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3667 | if (stat(fp, &buf) != 0) { |
| 3668 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3669 | err && *err ? *err : "", fp, strerror(errno)); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3670 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3671 | goto ignore_entry; |
| 3672 | } |
| 3673 | if (!S_ISREG(buf.st_mode)) |
| 3674 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3675 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3676 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3677 | is_bundle = 0; |
| 3678 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3679 | |
| 3680 | if (end) { |
| 3681 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3682 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3683 | is_bundle = 1; |
| 3684 | break; |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | if (is_bundle) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3689 | int dp_len; |
| 3690 | |
| 3691 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3692 | |
| 3693 | /* increment i and free de until we get to a non-bundle cert |
| 3694 | * Note here that we look at de_list[i + 1] before freeing de |
Willy Tarreau | 5b8a865 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 3695 | * this is important since ignore_entry will free de. This also |
| 3696 | * guarantees that de->d_name continues to hold the same prefix. |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3697 | */ |
Willy Tarreau | 5b8a865 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 3698 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3699 | free(de); |
| 3700 | i++; |
| 3701 | de = de_list[i]; |
| 3702 | } |
| 3703 | |
Willy Tarreau | 5b8a865 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 3704 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3705 | cfgerr |= ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3706 | /* Successfully processed the bundle */ |
| 3707 | goto ignore_entry; |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | #endif |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3712 | cfgerr |= ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3713 | ignore_entry: |
| 3714 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3715 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3716 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3717 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3718 | closedir(dir); |
| 3719 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3720 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3721 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3722 | cfgerr |= ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3723 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3724 | return cfgerr; |
| 3725 | } |
| 3726 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3727 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3728 | * done once. Zero is returned if the operation fails. No error is returned |
| 3729 | * if the random is said as not implemented, because we expect that openssl |
| 3730 | * will use another method once needed. |
| 3731 | */ |
| 3732 | static int ssl_initialize_random() |
| 3733 | { |
| 3734 | unsigned char random; |
| 3735 | static int random_initialized = 0; |
| 3736 | |
| 3737 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3738 | random_initialized = 1; |
| 3739 | |
| 3740 | return random_initialized; |
| 3741 | } |
| 3742 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3743 | /* release ssl bind conf */ |
| 3744 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3745 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3746 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3747 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3748 | free(conf->npn_str); |
| 3749 | conf->npn_str = NULL; |
| 3750 | #endif |
| 3751 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3752 | free(conf->alpn_str); |
| 3753 | conf->alpn_str = NULL; |
| 3754 | #endif |
| 3755 | free(conf->ca_file); |
| 3756 | conf->ca_file = NULL; |
| 3757 | free(conf->crl_file); |
| 3758 | conf->crl_file = NULL; |
| 3759 | free(conf->ciphers); |
| 3760 | conf->ciphers = NULL; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 3761 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 3762 | free(conf->ciphersuites); |
| 3763 | conf->ciphersuites = NULL; |
| 3764 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3765 | free(conf->curves); |
| 3766 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3767 | free(conf->ecdhe); |
| 3768 | conf->ecdhe = NULL; |
| 3769 | } |
| 3770 | } |
| 3771 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3772 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3773 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3774 | { |
| 3775 | char thisline[CRT_LINESIZE]; |
| 3776 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3777 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3778 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3779 | int linenum = 0; |
| 3780 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3781 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3782 | if ((f = fopen(file, "r")) == NULL) { |
| 3783 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3784 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3785 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3786 | |
| 3787 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3788 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3789 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3790 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3791 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3792 | char *crt_path; |
| 3793 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3794 | |
| 3795 | linenum++; |
| 3796 | end = line + strlen(line); |
| 3797 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3798 | /* Check if we reached the limit and the last char is not \n. |
| 3799 | * Watch out for the last line without the terminating '\n'! |
| 3800 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3801 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3802 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3803 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3804 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3805 | } |
| 3806 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3807 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3808 | newarg = 1; |
| 3809 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3810 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3811 | /* end of string, end of loop */ |
| 3812 | *line = 0; |
| 3813 | break; |
Willy Tarreau | 26e4ab4 | 2020-02-25 07:51:59 +0100 | [diff] [blame] | 3814 | } else if (isspace((unsigned char)*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3815 | newarg = 1; |
| 3816 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3817 | } else if (*line == '[') { |
| 3818 | if (ssl_b) { |
| 3819 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3820 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3821 | break; |
| 3822 | } |
| 3823 | if (!arg) { |
| 3824 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3825 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3826 | break; |
| 3827 | } |
| 3828 | ssl_b = arg; |
| 3829 | newarg = 1; |
| 3830 | *line = 0; |
| 3831 | } else if (*line == ']') { |
| 3832 | if (ssl_e) { |
| 3833 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3834 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3835 | break; |
| 3836 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3837 | if (!ssl_b) { |
| 3838 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3839 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3840 | break; |
| 3841 | } |
| 3842 | ssl_e = arg; |
| 3843 | newarg = 1; |
| 3844 | *line = 0; |
| 3845 | } else if (newarg) { |
| 3846 | if (arg == MAX_CRT_ARGS) { |
| 3847 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3848 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3849 | break; |
| 3850 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3851 | newarg = 0; |
| 3852 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3853 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3854 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3855 | } |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3856 | if (cfgerr & ERR_CODE) |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3857 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3858 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3859 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3860 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3861 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3862 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3863 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3864 | crt_path = args[0]; |
| 3865 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3866 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3867 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3868 | crt_path, linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3869 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3870 | break; |
| 3871 | } |
| 3872 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3873 | crt_path = path; |
| 3874 | } |
| 3875 | |
| 3876 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3877 | cur_arg = ssl_b ? ssl_b : 1; |
| 3878 | while (cur_arg < ssl_e) { |
| 3879 | newarg = 0; |
| 3880 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3881 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3882 | newarg = 1; |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3883 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3884 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3885 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3886 | args[cur_arg], linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3887 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3888 | } |
| 3889 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3890 | break; |
| 3891 | } |
| 3892 | } |
| 3893 | if (!cfgerr && !newarg) { |
| 3894 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3895 | args[cur_arg], linenum, file); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3896 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3897 | break; |
| 3898 | } |
| 3899 | } |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3900 | |
| 3901 | if (cfgerr & ERR_CODE) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3902 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3903 | free(ssl_conf); |
| 3904 | ssl_conf = NULL; |
| 3905 | break; |
| 3906 | } |
| 3907 | |
| 3908 | if (stat(crt_path, &buf) == 0) { |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3909 | cfgerr |= ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3910 | &args[cur_arg], arg - cur_arg - 1, err); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3911 | } else { |
| 3912 | cfgerr |= ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3913 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3914 | } |
| 3915 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 3916 | if (cfgerr & ERR_CODE) { |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3917 | memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3918 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3919 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3920 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3921 | fclose(f); |
| 3922 | return cfgerr; |
| 3923 | } |
| 3924 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3925 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3926 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3927 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3928 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3929 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3930 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3931 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3932 | SSL_OP_NO_SSLv2 | |
| 3933 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3934 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3935 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3936 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3937 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3938 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3939 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3940 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3941 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3942 | SSL_MODE_RELEASE_BUFFERS | |
| 3943 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3944 | struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3945 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3946 | int flags = MC_SSL_O_ALL; |
| 3947 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3948 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3949 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3950 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3951 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3952 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3953 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3954 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3955 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3956 | else |
| 3957 | flags = conf_ssl_methods->flags; |
| 3958 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3959 | min = conf_ssl_methods->min; |
| 3960 | max = conf_ssl_methods->max; |
| 3961 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3962 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3963 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3964 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3965 | if (min) |
| 3966 | flags |= (methodVersions[min].flag - 1); |
| 3967 | if (max) |
| 3968 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3969 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3970 | min = max = CONF_TLSV_NONE; |
| 3971 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3972 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3973 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3974 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3975 | if (min) { |
| 3976 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3977 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3978 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3979 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3980 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3981 | hole = 0; |
| 3982 | } |
| 3983 | max = i; |
| 3984 | } |
| 3985 | else { |
| 3986 | min = max = i; |
| 3987 | } |
| 3988 | } |
| 3989 | else { |
| 3990 | if (min) |
| 3991 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3992 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3993 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3994 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3995 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3996 | cfgerr += 1; |
| 3997 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3998 | /* save real min/max in bind_conf */ |
| 3999 | conf_ssl_methods->min = min; |
| 4000 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4001 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4002 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4003 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4004 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4005 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4006 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4007 | else |
William Lallemand | 6dbb9a1 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4008 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 4009 | /* clear every version flags in case SSL_CTX_new() |
| 4010 | * returns an SSL_CTX with disabled versions */ |
| 4011 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 4012 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4013 | if (flags & methodVersions[i].flag) |
| 4014 | options |= methodVersions[i].option; |
William Lallemand | 6dbb9a1 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4015 | |
| 4016 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4017 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4018 | /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4019 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4020 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4021 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4022 | |
| 4023 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4024 | options |= SSL_OP_NO_TICKET; |
| 4025 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4026 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4027 | |
| 4028 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4029 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4030 | #endif |
| 4031 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4032 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4033 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4034 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4035 | if (global_ssl.async) |
| 4036 | mode |= SSL_MODE_ASYNC; |
| 4037 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4038 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4039 | if (global_ssl.life_time) |
| 4040 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4041 | |
| 4042 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4043 | #ifdef OPENSSL_IS_BORINGSSL |
| 4044 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4045 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4046 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | e82c1d4 | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4047 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4048 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4049 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4050 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4051 | #else |
| 4052 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4053 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4054 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4055 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4056 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4059 | |
| 4060 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4061 | { |
| 4062 | if (first == block) { |
| 4063 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4064 | if (first->len > 0) |
| 4065 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | /* return first block from sh_ssl_sess */ |
| 4070 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4071 | { |
| 4072 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4073 | |
| 4074 | } |
| 4075 | |
| 4076 | /* store a session into the cache |
| 4077 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4078 | * data: asn1 encoded session |
| 4079 | * data_len: asn1 encoded session length |
| 4080 | * Returns 1 id session was stored (else 0) |
| 4081 | */ |
| 4082 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4083 | { |
| 4084 | struct shared_block *first; |
| 4085 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4086 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4087 | first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4088 | if (!first) { |
| 4089 | /* Could not retrieve enough free blocks to store that session */ |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | /* STORE the key in the first elem */ |
| 4094 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4095 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4096 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4097 | |
| 4098 | /* it returns the already existing node |
| 4099 | or current node if none, never returns null */ |
| 4100 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4101 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4102 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4103 | /* release the reserved row */ |
| 4104 | shctx_row_dec_hot(ssl_shctx, first); |
| 4105 | /* replace the previous session already in the tree */ |
| 4106 | sh_ssl_sess = oldsh_ssl_sess; |
| 4107 | /* ignore the previous session data, only use the header */ |
| 4108 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4109 | shctx_row_inc_hot(ssl_shctx, first); |
| 4110 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4111 | } |
| 4112 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4113 | if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) { |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4114 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4115 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4119 | |
| 4120 | return 1; |
| 4121 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4122 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4123 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4124 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4125 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4126 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4127 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4128 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4129 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4130 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4131 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4132 | int len; |
| 4133 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4134 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4135 | len = i2d_SSL_SESSION(sess, NULL); |
| 4136 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4137 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4138 | } else { |
| 4139 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4140 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4141 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4142 | } |
| 4143 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4144 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4145 | &ptr); |
| 4146 | } |
| 4147 | } else { |
| 4148 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4149 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4150 | } |
| 4151 | |
| 4152 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4153 | } |
| 4154 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4155 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4156 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4157 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4158 | { |
| 4159 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4160 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4161 | unsigned char *p; |
| 4162 | int data_len; |
Emeric Brun | 7b34de3 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4163 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4164 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4165 | |
| 4166 | /* Session id is already stored in to key and session id is known |
| 4167 | * so we dont store it to keep size. |
Emeric Brun | 7b34de3 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4168 | * note: SSL_SESSION_set1_id is using |
| 4169 | * a memcpy so we need to use a different pointer |
| 4170 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4171 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4172 | */ |
| 4173 | |
| 4174 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | 7b34de3 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4175 | |
| 4176 | /* copy value in an other buffer */ |
| 4177 | memcpy(encid, sid_data, sid_length); |
| 4178 | |
| 4179 | /* pad with 0 */ |
| 4180 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4181 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4182 | |
| 4183 | /* force length to zero to avoid ASN1 encoding */ |
| 4184 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4185 | |
| 4186 | /* force length to zero to avoid ASN1 encoding */ |
| 4187 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4190 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4191 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4192 | goto err; |
| 4193 | |
| 4194 | p = encsess; |
| 4195 | |
| 4196 | /* process ASN1 session encoding before the lock */ |
| 4197 | i2d_SSL_SESSION(sess, &p); |
| 4198 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4199 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4200 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4201 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4202 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4203 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4204 | err: |
| 4205 | /* reset original length values */ |
Emeric Brun | 7b34de3 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4206 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4207 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4208 | |
| 4209 | return 0; /* do not increment session reference count */ |
| 4210 | } |
| 4211 | |
| 4212 | /* SSL callback used on lookup an existing session cause none found in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4213 | SSL_SESSION *sh_ssl_sess_get_cb(SSL *ssl, __OPENSSL_110_CONST__ unsigned char *key, int key_len, int *do_copy) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4214 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4215 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4216 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4217 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4218 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4219 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4220 | |
| 4221 | global.shctx_lookups++; |
| 4222 | |
| 4223 | /* allow the session to be freed automatically by openssl */ |
| 4224 | *do_copy = 0; |
| 4225 | |
| 4226 | /* tree key is zeros padded sessionid */ |
| 4227 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4228 | memcpy(tmpkey, key, key_len); |
| 4229 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4230 | key = tmpkey; |
| 4231 | } |
| 4232 | |
| 4233 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4234 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4235 | |
| 4236 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4237 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4238 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4239 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4240 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4241 | global.shctx_misses++; |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4245 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4246 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4247 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4248 | shctx_row_data_get(ssl_shctx, first, data, sizeof(struct sh_ssl_sess_hdr), first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4249 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4250 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4251 | |
| 4252 | /* decode ASN1 session */ |
| 4253 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4254 | sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4255 | /* Reset session id and session id contenxt */ |
| 4256 | if (sess) { |
| 4257 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4258 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4259 | } |
| 4260 | |
| 4261 | return sess; |
| 4262 | } |
| 4263 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4264 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4265 | /* SSL callback used to signal session is no more used in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4266 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4267 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4268 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4269 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4270 | unsigned int sid_length; |
| 4271 | const unsigned char *sid_data; |
| 4272 | (void)ctx; |
| 4273 | |
| 4274 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4275 | /* tree key is zeros padded sessionid */ |
| 4276 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4277 | memcpy(tmpkey, sid_data, sid_length); |
| 4278 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4279 | sid_data = tmpkey; |
| 4280 | } |
| 4281 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4282 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4283 | |
| 4284 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4285 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4286 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4287 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4288 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4292 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | /* Set session cache mode to server and disable openssl internal cache. |
| 4296 | * Set shared cache callbacks on an ssl context. |
| 4297 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4298 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4299 | { |
| 4300 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4301 | |
| 4302 | if (!ssl_shctx) { |
| 4303 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4304 | return; |
| 4305 | } |
| 4306 | |
| 4307 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4308 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4309 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4310 | |
| 4311 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4312 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4313 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4314 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4315 | } |
| 4316 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4317 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4318 | { |
| 4319 | struct proxy *curproxy = bind_conf->frontend; |
| 4320 | int cfgerr = 0; |
| 4321 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4322 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4323 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4324 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4325 | const char *conf_ciphersuites; |
| 4326 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4327 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4328 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4329 | if (ssl_conf) { |
| 4330 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4331 | int i, min, max; |
| 4332 | int flags = MC_SSL_O_ALL; |
| 4333 | |
| 4334 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4335 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4336 | max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4337 | if (min) |
| 4338 | flags |= (methodVersions[min].flag - 1); |
| 4339 | if (max) |
| 4340 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4341 | min = max = CONF_TLSV_NONE; |
| 4342 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4343 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4344 | if (min) |
| 4345 | max = i; |
| 4346 | else |
| 4347 | min = max = i; |
| 4348 | } |
| 4349 | /* save real min/max */ |
| 4350 | conf_ssl_methods->min = min; |
| 4351 | conf_ssl_methods->max = max; |
| 4352 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4353 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4354 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4355 | cfgerr += 1; |
| 4356 | } |
| 4357 | } |
| 4358 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4359 | switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) { |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4360 | case SSL_SOCK_VERIFY_NONE: |
| 4361 | verify = SSL_VERIFY_NONE; |
| 4362 | break; |
| 4363 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4364 | verify = SSL_VERIFY_PEER; |
| 4365 | break; |
| 4366 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4367 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4368 | break; |
| 4369 | } |
| 4370 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4371 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4372 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4373 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4374 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4375 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4376 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4377 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4378 | curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4379 | cfgerr++; |
| 4380 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4381 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4382 | /* set CA names for client cert request, function returns void */ |
| 4383 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4384 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4385 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4386 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4387 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4388 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4389 | cfgerr++; |
| 4390 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4391 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4392 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4393 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4394 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4395 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4396 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4397 | curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4398 | cfgerr++; |
| 4399 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4400 | else { |
| 4401 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4402 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4403 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4404 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4405 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4406 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4407 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4408 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4409 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4410 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4411 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4412 | cfgerr++; |
| 4413 | } |
| 4414 | } |
| 4415 | #endif |
| 4416 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4417 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4418 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4419 | if (conf_ciphers && |
| 4420 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4421 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4422 | curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4423 | cfgerr++; |
| 4424 | } |
| 4425 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4426 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4427 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4428 | if (conf_ciphersuites && |
| 4429 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
| 4430 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4431 | curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4432 | cfgerr++; |
| 4433 | } |
| 4434 | #endif |
| 4435 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4436 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4437 | /* If tune.ssl.default-dh-param has not been set, |
| 4438 | neither has ssl-default-dh-file and no static DH |
| 4439 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4440 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4441 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4442 | (ssl_dh_ptr_index == -1 || |
| 4443 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4444 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4445 | const SSL_CIPHER * cipher = NULL; |
| 4446 | char cipher_description[128]; |
| 4447 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4448 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4449 | which is not ephemeral DH. */ |
| 4450 | const char dhe_description[] = " Kx=DH "; |
| 4451 | const char dhe_export_description[] = " Kx=DH("; |
| 4452 | int idx = 0; |
| 4453 | int dhe_found = 0; |
| 4454 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4455 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4456 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4457 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4458 | if (ssl) { |
| 4459 | ciphers = SSL_get_ciphers(ssl); |
| 4460 | |
| 4461 | if (ciphers) { |
| 4462 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4463 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4464 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4465 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4466 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4467 | dhe_found = 1; |
| 4468 | break; |
| 4469 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4470 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4471 | } |
| 4472 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4473 | SSL_free(ssl); |
| 4474 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4475 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4476 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4477 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4478 | ha_warning("Setting tune.ssl.default-dh-param to 1024 by default, if your workload permits it you should set it to at least 2048. Please set a value >= 1024 to make this warning disappear.\n"); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4479 | } |
| 4480 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4481 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4482 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4483 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4484 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4485 | if (local_dh_1024 == NULL) { |
| 4486 | local_dh_1024 = ssl_get_dh_1024(); |
| 4487 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4488 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4489 | if (local_dh_2048 == NULL) { |
| 4490 | local_dh_2048 = ssl_get_dh_2048(); |
| 4491 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4492 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4493 | if (local_dh_4096 == NULL) { |
| 4494 | local_dh_4096 = ssl_get_dh_4096(); |
| 4495 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4500 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4501 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4502 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4503 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4504 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4505 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4506 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4507 | ssl_conf_cur = NULL; |
| 4508 | if (ssl_conf && ssl_conf->npn_str) |
| 4509 | ssl_conf_cur = ssl_conf; |
| 4510 | else if (bind_conf->ssl_conf.npn_str) |
| 4511 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4512 | if (ssl_conf_cur) |
| 4513 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4514 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4515 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4516 | ssl_conf_cur = NULL; |
| 4517 | if (ssl_conf && ssl_conf->alpn_str) |
| 4518 | ssl_conf_cur = ssl_conf; |
| 4519 | else if (bind_conf->ssl_conf.alpn_str) |
| 4520 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4521 | if (ssl_conf_cur) |
| 4522 | SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4523 | #endif |
Lukas Tribus | d13e925 | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 4524 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4525 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4526 | if (conf_curves) { |
| 4527 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4528 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4529 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4530 | cfgerr++; |
| 4531 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4532 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4533 | } |
| 4534 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4535 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4536 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4537 | int i; |
| 4538 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4539 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4540 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4541 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4542 | NULL); |
| 4543 | |
| 4544 | if (ecdhe == NULL) { |
Eric Salama | 27c21cd | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 4545 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4546 | return cfgerr; |
| 4547 | } |
| 4548 | #else |
| 4549 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4550 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4551 | ECDHE_DEFAULT_CURVE); |
| 4552 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4553 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4554 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4555 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4556 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4557 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4558 | cfgerr++; |
| 4559 | } |
| 4560 | else { |
| 4561 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4562 | EC_KEY_free(ecdh); |
| 4563 | } |
| 4564 | } |
| 4565 | #endif |
| 4566 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4567 | return cfgerr; |
| 4568 | } |
| 4569 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4570 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4571 | { |
| 4572 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4573 | size_t prefixlen, suffixlen; |
| 4574 | |
| 4575 | /* Trivial case */ |
William Lallemand | b00a33b | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4576 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4577 | return 1; |
| 4578 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4579 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4580 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4581 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4582 | pattern_wildcard = NULL; |
| 4583 | pattern_left_label_end = pattern; |
| 4584 | while (*pattern_left_label_end != '.') { |
| 4585 | switch (*pattern_left_label_end) { |
| 4586 | case 0: |
| 4587 | /* End of label not found */ |
| 4588 | return 0; |
| 4589 | case '*': |
| 4590 | /* If there is more than one wildcards */ |
| 4591 | if (pattern_wildcard) |
| 4592 | return 0; |
| 4593 | pattern_wildcard = pattern_left_label_end; |
| 4594 | break; |
| 4595 | } |
| 4596 | pattern_left_label_end++; |
| 4597 | } |
| 4598 | |
| 4599 | /* If it's not trivial and there is no wildcard, it can't |
| 4600 | * match */ |
| 4601 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4602 | return 0; |
| 4603 | |
| 4604 | /* Make sure all labels match except the leftmost */ |
| 4605 | hostname_left_label_end = strchr(hostname, '.'); |
| 4606 | if (!hostname_left_label_end |
William Lallemand | b00a33b | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4607 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4608 | return 0; |
| 4609 | |
| 4610 | /* Make sure the leftmost label of the hostname is long enough |
| 4611 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4612 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4613 | return 0; |
| 4614 | |
| 4615 | /* Finally compare the string on either side of the |
| 4616 | * wildcard */ |
| 4617 | prefixlen = pattern_wildcard - pattern; |
| 4618 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | b00a33b | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4619 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4620 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4621 | return 0; |
| 4622 | |
| 4623 | return 1; |
| 4624 | } |
| 4625 | |
| 4626 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4627 | { |
| 4628 | SSL *ssl; |
| 4629 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4630 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4631 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4632 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4633 | |
| 4634 | int depth; |
| 4635 | X509 *cert; |
| 4636 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4637 | int i; |
| 4638 | X509_NAME *cert_subject; |
| 4639 | char *str; |
| 4640 | |
| 4641 | if (ok == 0) |
| 4642 | return ok; |
| 4643 | |
| 4644 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4645 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4646 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4647 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4648 | /* We're checking if the provided hostnames match the desired one. The |
| 4649 | * desired hostname comes from the SNI we presented if any, or if not |
| 4650 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4651 | * directive. If neither is set, we don't care about the name so the |
| 4652 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4653 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4654 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4655 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4656 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4657 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4658 | if (!servername) |
| 4659 | return ok; |
| 4660 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4661 | |
| 4662 | /* We only need to verify the CN on the actual server cert, |
| 4663 | * not the indirect CAs */ |
| 4664 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4665 | if (depth != 0) |
| 4666 | return ok; |
| 4667 | |
| 4668 | /* At this point, the cert is *not* OK unless we can find a |
| 4669 | * hostname match */ |
| 4670 | ok = 0; |
| 4671 | |
| 4672 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4673 | /* It seems like this might happen if verify peer isn't set */ |
| 4674 | if (!cert) |
| 4675 | return ok; |
| 4676 | |
| 4677 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4678 | if (alt_names) { |
| 4679 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4680 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4681 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4682 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4683 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4684 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4685 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4686 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4687 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4688 | OPENSSL_free(str); |
| 4689 | } |
| 4690 | } |
| 4691 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4692 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | cert_subject = X509_get_subject_name(cert); |
| 4696 | i = -1; |
| 4697 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4698 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4699 | ASN1_STRING *value; |
| 4700 | value = X509_NAME_ENTRY_get_data(entry); |
| 4701 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4702 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4703 | OPENSSL_free(str); |
| 4704 | } |
| 4705 | } |
| 4706 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4707 | /* report the mismatch and indicate if SNI was used or not */ |
| 4708 | if (!ok && !conn->err_code) |
| 4709 | conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4710 | return ok; |
| 4711 | } |
| 4712 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4713 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4714 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4715 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4716 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4717 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4718 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4719 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4720 | SSL_OP_NO_SSLv2 | |
| 4721 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4722 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4723 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4724 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4725 | SSL_MODE_RELEASE_BUFFERS | |
| 4726 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4727 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4728 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4729 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4730 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4731 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4732 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4733 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4734 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4735 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4736 | cfgerr++; |
| 4737 | } |
| 4738 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4739 | /* Automatic memory computations need to know we use SSL there */ |
| 4740 | global.ssl_used_backend = 1; |
| 4741 | |
| 4742 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4743 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4744 | if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4745 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4746 | curproxy->id, srv->id, |
| 4747 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4748 | cfgerr++; |
| 4749 | return cfgerr; |
| 4750 | } |
| 4751 | } |
Christopher Faulet | 68d35ae | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4752 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4753 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4754 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4755 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4756 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4757 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4758 | proxy_type_str(curproxy), curproxy->id, |
| 4759 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4760 | cfgerr++; |
| 4761 | return cfgerr; |
| 4762 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4763 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4764 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4765 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4766 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4767 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4768 | else |
| 4769 | flags = conf_ssl_methods->flags; |
| 4770 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4771 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4772 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4773 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4774 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4775 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4776 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4777 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4778 | min = max = CONF_TLSV_NONE; |
| 4779 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4780 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4781 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4782 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4783 | if (min) { |
| 4784 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4785 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4786 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4787 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4788 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4789 | hole = 0; |
| 4790 | } |
| 4791 | max = i; |
| 4792 | } |
| 4793 | else { |
| 4794 | min = max = i; |
| 4795 | } |
| 4796 | } |
| 4797 | else { |
| 4798 | if (min) |
| 4799 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4800 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4801 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4802 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4803 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4804 | cfgerr += 1; |
| 4805 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4806 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4807 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4808 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4809 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4810 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4811 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4812 | else |
| 4813 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4814 | if (flags & methodVersions[i].flag) |
| 4815 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4816 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4817 | /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4818 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4819 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4820 | #endif |
| 4821 | |
| 4822 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4823 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4824 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4825 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4826 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4827 | if (global_ssl.async) |
| 4828 | mode |= SSL_MODE_ASYNC; |
| 4829 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4830 | SSL_CTX_set_mode(ctx, mode); |
| 4831 | srv->ssl_ctx.ctx = ctx; |
| 4832 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4833 | if (srv->ssl_ctx.client_crt) { |
| 4834 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4835 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4836 | proxy_type_str(curproxy), curproxy->id, |
| 4837 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4838 | cfgerr++; |
| 4839 | } |
| 4840 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4841 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4842 | proxy_type_str(curproxy), curproxy->id, |
| 4843 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4844 | cfgerr++; |
| 4845 | } |
| 4846 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4847 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4848 | proxy_type_str(curproxy), curproxy->id, |
| 4849 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4850 | cfgerr++; |
| 4851 | } |
| 4852 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4853 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4854 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4855 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4856 | switch (srv->ssl_ctx.verify) { |
| 4857 | case SSL_SOCK_VERIFY_NONE: |
| 4858 | verify = SSL_VERIFY_NONE; |
| 4859 | break; |
| 4860 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4861 | verify = SSL_VERIFY_PEER; |
| 4862 | break; |
| 4863 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4864 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4865 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4866 | (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4867 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4868 | if (srv->ssl_ctx.ca_file) { |
| 4869 | /* load CAfile to verify */ |
| 4870 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4871 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4872 | curproxy->id, srv->id, |
| 4873 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4874 | cfgerr++; |
| 4875 | } |
| 4876 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4877 | else { |
| 4878 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4879 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n", |
| 4880 | curproxy->id, srv->id, |
| 4881 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4882 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4883 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4884 | curproxy->id, srv->id, |
| 4885 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4886 | cfgerr++; |
| 4887 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4888 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4889 | if (srv->ssl_ctx.crl_file) { |
| 4890 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4891 | |
| 4892 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4893 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4894 | curproxy->id, srv->id, |
| 4895 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4896 | cfgerr++; |
| 4897 | } |
| 4898 | else { |
| 4899 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4900 | } |
| 4901 | } |
| 4902 | #endif |
| 4903 | } |
| 4904 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4905 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4906 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4907 | SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4908 | if (srv->ssl_ctx.ciphers && |
| 4909 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4910 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4911 | curproxy->id, srv->id, |
| 4912 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4913 | cfgerr++; |
| 4914 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4915 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4916 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4917 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4918 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4919 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4920 | curproxy->id, srv->id, |
| 4921 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4922 | cfgerr++; |
| 4923 | } |
| 4924 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4925 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4926 | if (srv->ssl_ctx.npn_str) |
| 4927 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4928 | #endif |
| 4929 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4930 | if (srv->ssl_ctx.alpn_str) |
| 4931 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4932 | #endif |
| 4933 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4934 | |
| 4935 | return cfgerr; |
| 4936 | } |
| 4937 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4938 | /* Walks down the two trees in bind_conf and prepares all certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4939 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4940 | * encountered. |
| 4941 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4942 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4943 | { |
| 4944 | struct ebmb_node *node; |
| 4945 | struct sni_ctx *sni; |
| 4946 | int err = 0; |
| 4947 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4948 | /* Automatic memory computations need to know we use SSL there */ |
| 4949 | global.ssl_used_frontend = 1; |
| 4950 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4951 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4952 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4953 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4954 | err++; |
| 4955 | } |
| 4956 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4957 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4958 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4959 | /* It should not be necessary to call this function, but it's |
| 4960 | necessary first to check and move all initialisation related |
| 4961 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4962 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4963 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4964 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4965 | err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4966 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4967 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4968 | while (node) { |
| 4969 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4970 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4971 | /* only initialize the CTX on its first occurrence and |
| 4972 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4973 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4974 | node = ebmb_next(node); |
| 4975 | } |
| 4976 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4977 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4978 | while (node) { |
| 4979 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4980 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4981 | /* only initialize the CTX on its first occurrence and |
| 4982 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4983 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4984 | node = ebmb_next(node); |
| 4985 | } |
| 4986 | return err; |
| 4987 | } |
| 4988 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4989 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4990 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4991 | * alerts are directly emitted since the rest of the stack does it below. |
| 4992 | */ |
| 4993 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4994 | { |
| 4995 | struct proxy *px = bind_conf->frontend; |
| 4996 | int alloc_ctx; |
| 4997 | int err; |
| 4998 | |
| 4999 | if (!bind_conf->is_ssl) { |
| 5000 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5001 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5002 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5003 | } |
| 5004 | return 0; |
| 5005 | } |
| 5006 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5007 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5008 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5009 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5010 | } |
| 5011 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5012 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5013 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5014 | return -1; |
| 5015 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5016 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5017 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5018 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5019 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5020 | sizeof(*sh_ssl_sess_tree), |
| 5021 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5022 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5023 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5024 | ha_alert("Unable to initialize the lock for the shared SSL session cache. You can retry using the global statement 'tune.ssl.force-private-cache' but it could increase CPU usage due to renegotiations if nbproc > 1.\n"); |
| 5025 | else |
| 5026 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5027 | return -1; |
| 5028 | } |
| 5029 | /* free block callback */ |
| 5030 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5031 | /* init the root tree within the extra space */ |
| 5032 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5033 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5034 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5035 | err = 0; |
| 5036 | /* initialize all certificate contexts */ |
| 5037 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5038 | |
| 5039 | /* initialize CA variables if the certificates generation is enabled */ |
| 5040 | err += ssl_sock_load_ca(bind_conf); |
| 5041 | |
| 5042 | return -err; |
| 5043 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5044 | |
| 5045 | /* release ssl context allocated for servers. */ |
| 5046 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5047 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5048 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5049 | if (srv->ssl_ctx.alpn_str) |
| 5050 | free(srv->ssl_ctx.alpn_str); |
| 5051 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5052 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5053 | if (srv->ssl_ctx.npn_str) |
| 5054 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5055 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5056 | if (srv->ssl_ctx.ctx) |
| 5057 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 5058 | } |
| 5059 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5060 | /* Walks down the two trees in bind_conf and frees all the certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5061 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5062 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5063 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5064 | { |
| 5065 | struct ebmb_node *node, *back; |
| 5066 | struct sni_ctx *sni; |
| 5067 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5068 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5069 | while (node) { |
| 5070 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5071 | back = ebmb_next(node); |
| 5072 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5073 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5074 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5075 | ssl_sock_free_ssl_conf(sni->conf); |
| 5076 | free(sni->conf); |
| 5077 | sni->conf = NULL; |
| 5078 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5079 | free(sni); |
| 5080 | node = back; |
| 5081 | } |
| 5082 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5083 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5084 | while (node) { |
| 5085 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5086 | back = ebmb_next(node); |
| 5087 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5088 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5089 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5090 | ssl_sock_free_ssl_conf(sni->conf); |
| 5091 | free(sni->conf); |
| 5092 | sni->conf = NULL; |
| 5093 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5094 | free(sni); |
| 5095 | node = back; |
| 5096 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5097 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5098 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5099 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5100 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5101 | } |
| 5102 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5103 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5104 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5105 | { |
| 5106 | ssl_sock_free_ca(bind_conf); |
| 5107 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5108 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5109 | free(bind_conf->ca_sign_file); |
| 5110 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5111 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5112 | free(bind_conf->keys_ref->filename); |
| 5113 | free(bind_conf->keys_ref->tlskeys); |
| 5114 | LIST_DEL(&bind_conf->keys_ref->list); |
| 5115 | free(bind_conf->keys_ref); |
| 5116 | } |
| 5117 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5118 | bind_conf->ca_sign_pass = NULL; |
| 5119 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5120 | } |
| 5121 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5122 | /* Load CA cert file and private key used to generate certificates */ |
| 5123 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5124 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5125 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5126 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5127 | FILE *fp; |
| 5128 | X509 *cacert = NULL; |
| 5129 | EVP_PKEY *capkey = NULL; |
| 5130 | int err = 0; |
| 5131 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5132 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5133 | return err; |
| 5134 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5135 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5136 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5137 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5138 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5139 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5140 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5141 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5142 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5143 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5144 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5145 | "no CA certificate File configured at [%s:%d].\n", |
| 5146 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5147 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5148 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5149 | |
| 5150 | /* read in the CA certificate */ |
| 5151 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5152 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5153 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5154 | goto load_error; |
| 5155 | } |
| 5156 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5157 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5158 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5159 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5160 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5161 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5162 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5163 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5164 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5165 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5166 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5167 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5168 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5169 | bind_conf->ca_sign_cert = cacert; |
| 5170 | bind_conf->ca_sign_pkey = capkey; |
| 5171 | return err; |
| 5172 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5173 | read_error: |
| 5174 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5175 | if (capkey) EVP_PKEY_free(capkey); |
| 5176 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5177 | load_error: |
| 5178 | bind_conf->generate_certs = 0; |
| 5179 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5180 | return err; |
| 5181 | } |
| 5182 | |
| 5183 | /* Release CA cert and private key used to generate certificated */ |
| 5184 | void |
| 5185 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5186 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5187 | if (bind_conf->ca_sign_pkey) |
| 5188 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5189 | if (bind_conf->ca_sign_cert) |
| 5190 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5191 | bind_conf->ca_sign_pkey = NULL; |
| 5192 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5195 | /* |
| 5196 | * This function is called if SSL * context is not yet allocated. The function |
| 5197 | * is designed to be called before any other data-layer operation and sets the |
| 5198 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5199 | * It returns 0 on success and -1 in error case. |
| 5200 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5201 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5202 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5203 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5204 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5205 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5206 | return 0; |
| 5207 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5208 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5209 | return 0; |
| 5210 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5211 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5212 | if (!ctx) { |
| 5213 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5214 | return -1; |
| 5215 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5216 | ctx->wait_event.tasklet = tasklet_new(); |
| 5217 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5218 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5219 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5220 | return -1; |
| 5221 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5222 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5223 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5224 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5225 | ctx->sent_early_data = 0; |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5226 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5227 | ctx->conn = conn; |
Olivier Houchard | 81284e6 | 2019-06-06 13:21:23 +0200 | [diff] [blame] | 5228 | ctx->send_wait = NULL; |
| 5229 | ctx->recv_wait = NULL; |
Emeric Brun | 87cfd66 | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5230 | ctx->xprt_st = 0; |
| 5231 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5232 | |
| 5233 | /* Only work with sockets for now, this should be adapted when we'll |
| 5234 | * add QUIC support. |
| 5235 | */ |
| 5236 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5237 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5238 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5239 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5240 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5241 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5242 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 5243 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5244 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5245 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5246 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5247 | /* If it is in client mode initiate SSL session |
| 5248 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5249 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5250 | int may_retry = 1; |
| 5251 | |
| 5252 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5253 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5254 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 5255 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5256 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5257 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5258 | goto retry_connect; |
| 5259 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5260 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5261 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5262 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5263 | ctx->bio = BIO_new(ha_meth); |
| 5264 | if (!ctx->bio) { |
Olivier Houchard | b451b97 | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5265 | SSL_free(ctx->ssl); |
| 5266 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5267 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5268 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5269 | goto retry_connect; |
| 5270 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5271 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5272 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5273 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5274 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5275 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5276 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5277 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5278 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5279 | SSL_free(ctx->ssl); |
| 5280 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5281 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5282 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5283 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5284 | goto retry_connect; |
| 5285 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5286 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5287 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5288 | } |
| 5289 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5290 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5291 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5292 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5293 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, __objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5294 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5295 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5296 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5297 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5298 | } else if (sess) { |
| 5299 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5300 | } |
| 5301 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5302 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5303 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5304 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5305 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5306 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5307 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5308 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5309 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5310 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5311 | if (conn->flags & CO_FL_ERROR) |
| 5312 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5313 | return 0; |
| 5314 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5315 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5316 | int may_retry = 1; |
| 5317 | |
| 5318 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5319 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5320 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5321 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5322 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5323 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5324 | goto retry_accept; |
| 5325 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5326 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5327 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5328 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5329 | ctx->bio = BIO_new(ha_meth); |
| 5330 | if (!ctx->bio) { |
Olivier Houchard | b451b97 | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5331 | SSL_free(ctx->ssl); |
| 5332 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5333 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5334 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5335 | goto retry_accept; |
| 5336 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5337 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5338 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5339 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5340 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5341 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5342 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5343 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5344 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5345 | SSL_free(ctx->ssl); |
| 5346 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5347 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5348 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5349 | goto retry_accept; |
| 5350 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5351 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5352 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5353 | } |
| 5354 | |
Frédéric Lécaille | 583362f | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5355 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5356 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 5357 | b_alloc(&ctx->early_buf); |
| 5358 | SSL_set_max_early_data(ctx->ssl, |
| 5359 | /* Only allow early data if we managed to allocate |
| 5360 | * a buffer. |
| 5361 | */ |
| 5362 | (!b_is_null(&ctx->early_buf)) ? |
| 5363 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5364 | } |
| 5365 | #endif |
| 5366 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5367 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5368 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5369 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5370 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | 510fce5 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5371 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5372 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5373 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5374 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5375 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5376 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5377 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5378 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5379 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5380 | if (conn->flags & CO_FL_ERROR) |
| 5381 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5382 | return 0; |
| 5383 | } |
| 5384 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5385 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5386 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5387 | if (ctx && ctx->wait_event.tasklet) |
| 5388 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5389 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5390 | return -1; |
| 5391 | } |
| 5392 | |
| 5393 | |
| 5394 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5395 | * updates the FD status if it wants some polling before being called again. |
| 5396 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5397 | * otherwise it returns non-zero and removes itself from the connection's |
| 5398 | * flags (the bit is provided in <flag> by the caller). |
| 5399 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5400 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5401 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5402 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5403 | int ret; |
| 5404 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5405 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5406 | return 0; |
| 5407 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5408 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5409 | goto out_error; |
| 5410 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5411 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5412 | /* |
| 5413 | * Check if we have early data. If we do, we have to read them |
| 5414 | * before SSL_do_handshake() is called, And there's no way to |
| 5415 | * detect early data, except to try to read them |
| 5416 | */ |
| 5417 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5418 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5419 | |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5420 | while (1) { |
| 5421 | ret = SSL_read_early_data(ctx->ssl, |
| 5422 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5423 | &read_data); |
| 5424 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5425 | goto check_error; |
| 5426 | if (read_data > 0) { |
| 5427 | conn->flags |= CO_FL_EARLY_DATA; |
| 5428 | b_add(&ctx->early_buf, read_data); |
| 5429 | } |
| 5430 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5431 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5432 | if (!b_data(&ctx->early_buf)) |
| 5433 | b_free(&ctx->early_buf); |
| 5434 | break; |
| 5435 | } |
| 5436 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5437 | } |
| 5438 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5439 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5440 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5441 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5442 | * the reneg handshake. |
| 5443 | * Here we use SSL_peek as a workaround for reneg. |
| 5444 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5445 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5446 | char c; |
| 5447 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5448 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5449 | if (ret <= 0) { |
| 5450 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5451 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5452 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5453 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5454 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5455 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5456 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5457 | return 0; |
| 5458 | } |
| 5459 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5460 | /* handshake may have been completed but we have |
| 5461 | * no more data to read. |
| 5462 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5463 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5464 | ret = 1; |
| 5465 | goto reneg_ok; |
| 5466 | } |
| 5467 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5468 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5469 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5472 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5473 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5474 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5475 | return 0; |
| 5476 | } |
| 5477 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5478 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5479 | /* if errno is null, then connection was successfully established */ |
| 5480 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5481 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5482 | if (!conn->err_code) { |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5483 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5484 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5485 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5486 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5487 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5488 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5489 | /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5490 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5491 | empty_handshake = state == TLS_ST_BEFORE; |
| 5492 | #else |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5493 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5494 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5495 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5496 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5497 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5498 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5499 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5500 | else |
| 5501 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5502 | } |
| 5503 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5504 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5505 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5506 | else |
| 5507 | conn->err_code = CO_ER_SSL_ABORT; |
| 5508 | } |
| 5509 | } |
| 5510 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5511 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5512 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5513 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5514 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5515 | } |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5516 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5517 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5518 | goto out_error; |
| 5519 | } |
| 5520 | else { |
| 5521 | /* Fail on all other handshake errors */ |
| 5522 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5523 | * buffer, causing an RST to be emitted upon close() on |
| 5524 | * TCP sockets. We first try to drain possibly pending |
| 5525 | * data to avoid this as much as possible. |
| 5526 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5527 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5528 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5529 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5530 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5531 | goto out_error; |
| 5532 | } |
| 5533 | } |
| 5534 | /* read some data: consider handshake completed */ |
| 5535 | goto reneg_ok; |
| 5536 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5537 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5538 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5539 | if (ret != 1) { |
| 5540 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5541 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5542 | |
| 5543 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5544 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5545 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5546 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5547 | return 0; |
| 5548 | } |
| 5549 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5550 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5551 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5552 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5553 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5554 | return 0; |
| 5555 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5556 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5557 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5558 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5559 | return 0; |
| 5560 | } |
| 5561 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5562 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5563 | /* if errno is null, then connection was successfully established */ |
| 5564 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5565 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5566 | if (!conn->err_code) { |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5567 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5568 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5569 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5570 | #else |
| 5571 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5572 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5573 | /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5574 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5575 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5576 | #else |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5577 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5578 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5579 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5580 | if (empty_handshake) { |
| 5581 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5582 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5583 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5584 | else |
| 5585 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5586 | } |
| 5587 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5588 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5589 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5590 | else |
| 5591 | conn->err_code = CO_ER_SSL_ABORT; |
| 5592 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5593 | } |
| 5594 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5595 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5596 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5597 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5598 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5599 | } |
Lukas Tribus | 5db881f | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5600 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5601 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5602 | goto out_error; |
| 5603 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5604 | else { |
| 5605 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5606 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5607 | * buffer, causing an RST to be emitted upon close() on |
| 5608 | * TCP sockets. We first try to drain possibly pending |
| 5609 | * data to avoid this as much as possible. |
| 5610 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5611 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5612 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5613 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5614 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5615 | goto out_error; |
| 5616 | } |
| 5617 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5618 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5619 | else { |
| 5620 | /* |
| 5621 | * If the server refused the early data, we have to send a |
| 5622 | * 425 to the client, as we no longer have the data to sent |
| 5623 | * them again. |
| 5624 | */ |
| 5625 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5626 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5627 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5628 | goto out_error; |
| 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | #endif |
| 5633 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5634 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5635 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5636 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5637 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5638 | /* ASYNC engine API doesn't support moving read/write |
| 5639 | * buffers. So we disable ASYNC mode right after |
| 5640 | * the handshake to avoid buffer oveflows. |
| 5641 | */ |
| 5642 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5643 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5644 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5645 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5646 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5647 | if (objt_server(conn->target)) { |
| 5648 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5649 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5650 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5651 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5652 | else { |
| 5653 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5654 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5655 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5656 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | /* The connection is now established at both layers, it's time to leave */ |
| 5660 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5661 | return 1; |
| 5662 | |
| 5663 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5664 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5665 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5666 | ERR_clear_error(); |
| 5667 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5668 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5669 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5670 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5671 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5672 | } |
| 5673 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5674 | /* Fail on all other handshake errors */ |
| 5675 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5676 | if (!conn->err_code) |
| 5677 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5678 | return 0; |
| 5679 | } |
| 5680 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5681 | static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5682 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5683 | struct wait_event *sw; |
| 5684 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5685 | |
Olivier Houchard | a37eb6a | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5686 | if (!ctx) |
| 5687 | return -1; |
| 5688 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5689 | if (event_type & SUB_RETRY_RECV) { |
| 5690 | sw = param; |
| 5691 | BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5692 | sw->events |= SUB_RETRY_RECV; |
| 5693 | ctx->recv_wait = sw; |
| 5694 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5695 | !(ctx->wait_event.events & SUB_RETRY_RECV)) |
| 5696 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
| 5697 | event_type &= ~SUB_RETRY_RECV; |
| 5698 | } |
| 5699 | if (event_type & SUB_RETRY_SEND) { |
| 5700 | sw = param; |
| 5701 | BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5702 | sw->events |= SUB_RETRY_SEND; |
| 5703 | ctx->send_wait = sw; |
| 5704 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5705 | !(ctx->wait_event.events & SUB_RETRY_SEND)) |
| 5706 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
| 5707 | event_type &= ~SUB_RETRY_SEND; |
| 5708 | |
| 5709 | } |
| 5710 | if (event_type != 0) |
| 5711 | return -1; |
| 5712 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5713 | } |
| 5714 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5715 | static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5716 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5717 | struct wait_event *sw; |
| 5718 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5719 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5720 | if (event_type & SUB_RETRY_RECV) { |
| 5721 | sw = param; |
| 5722 | BUG_ON(ctx->recv_wait != sw); |
| 5723 | ctx->recv_wait = NULL; |
| 5724 | sw->events &= ~SUB_RETRY_RECV; |
| 5725 | /* If we subscribed, and we're not doing the handshake, |
| 5726 | * then we subscribed because the upper layer asked for it, |
| 5727 | * as the upper layer is no longer interested, we can |
| 5728 | * unsubscribe too. |
| 5729 | */ |
| 5730 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5731 | (ctx->wait_event.events & SUB_RETRY_RECV)) |
| 5732 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 5733 | &ctx->wait_event); |
| 5734 | } |
| 5735 | if (event_type & SUB_RETRY_SEND) { |
| 5736 | sw = param; |
| 5737 | BUG_ON(ctx->send_wait != sw); |
| 5738 | ctx->send_wait = NULL; |
| 5739 | sw->events &= ~SUB_RETRY_SEND; |
| 5740 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5741 | (ctx->wait_event.events & SUB_RETRY_SEND)) |
| 5742 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, |
| 5743 | &ctx->wait_event); |
| 5744 | |
| 5745 | } |
| 5746 | |
| 5747 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5748 | } |
| 5749 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5750 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5751 | * Returns 0 on success, and non-zero on failure. |
| 5752 | */ |
| 5753 | static int ssl_add_xprt(struct connection *conn, void *xprt_ctx, void *toadd_ctx, const struct xprt_ops *toadd_ops, void **oldxprt_ctx, const struct xprt_ops **oldxprt_ops) |
| 5754 | { |
| 5755 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5756 | |
| 5757 | if (oldxprt_ops != NULL) |
| 5758 | *oldxprt_ops = ctx->xprt; |
| 5759 | if (oldxprt_ctx != NULL) |
| 5760 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5761 | ctx->xprt = toadd_ops; |
| 5762 | ctx->xprt_ctx = toadd_ctx; |
| 5763 | return 0; |
| 5764 | } |
| 5765 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5766 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5767 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5768 | * XPRT. |
| 5769 | */ |
| 5770 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5771 | { |
| 5772 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5773 | |
| 5774 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5775 | ctx->xprt_ctx = newctx; |
| 5776 | ctx->xprt = newops; |
| 5777 | return 0; |
| 5778 | } |
| 5779 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5780 | } |
| 5781 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5782 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 5783 | { |
| 5784 | struct ssl_sock_ctx *ctx = context; |
| 5785 | |
| 5786 | /* First if we're doing an handshake, try that */ |
| 5787 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 5788 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 5789 | /* If we had an error, or the handshake is done and I/O is available, |
| 5790 | * let the upper layer know. |
| 5791 | * If no mux was set up yet, and nobody subscribed, then call |
| 5792 | * xprt_done_cb() ourself if it's set, or destroy the connection, |
| 5793 | * we can't be sure conn_fd_handler() will be called again. |
| 5794 | */ |
| 5795 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5796 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5797 | int ret = 0; |
| 5798 | int woke = 0; |
| 5799 | |
| 5800 | /* On error, wake any waiter */ |
| 5801 | if (ctx->recv_wait) { |
| 5802 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5803 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5804 | ctx->recv_wait = NULL; |
| 5805 | woke = 1; |
| 5806 | } |
| 5807 | if (ctx->send_wait) { |
| 5808 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5809 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5810 | ctx->send_wait = NULL; |
| 5811 | woke = 1; |
| 5812 | } |
| 5813 | /* If we're the first xprt for the connection, let the |
| 5814 | * upper layers know. If xprt_done_cb() is set, call it, |
| 5815 | * otherwise, we should have a mux, so call its wake |
| 5816 | * method if we didn't woke a tasklet already. |
| 5817 | */ |
| 5818 | if (ctx->conn->xprt_ctx == ctx) { |
| 5819 | if (ctx->conn->xprt_done_cb) |
| 5820 | ret = ctx->conn->xprt_done_cb(ctx->conn); |
| 5821 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 5822 | ctx->conn->mux->wake(ctx->conn); |
| 5823 | return NULL; |
| 5824 | } |
| 5825 | } |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5826 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5827 | /* If we have early data and somebody wants to receive, let them */ |
| 5828 | else if (b_data(&ctx->early_buf) && ctx->recv_wait) { |
| 5829 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
| 5830 | tasklet_wakeup(ctx->recv_wait->tasklet); |
| 5831 | ctx->recv_wait = NULL; |
| 5832 | |
| 5833 | } |
| 5834 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5835 | return NULL; |
| 5836 | } |
| 5837 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5838 | /* Receive up to <count> bytes from connection <conn>'s socket and store them |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5839 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5840 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5841 | * flags are updated with whatever special event is detected (error, read0, |
| 5842 | * empty). The caller is responsible for taking care of those events and |
| 5843 | * avoiding the call if inappropriate. The function does not call the |
| 5844 | * connection's polling update function, so the caller is responsible for this. |
| 5845 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5846 | static size_t ssl_sock_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5847 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5848 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5849 | ssize_t ret; |
| 5850 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5851 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5852 | conn_refresh_polling_flags(conn); |
| 5853 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5854 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5855 | goto out_error; |
| 5856 | |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5857 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5858 | if (b_data(&ctx->early_buf)) { |
| 5859 | try = b_contig_space(buf); |
| 5860 | if (try > b_data(&ctx->early_buf)) |
| 5861 | try = b_data(&ctx->early_buf); |
| 5862 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 5863 | b_add(buf, try); |
| 5864 | b_del(&ctx->early_buf, try); |
| 5865 | if (b_data(&ctx->early_buf) == 0) |
| 5866 | b_free(&ctx->early_buf); |
| 5867 | return try; |
| 5868 | } |
| 5869 | #endif |
| 5870 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5871 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5872 | /* a handshake was requested */ |
| 5873 | return 0; |
| 5874 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5875 | /* read the largest possible block. For this, we perform only one call |
| 5876 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5877 | * in which case we accept to do it once again. A new attempt is made on |
| 5878 | * EINTR too. |
| 5879 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5880 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5881 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5882 | try = b_contig_space(buf); |
| 5883 | if (!try) |
| 5884 | break; |
| 5885 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5886 | if (try > count) |
| 5887 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5888 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5889 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | 510fce5 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5890 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5891 | if (conn->flags & CO_FL_ERROR) { |
| 5892 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5893 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5894 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5895 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5896 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5897 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5898 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5899 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5900 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5901 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5902 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5903 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5904 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5905 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5906 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5907 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5908 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5909 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5910 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5911 | break; |
| 5912 | } |
| 5913 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5914 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5915 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5916 | SUB_RETRY_RECV, |
| 5917 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5918 | /* handshake is running, and it may need to re-enable read */ |
| 5919 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5920 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5921 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5922 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5923 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5924 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5925 | break; |
| 5926 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5927 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5928 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5929 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5930 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5931 | * stack before shutting down the connection for |
| 5932 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5933 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5934 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5935 | /* otherwise it's a real error */ |
| 5936 | goto out_error; |
| 5937 | } |
| 5938 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5939 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5940 | return done; |
| 5941 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5942 | clear_ssl_error: |
| 5943 | /* Clear openssl global errors stack */ |
| 5944 | ssl_sock_dump_errors(conn); |
| 5945 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5946 | read0: |
| 5947 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5948 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5949 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5950 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5951 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5952 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5953 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5954 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5955 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5956 | } |
| 5957 | |
| 5958 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5959 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5960 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5961 | * other pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5962 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5963 | * a second call may be performed. The connection's flags are updated with |
| 5964 | * whatever special event is detected (error, empty). The caller is responsible |
| 5965 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5966 | * function does not call the connection's polling update function, so the caller |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5967 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5968 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5969 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5970 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5971 | static size_t ssl_sock_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5972 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5973 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5974 | ssize_t ret; |
| 5975 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5976 | |
| 5977 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5978 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5979 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5980 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5981 | goto out_error; |
| 5982 | |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5983 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5984 | /* a handshake was requested */ |
| 5985 | return 0; |
| 5986 | |
| 5987 | /* send the largest possible block. For this we perform only one call |
| 5988 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5989 | * in which case we accept to do it once again. |
| 5990 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5991 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5992 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5993 | size_t written_data; |
| 5994 | #endif |
| 5995 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5996 | try = b_contig_data(buf, done); |
| 5997 | if (try > count) |
| 5998 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5999 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6000 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6001 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6002 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6003 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6004 | } |
| 6005 | else { |
| 6006 | /* we need to keep the information about the fact that |
| 6007 | * we're not limiting the upcoming send(), because if it |
| 6008 | * fails, we'll have to retry with at least as many data. |
| 6009 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6010 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6011 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6012 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6013 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6014 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6015 | unsigned int max_early; |
| 6016 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6017 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6018 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6019 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6020 | if (SSL_get0_session(ctx->ssl)) |
| 6021 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl)); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6022 | else |
| 6023 | max_early = 0; |
| 6024 | } |
| 6025 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6026 | if (try + ctx->sent_early_data > max_early) { |
| 6027 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6028 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6029 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6030 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6031 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6032 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6033 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6034 | ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6035 | if (ret == 1) { |
| 6036 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6037 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6038 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6039 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6040 | /* Initiate the handshake, now */ |
| 6041 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6042 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6043 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | } else |
| 6047 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6048 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6049 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6050 | if (conn->flags & CO_FL_ERROR) { |
| 6051 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6052 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6053 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6054 | if (ret > 0) { |
Olivier Houchard | f24502b | 2019-01-17 19:09:11 +0100 | [diff] [blame] | 6055 | /* A send succeeded, so we can consier ourself connected */ |
| 6056 | conn->flags |= CO_FL_CONNECTED; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6057 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6058 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6059 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6060 | } |
| 6061 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6062 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6063 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6064 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6065 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6066 | /* handshake is running, and it may need to re-enable write */ |
| 6067 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6068 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6069 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6070 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6071 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6072 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6073 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6074 | break; |
| 6075 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6076 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6077 | break; |
| 6078 | } |
| 6079 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6080 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6081 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6082 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6083 | SUB_RETRY_RECV, |
| 6084 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6085 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6086 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6087 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6088 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6089 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6090 | break; |
| 6091 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6092 | goto out_error; |
| 6093 | } |
| 6094 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6095 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6096 | return done; |
| 6097 | |
| 6098 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6099 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6100 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6101 | ERR_clear_error(); |
| 6102 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6103 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6104 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6105 | } |
| 6106 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6107 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6108 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6109 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6110 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6111 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6112 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6113 | if (ctx->wait_event.events != 0) |
| 6114 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6115 | ctx->wait_event.events, |
| 6116 | &ctx->wait_event); |
| 6117 | if (ctx->send_wait) { |
| 6118 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6119 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6120 | } |
| 6121 | if (ctx->recv_wait) { |
| 6122 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6123 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6124 | } |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6125 | if (ctx->xprt->close) |
| 6126 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6127 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6128 | if (global_ssl.async) { |
| 6129 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6130 | size_t num_all_fds = 0; |
| 6131 | int i; |
| 6132 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6133 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6134 | if (num_all_fds > 32) { |
| 6135 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6136 | return; |
| 6137 | } |
| 6138 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6139 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6140 | |
| 6141 | /* If an async job is pending, we must try to |
| 6142 | to catch the end using polling before calling |
| 6143 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6144 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6145 | for (i=0 ; i < num_all_fds ; i++) { |
| 6146 | /* switch on an handler designed to |
| 6147 | * handle the SSL_free |
| 6148 | */ |
| 6149 | afd = all_fd[i]; |
| 6150 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6151 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6152 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6153 | /* To ensure that the fd cache won't be used |
| 6154 | * and we'll catch a real RD event. |
| 6155 | */ |
| 6156 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6157 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6158 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6159 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6160 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6161 | return; |
| 6162 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6163 | /* Else we can remove the fds from the fdtab |
| 6164 | * and call SSL_free. |
| 6165 | * note: we do a fd_remove and not a delete |
| 6166 | * because the fd is owned by the engine. |
| 6167 | * the engine is responsible to close |
| 6168 | */ |
| 6169 | for (i=0 ; i < num_all_fds ; i++) |
| 6170 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6171 | } |
| 6172 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6173 | SSL_free(ctx->ssl); |
Olivier Houchard | f6715e7 | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6174 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6175 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6176 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6177 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6178 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6182 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6183 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6184 | static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6185 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6186 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6187 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6188 | if (conn->flags & CO_FL_HANDSHAKE) |
| 6189 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6190 | if (!clean) |
| 6191 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6192 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6193 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6194 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6195 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6196 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6197 | ERR_clear_error(); |
| 6198 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6201 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6202 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6203 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6204 | struct ssl_sock_ctx *ctx; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6205 | struct pkey_info *pkinfo; |
| 6206 | int bits = 0; |
| 6207 | int sig = TLSEXT_signature_anonymous; |
| 6208 | int len = -1; |
| 6209 | |
| 6210 | if (!ssl_sock_is_ssl(conn)) |
| 6211 | return 0; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6212 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6213 | pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6214 | if (pkinfo) { |
| 6215 | sig = pkinfo->sig; |
| 6216 | bits = pkinfo->bits; |
| 6217 | } else { |
| 6218 | /* multicert and generated cert have no pkey info */ |
| 6219 | X509 *crt; |
| 6220 | EVP_PKEY *pkey; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6221 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6222 | if (!crt) |
| 6223 | return 0; |
| 6224 | pkey = X509_get_pubkey(crt); |
| 6225 | if (pkey) { |
| 6226 | bits = EVP_PKEY_bits(pkey); |
| 6227 | switch(EVP_PKEY_base_id(pkey)) { |
| 6228 | case EVP_PKEY_RSA: |
| 6229 | sig = TLSEXT_signature_rsa; |
| 6230 | break; |
| 6231 | case EVP_PKEY_EC: |
| 6232 | sig = TLSEXT_signature_ecdsa; |
| 6233 | break; |
| 6234 | case EVP_PKEY_DSA: |
| 6235 | sig = TLSEXT_signature_dsa; |
| 6236 | break; |
| 6237 | } |
| 6238 | EVP_PKEY_free(pkey); |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | switch(sig) { |
| 6243 | case TLSEXT_signature_rsa: |
| 6244 | len = chunk_printf(out, "RSA%d", bits); |
| 6245 | break; |
| 6246 | case TLSEXT_signature_ecdsa: |
| 6247 | len = chunk_printf(out, "EC%d", bits); |
| 6248 | break; |
| 6249 | case TLSEXT_signature_dsa: |
| 6250 | len = chunk_printf(out, "DSA%d", bits); |
| 6251 | break; |
| 6252 | default: |
| 6253 | return 0; |
| 6254 | } |
| 6255 | if (len < 0) |
| 6256 | return 0; |
| 6257 | return 1; |
| 6258 | } |
| 6259 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6260 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6261 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6262 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6263 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6264 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6265 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6266 | X509 *crt; |
| 6267 | |
| 6268 | if (!ssl_sock_is_ssl(conn)) |
| 6269 | return NULL; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6270 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6271 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6272 | if (!crt) |
| 6273 | return NULL; |
| 6274 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6275 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6276 | } |
| 6277 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6278 | /* used for ppv2 authority */ |
| 6279 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6280 | { |
| 6281 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6282 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6283 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6284 | if (!ssl_sock_is_ssl(conn)) |
| 6285 | return NULL; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6286 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6287 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6288 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6289 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6290 | #endif |
| 6291 | } |
| 6292 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6293 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6294 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6295 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6296 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6297 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6298 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6299 | return NULL; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6300 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6301 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6302 | } |
| 6303 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6304 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6305 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6306 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6307 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6308 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6309 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6310 | return NULL; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6311 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6312 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6313 | } |
| 6314 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6315 | /* Extract a serial from a cert, and copy it to a chunk. |
| 6316 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 6317 | * -1 if output is not large enough. |
| 6318 | */ |
| 6319 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6320 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6321 | { |
| 6322 | ASN1_INTEGER *serial; |
| 6323 | |
| 6324 | serial = X509_get_serialNumber(crt); |
| 6325 | if (!serial) |
| 6326 | return 0; |
| 6327 | |
| 6328 | if (out->size < serial->length) |
| 6329 | return -1; |
| 6330 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6331 | memcpy(out->area, serial->data, serial->length); |
| 6332 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6333 | return 1; |
| 6334 | } |
| 6335 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6336 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6337 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 6338 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6339 | */ |
| 6340 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6341 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6342 | { |
| 6343 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6344 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6345 | |
| 6346 | len =i2d_X509(crt, NULL); |
| 6347 | if (len <= 0) |
| 6348 | return 1; |
| 6349 | |
| 6350 | if (out->size < len) |
| 6351 | return -1; |
| 6352 | |
| 6353 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6354 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6355 | return 1; |
| 6356 | } |
| 6357 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6358 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6359 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6360 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 6361 | * and -1 if output is not large enough. |
| 6362 | */ |
| 6363 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6364 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6365 | { |
| 6366 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 6367 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 6368 | |
| 6369 | if (gentm->length < 12) |
| 6370 | return 0; |
| 6371 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 6372 | return 0; |
| 6373 | if (out->size < gentm->length-2) |
| 6374 | return -1; |
| 6375 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6376 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 6377 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6378 | return 1; |
| 6379 | } |
| 6380 | else if (tm->type == V_ASN1_UTCTIME) { |
| 6381 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 6382 | |
| 6383 | if (utctm->length < 10) |
| 6384 | return 0; |
| 6385 | if (utctm->data[0] >= 0x35) |
| 6386 | return 0; |
| 6387 | if (out->size < utctm->length) |
| 6388 | return -1; |
| 6389 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6390 | memcpy(out->area, utctm->data, utctm->length); |
| 6391 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6392 | return 1; |
| 6393 | } |
| 6394 | |
| 6395 | return 0; |
| 6396 | } |
| 6397 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6398 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 6399 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 6400 | */ |
| 6401 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6402 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 6403 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6404 | { |
| 6405 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6406 | ASN1_OBJECT *obj; |
| 6407 | ASN1_STRING *data; |
| 6408 | const unsigned char *data_ptr; |
| 6409 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6410 | int i, j, n; |
| 6411 | int cur = 0; |
| 6412 | const char *s; |
| 6413 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6414 | int name_count; |
| 6415 | |
| 6416 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6417 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6418 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6419 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6420 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6421 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6422 | else |
| 6423 | j = i; |
| 6424 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6425 | ne = X509_NAME_get_entry(a, j); |
| 6426 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6427 | data = X509_NAME_ENTRY_get_data(ne); |
| 6428 | data_ptr = ASN1_STRING_get0_data(data); |
| 6429 | data_len = ASN1_STRING_length(data); |
| 6430 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6431 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6432 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6433 | s = tmp; |
| 6434 | } |
| 6435 | |
| 6436 | if (chunk_strcasecmp(entry, s) != 0) |
| 6437 | continue; |
| 6438 | |
| 6439 | if (pos < 0) |
| 6440 | cur--; |
| 6441 | else |
| 6442 | cur++; |
| 6443 | |
| 6444 | if (cur != pos) |
| 6445 | continue; |
| 6446 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6447 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6448 | return -1; |
| 6449 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6450 | memcpy(out->area, data_ptr, data_len); |
| 6451 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6452 | return 1; |
| 6453 | } |
| 6454 | |
| 6455 | return 0; |
| 6456 | |
| 6457 | } |
| 6458 | |
| 6459 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 6460 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 6461 | */ |
| 6462 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6463 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6464 | { |
| 6465 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6466 | ASN1_OBJECT *obj; |
| 6467 | ASN1_STRING *data; |
| 6468 | const unsigned char *data_ptr; |
| 6469 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6470 | int i, n, ln; |
| 6471 | int l = 0; |
| 6472 | const char *s; |
| 6473 | char *p; |
| 6474 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6475 | int name_count; |
| 6476 | |
| 6477 | |
| 6478 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6479 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6480 | out->data = 0; |
| 6481 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6482 | for (i = 0; i < name_count; i++) { |
| 6483 | ne = X509_NAME_get_entry(a, i); |
| 6484 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6485 | data = X509_NAME_ENTRY_get_data(ne); |
| 6486 | data_ptr = ASN1_STRING_get0_data(data); |
| 6487 | data_len = ASN1_STRING_length(data); |
| 6488 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6489 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6490 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6491 | s = tmp; |
| 6492 | } |
| 6493 | ln = strlen(s); |
| 6494 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6495 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6496 | if (l > out->size) |
| 6497 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6498 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6499 | |
| 6500 | *(p++)='/'; |
| 6501 | memcpy(p, s, ln); |
| 6502 | p += ln; |
| 6503 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6504 | memcpy(p, data_ptr, data_len); |
| 6505 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6506 | } |
| 6507 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6508 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6509 | return 0; |
| 6510 | |
| 6511 | return 1; |
| 6512 | } |
| 6513 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6514 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6515 | { |
| 6516 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6517 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6518 | |
Olivier Houchard | aa2ecea | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6519 | if (!ssl_sock_is_ssl(conn)) |
| 6520 | return; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6521 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6522 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6523 | #endif |
| 6524 | } |
| 6525 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6526 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6527 | * to disable SNI. |
| 6528 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6529 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6530 | { |
| 6531 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6532 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6533 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6534 | char *prev_name; |
| 6535 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6536 | if (!ssl_sock_is_ssl(conn)) |
| 6537 | return; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6538 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6539 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6540 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6541 | * new connection will present a new SNI. As an optimization we could |
| 6542 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6543 | * server. |
| 6544 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6545 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6546 | if ((!prev_name && hostname) || |
| 6547 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6548 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6549 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6550 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6551 | #endif |
| 6552 | } |
| 6553 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6554 | /* Extract peer certificate's common name into the chunk dest |
| 6555 | * Returns |
| 6556 | * the len of the extracted common name |
| 6557 | * or 0 if no CN found in DN |
| 6558 | * or -1 on error case (i.e. no peer certificate) |
| 6559 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6560 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6561 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6562 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6563 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6564 | X509 *crt = NULL; |
| 6565 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6566 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6567 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6568 | .area = (char *)&find_cn, |
| 6569 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6570 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6571 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6572 | |
| 6573 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6574 | goto out; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6575 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6576 | |
| 6577 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6578 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6579 | if (!crt) |
| 6580 | goto out; |
| 6581 | |
| 6582 | name = X509_get_subject_name(crt); |
| 6583 | if (!name) |
| 6584 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6585 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6586 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6587 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6588 | if (crt) |
| 6589 | X509_free(crt); |
| 6590 | |
| 6591 | return result; |
| 6592 | } |
| 6593 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6594 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6595 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6596 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6597 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6598 | X509 *crt = NULL; |
| 6599 | |
| 6600 | if (!ssl_sock_is_ssl(conn)) |
| 6601 | return 0; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6602 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6603 | |
| 6604 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6605 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6606 | if (!crt) |
| 6607 | return 0; |
| 6608 | |
| 6609 | X509_free(crt); |
| 6610 | return 1; |
| 6611 | } |
| 6612 | |
| 6613 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6614 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6615 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6616 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6617 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6618 | if (!ssl_sock_is_ssl(conn)) |
| 6619 | return 0; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6620 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6621 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | /* returns result from SSL verify */ |
| 6625 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6626 | { |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6627 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6628 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6629 | if (!ssl_sock_is_ssl(conn)) |
| 6630 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 4517b0c | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6631 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6632 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6633 | } |
| 6634 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6635 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6636 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6637 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6638 | * freed by the caller. NPN is also checked if available since older versions |
| 6639 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6640 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6641 | static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6642 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6643 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6644 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6645 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6646 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6647 | return 0; |
| 6648 | |
| 6649 | *str = NULL; |
| 6650 | |
| 6651 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6652 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6653 | if (*str) |
| 6654 | return 1; |
| 6655 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6656 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6657 | SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6658 | if (*str) |
| 6659 | return 1; |
| 6660 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6661 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6662 | return 0; |
| 6663 | } |
| 6664 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6665 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6666 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6667 | static int |
| 6668 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6669 | { |
| 6670 | struct connection *conn; |
| 6671 | |
| 6672 | conn = objt_conn(smp->sess->origin); |
| 6673 | if (!conn || conn->xprt != &ssl_sock) |
| 6674 | return 0; |
| 6675 | |
| 6676 | smp->flags = 0; |
| 6677 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | bb8643c | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 6678 | #ifdef OPENSSL_IS_BORINGSSL |
| 6679 | { |
| 6680 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6681 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 6682 | SSL_early_data_accepted(ctx->ssl)); |
| 6683 | } |
| 6684 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6685 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 629693f | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 6686 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0; |
Emmanuel Hocdet | bb8643c | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 6687 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6688 | return 1; |
| 6689 | } |
| 6690 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6691 | /* boolean, returns true if client cert was present */ |
| 6692 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6693 | smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6694 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6695 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6696 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6697 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6698 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6699 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6700 | return 0; |
| 6701 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6702 | ctx = conn->xprt_ctx; |
| 6703 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6704 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6705 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6706 | return 0; |
| 6707 | } |
| 6708 | |
| 6709 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6710 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6711 | smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6712 | |
| 6713 | return 1; |
| 6714 | } |
| 6715 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6716 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6717 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6718 | * should be use. |
| 6719 | */ |
| 6720 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6721 | smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6722 | { |
| 6723 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6724 | X509 *crt = NULL; |
| 6725 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6726 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6727 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6728 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6729 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6730 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6731 | if (!conn || conn->xprt != &ssl_sock) |
| 6732 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6733 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6734 | |
| 6735 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6736 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6737 | return 0; |
| 6738 | } |
| 6739 | |
| 6740 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6741 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6742 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6743 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6744 | |
| 6745 | if (!crt) |
| 6746 | goto out; |
| 6747 | |
| 6748 | smp_trash = get_trash_chunk(); |
| 6749 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6750 | goto out; |
| 6751 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6752 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6753 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6754 | ret = 1; |
| 6755 | out: |
| 6756 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6757 | if (cert_peer && crt) |
| 6758 | X509_free(crt); |
| 6759 | return ret; |
| 6760 | } |
| 6761 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6762 | /* binary, returns serial of certificate in a binary chunk. |
| 6763 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6764 | * should be use. |
| 6765 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6766 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6767 | smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6768 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6769 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6770 | X509 *crt = NULL; |
| 6771 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6772 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6773 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6774 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6775 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6776 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6777 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6778 | return 0; |
| 6779 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6780 | ctx = conn->xprt_ctx; |
| 6781 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6782 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6783 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6784 | return 0; |
| 6785 | } |
| 6786 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6787 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6788 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6789 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6790 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6791 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6792 | if (!crt) |
| 6793 | goto out; |
| 6794 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6795 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6796 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6797 | goto out; |
| 6798 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6799 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6800 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6801 | ret = 1; |
| 6802 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6803 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6804 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6805 | X509_free(crt); |
| 6806 | return ret; |
| 6807 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6808 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6809 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6810 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6811 | * should be use. |
| 6812 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6813 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6814 | smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6815 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6816 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6817 | X509 *crt = NULL; |
| 6818 | const EVP_MD *digest; |
| 6819 | int ret = 0; |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 6820 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6821 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6822 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6823 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6824 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6825 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6826 | if (!conn || conn->xprt != &ssl_sock) |
| 6827 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6828 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6829 | |
| 6830 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6831 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6832 | return 0; |
| 6833 | } |
| 6834 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6835 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6836 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6837 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6838 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6839 | if (!crt) |
| 6840 | goto out; |
| 6841 | |
| 6842 | smp_trash = get_trash_chunk(); |
| 6843 | digest = EVP_sha1(); |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 6844 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 6845 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6846 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6847 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6848 | ret = 1; |
| 6849 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6850 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6851 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6852 | X509_free(crt); |
| 6853 | return ret; |
| 6854 | } |
| 6855 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6856 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6857 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6858 | * should be use. |
| 6859 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6860 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6861 | smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6862 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6863 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6864 | X509 *crt = NULL; |
| 6865 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6866 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6867 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6868 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6869 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6870 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6871 | if (!conn || conn->xprt != &ssl_sock) |
| 6872 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6873 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6874 | |
| 6875 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6876 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6877 | return 0; |
| 6878 | } |
| 6879 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6880 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6881 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6882 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6883 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6884 | if (!crt) |
| 6885 | goto out; |
| 6886 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6887 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6888 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6889 | goto out; |
| 6890 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6891 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6892 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6893 | ret = 1; |
| 6894 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6895 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6896 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6897 | X509_free(crt); |
| 6898 | return ret; |
| 6899 | } |
| 6900 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6901 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6902 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6903 | * should be use. |
| 6904 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6905 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6906 | smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6907 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6908 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6909 | X509 *crt = NULL; |
| 6910 | X509_NAME *name; |
| 6911 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6912 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6913 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6914 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6915 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6916 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6917 | if (!conn || conn->xprt != &ssl_sock) |
| 6918 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6919 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6920 | |
| 6921 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6922 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6923 | return 0; |
| 6924 | } |
| 6925 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6926 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6927 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6928 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6929 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6930 | if (!crt) |
| 6931 | goto out; |
| 6932 | |
| 6933 | name = X509_get_issuer_name(crt); |
| 6934 | if (!name) |
| 6935 | goto out; |
| 6936 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6937 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6938 | if (args && args[0].type == ARGT_STR) { |
| 6939 | int pos = 1; |
| 6940 | |
| 6941 | if (args[1].type == ARGT_SINT) |
| 6942 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6943 | |
| 6944 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6945 | goto out; |
| 6946 | } |
| 6947 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6948 | goto out; |
| 6949 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6950 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6951 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6952 | ret = 1; |
| 6953 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6954 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6955 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6956 | X509_free(crt); |
| 6957 | return ret; |
| 6958 | } |
| 6959 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6960 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6961 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6962 | * should be use. |
| 6963 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6964 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6965 | smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6966 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6967 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6968 | X509 *crt = NULL; |
| 6969 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6970 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6971 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6972 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6973 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6974 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6975 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6976 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6977 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6978 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6979 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6980 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6981 | return 0; |
| 6982 | } |
| 6983 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6984 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6985 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6986 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6987 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6988 | if (!crt) |
| 6989 | goto out; |
| 6990 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6991 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6992 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6993 | goto out; |
| 6994 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6995 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6996 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6997 | ret = 1; |
| 6998 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6999 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7000 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7001 | X509_free(crt); |
| 7002 | return ret; |
| 7003 | } |
| 7004 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7005 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 7006 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7007 | * should be use. |
| 7008 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7009 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7010 | smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7011 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7012 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7013 | X509 *crt = NULL; |
| 7014 | X509_NAME *name; |
| 7015 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7016 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7017 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7018 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7019 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7020 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7021 | if (!conn || conn->xprt != &ssl_sock) |
| 7022 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7023 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7024 | |
| 7025 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7026 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7027 | return 0; |
| 7028 | } |
| 7029 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7030 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7031 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7032 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7033 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7034 | if (!crt) |
| 7035 | goto out; |
| 7036 | |
| 7037 | name = X509_get_subject_name(crt); |
| 7038 | if (!name) |
| 7039 | goto out; |
| 7040 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7041 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7042 | if (args && args[0].type == ARGT_STR) { |
| 7043 | int pos = 1; |
| 7044 | |
| 7045 | if (args[1].type == ARGT_SINT) |
| 7046 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7047 | |
| 7048 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 7049 | goto out; |
| 7050 | } |
| 7051 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 7052 | goto out; |
| 7053 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7054 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7055 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7056 | ret = 1; |
| 7057 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7058 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7059 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7060 | X509_free(crt); |
| 7061 | return ret; |
| 7062 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7063 | |
| 7064 | /* integer, returns true if current session use a client certificate */ |
| 7065 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7066 | smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7067 | { |
| 7068 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7069 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7070 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7071 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7072 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7073 | if (!conn || conn->xprt != &ssl_sock) |
| 7074 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7075 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7076 | |
| 7077 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7078 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7079 | return 0; |
| 7080 | } |
| 7081 | |
| 7082 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7083 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7084 | if (crt) { |
| 7085 | X509_free(crt); |
| 7086 | } |
| 7087 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7088 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7089 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7090 | return 1; |
| 7091 | } |
| 7092 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7093 | /* integer, returns the certificate version |
| 7094 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7095 | * should be use. |
| 7096 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7097 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7098 | smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7099 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7100 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7101 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7102 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7103 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7104 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7105 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7106 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7107 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7108 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7109 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7110 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7111 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7112 | return 0; |
| 7113 | } |
| 7114 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7115 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7116 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7117 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7118 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7119 | if (!crt) |
| 7120 | return 0; |
| 7121 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7122 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7123 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7124 | if (cert_peer) |
| 7125 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7126 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7127 | |
| 7128 | return 1; |
| 7129 | } |
| 7130 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7131 | /* string, returns the certificate's signature algorithm. |
| 7132 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7133 | * should be use. |
| 7134 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7135 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7136 | smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7137 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7138 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7139 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7140 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7141 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7142 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7143 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7144 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7145 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7146 | if (!conn || conn->xprt != &ssl_sock) |
| 7147 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7148 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7149 | |
| 7150 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7151 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7152 | return 0; |
| 7153 | } |
| 7154 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7155 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7156 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7157 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7158 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7159 | if (!crt) |
| 7160 | return 0; |
| 7161 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7162 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7163 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7164 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7165 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7166 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7167 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7168 | if (cert_peer) |
| 7169 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7170 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7171 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7172 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7173 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7174 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7175 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7176 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7177 | if (cert_peer) |
| 7178 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7179 | |
| 7180 | return 1; |
| 7181 | } |
| 7182 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7183 | /* string, returns the certificate's key algorithm. |
| 7184 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7185 | * should be use. |
| 7186 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7187 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7188 | smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7189 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7190 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7191 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7192 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7193 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7194 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7195 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7196 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7197 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7198 | if (!conn || conn->xprt != &ssl_sock) |
| 7199 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7200 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7201 | |
| 7202 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7203 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7204 | return 0; |
| 7205 | } |
| 7206 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7207 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7208 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7209 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7210 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7211 | if (!crt) |
| 7212 | return 0; |
| 7213 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7214 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 7215 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7216 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7217 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7218 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7219 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7220 | if (cert_peer) |
| 7221 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7222 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7223 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7224 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7225 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7226 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7227 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7228 | if (cert_peer) |
| 7229 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7230 | |
| 7231 | return 1; |
| 7232 | } |
| 7233 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7234 | /* boolean, returns true if front conn. transport layer is SSL. |
| 7235 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7236 | * char is 'b'. |
| 7237 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7238 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7239 | smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7240 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7241 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7242 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7243 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7244 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7245 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7246 | return 1; |
| 7247 | } |
| 7248 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7249 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7250 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7251 | smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7252 | { |
| 7253 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7254 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7255 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7256 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7257 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7258 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7259 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7260 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7261 | return 1; |
| 7262 | #else |
| 7263 | return 0; |
| 7264 | #endif |
| 7265 | } |
| 7266 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7267 | /* boolean, returns true if client session has been resumed. |
| 7268 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7269 | * char is 'b'. |
| 7270 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7271 | static int |
| 7272 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7273 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7274 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7275 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7276 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7277 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7278 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7279 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7280 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7281 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7282 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7283 | return 1; |
| 7284 | } |
| 7285 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7286 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 7287 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7288 | * char is 'b'. |
| 7289 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7290 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7291 | smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7292 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7293 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7294 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7295 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7296 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7297 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7298 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7299 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7300 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7301 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7302 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7303 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7304 | return 0; |
| 7305 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7306 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7307 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7308 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7309 | |
| 7310 | return 1; |
| 7311 | } |
| 7312 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7313 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 7314 | * is SSL. |
| 7315 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7316 | * char is 'b'. |
| 7317 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7318 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7319 | smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7320 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7321 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7322 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7323 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7324 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7325 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7326 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7327 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7328 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7329 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7330 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7331 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7332 | return 0; |
| 7333 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7334 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7335 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7336 | |
| 7337 | return 1; |
| 7338 | } |
| 7339 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7340 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 7341 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7342 | * char is 'b'. |
| 7343 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7344 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7345 | smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7346 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7347 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7348 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7349 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7350 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7351 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7352 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7353 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7354 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7355 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7356 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7357 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7358 | return 0; |
| 7359 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7360 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7361 | |
| 7362 | return 1; |
| 7363 | } |
| 7364 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7365 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7366 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7367 | smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7368 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7369 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7370 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7371 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7372 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7373 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7374 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7375 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7376 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7377 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7378 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7379 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7380 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7381 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7382 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7383 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7384 | (const unsigned char **)&smp->data.u.str.area, |
| 7385 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7386 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7387 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7388 | return 0; |
| 7389 | |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7390 | smp->data.u.str.data = len; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7391 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7392 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7393 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7394 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7395 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7396 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7397 | smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7398 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7399 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7400 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7401 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7402 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7403 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7404 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7405 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7406 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7407 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7408 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7409 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7410 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7411 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7412 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7413 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7414 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7415 | (const unsigned char **)&smp->data.u.str.area, |
| 7416 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7417 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7418 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7419 | return 0; |
| 7420 | |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7421 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7422 | return 1; |
| 7423 | } |
| 7424 | #endif |
| 7425 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7426 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 7427 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7428 | * char is 'b'. |
| 7429 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7430 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7431 | smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7432 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7433 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7434 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7435 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7436 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7437 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7438 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7439 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7440 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7441 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7442 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7443 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7444 | return 0; |
| 7445 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7446 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7447 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7448 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7449 | |
| 7450 | return 1; |
| 7451 | } |
| 7452 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7453 | /* binary, returns the SSL stream id if front conn. transport layer is SSL. |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7454 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7455 | * char is 'b'. |
| 7456 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7457 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7458 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7459 | smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7460 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7461 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7462 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7463 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7464 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7465 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7466 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7467 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7468 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7469 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7470 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7471 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7472 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7473 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7474 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7475 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7476 | return 0; |
| 7477 | |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7478 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len); |
Dragan Dosen | 9dc47c4 | 2020-05-04 09:07:28 +0200 | [diff] [blame] | 7479 | if (!smp->data.u.str.area || !len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7480 | return 0; |
| 7481 | |
Willy Tarreau | 767e8ad | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7482 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7483 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7484 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7485 | #endif |
| 7486 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7487 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 7488 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7489 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 7490 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7491 | { |
| 7492 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7493 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7494 | struct buffer *data; |
| 7495 | struct ssl_sock_ctx *ctx; |
| 7496 | |
| 7497 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7498 | return 0; |
| 7499 | ctx = conn->xprt_ctx; |
| 7500 | |
| 7501 | data = get_trash_chunk(); |
| 7502 | if (kw[7] == 'c') |
| 7503 | data->data = SSL_get_client_random(ctx->ssl, |
| 7504 | (unsigned char *) data->area, |
| 7505 | data->size); |
| 7506 | else |
| 7507 | data->data = SSL_get_server_random(ctx->ssl, |
| 7508 | (unsigned char *) data->area, |
| 7509 | data->size); |
| 7510 | if (!data->data) |
| 7511 | return 0; |
| 7512 | |
| 7513 | smp->flags = 0; |
| 7514 | smp->data.type = SMP_T_BIN; |
| 7515 | smp->data.u.str = *data; |
| 7516 | |
| 7517 | return 1; |
| 7518 | } |
| 7519 | |
| 7520 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7521 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7522 | { |
| 7523 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7524 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7525 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7526 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7527 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7528 | |
| 7529 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7530 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7531 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7532 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7533 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7534 | if (!ssl_sess) |
| 7535 | return 0; |
| 7536 | |
| 7537 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7538 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 7539 | (unsigned char *) data->area, |
| 7540 | data->size); |
| 7541 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7542 | return 0; |
| 7543 | |
| 7544 | smp->flags = 0; |
| 7545 | smp->data.type = SMP_T_BIN; |
| 7546 | smp->data.u.str = *data; |
| 7547 | |
| 7548 | return 1; |
| 7549 | } |
| 7550 | #endif |
| 7551 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7552 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7553 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7554 | smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7555 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7556 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7557 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7558 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7559 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7560 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7561 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7562 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7563 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7564 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7565 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7566 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7567 | smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7568 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 7569 | return 0; |
| 7570 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7571 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7572 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7573 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7574 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7575 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7576 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7577 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7578 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7579 | struct connection *conn; |
| 7580 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7581 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7582 | |
| 7583 | conn = objt_conn(smp->sess->origin); |
| 7584 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7585 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7586 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7587 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7588 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7589 | if (!capture) |
| 7590 | return 0; |
| 7591 | |
| 7592 | smp->flags = SMP_F_CONST; |
| 7593 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7594 | smp->data.u.str.area = capture->ciphersuite; |
| 7595 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7596 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | static int |
| 7600 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7601 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7602 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7603 | |
| 7604 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7605 | return 0; |
| 7606 | |
| 7607 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7608 | dump_binary(data, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7609 | smp->data.type = SMP_T_BIN; |
| 7610 | smp->data.u.str = *data; |
| 7611 | return 1; |
| 7612 | } |
| 7613 | |
| 7614 | static int |
| 7615 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7616 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7617 | struct connection *conn; |
| 7618 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7619 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7620 | |
| 7621 | conn = objt_conn(smp->sess->origin); |
| 7622 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7623 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7624 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7625 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7626 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7627 | if (!capture) |
| 7628 | return 0; |
| 7629 | |
| 7630 | smp->data.type = SMP_T_SINT; |
| 7631 | smp->data.u.sint = capture->xxh64; |
| 7632 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7633 | } |
| 7634 | |
| 7635 | static int |
| 7636 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7637 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7638 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7639 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7640 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7641 | |
| 7642 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7643 | return 0; |
| 7644 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7645 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7646 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7647 | const char *str; |
| 7648 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7649 | const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i; |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7650 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7651 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7652 | cipher = SSL_get_cipher_by_value(id); |
| 7653 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 7654 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7655 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7656 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7657 | #endif |
| 7658 | str = SSL_CIPHER_get_name(cipher); |
| 7659 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7660 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7661 | else |
| 7662 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7663 | } |
| 7664 | smp->data.type = SMP_T_STR; |
| 7665 | smp->data.u.str = *data; |
| 7666 | return 1; |
| 7667 | #else |
| 7668 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7669 | #endif |
| 7670 | } |
| 7671 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7672 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7673 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7674 | smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7675 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7676 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7677 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7678 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7679 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7680 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7681 | |
| 7682 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7683 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7684 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7685 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7686 | |
| 7687 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7688 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7689 | return 0; |
| 7690 | } |
| 7691 | |
| 7692 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7693 | if (!SSL_session_reused(ctx->ssl)) |
| 7694 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7695 | finished_trash->area, |
| 7696 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7697 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7698 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7699 | finished_trash->area, |
| 7700 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7701 | |
| 7702 | if (!finished_len) |
| 7703 | return 0; |
| 7704 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7705 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7706 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7707 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7708 | |
| 7709 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7710 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7711 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7712 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7713 | /* integer, returns the first verify error in CA chain of client certificate chain. */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7714 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7715 | smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7716 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7717 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7718 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7719 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7720 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7721 | if (!conn || conn->xprt != &ssl_sock) |
| 7722 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7723 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7724 | |
| 7725 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7726 | smp->flags = SMP_F_MAY_CHANGE; |
| 7727 | return 0; |
| 7728 | } |
| 7729 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7730 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7731 | smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7732 | smp->flags = 0; |
| 7733 | |
| 7734 | return 1; |
| 7735 | } |
| 7736 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7737 | /* integer, returns the depth of the first verify error in CA chain of client certificate chain. */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7738 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7739 | smp_fetch_ssl_c_ca_err_depth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7740 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7741 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7742 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7743 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7744 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7745 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7746 | return 0; |
| 7747 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7748 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7749 | smp->flags = SMP_F_MAY_CHANGE; |
| 7750 | return 0; |
| 7751 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7752 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7753 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7754 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7755 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7756 | smp->flags = 0; |
| 7757 | |
| 7758 | return 1; |
| 7759 | } |
| 7760 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7761 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7762 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7763 | smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7764 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7765 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7766 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7767 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7768 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7769 | if (!conn || conn->xprt != &ssl_sock) |
| 7770 | return 0; |
| 7771 | |
| 7772 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7773 | smp->flags = SMP_F_MAY_CHANGE; |
| 7774 | return 0; |
| 7775 | } |
| 7776 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7777 | ctx = conn->xprt_ctx; |
| 7778 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7779 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7780 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7781 | smp->flags = 0; |
| 7782 | |
| 7783 | return 1; |
| 7784 | } |
| 7785 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7786 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7787 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7788 | smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7789 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7790 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7791 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7792 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7793 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7794 | if (!conn || conn->xprt != &ssl_sock) |
| 7795 | return 0; |
| 7796 | |
| 7797 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7798 | smp->flags = SMP_F_MAY_CHANGE; |
| 7799 | return 0; |
| 7800 | } |
| 7801 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7802 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7803 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7804 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7805 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7806 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7807 | smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7808 | smp->flags = 0; |
| 7809 | |
| 7810 | return 1; |
| 7811 | } |
| 7812 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7813 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7814 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7815 | { |
| 7816 | if (!*args[cur_arg + 1]) { |
| 7817 | if (err) |
| 7818 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7819 | return ERR_ALERT | ERR_FATAL; |
| 7820 | } |
| 7821 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7822 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7823 | memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7824 | else |
| 7825 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7826 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7827 | return 0; |
| 7828 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7829 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7830 | { |
| 7831 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7832 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7833 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7834 | /* parse the "ca-sign-file" bind keyword */ |
| 7835 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7836 | { |
| 7837 | if (!*args[cur_arg + 1]) { |
| 7838 | if (err) |
| 7839 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7840 | return ERR_ALERT | ERR_FATAL; |
| 7841 | } |
| 7842 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7843 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7844 | memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7845 | else |
| 7846 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7847 | |
| 7848 | return 0; |
| 7849 | } |
| 7850 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7851 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7852 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7853 | { |
| 7854 | if (!*args[cur_arg + 1]) { |
| 7855 | if (err) |
| 7856 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7857 | return ERR_ALERT | ERR_FATAL; |
| 7858 | } |
| 7859 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7860 | return 0; |
| 7861 | } |
| 7862 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7863 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7864 | static int ssl_bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7865 | { |
| 7866 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7867 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7868 | return ERR_ALERT | ERR_FATAL; |
| 7869 | } |
| 7870 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7871 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7872 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7873 | return 0; |
| 7874 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7875 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7876 | { |
| 7877 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7878 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7879 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 7880 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7881 | /* parse the "ciphersuites" bind keyword */ |
| 7882 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7883 | { |
| 7884 | if (!*args[cur_arg + 1]) { |
| 7885 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 7886 | return ERR_ALERT | ERR_FATAL; |
| 7887 | } |
| 7888 | |
| 7889 | free(conf->ciphersuites); |
| 7890 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 7891 | return 0; |
| 7892 | } |
| 7893 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7894 | { |
| 7895 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 7896 | } |
| 7897 | #endif |
| 7898 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7899 | /* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7900 | static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7901 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7902 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7903 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7904 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7905 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7906 | return ERR_ALERT | ERR_FATAL; |
| 7907 | } |
| 7908 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7909 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7910 | if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) { |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7911 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7912 | return ERR_ALERT | ERR_FATAL; |
| 7913 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7914 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]); |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7915 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7916 | } |
| 7917 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7918 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7919 | } |
| 7920 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7921 | /* parse the "crt-list" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7922 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7923 | { |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7924 | int err_code; |
| 7925 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7926 | if (!*args[cur_arg + 1]) { |
| 7927 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7928 | return ERR_ALERT | ERR_FATAL; |
| 7929 | } |
| 7930 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7931 | err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err); |
| 7932 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7933 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7934 | |
Willy Tarreau | b131c87 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 7935 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7936 | } |
| 7937 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7938 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7939 | static int ssl_bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7940 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7941 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7942 | if (err) |
| 7943 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7944 | return ERR_ALERT | ERR_FATAL; |
| 7945 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7946 | if (!*args[cur_arg + 1]) { |
| 7947 | if (err) |
| 7948 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7949 | return ERR_ALERT | ERR_FATAL; |
| 7950 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7951 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7952 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7953 | memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7954 | else |
| 7955 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7956 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7957 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7958 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7959 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7960 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7961 | { |
| 7962 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7963 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7964 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7965 | /* parse the "curves" bind keyword keyword */ |
| 7966 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7967 | { |
Lukas Tribus | d13e925 | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 7968 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7969 | if (!*args[cur_arg + 1]) { |
| 7970 | if (err) |
| 7971 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7972 | return ERR_ALERT | ERR_FATAL; |
| 7973 | } |
| 7974 | conf->curves = strdup(args[cur_arg + 1]); |
| 7975 | return 0; |
| 7976 | #else |
| 7977 | if (err) |
| 7978 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7979 | return ERR_ALERT | ERR_FATAL; |
| 7980 | #endif |
| 7981 | } |
| 7982 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7983 | { |
| 7984 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7985 | } |
| 7986 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7987 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7988 | static int ssl_bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7989 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7990 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7991 | if (err) |
| 7992 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7993 | return ERR_ALERT | ERR_FATAL; |
| 7994 | #elif defined(OPENSSL_NO_ECDH) |
| 7995 | if (err) |
| 7996 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7997 | return ERR_ALERT | ERR_FATAL; |
| 7998 | #else |
| 7999 | if (!*args[cur_arg + 1]) { |
| 8000 | if (err) |
| 8001 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 8002 | return ERR_ALERT | ERR_FATAL; |
| 8003 | } |
| 8004 | |
| 8005 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8006 | |
| 8007 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8008 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8009 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8010 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8011 | { |
| 8012 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 8013 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8014 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8015 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8016 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8017 | { |
| 8018 | int code; |
| 8019 | char *p = args[cur_arg + 1]; |
| 8020 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 8021 | |
| 8022 | if (!*p) { |
| 8023 | if (err) |
| 8024 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 8025 | return ERR_ALERT | ERR_FATAL; |
| 8026 | } |
| 8027 | |
| 8028 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 8029 | ignerr = &conf->ca_ignerr; |
| 8030 | |
| 8031 | if (strcmp(p, "all") == 0) { |
| 8032 | *ignerr = ~0ULL; |
| 8033 | return 0; |
| 8034 | } |
| 8035 | |
| 8036 | while (p) { |
| 8037 | code = atoi(p); |
| 8038 | if ((code <= 0) || (code > 63)) { |
| 8039 | if (err) |
| 8040 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 8041 | args[cur_arg], code, args[cur_arg + 1]); |
| 8042 | return ERR_ALERT | ERR_FATAL; |
| 8043 | } |
| 8044 | *ignerr |= 1ULL << code; |
| 8045 | p = strchr(p, ','); |
| 8046 | if (p) |
| 8047 | p++; |
| 8048 | } |
| 8049 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8050 | return 0; |
| 8051 | } |
| 8052 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8053 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 8054 | static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8055 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8056 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8057 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8058 | p = strchr(arg, '-'); |
| 8059 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8060 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8061 | p++; |
| 8062 | if (!strcmp(p, "sslv3")) |
| 8063 | v = CONF_SSLV3; |
| 8064 | else if (!strcmp(p, "tlsv10")) |
| 8065 | v = CONF_TLSV10; |
| 8066 | else if (!strcmp(p, "tlsv11")) |
| 8067 | v = CONF_TLSV11; |
| 8068 | else if (!strcmp(p, "tlsv12")) |
| 8069 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8070 | else if (!strcmp(p, "tlsv13")) |
| 8071 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8072 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8073 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8074 | if (!strncmp(arg, "no-", 3)) |
| 8075 | methods->flags |= methodVersions[v].flag; |
| 8076 | else if (!strncmp(arg, "force-", 6)) |
| 8077 | methods->min = methods->max = v; |
| 8078 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8079 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8080 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8081 | fail: |
| 8082 | if (err) |
| 8083 | memprintf(err, "'%s' : option not implemented", arg); |
| 8084 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8085 | } |
| 8086 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8087 | static int bind_parse_tls_method_options(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8088 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8089 | return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err); |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8090 | } |
| 8091 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8092 | static int srv_parse_tls_method_options(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8093 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8094 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 8095 | } |
| 8096 | |
| 8097 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 8098 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 8099 | { |
| 8100 | uint16_t i, v = 0; |
| 8101 | char *argv = args[cur_arg + 1]; |
| 8102 | if (!*argv) { |
| 8103 | if (err) |
| 8104 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 8105 | return ERR_ALERT | ERR_FATAL; |
| 8106 | } |
| 8107 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8108 | if (!strcmp(argv, methodVersions[i].name)) |
| 8109 | v = i; |
| 8110 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8111 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8112 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8113 | return ERR_ALERT | ERR_FATAL; |
| 8114 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8115 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 8116 | methods->min = v; |
| 8117 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 8118 | methods->max = v; |
| 8119 | else { |
| 8120 | if (err) |
| 8121 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 8122 | return ERR_ALERT | ERR_FATAL; |
| 8123 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8124 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8125 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8126 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8127 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8128 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8129 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8130 | ha_warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n"); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8131 | #endif |
| 8132 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 8133 | } |
| 8134 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8135 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8136 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8137 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8138 | } |
| 8139 | |
| 8140 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8141 | { |
| 8142 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 8143 | } |
| 8144 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8145 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8146 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8147 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 8148 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8149 | return 0; |
| 8150 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8151 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8152 | /* parse the "allow-0rtt" bind keyword */ |
| 8153 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8154 | { |
| 8155 | conf->early_data = 1; |
| 8156 | return 0; |
| 8157 | } |
| 8158 | |
| 8159 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8160 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 8161 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8162 | return 0; |
| 8163 | } |
| 8164 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8165 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8166 | static int ssl_bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8167 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8168 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8169 | char *p1, *p2; |
| 8170 | |
| 8171 | if (!*args[cur_arg + 1]) { |
| 8172 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 8173 | return ERR_ALERT | ERR_FATAL; |
| 8174 | } |
| 8175 | |
| 8176 | free(conf->npn_str); |
| 8177 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8178 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8179 | * so we reuse each comma to store the next <len> and need |
| 8180 | * one more for the end of the string. |
| 8181 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8182 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8183 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8184 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 8185 | |
| 8186 | /* replace commas with the name length */ |
| 8187 | p1 = conf->npn_str; |
| 8188 | p2 = p1 + 1; |
| 8189 | while (1) { |
| 8190 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 8191 | if (!p2) |
| 8192 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8193 | |
| 8194 | if (p2 - (p1 + 1) > 255) { |
| 8195 | *p2 = '\0'; |
| 8196 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8197 | return ERR_ALERT | ERR_FATAL; |
| 8198 | } |
| 8199 | |
| 8200 | *p1 = p2 - (p1 + 1); |
| 8201 | p1 = p2; |
| 8202 | |
| 8203 | if (!*p2) |
| 8204 | break; |
| 8205 | |
| 8206 | *(p2++) = '\0'; |
| 8207 | } |
| 8208 | return 0; |
| 8209 | #else |
| 8210 | if (err) |
| 8211 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 8212 | return ERR_ALERT | ERR_FATAL; |
| 8213 | #endif |
| 8214 | } |
| 8215 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8216 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8217 | { |
| 8218 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8219 | } |
| 8220 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8221 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8222 | static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8223 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8224 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8225 | char *p1, *p2; |
| 8226 | |
| 8227 | if (!*args[cur_arg + 1]) { |
| 8228 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 8229 | return ERR_ALERT | ERR_FATAL; |
| 8230 | } |
| 8231 | |
| 8232 | free(conf->alpn_str); |
| 8233 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8234 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8235 | * so we reuse each comma to store the next <len> and need |
| 8236 | * one more for the end of the string. |
| 8237 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8238 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8239 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8240 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 8241 | |
| 8242 | /* replace commas with the name length */ |
| 8243 | p1 = conf->alpn_str; |
| 8244 | p2 = p1 + 1; |
| 8245 | while (1) { |
| 8246 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 8247 | if (!p2) |
| 8248 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8249 | |
| 8250 | if (p2 - (p1 + 1) > 255) { |
| 8251 | *p2 = '\0'; |
| 8252 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8253 | return ERR_ALERT | ERR_FATAL; |
| 8254 | } |
| 8255 | |
| 8256 | *p1 = p2 - (p1 + 1); |
| 8257 | p1 = p2; |
| 8258 | |
| 8259 | if (!*p2) |
| 8260 | break; |
| 8261 | |
| 8262 | *(p2++) = '\0'; |
| 8263 | } |
| 8264 | return 0; |
| 8265 | #else |
| 8266 | if (err) |
| 8267 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 8268 | return ERR_ALERT | ERR_FATAL; |
| 8269 | #endif |
| 8270 | } |
| 8271 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8272 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8273 | { |
| 8274 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8275 | } |
| 8276 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8277 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8278 | static int bind_parse_ssl(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8279 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 8280 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8281 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8282 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8283 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 8284 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8285 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8286 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 8287 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 8288 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8289 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8290 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 8291 | if (!conf->ssl_conf.ssl_methods.min) |
| 8292 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 8293 | if (!conf->ssl_conf.ssl_methods.max) |
| 8294 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8295 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8296 | return 0; |
| 8297 | } |
| 8298 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8299 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 8300 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8301 | { |
| 8302 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 8303 | return 0; |
| 8304 | } |
| 8305 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8306 | /* parse the "generate-certificates" bind keyword */ |
| 8307 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8308 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8309 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8310 | conf->generate_certs = 1; |
| 8311 | #else |
| 8312 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 8313 | err && *err ? *err : ""); |
| 8314 | #endif |
| 8315 | return 0; |
| 8316 | } |
| 8317 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8318 | /* parse the "strict-sni" bind keyword */ |
| 8319 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8320 | { |
| 8321 | conf->strict_sni = 1; |
| 8322 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8323 | } |
| 8324 | |
| 8325 | /* parse the "tls-ticket-keys" bind keyword */ |
| 8326 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8327 | { |
| 8328 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8329 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8330 | int i = 0; |
| 8331 | char thisline[LINESIZE]; |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8332 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8333 | |
| 8334 | if (!*args[cur_arg + 1]) { |
| 8335 | if (err) |
| 8336 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8337 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8338 | } |
| 8339 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8340 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8341 | if (keys_ref) { |
| 8342 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8343 | conf->keys_ref = keys_ref; |
| 8344 | return 0; |
| 8345 | } |
| 8346 | |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8347 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8348 | if (!keys_ref) { |
| 8349 | if (err) |
| 8350 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8351 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8352 | } |
| 8353 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8354 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8355 | if (!keys_ref->tlskeys) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8356 | if (err) |
| 8357 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8358 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8359 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8360 | |
| 8361 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 8362 | if (err) |
| 8363 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8364 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8365 | } |
| 8366 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8367 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8368 | if (!keys_ref->filename) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8369 | if (err) |
| 8370 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8371 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8372 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8373 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8374 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8375 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 8376 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8377 | int dec_size; |
| 8378 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8379 | /* Strip newline characters from the end */ |
| 8380 | if(thisline[len - 1] == '\n') |
| 8381 | thisline[--len] = 0; |
| 8382 | |
| 8383 | if(thisline[len - 1] == '\r') |
| 8384 | thisline[--len] = 0; |
| 8385 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8386 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 8387 | if (dec_size < 0) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8388 | if (err) |
| 8389 | memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8390 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8391 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8392 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 8393 | keys_ref->key_size_bits = 128; |
| 8394 | } |
| 8395 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 8396 | keys_ref->key_size_bits = 256; |
| 8397 | } |
| 8398 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 8399 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 8400 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8401 | if (err) |
| 8402 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8403 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8404 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8405 | i++; |
| 8406 | } |
| 8407 | |
| 8408 | if (i < TLS_TICKETS_NO) { |
| 8409 | if (err) |
| 8410 | memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO); |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8411 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8412 | } |
| 8413 | |
| 8414 | fclose(f); |
| 8415 | |
| 8416 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 8417 | i -= 2; |
| 8418 | keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8419 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8420 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8421 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8422 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8423 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8424 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 8425 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8426 | return 0; |
Christopher Faulet | 2bbc80d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8427 | |
| 8428 | fail: |
| 8429 | if (f) |
| 8430 | fclose(f); |
| 8431 | if (keys_ref) { |
| 8432 | free(keys_ref->filename); |
| 8433 | free(keys_ref->tlskeys); |
| 8434 | free(keys_ref); |
| 8435 | } |
| 8436 | return ERR_ALERT | ERR_FATAL; |
| 8437 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8438 | #else |
| 8439 | if (err) |
| 8440 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 8441 | return ERR_ALERT | ERR_FATAL; |
| 8442 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8443 | } |
| 8444 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8445 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8446 | static int ssl_bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8447 | { |
| 8448 | if (!*args[cur_arg + 1]) { |
| 8449 | if (err) |
| 8450 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 8451 | return ERR_ALERT | ERR_FATAL; |
| 8452 | } |
| 8453 | |
| 8454 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8455 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8456 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8457 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8458 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8459 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8460 | else { |
| 8461 | if (err) |
| 8462 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 8463 | args[cur_arg], args[cur_arg + 1]); |
| 8464 | return ERR_ALERT | ERR_FATAL; |
| 8465 | } |
| 8466 | |
| 8467 | return 0; |
| 8468 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8469 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8470 | { |
| 8471 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 8472 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8473 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8474 | /* parse the "no-ca-names" bind keyword */ |
| 8475 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8476 | { |
| 8477 | conf->no_ca_names = 1; |
| 8478 | return 0; |
| 8479 | } |
| 8480 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8481 | { |
| 8482 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 8483 | } |
| 8484 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8485 | /************** "server" keywords ****************/ |
| 8486 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8487 | /* parse the "npn" bind keyword */ |
| 8488 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8489 | { |
| 8490 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 8491 | char *p1, *p2; |
| 8492 | |
| 8493 | if (!*args[*cur_arg + 1]) { |
| 8494 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 8495 | return ERR_ALERT | ERR_FATAL; |
| 8496 | } |
| 8497 | |
| 8498 | free(newsrv->ssl_ctx.npn_str); |
| 8499 | |
| 8500 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8501 | * so we reuse each comma to store the next <len> and need |
| 8502 | * one more for the end of the string. |
| 8503 | */ |
| 8504 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8505 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 8506 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 8507 | newsrv->ssl_ctx.npn_len); |
| 8508 | |
| 8509 | /* replace commas with the name length */ |
| 8510 | p1 = newsrv->ssl_ctx.npn_str; |
| 8511 | p2 = p1 + 1; |
| 8512 | while (1) { |
| 8513 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 8514 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 8515 | if (!p2) |
| 8516 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8517 | |
| 8518 | if (p2 - (p1 + 1) > 255) { |
| 8519 | *p2 = '\0'; |
| 8520 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8521 | return ERR_ALERT | ERR_FATAL; |
| 8522 | } |
| 8523 | |
| 8524 | *p1 = p2 - (p1 + 1); |
| 8525 | p1 = p2; |
| 8526 | |
| 8527 | if (!*p2) |
| 8528 | break; |
| 8529 | |
| 8530 | *(p2++) = '\0'; |
| 8531 | } |
| 8532 | return 0; |
| 8533 | #else |
| 8534 | if (err) |
| 8535 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
| 8536 | return ERR_ALERT | ERR_FATAL; |
| 8537 | #endif |
| 8538 | } |
| 8539 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8540 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8541 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8542 | { |
| 8543 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 8544 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8545 | char **alpn_str; |
| 8546 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8547 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8548 | if (*args[*cur_arg] == 'c') { |
| 8549 | alpn_str = &newsrv->check.alpn_str; |
| 8550 | alpn_len = &newsrv->check.alpn_len; |
| 8551 | } else { |
| 8552 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 8553 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 8554 | |
| 8555 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8556 | if (!*args[*cur_arg + 1]) { |
| 8557 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 8558 | return ERR_ALERT | ERR_FATAL; |
| 8559 | } |
| 8560 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8561 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8562 | |
| 8563 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8564 | * so we reuse each comma to store the next <len> and need |
| 8565 | * one more for the end of the string. |
| 8566 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8567 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8568 | *alpn_str = calloc(1, *alpn_len + 1); |
| 8569 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8570 | |
| 8571 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8572 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8573 | p2 = p1 + 1; |
| 8574 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8575 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8576 | if (!p2) |
| 8577 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8578 | |
| 8579 | if (p2 - (p1 + 1) > 255) { |
| 8580 | *p2 = '\0'; |
| 8581 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8582 | return ERR_ALERT | ERR_FATAL; |
| 8583 | } |
| 8584 | |
| 8585 | *p1 = p2 - (p1 + 1); |
| 8586 | p1 = p2; |
| 8587 | |
| 8588 | if (!*p2) |
| 8589 | break; |
| 8590 | |
| 8591 | *(p2++) = '\0'; |
| 8592 | } |
| 8593 | return 0; |
| 8594 | #else |
| 8595 | if (err) |
| 8596 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
| 8597 | return ERR_ALERT | ERR_FATAL; |
| 8598 | #endif |
| 8599 | } |
| 8600 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8601 | /* parse the "ca-file" server keyword */ |
| 8602 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8603 | { |
| 8604 | if (!*args[*cur_arg + 1]) { |
| 8605 | if (err) |
| 8606 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 8607 | return ERR_ALERT | ERR_FATAL; |
| 8608 | } |
| 8609 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8610 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8611 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8612 | else |
| 8613 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 8614 | |
| 8615 | return 0; |
| 8616 | } |
| 8617 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 8618 | /* parse the "check-sni" server keyword */ |
| 8619 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8620 | { |
| 8621 | if (!*args[*cur_arg + 1]) { |
| 8622 | if (err) |
| 8623 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 8624 | return ERR_ALERT | ERR_FATAL; |
| 8625 | } |
| 8626 | |
| 8627 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 8628 | if (!newsrv->check.sni) { |
| 8629 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 8630 | return ERR_ALERT | ERR_FATAL; |
| 8631 | } |
| 8632 | return 0; |
| 8633 | |
| 8634 | } |
| 8635 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8636 | /* parse the "check-ssl" server keyword */ |
| 8637 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8638 | { |
| 8639 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8640 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8641 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8642 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8643 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8644 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8645 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8646 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8647 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 8648 | if (!newsrv->ssl_ctx.methods.min) |
| 8649 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 8650 | if (!newsrv->ssl_ctx.methods.max) |
| 8651 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 8652 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8653 | return 0; |
| 8654 | } |
| 8655 | |
| 8656 | /* parse the "ciphers" server keyword */ |
| 8657 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8658 | { |
| 8659 | if (!*args[*cur_arg + 1]) { |
| 8660 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8661 | return ERR_ALERT | ERR_FATAL; |
| 8662 | } |
| 8663 | |
| 8664 | free(newsrv->ssl_ctx.ciphers); |
| 8665 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 8666 | return 0; |
| 8667 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8668 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8669 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8670 | /* parse the "ciphersuites" server keyword */ |
| 8671 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8672 | { |
| 8673 | if (!*args[*cur_arg + 1]) { |
| 8674 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8675 | return ERR_ALERT | ERR_FATAL; |
| 8676 | } |
| 8677 | |
| 8678 | free(newsrv->ssl_ctx.ciphersuites); |
| 8679 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 8680 | return 0; |
| 8681 | } |
| 8682 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8683 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8684 | /* parse the "crl-file" server keyword */ |
| 8685 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8686 | { |
| 8687 | #ifndef X509_V_FLAG_CRL_CHECK |
| 8688 | if (err) |
| 8689 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 8690 | return ERR_ALERT | ERR_FATAL; |
| 8691 | #else |
| 8692 | if (!*args[*cur_arg + 1]) { |
| 8693 | if (err) |
| 8694 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 8695 | return ERR_ALERT | ERR_FATAL; |
| 8696 | } |
| 8697 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8698 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8699 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8700 | else |
| 8701 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 8702 | |
| 8703 | return 0; |
| 8704 | #endif |
| 8705 | } |
| 8706 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 8707 | /* parse the "crt" server keyword */ |
| 8708 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8709 | { |
| 8710 | if (!*args[*cur_arg + 1]) { |
| 8711 | if (err) |
| 8712 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 8713 | return ERR_ALERT | ERR_FATAL; |
| 8714 | } |
| 8715 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8716 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 8717 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 8718 | else |
| 8719 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 8720 | |
| 8721 | return 0; |
| 8722 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8723 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 8724 | /* parse the "no-check-ssl" server keyword */ |
| 8725 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8726 | { |
Christopher Faulet | 68d35ae | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 8727 | newsrv->check.use_ssl = -1; |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 8728 | free(newsrv->ssl_ctx.ciphers); |
| 8729 | newsrv->ssl_ctx.ciphers = NULL; |
| 8730 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 8731 | return 0; |
| 8732 | } |
| 8733 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 8734 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 8735 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8736 | { |
| 8737 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8738 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8739 | return 0; |
| 8740 | } |
| 8741 | |
| 8742 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 8743 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8744 | { |
| 8745 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8746 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8747 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 8748 | return 0; |
| 8749 | } |
| 8750 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 8751 | /* parse the "no-ssl" server keyword */ |
| 8752 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8753 | { |
Christopher Faulet | 68d35ae | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 8754 | newsrv->use_ssl = -1; |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 8755 | free(newsrv->ssl_ctx.ciphers); |
| 8756 | newsrv->ssl_ctx.ciphers = NULL; |
| 8757 | return 0; |
| 8758 | } |
| 8759 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8760 | /* parse the "allow-0rtt" server keyword */ |
| 8761 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8762 | { |
| 8763 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 8764 | return 0; |
| 8765 | } |
| 8766 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 8767 | /* parse the "no-ssl-reuse" server keyword */ |
| 8768 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8769 | { |
| 8770 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 8771 | return 0; |
| 8772 | } |
| 8773 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8774 | /* parse the "no-tls-tickets" server keyword */ |
| 8775 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8776 | { |
| 8777 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 8778 | return 0; |
| 8779 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 8780 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 8781 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8782 | { |
| 8783 | newsrv->pp_opts |= SRV_PP_V2; |
| 8784 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8785 | return 0; |
| 8786 | } |
| 8787 | |
| 8788 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 8789 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8790 | { |
| 8791 | newsrv->pp_opts |= SRV_PP_V2; |
| 8792 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8793 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 8794 | return 0; |
| 8795 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8796 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8797 | /* parse the "sni" server keyword */ |
| 8798 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8799 | { |
| 8800 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8801 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 8802 | return ERR_ALERT | ERR_FATAL; |
| 8803 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8804 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8805 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8806 | arg = args[*cur_arg + 1]; |
| 8807 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8808 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 8809 | return ERR_ALERT | ERR_FATAL; |
| 8810 | } |
| 8811 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8812 | free(newsrv->sni_expr); |
| 8813 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8814 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8815 | return 0; |
| 8816 | #endif |
| 8817 | } |
| 8818 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8819 | /* parse the "ssl" server keyword */ |
| 8820 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8821 | { |
| 8822 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8823 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8824 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8825 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8826 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8827 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8828 | #endif |
Jerome Magnin | 770bc8a | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 8829 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
| 8830 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 8831 | |
| 8832 | if (!newsrv->ssl_ctx.methods.min) |
| 8833 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 8834 | |
| 8835 | if (!newsrv->ssl_ctx.methods.max) |
| 8836 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 8837 | |
| 8838 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8839 | return 0; |
| 8840 | } |
| 8841 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8842 | /* parse the "ssl-reuse" server keyword */ |
| 8843 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8844 | { |
| 8845 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 8846 | return 0; |
| 8847 | } |
| 8848 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8849 | /* parse the "tls-tickets" server keyword */ |
| 8850 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8851 | { |
| 8852 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8853 | return 0; |
| 8854 | } |
| 8855 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8856 | /* parse the "verify" server keyword */ |
| 8857 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8858 | { |
| 8859 | if (!*args[*cur_arg + 1]) { |
| 8860 | if (err) |
| 8861 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8862 | return ERR_ALERT | ERR_FATAL; |
| 8863 | } |
| 8864 | |
| 8865 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8866 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8867 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8868 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8869 | else { |
| 8870 | if (err) |
| 8871 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8872 | args[*cur_arg], args[*cur_arg + 1]); |
| 8873 | return ERR_ALERT | ERR_FATAL; |
| 8874 | } |
| 8875 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8876 | return 0; |
| 8877 | } |
| 8878 | |
| 8879 | /* parse the "verifyhost" server keyword */ |
| 8880 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8881 | { |
| 8882 | if (!*args[*cur_arg + 1]) { |
| 8883 | if (err) |
| 8884 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8885 | return ERR_ALERT | ERR_FATAL; |
| 8886 | } |
| 8887 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8888 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8889 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8890 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8891 | return 0; |
| 8892 | } |
| 8893 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8894 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8895 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8896 | struct proxy *defpx, const char *file, int line, |
| 8897 | char **err) { |
| 8898 | int i = 1; |
| 8899 | |
| 8900 | if (*(args[i]) == 0) { |
| 8901 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8902 | return -1; |
| 8903 | } |
| 8904 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8905 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8906 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8907 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8908 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8909 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8910 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8911 | i++; |
| 8912 | else { |
| 8913 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8914 | return -1; |
| 8915 | } |
| 8916 | } |
| 8917 | else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) { |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8918 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8919 | return -1; |
| 8920 | } |
| 8921 | i++; |
| 8922 | } |
| 8923 | return 0; |
| 8924 | } |
| 8925 | |
| 8926 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8927 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8928 | struct proxy *defpx, const char *file, int line, |
| 8929 | char **err) { |
| 8930 | int i = 1; |
| 8931 | |
| 8932 | if (*(args[i]) == 0) { |
| 8933 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8934 | return -1; |
| 8935 | } |
| 8936 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8937 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8938 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8939 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8940 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8941 | i++; |
| 8942 | else { |
| 8943 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8944 | return -1; |
| 8945 | } |
| 8946 | } |
| 8947 | else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) { |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8948 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8949 | return -1; |
| 8950 | } |
| 8951 | i++; |
| 8952 | } |
| 8953 | return 0; |
| 8954 | } |
| 8955 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8956 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8957 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8958 | */ |
| 8959 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8960 | struct proxy *defpx, const char *file, int line, |
| 8961 | char **err) |
| 8962 | { |
| 8963 | char **target; |
| 8964 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8965 | target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8966 | |
| 8967 | if (too_many_args(1, args, err, NULL)) |
| 8968 | return -1; |
| 8969 | |
| 8970 | if (*target) { |
| 8971 | memprintf(err, "'%s' already specified.", args[0]); |
| 8972 | return -1; |
| 8973 | } |
| 8974 | |
| 8975 | if (*(args[1]) == 0) { |
| 8976 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8977 | return -1; |
| 8978 | } |
| 8979 | *target = strdup(args[1]); |
| 8980 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8981 | } |
| 8982 | |
| 8983 | /* parse the "ssl-mode-async" keyword in global section. |
| 8984 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8985 | */ |
| 8986 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8987 | struct proxy *defpx, const char *file, int line, |
| 8988 | char **err) |
| 8989 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8990 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8991 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8992 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8993 | return 0; |
| 8994 | #else |
| 8995 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8996 | return -1; |
| 8997 | #endif |
| 8998 | } |
| 8999 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9000 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9001 | static int ssl_check_async_engine_count(void) { |
| 9002 | int err_code = 0; |
| 9003 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 9004 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9005 | ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9006 | err_code = ERR_ABORT; |
| 9007 | } |
| 9008 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9009 | } |
| 9010 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9011 | /* parse the "ssl-engine" keyword in global section. |
| 9012 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9013 | */ |
| 9014 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 9015 | struct proxy *defpx, const char *file, int line, |
| 9016 | char **err) |
| 9017 | { |
| 9018 | char *algo; |
| 9019 | int ret = -1; |
| 9020 | |
| 9021 | if (*(args[1]) == 0) { |
| 9022 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 9023 | return ret; |
| 9024 | } |
| 9025 | |
| 9026 | if (*(args[2]) == 0) { |
| 9027 | /* if no list of algorithms is given, it defaults to ALL */ |
| 9028 | algo = strdup("ALL"); |
| 9029 | goto add_engine; |
| 9030 | } |
| 9031 | |
| 9032 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 9033 | if (strcmp(args[2], "algo") != 0) { |
| 9034 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 9035 | return ret; |
| 9036 | } |
| 9037 | |
| 9038 | if (*(args[3]) == 0) { |
| 9039 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 9040 | return ret; |
| 9041 | } |
| 9042 | algo = strdup(args[3]); |
| 9043 | |
| 9044 | add_engine: |
| 9045 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 9046 | openssl_engines_initialized++; |
| 9047 | ret = 0; |
| 9048 | } |
| 9049 | free(algo); |
| 9050 | return ret; |
| 9051 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9052 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9053 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9054 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 9055 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 9056 | */ |
| 9057 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 9058 | struct proxy *defpx, const char *file, int line, |
| 9059 | char **err) |
| 9060 | { |
| 9061 | char **target; |
| 9062 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9063 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers; |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9064 | |
| 9065 | if (too_many_args(1, args, err, NULL)) |
| 9066 | return -1; |
| 9067 | |
| 9068 | if (*(args[1]) == 0) { |
| 9069 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 9070 | return -1; |
| 9071 | } |
| 9072 | |
| 9073 | free(*target); |
| 9074 | *target = strdup(args[1]); |
| 9075 | return 0; |
| 9076 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9077 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9078 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9079 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 9080 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 9081 | */ |
| 9082 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 9083 | struct proxy *defpx, const char *file, int line, |
| 9084 | char **err) |
| 9085 | { |
| 9086 | char **target; |
| 9087 | |
| 9088 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 9089 | |
| 9090 | if (too_many_args(1, args, err, NULL)) |
| 9091 | return -1; |
| 9092 | |
| 9093 | if (*(args[1]) == 0) { |
| 9094 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 9095 | return -1; |
| 9096 | } |
| 9097 | |
| 9098 | free(*target); |
| 9099 | *target = strdup(args[1]); |
| 9100 | return 0; |
| 9101 | } |
| 9102 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9103 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9104 | /* parse various global tune.ssl settings consisting in positive integers. |
| 9105 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9106 | */ |
| 9107 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 9108 | struct proxy *defpx, const char *file, int line, |
| 9109 | char **err) |
| 9110 | { |
| 9111 | int *target; |
| 9112 | |
| 9113 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 9114 | target = &global.tune.sslcachesize; |
| 9115 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9116 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9117 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9118 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9119 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 9120 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9121 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 9122 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9123 | else { |
| 9124 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 9125 | return -1; |
| 9126 | } |
| 9127 | |
| 9128 | if (too_many_args(1, args, err, NULL)) |
| 9129 | return -1; |
| 9130 | |
| 9131 | if (*(args[1]) == 0) { |
| 9132 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9133 | return -1; |
| 9134 | } |
| 9135 | |
| 9136 | *target = atoi(args[1]); |
| 9137 | if (*target < 0) { |
| 9138 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 9139 | return -1; |
| 9140 | } |
| 9141 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9142 | } |
| 9143 | |
| 9144 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 9145 | struct proxy *defpx, const char *file, int line, |
| 9146 | char **err) |
| 9147 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9148 | int ret; |
| 9149 | |
| 9150 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 9151 | if (ret != 0) |
| 9152 | return ret; |
| 9153 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9154 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9155 | memprintf(err, "'%s' is already configured.", args[0]); |
| 9156 | return -1; |
| 9157 | } |
| 9158 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9159 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 9160 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9161 | memprintf(err, "Out of memory error."); |
| 9162 | return -1; |
| 9163 | } |
| 9164 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9165 | } |
| 9166 | |
| 9167 | /* parse "ssl.force-private-cache". |
| 9168 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9169 | */ |
| 9170 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 9171 | struct proxy *defpx, const char *file, int line, |
| 9172 | char **err) |
| 9173 | { |
| 9174 | if (too_many_args(0, args, err, NULL)) |
| 9175 | return -1; |
| 9176 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9177 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9178 | return 0; |
| 9179 | } |
| 9180 | |
| 9181 | /* parse "ssl.lifetime". |
| 9182 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9183 | */ |
| 9184 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 9185 | struct proxy *defpx, const char *file, int line, |
| 9186 | char **err) |
| 9187 | { |
| 9188 | const char *res; |
| 9189 | |
| 9190 | if (too_many_args(1, args, err, NULL)) |
| 9191 | return -1; |
| 9192 | |
| 9193 | if (*(args[1]) == 0) { |
| 9194 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 9195 | return -1; |
| 9196 | } |
| 9197 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9198 | res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 9199 | if (res == PARSE_TIME_OVER) { |
| 9200 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 9201 | args[1], args[0]); |
| 9202 | return -1; |
| 9203 | } |
| 9204 | else if (res == PARSE_TIME_UNDER) { |
| 9205 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 9206 | args[1], args[0]); |
| 9207 | return -1; |
| 9208 | } |
| 9209 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9210 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 9211 | return -1; |
| 9212 | } |
| 9213 | return 0; |
| 9214 | } |
| 9215 | |
| 9216 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9217 | /* parse "ssl-dh-param-file". |
| 9218 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9219 | */ |
| 9220 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 9221 | struct proxy *defpx, const char *file, int line, |
| 9222 | char **err) |
| 9223 | { |
| 9224 | if (too_many_args(1, args, err, NULL)) |
| 9225 | return -1; |
| 9226 | |
| 9227 | if (*(args[1]) == 0) { |
| 9228 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 9229 | return -1; |
| 9230 | } |
| 9231 | |
| 9232 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 9233 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 9234 | return -1; |
| 9235 | } |
| 9236 | return 0; |
| 9237 | } |
| 9238 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9239 | /* parse "ssl.default-dh-param". |
| 9240 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9241 | */ |
| 9242 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 9243 | struct proxy *defpx, const char *file, int line, |
| 9244 | char **err) |
| 9245 | { |
| 9246 | if (too_many_args(1, args, err, NULL)) |
| 9247 | return -1; |
| 9248 | |
| 9249 | if (*(args[1]) == 0) { |
| 9250 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9251 | return -1; |
| 9252 | } |
| 9253 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9254 | global_ssl.default_dh_param = atoi(args[1]); |
| 9255 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9256 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 9257 | return -1; |
| 9258 | } |
| 9259 | return 0; |
| 9260 | } |
| 9261 | #endif |
| 9262 | |
| 9263 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9264 | /* This function is used with TLS ticket keys management. It permits to browse |
| 9265 | * each reference. The variable <getnext> must contain the current node, |
| 9266 | * <end> point to the root node. |
| 9267 | */ |
| 9268 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9269 | static inline |
| 9270 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 9271 | { |
| 9272 | struct tls_keys_ref *ref = getnext; |
| 9273 | |
| 9274 | while (1) { |
| 9275 | |
| 9276 | /* Get next list entry. */ |
| 9277 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 9278 | |
| 9279 | /* If the entry is the last of the list, return NULL. */ |
| 9280 | if (&ref->list == end) |
| 9281 | return NULL; |
| 9282 | |
| 9283 | return ref; |
| 9284 | } |
| 9285 | } |
| 9286 | |
| 9287 | static inline |
| 9288 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 9289 | { |
| 9290 | int id; |
| 9291 | char *error; |
| 9292 | |
| 9293 | /* If the reference starts by a '#', this is numeric id. */ |
| 9294 | if (reference[0] == '#') { |
| 9295 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 9296 | id = strtol(reference + 1, &error, 10); |
| 9297 | if (*error != '\0') |
| 9298 | return NULL; |
| 9299 | |
| 9300 | /* Perform the unique id lookup. */ |
| 9301 | return tlskeys_ref_lookupid(id); |
| 9302 | } |
| 9303 | |
| 9304 | /* Perform the string lookup. */ |
| 9305 | return tlskeys_ref_lookup(reference); |
| 9306 | } |
| 9307 | #endif |
| 9308 | |
| 9309 | |
| 9310 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9311 | |
| 9312 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 9313 | |
| 9314 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 9315 | return cli_io_handler_tlskeys_files(appctx); |
| 9316 | } |
| 9317 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9318 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 9319 | * (next index to be dumped), and cli.p0 (next key reference). |
| 9320 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9321 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 9322 | |
| 9323 | struct stream_interface *si = appctx->owner; |
| 9324 | |
| 9325 | switch (appctx->st2) { |
| 9326 | case STAT_ST_INIT: |
| 9327 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 9328 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9329 | * later and restart at the state "STAT_ST_INIT". |
| 9330 | */ |
| 9331 | chunk_reset(&trash); |
| 9332 | |
| 9333 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 9334 | chunk_appendf(&trash, "# id secret\n"); |
| 9335 | else |
| 9336 | chunk_appendf(&trash, "# id (file)\n"); |
| 9337 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9338 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9339 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9340 | return 0; |
| 9341 | } |
| 9342 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9343 | /* Now, we start the browsing of the references lists. |
| 9344 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 9345 | * available field of this pointer is <list>. It is used with the function |
| 9346 | * tlskeys_list_get_next() for retruning the first available entry |
| 9347 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9348 | if (appctx->ctx.cli.p0 == NULL) { |
| 9349 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 9350 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9351 | } |
| 9352 | |
| 9353 | appctx->st2 = STAT_ST_LIST; |
| 9354 | /* fall through */ |
| 9355 | |
| 9356 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9357 | while (appctx->ctx.cli.p0) { |
| 9358 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9359 | |
| 9360 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9361 | if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9362 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9363 | |
| 9364 | if (appctx->ctx.cli.i1 == 0) |
| 9365 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 9366 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9367 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9368 | int head; |
| 9369 | |
| 9370 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 9371 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9372 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9373 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9374 | |
| 9375 | chunk_reset(t2); |
| 9376 | /* should never fail here because we dump only a key in the t2 buffer */ |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9377 | if (ref->key_size_bits == 128) { |
| 9378 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9379 | sizeof(struct tls_sess_key_128), |
| 9380 | t2->area, t2->size); |
| 9381 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9382 | t2->area); |
| 9383 | } |
| 9384 | else if (ref->key_size_bits == 256) { |
| 9385 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9386 | sizeof(struct tls_sess_key_256), |
| 9387 | t2->area, t2->size); |
| 9388 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9389 | t2->area); |
| 9390 | } |
| 9391 | else { |
| 9392 | /* This case should never happen */ |
| 9393 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 9394 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9395 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9396 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9397 | /* let's try again later from this stream. We add ourselves into |
| 9398 | * this stream's users so that it can remove us upon termination. |
| 9399 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9400 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9401 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9402 | return 0; |
| 9403 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9404 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9405 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9406 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9407 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9408 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9409 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9410 | /* let's try again later from this stream. We add ourselves into |
| 9411 | * this stream's users so that it can remove us upon termination. |
| 9412 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9413 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9414 | return 0; |
| 9415 | } |
| 9416 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9417 | if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9418 | break; |
| 9419 | |
| 9420 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9421 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9422 | } |
| 9423 | |
| 9424 | appctx->st2 = STAT_ST_FIN; |
| 9425 | /* fall through */ |
| 9426 | |
| 9427 | default: |
| 9428 | appctx->st2 = STAT_ST_FIN; |
| 9429 | return 1; |
| 9430 | } |
| 9431 | return 0; |
| 9432 | } |
| 9433 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9434 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9435 | static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9436 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9437 | /* no parameter, shows only file list */ |
| 9438 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9439 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9440 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9441 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9442 | } |
| 9443 | |
| 9444 | if (args[2][0] == '*') { |
| 9445 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9446 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9447 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9448 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 9449 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9450 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9451 | appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9452 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9453 | return 1; |
| 9454 | } |
| 9455 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9456 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9457 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9458 | } |
| 9459 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9460 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9461 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9462 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9463 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9464 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9465 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 9466 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9467 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9468 | appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9469 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9470 | return 1; |
| 9471 | } |
| 9472 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9473 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 9474 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9475 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9476 | appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9477 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9478 | return 1; |
| 9479 | } |
| 9480 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9481 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9482 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9483 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9484 | appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9485 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9486 | return 1; |
| 9487 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9488 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9489 | trash.data = ret; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9490 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) { |
| 9491 | appctx->ctx.cli.severity = LOG_ERR; |
| 9492 | appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n"; |
| 9493 | appctx->st0 = CLI_ST_PRINT; |
| 9494 | return 1; |
| 9495 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9496 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9497 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9498 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9499 | return 1; |
| 9500 | |
| 9501 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9502 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9503 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9504 | static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9505 | { |
| 9506 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 9507 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9508 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9509 | |
| 9510 | if (!payload) |
| 9511 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9512 | |
| 9513 | /* Expect one parameter: the new response in base64 encoding */ |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9514 | if (!*payload) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9515 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9516 | appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9517 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9518 | return 1; |
| 9519 | } |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9520 | |
| 9521 | /* remove \r and \n from the payload */ |
| 9522 | for (i = 0, j = 0; payload[i]; i++) { |
| 9523 | if (payload[i] == '\r' || payload[i] == '\n') |
| 9524 | continue; |
| 9525 | payload[j++] = payload[i]; |
| 9526 | } |
| 9527 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9528 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9529 | ret = base64dec(payload, j, trash.area, trash.size); |
| 9530 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9531 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9532 | appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9533 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9534 | return 1; |
| 9535 | } |
| 9536 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9537 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9538 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 9539 | if (err) { |
| 9540 | memprintf(&err, "%s.\n", err); |
| 9541 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9542 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9543 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 9544 | else { |
| 9545 | appctx->ctx.cli.severity = LOG_ERR; |
| 9546 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 9547 | appctx->st0 = CLI_ST_PRINT; |
| 9548 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9549 | return 1; |
| 9550 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9551 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9552 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9553 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9554 | return 1; |
| 9555 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9556 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9557 | appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9558 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9559 | return 1; |
| 9560 | #endif |
| 9561 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9562 | } |
| 9563 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9564 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9565 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 9566 | { |
| 9567 | switch (arg->type) { |
| 9568 | case ARGT_STR: |
| 9569 | smp->data.type = SMP_T_STR; |
| 9570 | smp->data.u.str = arg->data.str; |
| 9571 | return 1; |
| 9572 | case ARGT_VAR: |
| 9573 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 9574 | return 0; |
| 9575 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 9576 | return 0; |
| 9577 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 9578 | return 0; |
| 9579 | return 1; |
| 9580 | default: |
| 9581 | return 0; |
| 9582 | } |
| 9583 | } |
| 9584 | |
| 9585 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 9586 | const char *file, int line, char **err) |
| 9587 | { |
| 9588 | switch(args[0].data.sint) { |
| 9589 | case 128: |
| 9590 | case 192: |
| 9591 | case 256: |
| 9592 | break; |
| 9593 | default: |
| 9594 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 9595 | return 0; |
| 9596 | } |
| 9597 | /* Try to decode a variable. */ |
| 9598 | vars_check_arg(&args[1], NULL); |
| 9599 | vars_check_arg(&args[2], NULL); |
| 9600 | vars_check_arg(&args[3], NULL); |
| 9601 | return 1; |
| 9602 | } |
| 9603 | |
| 9604 | /* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 9605 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 9606 | { |
| 9607 | struct sample nonce, key, aead_tag; |
| 9608 | struct buffer *smp_trash, *smp_trash_alloc; |
| 9609 | EVP_CIPHER_CTX *ctx; |
| 9610 | int dec_size, ret; |
| 9611 | |
| 9612 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 9613 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 9614 | return 0; |
| 9615 | |
| 9616 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 9617 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 9618 | return 0; |
| 9619 | |
| 9620 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 9621 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 9622 | return 0; |
| 9623 | |
| 9624 | smp_trash = get_trash_chunk(); |
| 9625 | smp_trash_alloc = alloc_trash_chunk(); |
| 9626 | if (!smp_trash_alloc) |
| 9627 | return 0; |
| 9628 | |
| 9629 | ctx = EVP_CIPHER_CTX_new(); |
| 9630 | |
| 9631 | if (!ctx) |
| 9632 | goto err; |
| 9633 | |
| 9634 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9635 | if (dec_size < 0) |
| 9636 | goto err; |
| 9637 | smp_trash->data = dec_size; |
| 9638 | |
| 9639 | /* Set cipher type and mode */ |
| 9640 | switch(arg_p[0].data.sint) { |
| 9641 | case 128: |
| 9642 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 9643 | break; |
| 9644 | case 192: |
| 9645 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 9646 | break; |
| 9647 | case 256: |
| 9648 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 9649 | break; |
| 9650 | } |
| 9651 | |
| 9652 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 9653 | |
| 9654 | /* Initialise IV */ |
| 9655 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 9656 | goto err; |
| 9657 | |
| 9658 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9659 | if (dec_size < 0) |
| 9660 | goto err; |
| 9661 | smp_trash->data = dec_size; |
| 9662 | |
| 9663 | /* Initialise key */ |
| 9664 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 9665 | goto err; |
| 9666 | |
| 9667 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 9668 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 9669 | goto err; |
| 9670 | |
| 9671 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 9672 | if (dec_size < 0) |
| 9673 | goto err; |
| 9674 | smp_trash_alloc->data = dec_size; |
| 9675 | dec_size = smp_trash->data; |
| 9676 | |
| 9677 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 9678 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 9679 | |
| 9680 | if (ret <= 0) |
| 9681 | goto err; |
| 9682 | |
| 9683 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 9684 | smp->data.u.str.area = smp_trash->area; |
| 9685 | smp->data.type = SMP_T_BIN; |
| 9686 | smp->flags &= ~SMP_F_CONST; |
| 9687 | free_trash_chunk(smp_trash_alloc); |
| 9688 | return 1; |
| 9689 | |
| 9690 | err: |
| 9691 | free_trash_chunk(smp_trash_alloc); |
| 9692 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9693 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9694 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9695 | |
| 9696 | /* register cli keywords */ |
| 9697 | static struct cli_kw_list cli_kws = {{ },{ |
| 9698 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9699 | { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL }, |
Lukas Tribus | f4bbc43 | 2017-10-24 12:26:31 +0200 | [diff] [blame] | 9700 | { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9701 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9702 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9703 | { { NULL }, NULL, NULL, NULL } |
| 9704 | }}; |
| 9705 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9706 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9707 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9708 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9709 | * Please take care of keeping this list alphabetically sorted. |
| 9710 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9711 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9712 | { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9713 | { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 9714 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 9715 | { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 9716 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9717 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 9718 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9719 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 9720 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 9721 | { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9722 | { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 9723 | { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9724 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9725 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9726 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9727 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9728 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 9729 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9730 | { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9731 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9732 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9733 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9734 | { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 9735 | { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9736 | { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 9737 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9738 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9739 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9740 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9741 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9742 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9743 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9744 | { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 9745 | { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9746 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9747 | { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 9748 | { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 9749 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9750 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9751 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9752 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9753 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9754 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9755 | { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Emeric Brun | 55f4fa8 | 2014-04-30 17:11:25 +0200 | [diff] [blame] | 9756 | { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9757 | { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 9758 | { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9759 | { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9760 | { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 9761 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9762 | { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 9763 | { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 9764 | { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9765 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9766 | { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 9767 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9768 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9769 | { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9770 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9771 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9772 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 9773 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9774 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9775 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9776 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9777 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9778 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9779 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 9780 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9781 | { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9782 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9783 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9784 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9785 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9786 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9787 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9788 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9789 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9790 | { "ssl_fc_cipherlist_xxh", smp_fetch_ssl_fc_cl_xxh64, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9791 | { NULL, NULL, 0, 0, 0 }, |
| 9792 | }}; |
| 9793 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9794 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 9795 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9796 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9797 | * Please take care of keeping this list alphabetically sorted. |
| 9798 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9799 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 9800 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 9801 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 9802 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9803 | }}; |
| 9804 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9805 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 9806 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9807 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9808 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9809 | * all code contributors. |
| 9810 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9811 | * the config parser can report an appropriate error when a known keyword was |
| 9812 | * not enabled. |
| 9813 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9814 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9815 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9816 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9817 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9818 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9819 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9820 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9821 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9822 | { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9823 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9824 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9825 | { "no-ca-names", ssl_bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9826 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9827 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 9828 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9829 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9830 | { NULL, NULL, 0 }, |
| 9831 | }; |
| 9832 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9833 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 9834 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 9835 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9836 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9837 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9838 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9839 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 9840 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 9841 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 9842 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9843 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9844 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9845 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9846 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 9847 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 9848 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 9849 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 9850 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 9851 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 9852 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 9853 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 9854 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 9855 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9856 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9857 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9858 | { "no-ca-names", bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9859 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 9860 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 9861 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 9862 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9863 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9864 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 9865 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9866 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 9867 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9868 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 9869 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 9870 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9871 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 9872 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9873 | { NULL, NULL, 0 }, |
| 9874 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9875 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9876 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 9877 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9878 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9879 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9880 | * all code contributors. |
| 9881 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9882 | * the config parser can report an appropriate error when a known keyword was |
| 9883 | * not enabled. |
| 9884 | */ |
| 9885 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9886 | { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9887 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9888 | { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9889 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9890 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9891 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 9892 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9893 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9894 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 9895 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9896 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 9897 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 9898 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 9899 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 9900 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 9901 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 9902 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 9903 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 9904 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 9905 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 9906 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 9907 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 9908 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 9909 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 9910 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 9911 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 9912 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 9913 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9914 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9915 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 9916 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 9917 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 9918 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 9919 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 9920 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 9921 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 9922 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 9923 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 9924 | { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */ |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9925 | { NULL, NULL, 0, 0 }, |
| 9926 | }}; |
| 9927 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9928 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 9929 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9930 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9931 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 9932 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9933 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9934 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 9935 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9936 | #ifndef OPENSSL_NO_DH |
| 9937 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 9938 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9939 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9940 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9941 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9942 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9943 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 9944 | #ifndef OPENSSL_NO_DH |
| 9945 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 9946 | #endif |
| 9947 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 9948 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 9949 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 9950 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9951 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9952 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 9953 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9954 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9955 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9956 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9957 | #endif |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9958 | { 0, NULL, NULL }, |
| 9959 | }}; |
| 9960 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9961 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 9962 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9963 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 9964 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9965 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9966 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 9967 | #endif |
| 9968 | { NULL, NULL, 0, 0, 0 }, |
| 9969 | }}; |
| 9970 | |
| 9971 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 9972 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 9973 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 9974 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9975 | .snd_buf = ssl_sock_from_buf, |
| 9976 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 9977 | .subscribe = ssl_subscribe, |
| 9978 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 9979 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 9980 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9981 | .rcv_pipe = NULL, |
| 9982 | .snd_pipe = NULL, |
| 9983 | .shutr = NULL, |
| 9984 | .shutw = ssl_sock_shutw, |
| 9985 | .close = ssl_sock_close, |
| 9986 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 9987 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 9988 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 9989 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 9990 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 9991 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 9992 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9993 | }; |
| 9994 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9995 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 9996 | struct session *sess, struct stream *s, int flags) |
| 9997 | { |
| 9998 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9999 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10000 | |
| 10001 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10002 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10003 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10004 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10005 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10006 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10007 | s->req.flags |= CF_READ_NULL; |
| 10008 | return ACT_RET_YIELD; |
| 10009 | } |
| 10010 | } |
| 10011 | return (ACT_RET_CONT); |
| 10012 | } |
| 10013 | |
| 10014 | static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err) |
| 10015 | { |
| 10016 | rule->action_ptr = ssl_action_wait_for_hs; |
| 10017 | |
| 10018 | return ACT_RET_PRS_OK; |
| 10019 | } |
| 10020 | |
| 10021 | static struct action_kw_list http_req_actions = {ILH, { |
| 10022 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 10023 | { /* END */ } |
| 10024 | }}; |
| 10025 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10026 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 10027 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10028 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10029 | |
| 10030 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 10031 | { |
| 10032 | if (ptr) { |
| 10033 | chunk_destroy(ptr); |
| 10034 | free(ptr); |
| 10035 | } |
| 10036 | } |
| 10037 | |
| 10038 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 10039 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 10040 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10041 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 10042 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10043 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10044 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10045 | static void __ssl_sock_init(void) |
| 10046 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10047 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10048 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10049 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10050 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10051 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10052 | if (global_ssl.listen_default_ciphers) |
| 10053 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 10054 | if (global_ssl.connect_default_ciphers) |
| 10055 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10056 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10057 | if (global_ssl.listen_default_ciphersuites) |
| 10058 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 10059 | if (global_ssl.connect_default_ciphersuites) |
| 10060 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 10061 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 10062 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 10063 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10064 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10065 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 10066 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10067 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10068 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10069 | n = sk_SSL_COMP_num(cm); |
| 10070 | while (n--) { |
| 10071 | (void) sk_SSL_COMP_pop(cm); |
| 10072 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10073 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10074 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10075 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10076 | ssl_locking_init(); |
| 10077 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10078 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10079 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 10080 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 10081 | ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Thierry FOURNIER | 16ff050 | 2018-06-17 21:33:01 +0200 | [diff] [blame] | 10082 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 10083 | ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10084 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10085 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10086 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10087 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 10088 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10089 | hap_register_post_check(tlskeys_finalize_config); |
| 10090 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10091 | |
| 10092 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 10093 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 10094 | |
| 10095 | #ifndef OPENSSL_NO_DH |
| 10096 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 10097 | hap_register_post_deinit(ssl_free_dh); |
| 10098 | #endif |
| 10099 | #ifndef OPENSSL_NO_ENGINE |
| 10100 | hap_register_post_deinit(ssl_free_engines); |
| 10101 | #endif |
| 10102 | /* Load SSL string for the verbose & debug mode. */ |
| 10103 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 10104 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 10105 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 10106 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 10107 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 10108 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 10109 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 10110 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 10111 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10112 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 10113 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10114 | /* Compute and register the version string */ |
| 10115 | static void ssl_register_build_options() |
| 10116 | { |
| 10117 | char *ptr = NULL; |
| 10118 | int i; |
| 10119 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10120 | memprintf(&ptr, "Built with OpenSSL version : " |
| 10121 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 10122 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10123 | #else /* OPENSSL_IS_BORINGSSL */ |
| 10124 | OPENSSL_VERSION_TEXT |
| 10125 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 10126 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 10127 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10128 | #endif |
| 10129 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10130 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10131 | "no (library version too old)" |
| 10132 | #elif defined(OPENSSL_NO_TLSEXT) |
| 10133 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 10134 | #else |
| 10135 | "yes" |
| 10136 | #endif |
| 10137 | "", ptr); |
| 10138 | |
| 10139 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 10140 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 10141 | "yes" |
| 10142 | #else |
| 10143 | #ifdef OPENSSL_NO_TLSEXT |
| 10144 | "no (because of OPENSSL_NO_TLSEXT)" |
| 10145 | #else |
| 10146 | "no (version might be too old, 0.9.8f min needed)" |
| 10147 | #endif |
| 10148 | #endif |
| 10149 | "", ptr); |
| 10150 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 10151 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 10152 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 10153 | if (methodVersions[i].option) |
| 10154 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 10155 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10156 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10157 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10158 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10159 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 10160 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10161 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10162 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10163 | void ssl_free_engines(void) { |
| 10164 | struct ssl_engine_list *wl, *wlb; |
| 10165 | /* free up engine list */ |
| 10166 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 10167 | ENGINE_finish(wl->e); |
| 10168 | ENGINE_free(wl->e); |
| 10169 | LIST_DEL(&wl->list); |
| 10170 | free(wl); |
| 10171 | } |
| 10172 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10173 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 10174 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10175 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10176 | void ssl_free_dh(void) { |
| 10177 | if (local_dh_1024) { |
| 10178 | DH_free(local_dh_1024); |
| 10179 | local_dh_1024 = NULL; |
| 10180 | } |
| 10181 | if (local_dh_2048) { |
| 10182 | DH_free(local_dh_2048); |
| 10183 | local_dh_2048 = NULL; |
| 10184 | } |
| 10185 | if (local_dh_4096) { |
| 10186 | DH_free(local_dh_4096); |
| 10187 | local_dh_4096 = NULL; |
| 10188 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 10189 | if (global_dh) { |
| 10190 | DH_free(global_dh); |
| 10191 | global_dh = NULL; |
| 10192 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10193 | } |
| 10194 | #endif |
| 10195 | |
| 10196 | __attribute__((destructor)) |
| 10197 | static void __ssl_sock_deinit(void) |
| 10198 | { |
| 10199 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10200 | if (ssl_ctx_lru_tree) { |
| 10201 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 10202 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10203 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10204 | #endif |
| 10205 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10206 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10207 | ERR_remove_state(0); |
| 10208 | ERR_free_strings(); |
| 10209 | |
| 10210 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 10211 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10212 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10213 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10214 | CRYPTO_cleanup_all_ex_data(); |
| 10215 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 10216 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10217 | } |
| 10218 | |
| 10219 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10220 | /* |
| 10221 | * Local variables: |
| 10222 | * c-indent-level: 8 |
| 10223 | * c-basic-offset: 8 |
| 10224 | * End: |
| 10225 | */ |