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 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 60 | #include <ebpttree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 61 | #include <ebsttree.h> |
| 62 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 63 | #include <types/applet.h> |
| 64 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 65 | #include <types/global.h> |
| 66 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 67 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 69 | #include <proto/acl.h> |
| 70 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 71 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 73 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 74 | #include <proto/fd.h> |
| 75 | #include <proto/freq_ctr.h> |
| 76 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 77 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 78 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 79 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 80 | #include <proto/proto_tcp.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 81 | #include <proto/http_ana.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 82 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 83 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 84 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 85 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 86 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 87 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 88 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 89 | #include <proto/task.h> |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 90 | #include <proto/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 91 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 92 | /* ***** READ THIS before adding code here! ***** |
| 93 | * |
| 94 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 95 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 96 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 97 | * exclusively so that the whole code consistently uses the same macros. |
| 98 | * |
| 99 | * Whenever possible if a macro is missing in certain versions, it's better |
| 100 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 101 | */ |
| 102 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 103 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 104 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 105 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 106 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 107 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 108 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 109 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 110 | |
| 111 | /* Verify errors macros */ |
| 112 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 113 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 114 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 115 | |
| 116 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 117 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 118 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 119 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 120 | /* ssl_methods flags for ssl options */ |
| 121 | #define MC_SSL_O_ALL 0x0000 |
| 122 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 123 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 124 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 125 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 126 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 127 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 128 | /* file to guess during file loading */ |
| 129 | #define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */ |
| 130 | #define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */ |
| 131 | #define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */ |
| 132 | #define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */ |
| 133 | #define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */ |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 134 | #define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 135 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 136 | #define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY) |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 137 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 138 | /* ssl_methods versions */ |
| 139 | enum { |
| 140 | CONF_TLSV_NONE = 0, |
| 141 | CONF_TLSV_MIN = 1, |
| 142 | CONF_SSLV3 = 1, |
| 143 | CONF_TLSV10 = 2, |
| 144 | CONF_TLSV11 = 3, |
| 145 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 146 | CONF_TLSV13 = 5, |
| 147 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 150 | /* server and bind verify method, it uses a global value as default */ |
| 151 | enum { |
| 152 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 153 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 154 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 155 | SSL_SOCK_VERIFY_NONE = 3, |
| 156 | }; |
| 157 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 158 | int sslconns = 0; |
| 159 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 160 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 161 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 162 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 163 | static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */ |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 164 | static struct issuer_chain* ssl_get_issuer_chain(X509 *cert); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 165 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 166 | static struct { |
| 167 | char *crt_base; /* base directory path for certificates */ |
| 168 | char *ca_base; /* base directory path for CAs and CRLs */ |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 169 | char *issuers_chain_path; /* from "issuers-chain-path" */ |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 170 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 171 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 172 | |
| 173 | char *listen_default_ciphers; |
| 174 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 175 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 176 | char *listen_default_ciphersuites; |
| 177 | char *connect_default_ciphersuites; |
| 178 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 179 | int listen_default_ssloptions; |
| 180 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 181 | struct tls_version_filter listen_default_sslmethods; |
| 182 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 183 | |
| 184 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 185 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 186 | unsigned int max_record; /* SSL max record size */ |
| 187 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 188 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 189 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 190 | int extra_files; /* which files not defined in the configuration file are we looking for */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 191 | } global_ssl = { |
| 192 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 193 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 194 | #endif |
| 195 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 196 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 197 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 198 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 199 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 200 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 201 | #endif |
| 202 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 203 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 204 | #endif |
| 205 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 206 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 207 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 208 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 209 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 210 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 211 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 212 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 213 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 214 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 215 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 216 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 217 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 218 | #endif |
| 219 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 220 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 221 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 222 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 225 | static BIO_METHOD *ha_meth; |
| 226 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 227 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 228 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 229 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 230 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 231 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 232 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 233 | struct wait_event wait_event; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 234 | struct wait_event *subs; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 235 | int xprt_st; /* transport layer state, initialized to zero */ |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 236 | struct buffer early_buf; /* buffer to store the early data received */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 237 | int sent_early_data; /* Amount of early data we sent so far */ |
| 238 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 242 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 243 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 244 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 245 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 246 | /* Methods to implement OpenSSL BIO */ |
| 247 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 248 | { |
| 249 | struct buffer tmpbuf; |
| 250 | struct ssl_sock_ctx *ctx; |
| 251 | int ret; |
| 252 | |
| 253 | ctx = BIO_get_data(h); |
| 254 | tmpbuf.size = num; |
| 255 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 256 | tmpbuf.data = num; |
| 257 | tmpbuf.head = 0; |
| 258 | 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] | 259 | 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] | 260 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 261 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 262 | } else if (ret == 0) |
| 263 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 268 | { |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int ha_ssl_puts(BIO *h, const char *str) |
| 274 | { |
| 275 | |
| 276 | return ha_ssl_write(h, str, strlen(str)); |
| 277 | } |
| 278 | |
| 279 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 280 | { |
| 281 | struct buffer tmpbuf; |
| 282 | struct ssl_sock_ctx *ctx; |
| 283 | int ret; |
| 284 | |
| 285 | ctx = BIO_get_data(h); |
| 286 | tmpbuf.size = size; |
| 287 | tmpbuf.area = buf; |
| 288 | tmpbuf.data = 0; |
| 289 | tmpbuf.head = 0; |
| 290 | 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] | 291 | 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] | 292 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 293 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 294 | } else if (ret == 0) |
| 295 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 301 | { |
| 302 | int ret = 0; |
| 303 | switch (cmd) { |
| 304 | case BIO_CTRL_DUP: |
| 305 | case BIO_CTRL_FLUSH: |
| 306 | ret = 1; |
| 307 | break; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int ha_ssl_new(BIO *h) |
| 313 | { |
| 314 | BIO_set_init(h, 1); |
| 315 | BIO_set_data(h, NULL); |
| 316 | BIO_clear_flags(h, ~0); |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | static int ha_ssl_free(BIO *data) |
| 321 | { |
| 322 | |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 327 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 328 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 329 | static HA_RWLOCK_T *ssl_rwlocks; |
| 330 | |
| 331 | |
| 332 | unsigned long ssl_id_function(void) |
| 333 | { |
| 334 | return (unsigned long)tid; |
| 335 | } |
| 336 | |
| 337 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 338 | { |
| 339 | if (mode & CRYPTO_LOCK) { |
| 340 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 341 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 342 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 343 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 344 | } |
| 345 | else { |
| 346 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 347 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 348 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 349 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | static int ssl_locking_init(void) |
| 354 | { |
| 355 | int i; |
| 356 | |
| 357 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 358 | if (!ssl_rwlocks) |
| 359 | return -1; |
| 360 | |
| 361 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 362 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 363 | |
| 364 | CRYPTO_set_id_callback(ssl_id_function); |
| 365 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 366 | |
| 367 | return 0; |
| 368 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 369 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 372 | __decl_hathreads(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 373 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 374 | /* Uncommitted CKCH transaction */ |
| 375 | |
| 376 | static struct { |
| 377 | struct ckch_store *new_ckchs; |
| 378 | struct ckch_store *old_ckchs; |
| 379 | char *path; |
| 380 | } ckchs_transaction; |
| 381 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 382 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 383 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 384 | */ |
| 385 | struct cafile_entry { |
| 386 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 387 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 388 | struct ebmb_node node; |
| 389 | char path[0]; |
| 390 | }; |
| 391 | |
| 392 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 393 | |
| 394 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 395 | { |
| 396 | struct ebmb_node *eb; |
| 397 | |
| 398 | eb = ebst_lookup(&cafile_tree, path); |
| 399 | if (eb) { |
| 400 | struct cafile_entry *ca_e; |
| 401 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 402 | return ca_e->ca_store; |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | static int ssl_store_load_locations_file(char *path) |
| 408 | { |
| 409 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 410 | struct cafile_entry *ca_e; |
| 411 | X509_STORE *store = X509_STORE_new(); |
| 412 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 413 | int pathlen; |
| 414 | pathlen = strlen(path); |
| 415 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 416 | if (ca_e) { |
| 417 | memcpy(ca_e->path, path, pathlen + 1); |
| 418 | ca_e->ca_store = store; |
| 419 | ebst_insert(&cafile_tree, &ca_e->node); |
| 420 | return 1; |
| 421 | } |
| 422 | } |
| 423 | X509_STORE_free(store); |
| 424 | return 0; |
| 425 | } |
| 426 | return 1; |
| 427 | } |
| 428 | |
| 429 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 430 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 431 | { |
| 432 | X509_STORE *store; |
| 433 | store = ssl_store_get0_locations_file(path); |
| 434 | if (store_ctx && store) { |
| 435 | int i; |
| 436 | X509_OBJECT *obj; |
| 437 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 438 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 439 | obj = sk_X509_OBJECT_value(objs, i); |
| 440 | switch (X509_OBJECT_get_type(obj)) { |
| 441 | case X509_LU_X509: |
| 442 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 443 | break; |
| 444 | case X509_LU_CRL: |
| 445 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 446 | break; |
| 447 | default: |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 456 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 457 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 458 | { |
| 459 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 460 | return ssl_set_cert_crl_file(store_ctx, path); |
| 461 | } |
| 462 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 463 | /* |
| 464 | Extract CA_list from CA_file already in tree. |
| 465 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 466 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 467 | */ |
| 468 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 469 | { |
| 470 | struct ebmb_node *eb; |
| 471 | struct cafile_entry *ca_e; |
| 472 | |
| 473 | eb = ebst_lookup(&cafile_tree, path); |
| 474 | if (!eb) |
| 475 | return NULL; |
| 476 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 477 | |
| 478 | if (ca_e->ca_list == NULL) { |
| 479 | int i; |
| 480 | unsigned long key; |
| 481 | struct eb_root ca_name_tree = EB_ROOT; |
| 482 | struct eb64_node *node, *back; |
| 483 | struct { |
| 484 | struct eb64_node node; |
| 485 | X509_NAME *xname; |
| 486 | } *ca_name; |
| 487 | STACK_OF(X509_OBJECT) *objs; |
| 488 | STACK_OF(X509_NAME) *skn; |
| 489 | X509 *x; |
| 490 | X509_NAME *xn; |
| 491 | |
| 492 | skn = sk_X509_NAME_new_null(); |
| 493 | /* take x509 from cafile_tree */ |
| 494 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 495 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 496 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 497 | if (!x) |
| 498 | continue; |
| 499 | xn = X509_get_subject_name(x); |
| 500 | if (!xn) |
| 501 | continue; |
| 502 | /* Check for duplicates. */ |
| 503 | key = X509_NAME_hash(xn); |
| 504 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 505 | node && ca_name == NULL; |
| 506 | node = eb64_next(node)) { |
| 507 | ca_name = container_of(node, typeof(*ca_name), node); |
| 508 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 509 | ca_name = NULL; |
| 510 | } |
| 511 | /* find a duplicate */ |
| 512 | if (ca_name) |
| 513 | continue; |
| 514 | ca_name = calloc(1, sizeof *ca_name); |
| 515 | xn = X509_NAME_dup(xn); |
| 516 | if (!ca_name || |
| 517 | !xn || |
| 518 | !sk_X509_NAME_push(skn, xn)) { |
| 519 | free(ca_name); |
| 520 | X509_NAME_free(xn); |
| 521 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 522 | sk_X509_NAME_free(skn); |
| 523 | skn = NULL; |
| 524 | break; |
| 525 | } |
| 526 | ca_name->node.key = key; |
| 527 | ca_name->xname = xn; |
| 528 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 529 | } |
| 530 | ca_e->ca_list = skn; |
| 531 | /* remove temporary ca_name tree */ |
| 532 | node = eb64_first(&ca_name_tree); |
| 533 | while (node) { |
| 534 | ca_name = container_of(node, typeof(*ca_name), node); |
| 535 | back = eb64_next(node); |
| 536 | eb64_delete(node); |
| 537 | free(ca_name); |
| 538 | node = back; |
| 539 | } |
| 540 | } |
| 541 | return ca_e->ca_list; |
| 542 | } |
| 543 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 544 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 545 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 546 | unsigned long long int xxh64; |
| 547 | unsigned char ciphersuite_len; |
| 548 | char ciphersuite[0]; |
| 549 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 550 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 551 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 552 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 553 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 554 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 555 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 556 | #endif |
| 557 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 558 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 559 | static unsigned int openssl_engines_initialized; |
| 560 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 561 | struct ssl_engine_list { |
| 562 | struct list list; |
| 563 | ENGINE *e; |
| 564 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 565 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 566 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 567 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 568 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 569 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 570 | static DH *local_dh_1024 = NULL; |
| 571 | static DH *local_dh_2048 = NULL; |
| 572 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 573 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 574 | #endif /* OPENSSL_NO_DH */ |
| 575 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 576 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 577 | /* X509V3 Extensions that will be added on generated certificates */ |
| 578 | #define X509V3_EXT_SIZE 5 |
| 579 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 580 | "basicConstraints", |
| 581 | "nsComment", |
| 582 | "subjectKeyIdentifier", |
| 583 | "authorityKeyIdentifier", |
| 584 | "keyUsage", |
| 585 | }; |
| 586 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 587 | "CA:FALSE", |
| 588 | "\"OpenSSL Generated Certificate\"", |
| 589 | "hash", |
| 590 | "keyid,issuer:always", |
| 591 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 592 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 593 | /* LRU cache to store generated certificate */ |
| 594 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 595 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 596 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 597 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 598 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 599 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 600 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 601 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 602 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 603 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 604 | /* The order here matters for picking a default context, |
| 605 | * keep the most common keytype at the bottom of the list |
| 606 | */ |
| 607 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 608 | "dsa", |
| 609 | "ecdsa", |
| 610 | "rsa" |
| 611 | }; |
| 612 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 613 | #else |
| 614 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 615 | #endif |
| 616 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 617 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 618 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 619 | |
| 620 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 621 | |
| 622 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 623 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 624 | |
| 625 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 626 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 627 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 628 | /* |
| 629 | * This function gives the detail of the SSL error. It is used only |
| 630 | * if the debug mode and the verbose mode are activated. It dump all |
| 631 | * the SSL error until the stack was empty. |
| 632 | */ |
| 633 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 634 | { |
| 635 | unsigned long ret; |
| 636 | |
| 637 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 638 | while(1) { |
| 639 | ret = ERR_get_error(); |
| 640 | if (ret == 0) |
| 641 | return; |
| 642 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 643 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 644 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 649 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 650 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 651 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 652 | { |
| 653 | int err_code = ERR_ABORT; |
| 654 | ENGINE *engine; |
| 655 | struct ssl_engine_list *el; |
| 656 | |
| 657 | /* grab the structural reference to the engine */ |
| 658 | engine = ENGINE_by_id(engine_id); |
| 659 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 660 | 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] | 661 | goto fail_get; |
| 662 | } |
| 663 | |
| 664 | if (!ENGINE_init(engine)) { |
| 665 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 666 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 667 | goto fail_init; |
| 668 | } |
| 669 | |
| 670 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 671 | 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] | 672 | goto fail_set_method; |
| 673 | } |
| 674 | |
| 675 | el = calloc(1, sizeof(*el)); |
| 676 | el->e = engine; |
| 677 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 678 | nb_engines++; |
| 679 | if (global_ssl.async) |
| 680 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | |
| 683 | fail_set_method: |
| 684 | /* release the functional reference from ENGINE_init() */ |
| 685 | ENGINE_finish(engine); |
| 686 | |
| 687 | fail_init: |
| 688 | /* release the structural reference from ENGINE_by_id() */ |
| 689 | ENGINE_free(engine); |
| 690 | |
| 691 | fail_get: |
| 692 | return err_code; |
| 693 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 694 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 695 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 696 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 697 | /* |
| 698 | * openssl async fd handler |
| 699 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 700 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 701 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 702 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 703 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 704 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 705 | * to poll this fd until it is requested |
| 706 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 707 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 708 | fd_cant_recv(fd); |
| 709 | |
| 710 | /* crypto engine is available, let's notify the associated |
| 711 | * connection that it can pursue its processing. |
| 712 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 713 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 716 | /* |
| 717 | * openssl async delayed SSL_free handler |
| 718 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 719 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 720 | { |
| 721 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 722 | OSSL_ASYNC_FD all_fd[32]; |
| 723 | size_t num_all_fds = 0; |
| 724 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 725 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 726 | /* We suppose that the async job for a same SSL * |
| 727 | * are serialized. So if we are awake it is |
| 728 | * because the running job has just finished |
| 729 | * and we can remove all async fds safely |
| 730 | */ |
| 731 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 732 | if (num_all_fds > 32) { |
| 733 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 738 | for (i=0 ; i < num_all_fds ; i++) |
| 739 | fd_remove(all_fd[i]); |
| 740 | |
| 741 | /* 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] | 742 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 743 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 744 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 745 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 746 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 747 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 748 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 749 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 750 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 751 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 752 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 753 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 754 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 755 | size_t num_add_fds = 0; |
| 756 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 757 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 758 | |
| 759 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 760 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 761 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 762 | 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] | 763 | return; |
| 764 | } |
| 765 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 766 | 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] | 767 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 768 | /* We remove unused fds from the fdtab */ |
| 769 | for (i=0 ; i < num_del_fds ; i++) |
| 770 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 771 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 772 | /* We add new fds to the fdtab */ |
| 773 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 774 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 777 | num_add_fds = 0; |
| 778 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 779 | if (num_add_fds > 32) { |
| 780 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 781 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 782 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 783 | |
| 784 | /* We activate the polling for all known async fds */ |
| 785 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 786 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 787 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 788 | /* To ensure that the fd cache won't be used |
| 789 | * We'll prefer to catch a real RD event |
| 790 | * because handling an EAGAIN on this fd will |
| 791 | * result in a context switch and also |
| 792 | * some engines uses a fd in blocking mode. |
| 793 | */ |
| 794 | fd_cant_recv(add_fd[i]); |
| 795 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 796 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 797 | } |
| 798 | #endif |
| 799 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 800 | #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] | 801 | /* |
| 802 | * This function returns the number of seconds elapsed |
| 803 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 804 | * date presented un ASN1_GENERALIZEDTIME. |
| 805 | * |
| 806 | * In parsing error case, it returns -1. |
| 807 | */ |
| 808 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 809 | { |
| 810 | long epoch; |
| 811 | char *p, *end; |
| 812 | const unsigned short month_offset[12] = { |
| 813 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 814 | }; |
| 815 | int year, month; |
| 816 | |
| 817 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 818 | |
| 819 | p = (char *)d->data; |
| 820 | end = p + d->length; |
| 821 | |
| 822 | if (end - p < 4) return -1; |
| 823 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 824 | p += 4; |
| 825 | if (end - p < 2) return -1; |
| 826 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 827 | if (month < 1 || month > 12) return -1; |
| 828 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 829 | We consider leap years and the current month (<marsh or not) */ |
| 830 | epoch = ( ((year - 1970) * 365) |
| 831 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 832 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 833 | + month_offset[month-1] |
| 834 | ) * 24 * 60 * 60; |
| 835 | p += 2; |
| 836 | if (end - p < 2) return -1; |
| 837 | /* Add the number of seconds of completed days of current month */ |
| 838 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 839 | p += 2; |
| 840 | if (end - p < 2) return -1; |
| 841 | /* Add the completed hours of the current day */ |
| 842 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 843 | p += 2; |
| 844 | if (end - p < 2) return -1; |
| 845 | /* Add the completed minutes of the current hour */ |
| 846 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 847 | p += 2; |
| 848 | if (p == end) return -1; |
| 849 | /* Test if there is available seconds */ |
| 850 | if (p[0] < '0' || p[0] > '9') |
| 851 | goto nosec; |
| 852 | if (end - p < 2) return -1; |
| 853 | /* Add the seconds of the current minute */ |
| 854 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 855 | p += 2; |
| 856 | if (p == end) return -1; |
| 857 | /* Ignore seconds float part if present */ |
| 858 | if (p[0] == '.') { |
| 859 | do { |
| 860 | if (++p == end) return -1; |
| 861 | } while (p[0] >= '0' && p[0] <= '9'); |
| 862 | } |
| 863 | |
| 864 | nosec: |
| 865 | if (p[0] == 'Z') { |
| 866 | if (end - p != 1) return -1; |
| 867 | return epoch; |
| 868 | } |
| 869 | else if (p[0] == '+') { |
| 870 | if (end - p != 5) return -1; |
| 871 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 872 | 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] | 873 | } |
| 874 | else if (p[0] == '-') { |
| 875 | if (end - p != 5) return -1; |
| 876 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 877 | 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] | 878 | } |
| 879 | |
| 880 | return -1; |
| 881 | } |
| 882 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 883 | /* |
| 884 | * struct alignment works here such that the key.key is the same as key_data |
| 885 | * Do not change the placement of key_data |
| 886 | */ |
| 887 | struct certificate_ocsp { |
| 888 | struct ebmb_node key; |
| 889 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 890 | struct buffer response; |
| 891 | long expire; |
| 892 | }; |
| 893 | |
| 894 | struct ocsp_cbk_arg { |
| 895 | int is_single; |
| 896 | int single_kt; |
| 897 | union { |
| 898 | struct certificate_ocsp *s_ocsp; |
| 899 | /* |
| 900 | * m_ocsp will have multiple entries dependent on key type |
| 901 | * Entry 0 - DSA |
| 902 | * Entry 1 - ECDSA |
| 903 | * Entry 2 - RSA |
| 904 | */ |
| 905 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 906 | }; |
| 907 | }; |
| 908 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 909 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 910 | |
| 911 | /* This function starts to check if the OCSP response (in DER format) contained |
| 912 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 913 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 914 | * contained in the OCSP Response and exits on error if no match. |
| 915 | * If it's a valid OCSP Response: |
| 916 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 917 | * pointed by 'ocsp'. |
| 918 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 919 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 920 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 921 | * already present in the container, it will be overwritten. |
| 922 | * |
| 923 | * Note: OCSP response containing more than one OCSP Single response is not |
| 924 | * considered valid. |
| 925 | * |
| 926 | * Returns 0 on success, 1 in error case. |
| 927 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 928 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 929 | struct certificate_ocsp *ocsp, |
| 930 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 931 | { |
| 932 | OCSP_RESPONSE *resp; |
| 933 | OCSP_BASICRESP *bs = NULL; |
| 934 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 935 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 936 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 937 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 938 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 939 | int reason; |
| 940 | int ret = 1; |
| 941 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 942 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 943 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 944 | if (!resp) { |
| 945 | memprintf(err, "Unable to parse OCSP response"); |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | rc = OCSP_response_status(resp); |
| 950 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 951 | memprintf(err, "OCSP response status not successful"); |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | bs = OCSP_response_get1_basic(resp); |
| 956 | if (!bs) { |
| 957 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | count_sr = OCSP_resp_count(bs); |
| 962 | if (count_sr > 1) { |
| 963 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | sr = OCSP_resp_get0(bs, 0); |
| 968 | if (!sr) { |
| 969 | memprintf(err, "Failed to get OCSP single response"); |
| 970 | goto out; |
| 971 | } |
| 972 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 973 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 974 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 975 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 976 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 977 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 978 | goto out; |
| 979 | } |
| 980 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 981 | if (!nextupd) { |
| 982 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 983 | goto out; |
| 984 | } |
| 985 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 986 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 987 | if (!rc) { |
| 988 | memprintf(err, "OCSP single response: no longer valid."); |
| 989 | goto out; |
| 990 | } |
| 991 | |
| 992 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 993 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 994 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 995 | goto out; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (!ocsp) { |
| 1000 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 1001 | unsigned char *p; |
| 1002 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1003 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1004 | if (!rc) { |
| 1005 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | |
| 1009 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 1010 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 1011 | goto out; |
| 1012 | } |
| 1013 | |
| 1014 | p = key; |
| 1015 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1016 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1017 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1018 | if (!ocsp) { |
| 1019 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 1020 | goto out; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* According to comments on "chunk_dup", the |
| 1025 | previous chunk buffer will be freed */ |
| 1026 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1027 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1031 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1032 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1033 | ret = 0; |
| 1034 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1035 | ERR_clear_error(); |
| 1036 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1037 | if (bs) |
| 1038 | OCSP_BASICRESP_free(bs); |
| 1039 | |
| 1040 | if (resp) |
| 1041 | OCSP_RESPONSE_free(resp); |
| 1042 | |
| 1043 | return ret; |
| 1044 | } |
| 1045 | /* |
| 1046 | * External function use to update the OCSP response in the OCSP response's |
| 1047 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1048 | * to update in DER format. |
| 1049 | * |
| 1050 | * Returns 0 on success, 1 in error case. |
| 1051 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1052 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1053 | { |
| 1054 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1055 | } |
| 1056 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1057 | #endif |
| 1058 | |
| 1059 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1060 | /* |
| 1061 | * This function load the OCSP Resonse in DER format contained in file at |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1062 | * path 'ocsp_path' or base64 in a buffer <buf> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1063 | * |
| 1064 | * Returns 0 on success, 1 in error case. |
| 1065 | */ |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1066 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1067 | { |
| 1068 | int fd = -1; |
| 1069 | int r = 0; |
| 1070 | int ret = 1; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1071 | struct buffer *ocsp_response; |
| 1072 | struct buffer *src = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1073 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1074 | if (buf) { |
| 1075 | int i, j; |
| 1076 | /* if it's from a buffer it will be base64 */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1077 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1078 | /* remove \r and \n from the payload */ |
| 1079 | for (i = 0, j = 0; buf[i]; i++) { |
| 1080 | if (buf[i] == '\r' || buf[i] == '\n') |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1081 | continue; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1082 | buf[j++] = buf[i]; |
| 1083 | } |
| 1084 | buf[j] = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1085 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1086 | ret = base64dec(buf, j, trash.area, trash.size); |
| 1087 | if (ret < 0) { |
| 1088 | memprintf(err, "Error reading OCSP response in base64 format"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1089 | goto end; |
| 1090 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1091 | trash.data = ret; |
| 1092 | src = &trash; |
| 1093 | } else { |
| 1094 | fd = open(ocsp_path, O_RDONLY); |
| 1095 | if (fd == -1) { |
| 1096 | memprintf(err, "Error opening OCSP response file"); |
| 1097 | goto end; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1098 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1099 | |
| 1100 | trash.data = 0; |
| 1101 | while (trash.data < trash.size) { |
| 1102 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1103 | if (r < 0) { |
| 1104 | if (errno == EINTR) |
| 1105 | continue; |
| 1106 | |
| 1107 | memprintf(err, "Error reading OCSP response from file"); |
| 1108 | goto end; |
| 1109 | } |
| 1110 | else if (r == 0) { |
| 1111 | break; |
| 1112 | } |
| 1113 | trash.data += r; |
| 1114 | } |
| 1115 | close(fd); |
| 1116 | fd = -1; |
| 1117 | src = &trash; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1120 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 1121 | if (!chunk_dup(ocsp_response, src)) { |
| 1122 | free(ocsp_response); |
| 1123 | ocsp_response = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1124 | goto end; |
| 1125 | } |
Emmanuel Hocdet | 0667fae | 2020-01-16 14:41:36 +0100 | [diff] [blame] | 1126 | /* no error, fill ckch with new context, old context must be free */ |
| 1127 | if (ckch->ocsp_response) { |
| 1128 | free(ckch->ocsp_response->area); |
| 1129 | ckch->ocsp_response->area = NULL; |
| 1130 | free(ckch->ocsp_response); |
| 1131 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1132 | ckch->ocsp_response = ocsp_response; |
William Lallemand | e0f48ae | 2019-10-15 13:44:57 +0200 | [diff] [blame] | 1133 | ret = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1134 | end: |
| 1135 | if (fd != -1) |
| 1136 | close(fd); |
| 1137 | |
| 1138 | return ret; |
| 1139 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1140 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1141 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1142 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1143 | 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) |
| 1144 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1145 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1146 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1147 | struct connection *conn; |
| 1148 | int head; |
| 1149 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1150 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1151 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1152 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1153 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1154 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1155 | |
| 1156 | keys = ref->tlskeys; |
| 1157 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1158 | |
| 1159 | if (enc) { |
| 1160 | memcpy(key_name, keys[head].name, 16); |
| 1161 | |
| 1162 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1163 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1164 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1165 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1166 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1167 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1168 | goto end; |
| 1169 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1170 | 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] | 1171 | ret = 1; |
| 1172 | } |
| 1173 | else if (ref->key_size_bits == 256 ) { |
| 1174 | |
| 1175 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1176 | goto end; |
| 1177 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1178 | 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] | 1179 | ret = 1; |
| 1180 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1183 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1184 | goto found; |
| 1185 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1186 | ret = 0; |
| 1187 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1188 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1189 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1190 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1191 | 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] | 1192 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1193 | goto end; |
| 1194 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1195 | ret = i ? 2 : 1; |
| 1196 | } |
| 1197 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1198 | 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] | 1199 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1200 | goto end; |
| 1201 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1202 | ret = i ? 2 : 1; |
| 1203 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1204 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1205 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1206 | end: |
| 1207 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1208 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1212 | { |
| 1213 | struct tls_keys_ref *ref; |
| 1214 | |
| 1215 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1216 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1217 | return ref; |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1222 | { |
| 1223 | struct tls_keys_ref *ref; |
| 1224 | |
| 1225 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1226 | if (ref->unique_id == unique_id) |
| 1227 | return ref; |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1231 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1232 | * match existing ones, this function returns -1 |
| 1233 | * else it returns 0 on success. |
| 1234 | */ |
| 1235 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1236 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1237 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1238 | if (ref->key_size_bits == 128) { |
| 1239 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1240 | return -1; |
| 1241 | } |
| 1242 | else if (ref->key_size_bits == 256) { |
| 1243 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1244 | return -1; |
| 1245 | } |
| 1246 | else |
| 1247 | return -1; |
| 1248 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1249 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1250 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1251 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1252 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1253 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1254 | |
| 1255 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1258 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1259 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1260 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1261 | |
| 1262 | if(!ref) { |
| 1263 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1264 | return 1; |
| 1265 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1266 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1267 | memprintf(err, "Invalid key size"); |
| 1268 | return 1; |
| 1269 | } |
| 1270 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1271 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1272 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1273 | |
| 1274 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1275 | * automatic ids. It's called just after the basic checks. It returns |
| 1276 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1277 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1278 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1279 | { |
| 1280 | int i = 0; |
| 1281 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1282 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1283 | |
| 1284 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1285 | if (ref->unique_id == -1) { |
| 1286 | /* Look for the first free id. */ |
| 1287 | while (1) { |
| 1288 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1289 | if (ref2->unique_id == i) { |
| 1290 | i++; |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | if (&ref2->list == &tlskeys_reference) |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | /* Uses the unique id and increment it for the next entry. */ |
| 1299 | ref->unique_id = i; |
| 1300 | i++; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /* This sort the reference list by id. */ |
| 1305 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1306 | LIST_DEL(&ref->list); |
| 1307 | list_for_each_entry(ref3, &tkr, list) { |
| 1308 | if (ref->unique_id < ref3->unique_id) { |
| 1309 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | if (&ref3->list == &tkr) |
| 1314 | LIST_ADDQ(&tkr, &ref->list); |
| 1315 | } |
| 1316 | |
| 1317 | /* swap root */ |
| 1318 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1319 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1320 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1321 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1322 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1323 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1324 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1325 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1326 | { |
| 1327 | switch (evp_keytype) { |
| 1328 | case EVP_PKEY_RSA: |
| 1329 | return 2; |
| 1330 | case EVP_PKEY_DSA: |
| 1331 | return 0; |
| 1332 | case EVP_PKEY_EC: |
| 1333 | return 1; |
| 1334 | } |
| 1335 | |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1339 | /* |
| 1340 | * Callback used to set OCSP status extension content in server hello. |
| 1341 | */ |
| 1342 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1343 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1344 | struct certificate_ocsp *ocsp; |
| 1345 | struct ocsp_cbk_arg *ocsp_arg; |
| 1346 | char *ssl_buf; |
| 1347 | EVP_PKEY *ssl_pkey; |
| 1348 | int key_type; |
| 1349 | int index; |
| 1350 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1351 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1352 | |
| 1353 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1354 | if (!ssl_pkey) |
| 1355 | return SSL_TLSEXT_ERR_NOACK; |
| 1356 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1357 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1358 | |
| 1359 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1360 | ocsp = ocsp_arg->s_ocsp; |
| 1361 | else { |
| 1362 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1363 | * the certificate type |
| 1364 | */ |
| 1365 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1366 | |
| 1367 | if (index < 0) |
| 1368 | return SSL_TLSEXT_ERR_NOACK; |
| 1369 | |
| 1370 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1371 | |
| 1372 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1373 | |
| 1374 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1375 | !ocsp->response.area || |
| 1376 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1377 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1378 | return SSL_TLSEXT_ERR_NOACK; |
| 1379 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1380 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1381 | if (!ssl_buf) |
| 1382 | return SSL_TLSEXT_ERR_NOACK; |
| 1383 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1384 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1385 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1386 | |
| 1387 | return SSL_TLSEXT_ERR_OK; |
| 1388 | } |
| 1389 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1390 | #endif |
| 1391 | |
| 1392 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1393 | /* |
| 1394 | * This function enables the handling of OCSP status extension on 'ctx' if a |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1395 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1396 | * status extension, the issuer's certificate is mandatory. It should be |
| 1397 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1398 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1399 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1400 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1401 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1402 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1403 | * |
| 1404 | * 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] | 1405 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1406 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1407 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1408 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1409 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1410 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1411 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1412 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1413 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1414 | char *warn = NULL; |
| 1415 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1416 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1417 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1418 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1419 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1420 | if (!x) |
| 1421 | goto out; |
| 1422 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1423 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1424 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1425 | if (chain) { |
| 1426 | /* check if one of the certificate of the chain is the issuer */ |
| 1427 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1428 | X509 *ti = sk_X509_value(chain, i); |
| 1429 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1430 | issuer = ti; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1435 | if (!issuer) |
| 1436 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1437 | |
| 1438 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1439 | if (!cid) |
| 1440 | goto out; |
| 1441 | |
| 1442 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1443 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1444 | goto out; |
| 1445 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1446 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1447 | if (!ocsp) |
| 1448 | goto out; |
| 1449 | |
| 1450 | p = ocsp->key_data; |
| 1451 | i2d_OCSP_CERTID(cid, &p); |
| 1452 | |
| 1453 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1454 | if (iocsp == ocsp) |
| 1455 | ocsp = NULL; |
| 1456 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1457 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1458 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1459 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1460 | #endif |
| 1461 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1462 | |
| 1463 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1464 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1465 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1466 | |
| 1467 | cb_arg->is_single = 1; |
| 1468 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1469 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1470 | pkey = X509_get_pubkey(x); |
| 1471 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1472 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1473 | |
| 1474 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1475 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1476 | } else { |
| 1477 | /* |
| 1478 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1479 | * Update that cb_arg with the new cert's staple |
| 1480 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1481 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1482 | struct certificate_ocsp *tmp_ocsp; |
| 1483 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1484 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1485 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1486 | |
| 1487 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1488 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1489 | #else |
| 1490 | cb_arg = ctx->tlsext_status_arg; |
| 1491 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 1494 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1495 | * the order of operations below matter, take care when changing it |
| 1496 | */ |
| 1497 | tmp_ocsp = cb_arg->s_ocsp; |
| 1498 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1499 | cb_arg->s_ocsp = NULL; |
| 1500 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1501 | cb_arg->is_single = 0; |
| 1502 | cb_arg->single_kt = 0; |
| 1503 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1504 | pkey = X509_get_pubkey(x); |
| 1505 | key_type = EVP_PKEY_base_id(pkey); |
| 1506 | EVP_PKEY_free(pkey); |
| 1507 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1508 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1509 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1510 | cb_arg->m_ocsp[index] = iocsp; |
| 1511 | |
| 1512 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1513 | |
| 1514 | ret = 0; |
| 1515 | |
| 1516 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1517 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1518 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1519 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1523 | if (cid) |
| 1524 | OCSP_CERTID_free(cid); |
| 1525 | |
| 1526 | if (ocsp) |
| 1527 | free(ocsp); |
| 1528 | |
| 1529 | if (warn) |
| 1530 | free(warn); |
| 1531 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1532 | return ret; |
| 1533 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1534 | #else /* OPENSSL_IS_BORINGSSL */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1535 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1536 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1537 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1538 | } |
| 1539 | #endif |
| 1540 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1541 | #endif |
| 1542 | |
| 1543 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1544 | #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] | 1545 | |
| 1546 | #define CT_EXTENSION_TYPE 18 |
| 1547 | |
| 1548 | static int sctl_ex_index = -1; |
| 1549 | |
| 1550 | /* |
| 1551 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1552 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1553 | * is performed. |
| 1554 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1555 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1556 | { |
| 1557 | int ret = 1; |
| 1558 | int len, pos, sct_len; |
| 1559 | unsigned char *data; |
| 1560 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1561 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1562 | goto out; |
| 1563 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1564 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1565 | len = (data[0] << 8) | data[1]; |
| 1566 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1567 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1568 | goto out; |
| 1569 | |
| 1570 | data = data + 2; |
| 1571 | pos = 0; |
| 1572 | while (pos < len) { |
| 1573 | if (len - pos < 2) |
| 1574 | goto out; |
| 1575 | |
| 1576 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1577 | if (pos + sct_len + 2 > len) |
| 1578 | goto out; |
| 1579 | |
| 1580 | pos += sct_len + 2; |
| 1581 | } |
| 1582 | |
| 1583 | ret = 0; |
| 1584 | |
| 1585 | out: |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1589 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 1590 | * It fills the ckch->sctl buffer |
| 1591 | * return 0 on success or != 0 on failure */ |
| 1592 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1593 | { |
| 1594 | int fd = -1; |
| 1595 | int r = 0; |
| 1596 | int ret = 1; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1597 | struct buffer tmp; |
| 1598 | struct buffer *src; |
| 1599 | struct buffer *sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1600 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1601 | if (buf) { |
| 1602 | tmp.area = buf; |
| 1603 | tmp.data = strlen(buf); |
| 1604 | tmp.size = tmp.data + 1; |
| 1605 | src = &tmp; |
| 1606 | } else { |
| 1607 | fd = open(sctl_path, O_RDONLY); |
| 1608 | if (fd == -1) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1609 | goto end; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1610 | |
| 1611 | trash.data = 0; |
| 1612 | while (trash.data < trash.size) { |
| 1613 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1614 | if (r < 0) { |
| 1615 | if (errno == EINTR) |
| 1616 | continue; |
| 1617 | goto end; |
| 1618 | } |
| 1619 | else if (r == 0) { |
| 1620 | break; |
| 1621 | } |
| 1622 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1623 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1624 | src = &trash; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1627 | ret = ssl_sock_parse_sctl(src); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1628 | if (ret) |
| 1629 | goto end; |
| 1630 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1631 | sctl = calloc(1, sizeof(*sctl)); |
| 1632 | if (!chunk_dup(sctl, src)) { |
| 1633 | free(sctl); |
| 1634 | sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1635 | goto end; |
| 1636 | } |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1637 | /* no error, fill ckch with new context, old context must be free */ |
| 1638 | if (ckch->sctl) { |
| 1639 | free(ckch->sctl->area); |
| 1640 | ckch->sctl->area = NULL; |
| 1641 | free(ckch->sctl); |
| 1642 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1643 | ckch->sctl = sctl; |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1644 | ret = 0; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1645 | end: |
| 1646 | if (fd != -1) |
| 1647 | close(fd); |
| 1648 | |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
| 1652 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1653 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1654 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1655 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1656 | *out = (unsigned char *) sctl->area; |
| 1657 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1658 | |
| 1659 | return 1; |
| 1660 | } |
| 1661 | |
| 1662 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1663 | { |
| 1664 | return 1; |
| 1665 | } |
| 1666 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1667 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1668 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1669 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1670 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1671 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1672 | goto out; |
| 1673 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1674 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1675 | |
| 1676 | ret = 0; |
| 1677 | |
| 1678 | out: |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | #endif |
| 1683 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1684 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1685 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1686 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1687 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1688 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1689 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1690 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1691 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1692 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1693 | * change this to #if and do not assign a default value to this macro! |
| 1694 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1695 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1696 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1697 | if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1698 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1699 | conn->err_code = CO_ER_SSL_RENEG; |
| 1700 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1701 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1702 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1703 | |
| 1704 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1705 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1706 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1707 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1708 | consider that the buffering was activated, |
| 1709 | so we rise the output buffer size from 4k |
| 1710 | to 16k */ |
| 1711 | write_bio = SSL_get_wbio(ssl); |
| 1712 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1713 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1714 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1720 | /* Callback is called for each certificate of the chain during a verify |
| 1721 | ok is set to 1 if preverify detect no error on current certificate. |
| 1722 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1723 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1724 | { |
| 1725 | SSL *ssl; |
| 1726 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1727 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1728 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1729 | |
| 1730 | 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] | 1731 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1732 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1733 | ctx = conn->xprt_ctx; |
| 1734 | |
| 1735 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1736 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1737 | if (ok) /* no errors */ |
| 1738 | return ok; |
| 1739 | |
| 1740 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1741 | err = X509_STORE_CTX_get_error(x_store); |
| 1742 | |
| 1743 | /* check if CA error needs to be ignored */ |
| 1744 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1745 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1746 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1747 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1750 | 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] | 1751 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1752 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1753 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1754 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1756 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1760 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1761 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1762 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1763 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1764 | 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] | 1765 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1766 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1767 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1768 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1769 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1770 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1771 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1774 | static inline |
| 1775 | 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] | 1776 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1777 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1778 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1779 | unsigned char *msg; |
| 1780 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1781 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1782 | |
| 1783 | /* This function is called for "from client" and "to server" |
| 1784 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1785 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1786 | */ |
| 1787 | |
| 1788 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1789 | * otherwise it is set to 1. |
| 1790 | */ |
| 1791 | if (write_p != 0) |
| 1792 | return; |
| 1793 | |
| 1794 | /* content_type contains the type of message received or sent |
| 1795 | * according with the SSL/TLS protocol spec. This message is |
| 1796 | * encoded with one byte. The value 256 (two bytes) is used |
| 1797 | * for designing the SSL/TLS record layer. According with the |
| 1798 | * rfc6101, the expected message (other than 256) are: |
| 1799 | * - change_cipher_spec(20) |
| 1800 | * - alert(21) |
| 1801 | * - handshake(22) |
| 1802 | * - application_data(23) |
| 1803 | * - (255) |
| 1804 | * We are interessed by the handshake and specially the client |
| 1805 | * hello. |
| 1806 | */ |
| 1807 | if (content_type != 22) |
| 1808 | return; |
| 1809 | |
| 1810 | /* The message length is at least 4 bytes, containing the |
| 1811 | * message type and the message length. |
| 1812 | */ |
| 1813 | if (len < 4) |
| 1814 | return; |
| 1815 | |
| 1816 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1817 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1818 | * - hello_request(0) |
| 1819 | * - client_hello(1) |
| 1820 | * - server_hello(2) |
| 1821 | * - certificate(11) |
| 1822 | * - server_key_exchange (12) |
| 1823 | * - certificate_request(13) |
| 1824 | * - server_hello_done(14) |
| 1825 | * We are interested by the client hello. |
| 1826 | */ |
| 1827 | msg = (unsigned char *)buf; |
| 1828 | if (msg[0] != 1) |
| 1829 | return; |
| 1830 | |
| 1831 | /* Next three bytes are the length of the message. The total length |
| 1832 | * must be this decoded length + 4. If the length given as argument |
| 1833 | * is not the same, we abort the protocol dissector. |
| 1834 | */ |
| 1835 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1836 | if (len < rec_len + 4) |
| 1837 | return; |
| 1838 | msg += 4; |
| 1839 | end = msg + rec_len; |
| 1840 | if (end < msg) |
| 1841 | return; |
| 1842 | |
| 1843 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1844 | * 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] | 1845 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1846 | */ |
| 1847 | msg += 1 + 1 + 4 + 28; |
| 1848 | if (msg > end) |
| 1849 | return; |
| 1850 | |
| 1851 | /* Next, is session id: |
| 1852 | * if present, we have to jump by length + 1 for the size information |
| 1853 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1854 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1855 | if (msg[0] > 0) |
| 1856 | msg += msg[0]; |
| 1857 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1858 | if (msg > end) |
| 1859 | return; |
| 1860 | |
| 1861 | /* Next two bytes are the ciphersuite length. */ |
| 1862 | if (msg + 2 > end) |
| 1863 | return; |
| 1864 | rec_len = (msg[0] << 8) + msg[1]; |
| 1865 | msg += 2; |
| 1866 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1867 | return; |
| 1868 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1869 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1870 | if (!capture) |
| 1871 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1872 | /* Compute the xxh64 of the ciphersuite. */ |
| 1873 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1874 | |
| 1875 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1876 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1877 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1878 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1879 | |
| 1880 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1883 | /* Callback is called for ssl protocol analyse */ |
| 1884 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1885 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1886 | #ifdef TLS1_RT_HEARTBEAT |
| 1887 | /* test heartbeat received (write_p is set to 0 |
| 1888 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1889 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1890 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1891 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1892 | const unsigned char *p = buf; |
| 1893 | unsigned int payload; |
| 1894 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1895 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1896 | |
| 1897 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1898 | if (*p != TLS1_HB_REQUEST) |
| 1899 | return; |
| 1900 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1901 | 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] | 1902 | goto kill_it; |
| 1903 | |
| 1904 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1905 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1906 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1907 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1908 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1909 | * advertised payload is larger than the advertised packet |
| 1910 | * length, so we have garbage in the buffer between the |
| 1911 | * payload and the end of the buffer (p+len). We can't know |
| 1912 | * if the SSL stack is patched, and we don't know if we can |
| 1913 | * safely wipe out the area between p+3+len and payload. |
| 1914 | * So instead, we prevent the response from being sent by |
| 1915 | * setting the max_send_fragment to 0 and we report an SSL |
| 1916 | * error, which will kill this connection. It will be reported |
| 1917 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1918 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1919 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1920 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1921 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1922 | return; |
| 1923 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1924 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1925 | if (global_ssl.capture_cipherlist > 0) |
| 1926 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1927 | } |
| 1928 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1929 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1930 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1931 | const unsigned char *in, unsigned int inlen, |
| 1932 | void *arg) |
| 1933 | { |
| 1934 | struct server *srv = arg; |
| 1935 | |
| 1936 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1937 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1938 | return SSL_TLSEXT_ERR_OK; |
| 1939 | return SSL_TLSEXT_ERR_NOACK; |
| 1940 | } |
| 1941 | #endif |
| 1942 | |
| 1943 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1944 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1945 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1946 | */ |
| 1947 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1948 | unsigned int *len, void *arg) |
| 1949 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1950 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1951 | |
| 1952 | *data = (const unsigned char *)conf->npn_str; |
| 1953 | *len = conf->npn_len; |
| 1954 | return SSL_TLSEXT_ERR_OK; |
| 1955 | } |
| 1956 | #endif |
| 1957 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1958 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1959 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1960 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1961 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1962 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1963 | unsigned char *outlen, |
| 1964 | const unsigned char *server, |
| 1965 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1966 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1967 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1968 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1969 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1970 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1971 | return SSL_TLSEXT_ERR_NOACK; |
| 1972 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1973 | return SSL_TLSEXT_ERR_OK; |
| 1974 | } |
| 1975 | #endif |
| 1976 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1977 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1978 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1979 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1980 | /* Create a X509 certificate with the specified servername and serial. This |
| 1981 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1982 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1983 | 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] | 1984 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1985 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1986 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1987 | SSL_CTX *ssl_ctx = NULL; |
| 1988 | X509 *newcrt = NULL; |
| 1989 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1990 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1991 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1992 | X509_NAME *name; |
| 1993 | const EVP_MD *digest; |
| 1994 | X509V3_CTX ctx; |
| 1995 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1996 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1997 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1998 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1999 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2000 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 2001 | #else |
| 2002 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 2003 | if (tmp_ssl) |
| 2004 | pkey = SSL_get_privatekey(tmp_ssl); |
| 2005 | #endif |
| 2006 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2007 | goto mkcert_error; |
| 2008 | |
| 2009 | /* Create the certificate */ |
| 2010 | if (!(newcrt = X509_new())) |
| 2011 | goto mkcert_error; |
| 2012 | |
| 2013 | /* Set version number for the certificate (X509v3) and the serial |
| 2014 | * number */ |
| 2015 | if (X509_set_version(newcrt, 2L) != 1) |
| 2016 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 2017 | 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] | 2018 | |
| 2019 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 2020 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 2021 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2022 | goto mkcert_error; |
| 2023 | |
| 2024 | /* set public key in the certificate */ |
| 2025 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 2026 | goto mkcert_error; |
| 2027 | |
| 2028 | /* Set issuer name from the CA */ |
| 2029 | if (!(name = X509_get_subject_name(cacert))) |
| 2030 | goto mkcert_error; |
| 2031 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 2032 | goto mkcert_error; |
| 2033 | |
| 2034 | /* Set the subject name using the same, but the CN */ |
| 2035 | name = X509_NAME_dup(name); |
| 2036 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 2037 | (const unsigned char *)servername, |
| 2038 | -1, -1, 0) != 1) { |
| 2039 | X509_NAME_free(name); |
| 2040 | goto mkcert_error; |
| 2041 | } |
| 2042 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 2043 | X509_NAME_free(name); |
| 2044 | goto mkcert_error; |
| 2045 | } |
| 2046 | X509_NAME_free(name); |
| 2047 | |
| 2048 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2049 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2050 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 2051 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 2052 | X509_EXTENSION *ext; |
| 2053 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2054 | 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] | 2055 | goto mkcert_error; |
| 2056 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2057 | X509_EXTENSION_free(ext); |
| 2058 | goto mkcert_error; |
| 2059 | } |
| 2060 | X509_EXTENSION_free(ext); |
| 2061 | } |
| 2062 | |
| 2063 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2064 | |
| 2065 | key_type = EVP_PKEY_base_id(capkey); |
| 2066 | |
| 2067 | if (key_type == EVP_PKEY_DSA) |
| 2068 | digest = EVP_sha1(); |
| 2069 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2070 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2071 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2072 | digest = EVP_sha256(); |
| 2073 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2074 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2075 | int nid; |
| 2076 | |
| 2077 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2078 | goto mkcert_error; |
| 2079 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2080 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2081 | #else |
| 2082 | goto mkcert_error; |
| 2083 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2086 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2087 | goto mkcert_error; |
| 2088 | |
| 2089 | /* Create and set the new SSL_CTX */ |
| 2090 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2091 | goto mkcert_error; |
| 2092 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2093 | goto mkcert_error; |
| 2094 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2095 | goto mkcert_error; |
| 2096 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2097 | goto mkcert_error; |
| 2098 | |
| 2099 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2100 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2101 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2102 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2103 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2104 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2105 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2106 | 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] | 2107 | EC_KEY *ecc; |
| 2108 | int nid; |
| 2109 | |
| 2110 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2111 | goto end; |
| 2112 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2113 | goto end; |
| 2114 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2115 | EC_KEY_free(ecc); |
| 2116 | } |
| 2117 | #endif |
| 2118 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2119 | return ssl_ctx; |
| 2120 | |
| 2121 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2122 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2123 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2124 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2125 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2126 | return NULL; |
| 2127 | } |
| 2128 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2129 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2130 | 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] | 2131 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2132 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2133 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2134 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2135 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2138 | /* 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] | 2139 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2140 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2141 | 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] | 2142 | { |
| 2143 | struct lru64 *lru = NULL; |
| 2144 | |
| 2145 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2146 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2147 | 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] | 2148 | if (lru && lru->domain) { |
| 2149 | if (ssl) |
| 2150 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2151 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2152 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2153 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2154 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2155 | } |
| 2156 | return NULL; |
| 2157 | } |
| 2158 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2159 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2160 | * function is not thread-safe, it should only be used to check if a certificate |
| 2161 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2162 | * thread). It is kept for backward compatibility. */ |
| 2163 | SSL_CTX * |
| 2164 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2165 | { |
| 2166 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2167 | } |
| 2168 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2169 | /* Set a certificate int the LRU cache used to store generated |
| 2170 | * certificate. Return 0 on success, otherwise -1 */ |
| 2171 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2172 | 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] | 2173 | { |
| 2174 | struct lru64 *lru = NULL; |
| 2175 | |
| 2176 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2177 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2178 | 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] | 2179 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2180 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2181 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2182 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2183 | if (lru->domain && lru->data) |
| 2184 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2185 | 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] | 2186 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2187 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2188 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2189 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2192 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2193 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2194 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2195 | { |
| 2196 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2197 | } |
| 2198 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2199 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2200 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2201 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2202 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2203 | 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] | 2204 | { |
| 2205 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2206 | SSL_CTX *ssl_ctx = NULL; |
| 2207 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2208 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2209 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2210 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2211 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2212 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2213 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2214 | if (lru && lru->domain) |
| 2215 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2216 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2217 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2218 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2219 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2220 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2221 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2222 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2223 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2224 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2225 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2226 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2227 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2228 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2229 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2230 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | static int |
| 2234 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2235 | { |
| 2236 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2237 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2238 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2239 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2240 | key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2241 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2242 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2243 | } |
| 2244 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2245 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2246 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2247 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2248 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2249 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2250 | |
| 2251 | 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] | 2252 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2253 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2254 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2255 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2256 | #endif |
| 2257 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2258 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2259 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2260 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2261 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2262 | 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] | 2263 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2264 | 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] | 2265 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2266 | #endif |
| 2267 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2268 | 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] | 2269 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2270 | 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] | 2271 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2272 | #endif |
| 2273 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2274 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2275 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2276 | /* Unusable in this context. */ |
| 2277 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2278 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2279 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2280 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2281 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2282 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2283 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2284 | |
| 2285 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2286 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2287 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2288 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2289 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2290 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2291 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2292 | } |
| 2293 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2294 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2295 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2296 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2297 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2298 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2299 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2300 | } |
| 2301 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2302 | 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] | 2303 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2304 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2305 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2306 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2307 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2308 | } |
| 2309 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2310 | 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] | 2311 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2312 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2313 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2314 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2315 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2316 | } |
| 2317 | 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] | 2318 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2319 | 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] | 2320 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2321 | #endif |
| 2322 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2323 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2324 | #if SSL_OP_NO_TLSv1_3 |
| 2325 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2326 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2327 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2328 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2329 | #endif |
| 2330 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2331 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2332 | |
| 2333 | static struct { |
| 2334 | int option; |
| 2335 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2336 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2337 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2338 | const char *name; |
| 2339 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2340 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2341 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2342 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2343 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2344 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2345 | {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] | 2346 | }; |
| 2347 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2348 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2349 | { |
| 2350 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2351 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2352 | SSL_set_SSL_CTX(ssl, ctx); |
| 2353 | } |
| 2354 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2355 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | |
| 2357 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2358 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2359 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2360 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2362 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2363 | return SSL_TLSEXT_ERR_OK; |
| 2364 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2367 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2368 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2369 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2370 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2371 | #else |
| 2372 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2373 | { |
| 2374 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2375 | struct connection *conn; |
| 2376 | struct bind_conf *s; |
| 2377 | const uint8_t *extension_data; |
| 2378 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2379 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2380 | |
| 2381 | char *wildp = NULL; |
| 2382 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2383 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2384 | 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] | 2385 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2386 | int i; |
| 2387 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2388 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2389 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2391 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2392 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2393 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2394 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2395 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2396 | #else |
| 2397 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2398 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2399 | /* |
| 2400 | * The server_name extension was given too much extensibility when it |
| 2401 | * was written, so parsing the normal case is a bit complex. |
| 2402 | */ |
| 2403 | size_t len; |
| 2404 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2405 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2406 | /* Extract the length of the supplied list of names. */ |
| 2407 | len = (*extension_data++) << 8; |
| 2408 | len |= *extension_data++; |
| 2409 | if (len + 2 != extension_len) |
| 2410 | goto abort; |
| 2411 | /* |
| 2412 | * The list in practice only has a single element, so we only consider |
| 2413 | * the first one. |
| 2414 | */ |
| 2415 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2416 | goto abort; |
| 2417 | extension_len = len - 1; |
| 2418 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2419 | if (extension_len <= 2) |
| 2420 | goto abort; |
| 2421 | len = (*extension_data++) << 8; |
| 2422 | len |= *extension_data++; |
| 2423 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2424 | || memchr(extension_data, 0, len) != NULL) |
| 2425 | goto abort; |
| 2426 | servername = extension_data; |
| 2427 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2428 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2429 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2430 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2431 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2432 | } |
| 2433 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2434 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2435 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2436 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2437 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2438 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2439 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2440 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2441 | goto abort; |
| 2442 | } |
| 2443 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2444 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2445 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2446 | 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] | 2447 | #else |
| 2448 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2449 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2450 | uint8_t sign; |
| 2451 | size_t len; |
| 2452 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2453 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2454 | len = (*extension_data++) << 8; |
| 2455 | len |= *extension_data++; |
| 2456 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2457 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2458 | if (len % 2 != 0) |
| 2459 | goto abort; |
| 2460 | for (; len > 0; len -= 2) { |
| 2461 | extension_data++; /* hash */ |
| 2462 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2463 | switch (sign) { |
| 2464 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2465 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | case TLSEXT_signature_ecdsa: |
| 2468 | has_ecdsa_sig = 1; |
| 2469 | break; |
| 2470 | default: |
| 2471 | continue; |
| 2472 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2473 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2474 | break; |
| 2475 | } |
| 2476 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2477 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2478 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2479 | } |
| 2480 | 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] | 2481 | const SSL_CIPHER *cipher; |
| 2482 | size_t len; |
| 2483 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2484 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2485 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2486 | len = ctx->cipher_suites_len; |
| 2487 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2488 | #else |
| 2489 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2490 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2491 | if (len % 2 != 0) |
| 2492 | goto abort; |
| 2493 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2494 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2495 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2496 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2497 | #else |
| 2498 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2499 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2500 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2501 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2502 | break; |
| 2503 | } |
| 2504 | } |
| 2505 | } |
| 2506 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2507 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2508 | trash.area[i] = tolower(servername[i]); |
| 2509 | if (!wildp && (trash.area[i] == '.')) |
| 2510 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2511 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2512 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2513 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2514 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2515 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2516 | for (i = 0; i < 2; i++) { |
| 2517 | if (i == 0) /* lookup in full qualified names */ |
| 2518 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2519 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2520 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2521 | else |
| 2522 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2523 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2524 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2525 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2526 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2527 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2528 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2530 | break; |
| 2531 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2532 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2533 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2534 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2535 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2536 | if (!node_anonymous) |
| 2537 | node_anonymous = n; |
| 2538 | break; |
| 2539 | } |
| 2540 | } |
| 2541 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2542 | /* select by key_signature priority order */ |
| 2543 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2544 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2545 | : (node_anonymous ? node_anonymous |
| 2546 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2547 | : node_rsa /* no rsa signature case (far far away) */ |
| 2548 | ))); |
| 2549 | if (node) { |
| 2550 | /* switch ctx */ |
| 2551 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2552 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2553 | if (conf) { |
| 2554 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2555 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2556 | if (conf->early_data) |
| 2557 | allow_early = 1; |
| 2558 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2559 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2560 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2561 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2562 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2563 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2564 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2565 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2566 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2567 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2568 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2569 | } |
| 2570 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2571 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2572 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2573 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2574 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2575 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2576 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2577 | allow_early: |
| 2578 | #ifdef OPENSSL_IS_BORINGSSL |
| 2579 | if (allow_early) |
| 2580 | SSL_set_early_data_enabled(ssl, 1); |
| 2581 | #else |
| 2582 | if (!allow_early) |
| 2583 | SSL_set_max_early_data(ssl, 0); |
| 2584 | #endif |
| 2585 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2586 | abort: |
| 2587 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2588 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2589 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2590 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2591 | #else |
| 2592 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2593 | return 0; |
| 2594 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2598 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2599 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2600 | * warning when no match is found, which implies the default (first) cert |
| 2601 | * will keep being used. |
| 2602 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2603 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2604 | { |
| 2605 | const char *servername; |
| 2606 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2607 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2608 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2609 | int i; |
| 2610 | (void)al; /* shut gcc stupid warning */ |
| 2611 | |
| 2612 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2613 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2614 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2615 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2616 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2617 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2618 | if (s->strict_sni) |
| 2619 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2620 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2621 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2622 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2623 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2624 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2625 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2626 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2627 | if (!servername[i]) |
| 2628 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2629 | trash.area[i] = tolower(servername[i]); |
| 2630 | if (!wildp && (trash.area[i] == '.')) |
| 2631 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2632 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2633 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2634 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2635 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2636 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2637 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2638 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2639 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2640 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2641 | node = n; |
| 2642 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2643 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2644 | } |
| 2645 | if (!node && wildp) { |
| 2646 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2647 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2648 | /* lookup a not neg filter */ |
| 2649 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2650 | node = n; |
| 2651 | break; |
| 2652 | } |
| 2653 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2654 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2655 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2656 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2657 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2658 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2659 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2660 | return SSL_TLSEXT_ERR_OK; |
| 2661 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2662 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2663 | if (s->strict_sni) { |
| 2664 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2665 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2666 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2667 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2668 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2669 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2673 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2674 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2675 | return SSL_TLSEXT_ERR_OK; |
| 2676 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2677 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2678 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2679 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2680 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2681 | |
| 2682 | static DH * ssl_get_dh_1024(void) |
| 2683 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2684 | static unsigned char dh1024_p[]={ |
| 2685 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2686 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2687 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2688 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2689 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2690 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2691 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2692 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2693 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2694 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2695 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2696 | }; |
| 2697 | static unsigned char dh1024_g[]={ |
| 2698 | 0x02, |
| 2699 | }; |
| 2700 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2701 | BIGNUM *p; |
| 2702 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2703 | DH *dh = DH_new(); |
| 2704 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2705 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2706 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2707 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2708 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2709 | DH_free(dh); |
| 2710 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2711 | } else { |
| 2712 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | return dh; |
| 2716 | } |
| 2717 | |
| 2718 | static DH *ssl_get_dh_2048(void) |
| 2719 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2720 | static unsigned char dh2048_p[]={ |
| 2721 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2722 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2723 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2724 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2725 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2726 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2727 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2728 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2729 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2730 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2731 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2732 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2733 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2734 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2735 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2736 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2737 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2738 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2739 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2740 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2741 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2742 | 0xB7,0x1F,0x77,0xF3, |
| 2743 | }; |
| 2744 | static unsigned char dh2048_g[]={ |
| 2745 | 0x02, |
| 2746 | }; |
| 2747 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2748 | BIGNUM *p; |
| 2749 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2750 | DH *dh = DH_new(); |
| 2751 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2752 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2753 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2754 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2755 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2756 | DH_free(dh); |
| 2757 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2758 | } else { |
| 2759 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | return dh; |
| 2763 | } |
| 2764 | |
| 2765 | static DH *ssl_get_dh_4096(void) |
| 2766 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2767 | static unsigned char dh4096_p[]={ |
| 2768 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2769 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2770 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2771 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2772 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2773 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2774 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2775 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2776 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2777 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2778 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2779 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2780 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2781 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2782 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2783 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2784 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2785 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2786 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2787 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2788 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2789 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2790 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2791 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2792 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2793 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2794 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2795 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2796 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2797 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2798 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2799 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2800 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2801 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2802 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2803 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2804 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2805 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2806 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2807 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2808 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2809 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2810 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2811 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2812 | static unsigned char dh4096_g[]={ |
| 2813 | 0x02, |
| 2814 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2815 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2816 | BIGNUM *p; |
| 2817 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2818 | DH *dh = DH_new(); |
| 2819 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2820 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2821 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2822 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2823 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2824 | DH_free(dh); |
| 2825 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2826 | } else { |
| 2827 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | return dh; |
| 2831 | } |
| 2832 | |
| 2833 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2834 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2835 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2836 | { |
| 2837 | DH *dh = NULL; |
| 2838 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2839 | int type; |
| 2840 | |
| 2841 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2842 | |
| 2843 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2844 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2845 | */ |
| 2846 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2847 | keylen = EVP_PKEY_bits(pkey); |
| 2848 | } |
| 2849 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2850 | if (keylen > global_ssl.default_dh_param) { |
| 2851 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2854 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2855 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2856 | } |
| 2857 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2858 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2859 | } |
| 2860 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2861 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | return dh; |
| 2865 | } |
| 2866 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2867 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2868 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2869 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2870 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2871 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2872 | if (in == NULL) |
| 2873 | goto end; |
| 2874 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2875 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2876 | goto end; |
| 2877 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2878 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2879 | |
| 2880 | end: |
| 2881 | if (in) |
| 2882 | BIO_free(in); |
| 2883 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2884 | ERR_clear_error(); |
| 2885 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2886 | return dh; |
| 2887 | } |
| 2888 | |
| 2889 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2890 | { |
| 2891 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2892 | |
| 2893 | if (global_dh) { |
| 2894 | return 0; |
| 2895 | } |
| 2896 | |
| 2897 | return -1; |
| 2898 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2899 | #endif |
| 2900 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2901 | /* Alloc and init a ckch_inst */ |
| 2902 | static struct ckch_inst *ckch_inst_new() |
| 2903 | { |
| 2904 | struct ckch_inst *ckch_inst; |
| 2905 | |
| 2906 | ckch_inst = calloc(1, sizeof *ckch_inst); |
| 2907 | if (ckch_inst) |
| 2908 | LIST_INIT(&ckch_inst->sni_ctx); |
| 2909 | |
| 2910 | return ckch_inst; |
| 2911 | } |
| 2912 | |
| 2913 | |
| 2914 | /* This function allocates a sni_ctx and adds it to the ckch_inst */ |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2915 | static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst, |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2916 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2917 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2918 | { |
| 2919 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2920 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2921 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2922 | if (*name == '!') { |
| 2923 | neg = 1; |
| 2924 | name++; |
| 2925 | } |
| 2926 | if (*name == '*') { |
| 2927 | wild = 1; |
| 2928 | name++; |
| 2929 | } |
| 2930 | /* !* filter is a nop */ |
| 2931 | if (neg && wild) |
| 2932 | return order; |
| 2933 | if (*name) { |
| 2934 | int j, len; |
| 2935 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2936 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2937 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2938 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2939 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2940 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2941 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2942 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2943 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2944 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2945 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2946 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2947 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2948 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2949 | sc->order = order++; |
| 2950 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2951 | sc->wild = wild; |
| 2952 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 2953 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2954 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2955 | } |
| 2956 | return order; |
| 2957 | } |
| 2958 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2959 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2960 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2961 | * This function can't return an error. |
| 2962 | * |
| 2963 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2964 | */ |
| 2965 | static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
| 2966 | { |
| 2967 | |
| 2968 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2969 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2970 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2971 | |
| 2972 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2973 | |
| 2974 | /* ignore if sc0 was already inserted in a tree */ |
| 2975 | if (sc0->name.node.leaf_p) |
| 2976 | continue; |
| 2977 | |
| 2978 | /* Check for duplicates. */ |
| 2979 | if (sc0->wild) |
| 2980 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2981 | else |
| 2982 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2983 | |
| 2984 | for (; node; node = ebmb_next_dup(node)) { |
| 2985 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2986 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2987 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2988 | /* it's a duplicate, we should remove and free it */ |
| 2989 | LIST_DEL(&sc0->by_ckch_inst); |
| 2990 | free(sc0); |
| 2991 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2992 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | /* if duplicate, ignore the insertion */ |
| 2997 | if (!sc0) |
| 2998 | continue; |
| 2999 | |
| 3000 | if (sc0->wild) |
| 3001 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3002 | else |
| 3003 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3004 | |
| 3005 | /* replace the default_ctx if required with the first ctx */ |
| 3006 | if (ckch_inst->is_default && !def) { |
| 3007 | /* we don't need to free the default_ctx because the refcount was not incremented */ |
| 3008 | bind_conf->default_ctx = sc0->ctx; |
| 3009 | def = 1; |
| 3010 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3015 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3016 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3017 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3018 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3019 | /* tree of crtlist (crt-list/directory) */ |
| 3020 | static struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3021 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3022 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3023 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3024 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3025 | * DH parameter available in ckchs nor in global, the default |
| 3026 | * DH parameters are applied on the SSL_CTX. |
| 3027 | * Returns a bitfield containing the flags: |
| 3028 | * ERR_FATAL in any fatal error case |
| 3029 | * ERR_ALERT if a reason of the error is availabine in err |
| 3030 | * ERR_WARN if a warning is available into err |
| 3031 | * The value 0 means there is no error nor warning and |
| 3032 | * the operation succeed. |
| 3033 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3034 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3035 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 3036 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3037 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3038 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3039 | DH *dh = NULL; |
| 3040 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 3041 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3042 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3043 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 3044 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3045 | err && *err ? *err : "", path); |
| 3046 | #if defined(SSL_CTX_set_dh_auto) |
| 3047 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3048 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3049 | err && *err ? *err : ""); |
| 3050 | #else |
| 3051 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3052 | err && *err ? *err : ""); |
| 3053 | #endif |
| 3054 | ret |= ERR_WARN; |
| 3055 | goto end; |
| 3056 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3057 | |
| 3058 | if (ssl_dh_ptr_index >= 0) { |
| 3059 | /* store a pointer to the DH params to avoid complaining about |
| 3060 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3061 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3062 | } |
| 3063 | } |
| 3064 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3065 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 3066 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3067 | err && *err ? *err : "", path); |
| 3068 | #if defined(SSL_CTX_set_dh_auto) |
| 3069 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3070 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3071 | err && *err ? *err : ""); |
| 3072 | #else |
| 3073 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3074 | err && *err ? *err : ""); |
| 3075 | #endif |
| 3076 | ret |= ERR_WARN; |
| 3077 | goto end; |
| 3078 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3079 | } |
| 3080 | else { |
| 3081 | /* Clear openssl global errors stack */ |
| 3082 | ERR_clear_error(); |
| 3083 | |
| 3084 | if (global_ssl.default_dh_param <= 1024) { |
| 3085 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3086 | if (local_dh_1024 == NULL) |
| 3087 | local_dh_1024 = ssl_get_dh_1024(); |
| 3088 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3089 | if (local_dh_1024 == NULL) { |
| 3090 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3091 | err && *err ? *err : "", path); |
| 3092 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3093 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3094 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3095 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3096 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3097 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3098 | err && *err ? *err : "", path); |
| 3099 | #if defined(SSL_CTX_set_dh_auto) |
| 3100 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3101 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3102 | err && *err ? *err : ""); |
| 3103 | #else |
| 3104 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3105 | err && *err ? *err : ""); |
| 3106 | #endif |
| 3107 | ret |= ERR_WARN; |
| 3108 | goto end; |
| 3109 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3110 | } |
| 3111 | else { |
| 3112 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3113 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | end: |
William Lallemand | 4dd145a | 2020-02-05 11:46:33 +0100 | [diff] [blame] | 3117 | ERR_clear_error(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3118 | return ret; |
| 3119 | } |
| 3120 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3121 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3122 | /* Frees the contents of a cert_key_and_chain |
| 3123 | */ |
| 3124 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 3125 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3126 | if (!ckch) |
| 3127 | return; |
| 3128 | |
| 3129 | /* Free the certificate and set pointer to NULL */ |
| 3130 | if (ckch->cert) |
| 3131 | X509_free(ckch->cert); |
| 3132 | ckch->cert = NULL; |
| 3133 | |
| 3134 | /* Free the key and set pointer to NULL */ |
| 3135 | if (ckch->key) |
| 3136 | EVP_PKEY_free(ckch->key); |
| 3137 | ckch->key = NULL; |
| 3138 | |
| 3139 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3140 | if (ckch->chain) |
| 3141 | sk_X509_pop_free(ckch->chain, X509_free); |
| 3142 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3143 | |
William Lallemand | 455af50 | 2019-10-17 18:04:45 +0200 | [diff] [blame] | 3144 | if (ckch->dh) |
| 3145 | DH_free(ckch->dh); |
| 3146 | ckch->dh = NULL; |
| 3147 | |
| 3148 | if (ckch->sctl) { |
| 3149 | free(ckch->sctl->area); |
| 3150 | ckch->sctl->area = NULL; |
| 3151 | free(ckch->sctl); |
| 3152 | ckch->sctl = NULL; |
| 3153 | } |
| 3154 | |
| 3155 | if (ckch->ocsp_response) { |
| 3156 | free(ckch->ocsp_response->area); |
| 3157 | ckch->ocsp_response->area = NULL; |
| 3158 | free(ckch->ocsp_response); |
| 3159 | ckch->ocsp_response = NULL; |
| 3160 | } |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3161 | |
| 3162 | if (ckch->ocsp_issuer) |
William Lallemand | dad239d | 2020-01-23 11:59:02 +0100 | [diff] [blame] | 3163 | X509_free(ckch->ocsp_issuer); |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3164 | ckch->ocsp_issuer = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3165 | } |
| 3166 | |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3167 | /* |
| 3168 | * |
| 3169 | * This function copy a cert_key_and_chain in memory |
| 3170 | * |
| 3171 | * It's used to try to apply changes on a ckch before committing them, because |
| 3172 | * most of the time it's not possible to revert those changes |
| 3173 | * |
| 3174 | * Return a the dst or NULL |
| 3175 | */ |
| 3176 | static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 3177 | struct cert_key_and_chain *dst) |
| 3178 | { |
| 3179 | if (src->cert) { |
| 3180 | dst->cert = src->cert; |
| 3181 | X509_up_ref(src->cert); |
| 3182 | } |
| 3183 | |
| 3184 | if (src->key) { |
| 3185 | dst->key = src->key; |
| 3186 | EVP_PKEY_up_ref(src->key); |
| 3187 | } |
| 3188 | |
| 3189 | if (src->chain) { |
| 3190 | dst->chain = X509_chain_up_ref(src->chain); |
| 3191 | } |
| 3192 | |
| 3193 | if (src->dh) { |
| 3194 | DH_up_ref(src->dh); |
| 3195 | dst->dh = src->dh; |
| 3196 | } |
| 3197 | |
| 3198 | if (src->sctl) { |
| 3199 | struct buffer *sctl; |
| 3200 | |
| 3201 | sctl = calloc(1, sizeof(*sctl)); |
| 3202 | if (!chunk_dup(sctl, src->sctl)) { |
| 3203 | free(sctl); |
| 3204 | sctl = NULL; |
| 3205 | goto error; |
| 3206 | } |
| 3207 | dst->sctl = sctl; |
| 3208 | } |
| 3209 | |
| 3210 | if (src->ocsp_response) { |
| 3211 | struct buffer *ocsp_response; |
| 3212 | |
| 3213 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 3214 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 3215 | free(ocsp_response); |
| 3216 | ocsp_response = NULL; |
| 3217 | goto error; |
| 3218 | } |
| 3219 | dst->ocsp_response = ocsp_response; |
| 3220 | } |
| 3221 | |
| 3222 | if (src->ocsp_issuer) { |
| 3223 | X509_up_ref(src->ocsp_issuer); |
| 3224 | dst->ocsp_issuer = src->ocsp_issuer; |
| 3225 | } |
| 3226 | |
| 3227 | return dst; |
| 3228 | |
| 3229 | error: |
| 3230 | |
| 3231 | /* free everything */ |
| 3232 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 3233 | |
| 3234 | return NULL; |
| 3235 | } |
| 3236 | |
| 3237 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3238 | /* checks if a key and cert exists in the ckch |
| 3239 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3240 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3241 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 3242 | { |
| 3243 | return (ckch->cert != NULL && ckch->key != NULL); |
| 3244 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3245 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3246 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3247 | /* |
| 3248 | * return 0 on success or != 0 on failure |
| 3249 | */ |
| 3250 | static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 3251 | { |
| 3252 | int ret = 1; |
| 3253 | BIO *in = NULL; |
| 3254 | X509 *issuer; |
| 3255 | |
| 3256 | if (buf) { |
| 3257 | /* reading from a buffer */ |
| 3258 | in = BIO_new_mem_buf(buf, -1); |
| 3259 | if (in == NULL) { |
| 3260 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3261 | goto end; |
| 3262 | } |
| 3263 | |
| 3264 | } else { |
| 3265 | /* reading from a file */ |
| 3266 | in = BIO_new(BIO_s_file()); |
| 3267 | if (in == NULL) |
| 3268 | goto end; |
| 3269 | |
| 3270 | if (BIO_read_filename(in, path) <= 0) |
| 3271 | goto end; |
| 3272 | } |
| 3273 | |
| 3274 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3275 | if (!issuer) { |
| 3276 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3277 | err && *err ? *err : "", path); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3278 | goto end; |
| 3279 | } |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3280 | /* no error, fill ckch with new context, old context must be free */ |
| 3281 | if (ckch->ocsp_issuer) |
| 3282 | X509_free(ckch->ocsp_issuer); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3283 | ckch->ocsp_issuer = issuer; |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3284 | ret = 0; |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3285 | |
| 3286 | end: |
| 3287 | |
| 3288 | ERR_clear_error(); |
| 3289 | if (in) |
| 3290 | BIO_free(in); |
| 3291 | |
| 3292 | return ret; |
| 3293 | } |
| 3294 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3295 | |
| 3296 | /* |
| 3297 | * Try to load a PEM file from a <path> or a buffer <buf> |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3298 | * The PEM must contain at least a Certificate, |
| 3299 | * It could contain a DH, a certificate chain and a PrivateKey. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3300 | * |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3301 | * If it failed you should not attempt to use the ckch but free it. |
| 3302 | * |
| 3303 | * Return 0 on success or != 0 on failure |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3304 | */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3305 | static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3306 | { |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3307 | BIO *in = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3308 | int ret = 1; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3309 | X509 *ca; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3310 | X509 *cert = NULL; |
| 3311 | EVP_PKEY *key = NULL; |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3312 | DH *dh = NULL; |
| 3313 | STACK_OF(X509) *chain = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3314 | |
| 3315 | if (buf) { |
| 3316 | /* reading from a buffer */ |
| 3317 | in = BIO_new_mem_buf(buf, -1); |
| 3318 | if (in == NULL) { |
| 3319 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3320 | goto end; |
| 3321 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3322 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3323 | } else { |
| 3324 | /* reading from a file */ |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3325 | in = BIO_new(BIO_s_file()); |
| 3326 | if (in == NULL) |
| 3327 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3328 | |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3329 | if (BIO_read_filename(in, path) <= 0) |
| 3330 | goto end; |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3331 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3332 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3333 | /* Read Private Key */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3334 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3335 | /* no need to check for errors here, because the private key could be loaded later */ |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3336 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3337 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3338 | /* Seek back to beginning of file */ |
| 3339 | if (BIO_reset(in) == -1) { |
| 3340 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3341 | err && *err ? *err : "", path); |
| 3342 | goto end; |
| 3343 | } |
| 3344 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3345 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 3346 | /* no need to return an error there, dh is not mandatory */ |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3347 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3348 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3349 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3350 | if (BIO_reset(in) == -1) { |
| 3351 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3352 | err && *err ? *err : "", path); |
| 3353 | goto end; |
| 3354 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3355 | |
| 3356 | /* Read Certificate */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3357 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3358 | if (cert == NULL) { |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3359 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3360 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3361 | goto end; |
| 3362 | } |
| 3363 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3364 | /* Look for a Certificate Chain */ |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3365 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3366 | if (chain == NULL) |
| 3367 | chain = sk_X509_new_null(); |
| 3368 | if (!sk_X509_push(chain, ca)) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3369 | X509_free(ca); |
| 3370 | goto end; |
| 3371 | } |
| 3372 | } |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3373 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3374 | ret = ERR_get_error(); |
| 3375 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3376 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3377 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3378 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3379 | } |
| 3380 | |
William Lallemand | 75b15f7 | 2020-01-23 10:56:05 +0100 | [diff] [blame] | 3381 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 3382 | if (ckch->ocsp_response) { |
| 3383 | free(ckch->ocsp_response->area); |
| 3384 | ckch->ocsp_response->area = NULL; |
| 3385 | free(ckch->ocsp_response); |
| 3386 | ckch->ocsp_response = NULL; |
| 3387 | } |
| 3388 | |
| 3389 | if (ckch->sctl) { |
| 3390 | free(ckch->sctl->area); |
| 3391 | ckch->sctl->area = NULL; |
| 3392 | free(ckch->sctl); |
| 3393 | ckch->sctl = NULL; |
| 3394 | } |
| 3395 | |
| 3396 | if (ckch->ocsp_issuer) { |
| 3397 | X509_free(ckch->ocsp_issuer); |
| 3398 | ckch->ocsp_issuer = NULL; |
| 3399 | } |
| 3400 | |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3401 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 3402 | SWAP(ckch->key, key); |
| 3403 | SWAP(ckch->dh, dh); |
| 3404 | SWAP(ckch->cert, cert); |
| 3405 | SWAP(ckch->chain, chain); |
| 3406 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3407 | ret = 0; |
| 3408 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3409 | end: |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3410 | |
| 3411 | ERR_clear_error(); |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3412 | if (in) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3413 | BIO_free(in); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3414 | if (key) |
| 3415 | EVP_PKEY_free(key); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3416 | if (dh) |
| 3417 | DH_free(dh); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3418 | if (cert) |
| 3419 | X509_free(cert); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3420 | if (chain) |
| 3421 | sk_X509_pop_free(chain, X509_free); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3422 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3423 | return ret; |
| 3424 | } |
| 3425 | |
| 3426 | /* |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3427 | * Try to load a private key file from a <path> or a buffer <buf> |
| 3428 | * |
| 3429 | * If it failed you should not attempt to use the ckch but free it. |
| 3430 | * |
| 3431 | * Return 0 on success or != 0 on failure |
| 3432 | */ |
| 3433 | static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 3434 | { |
| 3435 | BIO *in = NULL; |
| 3436 | int ret = 1; |
| 3437 | EVP_PKEY *key = NULL; |
| 3438 | |
| 3439 | if (buf) { |
| 3440 | /* reading from a buffer */ |
| 3441 | in = BIO_new_mem_buf(buf, -1); |
| 3442 | if (in == NULL) { |
| 3443 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3444 | goto end; |
| 3445 | } |
| 3446 | |
| 3447 | } else { |
| 3448 | /* reading from a file */ |
| 3449 | in = BIO_new(BIO_s_file()); |
| 3450 | if (in == NULL) |
| 3451 | goto end; |
| 3452 | |
| 3453 | if (BIO_read_filename(in, path) <= 0) |
| 3454 | goto end; |
| 3455 | } |
| 3456 | |
| 3457 | /* Read Private Key */ |
| 3458 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3459 | if (key == NULL) { |
| 3460 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3461 | err && *err ? *err : "", path); |
| 3462 | goto end; |
| 3463 | } |
| 3464 | |
| 3465 | ret = 0; |
| 3466 | |
| 3467 | SWAP(ckch->key, key); |
| 3468 | |
| 3469 | end: |
| 3470 | |
| 3471 | ERR_clear_error(); |
| 3472 | if (in) |
| 3473 | BIO_free(in); |
| 3474 | if (key) |
| 3475 | EVP_PKEY_free(key); |
| 3476 | |
| 3477 | return ret; |
| 3478 | } |
| 3479 | |
| 3480 | /* |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3481 | * Try to load in a ckch every files related to a ckch. |
| 3482 | * (PEM, sctl, ocsp, issuer etc.) |
| 3483 | * |
| 3484 | * This function is only used to load files during the configuration parsing, |
| 3485 | * it is not used with the CLI. |
| 3486 | * |
| 3487 | * This allows us to carry the contents of the file without having to read the |
| 3488 | * file multiple times. The caller must call |
| 3489 | * ssl_sock_free_cert_key_and_chain_contents. |
| 3490 | * |
| 3491 | * returns: |
| 3492 | * 0 on Success |
| 3493 | * 1 on SSL Failure |
| 3494 | */ |
| 3495 | static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3496 | { |
| 3497 | int ret = 1; |
| 3498 | |
| 3499 | /* try to load the PEM */ |
| 3500 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 3501 | goto end; |
| 3502 | } |
| 3503 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3504 | /* try to load an external private key if it wasn't in the PEM */ |
| 3505 | if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) { |
| 3506 | char fp[MAXPATHLEN+1]; |
| 3507 | struct stat st; |
| 3508 | |
| 3509 | snprintf(fp, MAXPATHLEN+1, "%s.key", path); |
| 3510 | if (stat(fp, &st) == 0) { |
| 3511 | if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) { |
| 3512 | memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n", |
| 3513 | err && *err ? *err : "", fp); |
| 3514 | goto end; |
| 3515 | } |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | if (ckch->key == NULL) { |
| 3520 | memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path); |
| 3521 | goto end; |
| 3522 | } |
| 3523 | |
| 3524 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 3525 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 3526 | err && *err ? *err : "", path); |
| 3527 | goto end; |
| 3528 | } |
| 3529 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3530 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3531 | /* try to load the sctl file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3532 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3533 | char fp[MAXPATHLEN+1]; |
| 3534 | struct stat st; |
| 3535 | |
| 3536 | snprintf(fp, MAXPATHLEN+1, "%s.sctl", path); |
| 3537 | if (stat(fp, &st) == 0) { |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 3538 | if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3539 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3540 | err && *err ? *err : "", fp); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3541 | ret = 1; |
| 3542 | goto end; |
| 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3547 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3548 | /* try to load an ocsp response file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3549 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3550 | char fp[MAXPATHLEN+1]; |
| 3551 | struct stat st; |
| 3552 | |
| 3553 | snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path); |
| 3554 | if (stat(fp, &st) == 0) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 3555 | if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3556 | ret = 1; |
| 3557 | goto end; |
| 3558 | } |
| 3559 | } |
| 3560 | } |
| 3561 | |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3562 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3563 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3564 | /* if no issuer was found, try to load an issuer from the .issuer */ |
Emmanuel Hocdet | 078156d | 2020-01-22 17:02:53 +0100 | [diff] [blame] | 3565 | if (!ckch->ocsp_issuer) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3566 | struct stat st; |
| 3567 | char fp[MAXPATHLEN+1]; |
| 3568 | |
| 3569 | snprintf(fp, MAXPATHLEN+1, "%s.issuer", path); |
| 3570 | if (stat(fp, &st) == 0) { |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3571 | if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3572 | ret = 1; |
| 3573 | goto end; |
| 3574 | } |
| 3575 | |
| 3576 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
William Lallemand | 786188f | 2019-10-15 10:05:37 +0200 | [diff] [blame] | 3577 | memprintf(err, "%s '%s' is not an issuer'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3578 | err && *err ? *err : "", fp); |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3579 | ret = 1; |
| 3580 | goto end; |
| 3581 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | } |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3585 | #endif |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3586 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3587 | ret = 0; |
| 3588 | |
| 3589 | end: |
| 3590 | |
| 3591 | ERR_clear_error(); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3592 | |
| 3593 | /* Something went wrong in one of the reads */ |
| 3594 | if (ret != 0) |
| 3595 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3596 | |
| 3597 | return ret; |
| 3598 | } |
| 3599 | |
| 3600 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3601 | * Returns a bitfield containing the flags: |
| 3602 | * ERR_FATAL in any fatal error case |
| 3603 | * ERR_ALERT if the reason of the error is available in err |
| 3604 | * ERR_WARN if a warning is available into err |
| 3605 | * The value 0 means there is no error nor warning and |
| 3606 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3607 | */ |
| 3608 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3609 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3610 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3611 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3612 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3613 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3614 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3615 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3616 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3617 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3621 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3622 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3623 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3624 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3625 | } |
| 3626 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3627 | if (ckch->chain) { |
| 3628 | find_chain = ckch->chain; |
| 3629 | } else { |
| 3630 | /* Find Certificate Chain in global */ |
| 3631 | struct issuer_chain *issuer; |
| 3632 | issuer = ssl_get_issuer_chain(ckch->cert); |
| 3633 | if (issuer) |
| 3634 | find_chain = issuer->chain; |
| 3635 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3636 | |
| 3637 | /* If we didn't find a chain we *MUST* use an empty X509 structure */ |
| 3638 | if (find_chain == NULL) |
| 3639 | find_chain = sk_X509_new_null(); |
| 3640 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3641 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3642 | #ifdef SSL_CTX_set1_chain |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3643 | if (!SSL_CTX_set1_chain(ctx, find_chain)) { |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3644 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3645 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3646 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3647 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3648 | } |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3649 | #else |
| 3650 | { /* legacy compat (< openssl 1.0.2) */ |
| 3651 | X509 *ca; |
Emmanuel Hocdet | 140b64f | 2019-10-24 18:33:10 +0200 | [diff] [blame] | 3652 | STACK_OF(X509) *chain; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3653 | chain = X509_chain_up_ref(find_chain); |
Emmanuel Hocdet | 140b64f | 2019-10-24 18:33:10 +0200 | [diff] [blame] | 3654 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3655 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3656 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3657 | err && *err ? *err : "", path); |
| 3658 | X509_free(ca); |
Emmanuel Hocdet | 140b64f | 2019-10-24 18:33:10 +0200 | [diff] [blame] | 3659 | sk_X509_pop_free(chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3660 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3661 | goto end; |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3662 | } |
| 3663 | } |
| 3664 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3665 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3666 | #ifndef OPENSSL_NO_DH |
| 3667 | /* store a NULL pointer to indicate we have not yet loaded |
| 3668 | a custom DH param file */ |
| 3669 | if (ssl_dh_ptr_index >= 0) { |
| 3670 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3671 | } |
| 3672 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3673 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3674 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3675 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3676 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3677 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3678 | } |
| 3679 | #endif |
| 3680 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3681 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3682 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3683 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3684 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3685 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3686 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3687 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3688 | } |
| 3689 | } |
| 3690 | #endif |
| 3691 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 3692 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3693 | /* Load OCSP Info into context */ |
| 3694 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 3695 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3696 | 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", |
| 3697 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3698 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3699 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3700 | } |
| 3701 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3702 | #endif |
| 3703 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3704 | end: |
| 3705 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3706 | } |
| 3707 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3708 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3709 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3710 | 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] | 3711 | { |
| 3712 | struct sni_keytype *s_kt = NULL; |
| 3713 | struct ebmb_node *node; |
| 3714 | int i; |
| 3715 | |
| 3716 | for (i = 0; i < trash.size; i++) { |
| 3717 | if (!str[i]) |
| 3718 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3719 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3720 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3721 | trash.area[i] = 0; |
| 3722 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3723 | if (!node) { |
| 3724 | /* CN not found in tree */ |
| 3725 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3726 | /* Using memcpy here instead of strncpy. |
| 3727 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3728 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3729 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3730 | if (!s_kt) |
| 3731 | return -1; |
| 3732 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3733 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3734 | s_kt->keytypes = 0; |
| 3735 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3736 | } else { |
| 3737 | /* CN found in tree */ |
| 3738 | s_kt = container_of(node, struct sni_keytype, name); |
| 3739 | } |
| 3740 | |
| 3741 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3742 | s_kt->keytypes |= 1<<key_index; |
| 3743 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3744 | return 0; |
| 3745 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3746 | } |
| 3747 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3748 | #endif |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3749 | /* |
| 3750 | * Free a ckch_store and its ckch(s) |
| 3751 | * The linked ckch_inst are not free'd |
| 3752 | */ |
| 3753 | void ckchs_free(struct ckch_store *ckchs) |
| 3754 | { |
| 3755 | if (!ckchs) |
| 3756 | return; |
| 3757 | |
| 3758 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3759 | if (ckchs->multi) { |
| 3760 | int n; |
| 3761 | |
| 3762 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3763 | ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]); |
| 3764 | } else |
| 3765 | #endif |
| 3766 | { |
| 3767 | ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch); |
| 3768 | ckchs->ckch = NULL; |
| 3769 | } |
| 3770 | |
| 3771 | free(ckchs); |
| 3772 | } |
| 3773 | |
| 3774 | /* allocate and duplicate a ckch_store |
| 3775 | * Return a new ckch_store or NULL */ |
| 3776 | static struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 3777 | { |
| 3778 | struct ckch_store *dst; |
| 3779 | int pathlen; |
| 3780 | |
| 3781 | pathlen = strlen(src->path); |
| 3782 | dst = calloc(1, sizeof(*dst) + pathlen + 1); |
| 3783 | if (!dst) |
| 3784 | return NULL; |
| 3785 | /* copy previous key */ |
| 3786 | memcpy(dst->path, src->path, pathlen + 1); |
| 3787 | dst->multi = src->multi; |
| 3788 | LIST_INIT(&dst->ckch_inst); |
| 3789 | |
| 3790 | dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch)); |
| 3791 | if (!dst->ckch) |
| 3792 | goto error; |
| 3793 | |
| 3794 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3795 | if (src->multi) { |
| 3796 | int n; |
| 3797 | |
| 3798 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3799 | if (&src->ckch[n]) { |
| 3800 | if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n])) |
| 3801 | goto error; |
| 3802 | } |
| 3803 | } |
| 3804 | } else |
| 3805 | #endif |
| 3806 | { |
| 3807 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 3808 | goto error; |
| 3809 | } |
| 3810 | |
| 3811 | return dst; |
| 3812 | |
| 3813 | error: |
| 3814 | ckchs_free(dst); |
| 3815 | |
| 3816 | return NULL; |
| 3817 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3818 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3819 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3820 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3821 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3822 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3823 | { |
| 3824 | struct ebmb_node *eb; |
| 3825 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3826 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3827 | if (!eb) |
| 3828 | return NULL; |
| 3829 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3830 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3834 | * This function allocate a ckch_store and populate it with certificates from files. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3835 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3836 | static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3837 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3838 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3839 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3840 | ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1); |
| 3841 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3842 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3843 | goto end; |
| 3844 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3845 | ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1)); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3846 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3847 | if (!ckchs->ckch) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3848 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3849 | goto end; |
| 3850 | } |
| 3851 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3852 | LIST_INIT(&ckchs->ckch_inst); |
| 3853 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3854 | if (!multi) { |
| 3855 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3856 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3857 | goto end; |
| 3858 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3859 | /* insert into the ckchs tree */ |
| 3860 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3861 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3862 | } else { |
| 3863 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3864 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3865 | char fp[MAXPATHLEN+1] = {0}; |
| 3866 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* Load all possible certs and keys */ |
| 3869 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3870 | struct stat buf; |
| 3871 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3872 | if (stat(fp, &buf) == 0) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3873 | if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3874 | goto end; |
| 3875 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3876 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3877 | } |
| 3878 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3879 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3880 | |
| 3881 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 3882 | memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3883 | goto end; |
| 3884 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3885 | /* insert into the ckchs tree */ |
| 3886 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3887 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3888 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3889 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3890 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3891 | end: |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3892 | if (ckchs) { |
| 3893 | free(ckchs->ckch); |
| 3894 | ebmb_delete(&ckchs->node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3895 | } |
| 3896 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3897 | free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3898 | |
| 3899 | return NULL; |
| 3900 | } |
| 3901 | |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 3902 | |
| 3903 | /* |
| 3904 | * Free a sni filters array generated by ckch_inst_sni_ctx_to_sni_filters() |
| 3905 | */ |
| 3906 | static inline void free_sni_filters(char **sni_filter, int fcount) |
| 3907 | { |
| 3908 | int i; |
| 3909 | |
| 3910 | if (sni_filter) { |
| 3911 | for (i = 0; i < fcount; i++) { |
| 3912 | if (sni_filter[i]) { |
| 3913 | free(sni_filter[i]); |
| 3914 | sni_filter[i] = NULL; |
| 3915 | } |
| 3916 | } |
| 3917 | free(sni_filter); |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | /* |
| 3922 | * Fill <*sni_filter> with an allocated array of ptr to the existing filters, |
| 3923 | * The caller should free <*sni_filter>. |
| 3924 | * Fill <*fcount> with the number of filters |
| 3925 | * Return an ERR_* code. |
| 3926 | */ |
| 3927 | static int ckch_inst_sni_ctx_to_sni_filters(const struct ckch_inst *ckchi, char ***sni_filter, int *fcount, char **err) |
| 3928 | { |
| 3929 | struct sni_ctx *sc0; |
| 3930 | int errcode = 0; |
| 3931 | int i = 0; |
| 3932 | char **tmp_filter; |
| 3933 | int tmp_fcount = 0; |
| 3934 | |
| 3935 | list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) { |
| 3936 | tmp_fcount++; |
| 3937 | } |
| 3938 | |
| 3939 | if (!tmp_fcount) |
| 3940 | goto end; |
| 3941 | |
Tim Duesterhus | 8c12025 | 2020-03-19 16:12:09 +0100 | [diff] [blame] | 3942 | tmp_filter = calloc(tmp_fcount, sizeof(*tmp_filter)); |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 3943 | if (!tmp_filter) { |
| 3944 | errcode |= ERR_FATAL|ERR_ALERT; |
| 3945 | goto error; |
| 3946 | } |
| 3947 | |
| 3948 | list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) { |
| 3949 | size_t len = strlen((char *)sc0->name.key); |
| 3950 | |
| 3951 | /* we need to alloc and copy to insert a '!' or/and a '*' */ |
| 3952 | tmp_filter[i] = calloc(1, len + sc0->neg + sc0->wild + 1); |
| 3953 | if (!tmp_filter[i]) { |
| 3954 | errcode |= ERR_FATAL|ERR_ALERT; |
| 3955 | goto error; |
| 3956 | } |
| 3957 | |
| 3958 | if (sc0->neg) |
| 3959 | *tmp_filter[i] = '!'; |
| 3960 | if (sc0->wild) |
| 3961 | *(tmp_filter[i] + sc0->neg) = '*'; |
| 3962 | |
| 3963 | memcpy(tmp_filter[i] + sc0->neg + sc0->wild, (char *)sc0->name.key, len + 1); |
| 3964 | i++; |
| 3965 | } |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 3966 | *sni_filter = tmp_filter; |
Willy Tarreau | d04a2a6 | 2020-03-05 16:26:12 +0100 | [diff] [blame] | 3967 | end: |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 3968 | *fcount = tmp_fcount; |
| 3969 | |
| 3970 | return errcode; |
| 3971 | error: |
| 3972 | memprintf(err, "%sUnable to generate filters!", |
| 3973 | err && *err ? *err : ""); |
| 3974 | free_sni_filters(tmp_filter, tmp_fcount); |
| 3975 | |
| 3976 | return errcode; |
| 3977 | } |
| 3978 | |
| 3979 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3980 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3981 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3982 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3983 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3984 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3985 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3986 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3987 | * 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] | 3988 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3989 | * Returns a bitfield containing the flags: |
| 3990 | * ERR_FATAL in any fatal error case |
| 3991 | * ERR_ALERT if the reason of the error is available in err |
| 3992 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3993 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3994 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3995 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3996 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3997 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3998 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3999 | int i = 0, n = 0; |
| 4000 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 4001 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4002 | struct ebmb_node *node; |
| 4003 | struct ebmb_node *next; |
| 4004 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 4005 | * of keytypes |
| 4006 | */ |
| 4007 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4008 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4009 | X509_NAME *xname = NULL; |
| 4010 | char *str = NULL; |
| 4011 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4012 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 4013 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4014 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4015 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4016 | *ckchi = NULL; |
| 4017 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4018 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4019 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4020 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4021 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | ckch_inst = ckch_inst_new(); |
| 4025 | if (!ckch_inst) { |
| 4026 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4027 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4028 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4029 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4030 | } |
| 4031 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4032 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4033 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4034 | /* Process each ckch and update keytypes for each CN/SAN |
| 4035 | * for example, if CN/SAN www.a.com is associated with |
| 4036 | * certs with keytype 0 and 2, then at the end of the loop, |
| 4037 | * www.a.com will have: |
| 4038 | * keyindex = 0 | 1 | 4 = 5 |
| 4039 | */ |
| 4040 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4041 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4042 | |
| 4043 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 4044 | continue; |
| 4045 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4046 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4047 | for (i = 0; i < fcount; i++) { |
| 4048 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 4049 | if (ret < 0) { |
| 4050 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4051 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4052 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4053 | goto end; |
| 4054 | } |
| 4055 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4056 | } else { |
| 4057 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 4058 | * so the line that contains logic is marked via comments |
| 4059 | */ |
| 4060 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 4061 | i = -1; |
| 4062 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4063 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4064 | ASN1_STRING *value; |
| 4065 | value = X509_NAME_ENTRY_get_data(entry); |
| 4066 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4067 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4068 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4069 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4070 | OPENSSL_free(str); |
| 4071 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4072 | if (ret < 0) { |
| 4073 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4074 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4075 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4076 | goto end; |
| 4077 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4078 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4079 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4080 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4081 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4082 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4083 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 4084 | if (names) { |
| 4085 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4086 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4087 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4088 | if (name->type == GEN_DNS) { |
| 4089 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 4090 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4091 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4092 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4093 | OPENSSL_free(str); |
| 4094 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4095 | if (ret < 0) { |
| 4096 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4097 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4098 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4099 | goto end; |
| 4100 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4101 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 4107 | } |
| 4108 | |
| 4109 | /* If no files found, return error */ |
| 4110 | if (eb_is_empty(&sni_keytypes_map)) { |
| 4111 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4112 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4113 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4114 | goto end; |
| 4115 | } |
| 4116 | |
| 4117 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 4118 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 4119 | * and add each CTX to the SNI tree |
| 4120 | * |
| 4121 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4122 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4123 | * combination is denoted by the key in the map. Each key |
| 4124 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 4125 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 4126 | * entry in the array to correspond to the unique combo (key) |
| 4127 | * associated with i. This unique key combo (i) will be associated |
| 4128 | * with combos[i-1] |
| 4129 | */ |
| 4130 | |
| 4131 | node = ebmb_first(&sni_keytypes_map); |
| 4132 | while (node) { |
| 4133 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4134 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4135 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4136 | |
| 4137 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 4138 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 4139 | cur_ctx = key_combos[i-1].ctx; |
| 4140 | |
| 4141 | if (cur_ctx == NULL) { |
| 4142 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4143 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4144 | if (cur_ctx == NULL) { |
| 4145 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4146 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4147 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4148 | goto end; |
| 4149 | } |
| 4150 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 4151 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4152 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4153 | if (i & (1<<n)) { |
| 4154 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4155 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4156 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 4157 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4158 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4162 | /* Update key_combos */ |
| 4163 | key_combos[i-1].ctx = cur_ctx; |
| 4164 | } |
| 4165 | |
| 4166 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4167 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4168 | key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf, |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4169 | kinfo, str, key_combos[i-1].order); |
| 4170 | if (key_combos[i-1].order < 0) { |
| 4171 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4172 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4173 | goto end; |
| 4174 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4175 | node = ebmb_next(node); |
| 4176 | } |
| 4177 | |
| 4178 | |
| 4179 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 4180 | if (!bind_conf->default_ctx) { |
| 4181 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 4182 | if (key_combos[i].ctx) { |
| 4183 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4184 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4185 | ckch_inst->is_default = 1; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4186 | break; |
| 4187 | } |
| 4188 | } |
| 4189 | } |
| 4190 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4191 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4192 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4193 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 4194 | ckch_inst->filters = !!fcount; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4195 | end: |
| 4196 | |
| 4197 | if (names) |
| 4198 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 4199 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4200 | node = ebmb_first(&sni_keytypes_map); |
| 4201 | while (node) { |
| 4202 | next = ebmb_next(node); |
| 4203 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 4204 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4205 | node = next; |
| 4206 | } |
| 4207 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4208 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4209 | struct sni_ctx *sc0, *sc0b; |
| 4210 | |
| 4211 | /* free the SSL_CTX in case of error */ |
| 4212 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 4213 | if (key_combos[i].ctx) |
| 4214 | SSL_CTX_free(key_combos[i].ctx); |
| 4215 | } |
| 4216 | |
| 4217 | /* free the sni_ctx in case of error */ |
| 4218 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 4219 | |
| 4220 | ebmb_delete(&sc0->name); |
| 4221 | LIST_DEL(&sc0->by_ckch_inst); |
| 4222 | free(sc0); |
| 4223 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4224 | free(ckch_inst); |
| 4225 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4228 | *ckchi = ckch_inst; |
| 4229 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4230 | } |
| 4231 | #else |
| 4232 | /* This is a dummy, that just logs an error and returns error */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4233 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4234 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4235 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4236 | { |
| 4237 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4238 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4239 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4240 | } |
| 4241 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4242 | #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] | 4243 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4244 | /* |
| 4245 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4246 | * |
| 4247 | * Returns a bitfield containing the flags: |
| 4248 | * ERR_FATAL in any fatal error case |
| 4249 | * ERR_ALERT if the reason of the error is available in err |
| 4250 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4251 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4252 | static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
| 4253 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4254 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4255 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4256 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4257 | int order = 0; |
| 4258 | X509_NAME *xname; |
| 4259 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4260 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4261 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4262 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4263 | STACK_OF(GENERAL_NAME) *names; |
| 4264 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4265 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4266 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4267 | int errcode = 0; |
| 4268 | |
| 4269 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 4270 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4271 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4272 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4273 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4274 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4275 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4276 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 4277 | if (!ctx) { |
| 4278 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4279 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4280 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4281 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4284 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 4285 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4286 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4287 | |
| 4288 | ckch_inst = ckch_inst_new(); |
| 4289 | if (!ckch_inst) { |
| 4290 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4291 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4292 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4293 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4296 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4297 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4298 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4299 | switch(EVP_PKEY_base_id(pkey)) { |
| 4300 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4301 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4302 | break; |
| 4303 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4304 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 4305 | break; |
| 4306 | case EVP_PKEY_DSA: |
| 4307 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4308 | break; |
| 4309 | } |
| 4310 | EVP_PKEY_free(pkey); |
| 4311 | } |
| 4312 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4313 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4314 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4315 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4316 | if (order < 0) { |
| 4317 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4318 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4319 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4320 | } |
| 4321 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4322 | } |
| 4323 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4324 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4325 | names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4326 | if (names) { |
| 4327 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4328 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 4329 | if (name->type == GEN_DNS) { |
| 4330 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4331 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4332 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4333 | if (order < 0) { |
| 4334 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4335 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4336 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4337 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4338 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4339 | } |
| 4340 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4341 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4342 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4343 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4344 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4345 | i = -1; |
| 4346 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4347 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4348 | ASN1_STRING *value; |
| 4349 | |
| 4350 | value = X509_NAME_ENTRY_get_data(entry); |
| 4351 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4352 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4353 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4354 | if (order < 0) { |
| 4355 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4356 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4357 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4358 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4359 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4360 | } |
| 4361 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4362 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4363 | * the tree, so it will be discovered and cleaned in time. |
| 4364 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4365 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4366 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4367 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4368 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4369 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4370 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4371 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4372 | } |
| 4373 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4374 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4375 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4376 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4377 | ckch_inst->is_default = 1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4378 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4379 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4380 | /* everything succeed, the ckch instance can be used */ |
| 4381 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4382 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4383 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 4384 | ckch_inst->filters = !!fcount; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4385 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4386 | *ckchi = ckch_inst; |
| 4387 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4388 | |
| 4389 | error: |
| 4390 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4391 | if (ckch_inst) { |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4392 | struct sni_ctx *sc0, *sc0b; |
| 4393 | |
| 4394 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 4395 | |
| 4396 | ebmb_delete(&sc0->name); |
| 4397 | LIST_DEL(&sc0->by_ckch_inst); |
| 4398 | free(sc0); |
| 4399 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4400 | free(ckch_inst); |
| 4401 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4402 | } |
| 4403 | /* We only created 1 SSL_CTX so we can free it there */ |
| 4404 | SSL_CTX_free(ctx); |
| 4405 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4406 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4407 | } |
| 4408 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4409 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4410 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4411 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4412 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4413 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4414 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4415 | |
| 4416 | /* we found the ckchs in the tree, we can use it directly */ |
| 4417 | if (ckchs->multi) |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4418 | errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4419 | else |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4420 | errcode |= ckch_inst_new_load_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4421 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4422 | if (errcode & ERR_CODE) |
| 4423 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4424 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4425 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4426 | |
| 4427 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4428 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4429 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4430 | } |
| 4431 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4432 | |
| 4433 | /* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure |
| 4434 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4435 | * |
| 4436 | * This function tries to open and store certificate files. |
| 4437 | */ |
| 4438 | static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4439 | { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4440 | struct crtlist *dir; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4441 | struct dirent **de_list; |
| 4442 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4443 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 4444 | char *end; |
| 4445 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4446 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4447 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4448 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4449 | int is_bundle; |
| 4450 | int j; |
| 4451 | #endif |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4452 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4453 | /* strip trailing slashes, including first one */ |
| 4454 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 4455 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4456 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4457 | dir = malloc(sizeof(*dir) + strlen(path) + 1); |
| 4458 | if (dir == NULL) { |
| 4459 | memprintf(err, "not enough memory"); |
| 4460 | return ERR_ALERT | ERR_FATAL; |
| 4461 | } |
| 4462 | memcpy(dir->node.key, path, strlen(path) + 1); |
| 4463 | dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */ |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4464 | LIST_INIT(&dir->ord_entries); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4465 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4466 | n = scandir(path, &de_list, 0, alphasort); |
| 4467 | if (n < 0) { |
| 4468 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 4469 | err && *err ? *err : "", path, strerror(errno)); |
| 4470 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4471 | } |
| 4472 | else { |
| 4473 | for (i = 0; i < n; i++) { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4474 | struct crtlist_entry *entry; |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4475 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 4476 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4477 | end = strrchr(de->d_name, '.'); |
| 4478 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key"))) |
| 4479 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4480 | |
William Lallemand | a64593c | 2020-03-17 20:11:41 +0100 | [diff] [blame] | 4481 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 4482 | if (stat(fp, &buf) != 0) { |
| 4483 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4484 | err && *err ? *err : "", fp, strerror(errno)); |
| 4485 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4486 | goto ignore_entry; |
| 4487 | } |
| 4488 | if (!S_ISREG(buf.st_mode)) |
| 4489 | goto ignore_entry; |
| 4490 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4491 | entry = malloc(sizeof(*entry)); |
| 4492 | if (entry == NULL) { |
| 4493 | memprintf(err, "not enough memory '%s'", fp); |
| 4494 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4495 | goto ignore_entry; |
| 4496 | } |
| 4497 | |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4498 | /* directories don't use ssl_conf and filters */ |
| 4499 | entry->fcount = 0; |
| 4500 | entry->filters = NULL; |
| 4501 | entry->ssl_conf = NULL; |
| 4502 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4503 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4504 | is_bundle = 0; |
| 4505 | /* Check if current entry in directory is part of a multi-cert bundle */ |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4506 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4507 | if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) { |
| 4508 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 4509 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 4510 | is_bundle = 1; |
| 4511 | break; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4512 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4513 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4514 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4515 | if (is_bundle) { |
| 4516 | int dp_len; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4517 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4518 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4519 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4520 | /* increment i and free de until we get to a non-bundle cert |
| 4521 | * Note here that we look at de_list[i + 1] before freeing de |
| 4522 | * this is important since ignore_entry will free de. This also |
| 4523 | * guarantees that de->d_name continues to hold the same prefix. |
| 4524 | */ |
| 4525 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
| 4526 | free(de); |
| 4527 | i++; |
| 4528 | de = de_list[i]; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4529 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4530 | |
| 4531 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4532 | ckchs = ckchs_lookup(fp); |
| 4533 | if (ckchs == NULL) |
| 4534 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 4535 | if (ckchs == NULL) { |
| 4536 | free(de); |
| 4537 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4538 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4539 | goto end; |
| 4540 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4541 | entry->node.key = ckchs; |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4542 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4543 | ebpt_insert(&dir->entries, &entry->node); |
| 4544 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4545 | /* Successfully processed the bundle */ |
| 4546 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4547 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4548 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4549 | |
| 4550 | #endif |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4551 | ckchs = ckchs_lookup(fp); |
| 4552 | if (ckchs == NULL) |
| 4553 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 4554 | if (ckchs == NULL) { |
| 4555 | free(de); |
| 4556 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4557 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4558 | goto end; |
| 4559 | } |
| 4560 | entry->node.key = ckchs; |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4561 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4562 | ebpt_insert(&dir->entries, &entry->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4563 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4564 | ignore_entry: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4565 | free(de); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4566 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4567 | end: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4568 | free(de_list); |
| 4569 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4570 | |
| 4571 | if (cfgerr & ERR_CODE) { |
| 4572 | /* free the dir and entries on error */ |
| 4573 | struct ebpt_node *node; |
| 4574 | |
| 4575 | node = ebpt_first(&dir->entries); |
| 4576 | while (node) { |
| 4577 | struct crtlist_entry *entry; |
| 4578 | |
| 4579 | entry = ebpt_entry(node, typeof(*entry), node); |
| 4580 | node = ebpt_next(node); |
| 4581 | ebpt_delete(&entry->node); |
| 4582 | free(entry); |
| 4583 | } |
| 4584 | free(dir); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4585 | } else { |
| 4586 | *crtlist = dir; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4587 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4588 | return cfgerr; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4589 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4590 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4591 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4592 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4593 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4594 | * done once. Zero is returned if the operation fails. No error is returned |
| 4595 | * if the random is said as not implemented, because we expect that openssl |
| 4596 | * will use another method once needed. |
| 4597 | */ |
| 4598 | static int ssl_initialize_random() |
| 4599 | { |
| 4600 | unsigned char random; |
| 4601 | static int random_initialized = 0; |
| 4602 | |
| 4603 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4604 | random_initialized = 1; |
| 4605 | |
| 4606 | return random_initialized; |
| 4607 | } |
| 4608 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4609 | /* release ssl bind conf */ |
| 4610 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4611 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4612 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4613 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4614 | free(conf->npn_str); |
| 4615 | conf->npn_str = NULL; |
| 4616 | #endif |
| 4617 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4618 | free(conf->alpn_str); |
| 4619 | conf->alpn_str = NULL; |
| 4620 | #endif |
| 4621 | free(conf->ca_file); |
| 4622 | conf->ca_file = NULL; |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4623 | free(conf->ca_verify_file); |
| 4624 | conf->ca_verify_file = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4625 | free(conf->crl_file); |
| 4626 | conf->crl_file = NULL; |
| 4627 | free(conf->ciphers); |
| 4628 | conf->ciphers = NULL; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4629 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4630 | free(conf->ciphersuites); |
| 4631 | conf->ciphersuites = NULL; |
| 4632 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4633 | free(conf->curves); |
| 4634 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4635 | free(conf->ecdhe); |
| 4636 | conf->ecdhe = NULL; |
| 4637 | } |
| 4638 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4639 | /* free sni filters */ |
| 4640 | static void crtlist_free_filters(char **args) |
| 4641 | { |
| 4642 | int i; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4643 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4644 | if (!args) |
| 4645 | return; |
| 4646 | |
| 4647 | for (i = 0; args[i]; i++) |
| 4648 | free(args[i]); |
| 4649 | |
| 4650 | free(args); |
| 4651 | } |
| 4652 | |
| 4653 | /* Alloc and duplicate a char ** array */ |
| 4654 | static char **crtlist_dup_filters(char **args, int fcount) |
| 4655 | { |
| 4656 | char **dst; |
| 4657 | int i; |
| 4658 | |
William Lallemand | 3c516fc | 2020-03-20 10:04:34 +0100 | [diff] [blame] | 4659 | if (fcount == 0) |
| 4660 | return NULL; |
| 4661 | |
Tim Duesterhus | 2445f8d | 2020-03-19 16:12:10 +0100 | [diff] [blame] | 4662 | dst = calloc(fcount + 1, sizeof(*dst)); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4663 | if (!dst) |
| 4664 | return NULL; |
| 4665 | |
| 4666 | for (i = 0; i < fcount; i++) { |
| 4667 | dst[i] = strdup(args[i]); |
| 4668 | if (!dst[i]) |
| 4669 | goto error; |
| 4670 | } |
| 4671 | return dst; |
| 4672 | |
| 4673 | error: |
| 4674 | crtlist_free_filters(dst); |
| 4675 | return NULL; |
| 4676 | } |
| 4677 | |
| 4678 | |
| 4679 | /* Free a crtlist, from the crt_entry to the content of the ssl_conf */ |
| 4680 | static void crtlist_free(struct crtlist *crtlist) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4681 | { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4682 | struct crtlist_entry *entry, *s_entry; |
| 4683 | |
| 4684 | if (crtlist == NULL) |
| 4685 | return; |
| 4686 | |
| 4687 | list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) { |
| 4688 | ebpt_delete(&entry->node); |
| 4689 | LIST_DEL(&entry->by_crtlist); |
| 4690 | crtlist_free_filters(entry->filters); |
| 4691 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 4692 | free(entry->ssl_conf); |
| 4693 | free(entry); |
| 4694 | } |
| 4695 | ebmb_delete(&crtlist->node); |
| 4696 | free(crtlist); |
| 4697 | } |
| 4698 | |
| 4699 | /* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure |
| 4700 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4701 | * |
| 4702 | * This function tries to open and store certificate files. |
| 4703 | */ |
| 4704 | static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err) |
| 4705 | { |
| 4706 | struct crtlist *newlist; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4707 | char thisline[CRT_LINESIZE]; |
| 4708 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4709 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4710 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4711 | int linenum = 0; |
| 4712 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4713 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4714 | if ((f = fopen(file, "r")) == NULL) { |
| 4715 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4716 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4717 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4718 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4719 | newlist = malloc(sizeof(*newlist) + strlen(file) + 1); |
| 4720 | if (newlist == NULL) { |
| 4721 | memprintf(err, "Not enough memory!"); |
| 4722 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4723 | goto error; |
| 4724 | } |
| 4725 | memcpy(newlist->node.key, file, strlen(file) + 1); |
| 4726 | newlist->entries = EB_ROOT; |
| 4727 | LIST_INIT(&newlist->ord_entries); |
| 4728 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4729 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4730 | struct crtlist_entry *entry; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4731 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4732 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4733 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4734 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4735 | char *crt_path; |
| 4736 | struct ssl_bind_conf *ssl_conf = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4737 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4738 | |
| 4739 | linenum++; |
| 4740 | end = line + strlen(line); |
| 4741 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4742 | /* Check if we reached the limit and the last char is not \n. |
| 4743 | * Watch out for the last line without the terminating '\n'! |
| 4744 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4745 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4746 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4747 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4748 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4749 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4750 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4751 | newarg = 1; |
| 4752 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4753 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 4754 | /* end of string, end of loop */ |
| 4755 | *line = 0; |
| 4756 | break; |
Willy Tarreau | ded15b7 | 2020-02-25 07:51:59 +0100 | [diff] [blame] | 4757 | } else if (isspace((unsigned char)*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4758 | newarg = 1; |
| 4759 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4760 | } else if (*line == '[') { |
| 4761 | if (ssl_b) { |
| 4762 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4763 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4764 | break; |
| 4765 | } |
| 4766 | if (!arg) { |
| 4767 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4768 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4769 | break; |
| 4770 | } |
| 4771 | ssl_b = arg; |
| 4772 | newarg = 1; |
| 4773 | *line = 0; |
| 4774 | } else if (*line == ']') { |
| 4775 | if (ssl_e) { |
| 4776 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4777 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4778 | break; |
| 4779 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4780 | if (!ssl_b) { |
| 4781 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4782 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4783 | break; |
| 4784 | } |
| 4785 | ssl_e = arg; |
| 4786 | newarg = 1; |
| 4787 | *line = 0; |
| 4788 | } else if (newarg) { |
| 4789 | if (arg == MAX_CRT_ARGS) { |
| 4790 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4791 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4792 | break; |
| 4793 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4794 | newarg = 0; |
| 4795 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4796 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4797 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4798 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4799 | if (cfgerr) |
| 4800 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4801 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4802 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4803 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4804 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4805 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4806 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4807 | crt_path = args[0]; |
| 4808 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4809 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4810 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4811 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4812 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4813 | break; |
| 4814 | } |
| 4815 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4816 | crt_path = path; |
| 4817 | } |
| 4818 | |
| 4819 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4820 | cur_arg = ssl_b ? ssl_b : 1; |
| 4821 | while (cur_arg < ssl_e) { |
| 4822 | newarg = 0; |
| 4823 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4824 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4825 | newarg = 1; |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4826 | 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] | 4827 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4828 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4829 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4830 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4831 | } |
| 4832 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4833 | break; |
| 4834 | } |
| 4835 | } |
| 4836 | if (!cfgerr && !newarg) { |
| 4837 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4838 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4839 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4840 | break; |
| 4841 | } |
| 4842 | } |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4843 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4844 | /* Look for a ckch_store or create one */ |
| 4845 | ckchs = ckchs_lookup(crt_path); |
| 4846 | if (ckchs == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4847 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4848 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4849 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4850 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4851 | } |
William Lallemand | 909086e | 2020-03-17 16:53:27 +0100 | [diff] [blame] | 4852 | if (ckchs == NULL) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4853 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4854 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4855 | entry = malloc(sizeof(*entry)); |
| 4856 | if (entry == NULL) { |
| 4857 | memprintf(err, "Not enough memory!"); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4858 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4859 | } |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4860 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4861 | if (cfgerr & ERR_CODE) { |
| 4862 | free(entry); |
| 4863 | entry = NULL; |
| 4864 | ssl_sock_free_ssl_conf(ssl_conf); |
| 4865 | free(ssl_conf); |
| 4866 | ssl_conf = NULL; |
| 4867 | goto error; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4868 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4869 | entry->node.key = ckchs; |
| 4870 | entry->ssl_conf = ssl_conf; |
| 4871 | /* filters */ |
| 4872 | entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1); |
| 4873 | entry->fcount = arg - cur_arg - 1; |
| 4874 | ebpt_insert(&newlist->entries, &entry->node); |
| 4875 | LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4876 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4877 | if (cfgerr & ERR_CODE) |
| 4878 | goto error; |
| 4879 | |
| 4880 | fclose(f); |
| 4881 | *crtlist = newlist; |
| 4882 | |
| 4883 | return cfgerr; |
| 4884 | error: |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4885 | fclose(f); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4886 | crtlist_free(newlist); |
| 4887 | return cfgerr; |
| 4888 | } |
| 4889 | |
| 4890 | /* Load a crt-list file, this is done in 2 parts: |
| 4891 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 4892 | * - generate the instances by iterating on entries in the crtlist struct |
| 4893 | * |
| 4894 | * Nothing is locked there, this function is used in the configuration parser. |
| 4895 | * |
| 4896 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4897 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4898 | int ssl_sock_load_cert_list_file(char *file, int dir, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4899 | { |
| 4900 | struct crtlist *crtlist = NULL; |
| 4901 | struct ebmb_node *eb; |
| 4902 | struct crtlist_entry *entry; |
| 4903 | struct list instances; /* temporary list head */ |
| 4904 | int cfgerr = 0; |
| 4905 | |
| 4906 | LIST_INIT(&instances); |
| 4907 | /* look for an existing crtlist or create one */ |
| 4908 | eb = ebst_lookup(&crtlists_tree, file); |
| 4909 | if (eb) { |
| 4910 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 4911 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4912 | /* load a crt-list OR a directory */ |
| 4913 | if (dir) |
| 4914 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 4915 | else |
| 4916 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 4917 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4918 | if (!(cfgerr & ERR_CODE)) |
| 4919 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 4920 | } |
| 4921 | |
| 4922 | if (cfgerr & ERR_CODE) { |
| 4923 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4924 | goto error; |
| 4925 | } |
| 4926 | |
| 4927 | /* generates ckch instance from the crtlist_entry */ |
| 4928 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 4929 | struct ckch_store *store; |
| 4930 | struct ckch_inst *ckch_inst = NULL; |
| 4931 | |
| 4932 | store = entry->node.key; |
| 4933 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 4934 | if (cfgerr & ERR_CODE) { |
| 4935 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 4936 | goto error; |
| 4937 | } |
| 4938 | LIST_ADDQ(&instances, &ckch_inst->by_crtlist_entry); |
| 4939 | } |
| 4940 | /* add the instances to the actual instance list in the crtlist_entry */ |
| 4941 | LIST_SPLICE(&entry->ckch_inst, &instances); |
| 4942 | |
| 4943 | return cfgerr; |
| 4944 | error: |
| 4945 | { |
| 4946 | struct ckch_inst *inst, *s_inst; |
| 4947 | |
| 4948 | list_for_each_entry_safe(inst, s_inst, &instances, by_crtlist_entry) { |
| 4949 | struct sni_ctx *sni, *s_sni; |
| 4950 | |
| 4951 | /* free the sni_ctx */ |
| 4952 | list_for_each_entry_safe(sni, s_sni, &inst->sni_ctx, by_ckch_inst) { |
| 4953 | ebmb_delete(&sni->name); |
| 4954 | LIST_DEL(&sni->by_ckch_inst); |
| 4955 | free(sni); |
| 4956 | } |
| 4957 | LIST_DEL(&inst->by_crtlist_entry); |
| 4958 | free(inst); |
| 4959 | } |
| 4960 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4961 | return cfgerr; |
| 4962 | } |
| 4963 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4964 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 4965 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 4966 | { |
| 4967 | struct stat buf; |
| 4968 | char fp[MAXPATHLEN+1]; |
| 4969 | int cfgerr = 0; |
| 4970 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4971 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4972 | |
| 4973 | if ((ckchs = ckchs_lookup(path))) { |
| 4974 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4975 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4976 | } |
| 4977 | if (stat(path, &buf) == 0) { |
| 4978 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4979 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 4980 | if (!ckchs) |
| 4981 | return ERR_ALERT | ERR_FATAL; |
| 4982 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4983 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4984 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4985 | return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4986 | } |
| 4987 | } else { |
| 4988 | /* stat failed, could be a bundle */ |
| 4989 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 4990 | /* try to load a bundle if it is permitted */ |
| 4991 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 4992 | if (!ckchs) |
| 4993 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4994 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4995 | } else { |
| 4996 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4997 | err && *err ? *err : "", fp, strerror(errno)); |
| 4998 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | return cfgerr; |
| 5003 | } |
| 5004 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5005 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5006 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5007 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5008 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5009 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5010 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5011 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5012 | SSL_OP_NO_SSLv2 | |
| 5013 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 5014 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5015 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5016 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 5017 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5018 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5019 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5020 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5021 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5022 | SSL_MODE_RELEASE_BUFFERS | |
| 5023 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 5024 | 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] | 5025 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5026 | int flags = MC_SSL_O_ALL; |
| 5027 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5028 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5029 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5030 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5031 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5032 | 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] | 5033 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 5034 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5035 | 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] | 5036 | else |
| 5037 | flags = conf_ssl_methods->flags; |
| 5038 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 5039 | min = conf_ssl_methods->min; |
| 5040 | max = conf_ssl_methods->max; |
| 5041 | /* start with TLSv10 to remove SSLv3 per default */ |
| 5042 | if (!min && (!max || max >= CONF_TLSV10)) |
| 5043 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5044 | /* 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] | 5045 | if (min) |
| 5046 | flags |= (methodVersions[min].flag - 1); |
| 5047 | if (max) |
| 5048 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5049 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5050 | min = max = CONF_TLSV_NONE; |
| 5051 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5052 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5053 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5054 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5055 | if (min) { |
| 5056 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5057 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 5058 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5059 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 5060 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5061 | hole = 0; |
| 5062 | } |
| 5063 | max = i; |
| 5064 | } |
| 5065 | else { |
| 5066 | min = max = i; |
| 5067 | } |
| 5068 | } |
| 5069 | else { |
| 5070 | if (min) |
| 5071 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5072 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5073 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5074 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5075 | 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] | 5076 | cfgerr += 1; |
| 5077 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5078 | /* save real min/max in bind_conf */ |
| 5079 | conf_ssl_methods->min = min; |
| 5080 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5081 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5082 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5083 | /* 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] | 5084 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5085 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5086 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5087 | else |
| 5088 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5089 | if (flags & methodVersions[i].flag) |
| 5090 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5091 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5092 | /* 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] | 5093 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5094 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 5095 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5096 | |
| 5097 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 5098 | options |= SSL_OP_NO_TICKET; |
| 5099 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 5100 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 5101 | |
| 5102 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5103 | options |= SSL_OP_NO_RENEGOTIATION; |
| 5104 | #endif |
| 5105 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5106 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5107 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5108 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5109 | if (global_ssl.async) |
| 5110 | mode |= SSL_MODE_ASYNC; |
| 5111 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5112 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5113 | if (global_ssl.life_time) |
| 5114 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5115 | |
| 5116 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5117 | #ifdef OPENSSL_IS_BORINGSSL |
| 5118 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 5119 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 5120 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 5121 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 5122 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 5123 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 5124 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5125 | #else |
| 5126 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5127 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 5128 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5129 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5130 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5131 | } |
| 5132 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5133 | |
| 5134 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 5135 | { |
| 5136 | if (first == block) { |
| 5137 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5138 | if (first->len > 0) |
| 5139 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | /* return first block from sh_ssl_sess */ |
| 5144 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 5145 | { |
| 5146 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 5147 | |
| 5148 | } |
| 5149 | |
| 5150 | /* store a session into the cache |
| 5151 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 5152 | * data: asn1 encoded session |
| 5153 | * data_len: asn1 encoded session length |
| 5154 | * Returns 1 id session was stored (else 0) |
| 5155 | */ |
| 5156 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 5157 | { |
| 5158 | struct shared_block *first; |
| 5159 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 5160 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5161 | 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] | 5162 | if (!first) { |
| 5163 | /* Could not retrieve enough free blocks to store that session */ |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
| 5167 | /* STORE the key in the first elem */ |
| 5168 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5169 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 5170 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5171 | |
| 5172 | /* it returns the already existing node |
| 5173 | or current node if none, never returns null */ |
| 5174 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 5175 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 5176 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 5177 | /* release the reserved row */ |
| 5178 | shctx_row_dec_hot(ssl_shctx, first); |
| 5179 | /* replace the previous session already in the tree */ |
| 5180 | sh_ssl_sess = oldsh_ssl_sess; |
| 5181 | /* ignore the previous session data, only use the header */ |
| 5182 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 5183 | shctx_row_inc_hot(ssl_shctx, first); |
| 5184 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5185 | } |
| 5186 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5187 | 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] | 5188 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5189 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5193 | |
| 5194 | return 1; |
| 5195 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5196 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5197 | /* SSL callback used when a new session is created while connecting to a server */ |
| 5198 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 5199 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5200 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5201 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5202 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5203 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5204 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5205 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 5206 | int len; |
| 5207 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5208 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5209 | len = i2d_SSL_SESSION(sess, NULL); |
| 5210 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 5211 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 5212 | } else { |
| 5213 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5214 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 5215 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 5216 | } |
| 5217 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 5218 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 5219 | &ptr); |
| 5220 | } |
| 5221 | } else { |
| 5222 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5223 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 5224 | } |
| 5225 | |
| 5226 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5227 | } |
| 5228 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5229 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5230 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5231 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5232 | { |
| 5233 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 5234 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 5235 | unsigned char *p; |
| 5236 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5237 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5238 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5239 | |
| 5240 | /* Session id is already stored in to key and session id is known |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5241 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5242 | * note: SSL_SESSION_set1_id is using |
| 5243 | * a memcpy so we need to use a different pointer |
| 5244 | * than sid_data or sid_ctx_data to avoid valgrind |
| 5245 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5246 | */ |
| 5247 | |
| 5248 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5249 | |
| 5250 | /* copy value in an other buffer */ |
| 5251 | memcpy(encid, sid_data, sid_length); |
| 5252 | |
| 5253 | /* pad with 0 */ |
| 5254 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 5255 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 5256 | |
| 5257 | /* force length to zero to avoid ASN1 encoding */ |
| 5258 | SSL_SESSION_set1_id(sess, encid, 0); |
| 5259 | |
| 5260 | /* force length to zero to avoid ASN1 encoding */ |
| 5261 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5262 | |
| 5263 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 5264 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 5265 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 5266 | goto err; |
| 5267 | |
| 5268 | p = encsess; |
| 5269 | |
| 5270 | /* process ASN1 session encoding before the lock */ |
| 5271 | i2d_SSL_SESSION(sess, &p); |
| 5272 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5273 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5274 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5275 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5276 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5277 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5278 | err: |
| 5279 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5280 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 5281 | 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] | 5282 | |
| 5283 | return 0; /* do not increment session reference count */ |
| 5284 | } |
| 5285 | |
| 5286 | /* 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] | 5287 | 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] | 5288 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5289 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5290 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 5291 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5292 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5293 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5294 | |
| 5295 | global.shctx_lookups++; |
| 5296 | |
| 5297 | /* allow the session to be freed automatically by openssl */ |
| 5298 | *do_copy = 0; |
| 5299 | |
| 5300 | /* tree key is zeros padded sessionid */ |
| 5301 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5302 | memcpy(tmpkey, key, key_len); |
| 5303 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 5304 | key = tmpkey; |
| 5305 | } |
| 5306 | |
| 5307 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5308 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5309 | |
| 5310 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5311 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 5312 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5313 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5314 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5315 | global.shctx_misses++; |
| 5316 | return NULL; |
| 5317 | } |
| 5318 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5319 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 5320 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5321 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5322 | 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] | 5323 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5324 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5325 | |
| 5326 | /* decode ASN1 session */ |
| 5327 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5328 | 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] | 5329 | /* Reset session id and session id contenxt */ |
| 5330 | if (sess) { |
| 5331 | SSL_SESSION_set1_id(sess, key, key_len); |
| 5332 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5333 | } |
| 5334 | |
| 5335 | return sess; |
| 5336 | } |
| 5337 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5338 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5339 | /* 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] | 5340 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5341 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5342 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5343 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 5344 | unsigned int sid_length; |
| 5345 | const unsigned char *sid_data; |
| 5346 | (void)ctx; |
| 5347 | |
| 5348 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 5349 | /* tree key is zeros padded sessionid */ |
| 5350 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5351 | memcpy(tmpkey, sid_data, sid_length); |
| 5352 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 5353 | sid_data = tmpkey; |
| 5354 | } |
| 5355 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5356 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5357 | |
| 5358 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5359 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 5360 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5361 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5362 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5366 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | /* Set session cache mode to server and disable openssl internal cache. |
| 5370 | * Set shared cache callbacks on an ssl context. |
| 5371 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5372 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5373 | { |
| 5374 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5375 | |
| 5376 | if (!ssl_shctx) { |
| 5377 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 5378 | return; |
| 5379 | } |
| 5380 | |
| 5381 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 5382 | SSL_SESS_CACHE_NO_INTERNAL | |
| 5383 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 5384 | |
| 5385 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5386 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 5387 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 5388 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5389 | } |
| 5390 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5391 | /* |
| 5392 | * This function applies the SSL configuration on a SSL_CTX |
| 5393 | * It returns an error code and fills the <err> buffer |
| 5394 | */ |
| 5395 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx, char **err) |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5396 | { |
| 5397 | struct proxy *curproxy = bind_conf->frontend; |
| 5398 | int cfgerr = 0; |
| 5399 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5400 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5401 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5402 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5403 | const char *conf_ciphersuites; |
| 5404 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5405 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5406 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5407 | if (ssl_conf) { |
| 5408 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5409 | int i, min, max; |
| 5410 | int flags = MC_SSL_O_ALL; |
| 5411 | |
| 5412 | /* 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] | 5413 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5414 | 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] | 5415 | if (min) |
| 5416 | flags |= (methodVersions[min].flag - 1); |
| 5417 | if (max) |
| 5418 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5419 | min = max = CONF_TLSV_NONE; |
| 5420 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5421 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5422 | if (min) |
| 5423 | max = i; |
| 5424 | else |
| 5425 | min = max = i; |
| 5426 | } |
| 5427 | /* save real min/max */ |
| 5428 | conf_ssl_methods->min = min; |
| 5429 | conf_ssl_methods->max = max; |
| 5430 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5431 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5432 | err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5433 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5434 | } |
| 5435 | } |
| 5436 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5437 | 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] | 5438 | case SSL_SOCK_VERIFY_NONE: |
| 5439 | verify = SSL_VERIFY_NONE; |
| 5440 | break; |
| 5441 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5442 | verify = SSL_VERIFY_PEER; |
| 5443 | break; |
| 5444 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5445 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5446 | break; |
| 5447 | } |
| 5448 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5449 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5450 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5451 | char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5452 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5453 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5454 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5455 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5456 | memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5457 | err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5458 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5459 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5460 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 5461 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 5462 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5463 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5464 | } |
| 5465 | if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 5466 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5467 | SSL_CTX_set_client_CA_list(ctx, SSL_dup_CA_list(ssl_get_client_ca_file(ca_file))); |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 5468 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5469 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5470 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5471 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5472 | err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5473 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5474 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5475 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5476 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5477 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5478 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5479 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5480 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5481 | err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5482 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5483 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5484 | else { |
| 5485 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5486 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5487 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5488 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5489 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5490 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5491 | #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] | 5492 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5493 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5494 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5495 | err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5496 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5497 | } |
| 5498 | } |
| 5499 | #endif |
| 5500 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5501 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5502 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5503 | if (conf_ciphers && |
| 5504 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5505 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5506 | err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5507 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5508 | } |
| 5509 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5510 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5511 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5512 | if (conf_ciphersuites && |
| 5513 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5514 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5515 | err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5516 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5517 | } |
| 5518 | #endif |
| 5519 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5520 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5521 | /* If tune.ssl.default-dh-param has not been set, |
| 5522 | neither has ssl-default-dh-file and no static DH |
| 5523 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5524 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5525 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 5526 | (ssl_dh_ptr_index == -1 || |
| 5527 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5528 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 5529 | const SSL_CIPHER * cipher = NULL; |
| 5530 | char cipher_description[128]; |
| 5531 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 5532 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 5533 | which is not ephemeral DH. */ |
| 5534 | const char dhe_description[] = " Kx=DH "; |
| 5535 | const char dhe_export_description[] = " Kx=DH("; |
| 5536 | int idx = 0; |
| 5537 | int dhe_found = 0; |
| 5538 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5539 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5540 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5541 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5542 | if (ssl) { |
| 5543 | ciphers = SSL_get_ciphers(ssl); |
| 5544 | |
| 5545 | if (ciphers) { |
| 5546 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 5547 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 5548 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 5549 | if (strstr(cipher_description, dhe_description) != NULL || |
| 5550 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 5551 | dhe_found = 1; |
| 5552 | break; |
| 5553 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 5554 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5555 | } |
| 5556 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5557 | SSL_free(ssl); |
| 5558 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5559 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5560 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5561 | if (dhe_found) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5562 | memprintf(err, "%sSetting 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", |
| 5563 | err && *err ? *err : ""); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5564 | cfgerr |= ERR_WARN; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5565 | } |
| 5566 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5567 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5568 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5569 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5570 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5571 | if (local_dh_1024 == NULL) { |
| 5572 | local_dh_1024 = ssl_get_dh_1024(); |
| 5573 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5574 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5575 | if (local_dh_2048 == NULL) { |
| 5576 | local_dh_2048 = ssl_get_dh_2048(); |
| 5577 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5578 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5579 | if (local_dh_4096 == NULL) { |
| 5580 | local_dh_4096 = ssl_get_dh_4096(); |
| 5581 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5582 | } |
| 5583 | } |
| 5584 | } |
| 5585 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5586 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5587 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5588 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5589 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5590 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5591 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5592 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5593 | ssl_conf_cur = NULL; |
| 5594 | if (ssl_conf && ssl_conf->npn_str) |
| 5595 | ssl_conf_cur = ssl_conf; |
| 5596 | else if (bind_conf->ssl_conf.npn_str) |
| 5597 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5598 | if (ssl_conf_cur) |
| 5599 | 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] | 5600 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5601 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5602 | ssl_conf_cur = NULL; |
| 5603 | if (ssl_conf && ssl_conf->alpn_str) |
| 5604 | ssl_conf_cur = ssl_conf; |
| 5605 | else if (bind_conf->ssl_conf.alpn_str) |
| 5606 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5607 | if (ssl_conf_cur) |
| 5608 | 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] | 5609 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 5610 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5611 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5612 | if (conf_curves) { |
| 5613 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5614 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5615 | err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5616 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5617 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5618 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5619 | } |
| 5620 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5621 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5622 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5623 | int i; |
| 5624 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5625 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5626 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5627 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5628 | NULL); |
| 5629 | |
| 5630 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 5631 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5632 | return cfgerr; |
| 5633 | } |
| 5634 | #else |
| 5635 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5636 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5637 | ECDHE_DEFAULT_CURVE); |
| 5638 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5639 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5640 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5641 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5642 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5643 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5644 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5645 | } |
| 5646 | else { |
| 5647 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5648 | EC_KEY_free(ecdh); |
| 5649 | } |
| 5650 | } |
| 5651 | #endif |
| 5652 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5653 | return cfgerr; |
| 5654 | } |
| 5655 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5656 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5657 | { |
| 5658 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5659 | size_t prefixlen, suffixlen; |
| 5660 | |
| 5661 | /* Trivial case */ |
| 5662 | if (strcmp(pattern, hostname) == 0) |
| 5663 | return 1; |
| 5664 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5665 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5666 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5667 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5668 | pattern_wildcard = NULL; |
| 5669 | pattern_left_label_end = pattern; |
| 5670 | while (*pattern_left_label_end != '.') { |
| 5671 | switch (*pattern_left_label_end) { |
| 5672 | case 0: |
| 5673 | /* End of label not found */ |
| 5674 | return 0; |
| 5675 | case '*': |
| 5676 | /* If there is more than one wildcards */ |
| 5677 | if (pattern_wildcard) |
| 5678 | return 0; |
| 5679 | pattern_wildcard = pattern_left_label_end; |
| 5680 | break; |
| 5681 | } |
| 5682 | pattern_left_label_end++; |
| 5683 | } |
| 5684 | |
| 5685 | /* If it's not trivial and there is no wildcard, it can't |
| 5686 | * match */ |
| 5687 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5688 | return 0; |
| 5689 | |
| 5690 | /* Make sure all labels match except the leftmost */ |
| 5691 | hostname_left_label_end = strchr(hostname, '.'); |
| 5692 | if (!hostname_left_label_end |
| 5693 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5694 | return 0; |
| 5695 | |
| 5696 | /* Make sure the leftmost label of the hostname is long enough |
| 5697 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5698 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5699 | return 0; |
| 5700 | |
| 5701 | /* Finally compare the string on either side of the |
| 5702 | * wildcard */ |
| 5703 | prefixlen = pattern_wildcard - pattern; |
| 5704 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5705 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5706 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5707 | return 0; |
| 5708 | |
| 5709 | return 1; |
| 5710 | } |
| 5711 | |
| 5712 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5713 | { |
| 5714 | SSL *ssl; |
| 5715 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5716 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5717 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5718 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5719 | |
| 5720 | int depth; |
| 5721 | X509 *cert; |
| 5722 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5723 | int i; |
| 5724 | X509_NAME *cert_subject; |
| 5725 | char *str; |
| 5726 | |
| 5727 | if (ok == 0) |
| 5728 | return ok; |
| 5729 | |
| 5730 | 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] | 5731 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5732 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5733 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5734 | /* We're checking if the provided hostnames match the desired one. The |
| 5735 | * desired hostname comes from the SNI we presented if any, or if not |
| 5736 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5737 | * directive. If neither is set, we don't care about the name so the |
| 5738 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5739 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5740 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5741 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5742 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5743 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5744 | if (!servername) |
| 5745 | return ok; |
| 5746 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5747 | |
| 5748 | /* We only need to verify the CN on the actual server cert, |
| 5749 | * not the indirect CAs */ |
| 5750 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5751 | if (depth != 0) |
| 5752 | return ok; |
| 5753 | |
| 5754 | /* At this point, the cert is *not* OK unless we can find a |
| 5755 | * hostname match */ |
| 5756 | ok = 0; |
| 5757 | |
| 5758 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5759 | /* It seems like this might happen if verify peer isn't set */ |
| 5760 | if (!cert) |
| 5761 | return ok; |
| 5762 | |
| 5763 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5764 | if (alt_names) { |
| 5765 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5766 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5767 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5768 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5769 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5770 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5771 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5772 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5773 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5774 | OPENSSL_free(str); |
| 5775 | } |
| 5776 | } |
| 5777 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5778 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | cert_subject = X509_get_subject_name(cert); |
| 5782 | i = -1; |
| 5783 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5784 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5785 | ASN1_STRING *value; |
| 5786 | value = X509_NAME_ENTRY_get_data(entry); |
| 5787 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5788 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5789 | OPENSSL_free(str); |
| 5790 | } |
| 5791 | } |
| 5792 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5793 | /* report the mismatch and indicate if SNI was used or not */ |
| 5794 | if (!ok && !conn->err_code) |
| 5795 | 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] | 5796 | return ok; |
| 5797 | } |
| 5798 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5799 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5800 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5801 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5802 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5803 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5804 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5805 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5806 | SSL_OP_NO_SSLv2 | |
| 5807 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5808 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5809 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5810 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5811 | SSL_MODE_RELEASE_BUFFERS | |
| 5812 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5813 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5814 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5815 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5816 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5817 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5818 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5819 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5820 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5821 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5822 | cfgerr++; |
| 5823 | } |
| 5824 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5825 | /* Automatic memory computations need to know we use SSL there */ |
| 5826 | global.ssl_used_backend = 1; |
| 5827 | |
| 5828 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5829 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5830 | 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] | 5831 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5832 | curproxy->id, srv->id, |
| 5833 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5834 | cfgerr++; |
| 5835 | return cfgerr; |
| 5836 | } |
| 5837 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5838 | if (srv->use_ssl) |
| 5839 | srv->xprt = &ssl_sock; |
| 5840 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 5841 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5842 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5843 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5844 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5845 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5846 | proxy_type_str(curproxy), curproxy->id, |
| 5847 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5848 | cfgerr++; |
| 5849 | return cfgerr; |
| 5850 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5851 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5852 | 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] | 5853 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5854 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5855 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5856 | else |
| 5857 | flags = conf_ssl_methods->flags; |
| 5858 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5859 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5860 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5861 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5862 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5863 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5864 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5865 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5866 | min = max = CONF_TLSV_NONE; |
| 5867 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5868 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5869 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5870 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5871 | if (min) { |
| 5872 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5873 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5874 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5875 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5876 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5877 | hole = 0; |
| 5878 | } |
| 5879 | max = i; |
| 5880 | } |
| 5881 | else { |
| 5882 | min = max = i; |
| 5883 | } |
| 5884 | } |
| 5885 | else { |
| 5886 | if (min) |
| 5887 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5888 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5889 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5890 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5891 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5892 | cfgerr += 1; |
| 5893 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5894 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5895 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5896 | /* 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] | 5897 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5898 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5899 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5900 | else |
| 5901 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5902 | if (flags & methodVersions[i].flag) |
| 5903 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5904 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5905 | /* 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] | 5906 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5907 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5908 | #endif |
| 5909 | |
| 5910 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5911 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5912 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5913 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5914 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5915 | if (global_ssl.async) |
| 5916 | mode |= SSL_MODE_ASYNC; |
| 5917 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5918 | SSL_CTX_set_mode(ctx, mode); |
| 5919 | srv->ssl_ctx.ctx = ctx; |
| 5920 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5921 | if (srv->ssl_ctx.client_crt) { |
| 5922 | 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] | 5923 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 5924 | proxy_type_str(curproxy), curproxy->id, |
| 5925 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5926 | cfgerr++; |
| 5927 | } |
| 5928 | 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] | 5929 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 5930 | proxy_type_str(curproxy), curproxy->id, |
| 5931 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5932 | cfgerr++; |
| 5933 | } |
| 5934 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5935 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 5936 | proxy_type_str(curproxy), curproxy->id, |
| 5937 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5938 | cfgerr++; |
| 5939 | } |
| 5940 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5941 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5942 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5943 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5944 | switch (srv->ssl_ctx.verify) { |
| 5945 | case SSL_SOCK_VERIFY_NONE: |
| 5946 | verify = SSL_VERIFY_NONE; |
| 5947 | break; |
| 5948 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5949 | verify = SSL_VERIFY_PEER; |
| 5950 | break; |
| 5951 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5952 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5953 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5954 | (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] | 5955 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5956 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5957 | /* set CAfile to verify */ |
| 5958 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 5959 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5960 | curproxy->id, srv->id, |
| 5961 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5962 | cfgerr++; |
| 5963 | } |
| 5964 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5965 | else { |
| 5966 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5967 | 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", |
| 5968 | curproxy->id, srv->id, |
| 5969 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5970 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5971 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 5972 | curproxy->id, srv->id, |
| 5973 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5974 | cfgerr++; |
| 5975 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5976 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5977 | if (srv->ssl_ctx.crl_file) { |
| 5978 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 5979 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5980 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5981 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 5982 | curproxy->id, srv->id, |
| 5983 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5984 | cfgerr++; |
| 5985 | } |
| 5986 | else { |
| 5987 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5988 | } |
| 5989 | } |
| 5990 | #endif |
| 5991 | } |
| 5992 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5993 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 5994 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5995 | 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] | 5996 | if (srv->ssl_ctx.ciphers && |
| 5997 | !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] | 5998 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 5999 | curproxy->id, srv->id, |
| 6000 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6001 | cfgerr++; |
| 6002 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6003 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6004 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6005 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 6006 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6007 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 6008 | curproxy->id, srv->id, |
| 6009 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 6010 | cfgerr++; |
| 6011 | } |
| 6012 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6013 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 6014 | if (srv->ssl_ctx.npn_str) |
| 6015 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 6016 | #endif |
| 6017 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6018 | if (srv->ssl_ctx.alpn_str) |
| 6019 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 6020 | #endif |
| 6021 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6022 | |
| 6023 | return cfgerr; |
| 6024 | } |
| 6025 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6026 | /* 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] | 6027 | * be NULL, in which case nothing is done. Returns the number of errors |
| 6028 | * encountered. |
| 6029 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6030 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6031 | { |
| 6032 | struct ebmb_node *node; |
| 6033 | struct sni_ctx *sni; |
| 6034 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6035 | int errcode = 0; |
| 6036 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6037 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 6038 | /* Automatic memory computations need to know we use SSL there */ |
| 6039 | global.ssl_used_frontend = 1; |
| 6040 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6041 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 6042 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6043 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6044 | err++; |
| 6045 | } |
| 6046 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 6047 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 6048 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6049 | /* It should not be necessary to call this function, but it's |
| 6050 | necessary first to check and move all initialisation related |
| 6051 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6052 | errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6053 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6054 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6055 | errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6056 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6057 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6058 | while (node) { |
| 6059 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6060 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 6061 | /* only initialize the CTX on its first occurrence and |
| 6062 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6063 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6064 | node = ebmb_next(node); |
| 6065 | } |
| 6066 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6067 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6068 | while (node) { |
| 6069 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6070 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6071 | /* only initialize the CTX on its first occurrence and |
| 6072 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6073 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 6074 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6075 | node = ebmb_next(node); |
| 6076 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6077 | |
| 6078 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6079 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6080 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6081 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6082 | err++; |
| 6083 | } |
| 6084 | |
| 6085 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6086 | return err; |
| 6087 | } |
| 6088 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6089 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 6090 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 6091 | * alerts are directly emitted since the rest of the stack does it below. |
| 6092 | */ |
| 6093 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 6094 | { |
| 6095 | struct proxy *px = bind_conf->frontend; |
| 6096 | int alloc_ctx; |
| 6097 | int err; |
| 6098 | |
| 6099 | if (!bind_conf->is_ssl) { |
| 6100 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6101 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 6102 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6103 | } |
| 6104 | return 0; |
| 6105 | } |
| 6106 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6107 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6108 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 6109 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6110 | } |
| 6111 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6112 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 6113 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6114 | return -1; |
| 6115 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6116 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 6117 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6118 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 6119 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6120 | sizeof(*sh_ssl_sess_tree), |
| 6121 | ((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] | 6122 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6123 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 6124 | 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"); |
| 6125 | else |
| 6126 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 6127 | return -1; |
| 6128 | } |
| 6129 | /* free block callback */ |
| 6130 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 6131 | /* init the root tree within the extra space */ |
| 6132 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 6133 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6134 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6135 | err = 0; |
| 6136 | /* initialize all certificate contexts */ |
| 6137 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 6138 | |
| 6139 | /* initialize CA variables if the certificates generation is enabled */ |
| 6140 | err += ssl_sock_load_ca(bind_conf); |
| 6141 | |
| 6142 | return -err; |
| 6143 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6144 | |
| 6145 | /* release ssl context allocated for servers. */ |
| 6146 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 6147 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6148 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6149 | if (srv->ssl_ctx.alpn_str) |
| 6150 | free(srv->ssl_ctx.alpn_str); |
| 6151 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 6152 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6153 | if (srv->ssl_ctx.npn_str) |
| 6154 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 6155 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6156 | if (srv->ssl_ctx.ctx) |
| 6157 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 6158 | } |
| 6159 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6160 | /* 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] | 6161 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 6162 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6163 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6164 | { |
| 6165 | struct ebmb_node *node, *back; |
| 6166 | struct sni_ctx *sni; |
| 6167 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6168 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6169 | while (node) { |
| 6170 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6171 | back = ebmb_next(node); |
| 6172 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6173 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6174 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6175 | ssl_sock_free_ssl_conf(sni->conf); |
| 6176 | free(sni->conf); |
| 6177 | sni->conf = NULL; |
| 6178 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6179 | free(sni); |
| 6180 | node = back; |
| 6181 | } |
| 6182 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6183 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6184 | while (node) { |
| 6185 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6186 | back = ebmb_next(node); |
| 6187 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6188 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6189 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6190 | ssl_sock_free_ssl_conf(sni->conf); |
| 6191 | free(sni->conf); |
| 6192 | sni->conf = NULL; |
| 6193 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6194 | free(sni); |
| 6195 | node = back; |
| 6196 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6197 | SSL_CTX_free(bind_conf->initial_ctx); |
| 6198 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6199 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6200 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6201 | } |
| 6202 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6203 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 6204 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 6205 | { |
| 6206 | ssl_sock_free_ca(bind_conf); |
| 6207 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6208 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6209 | free(bind_conf->ca_sign_file); |
| 6210 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 6211 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6212 | free(bind_conf->keys_ref->filename); |
| 6213 | free(bind_conf->keys_ref->tlskeys); |
| 6214 | LIST_DEL(&bind_conf->keys_ref->list); |
| 6215 | free(bind_conf->keys_ref); |
| 6216 | } |
| 6217 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6218 | bind_conf->ca_sign_pass = NULL; |
| 6219 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6220 | } |
| 6221 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6222 | /* Load CA cert file and private key used to generate certificates */ |
| 6223 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6224 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6225 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6226 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6227 | FILE *fp; |
| 6228 | X509 *cacert = NULL; |
| 6229 | EVP_PKEY *capkey = NULL; |
| 6230 | int err = 0; |
| 6231 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 6232 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6233 | return err; |
| 6234 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6235 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6236 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6237 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6238 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6239 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6240 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6241 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6242 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6243 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6244 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 6245 | "no CA certificate File configured at [%s:%d].\n", |
| 6246 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6247 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6248 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6249 | |
| 6250 | /* read in the CA certificate */ |
| 6251 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6252 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6253 | 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] | 6254 | goto load_error; |
| 6255 | } |
| 6256 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6257 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6258 | 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] | 6259 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6260 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6261 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6262 | 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] | 6263 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 6264 | 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] | 6265 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6266 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6267 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6268 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6269 | bind_conf->ca_sign_cert = cacert; |
| 6270 | bind_conf->ca_sign_pkey = capkey; |
| 6271 | return err; |
| 6272 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6273 | read_error: |
| 6274 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6275 | if (capkey) EVP_PKEY_free(capkey); |
| 6276 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6277 | load_error: |
| 6278 | bind_conf->generate_certs = 0; |
| 6279 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6280 | return err; |
| 6281 | } |
| 6282 | |
| 6283 | /* Release CA cert and private key used to generate certificated */ |
| 6284 | void |
| 6285 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 6286 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6287 | if (bind_conf->ca_sign_pkey) |
| 6288 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 6289 | if (bind_conf->ca_sign_cert) |
| 6290 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 6291 | bind_conf->ca_sign_pkey = NULL; |
| 6292 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6293 | } |
| 6294 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6295 | /* |
| 6296 | * This function is called if SSL * context is not yet allocated. The function |
| 6297 | * is designed to be called before any other data-layer operation and sets the |
| 6298 | * handshake flag on the connection. It is safe to call it multiple times. |
| 6299 | * It returns 0 on success and -1 in error case. |
| 6300 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6301 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6302 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6303 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6304 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6305 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6306 | return 0; |
| 6307 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6308 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6309 | return 0; |
| 6310 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6311 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6312 | if (!ctx) { |
| 6313 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6314 | return -1; |
| 6315 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6316 | ctx->wait_event.tasklet = tasklet_new(); |
| 6317 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6318 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6319 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6320 | return -1; |
| 6321 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6322 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6323 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6324 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6325 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6326 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6327 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6328 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6329 | ctx->xprt_st = 0; |
| 6330 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6331 | |
| 6332 | /* Only work with sockets for now, this should be adapted when we'll |
| 6333 | * add QUIC support. |
| 6334 | */ |
| 6335 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6336 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6337 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6338 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6339 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6340 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6341 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 6342 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6343 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6344 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6345 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6346 | /* If it is in client mode initiate SSL session |
| 6347 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6348 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6349 | int may_retry = 1; |
| 6350 | |
| 6351 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6352 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6353 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 6354 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6355 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6356 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6357 | goto retry_connect; |
| 6358 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6359 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6360 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6361 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6362 | ctx->bio = BIO_new(ha_meth); |
| 6363 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6364 | SSL_free(ctx->ssl); |
| 6365 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6366 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6367 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6368 | goto retry_connect; |
| 6369 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6370 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6371 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6372 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6373 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6374 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6375 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6376 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6377 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6378 | SSL_free(ctx->ssl); |
| 6379 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6380 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6381 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6382 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6383 | goto retry_connect; |
| 6384 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6385 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6386 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6387 | } |
| 6388 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6389 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6390 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6391 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6392 | 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] | 6393 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6394 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6395 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6396 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6397 | } else if (sess) { |
| 6398 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6399 | } |
| 6400 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6401 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6402 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6403 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6404 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6405 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6406 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6407 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6408 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6409 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6410 | return 0; |
| 6411 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6412 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6413 | int may_retry = 1; |
| 6414 | |
| 6415 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6416 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6417 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 6418 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6419 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6420 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6421 | goto retry_accept; |
| 6422 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6423 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6424 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6425 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6426 | ctx->bio = BIO_new(ha_meth); |
| 6427 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6428 | SSL_free(ctx->ssl); |
| 6429 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6430 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6431 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6432 | goto retry_accept; |
| 6433 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6434 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6435 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6436 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6437 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6438 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6439 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6440 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6441 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6442 | SSL_free(ctx->ssl); |
| 6443 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6444 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6445 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6446 | goto retry_accept; |
| 6447 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6448 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6449 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6450 | } |
| 6451 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6452 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6453 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 6454 | b_alloc(&ctx->early_buf); |
| 6455 | SSL_set_max_early_data(ctx->ssl, |
| 6456 | /* Only allow early data if we managed to allocate |
| 6457 | * a buffer. |
| 6458 | */ |
| 6459 | (!b_is_null(&ctx->early_buf)) ? |
| 6460 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6461 | } |
| 6462 | #endif |
| 6463 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6464 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6465 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6466 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6467 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6468 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6469 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 6470 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6471 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6472 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6473 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6474 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6475 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6476 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6477 | return 0; |
| 6478 | } |
| 6479 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6480 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6481 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6482 | if (ctx && ctx->wait_event.tasklet) |
| 6483 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6484 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6485 | return -1; |
| 6486 | } |
| 6487 | |
| 6488 | |
| 6489 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6490 | * updates the FD status if it wants some polling before being called again. |
| 6491 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6492 | * otherwise it returns non-zero and removes itself from the connection's |
| 6493 | * flags (the bit is provided in <flag> by the caller). |
| 6494 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6495 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6496 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6497 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6498 | int ret; |
| 6499 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6500 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6501 | return 0; |
| 6502 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6503 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6504 | goto out_error; |
| 6505 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6506 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6507 | /* |
| 6508 | * Check if we have early data. If we do, we have to read them |
| 6509 | * before SSL_do_handshake() is called, And there's no way to |
| 6510 | * detect early data, except to try to read them |
| 6511 | */ |
| 6512 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6513 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6514 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6515 | while (1) { |
| 6516 | ret = SSL_read_early_data(ctx->ssl, |
| 6517 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6518 | &read_data); |
| 6519 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6520 | goto check_error; |
| 6521 | if (read_data > 0) { |
| 6522 | conn->flags |= CO_FL_EARLY_DATA; |
| 6523 | b_add(&ctx->early_buf, read_data); |
| 6524 | } |
| 6525 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6526 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6527 | if (!b_data(&ctx->early_buf)) |
| 6528 | b_free(&ctx->early_buf); |
| 6529 | break; |
| 6530 | } |
| 6531 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6532 | } |
| 6533 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6534 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6535 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6536 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6537 | * the reneg handshake. |
| 6538 | * Here we use SSL_peek as a workaround for reneg. |
| 6539 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6540 | if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6541 | char c; |
| 6542 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6543 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6544 | if (ret <= 0) { |
| 6545 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6546 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6547 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6548 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6549 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6550 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6551 | 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] | 6552 | return 0; |
| 6553 | } |
| 6554 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6555 | /* handshake may have been completed but we have |
| 6556 | * no more data to read. |
| 6557 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6558 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6559 | ret = 1; |
| 6560 | goto reneg_ok; |
| 6561 | } |
| 6562 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6563 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6564 | 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] | 6565 | return 0; |
| 6566 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6567 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6568 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6569 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6570 | return 0; |
| 6571 | } |
| 6572 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6573 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6574 | /* if errno is null, then connection was successfully established */ |
| 6575 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6576 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6577 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6578 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6579 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6580 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6581 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6582 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6583 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6584 | /* 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] | 6585 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6586 | empty_handshake = state == TLS_ST_BEFORE; |
| 6587 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6588 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6589 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6590 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6591 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6592 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6593 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6594 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6595 | else |
| 6596 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6597 | } |
| 6598 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6599 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6600 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6601 | else |
| 6602 | conn->err_code = CO_ER_SSL_ABORT; |
| 6603 | } |
| 6604 | } |
| 6605 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6606 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6607 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6608 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6609 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6610 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6611 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6612 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6613 | goto out_error; |
| 6614 | } |
| 6615 | else { |
| 6616 | /* Fail on all other handshake errors */ |
| 6617 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6618 | * buffer, causing an RST to be emitted upon close() on |
| 6619 | * TCP sockets. We first try to drain possibly pending |
| 6620 | * data to avoid this as much as possible. |
| 6621 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6622 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6623 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6624 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6625 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6626 | goto out_error; |
| 6627 | } |
| 6628 | } |
| 6629 | /* read some data: consider handshake completed */ |
| 6630 | goto reneg_ok; |
| 6631 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6632 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6633 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6634 | if (ret != 1) { |
| 6635 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6636 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6637 | |
| 6638 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6639 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6640 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6641 | 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] | 6642 | return 0; |
| 6643 | } |
| 6644 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6645 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6646 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6647 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6648 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6649 | return 0; |
| 6650 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6651 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6652 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6653 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6654 | return 0; |
| 6655 | } |
| 6656 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6657 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6658 | /* if errno is null, then connection was successfully established */ |
| 6659 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6660 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6661 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6662 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6663 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6664 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6665 | #else |
| 6666 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6667 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6668 | /* 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] | 6669 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6670 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6671 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6672 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6673 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6674 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6675 | if (empty_handshake) { |
| 6676 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6677 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6678 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6679 | else |
| 6680 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6681 | } |
| 6682 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6683 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6684 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6685 | else |
| 6686 | conn->err_code = CO_ER_SSL_ABORT; |
| 6687 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6688 | } |
| 6689 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6690 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6691 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6692 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6693 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6694 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6695 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6696 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6697 | goto out_error; |
| 6698 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6699 | else { |
| 6700 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6701 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6702 | * buffer, causing an RST to be emitted upon close() on |
| 6703 | * TCP sockets. We first try to drain possibly pending |
| 6704 | * data to avoid this as much as possible. |
| 6705 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6706 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6707 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6708 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6709 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6710 | goto out_error; |
| 6711 | } |
| 6712 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6713 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6714 | else { |
| 6715 | /* |
| 6716 | * If the server refused the early data, we have to send a |
| 6717 | * 425 to the client, as we no longer have the data to sent |
| 6718 | * them again. |
| 6719 | */ |
| 6720 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6721 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6722 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6723 | goto out_error; |
| 6724 | } |
| 6725 | } |
| 6726 | } |
| 6727 | #endif |
| 6728 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6729 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6730 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6731 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6732 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6733 | /* ASYNC engine API doesn't support moving read/write |
| 6734 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6735 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6736 | */ |
| 6737 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6738 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6739 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6740 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6741 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6742 | if (objt_server(conn->target)) { |
| 6743 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6744 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6745 | 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] | 6746 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6747 | else { |
| 6748 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6749 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6750 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6751 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | /* The connection is now established at both layers, it's time to leave */ |
| 6755 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6756 | return 1; |
| 6757 | |
| 6758 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6759 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6760 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6761 | ERR_clear_error(); |
| 6762 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6763 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6764 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6765 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6766 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6767 | } |
| 6768 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6769 | /* Fail on all other handshake errors */ |
| 6770 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6771 | if (!conn->err_code) |
| 6772 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6773 | return 0; |
| 6774 | } |
| 6775 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6776 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6777 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6778 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6779 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6780 | * unless the transport layer was already released. |
| 6781 | */ |
| 6782 | static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6783 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6784 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6785 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6786 | if (!ctx) |
| 6787 | return -1; |
| 6788 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6789 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6790 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6791 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6792 | ctx->subs = es; |
| 6793 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6794 | |
| 6795 | /* we may have to subscribe to lower layers for new events */ |
| 6796 | event_type &= ~ctx->wait_event.events; |
| 6797 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6798 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6799 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6800 | } |
| 6801 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6802 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6803 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6804 | * subscribe() call. It always returns zero. |
| 6805 | */ |
| 6806 | static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6807 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6808 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6809 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6810 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6811 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6812 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6813 | es->events &= ~event_type; |
| 6814 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6815 | ctx->subs = NULL; |
| 6816 | |
| 6817 | /* If we subscribed, and we're not doing the handshake, |
| 6818 | * then we subscribed because the upper layer asked for it, |
| 6819 | * as the upper layer is no longer interested, we can |
| 6820 | * unsubscribe too. |
| 6821 | */ |
| 6822 | event_type &= ctx->wait_event.events; |
| 6823 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6824 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6825 | |
| 6826 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6827 | } |
| 6828 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6829 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6830 | * Returns 0 on success, and non-zero on failure. |
| 6831 | */ |
| 6832 | 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) |
| 6833 | { |
| 6834 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6835 | |
| 6836 | if (oldxprt_ops != NULL) |
| 6837 | *oldxprt_ops = ctx->xprt; |
| 6838 | if (oldxprt_ctx != NULL) |
| 6839 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6840 | ctx->xprt = toadd_ops; |
| 6841 | ctx->xprt_ctx = toadd_ctx; |
| 6842 | return 0; |
| 6843 | } |
| 6844 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6845 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6846 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6847 | * XPRT. |
| 6848 | */ |
| 6849 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6850 | { |
| 6851 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6852 | |
| 6853 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6854 | ctx->xprt_ctx = newctx; |
| 6855 | ctx->xprt = newops; |
| 6856 | return 0; |
| 6857 | } |
| 6858 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6859 | } |
| 6860 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6861 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6862 | { |
| 6863 | struct ssl_sock_ctx *ctx = context; |
| 6864 | |
| 6865 | /* First if we're doing an handshake, try that */ |
| 6866 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6867 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6868 | /* If we had an error, or the handshake is done and I/O is available, |
| 6869 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6870 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6871 | * we can't be sure conn_fd_handler() will be called again. |
| 6872 | */ |
| 6873 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6874 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6875 | int ret = 0; |
| 6876 | int woke = 0; |
| 6877 | |
| 6878 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6879 | if (ctx->subs) { |
| 6880 | tasklet_wakeup(ctx->subs->tasklet); |
| 6881 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6882 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6883 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6884 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6885 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6886 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6887 | * upper layers know. If we have no mux, create it, |
| 6888 | * and once we have a mux, call its wake method if we didn't |
| 6889 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6890 | */ |
| 6891 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6892 | if (!ctx->conn->mux) |
| 6893 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6894 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6895 | ctx->conn->mux->wake(ctx->conn); |
| 6896 | return NULL; |
| 6897 | } |
| 6898 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6899 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6900 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6901 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6902 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6903 | tasklet_wakeup(ctx->subs->tasklet); |
| 6904 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6905 | if (!ctx->subs->events) |
| 6906 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6907 | } |
| 6908 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6909 | return NULL; |
| 6910 | } |
| 6911 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6912 | /* 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] | 6913 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6914 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6915 | * flags are updated with whatever special event is detected (error, read0, |
| 6916 | * empty). The caller is responsible for taking care of those events and |
| 6917 | * avoiding the call if inappropriate. The function does not call the |
| 6918 | * connection's polling update function, so the caller is responsible for this. |
| 6919 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6920 | 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] | 6921 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6922 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6923 | ssize_t ret; |
| 6924 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6925 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6926 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6927 | goto out_error; |
| 6928 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6929 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6930 | if (b_data(&ctx->early_buf)) { |
| 6931 | try = b_contig_space(buf); |
| 6932 | if (try > b_data(&ctx->early_buf)) |
| 6933 | try = b_data(&ctx->early_buf); |
| 6934 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6935 | b_add(buf, try); |
| 6936 | b_del(&ctx->early_buf, try); |
| 6937 | if (b_data(&ctx->early_buf) == 0) |
| 6938 | b_free(&ctx->early_buf); |
| 6939 | return try; |
| 6940 | } |
| 6941 | #endif |
| 6942 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6943 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6944 | /* a handshake was requested */ |
| 6945 | return 0; |
| 6946 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6947 | /* read the largest possible block. For this, we perform only one call |
| 6948 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6949 | * in which case we accept to do it once again. A new attempt is made on |
| 6950 | * EINTR too. |
| 6951 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6952 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6953 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6954 | try = b_contig_space(buf); |
| 6955 | if (!try) |
| 6956 | break; |
| 6957 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6958 | if (try > count) |
| 6959 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6960 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6961 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6962 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6963 | if (conn->flags & CO_FL_ERROR) { |
| 6964 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6965 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6966 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6967 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6968 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6969 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6970 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6971 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6972 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6973 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6974 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6975 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6976 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6977 | 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] | 6978 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6979 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6980 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6981 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6982 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6983 | break; |
| 6984 | } |
| 6985 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6986 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6987 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6988 | SUB_RETRY_RECV, |
| 6989 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6990 | /* handshake is running, and it may need to re-enable read */ |
| 6991 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6992 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6993 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6994 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6995 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6996 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6997 | break; |
| 6998 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6999 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7000 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 7001 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7002 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 7003 | * stack before shutting down the connection for |
| 7004 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7005 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 7006 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7007 | /* otherwise it's a real error */ |
| 7008 | goto out_error; |
| 7009 | } |
| 7010 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7011 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7012 | return done; |
| 7013 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7014 | clear_ssl_error: |
| 7015 | /* Clear openssl global errors stack */ |
| 7016 | ssl_sock_dump_errors(conn); |
| 7017 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7018 | read0: |
| 7019 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7020 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7021 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7022 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7023 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7024 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7025 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7026 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7027 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7028 | } |
| 7029 | |
| 7030 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7031 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 7032 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 7033 | * 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] | 7034 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 7035 | * a second call may be performed. The connection's flags are updated with |
| 7036 | * whatever special event is detected (error, empty). The caller is responsible |
| 7037 | * for taking care of those events and avoiding the call if inappropriate. The |
| 7038 | * 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] | 7039 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 7040 | * caller to take care of this. It's up to the caller to update the buffer's |
| 7041 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7042 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7043 | 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] | 7044 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7045 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7046 | ssize_t ret; |
| 7047 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7048 | |
| 7049 | done = 0; |
| 7050 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7051 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7052 | goto out_error; |
| 7053 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7054 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7055 | /* a handshake was requested */ |
| 7056 | return 0; |
| 7057 | |
| 7058 | /* send the largest possible block. For this we perform only one call |
| 7059 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 7060 | * in which case we accept to do it once again. |
| 7061 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7062 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7063 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7064 | size_t written_data; |
| 7065 | #endif |
| 7066 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7067 | try = b_contig_data(buf, done); |
| 7068 | if (try > count) |
| 7069 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7070 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 7071 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7072 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7073 | global_ssl.max_record && try > global_ssl.max_record) { |
| 7074 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7075 | } |
| 7076 | else { |
| 7077 | /* we need to keep the information about the fact that |
| 7078 | * we're not limiting the upcoming send(), because if it |
| 7079 | * fails, we'll have to retry with at least as many data. |
| 7080 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7081 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7082 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7083 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7084 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7085 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7086 | unsigned int max_early; |
| 7087 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7088 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7089 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7090 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7091 | if (SSL_get0_session(ctx->ssl)) |
| 7092 | 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] | 7093 | else |
| 7094 | max_early = 0; |
| 7095 | } |
| 7096 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7097 | if (try + ctx->sent_early_data > max_early) { |
| 7098 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7099 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7100 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7101 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7102 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7103 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7104 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7105 | 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] | 7106 | if (ret == 1) { |
| 7107 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7108 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7109 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7110 | 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] | 7111 | /* Initiate the handshake, now */ |
| 7112 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 7113 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7114 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | } else |
| 7118 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7119 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7120 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7121 | if (conn->flags & CO_FL_ERROR) { |
| 7122 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7123 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7124 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7125 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 7126 | /* A send succeeded, so we can consider ourself connected */ |
| 7127 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7128 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7129 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7130 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7131 | } |
| 7132 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7133 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7134 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7135 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7136 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7137 | /* handshake is running, and it may need to re-enable write */ |
| 7138 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7139 | 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] | 7140 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7141 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7142 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7143 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7144 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7145 | break; |
| 7146 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7147 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7148 | break; |
| 7149 | } |
| 7150 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7151 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7152 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7153 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7154 | SUB_RETRY_RECV, |
| 7155 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7156 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7157 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7158 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7159 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7160 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7161 | break; |
| 7162 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7163 | goto out_error; |
| 7164 | } |
| 7165 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7166 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7167 | return done; |
| 7168 | |
| 7169 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7170 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7171 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7172 | ERR_clear_error(); |
| 7173 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7174 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7175 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7176 | } |
| 7177 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7178 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7179 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7180 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7181 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7182 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7183 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7184 | if (ctx->wait_event.events != 0) |
| 7185 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 7186 | ctx->wait_event.events, |
| 7187 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7188 | if (ctx->subs) { |
| 7189 | ctx->subs->events = 0; |
| 7190 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7191 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7192 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 7193 | if (ctx->xprt->close) |
| 7194 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7195 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7196 | if (global_ssl.async) { |
| 7197 | OSSL_ASYNC_FD all_fd[32], afd; |
| 7198 | size_t num_all_fds = 0; |
| 7199 | int i; |
| 7200 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7201 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7202 | if (num_all_fds > 32) { |
| 7203 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 7204 | return; |
| 7205 | } |
| 7206 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7207 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7208 | |
| 7209 | /* If an async job is pending, we must try to |
| 7210 | to catch the end using polling before calling |
| 7211 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7212 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7213 | for (i=0 ; i < num_all_fds ; i++) { |
| 7214 | /* switch on an handler designed to |
| 7215 | * handle the SSL_free |
| 7216 | */ |
| 7217 | afd = all_fd[i]; |
| 7218 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7219 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7220 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 7221 | /* To ensure that the fd cache won't be used |
| 7222 | * and we'll catch a real RD event. |
| 7223 | */ |
| 7224 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7225 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7226 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7227 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7228 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7229 | return; |
| 7230 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7231 | /* Else we can remove the fds from the fdtab |
| 7232 | * and call SSL_free. |
| 7233 | * note: we do a fd_remove and not a delete |
| 7234 | * because the fd is owned by the engine. |
| 7235 | * the engine is responsible to close |
| 7236 | */ |
| 7237 | for (i=0 ; i < num_all_fds ; i++) |
| 7238 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7239 | } |
| 7240 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7241 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7242 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7243 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7244 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7245 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7246 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7247 | } |
| 7248 | |
| 7249 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 7250 | * any case, flags the connection as reusable if no handshake was in progress. |
| 7251 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7252 | 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] | 7253 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7254 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7255 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7256 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7257 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 7258 | if (!clean) |
| 7259 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7260 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7261 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7262 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7263 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7264 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7265 | ERR_clear_error(); |
| 7266 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7267 | } |
| 7268 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7269 | /* fill a buffer with the algorithm and size of a public key */ |
| 7270 | static int cert_get_pkey_algo(X509 *crt, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7271 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7272 | int bits = 0; |
| 7273 | int sig = TLSEXT_signature_anonymous; |
| 7274 | int len = -1; |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7275 | EVP_PKEY *pkey; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7276 | |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7277 | pkey = X509_get_pubkey(crt); |
| 7278 | if (pkey) { |
| 7279 | bits = EVP_PKEY_bits(pkey); |
| 7280 | switch(EVP_PKEY_base_id(pkey)) { |
| 7281 | case EVP_PKEY_RSA: |
| 7282 | sig = TLSEXT_signature_rsa; |
| 7283 | break; |
| 7284 | case EVP_PKEY_EC: |
| 7285 | sig = TLSEXT_signature_ecdsa; |
| 7286 | break; |
| 7287 | case EVP_PKEY_DSA: |
| 7288 | sig = TLSEXT_signature_dsa; |
| 7289 | break; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7290 | } |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7291 | EVP_PKEY_free(pkey); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7292 | } |
| 7293 | |
| 7294 | switch(sig) { |
| 7295 | case TLSEXT_signature_rsa: |
| 7296 | len = chunk_printf(out, "RSA%d", bits); |
| 7297 | break; |
| 7298 | case TLSEXT_signature_ecdsa: |
| 7299 | len = chunk_printf(out, "EC%d", bits); |
| 7300 | break; |
| 7301 | case TLSEXT_signature_dsa: |
| 7302 | len = chunk_printf(out, "DSA%d", bits); |
| 7303 | break; |
| 7304 | default: |
| 7305 | return 0; |
| 7306 | } |
| 7307 | if (len < 0) |
| 7308 | return 0; |
| 7309 | return 1; |
| 7310 | } |
| 7311 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 7312 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7313 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7314 | { |
| 7315 | struct ssl_sock_ctx *ctx; |
| 7316 | X509 *crt; |
| 7317 | |
| 7318 | if (!ssl_sock_is_ssl(conn)) |
| 7319 | return 0; |
| 7320 | |
| 7321 | ctx = conn->xprt_ctx; |
| 7322 | |
| 7323 | crt = SSL_get_certificate(ctx->ssl); |
| 7324 | if (!crt) |
| 7325 | return 0; |
| 7326 | |
| 7327 | return cert_get_pkey_algo(crt, out); |
| 7328 | } |
| 7329 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7330 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7331 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7332 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7333 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7334 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7335 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7336 | X509 *crt; |
| 7337 | |
| 7338 | if (!ssl_sock_is_ssl(conn)) |
| 7339 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7340 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7341 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7342 | if (!crt) |
| 7343 | return NULL; |
| 7344 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7345 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7346 | } |
| 7347 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7348 | /* used for ppv2 authority */ |
| 7349 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7350 | { |
| 7351 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7352 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7353 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7354 | if (!ssl_sock_is_ssl(conn)) |
| 7355 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7356 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7357 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7358 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7359 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7360 | #endif |
| 7361 | } |
| 7362 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7363 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7364 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7365 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7366 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7367 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7368 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7369 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7370 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7371 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7372 | } |
| 7373 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7374 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7375 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7376 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7377 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7378 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7379 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7380 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7381 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7382 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7383 | } |
| 7384 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7385 | /* Extract a serial from a cert, and copy it to a chunk. |
| 7386 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 7387 | * -1 if output is not large enough. |
| 7388 | */ |
| 7389 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7390 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7391 | { |
| 7392 | ASN1_INTEGER *serial; |
| 7393 | |
| 7394 | serial = X509_get_serialNumber(crt); |
| 7395 | if (!serial) |
| 7396 | return 0; |
| 7397 | |
| 7398 | if (out->size < serial->length) |
| 7399 | return -1; |
| 7400 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7401 | memcpy(out->area, serial->data, serial->length); |
| 7402 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7403 | return 1; |
| 7404 | } |
| 7405 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7406 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7407 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 7408 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7409 | */ |
| 7410 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7411 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7412 | { |
| 7413 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7414 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7415 | |
| 7416 | len =i2d_X509(crt, NULL); |
| 7417 | if (len <= 0) |
| 7418 | return 1; |
| 7419 | |
| 7420 | if (out->size < len) |
| 7421 | return -1; |
| 7422 | |
| 7423 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7424 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7425 | return 1; |
| 7426 | } |
| 7427 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7428 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7429 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7430 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 7431 | * and -1 if output is not large enough. |
| 7432 | */ |
| 7433 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7434 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7435 | { |
| 7436 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 7437 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 7438 | |
| 7439 | if (gentm->length < 12) |
| 7440 | return 0; |
| 7441 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 7442 | return 0; |
| 7443 | if (out->size < gentm->length-2) |
| 7444 | return -1; |
| 7445 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7446 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 7447 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7448 | return 1; |
| 7449 | } |
| 7450 | else if (tm->type == V_ASN1_UTCTIME) { |
| 7451 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 7452 | |
| 7453 | if (utctm->length < 10) |
| 7454 | return 0; |
| 7455 | if (utctm->data[0] >= 0x35) |
| 7456 | return 0; |
| 7457 | if (out->size < utctm->length) |
| 7458 | return -1; |
| 7459 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7460 | memcpy(out->area, utctm->data, utctm->length); |
| 7461 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7462 | return 1; |
| 7463 | } |
| 7464 | |
| 7465 | return 0; |
| 7466 | } |
| 7467 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7468 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 7469 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 7470 | */ |
| 7471 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7472 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 7473 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7474 | { |
| 7475 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7476 | ASN1_OBJECT *obj; |
| 7477 | ASN1_STRING *data; |
| 7478 | const unsigned char *data_ptr; |
| 7479 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7480 | int i, j, n; |
| 7481 | int cur = 0; |
| 7482 | const char *s; |
| 7483 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7484 | int name_count; |
| 7485 | |
| 7486 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7487 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7488 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7489 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7490 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7491 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7492 | else |
| 7493 | j = i; |
| 7494 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7495 | ne = X509_NAME_get_entry(a, j); |
| 7496 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7497 | data = X509_NAME_ENTRY_get_data(ne); |
| 7498 | data_ptr = ASN1_STRING_get0_data(data); |
| 7499 | data_len = ASN1_STRING_length(data); |
| 7500 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7501 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7502 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7503 | s = tmp; |
| 7504 | } |
| 7505 | |
| 7506 | if (chunk_strcasecmp(entry, s) != 0) |
| 7507 | continue; |
| 7508 | |
| 7509 | if (pos < 0) |
| 7510 | cur--; |
| 7511 | else |
| 7512 | cur++; |
| 7513 | |
| 7514 | if (cur != pos) |
| 7515 | continue; |
| 7516 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7517 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7518 | return -1; |
| 7519 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7520 | memcpy(out->area, data_ptr, data_len); |
| 7521 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7522 | return 1; |
| 7523 | } |
| 7524 | |
| 7525 | return 0; |
| 7526 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | /* |
| 7530 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 7531 | * Return 0; |
| 7532 | */ |
| 7533 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7534 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 7535 | { |
| 7536 | int i; |
| 7537 | char *str; |
| 7538 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 7539 | |
| 7540 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 7541 | if (names) { |
| 7542 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 7543 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 7544 | if (i > 0) |
| 7545 | chunk_appendf(out, ", "); |
| 7546 | if (name->type == GEN_DNS) { |
| 7547 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 7548 | chunk_appendf(out, "DNS:%s", str); |
| 7549 | OPENSSL_free(str); |
| 7550 | } |
| 7551 | } |
| 7552 | } |
| 7553 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 7554 | } |
| 7555 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7556 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7557 | #endif |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7558 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7559 | /* |
| 7560 | * Extract the DN in the specified format from the X509_NAME and copy result to a chunk. |
| 7561 | * Currently supports rfc2253 for returning LDAP V3 DNs. |
| 7562 | * Returns 1 if dn entries exist, 0 if no dn entry was found. |
| 7563 | */ |
| 7564 | static int |
| 7565 | ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out) |
| 7566 | { |
| 7567 | BIO *bio = NULL; |
| 7568 | int ret = 0; |
| 7569 | int data_len = 0; |
| 7570 | |
| 7571 | if (chunk_strcmp(format, "rfc2253") == 0) { |
| 7572 | bio = BIO_new(BIO_s_mem()); |
| 7573 | if (bio == NULL) |
| 7574 | goto out; |
| 7575 | |
| 7576 | if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0) |
| 7577 | goto out; |
| 7578 | |
| 7579 | if ((data_len = BIO_read(bio, out->area, out->size)) <= 0) |
| 7580 | goto out; |
| 7581 | |
| 7582 | out->data = data_len; |
| 7583 | |
| 7584 | ret = 1; |
| 7585 | } |
| 7586 | out: |
| 7587 | if (bio) |
| 7588 | BIO_free(bio); |
| 7589 | return ret; |
| 7590 | } |
| 7591 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7592 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 7593 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 7594 | */ |
| 7595 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7596 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7597 | { |
| 7598 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7599 | ASN1_OBJECT *obj; |
| 7600 | ASN1_STRING *data; |
| 7601 | const unsigned char *data_ptr; |
| 7602 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7603 | int i, n, ln; |
| 7604 | int l = 0; |
| 7605 | const char *s; |
| 7606 | char *p; |
| 7607 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7608 | int name_count; |
| 7609 | |
| 7610 | |
| 7611 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7612 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7613 | out->data = 0; |
| 7614 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7615 | for (i = 0; i < name_count; i++) { |
| 7616 | ne = X509_NAME_get_entry(a, i); |
| 7617 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7618 | data = X509_NAME_ENTRY_get_data(ne); |
| 7619 | data_ptr = ASN1_STRING_get0_data(data); |
| 7620 | data_len = ASN1_STRING_length(data); |
| 7621 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7622 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7623 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7624 | s = tmp; |
| 7625 | } |
| 7626 | ln = strlen(s); |
| 7627 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7628 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7629 | if (l > out->size) |
| 7630 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7631 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7632 | |
| 7633 | *(p++)='/'; |
| 7634 | memcpy(p, s, ln); |
| 7635 | p += ln; |
| 7636 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7637 | memcpy(p, data_ptr, data_len); |
| 7638 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7639 | } |
| 7640 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7641 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7642 | return 0; |
| 7643 | |
| 7644 | return 1; |
| 7645 | } |
| 7646 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7647 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7648 | { |
| 7649 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7650 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7651 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7652 | if (!ssl_sock_is_ssl(conn)) |
| 7653 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7654 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7655 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7656 | #endif |
| 7657 | } |
| 7658 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7659 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7660 | * to disable SNI. |
| 7661 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7662 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7663 | { |
| 7664 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7665 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7666 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7667 | char *prev_name; |
| 7668 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7669 | if (!ssl_sock_is_ssl(conn)) |
| 7670 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7671 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7672 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7673 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7674 | * new connection will present a new SNI. As an optimization we could |
| 7675 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7676 | * server. |
| 7677 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7678 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7679 | if ((!prev_name && hostname) || |
| 7680 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7681 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7682 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7683 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7684 | #endif |
| 7685 | } |
| 7686 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7687 | /* Extract peer certificate's common name into the chunk dest |
| 7688 | * Returns |
| 7689 | * the len of the extracted common name |
| 7690 | * or 0 if no CN found in DN |
| 7691 | * or -1 on error case (i.e. no peer certificate) |
| 7692 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7693 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7694 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7695 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7696 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7697 | X509 *crt = NULL; |
| 7698 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7699 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7700 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7701 | .area = (char *)&find_cn, |
| 7702 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7703 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7704 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7705 | |
| 7706 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7707 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7708 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7709 | |
| 7710 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7711 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7712 | if (!crt) |
| 7713 | goto out; |
| 7714 | |
| 7715 | name = X509_get_subject_name(crt); |
| 7716 | if (!name) |
| 7717 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7718 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7719 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7720 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7721 | if (crt) |
| 7722 | X509_free(crt); |
| 7723 | |
| 7724 | return result; |
| 7725 | } |
| 7726 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7727 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7728 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7729 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7730 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7731 | X509 *crt = NULL; |
| 7732 | |
| 7733 | if (!ssl_sock_is_ssl(conn)) |
| 7734 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7735 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7736 | |
| 7737 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7738 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7739 | if (!crt) |
| 7740 | return 0; |
| 7741 | |
| 7742 | X509_free(crt); |
| 7743 | return 1; |
| 7744 | } |
| 7745 | |
| 7746 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7747 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7748 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7749 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7750 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7751 | if (!ssl_sock_is_ssl(conn)) |
| 7752 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7753 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7754 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | /* returns result from SSL verify */ |
| 7758 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7759 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7760 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7761 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7762 | if (!ssl_sock_is_ssl(conn)) |
| 7763 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7764 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7765 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7766 | } |
| 7767 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7768 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7769 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7770 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7771 | * freed by the caller. NPN is also checked if available since older versions |
| 7772 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7773 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7774 | 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] | 7775 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7776 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7777 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7778 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7779 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7780 | return 0; |
| 7781 | |
| 7782 | *str = NULL; |
| 7783 | |
| 7784 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7785 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7786 | if (*str) |
| 7787 | return 1; |
| 7788 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7789 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7790 | 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] | 7791 | if (*str) |
| 7792 | return 1; |
| 7793 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7794 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7795 | return 0; |
| 7796 | } |
| 7797 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7798 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7799 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7800 | static int |
| 7801 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7802 | { |
| 7803 | struct connection *conn; |
| 7804 | |
| 7805 | conn = objt_conn(smp->sess->origin); |
| 7806 | if (!conn || conn->xprt != &ssl_sock) |
| 7807 | return 0; |
| 7808 | |
| 7809 | smp->flags = 0; |
| 7810 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7811 | #ifdef OPENSSL_IS_BORINGSSL |
| 7812 | { |
| 7813 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7814 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7815 | SSL_early_data_accepted(ctx->ssl)); |
| 7816 | } |
| 7817 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7818 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 7819 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7820 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7821 | return 1; |
| 7822 | } |
| 7823 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7824 | /* boolean, returns true if client cert was present */ |
| 7825 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7826 | 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] | 7827 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7828 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7829 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7830 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7831 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7832 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7833 | return 0; |
| 7834 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7835 | ctx = conn->xprt_ctx; |
| 7836 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7837 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7838 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7839 | return 0; |
| 7840 | } |
| 7841 | |
| 7842 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7843 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7844 | 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] | 7845 | |
| 7846 | return 1; |
| 7847 | } |
| 7848 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7849 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7850 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7851 | * should be use. |
| 7852 | */ |
| 7853 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7854 | 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] | 7855 | { |
| 7856 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7857 | X509 *crt = NULL; |
| 7858 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7859 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7860 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7861 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7862 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7863 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7864 | if (!conn || conn->xprt != &ssl_sock) |
| 7865 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7866 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7867 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7868 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7869 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7870 | return 0; |
| 7871 | } |
| 7872 | |
| 7873 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7874 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7875 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7876 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7877 | |
| 7878 | if (!crt) |
| 7879 | goto out; |
| 7880 | |
| 7881 | smp_trash = get_trash_chunk(); |
| 7882 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7883 | goto out; |
| 7884 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7885 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7886 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7887 | ret = 1; |
| 7888 | out: |
| 7889 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7890 | if (cert_peer && crt) |
| 7891 | X509_free(crt); |
| 7892 | return ret; |
| 7893 | } |
| 7894 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7895 | /* binary, returns serial of certificate in a binary chunk. |
| 7896 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7897 | * should be use. |
| 7898 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7899 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7900 | 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] | 7901 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7902 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7903 | X509 *crt = NULL; |
| 7904 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7905 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7906 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7907 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7908 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7909 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7910 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7911 | return 0; |
| 7912 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7913 | ctx = conn->xprt_ctx; |
| 7914 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7915 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7916 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7917 | return 0; |
| 7918 | } |
| 7919 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7920 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7921 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7922 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7923 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7924 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7925 | if (!crt) |
| 7926 | goto out; |
| 7927 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7928 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7929 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 7930 | goto out; |
| 7931 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7932 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7933 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7934 | ret = 1; |
| 7935 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7936 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7937 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7938 | X509_free(crt); |
| 7939 | return ret; |
| 7940 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7941 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7942 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 7943 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7944 | * should be use. |
| 7945 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7946 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7947 | 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] | 7948 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7949 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7950 | X509 *crt = NULL; |
| 7951 | const EVP_MD *digest; |
| 7952 | int ret = 0; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7953 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7954 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7955 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7956 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7957 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7958 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7959 | if (!conn || conn->xprt != &ssl_sock) |
| 7960 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7961 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7962 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7963 | if (conn->flags & CO_FL_WAIT_XPRT) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7964 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7965 | return 0; |
| 7966 | } |
| 7967 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7968 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7969 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7970 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7971 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7972 | if (!crt) |
| 7973 | goto out; |
| 7974 | |
| 7975 | smp_trash = get_trash_chunk(); |
| 7976 | digest = EVP_sha1(); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7977 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 7978 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7979 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7980 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7981 | ret = 1; |
| 7982 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7983 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7984 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7985 | X509_free(crt); |
| 7986 | return ret; |
| 7987 | } |
| 7988 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7989 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 7990 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7991 | * should be use. |
| 7992 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7993 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7994 | 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] | 7995 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7996 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7997 | X509 *crt = NULL; |
| 7998 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7999 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8000 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8001 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8002 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8003 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8004 | if (!conn || conn->xprt != &ssl_sock) |
| 8005 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8006 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8007 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8008 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8009 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8010 | return 0; |
| 8011 | } |
| 8012 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8013 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8014 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8015 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8016 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8017 | if (!crt) |
| 8018 | goto out; |
| 8019 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8020 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8021 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8022 | goto out; |
| 8023 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8024 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8025 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8026 | ret = 1; |
| 8027 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8028 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8029 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8030 | X509_free(crt); |
| 8031 | return ret; |
| 8032 | } |
| 8033 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8034 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 8035 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8036 | * should be use. |
| 8037 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8038 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8039 | 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] | 8040 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8041 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8042 | X509 *crt = NULL; |
| 8043 | X509_NAME *name; |
| 8044 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8045 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8046 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8047 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8048 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8049 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8050 | if (!conn || conn->xprt != &ssl_sock) |
| 8051 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8052 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8053 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8054 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8055 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8056 | return 0; |
| 8057 | } |
| 8058 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8059 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8060 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8061 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8062 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8063 | if (!crt) |
| 8064 | goto out; |
| 8065 | |
| 8066 | name = X509_get_issuer_name(crt); |
| 8067 | if (!name) |
| 8068 | goto out; |
| 8069 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8070 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8071 | if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8072 | int pos = 1; |
| 8073 | |
| 8074 | if (args[1].type == ARGT_SINT) |
| 8075 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8076 | |
| 8077 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8078 | goto out; |
| 8079 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8080 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8081 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8082 | goto out; |
| 8083 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8084 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8085 | goto out; |
| 8086 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8087 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8088 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8089 | ret = 1; |
| 8090 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8091 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8092 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8093 | X509_free(crt); |
| 8094 | return ret; |
| 8095 | } |
| 8096 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8097 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 8098 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8099 | * should be use. |
| 8100 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8101 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8102 | 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] | 8103 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8104 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8105 | X509 *crt = NULL; |
| 8106 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8107 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8108 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8109 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8110 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8111 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8112 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8113 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8114 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8115 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8116 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8117 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8118 | return 0; |
| 8119 | } |
| 8120 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8121 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8122 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8123 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8124 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8125 | if (!crt) |
| 8126 | goto out; |
| 8127 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8128 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8129 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8130 | goto out; |
| 8131 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8132 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8133 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8134 | ret = 1; |
| 8135 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8136 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8137 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8138 | X509_free(crt); |
| 8139 | return ret; |
| 8140 | } |
| 8141 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8142 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 8143 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8144 | * should be use. |
| 8145 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8146 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8147 | 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] | 8148 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8149 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8150 | X509 *crt = NULL; |
| 8151 | X509_NAME *name; |
| 8152 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8153 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8154 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8155 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8156 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8157 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8158 | if (!conn || conn->xprt != &ssl_sock) |
| 8159 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8160 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8161 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8162 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8163 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8164 | return 0; |
| 8165 | } |
| 8166 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8167 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8168 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8169 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8170 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8171 | if (!crt) |
| 8172 | goto out; |
| 8173 | |
| 8174 | name = X509_get_subject_name(crt); |
| 8175 | if (!name) |
| 8176 | goto out; |
| 8177 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8178 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8179 | if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8180 | int pos = 1; |
| 8181 | |
| 8182 | if (args[1].type == ARGT_SINT) |
| 8183 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8184 | |
| 8185 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8186 | goto out; |
| 8187 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8188 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8189 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8190 | goto out; |
| 8191 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8192 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8193 | goto out; |
| 8194 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8195 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8196 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8197 | ret = 1; |
| 8198 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8199 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8200 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8201 | X509_free(crt); |
| 8202 | return ret; |
| 8203 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8204 | |
| 8205 | /* integer, returns true if current session use a client certificate */ |
| 8206 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8207 | 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] | 8208 | { |
| 8209 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8210 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8211 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8212 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8213 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8214 | if (!conn || conn->xprt != &ssl_sock) |
| 8215 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8216 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8217 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8218 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8219 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8220 | return 0; |
| 8221 | } |
| 8222 | |
| 8223 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8224 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8225 | if (crt) { |
| 8226 | X509_free(crt); |
| 8227 | } |
| 8228 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8229 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8230 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8231 | return 1; |
| 8232 | } |
| 8233 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8234 | /* integer, returns the certificate version |
| 8235 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8236 | * should be use. |
| 8237 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8238 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8239 | 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] | 8240 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8241 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8242 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8243 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8244 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8245 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8246 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8247 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8248 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8249 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8250 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8251 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8252 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8253 | return 0; |
| 8254 | } |
| 8255 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8256 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8257 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8258 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8259 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8260 | if (!crt) |
| 8261 | return 0; |
| 8262 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8263 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8264 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8265 | if (cert_peer) |
| 8266 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8267 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8268 | |
| 8269 | return 1; |
| 8270 | } |
| 8271 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8272 | /* string, returns the certificate's signature algorithm. |
| 8273 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8274 | * should be use. |
| 8275 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8276 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8277 | 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] | 8278 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8279 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8280 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8281 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8282 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8283 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8284 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8285 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8286 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8287 | if (!conn || conn->xprt != &ssl_sock) |
| 8288 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8289 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8290 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8291 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8292 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8293 | return 0; |
| 8294 | } |
| 8295 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8296 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8297 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8298 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8299 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8300 | if (!crt) |
| 8301 | return 0; |
| 8302 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8303 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 8304 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8305 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8306 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8307 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8308 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8309 | if (cert_peer) |
| 8310 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8311 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8312 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8313 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8314 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8315 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8316 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8317 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8318 | if (cert_peer) |
| 8319 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8320 | |
| 8321 | return 1; |
| 8322 | } |
| 8323 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8324 | /* string, returns the certificate's key algorithm. |
| 8325 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8326 | * should be use. |
| 8327 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8328 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8329 | 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] | 8330 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8331 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8332 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8333 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8334 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8335 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8336 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8337 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8338 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8339 | if (!conn || conn->xprt != &ssl_sock) |
| 8340 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8341 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8342 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8343 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8344 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8345 | return 0; |
| 8346 | } |
| 8347 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8348 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8349 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8350 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8351 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8352 | if (!crt) |
| 8353 | return 0; |
| 8354 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8355 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 8356 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8357 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8358 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8359 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8360 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8361 | if (cert_peer) |
| 8362 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8363 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8364 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8365 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8366 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8367 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8368 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8369 | if (cert_peer) |
| 8370 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8371 | |
| 8372 | return 1; |
| 8373 | } |
| 8374 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8375 | /* boolean, returns true if front conn. transport layer is SSL. |
| 8376 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8377 | * char is 'b'. |
| 8378 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8379 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8380 | 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] | 8381 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8382 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8383 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8384 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8385 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8386 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8387 | return 1; |
| 8388 | } |
| 8389 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8390 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8391 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8392 | 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] | 8393 | { |
| 8394 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8395 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8396 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8397 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8398 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8399 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8400 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8401 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8402 | return 1; |
| 8403 | #else |
| 8404 | return 0; |
| 8405 | #endif |
| 8406 | } |
| 8407 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8408 | /* boolean, returns true if client session has been resumed. |
| 8409 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8410 | * char is 'b'. |
| 8411 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8412 | static int |
| 8413 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8414 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8415 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8416 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8417 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8418 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8419 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8420 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8421 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8422 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8423 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8424 | return 1; |
| 8425 | } |
| 8426 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8427 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 8428 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8429 | * char is 'b'. |
| 8430 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8431 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8432 | 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] | 8433 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8434 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8435 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8436 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8437 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8438 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8439 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8440 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8441 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8442 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8443 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8444 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8445 | return 0; |
| 8446 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8447 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8448 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8449 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8450 | |
| 8451 | return 1; |
| 8452 | } |
| 8453 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8454 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 8455 | * is SSL. |
| 8456 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8457 | * char is 'b'. |
| 8458 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8459 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8460 | 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] | 8461 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8462 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8463 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8464 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8465 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8466 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8467 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8468 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8469 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8470 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8471 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8472 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8473 | return 0; |
| 8474 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8475 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8476 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8477 | |
| 8478 | return 1; |
| 8479 | } |
| 8480 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8481 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 8482 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8483 | * char is 'b'. |
| 8484 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8485 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8486 | 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] | 8487 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8488 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8489 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8490 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8491 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8492 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8493 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8494 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8495 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8496 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8497 | 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] | 8498 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8499 | return 0; |
| 8500 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8501 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8502 | |
| 8503 | return 1; |
| 8504 | } |
| 8505 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8506 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8507 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8508 | 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] | 8509 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8510 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8511 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8512 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8513 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8514 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8515 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8516 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8517 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8518 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8519 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8520 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8521 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8522 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8523 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8524 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8525 | (const unsigned char **)&smp->data.u.str.area, |
| 8526 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8527 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8528 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8529 | return 0; |
| 8530 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8531 | smp->data.u.str.data = len; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8532 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8533 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8534 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8535 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8536 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8537 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8538 | 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] | 8539 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8540 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8541 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8542 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8543 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8544 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8545 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8546 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8547 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8548 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8549 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8550 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8551 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8552 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8553 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8554 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8555 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8556 | (const unsigned char **)&smp->data.u.str.area, |
| 8557 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8558 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8559 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8560 | return 0; |
| 8561 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8562 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8563 | return 1; |
| 8564 | } |
| 8565 | #endif |
| 8566 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8567 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 8568 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8569 | * char is 'b'. |
| 8570 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8572 | 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] | 8573 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8574 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8575 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8576 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8577 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8578 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8579 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8580 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8581 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8582 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8583 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8584 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8585 | return 0; |
| 8586 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8587 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8588 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8589 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8590 | |
| 8591 | return 1; |
| 8592 | } |
| 8593 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8594 | /* 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] | 8595 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8596 | * char is 'b'. |
| 8597 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8598 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8599 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8600 | 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] | 8601 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8602 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8603 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8604 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8605 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8606 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8607 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8608 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8609 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8610 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8611 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8612 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8613 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8614 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8615 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8616 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8617 | return 0; |
| 8618 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8619 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8620 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8621 | return 0; |
| 8622 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8623 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8624 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8625 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8626 | #endif |
| 8627 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8628 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8629 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8630 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 8631 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8632 | { |
| 8633 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8634 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8635 | struct buffer *data; |
| 8636 | struct ssl_sock_ctx *ctx; |
| 8637 | |
| 8638 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8639 | return 0; |
| 8640 | ctx = conn->xprt_ctx; |
| 8641 | |
| 8642 | data = get_trash_chunk(); |
| 8643 | if (kw[7] == 'c') |
| 8644 | data->data = SSL_get_client_random(ctx->ssl, |
| 8645 | (unsigned char *) data->area, |
| 8646 | data->size); |
| 8647 | else |
| 8648 | data->data = SSL_get_server_random(ctx->ssl, |
| 8649 | (unsigned char *) data->area, |
| 8650 | data->size); |
| 8651 | if (!data->data) |
| 8652 | return 0; |
| 8653 | |
| 8654 | smp->flags = 0; |
| 8655 | smp->data.type = SMP_T_BIN; |
| 8656 | smp->data.u.str = *data; |
| 8657 | |
| 8658 | return 1; |
| 8659 | } |
| 8660 | |
| 8661 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8662 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8663 | { |
| 8664 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8665 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8666 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8667 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8668 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8669 | |
| 8670 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8671 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8672 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8673 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8674 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8675 | if (!ssl_sess) |
| 8676 | return 0; |
| 8677 | |
| 8678 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8679 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8680 | (unsigned char *) data->area, |
| 8681 | data->size); |
| 8682 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8683 | return 0; |
| 8684 | |
| 8685 | smp->flags = 0; |
| 8686 | smp->data.type = SMP_T_BIN; |
| 8687 | smp->data.u.str = *data; |
| 8688 | |
| 8689 | return 1; |
| 8690 | } |
| 8691 | #endif |
| 8692 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8693 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8694 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8695 | 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] | 8696 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8697 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8698 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8699 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8700 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8701 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8702 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8703 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8704 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8705 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8706 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8707 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8708 | 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] | 8709 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8710 | return 0; |
| 8711 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8712 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8713 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8714 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8715 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8716 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8717 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8718 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8719 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8720 | struct connection *conn; |
| 8721 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8722 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8723 | |
| 8724 | conn = objt_conn(smp->sess->origin); |
| 8725 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8726 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8727 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8728 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8729 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8730 | if (!capture) |
| 8731 | return 0; |
| 8732 | |
| 8733 | smp->flags = SMP_F_CONST; |
| 8734 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8735 | smp->data.u.str.area = capture->ciphersuite; |
| 8736 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8737 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | static int |
| 8741 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8742 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8743 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8744 | |
| 8745 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8746 | return 0; |
| 8747 | |
| 8748 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8749 | 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] | 8750 | smp->data.type = SMP_T_BIN; |
| 8751 | smp->data.u.str = *data; |
| 8752 | return 1; |
| 8753 | } |
| 8754 | |
| 8755 | static int |
| 8756 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8757 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8758 | struct connection *conn; |
| 8759 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8760 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8761 | |
| 8762 | conn = objt_conn(smp->sess->origin); |
| 8763 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8764 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8765 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8766 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8767 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8768 | if (!capture) |
| 8769 | return 0; |
| 8770 | |
| 8771 | smp->data.type = SMP_T_SINT; |
| 8772 | smp->data.u.sint = capture->xxh64; |
| 8773 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8774 | } |
| 8775 | |
| 8776 | static int |
| 8777 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8778 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8779 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8780 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8781 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8782 | |
| 8783 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8784 | return 0; |
| 8785 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8786 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8787 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8788 | const char *str; |
| 8789 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8790 | 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] | 8791 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8792 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8793 | cipher = SSL_get_cipher_by_value(id); |
| 8794 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8795 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8796 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8797 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8798 | #endif |
| 8799 | str = SSL_CIPHER_get_name(cipher); |
| 8800 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8801 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8802 | else |
| 8803 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8804 | } |
| 8805 | smp->data.type = SMP_T_STR; |
| 8806 | smp->data.u.str = *data; |
| 8807 | return 1; |
| 8808 | #else |
| 8809 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8810 | #endif |
| 8811 | } |
| 8812 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8813 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8814 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8815 | 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] | 8816 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8817 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8818 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8819 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8820 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8821 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8822 | |
| 8823 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8824 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8825 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8826 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8827 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8828 | if (conn->flags & CO_FL_WAIT_XPRT) { |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8829 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8830 | return 0; |
| 8831 | } |
| 8832 | |
| 8833 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8834 | if (!SSL_session_reused(ctx->ssl)) |
| 8835 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8836 | finished_trash->area, |
| 8837 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8838 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8839 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8840 | finished_trash->area, |
| 8841 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8842 | |
| 8843 | if (!finished_len) |
| 8844 | return 0; |
| 8845 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8846 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8847 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8848 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8849 | |
| 8850 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8851 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8852 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8853 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8854 | /* 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] | 8855 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8856 | 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] | 8857 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8858 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8859 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8860 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8861 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8862 | if (!conn || conn->xprt != &ssl_sock) |
| 8863 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8864 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8865 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8866 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8867 | smp->flags = SMP_F_MAY_CHANGE; |
| 8868 | return 0; |
| 8869 | } |
| 8870 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8871 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8872 | 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] | 8873 | smp->flags = 0; |
| 8874 | |
| 8875 | return 1; |
| 8876 | } |
| 8877 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8878 | /* 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] | 8879 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8880 | 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] | 8881 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8882 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8883 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8884 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8885 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8886 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8887 | return 0; |
| 8888 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8889 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8890 | smp->flags = SMP_F_MAY_CHANGE; |
| 8891 | return 0; |
| 8892 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8893 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8894 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8895 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8896 | 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] | 8897 | smp->flags = 0; |
| 8898 | |
| 8899 | return 1; |
| 8900 | } |
| 8901 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8902 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8903 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8904 | 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] | 8905 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8906 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8907 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8908 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8909 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8910 | if (!conn || conn->xprt != &ssl_sock) |
| 8911 | return 0; |
| 8912 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8913 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8914 | smp->flags = SMP_F_MAY_CHANGE; |
| 8915 | return 0; |
| 8916 | } |
| 8917 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8918 | ctx = conn->xprt_ctx; |
| 8919 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8920 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8921 | 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] | 8922 | smp->flags = 0; |
| 8923 | |
| 8924 | return 1; |
| 8925 | } |
| 8926 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8927 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8928 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8929 | 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] | 8930 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8931 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8932 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8933 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8934 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8935 | if (!conn || conn->xprt != &ssl_sock) |
| 8936 | return 0; |
| 8937 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8938 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8939 | smp->flags = SMP_F_MAY_CHANGE; |
| 8940 | return 0; |
| 8941 | } |
| 8942 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8943 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8944 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8945 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8946 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8947 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8948 | 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] | 8949 | smp->flags = 0; |
| 8950 | |
| 8951 | return 1; |
| 8952 | } |
| 8953 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8954 | /* for ca-file and ca-verify-file */ |
| 8955 | static int ssl_bind_parse_ca_file_common(char **args, int cur_arg, char **ca_file_p, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8956 | { |
| 8957 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8958 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8959 | return ERR_ALERT | ERR_FATAL; |
| 8960 | } |
| 8961 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8962 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8963 | memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8964 | else |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8965 | memprintf(ca_file_p, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8966 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8967 | if (!ssl_store_load_locations_file(*ca_file_p)) { |
| 8968 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p); |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 8969 | return ERR_ALERT | ERR_FATAL; |
| 8970 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8971 | return 0; |
| 8972 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8973 | |
| 8974 | /* parse the "ca-file" bind keyword */ |
| 8975 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8976 | { |
| 8977 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err); |
| 8978 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8979 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8980 | { |
| 8981 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8982 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8983 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8984 | /* parse the "ca-verify-file" bind keyword */ |
| 8985 | static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8986 | { |
| 8987 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err); |
| 8988 | } |
| 8989 | static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8990 | { |
| 8991 | return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8992 | } |
| 8993 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8994 | /* parse the "ca-sign-file" bind keyword */ |
| 8995 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8996 | { |
| 8997 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8998 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8999 | return ERR_ALERT | ERR_FATAL; |
| 9000 | } |
| 9001 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9002 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9003 | 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] | 9004 | else |
| 9005 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 9006 | |
| 9007 | return 0; |
| 9008 | } |
| 9009 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9010 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9011 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9012 | { |
| 9013 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9014 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9015 | return ERR_ALERT | ERR_FATAL; |
| 9016 | } |
| 9017 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 9018 | return 0; |
| 9019 | } |
| 9020 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9021 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9022 | 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] | 9023 | { |
| 9024 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9025 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9026 | return ERR_ALERT | ERR_FATAL; |
| 9027 | } |
| 9028 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9029 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9030 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9031 | return 0; |
| 9032 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9033 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9034 | { |
| 9035 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 9036 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9037 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9038 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9039 | /* parse the "ciphersuites" bind keyword */ |
| 9040 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9041 | { |
| 9042 | if (!*args[cur_arg + 1]) { |
| 9043 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 9044 | return ERR_ALERT | ERR_FATAL; |
| 9045 | } |
| 9046 | |
| 9047 | free(conf->ciphersuites); |
| 9048 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 9049 | return 0; |
| 9050 | } |
| 9051 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9052 | { |
| 9053 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 9054 | } |
| 9055 | #endif |
| 9056 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9057 | /* 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] | 9058 | 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] | 9059 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 9060 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 9061 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9062 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9063 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9064 | return ERR_ALERT | ERR_FATAL; |
| 9065 | } |
| 9066 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9067 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 9068 | 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] | 9069 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 9070 | return ERR_ALERT | ERR_FATAL; |
| 9071 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9072 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9073 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9076 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9077 | } |
| 9078 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9079 | /* 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] | 9080 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9081 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9082 | int err_code; |
| 9083 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9084 | if (!*args[cur_arg + 1]) { |
| 9085 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 9086 | return ERR_ALERT | ERR_FATAL; |
| 9087 | } |
| 9088 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 9089 | err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9090 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 9091 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9092 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9093 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9094 | } |
| 9095 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 9096 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9097 | 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] | 9098 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9099 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9100 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9101 | return ERR_ALERT | ERR_FATAL; |
| 9102 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9103 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9104 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9105 | return ERR_ALERT | ERR_FATAL; |
| 9106 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9107 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9108 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9109 | 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] | 9110 | else |
| 9111 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9112 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9113 | if (!ssl_store_load_locations_file(conf->crl_file)) { |
| 9114 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file); |
| 9115 | return ERR_ALERT | ERR_FATAL; |
| 9116 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9117 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9118 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9119 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9120 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9121 | { |
| 9122 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9123 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9124 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9125 | /* parse the "curves" bind keyword keyword */ |
| 9126 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9127 | { |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 9128 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9129 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9130 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9131 | return ERR_ALERT | ERR_FATAL; |
| 9132 | } |
| 9133 | conf->curves = strdup(args[cur_arg + 1]); |
| 9134 | return 0; |
| 9135 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9136 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9137 | return ERR_ALERT | ERR_FATAL; |
| 9138 | #endif |
| 9139 | } |
| 9140 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9141 | { |
| 9142 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 9143 | } |
| 9144 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9145 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9146 | 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] | 9147 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9148 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9149 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9150 | return ERR_ALERT | ERR_FATAL; |
| 9151 | #elif defined(OPENSSL_NO_ECDH) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9152 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9153 | return ERR_ALERT | ERR_FATAL; |
| 9154 | #else |
| 9155 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9156 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9157 | return ERR_ALERT | ERR_FATAL; |
| 9158 | } |
| 9159 | |
| 9160 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9161 | |
| 9162 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9163 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9164 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9165 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9166 | { |
| 9167 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 9168 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9169 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9170 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9171 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9172 | { |
| 9173 | int code; |
| 9174 | char *p = args[cur_arg + 1]; |
| 9175 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 9176 | |
| 9177 | if (!*p) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9178 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9179 | return ERR_ALERT | ERR_FATAL; |
| 9180 | } |
| 9181 | |
| 9182 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 9183 | ignerr = &conf->ca_ignerr; |
| 9184 | |
| 9185 | if (strcmp(p, "all") == 0) { |
| 9186 | *ignerr = ~0ULL; |
| 9187 | return 0; |
| 9188 | } |
| 9189 | |
| 9190 | while (p) { |
| 9191 | code = atoi(p); |
| 9192 | if ((code <= 0) || (code > 63)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9193 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 9194 | args[cur_arg], code, args[cur_arg + 1]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9195 | return ERR_ALERT | ERR_FATAL; |
| 9196 | } |
| 9197 | *ignerr |= 1ULL << code; |
| 9198 | p = strchr(p, ','); |
| 9199 | if (p) |
| 9200 | p++; |
| 9201 | } |
| 9202 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9203 | return 0; |
| 9204 | } |
| 9205 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9206 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 9207 | 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] | 9208 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9209 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9210 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9211 | p = strchr(arg, '-'); |
| 9212 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9213 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9214 | p++; |
| 9215 | if (!strcmp(p, "sslv3")) |
| 9216 | v = CONF_SSLV3; |
| 9217 | else if (!strcmp(p, "tlsv10")) |
| 9218 | v = CONF_TLSV10; |
| 9219 | else if (!strcmp(p, "tlsv11")) |
| 9220 | v = CONF_TLSV11; |
| 9221 | else if (!strcmp(p, "tlsv12")) |
| 9222 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9223 | else if (!strcmp(p, "tlsv13")) |
| 9224 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9225 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9226 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9227 | if (!strncmp(arg, "no-", 3)) |
| 9228 | methods->flags |= methodVersions[v].flag; |
| 9229 | else if (!strncmp(arg, "force-", 6)) |
| 9230 | methods->min = methods->max = v; |
| 9231 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9232 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9233 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9234 | fail: |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9235 | memprintf(err, "'%s' : option not implemented", arg); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9236 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9237 | } |
| 9238 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9239 | 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] | 9240 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9241 | 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] | 9242 | } |
| 9243 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9244 | 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] | 9245 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9246 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 9247 | } |
| 9248 | |
| 9249 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 9250 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 9251 | { |
| 9252 | uint16_t i, v = 0; |
| 9253 | char *argv = args[cur_arg + 1]; |
| 9254 | if (!*argv) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9255 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9256 | return ERR_ALERT | ERR_FATAL; |
| 9257 | } |
| 9258 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9259 | if (!strcmp(argv, methodVersions[i].name)) |
| 9260 | v = i; |
| 9261 | if (!v) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9262 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9263 | return ERR_ALERT | ERR_FATAL; |
| 9264 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9265 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 9266 | methods->min = v; |
| 9267 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 9268 | methods->max = v; |
| 9269 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9270 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9271 | return ERR_ALERT | ERR_FATAL; |
| 9272 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9273 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9274 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9275 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9276 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9277 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9278 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9279 | 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] | 9280 | #endif |
| 9281 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 9282 | } |
| 9283 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9284 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9285 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9286 | 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] | 9287 | } |
| 9288 | |
| 9289 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9290 | { |
| 9291 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 9292 | } |
| 9293 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9294 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9295 | 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] | 9296 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 9297 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9298 | return 0; |
| 9299 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9300 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9301 | /* parse the "allow-0rtt" bind keyword */ |
| 9302 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9303 | { |
| 9304 | conf->early_data = 1; |
| 9305 | return 0; |
| 9306 | } |
| 9307 | |
| 9308 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9309 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 9310 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9311 | return 0; |
| 9312 | } |
| 9313 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9314 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9315 | 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] | 9316 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9317 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9318 | char *p1, *p2; |
| 9319 | |
| 9320 | if (!*args[cur_arg + 1]) { |
| 9321 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 9322 | return ERR_ALERT | ERR_FATAL; |
| 9323 | } |
| 9324 | |
| 9325 | free(conf->npn_str); |
| 9326 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9327 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9328 | * so we reuse each comma to store the next <len> and need |
| 9329 | * one more for the end of the string. |
| 9330 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9331 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9332 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9333 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 9334 | |
| 9335 | /* replace commas with the name length */ |
| 9336 | p1 = conf->npn_str; |
| 9337 | p2 = p1 + 1; |
| 9338 | while (1) { |
| 9339 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 9340 | if (!p2) |
| 9341 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9342 | |
| 9343 | if (p2 - (p1 + 1) > 255) { |
| 9344 | *p2 = '\0'; |
| 9345 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9346 | return ERR_ALERT | ERR_FATAL; |
| 9347 | } |
| 9348 | |
| 9349 | *p1 = p2 - (p1 + 1); |
| 9350 | p1 = p2; |
| 9351 | |
| 9352 | if (!*p2) |
| 9353 | break; |
| 9354 | |
| 9355 | *(p2++) = '\0'; |
| 9356 | } |
| 9357 | return 0; |
| 9358 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9359 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9360 | return ERR_ALERT | ERR_FATAL; |
| 9361 | #endif |
| 9362 | } |
| 9363 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9364 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9365 | { |
| 9366 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9367 | } |
| 9368 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9369 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9370 | 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] | 9371 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9372 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9373 | char *p1, *p2; |
| 9374 | |
| 9375 | if (!*args[cur_arg + 1]) { |
| 9376 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 9377 | return ERR_ALERT | ERR_FATAL; |
| 9378 | } |
| 9379 | |
| 9380 | free(conf->alpn_str); |
| 9381 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9382 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9383 | * so we reuse each comma to store the next <len> and need |
| 9384 | * one more for the end of the string. |
| 9385 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9386 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9387 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9388 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 9389 | |
| 9390 | /* replace commas with the name length */ |
| 9391 | p1 = conf->alpn_str; |
| 9392 | p2 = p1 + 1; |
| 9393 | while (1) { |
| 9394 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 9395 | if (!p2) |
| 9396 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9397 | |
| 9398 | if (p2 - (p1 + 1) > 255) { |
| 9399 | *p2 = '\0'; |
| 9400 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9401 | return ERR_ALERT | ERR_FATAL; |
| 9402 | } |
| 9403 | |
| 9404 | *p1 = p2 - (p1 + 1); |
| 9405 | p1 = p2; |
| 9406 | |
| 9407 | if (!*p2) |
| 9408 | break; |
| 9409 | |
| 9410 | *(p2++) = '\0'; |
| 9411 | } |
| 9412 | return 0; |
| 9413 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9414 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9415 | return ERR_ALERT | ERR_FATAL; |
| 9416 | #endif |
| 9417 | } |
| 9418 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9419 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9420 | { |
| 9421 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9422 | } |
| 9423 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9424 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9425 | 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] | 9426 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 9427 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9428 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9429 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9430 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 9431 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9432 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9433 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 9434 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9435 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9436 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9437 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 9438 | if (!conf->ssl_conf.ssl_methods.min) |
| 9439 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 9440 | if (!conf->ssl_conf.ssl_methods.max) |
| 9441 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9442 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9443 | return 0; |
| 9444 | } |
| 9445 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9446 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 9447 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9448 | { |
| 9449 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 9450 | return 0; |
| 9451 | } |
| 9452 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9453 | /* parse the "generate-certificates" bind keyword */ |
| 9454 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9455 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9456 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9457 | conf->generate_certs = 1; |
| 9458 | #else |
| 9459 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 9460 | err && *err ? *err : ""); |
| 9461 | #endif |
| 9462 | return 0; |
| 9463 | } |
| 9464 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9465 | /* parse the "strict-sni" bind keyword */ |
| 9466 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9467 | { |
| 9468 | conf->strict_sni = 1; |
| 9469 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9470 | } |
| 9471 | |
| 9472 | /* parse the "tls-ticket-keys" bind keyword */ |
| 9473 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9474 | { |
| 9475 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9476 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9477 | int i = 0; |
| 9478 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9479 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9480 | |
| 9481 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9482 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9483 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9484 | } |
| 9485 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9486 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9487 | if (keys_ref) { |
| 9488 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9489 | conf->keys_ref = keys_ref; |
| 9490 | return 0; |
| 9491 | } |
| 9492 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9493 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9494 | if (!keys_ref) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9495 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9496 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9497 | } |
| 9498 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9499 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9500 | if (!keys_ref->tlskeys) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9501 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9502 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9503 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9504 | |
| 9505 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9506 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9507 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9508 | } |
| 9509 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9510 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9511 | if (!keys_ref->filename) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9512 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9513 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9514 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9515 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9516 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9517 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 9518 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9519 | int dec_size; |
| 9520 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9521 | /* Strip newline characters from the end */ |
| 9522 | if(thisline[len - 1] == '\n') |
| 9523 | thisline[--len] = 0; |
| 9524 | |
| 9525 | if(thisline[len - 1] == '\r') |
| 9526 | thisline[--len] = 0; |
| 9527 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9528 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 9529 | if (dec_size < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9530 | memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9531 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9532 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9533 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 9534 | keys_ref->key_size_bits = 128; |
| 9535 | } |
| 9536 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 9537 | keys_ref->key_size_bits = 256; |
| 9538 | } |
| 9539 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 9540 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 9541 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9542 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9543 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9544 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9545 | i++; |
| 9546 | } |
| 9547 | |
| 9548 | if (i < TLS_TICKETS_NO) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9549 | memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9550 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9551 | } |
| 9552 | |
| 9553 | fclose(f); |
| 9554 | |
| 9555 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 9556 | i -= 2; |
| 9557 | 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] | 9558 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9559 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9560 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9561 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9562 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9563 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 9564 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9565 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9566 | |
| 9567 | fail: |
| 9568 | if (f) |
| 9569 | fclose(f); |
| 9570 | if (keys_ref) { |
| 9571 | free(keys_ref->filename); |
| 9572 | free(keys_ref->tlskeys); |
| 9573 | free(keys_ref); |
| 9574 | } |
| 9575 | return ERR_ALERT | ERR_FATAL; |
| 9576 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9577 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9578 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9579 | return ERR_ALERT | ERR_FATAL; |
| 9580 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9581 | } |
| 9582 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9583 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9584 | 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] | 9585 | { |
| 9586 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9587 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9588 | return ERR_ALERT | ERR_FATAL; |
| 9589 | } |
| 9590 | |
| 9591 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9592 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9593 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9594 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9595 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9596 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9597 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9598 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 9599 | args[cur_arg], args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9600 | return ERR_ALERT | ERR_FATAL; |
| 9601 | } |
| 9602 | |
| 9603 | return 0; |
| 9604 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9605 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9606 | { |
| 9607 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 9608 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9609 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9610 | /* parse the "no-ca-names" bind keyword */ |
| 9611 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9612 | { |
| 9613 | conf->no_ca_names = 1; |
| 9614 | return 0; |
| 9615 | } |
| 9616 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9617 | { |
| 9618 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 9619 | } |
| 9620 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9621 | /************** "server" keywords ****************/ |
| 9622 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9623 | /* parse the "npn" bind keyword */ |
| 9624 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9625 | { |
| 9626 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9627 | char *p1, *p2; |
| 9628 | |
| 9629 | if (!*args[*cur_arg + 1]) { |
| 9630 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 9631 | return ERR_ALERT | ERR_FATAL; |
| 9632 | } |
| 9633 | |
| 9634 | free(newsrv->ssl_ctx.npn_str); |
| 9635 | |
| 9636 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9637 | * so we reuse each comma to store the next <len> and need |
| 9638 | * one more for the end of the string. |
| 9639 | */ |
| 9640 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9641 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 9642 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 9643 | newsrv->ssl_ctx.npn_len); |
| 9644 | |
| 9645 | /* replace commas with the name length */ |
| 9646 | p1 = newsrv->ssl_ctx.npn_str; |
| 9647 | p2 = p1 + 1; |
| 9648 | while (1) { |
| 9649 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 9650 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 9651 | if (!p2) |
| 9652 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9653 | |
| 9654 | if (p2 - (p1 + 1) > 255) { |
| 9655 | *p2 = '\0'; |
| 9656 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9657 | return ERR_ALERT | ERR_FATAL; |
| 9658 | } |
| 9659 | |
| 9660 | *p1 = p2 - (p1 + 1); |
| 9661 | p1 = p2; |
| 9662 | |
| 9663 | if (!*p2) |
| 9664 | break; |
| 9665 | |
| 9666 | *(p2++) = '\0'; |
| 9667 | } |
| 9668 | return 0; |
| 9669 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9670 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9671 | return ERR_ALERT | ERR_FATAL; |
| 9672 | #endif |
| 9673 | } |
| 9674 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9675 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9676 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9677 | { |
| 9678 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9679 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9680 | char **alpn_str; |
| 9681 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9682 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9683 | if (*args[*cur_arg] == 'c') { |
| 9684 | alpn_str = &newsrv->check.alpn_str; |
| 9685 | alpn_len = &newsrv->check.alpn_len; |
| 9686 | } else { |
| 9687 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9688 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9689 | |
| 9690 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9691 | if (!*args[*cur_arg + 1]) { |
| 9692 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 9693 | return ERR_ALERT | ERR_FATAL; |
| 9694 | } |
| 9695 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9696 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9697 | |
| 9698 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9699 | * so we reuse each comma to store the next <len> and need |
| 9700 | * one more for the end of the string. |
| 9701 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9702 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9703 | *alpn_str = calloc(1, *alpn_len + 1); |
| 9704 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9705 | |
| 9706 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9707 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9708 | p2 = p1 + 1; |
| 9709 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9710 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9711 | if (!p2) |
| 9712 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9713 | |
| 9714 | if (p2 - (p1 + 1) > 255) { |
| 9715 | *p2 = '\0'; |
| 9716 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9717 | return ERR_ALERT | ERR_FATAL; |
| 9718 | } |
| 9719 | |
| 9720 | *p1 = p2 - (p1 + 1); |
| 9721 | p1 = p2; |
| 9722 | |
| 9723 | if (!*p2) |
| 9724 | break; |
| 9725 | |
| 9726 | *(p2++) = '\0'; |
| 9727 | } |
| 9728 | return 0; |
| 9729 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9730 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9731 | return ERR_ALERT | ERR_FATAL; |
| 9732 | #endif |
| 9733 | } |
| 9734 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9735 | /* parse the "ca-file" server keyword */ |
| 9736 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9737 | { |
| 9738 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9739 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9740 | return ERR_ALERT | ERR_FATAL; |
| 9741 | } |
| 9742 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9743 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9744 | 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] | 9745 | else |
| 9746 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9747 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 9748 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) { |
| 9749 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file); |
| 9750 | return ERR_ALERT | ERR_FATAL; |
| 9751 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9752 | return 0; |
| 9753 | } |
| 9754 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9755 | /* parse the "check-sni" server keyword */ |
| 9756 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9757 | { |
| 9758 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9759 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9760 | return ERR_ALERT | ERR_FATAL; |
| 9761 | } |
| 9762 | |
| 9763 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9764 | if (!newsrv->check.sni) { |
| 9765 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9766 | return ERR_ALERT | ERR_FATAL; |
| 9767 | } |
| 9768 | return 0; |
| 9769 | |
| 9770 | } |
| 9771 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9772 | /* parse the "check-ssl" server keyword */ |
| 9773 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9774 | { |
| 9775 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9776 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9777 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9778 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9779 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9780 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9781 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9782 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9783 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9784 | if (!newsrv->ssl_ctx.methods.min) |
| 9785 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9786 | if (!newsrv->ssl_ctx.methods.max) |
| 9787 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9788 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9789 | return 0; |
| 9790 | } |
| 9791 | |
| 9792 | /* parse the "ciphers" server keyword */ |
| 9793 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9794 | { |
| 9795 | if (!*args[*cur_arg + 1]) { |
| 9796 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9797 | return ERR_ALERT | ERR_FATAL; |
| 9798 | } |
| 9799 | |
| 9800 | free(newsrv->ssl_ctx.ciphers); |
| 9801 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9802 | return 0; |
| 9803 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9804 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9805 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9806 | /* parse the "ciphersuites" server keyword */ |
| 9807 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9808 | { |
| 9809 | if (!*args[*cur_arg + 1]) { |
| 9810 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9811 | return ERR_ALERT | ERR_FATAL; |
| 9812 | } |
| 9813 | |
| 9814 | free(newsrv->ssl_ctx.ciphersuites); |
| 9815 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9816 | return 0; |
| 9817 | } |
| 9818 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9819 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9820 | /* parse the "crl-file" server keyword */ |
| 9821 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9822 | { |
| 9823 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9824 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9825 | return ERR_ALERT | ERR_FATAL; |
| 9826 | #else |
| 9827 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9828 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9829 | return ERR_ALERT | ERR_FATAL; |
| 9830 | } |
| 9831 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9832 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9833 | 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] | 9834 | else |
| 9835 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9836 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9837 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) { |
| 9838 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file); |
| 9839 | return ERR_ALERT | ERR_FATAL; |
| 9840 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9841 | return 0; |
| 9842 | #endif |
| 9843 | } |
| 9844 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9845 | /* parse the "crt" server keyword */ |
| 9846 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9847 | { |
| 9848 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9849 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9850 | return ERR_ALERT | ERR_FATAL; |
| 9851 | } |
| 9852 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9853 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9854 | 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] | 9855 | else |
| 9856 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9857 | |
| 9858 | return 0; |
| 9859 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9860 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9861 | /* parse the "no-check-ssl" server keyword */ |
| 9862 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9863 | { |
| 9864 | newsrv->check.use_ssl = 0; |
| 9865 | free(newsrv->ssl_ctx.ciphers); |
| 9866 | newsrv->ssl_ctx.ciphers = NULL; |
| 9867 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9868 | return 0; |
| 9869 | } |
| 9870 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9871 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9872 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9873 | { |
| 9874 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9875 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9876 | return 0; |
| 9877 | } |
| 9878 | |
| 9879 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9880 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9881 | { |
| 9882 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9883 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9884 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9885 | return 0; |
| 9886 | } |
| 9887 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9888 | /* parse the "no-ssl" server keyword */ |
| 9889 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9890 | { |
| 9891 | newsrv->use_ssl = 0; |
| 9892 | free(newsrv->ssl_ctx.ciphers); |
| 9893 | newsrv->ssl_ctx.ciphers = NULL; |
| 9894 | return 0; |
| 9895 | } |
| 9896 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9897 | /* parse the "allow-0rtt" server keyword */ |
| 9898 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9899 | { |
| 9900 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9901 | return 0; |
| 9902 | } |
| 9903 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9904 | /* parse the "no-ssl-reuse" server keyword */ |
| 9905 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9906 | { |
| 9907 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9908 | return 0; |
| 9909 | } |
| 9910 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9911 | /* parse the "no-tls-tickets" server keyword */ |
| 9912 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9913 | { |
| 9914 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 9915 | return 0; |
| 9916 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 9917 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 9918 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9919 | { |
| 9920 | newsrv->pp_opts |= SRV_PP_V2; |
| 9921 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9922 | return 0; |
| 9923 | } |
| 9924 | |
| 9925 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 9926 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9927 | { |
| 9928 | newsrv->pp_opts |= SRV_PP_V2; |
| 9929 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9930 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 9931 | return 0; |
| 9932 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9933 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9934 | /* parse the "sni" server keyword */ |
| 9935 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9936 | { |
| 9937 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9938 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 9939 | return ERR_ALERT | ERR_FATAL; |
| 9940 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9941 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9942 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9943 | arg = args[*cur_arg + 1]; |
| 9944 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9945 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 9946 | return ERR_ALERT | ERR_FATAL; |
| 9947 | } |
| 9948 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9949 | free(newsrv->sni_expr); |
| 9950 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9951 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9952 | return 0; |
| 9953 | #endif |
| 9954 | } |
| 9955 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9956 | /* parse the "ssl" server keyword */ |
| 9957 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9958 | { |
| 9959 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9960 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9961 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9962 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9963 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9964 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9965 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9966 | return 0; |
| 9967 | } |
| 9968 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9969 | /* parse the "ssl-reuse" server keyword */ |
| 9970 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9971 | { |
| 9972 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 9973 | return 0; |
| 9974 | } |
| 9975 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9976 | /* parse the "tls-tickets" server keyword */ |
| 9977 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9978 | { |
| 9979 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 9980 | return 0; |
| 9981 | } |
| 9982 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9983 | /* parse the "verify" server keyword */ |
| 9984 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9985 | { |
| 9986 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9987 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9988 | return ERR_ALERT | ERR_FATAL; |
| 9989 | } |
| 9990 | |
| 9991 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9992 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9993 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9994 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9995 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9996 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 9997 | args[*cur_arg], args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9998 | return ERR_ALERT | ERR_FATAL; |
| 9999 | } |
| 10000 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10001 | return 0; |
| 10002 | } |
| 10003 | |
| 10004 | /* parse the "verifyhost" server keyword */ |
| 10005 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10006 | { |
| 10007 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10008 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10009 | return ERR_ALERT | ERR_FATAL; |
| 10010 | } |
| 10011 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 10012 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10013 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 10014 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10015 | return 0; |
| 10016 | } |
| 10017 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10018 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 10019 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 10020 | struct proxy *defpx, const char *file, int line, |
| 10021 | char **err) { |
| 10022 | int i = 1; |
| 10023 | |
| 10024 | if (*(args[i]) == 0) { |
| 10025 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10026 | return -1; |
| 10027 | } |
| 10028 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10029 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10030 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 10031 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 10032 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10033 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10034 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 10035 | i++; |
| 10036 | else { |
| 10037 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10038 | return -1; |
| 10039 | } |
| 10040 | } |
| 10041 | 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] | 10042 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10043 | return -1; |
| 10044 | } |
| 10045 | i++; |
| 10046 | } |
| 10047 | return 0; |
| 10048 | } |
| 10049 | |
| 10050 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 10051 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 10052 | struct proxy *defpx, const char *file, int line, |
| 10053 | char **err) { |
| 10054 | int i = 1; |
| 10055 | |
| 10056 | if (*(args[i]) == 0) { |
| 10057 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10058 | return -1; |
| 10059 | } |
| 10060 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10061 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10062 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10063 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10064 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 10065 | i++; |
| 10066 | else { |
| 10067 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10068 | return -1; |
| 10069 | } |
| 10070 | } |
| 10071 | 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] | 10072 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10073 | return -1; |
| 10074 | } |
| 10075 | i++; |
| 10076 | } |
| 10077 | return 0; |
| 10078 | } |
| 10079 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10080 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 10081 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10082 | */ |
| 10083 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 10084 | struct proxy *defpx, const char *file, int line, |
| 10085 | char **err) |
| 10086 | { |
| 10087 | char **target; |
| 10088 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10089 | 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] | 10090 | |
| 10091 | if (too_many_args(1, args, err, NULL)) |
| 10092 | return -1; |
| 10093 | |
| 10094 | if (*target) { |
| 10095 | memprintf(err, "'%s' already specified.", args[0]); |
| 10096 | return -1; |
| 10097 | } |
| 10098 | |
| 10099 | if (*(args[1]) == 0) { |
| 10100 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 10101 | return -1; |
| 10102 | } |
| 10103 | *target = strdup(args[1]); |
| 10104 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10105 | } |
| 10106 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10107 | /* "issuers-chain-path" load chain certificate in global */ |
| 10108 | static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
| 10109 | { |
| 10110 | X509 *ca; |
| 10111 | X509_NAME *name = NULL; |
| 10112 | ASN1_OCTET_STRING *skid = NULL; |
| 10113 | STACK_OF(X509) *chain = NULL; |
| 10114 | struct issuer_chain *issuer; |
| 10115 | struct eb64_node *node; |
| 10116 | char *path; |
| 10117 | u64 key; |
| 10118 | int ret = 0; |
| 10119 | |
| 10120 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 10121 | if (chain == NULL) { |
| 10122 | chain = sk_X509_new_null(); |
| 10123 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 10124 | name = X509_get_subject_name(ca); |
| 10125 | } |
| 10126 | if (!sk_X509_push(chain, ca)) { |
| 10127 | X509_free(ca); |
| 10128 | goto end; |
| 10129 | } |
| 10130 | } |
| 10131 | if (!chain) { |
| 10132 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 10133 | goto end; |
| 10134 | } |
| 10135 | if (!skid) { |
| 10136 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 10137 | goto end; |
| 10138 | } |
| 10139 | if (!name) { |
| 10140 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 10141 | goto end; |
| 10142 | } |
| 10143 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10144 | for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) { |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10145 | issuer = container_of(node, typeof(*issuer), node); |
| 10146 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 10147 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 10148 | goto end; |
| 10149 | } |
| 10150 | } |
| 10151 | issuer = calloc(1, sizeof *issuer); |
| 10152 | path = strdup(fp); |
| 10153 | if (!issuer || !path) { |
| 10154 | free(issuer); |
| 10155 | free(path); |
| 10156 | goto end; |
| 10157 | } |
| 10158 | issuer->node.key = key; |
| 10159 | issuer->path = path; |
| 10160 | issuer->chain = chain; |
| 10161 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10162 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10163 | ret = 1; |
| 10164 | end: |
| 10165 | if (skid) |
| 10166 | ASN1_OCTET_STRING_free(skid); |
| 10167 | if (chain) |
| 10168 | sk_X509_pop_free(chain, X509_free); |
| 10169 | return ret; |
| 10170 | } |
| 10171 | |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 10172 | static struct issuer_chain* ssl_get_issuer_chain(X509 *cert) |
| 10173 | { |
| 10174 | AUTHORITY_KEYID *akid; |
| 10175 | struct issuer_chain *issuer = NULL; |
| 10176 | |
| 10177 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 10178 | if (akid) { |
| 10179 | struct eb64_node *node; |
| 10180 | u64 hk; |
| 10181 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 10182 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 10183 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 10184 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 10185 | issuer = ti; |
| 10186 | break; |
| 10187 | } |
| 10188 | } |
| 10189 | AUTHORITY_KEYID_free(akid); |
| 10190 | } |
| 10191 | return issuer; |
| 10192 | } |
| 10193 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10194 | static void ssl_free_global_issuers(void) |
| 10195 | { |
| 10196 | struct eb64_node *node, *back; |
| 10197 | struct issuer_chain *issuer; |
| 10198 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10199 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10200 | while (node) { |
| 10201 | issuer = container_of(node, typeof(*issuer), node); |
| 10202 | back = eb64_next(node); |
| 10203 | eb64_delete(node); |
| 10204 | free(issuer->path); |
| 10205 | sk_X509_pop_free(issuer->chain, X509_free); |
| 10206 | free(issuer); |
| 10207 | node = back; |
| 10208 | } |
| 10209 | } |
| 10210 | |
| 10211 | static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx, |
| 10212 | struct proxy *defpx, const char *file, int line, |
| 10213 | char **err) |
| 10214 | { |
| 10215 | char *path; |
| 10216 | struct dirent **de_list; |
| 10217 | int i, n; |
| 10218 | struct stat buf; |
| 10219 | char *end; |
| 10220 | char fp[MAXPATHLEN+1]; |
| 10221 | |
| 10222 | if (too_many_args(1, args, err, NULL)) |
| 10223 | return -1; |
| 10224 | |
| 10225 | path = args[1]; |
| 10226 | if (*path == 0 || stat(path, &buf)) { |
| 10227 | memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n", |
| 10228 | err && *err ? *err : "", args[0]); |
| 10229 | return -1; |
| 10230 | } |
| 10231 | if (S_ISDIR(buf.st_mode) == 0) { |
| 10232 | memprintf(err, "%sglobal statement '%s': %s is not a directory.\n", |
| 10233 | err && *err ? *err : "", args[0], path); |
| 10234 | return -1; |
| 10235 | } |
| 10236 | |
| 10237 | /* strip trailing slashes, including first one */ |
| 10238 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 10239 | *end = 0; |
| 10240 | /* path already parsed? */ |
| 10241 | if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0) |
| 10242 | return 0; |
| 10243 | /* overwrite old issuers_chain_path */ |
| 10244 | free(global_ssl.issuers_chain_path); |
| 10245 | global_ssl.issuers_chain_path = strdup(path); |
| 10246 | ssl_free_global_issuers(); |
| 10247 | |
| 10248 | n = scandir(path, &de_list, 0, alphasort); |
| 10249 | if (n < 0) { |
| 10250 | memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n", |
| 10251 | err && *err ? *err : "", args[0], path, strerror(errno)); |
| 10252 | return -1; |
| 10253 | } |
| 10254 | for (i = 0; i < n; i++) { |
| 10255 | struct dirent *de = de_list[i]; |
| 10256 | BIO *in = NULL; |
| 10257 | char *warn = NULL; |
| 10258 | |
| 10259 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 10260 | free(de); |
| 10261 | if (stat(fp, &buf) != 0) { |
| 10262 | ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno)); |
| 10263 | goto next; |
| 10264 | } |
| 10265 | if (!S_ISREG(buf.st_mode)) |
| 10266 | goto next; |
| 10267 | |
| 10268 | in = BIO_new(BIO_s_file()); |
| 10269 | if (in == NULL) |
| 10270 | goto next; |
| 10271 | if (BIO_read_filename(in, fp) <= 0) |
| 10272 | goto next; |
| 10273 | ssl_load_global_issuer_from_BIO(in, fp, &warn); |
| 10274 | if (warn) { |
Tim Duesterhus | e8aa5f2 | 2020-02-19 11:41:13 +0100 | [diff] [blame] | 10275 | ha_warning("%s", warn); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10276 | free(warn); |
| 10277 | warn = NULL; |
| 10278 | } |
| 10279 | next: |
| 10280 | if (in) |
| 10281 | BIO_free(in); |
| 10282 | } |
| 10283 | free(de_list); |
| 10284 | |
| 10285 | return 0; |
| 10286 | } |
| 10287 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10288 | /* parse the "ssl-mode-async" keyword in global section. |
| 10289 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10290 | */ |
| 10291 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 10292 | struct proxy *defpx, const char *file, int line, |
| 10293 | char **err) |
| 10294 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10295 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10296 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 10297 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10298 | return 0; |
| 10299 | #else |
| 10300 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 10301 | return -1; |
| 10302 | #endif |
| 10303 | } |
| 10304 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10305 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10306 | static int ssl_check_async_engine_count(void) { |
| 10307 | int err_code = 0; |
| 10308 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 10309 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 10310 | 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] | 10311 | err_code = ERR_ABORT; |
| 10312 | } |
| 10313 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10314 | } |
| 10315 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10316 | /* parse the "ssl-engine" keyword in global section. |
| 10317 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10318 | */ |
| 10319 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 10320 | struct proxy *defpx, const char *file, int line, |
| 10321 | char **err) |
| 10322 | { |
| 10323 | char *algo; |
| 10324 | int ret = -1; |
| 10325 | |
| 10326 | if (*(args[1]) == 0) { |
| 10327 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 10328 | return ret; |
| 10329 | } |
| 10330 | |
| 10331 | if (*(args[2]) == 0) { |
| 10332 | /* if no list of algorithms is given, it defaults to ALL */ |
| 10333 | algo = strdup("ALL"); |
| 10334 | goto add_engine; |
| 10335 | } |
| 10336 | |
| 10337 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 10338 | if (strcmp(args[2], "algo") != 0) { |
| 10339 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 10340 | return ret; |
| 10341 | } |
| 10342 | |
| 10343 | if (*(args[3]) == 0) { |
| 10344 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 10345 | return ret; |
| 10346 | } |
| 10347 | algo = strdup(args[3]); |
| 10348 | |
| 10349 | add_engine: |
| 10350 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 10351 | openssl_engines_initialized++; |
| 10352 | ret = 0; |
| 10353 | } |
| 10354 | free(algo); |
| 10355 | return ret; |
| 10356 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10357 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10358 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10359 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 10360 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10361 | */ |
| 10362 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 10363 | struct proxy *defpx, const char *file, int line, |
| 10364 | char **err) |
| 10365 | { |
| 10366 | char **target; |
| 10367 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10368 | 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] | 10369 | |
| 10370 | if (too_many_args(1, args, err, NULL)) |
| 10371 | return -1; |
| 10372 | |
| 10373 | if (*(args[1]) == 0) { |
| 10374 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10375 | return -1; |
| 10376 | } |
| 10377 | |
| 10378 | free(*target); |
| 10379 | *target = strdup(args[1]); |
| 10380 | return 0; |
| 10381 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10382 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10383 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10384 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 10385 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10386 | */ |
| 10387 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 10388 | struct proxy *defpx, const char *file, int line, |
| 10389 | char **err) |
| 10390 | { |
| 10391 | char **target; |
| 10392 | |
| 10393 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 10394 | |
| 10395 | if (too_many_args(1, args, err, NULL)) |
| 10396 | return -1; |
| 10397 | |
| 10398 | if (*(args[1]) == 0) { |
| 10399 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10400 | return -1; |
| 10401 | } |
| 10402 | |
| 10403 | free(*target); |
| 10404 | *target = strdup(args[1]); |
| 10405 | return 0; |
| 10406 | } |
| 10407 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10408 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10409 | /* parse various global tune.ssl settings consisting in positive integers. |
| 10410 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10411 | */ |
| 10412 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 10413 | struct proxy *defpx, const char *file, int line, |
| 10414 | char **err) |
| 10415 | { |
| 10416 | int *target; |
| 10417 | |
| 10418 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 10419 | target = &global.tune.sslcachesize; |
| 10420 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10421 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10422 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10423 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10424 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 10425 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10426 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 10427 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10428 | else { |
| 10429 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 10430 | return -1; |
| 10431 | } |
| 10432 | |
| 10433 | if (too_many_args(1, args, err, NULL)) |
| 10434 | return -1; |
| 10435 | |
| 10436 | if (*(args[1]) == 0) { |
| 10437 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10438 | return -1; |
| 10439 | } |
| 10440 | |
| 10441 | *target = atoi(args[1]); |
| 10442 | if (*target < 0) { |
| 10443 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 10444 | return -1; |
| 10445 | } |
| 10446 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10447 | } |
| 10448 | |
| 10449 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 10450 | struct proxy *defpx, const char *file, int line, |
| 10451 | char **err) |
| 10452 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10453 | int ret; |
| 10454 | |
| 10455 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 10456 | if (ret != 0) |
| 10457 | return ret; |
| 10458 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10459 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10460 | memprintf(err, "'%s' is already configured.", args[0]); |
| 10461 | return -1; |
| 10462 | } |
| 10463 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10464 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 10465 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10466 | memprintf(err, "Out of memory error."); |
| 10467 | return -1; |
| 10468 | } |
| 10469 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10470 | } |
| 10471 | |
| 10472 | /* parse "ssl.force-private-cache". |
| 10473 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10474 | */ |
| 10475 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 10476 | struct proxy *defpx, const char *file, int line, |
| 10477 | char **err) |
| 10478 | { |
| 10479 | if (too_many_args(0, args, err, NULL)) |
| 10480 | return -1; |
| 10481 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10482 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10483 | return 0; |
| 10484 | } |
| 10485 | |
| 10486 | /* parse "ssl.lifetime". |
| 10487 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10488 | */ |
| 10489 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 10490 | struct proxy *defpx, const char *file, int line, |
| 10491 | char **err) |
| 10492 | { |
| 10493 | const char *res; |
| 10494 | |
| 10495 | if (too_many_args(1, args, err, NULL)) |
| 10496 | return -1; |
| 10497 | |
| 10498 | if (*(args[1]) == 0) { |
| 10499 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 10500 | return -1; |
| 10501 | } |
| 10502 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10503 | 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] | 10504 | if (res == PARSE_TIME_OVER) { |
| 10505 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 10506 | args[1], args[0]); |
| 10507 | return -1; |
| 10508 | } |
| 10509 | else if (res == PARSE_TIME_UNDER) { |
| 10510 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 10511 | args[1], args[0]); |
| 10512 | return -1; |
| 10513 | } |
| 10514 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10515 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 10516 | return -1; |
| 10517 | } |
| 10518 | return 0; |
| 10519 | } |
| 10520 | |
| 10521 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10522 | /* parse "ssl-dh-param-file". |
| 10523 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10524 | */ |
| 10525 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 10526 | struct proxy *defpx, const char *file, int line, |
| 10527 | char **err) |
| 10528 | { |
| 10529 | if (too_many_args(1, args, err, NULL)) |
| 10530 | return -1; |
| 10531 | |
| 10532 | if (*(args[1]) == 0) { |
| 10533 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 10534 | return -1; |
| 10535 | } |
| 10536 | |
| 10537 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 10538 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 10539 | return -1; |
| 10540 | } |
| 10541 | return 0; |
| 10542 | } |
| 10543 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10544 | /* parse "ssl.default-dh-param". |
| 10545 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10546 | */ |
| 10547 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 10548 | struct proxy *defpx, const char *file, int line, |
| 10549 | char **err) |
| 10550 | { |
| 10551 | if (too_many_args(1, args, err, NULL)) |
| 10552 | return -1; |
| 10553 | |
| 10554 | if (*(args[1]) == 0) { |
| 10555 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10556 | return -1; |
| 10557 | } |
| 10558 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10559 | global_ssl.default_dh_param = atoi(args[1]); |
| 10560 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10561 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 10562 | return -1; |
| 10563 | } |
| 10564 | return 0; |
| 10565 | } |
| 10566 | #endif |
| 10567 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10568 | |
| 10569 | /* |
| 10570 | * parse "ssl-load-extra-files". |
| 10571 | * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none" |
| 10572 | */ |
| 10573 | static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx, |
| 10574 | struct proxy *defpx, const char *file, int line, |
| 10575 | char **err) |
| 10576 | { |
| 10577 | int i; |
| 10578 | int gf = SSL_GF_NONE; |
| 10579 | |
| 10580 | if (*(args[1]) == 0) |
| 10581 | goto err_arg; |
| 10582 | |
| 10583 | for (i = 1; *args[i]; i++) { |
| 10584 | |
| 10585 | if (!strcmp("bundle", args[i])) { |
| 10586 | gf |= SSL_GF_BUNDLE; |
| 10587 | |
| 10588 | } else if (!strcmp("sctl", args[i])) { |
| 10589 | gf |= SSL_GF_SCTL; |
| 10590 | |
| 10591 | } else if (!strcmp("ocsp", args[i])){ |
| 10592 | gf |= SSL_GF_OCSP; |
| 10593 | |
| 10594 | } else if (!strcmp("issuer", args[i])){ |
| 10595 | gf |= SSL_GF_OCSP_ISSUER; |
| 10596 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10597 | } else if (!strcmp("key", args[i])) { |
| 10598 | gf |= SSL_GF_KEY; |
| 10599 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10600 | } else if (!strcmp("none", args[i])) { |
| 10601 | if (gf != SSL_GF_NONE) |
| 10602 | goto err_alone; |
| 10603 | gf = SSL_GF_NONE; |
| 10604 | i++; |
| 10605 | break; |
| 10606 | |
| 10607 | } else if (!strcmp("all", args[i])) { |
| 10608 | if (gf != SSL_GF_NONE) |
| 10609 | goto err_alone; |
| 10610 | gf = SSL_GF_ALL; |
| 10611 | i++; |
| 10612 | break; |
| 10613 | } else { |
| 10614 | goto err_arg; |
| 10615 | } |
| 10616 | } |
| 10617 | /* break from loop but there are still arguments */ |
| 10618 | if (*args[i]) |
| 10619 | goto err_alone; |
| 10620 | |
| 10621 | global_ssl.extra_files = gf; |
| 10622 | |
| 10623 | return 0; |
| 10624 | |
| 10625 | err_alone: |
| 10626 | memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]); |
| 10627 | return -1; |
| 10628 | |
| 10629 | err_arg: |
| 10630 | memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]); |
| 10631 | return -1; |
| 10632 | } |
| 10633 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10634 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10635 | /* This function is used with TLS ticket keys management. It permits to browse |
| 10636 | * each reference. The variable <getnext> must contain the current node, |
| 10637 | * <end> point to the root node. |
| 10638 | */ |
| 10639 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10640 | static inline |
| 10641 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 10642 | { |
| 10643 | struct tls_keys_ref *ref = getnext; |
| 10644 | |
| 10645 | while (1) { |
| 10646 | |
| 10647 | /* Get next list entry. */ |
| 10648 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 10649 | |
| 10650 | /* If the entry is the last of the list, return NULL. */ |
| 10651 | if (&ref->list == end) |
| 10652 | return NULL; |
| 10653 | |
| 10654 | return ref; |
| 10655 | } |
| 10656 | } |
| 10657 | |
| 10658 | static inline |
| 10659 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 10660 | { |
| 10661 | int id; |
| 10662 | char *error; |
| 10663 | |
| 10664 | /* If the reference starts by a '#', this is numeric id. */ |
| 10665 | if (reference[0] == '#') { |
| 10666 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 10667 | id = strtol(reference + 1, &error, 10); |
| 10668 | if (*error != '\0') |
| 10669 | return NULL; |
| 10670 | |
| 10671 | /* Perform the unique id lookup. */ |
| 10672 | return tlskeys_ref_lookupid(id); |
| 10673 | } |
| 10674 | |
| 10675 | /* Perform the string lookup. */ |
| 10676 | return tlskeys_ref_lookup(reference); |
| 10677 | } |
| 10678 | #endif |
| 10679 | |
| 10680 | |
| 10681 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10682 | |
| 10683 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 10684 | |
| 10685 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 10686 | return cli_io_handler_tlskeys_files(appctx); |
| 10687 | } |
| 10688 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10689 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 10690 | * (next index to be dumped), and cli.p0 (next key reference). |
| 10691 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10692 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 10693 | |
| 10694 | struct stream_interface *si = appctx->owner; |
| 10695 | |
| 10696 | switch (appctx->st2) { |
| 10697 | case STAT_ST_INIT: |
| 10698 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 10699 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10700 | * later and restart at the state "STAT_ST_INIT". |
| 10701 | */ |
| 10702 | chunk_reset(&trash); |
| 10703 | |
| 10704 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 10705 | chunk_appendf(&trash, "# id secret\n"); |
| 10706 | else |
| 10707 | chunk_appendf(&trash, "# id (file)\n"); |
| 10708 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10709 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10710 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10711 | return 0; |
| 10712 | } |
| 10713 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10714 | /* Now, we start the browsing of the references lists. |
| 10715 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 10716 | * available field of this pointer is <list>. It is used with the function |
| 10717 | * tlskeys_list_get_next() for retruning the first available entry |
| 10718 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10719 | if (appctx->ctx.cli.p0 == NULL) { |
| 10720 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 10721 | 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] | 10722 | } |
| 10723 | |
| 10724 | appctx->st2 = STAT_ST_LIST; |
| 10725 | /* fall through */ |
| 10726 | |
| 10727 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10728 | while (appctx->ctx.cli.p0) { |
| 10729 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10730 | |
| 10731 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10732 | 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] | 10733 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10734 | |
| 10735 | if (appctx->ctx.cli.i1 == 0) |
| 10736 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 10737 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10738 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10739 | int head; |
| 10740 | |
| 10741 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 10742 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10743 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10744 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10745 | |
| 10746 | chunk_reset(t2); |
| 10747 | /* 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] | 10748 | if (ref->key_size_bits == 128) { |
| 10749 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10750 | sizeof(struct tls_sess_key_128), |
| 10751 | t2->area, t2->size); |
| 10752 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10753 | t2->area); |
| 10754 | } |
| 10755 | else if (ref->key_size_bits == 256) { |
| 10756 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10757 | sizeof(struct tls_sess_key_256), |
| 10758 | t2->area, t2->size); |
| 10759 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10760 | t2->area); |
| 10761 | } |
| 10762 | else { |
| 10763 | /* This case should never happen */ |
| 10764 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 10765 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10766 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10767 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10768 | /* let's try again later from this stream. We add ourselves into |
| 10769 | * this stream's users so that it can remove us upon termination. |
| 10770 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10771 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10772 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10773 | return 0; |
| 10774 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10775 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10776 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10777 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10778 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10779 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10780 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10781 | /* let's try again later from this stream. We add ourselves into |
| 10782 | * this stream's users so that it can remove us upon termination. |
| 10783 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10784 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10785 | return 0; |
| 10786 | } |
| 10787 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10788 | 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] | 10789 | break; |
| 10790 | |
| 10791 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10792 | 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] | 10793 | } |
| 10794 | |
| 10795 | appctx->st2 = STAT_ST_FIN; |
| 10796 | /* fall through */ |
| 10797 | |
| 10798 | default: |
| 10799 | appctx->st2 = STAT_ST_FIN; |
| 10800 | return 1; |
| 10801 | } |
| 10802 | return 0; |
| 10803 | } |
| 10804 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10805 | /* 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] | 10806 | 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] | 10807 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10808 | /* no parameter, shows only file list */ |
| 10809 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10810 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10811 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10812 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10813 | } |
| 10814 | |
| 10815 | if (args[2][0] == '*') { |
| 10816 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10817 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10818 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10819 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10820 | if (!appctx->ctx.cli.p0) |
| 10821 | return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10822 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10823 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10824 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10825 | } |
| 10826 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10827 | 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] | 10828 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10829 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10830 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10831 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10832 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10833 | if (!*args[3] || !*args[4]) |
| 10834 | return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10835 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10836 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10837 | if (!ref) |
| 10838 | return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10839 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10840 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10841 | if (ret < 0) |
| 10842 | return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n"); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 10843 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10844 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10845 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 10846 | return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10847 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10848 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10849 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10850 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10851 | |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 10852 | /* |
| 10853 | * Take an ssl_bind_conf structure and append the configuration line used to |
| 10854 | * create it in the buffer |
| 10855 | */ |
| 10856 | static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf) |
| 10857 | { |
| 10858 | int space = 0; |
| 10859 | |
| 10860 | if (conf == NULL) |
| 10861 | return; |
| 10862 | |
| 10863 | chunk_appendf(buf, " ["); |
| 10864 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 10865 | if (conf->npn_str) { |
| 10866 | int len = conf->npn_len; |
| 10867 | char *ptr = conf->npn_str; |
| 10868 | int comma = 0; |
| 10869 | |
| 10870 | if (space) chunk_appendf(buf, " "); |
| 10871 | chunk_appendf(buf, "npn "); |
| 10872 | while (len) { |
| 10873 | unsigned short size; |
| 10874 | |
| 10875 | size = *ptr; |
| 10876 | ptr++; |
| 10877 | if (comma) |
| 10878 | chunk_memcat(buf, ",", 1); |
| 10879 | chunk_memcat(buf, ptr, size); |
| 10880 | ptr += size; |
| 10881 | len -= size + 1; |
| 10882 | comma = 1; |
| 10883 | } |
| 10884 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10885 | space++; |
| 10886 | } |
| 10887 | #endif |
| 10888 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 10889 | if (conf->alpn_str) { |
| 10890 | int len = conf->alpn_len; |
| 10891 | char *ptr = conf->alpn_str; |
| 10892 | int comma = 0; |
| 10893 | |
| 10894 | if (space) chunk_appendf(buf, " "); |
| 10895 | chunk_appendf(buf, "alpn "); |
| 10896 | while (len) { |
| 10897 | unsigned short size; |
| 10898 | |
| 10899 | size = *ptr; |
| 10900 | ptr++; |
| 10901 | if (comma) |
| 10902 | chunk_memcat(buf, ",", 1); |
| 10903 | chunk_memcat(buf, ptr, size); |
| 10904 | ptr += size; |
| 10905 | len -= size + 1; |
| 10906 | comma = 1; |
| 10907 | } |
| 10908 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10909 | space++; |
| 10910 | } |
| 10911 | #endif |
| 10912 | /* verify */ |
| 10913 | { |
| 10914 | if (conf->verify == SSL_SOCK_VERIFY_NONE) { |
| 10915 | if (space) chunk_appendf(buf, " "); |
| 10916 | chunk_appendf(buf, "verify none"); |
| 10917 | space++; |
| 10918 | } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) { |
| 10919 | if (space) chunk_appendf(buf, " "); |
| 10920 | chunk_appendf(buf, "verify optional"); |
| 10921 | space++; |
| 10922 | } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 10923 | if (space) chunk_appendf(buf, " "); |
| 10924 | chunk_appendf(buf, "verify required"); |
| 10925 | space++; |
| 10926 | } |
| 10927 | } |
| 10928 | |
| 10929 | if (conf->no_ca_names) { |
| 10930 | if (space) chunk_appendf(buf, " "); |
| 10931 | chunk_appendf(buf, "no-ca-names"); |
| 10932 | space++; |
| 10933 | } |
| 10934 | |
| 10935 | if (conf->early_data) { |
| 10936 | if (space) chunk_appendf(buf, " "); |
| 10937 | chunk_appendf(buf, "allow-0rtt"); |
| 10938 | space++; |
| 10939 | } |
| 10940 | if (conf->ca_file) { |
| 10941 | if (space) chunk_appendf(buf, " "); |
| 10942 | chunk_appendf(buf, "ca-file %s", conf->ca_file); |
| 10943 | space++; |
| 10944 | } |
| 10945 | if (conf->crl_file) { |
| 10946 | if (space) chunk_appendf(buf, " "); |
| 10947 | chunk_appendf(buf, "crl-file %s", conf->crl_file); |
| 10948 | space++; |
| 10949 | } |
| 10950 | if (conf->ciphers) { |
| 10951 | if (space) chunk_appendf(buf, " "); |
| 10952 | chunk_appendf(buf, "ciphers %s", conf->ciphers); |
| 10953 | space++; |
| 10954 | } |
| 10955 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 10956 | if (conf->ciphersuites) { |
| 10957 | if (space) chunk_appendf(buf, " "); |
| 10958 | chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites); |
| 10959 | space++; |
| 10960 | } |
| 10961 | #endif |
| 10962 | if (conf->curves) { |
| 10963 | if (space) chunk_appendf(buf, " "); |
| 10964 | chunk_appendf(buf, "curves %s", conf->curves); |
| 10965 | space++; |
| 10966 | } |
| 10967 | if (conf->ecdhe) { |
| 10968 | if (space) chunk_appendf(buf, " "); |
| 10969 | chunk_appendf(buf, "ecdhe %s", conf->ecdhe); |
| 10970 | space++; |
| 10971 | } |
| 10972 | |
| 10973 | /* the crt-lists only support ssl-min-ver and ssl-max-ver */ |
| 10974 | /* XXX: this part need to be revamp so we don't dump the default settings */ |
| 10975 | if (conf->ssl_methods.min) { |
| 10976 | if (space) chunk_appendf(buf, " "); |
| 10977 | chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name); |
| 10978 | space++; |
| 10979 | } |
| 10980 | |
| 10981 | if (conf->ssl_methods.max) { |
| 10982 | if (space) chunk_appendf(buf, " "); |
| 10983 | chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name); |
| 10984 | space++; |
| 10985 | } |
| 10986 | |
| 10987 | chunk_appendf(buf, "] "); |
| 10988 | |
| 10989 | return; |
| 10990 | } |
| 10991 | |
| 10992 | /* dump a list of filters */ |
| 10993 | static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry) |
| 10994 | { |
| 10995 | int space = 0; |
| 10996 | int i; |
| 10997 | |
| 10998 | if (!entry->fcount) |
| 10999 | return; |
| 11000 | |
| 11001 | for (i = 0; i < entry->fcount; i++) { |
| 11002 | if (space) |
| 11003 | chunk_appendf(buf, " "); |
| 11004 | chunk_appendf(buf, "%s", entry->filters[i]); |
| 11005 | space = 1; |
| 11006 | } |
| 11007 | return; |
| 11008 | } |
| 11009 | |
| 11010 | /* CLI IO handler for '(show|dump) ssl crt-list' */ |
| 11011 | static int cli_io_handler_dump_crtlist(struct appctx *appctx) |
| 11012 | { |
| 11013 | struct buffer *trash = alloc_trash_chunk(); |
| 11014 | struct stream_interface *si = appctx->owner; |
| 11015 | struct ebmb_node *lnode; |
| 11016 | |
| 11017 | if (trash == NULL) |
| 11018 | return 1; |
| 11019 | |
| 11020 | /* dump the list of crt-lists */ |
| 11021 | lnode = appctx->ctx.cli.p1; |
| 11022 | if (lnode == NULL) |
| 11023 | lnode = ebmb_first(&crtlists_tree); |
| 11024 | while (lnode) { |
| 11025 | chunk_appendf(trash, "%s\n", lnode->key); |
| 11026 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11027 | si_rx_room_blk(si); |
| 11028 | goto yield; |
| 11029 | } |
| 11030 | lnode = ebmb_next(lnode); |
| 11031 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11032 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11033 | return 1; |
| 11034 | yield: |
| 11035 | appctx->ctx.cli.p1 = lnode; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11036 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11037 | return 0; |
| 11038 | } |
| 11039 | |
| 11040 | /* CLI IO handler for '(show|dump) ssl crt-list <filename>' */ |
| 11041 | static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx) |
| 11042 | { |
| 11043 | struct buffer *trash = alloc_trash_chunk(); |
| 11044 | struct crtlist *crtlist; |
| 11045 | struct stream_interface *si = appctx->owner; |
| 11046 | struct crtlist_entry *entry; |
| 11047 | |
| 11048 | if (trash == NULL) |
| 11049 | return 1; |
| 11050 | |
| 11051 | crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node); |
| 11052 | |
| 11053 | entry = appctx->ctx.cli.p1; |
| 11054 | if (entry == NULL) { |
| 11055 | entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist); |
| 11056 | chunk_appendf(trash, "# %s\n", crtlist->node.key); |
| 11057 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11058 | si_rx_room_blk(si); |
| 11059 | goto yield; |
| 11060 | } |
| 11061 | } |
| 11062 | |
| 11063 | list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) { |
| 11064 | struct ckch_store *store; |
| 11065 | const char *filename; |
| 11066 | |
| 11067 | store = entry->node.key; |
| 11068 | filename = store->path; |
| 11069 | if (appctx->ctx.cli.i0 == 's') /* show */ |
| 11070 | chunk_appendf(trash, "%p ", entry); |
| 11071 | chunk_appendf(trash, "%s", filename); |
| 11072 | dump_crtlist_sslconf(trash, entry->ssl_conf); |
| 11073 | dump_crtlist_filters(trash, entry); |
| 11074 | chunk_appendf(trash, "\n"); |
| 11075 | |
| 11076 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11077 | si_rx_room_blk(si); |
| 11078 | goto yield; |
| 11079 | } |
| 11080 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11081 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11082 | return 1; |
| 11083 | yield: |
| 11084 | appctx->ctx.cli.p1 = entry; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11085 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11086 | return 0; |
| 11087 | } |
| 11088 | |
| 11089 | /* CLI argument parser for '(show|dump) ssl crt-list' */ |
| 11090 | static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11091 | { |
| 11092 | struct ebmb_node *lnode; |
| 11093 | int mode; |
| 11094 | |
| 11095 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11096 | return 1; |
| 11097 | |
| 11098 | appctx->ctx.cli.p0 = NULL; |
| 11099 | appctx->ctx.cli.p1 = NULL; |
| 11100 | mode = (int)args[0][0]; /* 'd' or 's' */ |
| 11101 | |
| 11102 | if (mode == 'd' && !*args[3]) |
| 11103 | return cli_err(appctx, "'dump ssl crt-list' expects a filename or a directory\n"); |
| 11104 | |
| 11105 | if (*args[3]) { |
| 11106 | lnode = ebst_lookup(&crtlists_tree, args[3]); |
| 11107 | if (lnode == NULL) |
| 11108 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 11109 | |
| 11110 | appctx->ctx.cli.p0 = lnode; |
| 11111 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 11112 | } |
| 11113 | appctx->ctx.cli.i0 = mode; |
| 11114 | |
| 11115 | return 0; |
| 11116 | } |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11117 | |
| 11118 | /* Type of SSL payloads that can be updated over the CLI */ |
| 11119 | |
| 11120 | enum { |
| 11121 | CERT_TYPE_PEM = 0, |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11122 | CERT_TYPE_KEY, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11123 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11124 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11125 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11126 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11127 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11128 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11129 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11130 | CERT_TYPE_MAX, |
| 11131 | }; |
| 11132 | |
| 11133 | struct { |
| 11134 | const char *ext; |
| 11135 | int type; |
| 11136 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 11137 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11138 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11139 | [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */ |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11140 | [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch }, |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 11141 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11142 | [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file }, |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 11143 | #endif |
| 11144 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11145 | [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file }, |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 11146 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11147 | [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch }, |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11148 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11149 | }; |
| 11150 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11151 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 11152 | enum { |
| 11153 | SETCERT_ST_INIT = 0, |
| 11154 | SETCERT_ST_GEN, |
| 11155 | SETCERT_ST_INSERT, |
| 11156 | SETCERT_ST_FIN, |
| 11157 | }; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11158 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11159 | /* release function of the `show ssl cert' command */ |
| 11160 | static void cli_release_show_cert(struct appctx *appctx) |
| 11161 | { |
| 11162 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11163 | } |
| 11164 | |
| 11165 | /* IO handler of "show ssl cert <filename>" */ |
| 11166 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 11167 | { |
| 11168 | struct buffer *trash = alloc_trash_chunk(); |
| 11169 | struct ebmb_node *node; |
| 11170 | struct stream_interface *si = appctx->owner; |
| 11171 | struct ckch_store *ckchs; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11172 | |
| 11173 | if (trash == NULL) |
| 11174 | return 1; |
| 11175 | |
| 11176 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11177 | if (ckchs_transaction.old_ckchs) { |
| 11178 | ckchs = ckchs_transaction.old_ckchs; |
| 11179 | chunk_appendf(trash, "# transaction\n"); |
| 11180 | if (!ckchs->multi) { |
| 11181 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11182 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11183 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11184 | int n; |
| 11185 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11186 | chunk_appendf(trash, "*%s:", ckchs->path); |
| 11187 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11188 | if (ckchs->ckch[n].cert) |
| 11189 | chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11190 | } |
| 11191 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11192 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11193 | } |
| 11194 | } |
| 11195 | } |
| 11196 | |
| 11197 | if (!appctx->ctx.cli.p0) { |
| 11198 | chunk_appendf(trash, "# filename\n"); |
| 11199 | node = ebmb_first(&ckchs_tree); |
| 11200 | } else { |
| 11201 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 11202 | } |
| 11203 | while (node) { |
| 11204 | ckchs = ebmb_entry(node, struct ckch_store, node); |
| 11205 | if (!ckchs->multi) { |
| 11206 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11207 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11208 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11209 | int n; |
| 11210 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11211 | chunk_appendf(trash, "%s:", ckchs->path); |
| 11212 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11213 | if (ckchs->ckch[n].cert) |
| 11214 | chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11215 | } |
| 11216 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11217 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11218 | } |
| 11219 | |
| 11220 | node = ebmb_next(node); |
| 11221 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11222 | si_rx_room_blk(si); |
| 11223 | goto yield; |
| 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | appctx->ctx.cli.p0 = NULL; |
| 11228 | free_trash_chunk(trash); |
| 11229 | return 1; |
| 11230 | yield: |
| 11231 | |
| 11232 | free_trash_chunk(trash); |
| 11233 | appctx->ctx.cli.p0 = ckchs; |
| 11234 | return 0; /* should come back */ |
| 11235 | } |
| 11236 | |
| 11237 | /* IO handler of the details "show ssl cert <filename>" */ |
| 11238 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 11239 | { |
| 11240 | struct stream_interface *si = appctx->owner; |
| 11241 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 11242 | struct buffer *out = alloc_trash_chunk(); |
| 11243 | struct buffer *tmp = alloc_trash_chunk(); |
| 11244 | X509_NAME *name = NULL; |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11245 | STACK_OF(X509) *chain; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11246 | unsigned int len = 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11247 | int write = -1; |
| 11248 | BIO *bio = NULL; |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11249 | int i; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11250 | |
| 11251 | if (!tmp || !out) |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11252 | goto end_no_putchk; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11253 | |
| 11254 | if (!ckchs->multi) { |
| 11255 | chunk_appendf(out, "Filename: "); |
| 11256 | if (ckchs == ckchs_transaction.new_ckchs) |
| 11257 | chunk_appendf(out, "*"); |
| 11258 | chunk_appendf(out, "%s\n", ckchs->path); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11259 | |
William Lallemand | 59c16fc | 2020-03-19 20:26:02 +0100 | [diff] [blame] | 11260 | chunk_appendf(out, "Status: "); |
| 11261 | if (ckchs->ckch->cert == NULL) |
| 11262 | chunk_appendf(out, "Empty\n"); |
| 11263 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 11264 | chunk_appendf(out, "Unused\n"); |
| 11265 | else |
| 11266 | chunk_appendf(out, "Used\n"); |
| 11267 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11268 | if (ckchs->ckch->cert == NULL) |
| 11269 | goto end; |
| 11270 | |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11271 | chain = ckchs->ckch->chain; |
| 11272 | if (chain == NULL) { |
| 11273 | struct issuer_chain *issuer; |
| 11274 | issuer = ssl_get_issuer_chain(ckchs->ckch->cert); |
| 11275 | if (issuer) { |
| 11276 | chain = issuer->chain; |
| 11277 | chunk_appendf(out, "Chain Filename: "); |
| 11278 | chunk_appendf(out, "%s\n", issuer->path); |
| 11279 | } |
| 11280 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11281 | chunk_appendf(out, "Serial: "); |
| 11282 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 11283 | goto end; |
| 11284 | dump_binary(out, tmp->area, tmp->data); |
| 11285 | chunk_appendf(out, "\n"); |
| 11286 | |
| 11287 | chunk_appendf(out, "notBefore: "); |
| 11288 | chunk_reset(tmp); |
| 11289 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11290 | goto end; |
| 11291 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 11292 | goto end; |
| 11293 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 11294 | tmp->area[write] = '\0'; |
| 11295 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11296 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11297 | chunk_appendf(out, "%s\n", tmp->area); |
| 11298 | |
| 11299 | chunk_appendf(out, "notAfter: "); |
| 11300 | chunk_reset(tmp); |
| 11301 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11302 | goto end; |
| 11303 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 11304 | goto end; |
| 11305 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 11306 | goto end; |
| 11307 | tmp->area[write] = '\0'; |
| 11308 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11309 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11310 | chunk_appendf(out, "%s\n", tmp->area); |
| 11311 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11312 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11313 | chunk_appendf(out, "Subject Alternative Name: "); |
| 11314 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 11315 | goto end; |
| 11316 | *(out->area + out->data) = '\0'; |
| 11317 | chunk_appendf(out, "\n"); |
| 11318 | #endif |
| 11319 | chunk_reset(tmp); |
| 11320 | chunk_appendf(out, "Algorithm: "); |
| 11321 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 11322 | goto end; |
| 11323 | chunk_appendf(out, "%s\n", tmp->area); |
| 11324 | |
| 11325 | chunk_reset(tmp); |
| 11326 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11327 | if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0) |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11328 | goto end; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11329 | tmp->data = len; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11330 | dump_binary(out, tmp->area, tmp->data); |
| 11331 | chunk_appendf(out, "\n"); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11332 | |
William Lallemand | a90e593 | 2020-02-25 14:07:58 +0100 | [diff] [blame] | 11333 | chunk_appendf(out, "Subject: "); |
| 11334 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 11335 | goto end; |
| 11336 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11337 | goto end; |
| 11338 | *(tmp->area + tmp->data) = '\0'; |
| 11339 | chunk_appendf(out, "%s\n", tmp->area); |
| 11340 | |
| 11341 | chunk_appendf(out, "Issuer: "); |
| 11342 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 11343 | goto end; |
| 11344 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11345 | goto end; |
| 11346 | *(tmp->area + tmp->data) = '\0'; |
| 11347 | chunk_appendf(out, "%s\n", tmp->area); |
| 11348 | |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11349 | /* Displays subject of each certificate in the chain */ |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11350 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 11351 | X509 *ca = sk_X509_value(chain, i); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11352 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11353 | chunk_appendf(out, "Chain Subject: "); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11354 | if ((name = X509_get_subject_name(ca)) == NULL) |
| 11355 | goto end; |
| 11356 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11357 | goto end; |
| 11358 | *(tmp->area + tmp->data) = '\0'; |
| 11359 | chunk_appendf(out, "%s\n", tmp->area); |
| 11360 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11361 | chunk_appendf(out, "Chain Issuer: "); |
| 11362 | if ((name = X509_get_issuer_name(ca)) == NULL) |
| 11363 | goto end; |
| 11364 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11365 | goto end; |
| 11366 | *(tmp->area + tmp->data) = '\0'; |
| 11367 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11368 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11369 | } |
| 11370 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11371 | end: |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11372 | if (ci_putchk(si_ic(si), out) == -1) { |
| 11373 | si_rx_room_blk(si); |
| 11374 | goto yield; |
| 11375 | } |
| 11376 | |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11377 | end_no_putchk: |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11378 | if (bio) |
| 11379 | BIO_free(bio); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11380 | free_trash_chunk(tmp); |
| 11381 | free_trash_chunk(out); |
| 11382 | return 1; |
| 11383 | yield: |
| 11384 | free_trash_chunk(tmp); |
| 11385 | free_trash_chunk(out); |
| 11386 | return 0; /* should come back */ |
| 11387 | } |
| 11388 | |
| 11389 | /* parsing function for 'show ssl cert [certfile]' */ |
| 11390 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11391 | { |
| 11392 | struct ckch_store *ckchs; |
| 11393 | |
| 11394 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 11395 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 11396 | |
| 11397 | /* The operations on the CKCH architecture are locked so we can |
| 11398 | * manipulate ckch_store and ckch_inst */ |
| 11399 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11400 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 11401 | |
| 11402 | /* check if there is a certificate to lookup */ |
| 11403 | if (*args[3]) { |
| 11404 | if (*args[3] == '*') { |
| 11405 | if (!ckchs_transaction.new_ckchs) |
| 11406 | goto error; |
| 11407 | |
| 11408 | ckchs = ckchs_transaction.new_ckchs; |
| 11409 | |
| 11410 | if (strcmp(args[3] + 1, ckchs->path)) |
| 11411 | goto error; |
| 11412 | |
| 11413 | } else { |
| 11414 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 11415 | goto error; |
| 11416 | |
| 11417 | } |
| 11418 | |
| 11419 | if (ckchs->multi) |
| 11420 | goto error; |
| 11421 | |
| 11422 | appctx->ctx.cli.p0 = ckchs; |
| 11423 | /* use the IO handler that shows details */ |
| 11424 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 11425 | } |
| 11426 | |
| 11427 | return 0; |
| 11428 | |
| 11429 | error: |
| 11430 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11431 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 11432 | } |
| 11433 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11434 | /* release function of the `set ssl cert' command, free things and unlock the spinlock */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11435 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11436 | { |
| 11437 | struct ckch_store *new_ckchs; |
| 11438 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11439 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11440 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11441 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11442 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11443 | /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */ |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11444 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11445 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11446 | if (!new_ckchs) |
| 11447 | return; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11448 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11449 | /* if the allocation failed, we need to free everything from the temporary list */ |
| 11450 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 11451 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11452 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11453 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 11454 | if (sc0->order == 0) /* we only free if it's the first inserted */ |
| 11455 | SSL_CTX_free(sc0->ctx); |
| 11456 | LIST_DEL(&sc0->by_ckch_inst); |
| 11457 | free(sc0); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11458 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11459 | LIST_DEL(&ckchi->by_ckchs); |
| 11460 | free(ckchi); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11461 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11462 | ckchs_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11463 | } |
| 11464 | } |
| 11465 | |
| 11466 | |
| 11467 | /* |
| 11468 | * This function tries to create the new ckch_inst and their SNIs |
| 11469 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11470 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11471 | { |
| 11472 | struct stream_interface *si = appctx->owner; |
| 11473 | int y = 0; |
| 11474 | char *err = NULL; |
| 11475 | int errcode = 0; |
| 11476 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 11477 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11478 | struct buffer *trash = alloc_trash_chunk(); |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11479 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11480 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 11481 | if (trash == NULL) |
| 11482 | goto error; |
| 11483 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11484 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 11485 | goto error; |
| 11486 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11487 | while (1) { |
| 11488 | switch (appctx->st2) { |
| 11489 | case SETCERT_ST_INIT: |
| 11490 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11491 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11492 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11493 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11494 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11495 | } |
| 11496 | appctx->st2 = SETCERT_ST_GEN; |
| 11497 | /* fallthrough */ |
| 11498 | case SETCERT_ST_GEN: |
| 11499 | /* |
| 11500 | * This state generates the ckch instances with their |
| 11501 | * sni_ctxs and SSL_CTX. |
| 11502 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11503 | * Since the SSL_CTX generation can be CPU consumer, we |
| 11504 | * yield every 10 instances. |
| 11505 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11506 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11507 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11508 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11509 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11510 | if (!new_ckchs) |
| 11511 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11512 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11513 | /* get the next ckchi to regenerate */ |
| 11514 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 11515 | /* we didn't start yet, set it to the first elem */ |
| 11516 | if (ckchi == NULL) |
| 11517 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11518 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11519 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11520 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11521 | struct ckch_inst *new_inst; |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11522 | char **sni_filter = NULL; |
| 11523 | int fcount = 0; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11524 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11525 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11526 | if (y >= 10) { |
| 11527 | /* save the next ckchi to compute */ |
| 11528 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 11529 | goto yield; |
| 11530 | } |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 11531 | if (ckchi->filters) { |
| 11532 | errcode |= ckch_inst_sni_ctx_to_sni_filters(ckchi, &sni_filter, &fcount, &err); |
| 11533 | if (errcode & ERR_CODE) |
| 11534 | goto error; |
| 11535 | } |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11536 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11537 | if (new_ckchs->multi) |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11538 | errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11539 | else |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11540 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
| 11541 | |
| 11542 | free_sni_filters(sni_filter, fcount); |
| 11543 | sni_filter = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11544 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11545 | if (errcode & ERR_CODE) |
| 11546 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11547 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 11548 | /* if the previous ckchi was used as the default */ |
| 11549 | if (ckchi->is_default) |
| 11550 | new_inst->is_default = 1; |
| 11551 | |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11552 | /* we need to initialize the SSL_CTX generated */ |
William Lallemand | 696f317 | 2020-02-07 20:45:24 +0100 | [diff] [blame] | 11553 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 11554 | list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) { |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 11555 | if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11556 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 11557 | if (errcode & ERR_CODE) |
| 11558 | goto error; |
| 11559 | } |
| 11560 | } |
| 11561 | |
| 11562 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11563 | /* display one dot per new instance */ |
| 11564 | chunk_appendf(trash, "."); |
| 11565 | /* link the new ckch_inst to the duplicate */ |
| 11566 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11567 | y++; |
| 11568 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11569 | appctx->st2 = SETCERT_ST_INSERT; |
| 11570 | /* fallthrough */ |
| 11571 | case SETCERT_ST_INSERT: |
| 11572 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11573 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11574 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11575 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11576 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11577 | if (!new_ckchs) |
| 11578 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11579 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 11580 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11581 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 11582 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11583 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 11584 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11585 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11586 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11587 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 11588 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11589 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11590 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11591 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 11592 | ebmb_delete(&sc0->name); |
| 11593 | LIST_DEL(&sc0->by_ckch_inst); |
| 11594 | free(sc0); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11595 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11596 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11597 | LIST_DEL(&ckchi->by_ckchs); |
| 11598 | free(ckchi); |
| 11599 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11600 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11601 | /* Replace the old ckchs by the new one */ |
| 11602 | ebmb_delete(&old_ckchs->node); |
| 11603 | ckchs_free(old_ckchs); |
| 11604 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11605 | appctx->st2 = SETCERT_ST_FIN; |
| 11606 | /* fallthrough */ |
| 11607 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11608 | /* we achieved the transaction, we can set everything to NULL */ |
| 11609 | free(ckchs_transaction.path); |
| 11610 | ckchs_transaction.path = NULL; |
| 11611 | ckchs_transaction.new_ckchs = NULL; |
| 11612 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11613 | goto end; |
| 11614 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11615 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11616 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11617 | |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 11618 | chunk_appendf(trash, "\n"); |
| 11619 | if (errcode & ERR_WARN) |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 11620 | chunk_appendf(trash, "%s", err); |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 11621 | chunk_appendf(trash, "Success!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11622 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11623 | si_rx_room_blk(si); |
| 11624 | free_trash_chunk(trash); |
| 11625 | /* success: call the release function and don't come back */ |
| 11626 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11627 | yield: |
| 11628 | /* store the state */ |
| 11629 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11630 | si_rx_room_blk(si); |
| 11631 | free_trash_chunk(trash); |
| 11632 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11633 | return 0; /* should come back */ |
| 11634 | |
| 11635 | error: |
| 11636 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 11637 | if (trash) { |
| 11638 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 11639 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11640 | si_rx_room_blk(si); |
| 11641 | free_trash_chunk(trash); |
| 11642 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11643 | /* error: call the release function and don't come back */ |
| 11644 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11645 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11646 | |
| 11647 | /* |
| 11648 | * Parsing function of 'commit ssl cert' |
| 11649 | */ |
| 11650 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11651 | { |
| 11652 | char *err = NULL; |
| 11653 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11654 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11655 | return 1; |
| 11656 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11657 | if (!*args[3]) |
| 11658 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 11659 | |
| 11660 | /* The operations on the CKCH architecture are locked so we can |
| 11661 | * manipulate ckch_store and ckch_inst */ |
| 11662 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11663 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 11664 | |
| 11665 | if (!ckchs_transaction.path) { |
| 11666 | memprintf(&err, "No ongoing transaction! !\n"); |
| 11667 | goto error; |
| 11668 | } |
| 11669 | |
| 11670 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11671 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 11672 | goto error; |
| 11673 | } |
| 11674 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11675 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11676 | if (ckchs_transaction.new_ckchs->multi) { |
| 11677 | int n; |
| 11678 | |
| 11679 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11680 | if (ckchs_transaction.new_ckchs->ckch[n].cert && !X509_check_private_key(ckchs_transaction.new_ckchs->ckch[n].cert, ckchs_transaction.new_ckchs->ckch[n].key)) { |
| 11681 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11682 | goto error; |
| 11683 | } |
| 11684 | } |
| 11685 | } else |
| 11686 | #endif |
| 11687 | { |
| 11688 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 11689 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11690 | goto error; |
| 11691 | } |
| 11692 | } |
| 11693 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11694 | /* init the appctx structure */ |
| 11695 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11696 | appctx->ctx.ssl.next_ckchi = NULL; |
| 11697 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 11698 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 11699 | |
| 11700 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 11701 | return 0; |
| 11702 | |
| 11703 | error: |
| 11704 | |
| 11705 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11706 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 11707 | |
| 11708 | return cli_dynerr(appctx, err); |
| 11709 | } |
| 11710 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11711 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11712 | * Parsing function of `set ssl cert`, it updates or creates a temporary ckch. |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11713 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11714 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11715 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 11716 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11717 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11718 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11719 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11720 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11721 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11722 | char *end; |
| 11723 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11724 | struct cert_key_and_chain *ckch; |
| 11725 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11726 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11727 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11728 | return 1; |
| 11729 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11730 | if ((buf = alloc_trash_chunk()) == NULL) |
| 11731 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11732 | |
| 11733 | if (!*args[3] || !payload) |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 11734 | return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11735 | |
| 11736 | /* The operations on the CKCH architecture are locked so we can |
| 11737 | * manipulate ckch_store and ckch_inst */ |
| 11738 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11739 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 11740 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11741 | if (!chunk_strcpy(buf, args[3])) { |
| 11742 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11743 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11744 | goto end; |
| 11745 | } |
| 11746 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11747 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11748 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11749 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11750 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 11751 | *end = '\0'; |
| 11752 | type = cert_exts[i].type; |
| 11753 | break; |
| 11754 | } |
| 11755 | } |
| 11756 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11757 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11758 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11759 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11760 | /* if there is an ongoing transaction */ |
| 11761 | if (ckchs_transaction.path) { |
| 11762 | /* if the ongoing transaction is a bundle, we need to find which part of the bundle need to be updated */ |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11763 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11764 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11765 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11766 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11767 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11768 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11769 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11770 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11771 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11772 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11773 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11774 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11775 | break; |
| 11776 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11777 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11778 | if (bundle < 0) { |
| 11779 | memprintf(&err, "The ongoing transaction is the '%s' bundle. You need to specify which part of the bundle you want to update ('%s.{rsa,ecdsa,dsa}')\n", ckchs_transaction.path, buf->area); |
| 11780 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11781 | goto end; |
| 11782 | } |
| 11783 | } |
| 11784 | #endif |
| 11785 | |
| 11786 | /* if there is an ongoing transaction, check if this is the same file */ |
| 11787 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 11788 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 11789 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11790 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11791 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11792 | |
| 11793 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 11794 | |
| 11795 | } else { |
| 11796 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 11797 | |
| 11798 | /* lookup for the certificate in the tree: |
| 11799 | * check if this is used as a bundle AND as a unique certificate */ |
| 11800 | for (i = 0; i < 2; i++) { |
| 11801 | |
| 11802 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 11803 | /* only the bundle name is in the tree and you should |
| 11804 | * never update a bundle name, only a filename */ |
| 11805 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 11806 | /* we tried to look for a non-bundle and we found a bundle */ |
| 11807 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 11808 | err ? err : "", args[3], args[3]); |
| 11809 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11810 | goto end; |
| 11811 | } |
William Lallemand | 3246d94 | 2019-11-04 14:02:11 +0100 | [diff] [blame] | 11812 | /* If we want a bundle but this is not a bundle |
| 11813 | * example: When you try to update <file>.rsa, but |
| 11814 | * <file> is a regular file */ |
| 11815 | if (bundle >= 0 && find_ckchs[i]->multi == 0) { |
| 11816 | find_ckchs[i] = NULL; |
| 11817 | break; |
| 11818 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11819 | } |
| 11820 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11821 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11822 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11823 | int j; |
| 11824 | |
| 11825 | /* check if it was used in a bundle by removing the |
| 11826 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 11827 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11828 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11829 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11830 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11831 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11832 | break; |
| 11833 | } |
| 11834 | } |
William Lallemand | 37031b8 | 2019-11-04 13:38:53 +0100 | [diff] [blame] | 11835 | if (bundle < 0) /* we didn't find a bundle extension */ |
| 11836 | break; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11837 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11838 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11839 | /* bundles are not supported here, so we don't need to lookup again */ |
| 11840 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11841 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11842 | } |
| 11843 | |
| 11844 | if (find_ckchs[0] && find_ckchs[1]) { |
| 11845 | memprintf(&err, "%sUpdating a certificate which is used in the HAProxy configuration as a bundle and as a unique certificate is not supported. ('%s' and '%s')\n", |
| 11846 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 11847 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11848 | goto end; |
| 11849 | } |
| 11850 | |
| 11851 | appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1]; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11852 | } |
| 11853 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11854 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11855 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11856 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11857 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11858 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11859 | } |
| 11860 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame] | 11861 | if (!appctx->ctx.ssl.path) { |
| 11862 | /* this is a new transaction, set the path of the transaction */ |
| 11863 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 11864 | if (!appctx->ctx.ssl.path) { |
| 11865 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11866 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11867 | goto end; |
| 11868 | } |
| 11869 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11870 | |
| 11871 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11872 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11873 | /* duplicate the ckch store */ |
| 11874 | new_ckchs = ckchs_dup(old_ckchs); |
| 11875 | if (!new_ckchs) { |
| 11876 | memprintf(&err, "%sCannot allocate memory!\n", |
| 11877 | err ? err : ""); |
| 11878 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11879 | goto end; |
| 11880 | } |
| 11881 | |
| 11882 | if (!new_ckchs->multi) |
| 11883 | ckch = new_ckchs->ckch; |
| 11884 | else |
| 11885 | ckch = &new_ckchs->ckch[bundle]; |
| 11886 | |
| 11887 | /* appply the change on the duplicate */ |
| 11888 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 11889 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11890 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11891 | goto end; |
| 11892 | } |
| 11893 | |
| 11894 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 11895 | |
| 11896 | /* we succeed, we can save the ckchs in the transaction */ |
| 11897 | |
| 11898 | /* if there wasn't a transaction, update the old ckchs */ |
William Dauchy | c8bb153 | 2019-11-24 15:04:20 +0100 | [diff] [blame] | 11899 | if (!ckchs_transaction.old_ckchs) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11900 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11901 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 11902 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 11903 | } else { |
| 11904 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 11905 | |
| 11906 | } |
| 11907 | |
| 11908 | /* free the previous ckchs if there was a transaction */ |
| 11909 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11910 | |
| 11911 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 11912 | |
| 11913 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11914 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11915 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11916 | end: |
| 11917 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11918 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11919 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11920 | |
| 11921 | ckchs_free(appctx->ctx.ssl.new_ckchs); |
| 11922 | appctx->ctx.ssl.new_ckchs = NULL; |
| 11923 | |
| 11924 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11925 | |
| 11926 | free(appctx->ctx.ssl.path); |
| 11927 | appctx->ctx.ssl.path = NULL; |
| 11928 | |
| 11929 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11930 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11931 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11932 | |
| 11933 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11934 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11935 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11936 | /* TODO: handle the ERR_WARN which are not handled because of the io_handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11937 | } |
| 11938 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11939 | /* parsing function of 'abort ssl cert' */ |
| 11940 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11941 | { |
| 11942 | char *err = NULL; |
| 11943 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11944 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11945 | return 1; |
| 11946 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11947 | if (!*args[3]) |
| 11948 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 11949 | |
| 11950 | /* The operations on the CKCH architecture are locked so we can |
| 11951 | * manipulate ckch_store and ckch_inst */ |
| 11952 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11953 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 11954 | |
| 11955 | if (!ckchs_transaction.path) { |
| 11956 | memprintf(&err, "No ongoing transaction!\n"); |
| 11957 | goto error; |
| 11958 | } |
| 11959 | |
| 11960 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11961 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 11962 | goto error; |
| 11963 | } |
| 11964 | |
| 11965 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 11966 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11967 | ckchs_transaction.new_ckchs = NULL; |
| 11968 | ckchs_free(ckchs_transaction.old_ckchs); |
| 11969 | ckchs_transaction.old_ckchs = NULL; |
| 11970 | free(ckchs_transaction.path); |
| 11971 | ckchs_transaction.path = NULL; |
| 11972 | |
| 11973 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11974 | |
| 11975 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 11976 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 11977 | |
| 11978 | error: |
| 11979 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11980 | |
| 11981 | return cli_dynerr(appctx, err); |
| 11982 | } |
| 11983 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11984 | /* parsing function of 'new ssl cert' */ |
| 11985 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11986 | { |
| 11987 | struct ckch_store *store; |
| 11988 | char *err = NULL; |
| 11989 | char *path; |
| 11990 | |
| 11991 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11992 | return 1; |
| 11993 | |
| 11994 | if (!*args[3]) |
| 11995 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 11996 | |
| 11997 | path = args[3]; |
| 11998 | |
| 11999 | /* The operations on the CKCH architecture are locked so we can |
| 12000 | * manipulate ckch_store and ckch_inst */ |
| 12001 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12002 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 12003 | |
| 12004 | store = ckchs_lookup(path); |
| 12005 | if (store != NULL) { |
| 12006 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 12007 | store = NULL; /* we don't want to free it */ |
| 12008 | goto error; |
| 12009 | } |
| 12010 | store = calloc(1, sizeof(*store) + strlen(path) + 1); |
| 12011 | if (!store) { |
| 12012 | memprintf(&err, "unable to allocate memory.\n"); |
| 12013 | goto error; |
| 12014 | } |
| 12015 | store->ckch = calloc(1, sizeof(*store->ckch)); |
| 12016 | if (!store->ckch) { |
| 12017 | memprintf(&err, "unable to allocate memory.\n"); |
| 12018 | goto error; |
| 12019 | } |
| 12020 | /* we won't create any instance */ |
| 12021 | LIST_INIT(&store->ckch_inst); |
| 12022 | |
| 12023 | /* we won't support multi-certificate bundle here */ |
| 12024 | store->multi = 0; |
| 12025 | |
| 12026 | /* insert into the ckchs tree */ |
| 12027 | memcpy(store->path, path, strlen(path) + 1); |
| 12028 | ebst_insert(&ckchs_tree, &store->node); |
| 12029 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 12030 | |
| 12031 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12032 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12033 | error: |
| 12034 | free(store); |
| 12035 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12036 | return cli_dynerr(appctx, err); |
| 12037 | } |
| 12038 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 12039 | 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] | 12040 | { |
| 12041 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 12042 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12043 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 12044 | |
| 12045 | if (!payload) |
| 12046 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12047 | |
| 12048 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12049 | if (!*payload) |
| 12050 | return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n"); |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 12051 | |
| 12052 | /* remove \r and \n from the payload */ |
| 12053 | for (i = 0, j = 0; payload[i]; i++) { |
| 12054 | if (payload[i] == '\r' || payload[i] == '\n') |
| 12055 | continue; |
| 12056 | payload[j++] = payload[i]; |
| 12057 | } |
| 12058 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12059 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12060 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12061 | if (ret < 0) |
| 12062 | return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12063 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12064 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12065 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12066 | if (err) |
| 12067 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 12068 | else |
| 12069 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12070 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12071 | |
| 12072 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12073 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12074 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12075 | #endif |
| 12076 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12077 | } |
| 12078 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12079 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12080 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 12081 | { |
| 12082 | switch (arg->type) { |
| 12083 | case ARGT_STR: |
| 12084 | smp->data.type = SMP_T_STR; |
| 12085 | smp->data.u.str = arg->data.str; |
| 12086 | return 1; |
| 12087 | case ARGT_VAR: |
| 12088 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 12089 | return 0; |
| 12090 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 12091 | return 0; |
| 12092 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 12093 | return 0; |
| 12094 | return 1; |
| 12095 | default: |
| 12096 | return 0; |
| 12097 | } |
| 12098 | } |
| 12099 | |
| 12100 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 12101 | const char *file, int line, char **err) |
| 12102 | { |
| 12103 | switch(args[0].data.sint) { |
| 12104 | case 128: |
| 12105 | case 192: |
| 12106 | case 256: |
| 12107 | break; |
| 12108 | default: |
| 12109 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 12110 | return 0; |
| 12111 | } |
| 12112 | /* Try to decode a variable. */ |
| 12113 | vars_check_arg(&args[1], NULL); |
| 12114 | vars_check_arg(&args[2], NULL); |
| 12115 | vars_check_arg(&args[3], NULL); |
| 12116 | return 1; |
| 12117 | } |
| 12118 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12119 | /* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12120 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 12121 | { |
| 12122 | struct sample nonce, key, aead_tag; |
| 12123 | struct buffer *smp_trash, *smp_trash_alloc; |
| 12124 | EVP_CIPHER_CTX *ctx; |
| 12125 | int dec_size, ret; |
| 12126 | |
| 12127 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 12128 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 12129 | return 0; |
| 12130 | |
| 12131 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 12132 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 12133 | return 0; |
| 12134 | |
| 12135 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 12136 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 12137 | return 0; |
| 12138 | |
| 12139 | smp_trash = get_trash_chunk(); |
| 12140 | smp_trash_alloc = alloc_trash_chunk(); |
| 12141 | if (!smp_trash_alloc) |
| 12142 | return 0; |
| 12143 | |
| 12144 | ctx = EVP_CIPHER_CTX_new(); |
| 12145 | |
| 12146 | if (!ctx) |
| 12147 | goto err; |
| 12148 | |
| 12149 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12150 | if (dec_size < 0) |
| 12151 | goto err; |
| 12152 | smp_trash->data = dec_size; |
| 12153 | |
| 12154 | /* Set cipher type and mode */ |
| 12155 | switch(arg_p[0].data.sint) { |
| 12156 | case 128: |
| 12157 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 12158 | break; |
| 12159 | case 192: |
| 12160 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 12161 | break; |
| 12162 | case 256: |
| 12163 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 12164 | break; |
| 12165 | } |
| 12166 | |
| 12167 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 12168 | |
| 12169 | /* Initialise IV */ |
| 12170 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 12171 | goto err; |
| 12172 | |
| 12173 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12174 | if (dec_size < 0) |
| 12175 | goto err; |
| 12176 | smp_trash->data = dec_size; |
| 12177 | |
| 12178 | /* Initialise key */ |
| 12179 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 12180 | goto err; |
| 12181 | |
| 12182 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 12183 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 12184 | goto err; |
| 12185 | |
| 12186 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 12187 | if (dec_size < 0) |
| 12188 | goto err; |
| 12189 | smp_trash_alloc->data = dec_size; |
| 12190 | dec_size = smp_trash->data; |
| 12191 | |
| 12192 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 12193 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 12194 | |
| 12195 | if (ret <= 0) |
| 12196 | goto err; |
| 12197 | |
| 12198 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 12199 | smp->data.u.str.area = smp_trash->area; |
| 12200 | smp->data.type = SMP_T_BIN; |
| 12201 | smp->flags &= ~SMP_F_CONST; |
| 12202 | free_trash_chunk(smp_trash_alloc); |
| 12203 | return 1; |
| 12204 | |
| 12205 | err: |
| 12206 | free_trash_chunk(smp_trash_alloc); |
| 12207 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12208 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12209 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12210 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12211 | /* Argument validation functions */ |
| 12212 | |
| 12213 | /* This function is used to validate the arguments passed to any "x_dn" ssl |
| 12214 | * keywords. These keywords support specifying a third parameter that must be |
| 12215 | * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK. |
| 12216 | */ |
| 12217 | int val_dnfmt(struct arg *arg, char **err_msg) |
| 12218 | { |
| 12219 | if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) { |
| 12220 | memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument."); |
| 12221 | return 0; |
| 12222 | } |
| 12223 | return 1; |
| 12224 | } |
| 12225 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12226 | /* register cli keywords */ |
| 12227 | static struct cli_kw_list cli_kws = {{ },{ |
| 12228 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12229 | { { "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] | 12230 | { { "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] | 12231 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 12232 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12233 | { { "new", "ssl", "cert", NULL }, "new ssl cert <certfile> : create a new certificate file to be used in a crt-list or a directory", cli_parse_new_cert, NULL, NULL }, |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12234 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 12235 | { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert }, |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12236 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 12237 | { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert }, |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 12238 | { { "dump", "ssl", "crt-list", NULL }, "dump ssl crt-list <filename> : dump the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL }, |
| 12239 | { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [<filename>] : show the list of crt-lists or the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12240 | { { NULL }, NULL, NULL, NULL } |
| 12241 | }}; |
| 12242 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12243 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12244 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12245 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12246 | * Please take care of keeping this list alphabetically sorted. |
| 12247 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12248 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12249 | { "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] | 12250 | { "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] | 12251 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 12252 | { "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] | 12253 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12254 | { "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] | 12255 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 12256 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 12257 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 12258 | { "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] | 12259 | { "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] | 12260 | { "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] | 12261 | { "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] | 12262 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12263 | { "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] | 12264 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12265 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12266 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12267 | { "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] | 12268 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12269 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12270 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12271 | { "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] | 12272 | { "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] | 12273 | { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12274 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 12275 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12276 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12277 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12278 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12279 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 12280 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12281 | { "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] | 12282 | { "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] | 12283 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12284 | { "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] | 12285 | { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12286 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 12287 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12288 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12289 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12290 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12291 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 12292 | { "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] | 12293 | { "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] | 12294 | { "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] | 12295 | { "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] | 12296 | { "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] | 12297 | { "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] | 12298 | { "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] | 12299 | { "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] | 12300 | { "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] | 12301 | { "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] | 12302 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12303 | { "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] | 12304 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 12305 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12306 | { "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] | 12307 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12308 | { "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] | 12309 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 12310 | { "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] | 12311 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12312 | { "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] | 12313 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12314 | { "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] | 12315 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12316 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12317 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12318 | { "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] | 12319 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12320 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 12321 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12322 | { "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] | 12323 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12324 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12325 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12326 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12327 | { "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] | 12328 | { NULL, NULL, 0, 0, 0 }, |
| 12329 | }}; |
| 12330 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12331 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 12332 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12333 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12334 | * Please take care of keeping this list alphabetically sorted. |
| 12335 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12336 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12337 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 12338 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12339 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12340 | }}; |
| 12341 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12342 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 12343 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12344 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12345 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12346 | * all code contributors. |
| 12347 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12348 | * the config parser can report an appropriate error when a known keyword was |
| 12349 | * not enabled. |
| 12350 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12351 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12352 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12353 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12354 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12355 | { "ca-verify-file", ssl_bind_parse_ca_verify_file, 1 }, /* set CAverify file to process verify on client cert */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12356 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12357 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12358 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12359 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12360 | { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */ |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 12361 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12362 | { "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] | 12363 | { "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] | 12364 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 12365 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 12366 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12367 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12368 | { NULL, NULL, 0 }, |
| 12369 | }; |
| 12370 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12371 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 12372 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 12373 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12374 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12375 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12376 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12377 | { "ca-verify-file", bind_parse_ca_verify_file, 1 }, /* set CAverify file to process verify on client cert */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12378 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 12379 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 12380 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 12381 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12382 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12383 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12384 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12385 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12386 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12387 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12388 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 12389 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 12390 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 12391 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 12392 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 12393 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 12394 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12395 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12396 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 12397 | { "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] | 12398 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 12399 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 12400 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 12401 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12402 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12403 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 12404 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12405 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 12406 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12407 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 12408 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 12409 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12410 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 12411 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12412 | { NULL, NULL, 0 }, |
| 12413 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12414 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12415 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 12416 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12417 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12418 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12419 | * all code contributors. |
| 12420 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12421 | * the config parser can report an appropriate error when a known keyword was |
| 12422 | * not enabled. |
| 12423 | */ |
| 12424 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 12425 | { "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] | 12426 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12427 | { "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] | 12428 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 12429 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12430 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 12431 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12432 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12433 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 12434 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12435 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 12436 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 12437 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 12438 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 12439 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 12440 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 12441 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 12442 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 12443 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 12444 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 12445 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 12446 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 12447 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 12448 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 12449 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 12450 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 12451 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 12452 | { "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] | 12453 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12454 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 12455 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 12456 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 12457 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 12458 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 12459 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 12460 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 12461 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 12462 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 12463 | { "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] | 12464 | { NULL, NULL, 0, 0 }, |
| 12465 | }}; |
| 12466 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12467 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 12468 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12469 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 12470 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 12471 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 12472 | { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 12473 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12474 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 12475 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 12476 | #ifndef OPENSSL_NO_DH |
| 12477 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 12478 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 12479 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12480 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12481 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12482 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 12483 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 12484 | #ifndef OPENSSL_NO_DH |
| 12485 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 12486 | #endif |
| 12487 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 12488 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 12489 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 12490 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12491 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 12492 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 12493 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12494 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12495 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12496 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12497 | #endif |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 12498 | { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12499 | { 0, NULL, NULL }, |
| 12500 | }}; |
| 12501 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12502 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12503 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12504 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12505 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12506 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12507 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 12508 | #endif |
| 12509 | { NULL, NULL, 0, 0, 0 }, |
| 12510 | }}; |
| 12511 | |
| 12512 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 12513 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 12514 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 12515 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12516 | .snd_buf = ssl_sock_from_buf, |
| 12517 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 12518 | .subscribe = ssl_subscribe, |
| 12519 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 12520 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 12521 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12522 | .rcv_pipe = NULL, |
| 12523 | .snd_pipe = NULL, |
| 12524 | .shutr = NULL, |
| 12525 | .shutw = ssl_sock_shutw, |
| 12526 | .close = ssl_sock_close, |
| 12527 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 12528 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 12529 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 12530 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 12531 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 12532 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 12533 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12534 | }; |
| 12535 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12536 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 12537 | struct session *sess, struct stream *s, int flags) |
| 12538 | { |
| 12539 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12540 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12541 | |
| 12542 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12543 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12544 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12545 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12546 | 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] | 12547 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12548 | s->req.flags |= CF_READ_NULL; |
| 12549 | return ACT_RET_YIELD; |
| 12550 | } |
| 12551 | } |
| 12552 | return (ACT_RET_CONT); |
| 12553 | } |
| 12554 | |
| 12555 | 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) |
| 12556 | { |
| 12557 | rule->action_ptr = ssl_action_wait_for_hs; |
| 12558 | |
| 12559 | return ACT_RET_PRS_OK; |
| 12560 | } |
| 12561 | |
| 12562 | static struct action_kw_list http_req_actions = {ILH, { |
| 12563 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 12564 | { /* END */ } |
| 12565 | }}; |
| 12566 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12567 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 12568 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12569 | #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] | 12570 | |
| 12571 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 12572 | { |
| 12573 | if (ptr) { |
| 12574 | chunk_destroy(ptr); |
| 12575 | free(ptr); |
| 12576 | } |
| 12577 | } |
| 12578 | |
| 12579 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 12580 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 12581 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12582 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 12583 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 12584 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12585 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12586 | static void __ssl_sock_init(void) |
| 12587 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12588 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12589 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12590 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12591 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12592 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 12593 | if (global_ssl.listen_default_ciphers) |
| 12594 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 12595 | if (global_ssl.connect_default_ciphers) |
| 12596 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12597 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12598 | if (global_ssl.listen_default_ciphersuites) |
| 12599 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 12600 | if (global_ssl.connect_default_ciphersuites) |
| 12601 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 12602 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 12603 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 12604 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 12605 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12606 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12607 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12608 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12609 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12610 | n = sk_SSL_COMP_num(cm); |
| 12611 | while (n--) { |
| 12612 | (void) sk_SSL_COMP_pop(cm); |
| 12613 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12614 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12615 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12616 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12617 | ssl_locking_init(); |
| 12618 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12619 | #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] | 12620 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 12621 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 12622 | 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] | 12623 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12624 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12625 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 12626 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12627 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 12628 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12629 | hap_register_post_check(tlskeys_finalize_config); |
| 12630 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12631 | |
| 12632 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 12633 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 12634 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 12635 | hap_register_post_deinit(ssl_free_global_issuers); |
| 12636 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12637 | #ifndef OPENSSL_NO_DH |
| 12638 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 12639 | hap_register_post_deinit(ssl_free_dh); |
| 12640 | #endif |
| 12641 | #ifndef OPENSSL_NO_ENGINE |
| 12642 | hap_register_post_deinit(ssl_free_engines); |
| 12643 | #endif |
| 12644 | /* Load SSL string for the verbose & debug mode. */ |
| 12645 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 12646 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 12647 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 12648 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 12649 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 12650 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 12651 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 12652 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 12653 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12654 | |
| 12655 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12656 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 12657 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12658 | /* Compute and register the version string */ |
| 12659 | static void ssl_register_build_options() |
| 12660 | { |
| 12661 | char *ptr = NULL; |
| 12662 | int i; |
| 12663 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12664 | memprintf(&ptr, "Built with OpenSSL version : " |
| 12665 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 12666 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12667 | #else /* OPENSSL_IS_BORINGSSL */ |
| 12668 | OPENSSL_VERSION_TEXT |
| 12669 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12670 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 12671 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12672 | #endif |
| 12673 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 12674 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12675 | "no (library version too old)" |
| 12676 | #elif defined(OPENSSL_NO_TLSEXT) |
| 12677 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 12678 | #else |
| 12679 | "yes" |
| 12680 | #endif |
| 12681 | "", ptr); |
| 12682 | |
| 12683 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 12684 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 12685 | "yes" |
| 12686 | #else |
| 12687 | #ifdef OPENSSL_NO_TLSEXT |
| 12688 | "no (because of OPENSSL_NO_TLSEXT)" |
| 12689 | #else |
| 12690 | "no (version might be too old, 0.9.8f min needed)" |
| 12691 | #endif |
| 12692 | #endif |
| 12693 | "", ptr); |
| 12694 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 12695 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 12696 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 12697 | if (methodVersions[i].option) |
| 12698 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 12699 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12700 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12701 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12702 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12703 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 12704 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12705 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12706 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12707 | void ssl_free_engines(void) { |
| 12708 | struct ssl_engine_list *wl, *wlb; |
| 12709 | /* free up engine list */ |
| 12710 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 12711 | ENGINE_finish(wl->e); |
| 12712 | ENGINE_free(wl->e); |
| 12713 | LIST_DEL(&wl->list); |
| 12714 | free(wl); |
| 12715 | } |
| 12716 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12717 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 12718 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12719 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12720 | void ssl_free_dh(void) { |
| 12721 | if (local_dh_1024) { |
| 12722 | DH_free(local_dh_1024); |
| 12723 | local_dh_1024 = NULL; |
| 12724 | } |
| 12725 | if (local_dh_2048) { |
| 12726 | DH_free(local_dh_2048); |
| 12727 | local_dh_2048 = NULL; |
| 12728 | } |
| 12729 | if (local_dh_4096) { |
| 12730 | DH_free(local_dh_4096); |
| 12731 | local_dh_4096 = NULL; |
| 12732 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 12733 | if (global_dh) { |
| 12734 | DH_free(global_dh); |
| 12735 | global_dh = NULL; |
| 12736 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12737 | } |
| 12738 | #endif |
| 12739 | |
| 12740 | __attribute__((destructor)) |
| 12741 | static void __ssl_sock_deinit(void) |
| 12742 | { |
| 12743 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12744 | if (ssl_ctx_lru_tree) { |
| 12745 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 12746 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12747 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12748 | #endif |
| 12749 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12750 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12751 | ERR_remove_state(0); |
| 12752 | ERR_free_strings(); |
| 12753 | |
| 12754 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12755 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12756 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12757 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12758 | CRYPTO_cleanup_all_ex_data(); |
| 12759 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 12760 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12761 | } |
| 12762 | |
| 12763 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12764 | /* |
| 12765 | * Local variables: |
| 12766 | * c-indent-level: 8 |
| 12767 | * c-basic-offset: 8 |
| 12768 | * End: |
| 12769 | */ |