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 | |
| 3942 | tmp_filter = malloc(sizeof(*tmp_filter) * tmp_fcount); |
| 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 | |
| 4659 | dst = calloc(fcount, sizeof(*dst) + 1); |
| 4660 | if (!dst) |
| 4661 | return NULL; |
| 4662 | |
| 4663 | for (i = 0; i < fcount; i++) { |
| 4664 | dst[i] = strdup(args[i]); |
| 4665 | if (!dst[i]) |
| 4666 | goto error; |
| 4667 | } |
| 4668 | return dst; |
| 4669 | |
| 4670 | error: |
| 4671 | crtlist_free_filters(dst); |
| 4672 | return NULL; |
| 4673 | } |
| 4674 | |
| 4675 | |
| 4676 | /* Free a crtlist, from the crt_entry to the content of the ssl_conf */ |
| 4677 | static void crtlist_free(struct crtlist *crtlist) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4678 | { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4679 | struct crtlist_entry *entry, *s_entry; |
| 4680 | |
| 4681 | if (crtlist == NULL) |
| 4682 | return; |
| 4683 | |
| 4684 | list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) { |
| 4685 | ebpt_delete(&entry->node); |
| 4686 | LIST_DEL(&entry->by_crtlist); |
| 4687 | crtlist_free_filters(entry->filters); |
| 4688 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 4689 | free(entry->ssl_conf); |
| 4690 | free(entry); |
| 4691 | } |
| 4692 | ebmb_delete(&crtlist->node); |
| 4693 | free(crtlist); |
| 4694 | } |
| 4695 | |
| 4696 | /* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure |
| 4697 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4698 | * |
| 4699 | * This function tries to open and store certificate files. |
| 4700 | */ |
| 4701 | static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err) |
| 4702 | { |
| 4703 | struct crtlist *newlist; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4704 | char thisline[CRT_LINESIZE]; |
| 4705 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4706 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4707 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4708 | int linenum = 0; |
| 4709 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4710 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4711 | if ((f = fopen(file, "r")) == NULL) { |
| 4712 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4713 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4714 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4715 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4716 | newlist = malloc(sizeof(*newlist) + strlen(file) + 1); |
| 4717 | if (newlist == NULL) { |
| 4718 | memprintf(err, "Not enough memory!"); |
| 4719 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4720 | goto error; |
| 4721 | } |
| 4722 | memcpy(newlist->node.key, file, strlen(file) + 1); |
| 4723 | newlist->entries = EB_ROOT; |
| 4724 | LIST_INIT(&newlist->ord_entries); |
| 4725 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4726 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4727 | struct crtlist_entry *entry; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4728 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4729 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4730 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4731 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4732 | char *crt_path; |
| 4733 | struct ssl_bind_conf *ssl_conf = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4734 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4735 | |
| 4736 | linenum++; |
| 4737 | end = line + strlen(line); |
| 4738 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4739 | /* Check if we reached the limit and the last char is not \n. |
| 4740 | * Watch out for the last line without the terminating '\n'! |
| 4741 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4742 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4743 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4744 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4745 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4746 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4747 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4748 | newarg = 1; |
| 4749 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4750 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 4751 | /* end of string, end of loop */ |
| 4752 | *line = 0; |
| 4753 | break; |
Willy Tarreau | ded15b7 | 2020-02-25 07:51:59 +0100 | [diff] [blame] | 4754 | } else if (isspace((unsigned char)*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4755 | newarg = 1; |
| 4756 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4757 | } else if (*line == '[') { |
| 4758 | if (ssl_b) { |
| 4759 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4760 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4761 | break; |
| 4762 | } |
| 4763 | if (!arg) { |
| 4764 | 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] | 4765 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4766 | break; |
| 4767 | } |
| 4768 | ssl_b = arg; |
| 4769 | newarg = 1; |
| 4770 | *line = 0; |
| 4771 | } else if (*line == ']') { |
| 4772 | if (ssl_e) { |
| 4773 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4774 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4775 | break; |
| 4776 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4777 | if (!ssl_b) { |
| 4778 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4779 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4780 | break; |
| 4781 | } |
| 4782 | ssl_e = arg; |
| 4783 | newarg = 1; |
| 4784 | *line = 0; |
| 4785 | } else if (newarg) { |
| 4786 | if (arg == MAX_CRT_ARGS) { |
| 4787 | 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] | 4788 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4789 | break; |
| 4790 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4791 | newarg = 0; |
| 4792 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4793 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4794 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4795 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4796 | if (cfgerr) |
| 4797 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4798 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4799 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4800 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4801 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4802 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4803 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4804 | crt_path = args[0]; |
| 4805 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4806 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4807 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4808 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4809 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4810 | break; |
| 4811 | } |
| 4812 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4813 | crt_path = path; |
| 4814 | } |
| 4815 | |
| 4816 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4817 | cur_arg = ssl_b ? ssl_b : 1; |
| 4818 | while (cur_arg < ssl_e) { |
| 4819 | newarg = 0; |
| 4820 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4821 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4822 | newarg = 1; |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4823 | 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] | 4824 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4825 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4826 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4827 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4828 | } |
| 4829 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4830 | break; |
| 4831 | } |
| 4832 | } |
| 4833 | if (!cfgerr && !newarg) { |
| 4834 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4835 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4836 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4837 | break; |
| 4838 | } |
| 4839 | } |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4840 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4841 | /* Look for a ckch_store or create one */ |
| 4842 | ckchs = ckchs_lookup(crt_path); |
| 4843 | if (ckchs == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4844 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4845 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4846 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4847 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4848 | } |
William Lallemand | 909086e | 2020-03-17 16:53:27 +0100 | [diff] [blame] | 4849 | if (ckchs == NULL) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4850 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4851 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4852 | entry = malloc(sizeof(*entry)); |
| 4853 | if (entry == NULL) { |
| 4854 | memprintf(err, "Not enough memory!"); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4855 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4856 | } |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4857 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4858 | if (cfgerr & ERR_CODE) { |
| 4859 | free(entry); |
| 4860 | entry = NULL; |
| 4861 | ssl_sock_free_ssl_conf(ssl_conf); |
| 4862 | free(ssl_conf); |
| 4863 | ssl_conf = NULL; |
| 4864 | goto error; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4865 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4866 | entry->node.key = ckchs; |
| 4867 | entry->ssl_conf = ssl_conf; |
| 4868 | /* filters */ |
| 4869 | entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1); |
| 4870 | entry->fcount = arg - cur_arg - 1; |
| 4871 | ebpt_insert(&newlist->entries, &entry->node); |
| 4872 | LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4873 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4874 | if (cfgerr & ERR_CODE) |
| 4875 | goto error; |
| 4876 | |
| 4877 | fclose(f); |
| 4878 | *crtlist = newlist; |
| 4879 | |
| 4880 | return cfgerr; |
| 4881 | error: |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4882 | fclose(f); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4883 | crtlist_free(newlist); |
| 4884 | return cfgerr; |
| 4885 | } |
| 4886 | |
| 4887 | /* Load a crt-list file, this is done in 2 parts: |
| 4888 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 4889 | * - generate the instances by iterating on entries in the crtlist struct |
| 4890 | * |
| 4891 | * Nothing is locked there, this function is used in the configuration parser. |
| 4892 | * |
| 4893 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4894 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4895 | 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] | 4896 | { |
| 4897 | struct crtlist *crtlist = NULL; |
| 4898 | struct ebmb_node *eb; |
| 4899 | struct crtlist_entry *entry; |
| 4900 | struct list instances; /* temporary list head */ |
| 4901 | int cfgerr = 0; |
| 4902 | |
| 4903 | LIST_INIT(&instances); |
| 4904 | /* look for an existing crtlist or create one */ |
| 4905 | eb = ebst_lookup(&crtlists_tree, file); |
| 4906 | if (eb) { |
| 4907 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 4908 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4909 | /* load a crt-list OR a directory */ |
| 4910 | if (dir) |
| 4911 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 4912 | else |
| 4913 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 4914 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4915 | if (!(cfgerr & ERR_CODE)) |
| 4916 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 4917 | } |
| 4918 | |
| 4919 | if (cfgerr & ERR_CODE) { |
| 4920 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4921 | goto error; |
| 4922 | } |
| 4923 | |
| 4924 | /* generates ckch instance from the crtlist_entry */ |
| 4925 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 4926 | struct ckch_store *store; |
| 4927 | struct ckch_inst *ckch_inst = NULL; |
| 4928 | |
| 4929 | store = entry->node.key; |
| 4930 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 4931 | if (cfgerr & ERR_CODE) { |
| 4932 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 4933 | goto error; |
| 4934 | } |
| 4935 | LIST_ADDQ(&instances, &ckch_inst->by_crtlist_entry); |
| 4936 | } |
| 4937 | /* add the instances to the actual instance list in the crtlist_entry */ |
| 4938 | LIST_SPLICE(&entry->ckch_inst, &instances); |
| 4939 | |
| 4940 | return cfgerr; |
| 4941 | error: |
| 4942 | { |
| 4943 | struct ckch_inst *inst, *s_inst; |
| 4944 | |
| 4945 | list_for_each_entry_safe(inst, s_inst, &instances, by_crtlist_entry) { |
| 4946 | struct sni_ctx *sni, *s_sni; |
| 4947 | |
| 4948 | /* free the sni_ctx */ |
| 4949 | list_for_each_entry_safe(sni, s_sni, &inst->sni_ctx, by_ckch_inst) { |
| 4950 | ebmb_delete(&sni->name); |
| 4951 | LIST_DEL(&sni->by_ckch_inst); |
| 4952 | free(sni); |
| 4953 | } |
| 4954 | LIST_DEL(&inst->by_crtlist_entry); |
| 4955 | free(inst); |
| 4956 | } |
| 4957 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4958 | return cfgerr; |
| 4959 | } |
| 4960 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4961 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 4962 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 4963 | { |
| 4964 | struct stat buf; |
| 4965 | char fp[MAXPATHLEN+1]; |
| 4966 | int cfgerr = 0; |
| 4967 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4968 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4969 | |
| 4970 | if ((ckchs = ckchs_lookup(path))) { |
| 4971 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4972 | 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] | 4973 | } |
| 4974 | if (stat(path, &buf) == 0) { |
| 4975 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4976 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 4977 | if (!ckchs) |
| 4978 | return ERR_ALERT | ERR_FATAL; |
| 4979 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4980 | 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] | 4981 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4982 | 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] | 4983 | } |
| 4984 | } else { |
| 4985 | /* stat failed, could be a bundle */ |
| 4986 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 4987 | /* try to load a bundle if it is permitted */ |
| 4988 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 4989 | if (!ckchs) |
| 4990 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4991 | 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] | 4992 | } else { |
| 4993 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4994 | err && *err ? *err : "", fp, strerror(errno)); |
| 4995 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | return cfgerr; |
| 5000 | } |
| 5001 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5002 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5003 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5004 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5005 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5006 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5007 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5008 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5009 | SSL_OP_NO_SSLv2 | |
| 5010 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 5011 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5012 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5013 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 5014 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5015 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5016 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5017 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5018 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5019 | SSL_MODE_RELEASE_BUFFERS | |
| 5020 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 5021 | 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] | 5022 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5023 | int flags = MC_SSL_O_ALL; |
| 5024 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5025 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5026 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5027 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5028 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5029 | 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] | 5030 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 5031 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5032 | 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] | 5033 | else |
| 5034 | flags = conf_ssl_methods->flags; |
| 5035 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 5036 | min = conf_ssl_methods->min; |
| 5037 | max = conf_ssl_methods->max; |
| 5038 | /* start with TLSv10 to remove SSLv3 per default */ |
| 5039 | if (!min && (!max || max >= CONF_TLSV10)) |
| 5040 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5041 | /* 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] | 5042 | if (min) |
| 5043 | flags |= (methodVersions[min].flag - 1); |
| 5044 | if (max) |
| 5045 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5046 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5047 | min = max = CONF_TLSV_NONE; |
| 5048 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5049 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5050 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5051 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5052 | if (min) { |
| 5053 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5054 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 5055 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5056 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 5057 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5058 | hole = 0; |
| 5059 | } |
| 5060 | max = i; |
| 5061 | } |
| 5062 | else { |
| 5063 | min = max = i; |
| 5064 | } |
| 5065 | } |
| 5066 | else { |
| 5067 | if (min) |
| 5068 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5069 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5070 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5071 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5072 | 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] | 5073 | cfgerr += 1; |
| 5074 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5075 | /* save real min/max in bind_conf */ |
| 5076 | conf_ssl_methods->min = min; |
| 5077 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5078 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5079 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5080 | /* 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] | 5081 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5082 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5083 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5084 | else |
| 5085 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5086 | if (flags & methodVersions[i].flag) |
| 5087 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5088 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5089 | /* 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] | 5090 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5091 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 5092 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5093 | |
| 5094 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 5095 | options |= SSL_OP_NO_TICKET; |
| 5096 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 5097 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 5098 | |
| 5099 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5100 | options |= SSL_OP_NO_RENEGOTIATION; |
| 5101 | #endif |
| 5102 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5103 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5104 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5105 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5106 | if (global_ssl.async) |
| 5107 | mode |= SSL_MODE_ASYNC; |
| 5108 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5109 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5110 | if (global_ssl.life_time) |
| 5111 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5112 | |
| 5113 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5114 | #ifdef OPENSSL_IS_BORINGSSL |
| 5115 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 5116 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 5117 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 5118 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 5119 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 5120 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 5121 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5122 | #else |
| 5123 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5124 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 5125 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5126 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5127 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5130 | |
| 5131 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 5132 | { |
| 5133 | if (first == block) { |
| 5134 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5135 | if (first->len > 0) |
| 5136 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 5137 | } |
| 5138 | } |
| 5139 | |
| 5140 | /* return first block from sh_ssl_sess */ |
| 5141 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 5142 | { |
| 5143 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 5144 | |
| 5145 | } |
| 5146 | |
| 5147 | /* store a session into the cache |
| 5148 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 5149 | * data: asn1 encoded session |
| 5150 | * data_len: asn1 encoded session length |
| 5151 | * Returns 1 id session was stored (else 0) |
| 5152 | */ |
| 5153 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 5154 | { |
| 5155 | struct shared_block *first; |
| 5156 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 5157 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5158 | 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] | 5159 | if (!first) { |
| 5160 | /* Could not retrieve enough free blocks to store that session */ |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
| 5164 | /* STORE the key in the first elem */ |
| 5165 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5166 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 5167 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5168 | |
| 5169 | /* it returns the already existing node |
| 5170 | or current node if none, never returns null */ |
| 5171 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 5172 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 5173 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 5174 | /* release the reserved row */ |
| 5175 | shctx_row_dec_hot(ssl_shctx, first); |
| 5176 | /* replace the previous session already in the tree */ |
| 5177 | sh_ssl_sess = oldsh_ssl_sess; |
| 5178 | /* ignore the previous session data, only use the header */ |
| 5179 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 5180 | shctx_row_inc_hot(ssl_shctx, first); |
| 5181 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5182 | } |
| 5183 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5184 | 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] | 5185 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5186 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5190 | |
| 5191 | return 1; |
| 5192 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5193 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5194 | /* SSL callback used when a new session is created while connecting to a server */ |
| 5195 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 5196 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5197 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5198 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5199 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5200 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5201 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5202 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 5203 | int len; |
| 5204 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5205 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5206 | len = i2d_SSL_SESSION(sess, NULL); |
| 5207 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 5208 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 5209 | } else { |
| 5210 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5211 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 5212 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 5213 | } |
| 5214 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 5215 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 5216 | &ptr); |
| 5217 | } |
| 5218 | } else { |
| 5219 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5220 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 5221 | } |
| 5222 | |
| 5223 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5224 | } |
| 5225 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5226 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5227 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5228 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5229 | { |
| 5230 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 5231 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 5232 | unsigned char *p; |
| 5233 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5234 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5235 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5236 | |
| 5237 | /* 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] | 5238 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5239 | * note: SSL_SESSION_set1_id is using |
| 5240 | * a memcpy so we need to use a different pointer |
| 5241 | * than sid_data or sid_ctx_data to avoid valgrind |
| 5242 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5243 | */ |
| 5244 | |
| 5245 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5246 | |
| 5247 | /* copy value in an other buffer */ |
| 5248 | memcpy(encid, sid_data, sid_length); |
| 5249 | |
| 5250 | /* pad with 0 */ |
| 5251 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 5252 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 5253 | |
| 5254 | /* force length to zero to avoid ASN1 encoding */ |
| 5255 | SSL_SESSION_set1_id(sess, encid, 0); |
| 5256 | |
| 5257 | /* force length to zero to avoid ASN1 encoding */ |
| 5258 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5259 | |
| 5260 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 5261 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 5262 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 5263 | goto err; |
| 5264 | |
| 5265 | p = encsess; |
| 5266 | |
| 5267 | /* process ASN1 session encoding before the lock */ |
| 5268 | i2d_SSL_SESSION(sess, &p); |
| 5269 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5270 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5271 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5272 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5273 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5274 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5275 | err: |
| 5276 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5277 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 5278 | 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] | 5279 | |
| 5280 | return 0; /* do not increment session reference count */ |
| 5281 | } |
| 5282 | |
| 5283 | /* 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] | 5284 | 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] | 5285 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5286 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5287 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 5288 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5289 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5290 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5291 | |
| 5292 | global.shctx_lookups++; |
| 5293 | |
| 5294 | /* allow the session to be freed automatically by openssl */ |
| 5295 | *do_copy = 0; |
| 5296 | |
| 5297 | /* tree key is zeros padded sessionid */ |
| 5298 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5299 | memcpy(tmpkey, key, key_len); |
| 5300 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 5301 | key = tmpkey; |
| 5302 | } |
| 5303 | |
| 5304 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5305 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5306 | |
| 5307 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5308 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 5309 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5310 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5311 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5312 | global.shctx_misses++; |
| 5313 | return NULL; |
| 5314 | } |
| 5315 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5316 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 5317 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5318 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5319 | 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] | 5320 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5321 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5322 | |
| 5323 | /* decode ASN1 session */ |
| 5324 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5325 | 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] | 5326 | /* Reset session id and session id contenxt */ |
| 5327 | if (sess) { |
| 5328 | SSL_SESSION_set1_id(sess, key, key_len); |
| 5329 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5330 | } |
| 5331 | |
| 5332 | return sess; |
| 5333 | } |
| 5334 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5335 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5336 | /* 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] | 5337 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5338 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5339 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5340 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 5341 | unsigned int sid_length; |
| 5342 | const unsigned char *sid_data; |
| 5343 | (void)ctx; |
| 5344 | |
| 5345 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 5346 | /* tree key is zeros padded sessionid */ |
| 5347 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5348 | memcpy(tmpkey, sid_data, sid_length); |
| 5349 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 5350 | sid_data = tmpkey; |
| 5351 | } |
| 5352 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5353 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5354 | |
| 5355 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5356 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 5357 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5358 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5359 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5363 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /* Set session cache mode to server and disable openssl internal cache. |
| 5367 | * Set shared cache callbacks on an ssl context. |
| 5368 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5369 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5370 | { |
| 5371 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5372 | |
| 5373 | if (!ssl_shctx) { |
| 5374 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 5375 | return; |
| 5376 | } |
| 5377 | |
| 5378 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 5379 | SSL_SESS_CACHE_NO_INTERNAL | |
| 5380 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 5381 | |
| 5382 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5383 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 5384 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 5385 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5386 | } |
| 5387 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5388 | /* |
| 5389 | * This function applies the SSL configuration on a SSL_CTX |
| 5390 | * It returns an error code and fills the <err> buffer |
| 5391 | */ |
| 5392 | 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] | 5393 | { |
| 5394 | struct proxy *curproxy = bind_conf->frontend; |
| 5395 | int cfgerr = 0; |
| 5396 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5397 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5398 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5399 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5400 | const char *conf_ciphersuites; |
| 5401 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5402 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5403 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5404 | if (ssl_conf) { |
| 5405 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5406 | int i, min, max; |
| 5407 | int flags = MC_SSL_O_ALL; |
| 5408 | |
| 5409 | /* 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] | 5410 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5411 | 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] | 5412 | if (min) |
| 5413 | flags |= (methodVersions[min].flag - 1); |
| 5414 | if (max) |
| 5415 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5416 | min = max = CONF_TLSV_NONE; |
| 5417 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5418 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5419 | if (min) |
| 5420 | max = i; |
| 5421 | else |
| 5422 | min = max = i; |
| 5423 | } |
| 5424 | /* save real min/max */ |
| 5425 | conf_ssl_methods->min = min; |
| 5426 | conf_ssl_methods->max = max; |
| 5427 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5428 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5429 | 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] | 5430 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5434 | 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] | 5435 | case SSL_SOCK_VERIFY_NONE: |
| 5436 | verify = SSL_VERIFY_NONE; |
| 5437 | break; |
| 5438 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5439 | verify = SSL_VERIFY_PEER; |
| 5440 | break; |
| 5441 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5442 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5443 | break; |
| 5444 | } |
| 5445 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5446 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5447 | 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] | 5448 | 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] | 5449 | 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] | 5450 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5451 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5452 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5453 | 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] | 5454 | 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] | 5455 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5456 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5457 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 5458 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 5459 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5460 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5461 | } |
| 5462 | 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] | 5463 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5464 | 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] | 5465 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5466 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5467 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5468 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5469 | 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] | 5470 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5471 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5472 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5473 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5474 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5475 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5476 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5477 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5478 | 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] | 5479 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5480 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5481 | else { |
| 5482 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5483 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5484 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5485 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5486 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5487 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5488 | #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] | 5489 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5490 | 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] | 5491 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5492 | 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] | 5493 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5494 | } |
| 5495 | } |
| 5496 | #endif |
| 5497 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5498 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5499 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5500 | if (conf_ciphers && |
| 5501 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5502 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5503 | 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] | 5504 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5505 | } |
| 5506 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5507 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5508 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5509 | if (conf_ciphersuites && |
| 5510 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5511 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5512 | 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] | 5513 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5514 | } |
| 5515 | #endif |
| 5516 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5517 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5518 | /* If tune.ssl.default-dh-param has not been set, |
| 5519 | neither has ssl-default-dh-file and no static DH |
| 5520 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5521 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5522 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 5523 | (ssl_dh_ptr_index == -1 || |
| 5524 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5525 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 5526 | const SSL_CIPHER * cipher = NULL; |
| 5527 | char cipher_description[128]; |
| 5528 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 5529 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 5530 | which is not ephemeral DH. */ |
| 5531 | const char dhe_description[] = " Kx=DH "; |
| 5532 | const char dhe_export_description[] = " Kx=DH("; |
| 5533 | int idx = 0; |
| 5534 | int dhe_found = 0; |
| 5535 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5536 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5537 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5538 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5539 | if (ssl) { |
| 5540 | ciphers = SSL_get_ciphers(ssl); |
| 5541 | |
| 5542 | if (ciphers) { |
| 5543 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 5544 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 5545 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 5546 | if (strstr(cipher_description, dhe_description) != NULL || |
| 5547 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 5548 | dhe_found = 1; |
| 5549 | break; |
| 5550 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 5551 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5552 | } |
| 5553 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5554 | SSL_free(ssl); |
| 5555 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5556 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5557 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5558 | if (dhe_found) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5559 | 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", |
| 5560 | err && *err ? *err : ""); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5561 | cfgerr |= ERR_WARN; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5562 | } |
| 5563 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5564 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5565 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5566 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5567 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5568 | if (local_dh_1024 == NULL) { |
| 5569 | local_dh_1024 = ssl_get_dh_1024(); |
| 5570 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5571 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5572 | if (local_dh_2048 == NULL) { |
| 5573 | local_dh_2048 = ssl_get_dh_2048(); |
| 5574 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5575 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5576 | if (local_dh_4096 == NULL) { |
| 5577 | local_dh_4096 = ssl_get_dh_4096(); |
| 5578 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | } |
| 5582 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5583 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5584 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5585 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5586 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5587 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5588 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5589 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5590 | ssl_conf_cur = NULL; |
| 5591 | if (ssl_conf && ssl_conf->npn_str) |
| 5592 | ssl_conf_cur = ssl_conf; |
| 5593 | else if (bind_conf->ssl_conf.npn_str) |
| 5594 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5595 | if (ssl_conf_cur) |
| 5596 | 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] | 5597 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5598 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5599 | ssl_conf_cur = NULL; |
| 5600 | if (ssl_conf && ssl_conf->alpn_str) |
| 5601 | ssl_conf_cur = ssl_conf; |
| 5602 | else if (bind_conf->ssl_conf.alpn_str) |
| 5603 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5604 | if (ssl_conf_cur) |
| 5605 | 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] | 5606 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 5607 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5608 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5609 | if (conf_curves) { |
| 5610 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5611 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5612 | 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] | 5613 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5614 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5615 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5616 | } |
| 5617 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5618 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5619 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5620 | int i; |
| 5621 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5622 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5623 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5624 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5625 | NULL); |
| 5626 | |
| 5627 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 5628 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5629 | return cfgerr; |
| 5630 | } |
| 5631 | #else |
| 5632 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5633 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5634 | ECDHE_DEFAULT_CURVE); |
| 5635 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5636 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5637 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5638 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5639 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5640 | 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] | 5641 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5642 | } |
| 5643 | else { |
| 5644 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5645 | EC_KEY_free(ecdh); |
| 5646 | } |
| 5647 | } |
| 5648 | #endif |
| 5649 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5650 | return cfgerr; |
| 5651 | } |
| 5652 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5653 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5654 | { |
| 5655 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5656 | size_t prefixlen, suffixlen; |
| 5657 | |
| 5658 | /* Trivial case */ |
| 5659 | if (strcmp(pattern, hostname) == 0) |
| 5660 | return 1; |
| 5661 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5662 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5663 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5664 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5665 | pattern_wildcard = NULL; |
| 5666 | pattern_left_label_end = pattern; |
| 5667 | while (*pattern_left_label_end != '.') { |
| 5668 | switch (*pattern_left_label_end) { |
| 5669 | case 0: |
| 5670 | /* End of label not found */ |
| 5671 | return 0; |
| 5672 | case '*': |
| 5673 | /* If there is more than one wildcards */ |
| 5674 | if (pattern_wildcard) |
| 5675 | return 0; |
| 5676 | pattern_wildcard = pattern_left_label_end; |
| 5677 | break; |
| 5678 | } |
| 5679 | pattern_left_label_end++; |
| 5680 | } |
| 5681 | |
| 5682 | /* If it's not trivial and there is no wildcard, it can't |
| 5683 | * match */ |
| 5684 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5685 | return 0; |
| 5686 | |
| 5687 | /* Make sure all labels match except the leftmost */ |
| 5688 | hostname_left_label_end = strchr(hostname, '.'); |
| 5689 | if (!hostname_left_label_end |
| 5690 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5691 | return 0; |
| 5692 | |
| 5693 | /* Make sure the leftmost label of the hostname is long enough |
| 5694 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5695 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5696 | return 0; |
| 5697 | |
| 5698 | /* Finally compare the string on either side of the |
| 5699 | * wildcard */ |
| 5700 | prefixlen = pattern_wildcard - pattern; |
| 5701 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5702 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5703 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5704 | return 0; |
| 5705 | |
| 5706 | return 1; |
| 5707 | } |
| 5708 | |
| 5709 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5710 | { |
| 5711 | SSL *ssl; |
| 5712 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5713 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5714 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5715 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5716 | |
| 5717 | int depth; |
| 5718 | X509 *cert; |
| 5719 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5720 | int i; |
| 5721 | X509_NAME *cert_subject; |
| 5722 | char *str; |
| 5723 | |
| 5724 | if (ok == 0) |
| 5725 | return ok; |
| 5726 | |
| 5727 | 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] | 5728 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5729 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5730 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5731 | /* We're checking if the provided hostnames match the desired one. The |
| 5732 | * desired hostname comes from the SNI we presented if any, or if not |
| 5733 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5734 | * directive. If neither is set, we don't care about the name so the |
| 5735 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5736 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5737 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5738 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5739 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5740 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5741 | if (!servername) |
| 5742 | return ok; |
| 5743 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5744 | |
| 5745 | /* We only need to verify the CN on the actual server cert, |
| 5746 | * not the indirect CAs */ |
| 5747 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5748 | if (depth != 0) |
| 5749 | return ok; |
| 5750 | |
| 5751 | /* At this point, the cert is *not* OK unless we can find a |
| 5752 | * hostname match */ |
| 5753 | ok = 0; |
| 5754 | |
| 5755 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5756 | /* It seems like this might happen if verify peer isn't set */ |
| 5757 | if (!cert) |
| 5758 | return ok; |
| 5759 | |
| 5760 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5761 | if (alt_names) { |
| 5762 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5763 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5764 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5765 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5766 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5767 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5768 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5769 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5770 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5771 | OPENSSL_free(str); |
| 5772 | } |
| 5773 | } |
| 5774 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5775 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | cert_subject = X509_get_subject_name(cert); |
| 5779 | i = -1; |
| 5780 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5781 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5782 | ASN1_STRING *value; |
| 5783 | value = X509_NAME_ENTRY_get_data(entry); |
| 5784 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5785 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5786 | OPENSSL_free(str); |
| 5787 | } |
| 5788 | } |
| 5789 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5790 | /* report the mismatch and indicate if SNI was used or not */ |
| 5791 | if (!ok && !conn->err_code) |
| 5792 | 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] | 5793 | return ok; |
| 5794 | } |
| 5795 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5796 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5797 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5798 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5799 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5800 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5801 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5802 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5803 | SSL_OP_NO_SSLv2 | |
| 5804 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5805 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5806 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5807 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5808 | SSL_MODE_RELEASE_BUFFERS | |
| 5809 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5810 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5811 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5812 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5813 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5814 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5815 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5816 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5817 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5818 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5819 | cfgerr++; |
| 5820 | } |
| 5821 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5822 | /* Automatic memory computations need to know we use SSL there */ |
| 5823 | global.ssl_used_backend = 1; |
| 5824 | |
| 5825 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5826 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5827 | 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] | 5828 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5829 | curproxy->id, srv->id, |
| 5830 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5831 | cfgerr++; |
| 5832 | return cfgerr; |
| 5833 | } |
| 5834 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5835 | if (srv->use_ssl) |
| 5836 | srv->xprt = &ssl_sock; |
| 5837 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 5838 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5839 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5840 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5841 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5842 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5843 | proxy_type_str(curproxy), curproxy->id, |
| 5844 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5845 | cfgerr++; |
| 5846 | return cfgerr; |
| 5847 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5848 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5849 | 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] | 5850 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5851 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5852 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5853 | else |
| 5854 | flags = conf_ssl_methods->flags; |
| 5855 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5856 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5857 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5858 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5859 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5860 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5861 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5862 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5863 | min = max = CONF_TLSV_NONE; |
| 5864 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5865 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5866 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5867 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5868 | if (min) { |
| 5869 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5870 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5871 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5872 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5873 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5874 | hole = 0; |
| 5875 | } |
| 5876 | max = i; |
| 5877 | } |
| 5878 | else { |
| 5879 | min = max = i; |
| 5880 | } |
| 5881 | } |
| 5882 | else { |
| 5883 | if (min) |
| 5884 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5885 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5886 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5887 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5888 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5889 | cfgerr += 1; |
| 5890 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5892 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5893 | /* 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] | 5894 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5895 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5896 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5897 | else |
| 5898 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5899 | if (flags & methodVersions[i].flag) |
| 5900 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5901 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5902 | /* 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] | 5903 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5904 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5905 | #endif |
| 5906 | |
| 5907 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5908 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5909 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5910 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5911 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5912 | if (global_ssl.async) |
| 5913 | mode |= SSL_MODE_ASYNC; |
| 5914 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5915 | SSL_CTX_set_mode(ctx, mode); |
| 5916 | srv->ssl_ctx.ctx = ctx; |
| 5917 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5918 | if (srv->ssl_ctx.client_crt) { |
| 5919 | 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] | 5920 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 5921 | proxy_type_str(curproxy), curproxy->id, |
| 5922 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5923 | cfgerr++; |
| 5924 | } |
| 5925 | 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] | 5926 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 5927 | proxy_type_str(curproxy), curproxy->id, |
| 5928 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5929 | cfgerr++; |
| 5930 | } |
| 5931 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5932 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 5933 | proxy_type_str(curproxy), curproxy->id, |
| 5934 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5935 | cfgerr++; |
| 5936 | } |
| 5937 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5938 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5939 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5940 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5941 | switch (srv->ssl_ctx.verify) { |
| 5942 | case SSL_SOCK_VERIFY_NONE: |
| 5943 | verify = SSL_VERIFY_NONE; |
| 5944 | break; |
| 5945 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5946 | verify = SSL_VERIFY_PEER; |
| 5947 | break; |
| 5948 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5949 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5950 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5951 | (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] | 5952 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5953 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5954 | /* set CAfile to verify */ |
| 5955 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 5956 | 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] | 5957 | curproxy->id, srv->id, |
| 5958 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5959 | cfgerr++; |
| 5960 | } |
| 5961 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5962 | else { |
| 5963 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5964 | 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", |
| 5965 | curproxy->id, srv->id, |
| 5966 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5967 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5968 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 5969 | curproxy->id, srv->id, |
| 5970 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5971 | cfgerr++; |
| 5972 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5973 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5974 | if (srv->ssl_ctx.crl_file) { |
| 5975 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 5976 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5977 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5978 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 5979 | curproxy->id, srv->id, |
| 5980 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5981 | cfgerr++; |
| 5982 | } |
| 5983 | else { |
| 5984 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5985 | } |
| 5986 | } |
| 5987 | #endif |
| 5988 | } |
| 5989 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5990 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 5991 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5992 | 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] | 5993 | if (srv->ssl_ctx.ciphers && |
| 5994 | !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] | 5995 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 5996 | curproxy->id, srv->id, |
| 5997 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5998 | cfgerr++; |
| 5999 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6000 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6001 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6002 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 6003 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6004 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 6005 | curproxy->id, srv->id, |
| 6006 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 6007 | cfgerr++; |
| 6008 | } |
| 6009 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6010 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 6011 | if (srv->ssl_ctx.npn_str) |
| 6012 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 6013 | #endif |
| 6014 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6015 | if (srv->ssl_ctx.alpn_str) |
| 6016 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 6017 | #endif |
| 6018 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6019 | |
| 6020 | return cfgerr; |
| 6021 | } |
| 6022 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6023 | /* 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] | 6024 | * be NULL, in which case nothing is done. Returns the number of errors |
| 6025 | * encountered. |
| 6026 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6027 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6028 | { |
| 6029 | struct ebmb_node *node; |
| 6030 | struct sni_ctx *sni; |
| 6031 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6032 | int errcode = 0; |
| 6033 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6034 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 6035 | /* Automatic memory computations need to know we use SSL there */ |
| 6036 | global.ssl_used_frontend = 1; |
| 6037 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6038 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 6039 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6040 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6041 | err++; |
| 6042 | } |
| 6043 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 6044 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 6045 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6046 | /* It should not be necessary to call this function, but it's |
| 6047 | necessary first to check and move all initialisation related |
| 6048 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6049 | 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] | 6050 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6051 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6052 | 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] | 6053 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6054 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6055 | while (node) { |
| 6056 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6057 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 6058 | /* only initialize the CTX on its first occurrence and |
| 6059 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6060 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6061 | node = ebmb_next(node); |
| 6062 | } |
| 6063 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6064 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6065 | while (node) { |
| 6066 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6067 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6068 | /* only initialize the CTX on its first occurrence and |
| 6069 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6070 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 6071 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6072 | node = ebmb_next(node); |
| 6073 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6074 | |
| 6075 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6076 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6077 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6078 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6079 | err++; |
| 6080 | } |
| 6081 | |
| 6082 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6083 | return err; |
| 6084 | } |
| 6085 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6086 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 6087 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 6088 | * alerts are directly emitted since the rest of the stack does it below. |
| 6089 | */ |
| 6090 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 6091 | { |
| 6092 | struct proxy *px = bind_conf->frontend; |
| 6093 | int alloc_ctx; |
| 6094 | int err; |
| 6095 | |
| 6096 | if (!bind_conf->is_ssl) { |
| 6097 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6098 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 6099 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6100 | } |
| 6101 | return 0; |
| 6102 | } |
| 6103 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6104 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6105 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 6106 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6107 | } |
| 6108 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6109 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 6110 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6111 | return -1; |
| 6112 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6113 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 6114 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6115 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 6116 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6117 | sizeof(*sh_ssl_sess_tree), |
| 6118 | ((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] | 6119 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6120 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 6121 | 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"); |
| 6122 | else |
| 6123 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 6124 | return -1; |
| 6125 | } |
| 6126 | /* free block callback */ |
| 6127 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 6128 | /* init the root tree within the extra space */ |
| 6129 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 6130 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6131 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6132 | err = 0; |
| 6133 | /* initialize all certificate contexts */ |
| 6134 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 6135 | |
| 6136 | /* initialize CA variables if the certificates generation is enabled */ |
| 6137 | err += ssl_sock_load_ca(bind_conf); |
| 6138 | |
| 6139 | return -err; |
| 6140 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6141 | |
| 6142 | /* release ssl context allocated for servers. */ |
| 6143 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 6144 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6145 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6146 | if (srv->ssl_ctx.alpn_str) |
| 6147 | free(srv->ssl_ctx.alpn_str); |
| 6148 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 6149 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6150 | if (srv->ssl_ctx.npn_str) |
| 6151 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 6152 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6153 | if (srv->ssl_ctx.ctx) |
| 6154 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 6155 | } |
| 6156 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6157 | /* 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] | 6158 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 6159 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6160 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6161 | { |
| 6162 | struct ebmb_node *node, *back; |
| 6163 | struct sni_ctx *sni; |
| 6164 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6165 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6166 | while (node) { |
| 6167 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6168 | back = ebmb_next(node); |
| 6169 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6170 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6171 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6172 | ssl_sock_free_ssl_conf(sni->conf); |
| 6173 | free(sni->conf); |
| 6174 | sni->conf = NULL; |
| 6175 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6176 | free(sni); |
| 6177 | node = back; |
| 6178 | } |
| 6179 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6180 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6181 | while (node) { |
| 6182 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6183 | back = ebmb_next(node); |
| 6184 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6185 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6186 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6187 | ssl_sock_free_ssl_conf(sni->conf); |
| 6188 | free(sni->conf); |
| 6189 | sni->conf = NULL; |
| 6190 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6191 | free(sni); |
| 6192 | node = back; |
| 6193 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6194 | SSL_CTX_free(bind_conf->initial_ctx); |
| 6195 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6196 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6197 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6198 | } |
| 6199 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6200 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 6201 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 6202 | { |
| 6203 | ssl_sock_free_ca(bind_conf); |
| 6204 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6205 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6206 | free(bind_conf->ca_sign_file); |
| 6207 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 6208 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6209 | free(bind_conf->keys_ref->filename); |
| 6210 | free(bind_conf->keys_ref->tlskeys); |
| 6211 | LIST_DEL(&bind_conf->keys_ref->list); |
| 6212 | free(bind_conf->keys_ref); |
| 6213 | } |
| 6214 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6215 | bind_conf->ca_sign_pass = NULL; |
| 6216 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6219 | /* Load CA cert file and private key used to generate certificates */ |
| 6220 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6221 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6222 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6223 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6224 | FILE *fp; |
| 6225 | X509 *cacert = NULL; |
| 6226 | EVP_PKEY *capkey = NULL; |
| 6227 | int err = 0; |
| 6228 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 6229 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6230 | return err; |
| 6231 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6232 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6233 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6234 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6235 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6236 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6237 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6238 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6239 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6240 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6241 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 6242 | "no CA certificate File configured at [%s:%d].\n", |
| 6243 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6244 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6245 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6246 | |
| 6247 | /* read in the CA certificate */ |
| 6248 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6249 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6250 | 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] | 6251 | goto load_error; |
| 6252 | } |
| 6253 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6254 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6255 | 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] | 6256 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6257 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6258 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6259 | 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] | 6260 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 6261 | 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] | 6262 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6263 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6264 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6265 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6266 | bind_conf->ca_sign_cert = cacert; |
| 6267 | bind_conf->ca_sign_pkey = capkey; |
| 6268 | return err; |
| 6269 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6270 | read_error: |
| 6271 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6272 | if (capkey) EVP_PKEY_free(capkey); |
| 6273 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6274 | load_error: |
| 6275 | bind_conf->generate_certs = 0; |
| 6276 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6277 | return err; |
| 6278 | } |
| 6279 | |
| 6280 | /* Release CA cert and private key used to generate certificated */ |
| 6281 | void |
| 6282 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 6283 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6284 | if (bind_conf->ca_sign_pkey) |
| 6285 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 6286 | if (bind_conf->ca_sign_cert) |
| 6287 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 6288 | bind_conf->ca_sign_pkey = NULL; |
| 6289 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6290 | } |
| 6291 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6292 | /* |
| 6293 | * This function is called if SSL * context is not yet allocated. The function |
| 6294 | * is designed to be called before any other data-layer operation and sets the |
| 6295 | * handshake flag on the connection. It is safe to call it multiple times. |
| 6296 | * It returns 0 on success and -1 in error case. |
| 6297 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6298 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6299 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6300 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6301 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6302 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6303 | return 0; |
| 6304 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6305 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6306 | return 0; |
| 6307 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6308 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6309 | if (!ctx) { |
| 6310 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6311 | return -1; |
| 6312 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6313 | ctx->wait_event.tasklet = tasklet_new(); |
| 6314 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6315 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6316 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6317 | return -1; |
| 6318 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6319 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6320 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6321 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6322 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6323 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6324 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6325 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6326 | ctx->xprt_st = 0; |
| 6327 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6328 | |
| 6329 | /* Only work with sockets for now, this should be adapted when we'll |
| 6330 | * add QUIC support. |
| 6331 | */ |
| 6332 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6333 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6334 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6335 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6336 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6337 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6338 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 6339 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6340 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6341 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6342 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6343 | /* If it is in client mode initiate SSL session |
| 6344 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6345 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6346 | int may_retry = 1; |
| 6347 | |
| 6348 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6349 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6350 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 6351 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6352 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6353 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6354 | goto retry_connect; |
| 6355 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6356 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6357 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6358 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6359 | ctx->bio = BIO_new(ha_meth); |
| 6360 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6361 | SSL_free(ctx->ssl); |
| 6362 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6363 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6364 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6365 | goto retry_connect; |
| 6366 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6367 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6368 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6369 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6370 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6371 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6372 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6373 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6374 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6375 | SSL_free(ctx->ssl); |
| 6376 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6377 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6378 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6379 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6380 | goto retry_connect; |
| 6381 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6382 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6383 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6384 | } |
| 6385 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6386 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6387 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6388 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6389 | 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] | 6390 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6391 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6392 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6393 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6394 | } else if (sess) { |
| 6395 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6396 | } |
| 6397 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6398 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6399 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6400 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6401 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6402 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6403 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6404 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6405 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6406 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6407 | return 0; |
| 6408 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6409 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6410 | int may_retry = 1; |
| 6411 | |
| 6412 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6413 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6414 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 6415 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6416 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6417 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6418 | goto retry_accept; |
| 6419 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6420 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6421 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6422 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6423 | ctx->bio = BIO_new(ha_meth); |
| 6424 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6425 | SSL_free(ctx->ssl); |
| 6426 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6427 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6428 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6429 | goto retry_accept; |
| 6430 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6431 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6432 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6433 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6434 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6435 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6436 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6437 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6438 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6439 | SSL_free(ctx->ssl); |
| 6440 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6441 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6442 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6443 | goto retry_accept; |
| 6444 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6445 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6446 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6447 | } |
| 6448 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6449 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6450 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 6451 | b_alloc(&ctx->early_buf); |
| 6452 | SSL_set_max_early_data(ctx->ssl, |
| 6453 | /* Only allow early data if we managed to allocate |
| 6454 | * a buffer. |
| 6455 | */ |
| 6456 | (!b_is_null(&ctx->early_buf)) ? |
| 6457 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6458 | } |
| 6459 | #endif |
| 6460 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6461 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6462 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6463 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6464 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6465 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6466 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 6467 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6468 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6469 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6470 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6471 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6472 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6473 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6474 | return 0; |
| 6475 | } |
| 6476 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6477 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6478 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6479 | if (ctx && ctx->wait_event.tasklet) |
| 6480 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6481 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6482 | return -1; |
| 6483 | } |
| 6484 | |
| 6485 | |
| 6486 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6487 | * updates the FD status if it wants some polling before being called again. |
| 6488 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6489 | * otherwise it returns non-zero and removes itself from the connection's |
| 6490 | * flags (the bit is provided in <flag> by the caller). |
| 6491 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6492 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6493 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6494 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6495 | int ret; |
| 6496 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6497 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6498 | return 0; |
| 6499 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6500 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6501 | goto out_error; |
| 6502 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6503 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6504 | /* |
| 6505 | * Check if we have early data. If we do, we have to read them |
| 6506 | * before SSL_do_handshake() is called, And there's no way to |
| 6507 | * detect early data, except to try to read them |
| 6508 | */ |
| 6509 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6510 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6511 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6512 | while (1) { |
| 6513 | ret = SSL_read_early_data(ctx->ssl, |
| 6514 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6515 | &read_data); |
| 6516 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6517 | goto check_error; |
| 6518 | if (read_data > 0) { |
| 6519 | conn->flags |= CO_FL_EARLY_DATA; |
| 6520 | b_add(&ctx->early_buf, read_data); |
| 6521 | } |
| 6522 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6523 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6524 | if (!b_data(&ctx->early_buf)) |
| 6525 | b_free(&ctx->early_buf); |
| 6526 | break; |
| 6527 | } |
| 6528 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6529 | } |
| 6530 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6531 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6532 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6533 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6534 | * the reneg handshake. |
| 6535 | * Here we use SSL_peek as a workaround for reneg. |
| 6536 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6537 | 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] | 6538 | char c; |
| 6539 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6540 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6541 | if (ret <= 0) { |
| 6542 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6543 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6544 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6545 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6546 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6547 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6548 | 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] | 6549 | return 0; |
| 6550 | } |
| 6551 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6552 | /* handshake may have been completed but we have |
| 6553 | * no more data to read. |
| 6554 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6555 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6556 | ret = 1; |
| 6557 | goto reneg_ok; |
| 6558 | } |
| 6559 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6560 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6561 | 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] | 6562 | return 0; |
| 6563 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6564 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6565 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6566 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6567 | return 0; |
| 6568 | } |
| 6569 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6570 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6571 | /* if errno is null, then connection was successfully established */ |
| 6572 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6573 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6574 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6575 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6576 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6577 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6578 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6579 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6580 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6581 | /* 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] | 6582 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6583 | empty_handshake = state == TLS_ST_BEFORE; |
| 6584 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6585 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6586 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6587 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6588 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6589 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6590 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6591 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6592 | else |
| 6593 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6594 | } |
| 6595 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6596 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6597 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6598 | else |
| 6599 | conn->err_code = CO_ER_SSL_ABORT; |
| 6600 | } |
| 6601 | } |
| 6602 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6603 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6604 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6605 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6606 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6607 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6608 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6609 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6610 | goto out_error; |
| 6611 | } |
| 6612 | else { |
| 6613 | /* Fail on all other handshake errors */ |
| 6614 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6615 | * buffer, causing an RST to be emitted upon close() on |
| 6616 | * TCP sockets. We first try to drain possibly pending |
| 6617 | * data to avoid this as much as possible. |
| 6618 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6619 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6620 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6621 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6622 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6623 | goto out_error; |
| 6624 | } |
| 6625 | } |
| 6626 | /* read some data: consider handshake completed */ |
| 6627 | goto reneg_ok; |
| 6628 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6629 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6630 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6631 | if (ret != 1) { |
| 6632 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6633 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6634 | |
| 6635 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6636 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6637 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6638 | 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] | 6639 | return 0; |
| 6640 | } |
| 6641 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6642 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6643 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6644 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6645 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6646 | return 0; |
| 6647 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6648 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6649 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6650 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6651 | return 0; |
| 6652 | } |
| 6653 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6654 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6655 | /* if errno is null, then connection was successfully established */ |
| 6656 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6657 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6658 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6659 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6660 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6661 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6662 | #else |
| 6663 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6664 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6665 | /* 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] | 6666 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6667 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6668 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6669 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6670 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6671 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6672 | if (empty_handshake) { |
| 6673 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6674 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6675 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6676 | else |
| 6677 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6678 | } |
| 6679 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6680 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6681 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6682 | else |
| 6683 | conn->err_code = CO_ER_SSL_ABORT; |
| 6684 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6685 | } |
| 6686 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6687 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6688 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6689 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6690 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6691 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6692 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6693 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6694 | goto out_error; |
| 6695 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6696 | else { |
| 6697 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6698 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6699 | * buffer, causing an RST to be emitted upon close() on |
| 6700 | * TCP sockets. We first try to drain possibly pending |
| 6701 | * data to avoid this as much as possible. |
| 6702 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6703 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6704 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6705 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6706 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6707 | goto out_error; |
| 6708 | } |
| 6709 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6710 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6711 | else { |
| 6712 | /* |
| 6713 | * If the server refused the early data, we have to send a |
| 6714 | * 425 to the client, as we no longer have the data to sent |
| 6715 | * them again. |
| 6716 | */ |
| 6717 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6718 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6719 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6720 | goto out_error; |
| 6721 | } |
| 6722 | } |
| 6723 | } |
| 6724 | #endif |
| 6725 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6726 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6727 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6728 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6729 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6730 | /* ASYNC engine API doesn't support moving read/write |
| 6731 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6732 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6733 | */ |
| 6734 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6735 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6736 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6737 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6738 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6739 | if (objt_server(conn->target)) { |
| 6740 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6741 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6742 | 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] | 6743 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6744 | else { |
| 6745 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6746 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6747 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6748 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6749 | } |
| 6750 | |
| 6751 | /* The connection is now established at both layers, it's time to leave */ |
| 6752 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6753 | return 1; |
| 6754 | |
| 6755 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6756 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6757 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6758 | ERR_clear_error(); |
| 6759 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6760 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6761 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6762 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6763 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6764 | } |
| 6765 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6766 | /* Fail on all other handshake errors */ |
| 6767 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6768 | if (!conn->err_code) |
| 6769 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6770 | return 0; |
| 6771 | } |
| 6772 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6773 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6774 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6775 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6776 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6777 | * unless the transport layer was already released. |
| 6778 | */ |
| 6779 | 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] | 6780 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6781 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6782 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6783 | if (!ctx) |
| 6784 | return -1; |
| 6785 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6786 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6787 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6788 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6789 | ctx->subs = es; |
| 6790 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6791 | |
| 6792 | /* we may have to subscribe to lower layers for new events */ |
| 6793 | event_type &= ~ctx->wait_event.events; |
| 6794 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6795 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6796 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6797 | } |
| 6798 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6799 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6800 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6801 | * subscribe() call. It always returns zero. |
| 6802 | */ |
| 6803 | 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] | 6804 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6805 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6806 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6807 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6808 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6809 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6810 | es->events &= ~event_type; |
| 6811 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6812 | ctx->subs = NULL; |
| 6813 | |
| 6814 | /* If we subscribed, and we're not doing the handshake, |
| 6815 | * then we subscribed because the upper layer asked for it, |
| 6816 | * as the upper layer is no longer interested, we can |
| 6817 | * unsubscribe too. |
| 6818 | */ |
| 6819 | event_type &= ctx->wait_event.events; |
| 6820 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6821 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6822 | |
| 6823 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6824 | } |
| 6825 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6826 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6827 | * Returns 0 on success, and non-zero on failure. |
| 6828 | */ |
| 6829 | 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) |
| 6830 | { |
| 6831 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6832 | |
| 6833 | if (oldxprt_ops != NULL) |
| 6834 | *oldxprt_ops = ctx->xprt; |
| 6835 | if (oldxprt_ctx != NULL) |
| 6836 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6837 | ctx->xprt = toadd_ops; |
| 6838 | ctx->xprt_ctx = toadd_ctx; |
| 6839 | return 0; |
| 6840 | } |
| 6841 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6842 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6843 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6844 | * XPRT. |
| 6845 | */ |
| 6846 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6847 | { |
| 6848 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6849 | |
| 6850 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6851 | ctx->xprt_ctx = newctx; |
| 6852 | ctx->xprt = newops; |
| 6853 | return 0; |
| 6854 | } |
| 6855 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6856 | } |
| 6857 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6858 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6859 | { |
| 6860 | struct ssl_sock_ctx *ctx = context; |
| 6861 | |
| 6862 | /* First if we're doing an handshake, try that */ |
| 6863 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6864 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6865 | /* If we had an error, or the handshake is done and I/O is available, |
| 6866 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6867 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6868 | * we can't be sure conn_fd_handler() will be called again. |
| 6869 | */ |
| 6870 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6871 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6872 | int ret = 0; |
| 6873 | int woke = 0; |
| 6874 | |
| 6875 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6876 | if (ctx->subs) { |
| 6877 | tasklet_wakeup(ctx->subs->tasklet); |
| 6878 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6879 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6880 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6881 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6882 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6883 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6884 | * upper layers know. If we have no mux, create it, |
| 6885 | * and once we have a mux, call its wake method if we didn't |
| 6886 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6887 | */ |
| 6888 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6889 | if (!ctx->conn->mux) |
| 6890 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6891 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6892 | ctx->conn->mux->wake(ctx->conn); |
| 6893 | return NULL; |
| 6894 | } |
| 6895 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6896 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6897 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6898 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6899 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6900 | tasklet_wakeup(ctx->subs->tasklet); |
| 6901 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6902 | if (!ctx->subs->events) |
| 6903 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6904 | } |
| 6905 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6906 | return NULL; |
| 6907 | } |
| 6908 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6909 | /* 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] | 6910 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6911 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6912 | * flags are updated with whatever special event is detected (error, read0, |
| 6913 | * empty). The caller is responsible for taking care of those events and |
| 6914 | * avoiding the call if inappropriate. The function does not call the |
| 6915 | * connection's polling update function, so the caller is responsible for this. |
| 6916 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6917 | 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] | 6918 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6919 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6920 | ssize_t ret; |
| 6921 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6922 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6923 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6924 | goto out_error; |
| 6925 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6926 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6927 | if (b_data(&ctx->early_buf)) { |
| 6928 | try = b_contig_space(buf); |
| 6929 | if (try > b_data(&ctx->early_buf)) |
| 6930 | try = b_data(&ctx->early_buf); |
| 6931 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6932 | b_add(buf, try); |
| 6933 | b_del(&ctx->early_buf, try); |
| 6934 | if (b_data(&ctx->early_buf) == 0) |
| 6935 | b_free(&ctx->early_buf); |
| 6936 | return try; |
| 6937 | } |
| 6938 | #endif |
| 6939 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6940 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6941 | /* a handshake was requested */ |
| 6942 | return 0; |
| 6943 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6944 | /* read the largest possible block. For this, we perform only one call |
| 6945 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6946 | * in which case we accept to do it once again. A new attempt is made on |
| 6947 | * EINTR too. |
| 6948 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6949 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6950 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6951 | try = b_contig_space(buf); |
| 6952 | if (!try) |
| 6953 | break; |
| 6954 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6955 | if (try > count) |
| 6956 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6957 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6958 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6959 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6960 | if (conn->flags & CO_FL_ERROR) { |
| 6961 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6962 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6963 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6964 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6965 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6966 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6967 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6968 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6969 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6970 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6971 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6972 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6973 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6974 | 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] | 6975 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6976 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6977 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6978 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6979 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6980 | break; |
| 6981 | } |
| 6982 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6983 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6984 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6985 | SUB_RETRY_RECV, |
| 6986 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6987 | /* handshake is running, and it may need to re-enable read */ |
| 6988 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6989 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6990 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6991 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6992 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6993 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6994 | break; |
| 6995 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6996 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6997 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6998 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6999 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 7000 | * stack before shutting down the connection for |
| 7001 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7002 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 7003 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7004 | /* otherwise it's a real error */ |
| 7005 | goto out_error; |
| 7006 | } |
| 7007 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7008 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7009 | return done; |
| 7010 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7011 | clear_ssl_error: |
| 7012 | /* Clear openssl global errors stack */ |
| 7013 | ssl_sock_dump_errors(conn); |
| 7014 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7015 | read0: |
| 7016 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7017 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7018 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7019 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7020 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7021 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7022 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7023 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7024 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7025 | } |
| 7026 | |
| 7027 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7028 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 7029 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 7030 | * 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] | 7031 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 7032 | * a second call may be performed. The connection's flags are updated with |
| 7033 | * whatever special event is detected (error, empty). The caller is responsible |
| 7034 | * for taking care of those events and avoiding the call if inappropriate. The |
| 7035 | * 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] | 7036 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 7037 | * caller to take care of this. It's up to the caller to update the buffer's |
| 7038 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7039 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7040 | 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] | 7041 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7042 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7043 | ssize_t ret; |
| 7044 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7045 | |
| 7046 | done = 0; |
| 7047 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7048 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7049 | goto out_error; |
| 7050 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7051 | 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] | 7052 | /* a handshake was requested */ |
| 7053 | return 0; |
| 7054 | |
| 7055 | /* send the largest possible block. For this we perform only one call |
| 7056 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 7057 | * in which case we accept to do it once again. |
| 7058 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7059 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7060 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7061 | size_t written_data; |
| 7062 | #endif |
| 7063 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7064 | try = b_contig_data(buf, done); |
| 7065 | if (try > count) |
| 7066 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7067 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 7068 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7069 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7070 | global_ssl.max_record && try > global_ssl.max_record) { |
| 7071 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7072 | } |
| 7073 | else { |
| 7074 | /* we need to keep the information about the fact that |
| 7075 | * we're not limiting the upcoming send(), because if it |
| 7076 | * fails, we'll have to retry with at least as many data. |
| 7077 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7078 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7079 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7080 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7081 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7082 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7083 | unsigned int max_early; |
| 7084 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7085 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7086 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7087 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7088 | if (SSL_get0_session(ctx->ssl)) |
| 7089 | 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] | 7090 | else |
| 7091 | max_early = 0; |
| 7092 | } |
| 7093 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7094 | if (try + ctx->sent_early_data > max_early) { |
| 7095 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7096 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7097 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7098 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7099 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7100 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7101 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7102 | 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] | 7103 | if (ret == 1) { |
| 7104 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7105 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7106 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7107 | 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] | 7108 | /* Initiate the handshake, now */ |
| 7109 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 7110 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7111 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7112 | } |
| 7113 | |
| 7114 | } else |
| 7115 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7116 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7117 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7118 | if (conn->flags & CO_FL_ERROR) { |
| 7119 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7120 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7121 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7122 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 7123 | /* A send succeeded, so we can consider ourself connected */ |
| 7124 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7125 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7126 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7127 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7128 | } |
| 7129 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7130 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7131 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7132 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7133 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7134 | /* handshake is running, and it may need to re-enable write */ |
| 7135 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7136 | 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] | 7137 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7138 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7139 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7140 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7141 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7142 | break; |
| 7143 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7144 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7145 | break; |
| 7146 | } |
| 7147 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7148 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7149 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7150 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7151 | SUB_RETRY_RECV, |
| 7152 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7153 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7154 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7155 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7156 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7157 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7158 | break; |
| 7159 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7160 | goto out_error; |
| 7161 | } |
| 7162 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7163 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7164 | return done; |
| 7165 | |
| 7166 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7167 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7168 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7169 | ERR_clear_error(); |
| 7170 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7171 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7172 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7173 | } |
| 7174 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7175 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7176 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7177 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7178 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7179 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7180 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7181 | if (ctx->wait_event.events != 0) |
| 7182 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 7183 | ctx->wait_event.events, |
| 7184 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7185 | if (ctx->subs) { |
| 7186 | ctx->subs->events = 0; |
| 7187 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7188 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7189 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 7190 | if (ctx->xprt->close) |
| 7191 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7192 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7193 | if (global_ssl.async) { |
| 7194 | OSSL_ASYNC_FD all_fd[32], afd; |
| 7195 | size_t num_all_fds = 0; |
| 7196 | int i; |
| 7197 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7198 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7199 | if (num_all_fds > 32) { |
| 7200 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 7201 | return; |
| 7202 | } |
| 7203 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7204 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7205 | |
| 7206 | /* If an async job is pending, we must try to |
| 7207 | to catch the end using polling before calling |
| 7208 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7209 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7210 | for (i=0 ; i < num_all_fds ; i++) { |
| 7211 | /* switch on an handler designed to |
| 7212 | * handle the SSL_free |
| 7213 | */ |
| 7214 | afd = all_fd[i]; |
| 7215 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7216 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7217 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 7218 | /* To ensure that the fd cache won't be used |
| 7219 | * and we'll catch a real RD event. |
| 7220 | */ |
| 7221 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7222 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7223 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7224 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7225 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7226 | return; |
| 7227 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7228 | /* Else we can remove the fds from the fdtab |
| 7229 | * and call SSL_free. |
| 7230 | * note: we do a fd_remove and not a delete |
| 7231 | * because the fd is owned by the engine. |
| 7232 | * the engine is responsible to close |
| 7233 | */ |
| 7234 | for (i=0 ; i < num_all_fds ; i++) |
| 7235 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7236 | } |
| 7237 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7238 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7239 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7240 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7241 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7242 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7243 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 7247 | * any case, flags the connection as reusable if no handshake was in progress. |
| 7248 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7249 | 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] | 7250 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7251 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7252 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7253 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7254 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 7255 | if (!clean) |
| 7256 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7257 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7258 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7259 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7260 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7261 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7262 | ERR_clear_error(); |
| 7263 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7264 | } |
| 7265 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7266 | /* fill a buffer with the algorithm and size of a public key */ |
| 7267 | static int cert_get_pkey_algo(X509 *crt, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7268 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7269 | int bits = 0; |
| 7270 | int sig = TLSEXT_signature_anonymous; |
| 7271 | int len = -1; |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7272 | EVP_PKEY *pkey; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7273 | |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7274 | pkey = X509_get_pubkey(crt); |
| 7275 | if (pkey) { |
| 7276 | bits = EVP_PKEY_bits(pkey); |
| 7277 | switch(EVP_PKEY_base_id(pkey)) { |
| 7278 | case EVP_PKEY_RSA: |
| 7279 | sig = TLSEXT_signature_rsa; |
| 7280 | break; |
| 7281 | case EVP_PKEY_EC: |
| 7282 | sig = TLSEXT_signature_ecdsa; |
| 7283 | break; |
| 7284 | case EVP_PKEY_DSA: |
| 7285 | sig = TLSEXT_signature_dsa; |
| 7286 | break; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7287 | } |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7288 | EVP_PKEY_free(pkey); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | switch(sig) { |
| 7292 | case TLSEXT_signature_rsa: |
| 7293 | len = chunk_printf(out, "RSA%d", bits); |
| 7294 | break; |
| 7295 | case TLSEXT_signature_ecdsa: |
| 7296 | len = chunk_printf(out, "EC%d", bits); |
| 7297 | break; |
| 7298 | case TLSEXT_signature_dsa: |
| 7299 | len = chunk_printf(out, "DSA%d", bits); |
| 7300 | break; |
| 7301 | default: |
| 7302 | return 0; |
| 7303 | } |
| 7304 | if (len < 0) |
| 7305 | return 0; |
| 7306 | return 1; |
| 7307 | } |
| 7308 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 7309 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7310 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7311 | { |
| 7312 | struct ssl_sock_ctx *ctx; |
| 7313 | X509 *crt; |
| 7314 | |
| 7315 | if (!ssl_sock_is_ssl(conn)) |
| 7316 | return 0; |
| 7317 | |
| 7318 | ctx = conn->xprt_ctx; |
| 7319 | |
| 7320 | crt = SSL_get_certificate(ctx->ssl); |
| 7321 | if (!crt) |
| 7322 | return 0; |
| 7323 | |
| 7324 | return cert_get_pkey_algo(crt, out); |
| 7325 | } |
| 7326 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7327 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7328 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7329 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7330 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7331 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7332 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7333 | X509 *crt; |
| 7334 | |
| 7335 | if (!ssl_sock_is_ssl(conn)) |
| 7336 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7337 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7338 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7339 | if (!crt) |
| 7340 | return NULL; |
| 7341 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7342 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7343 | } |
| 7344 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7345 | /* used for ppv2 authority */ |
| 7346 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7347 | { |
| 7348 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7349 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7350 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7351 | if (!ssl_sock_is_ssl(conn)) |
| 7352 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7353 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7354 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7355 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7356 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7357 | #endif |
| 7358 | } |
| 7359 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7360 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7361 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7362 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7363 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7364 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7365 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7366 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7367 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7368 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7369 | } |
| 7370 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7371 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7372 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7373 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7374 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7375 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7376 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7377 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7378 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7379 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7380 | } |
| 7381 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7382 | /* Extract a serial from a cert, and copy it to a chunk. |
| 7383 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 7384 | * -1 if output is not large enough. |
| 7385 | */ |
| 7386 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7387 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7388 | { |
| 7389 | ASN1_INTEGER *serial; |
| 7390 | |
| 7391 | serial = X509_get_serialNumber(crt); |
| 7392 | if (!serial) |
| 7393 | return 0; |
| 7394 | |
| 7395 | if (out->size < serial->length) |
| 7396 | return -1; |
| 7397 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7398 | memcpy(out->area, serial->data, serial->length); |
| 7399 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7400 | return 1; |
| 7401 | } |
| 7402 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7403 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7404 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 7405 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7406 | */ |
| 7407 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7408 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7409 | { |
| 7410 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7411 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7412 | |
| 7413 | len =i2d_X509(crt, NULL); |
| 7414 | if (len <= 0) |
| 7415 | return 1; |
| 7416 | |
| 7417 | if (out->size < len) |
| 7418 | return -1; |
| 7419 | |
| 7420 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7421 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7422 | return 1; |
| 7423 | } |
| 7424 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7425 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7426 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7427 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 7428 | * and -1 if output is not large enough. |
| 7429 | */ |
| 7430 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7431 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7432 | { |
| 7433 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 7434 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 7435 | |
| 7436 | if (gentm->length < 12) |
| 7437 | return 0; |
| 7438 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 7439 | return 0; |
| 7440 | if (out->size < gentm->length-2) |
| 7441 | return -1; |
| 7442 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7443 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 7444 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7445 | return 1; |
| 7446 | } |
| 7447 | else if (tm->type == V_ASN1_UTCTIME) { |
| 7448 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 7449 | |
| 7450 | if (utctm->length < 10) |
| 7451 | return 0; |
| 7452 | if (utctm->data[0] >= 0x35) |
| 7453 | return 0; |
| 7454 | if (out->size < utctm->length) |
| 7455 | return -1; |
| 7456 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7457 | memcpy(out->area, utctm->data, utctm->length); |
| 7458 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7459 | return 1; |
| 7460 | } |
| 7461 | |
| 7462 | return 0; |
| 7463 | } |
| 7464 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7465 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 7466 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 7467 | */ |
| 7468 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7469 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 7470 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7471 | { |
| 7472 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7473 | ASN1_OBJECT *obj; |
| 7474 | ASN1_STRING *data; |
| 7475 | const unsigned char *data_ptr; |
| 7476 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7477 | int i, j, n; |
| 7478 | int cur = 0; |
| 7479 | const char *s; |
| 7480 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7481 | int name_count; |
| 7482 | |
| 7483 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7484 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7485 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7486 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7487 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7488 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7489 | else |
| 7490 | j = i; |
| 7491 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7492 | ne = X509_NAME_get_entry(a, j); |
| 7493 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7494 | data = X509_NAME_ENTRY_get_data(ne); |
| 7495 | data_ptr = ASN1_STRING_get0_data(data); |
| 7496 | data_len = ASN1_STRING_length(data); |
| 7497 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7498 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7499 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7500 | s = tmp; |
| 7501 | } |
| 7502 | |
| 7503 | if (chunk_strcasecmp(entry, s) != 0) |
| 7504 | continue; |
| 7505 | |
| 7506 | if (pos < 0) |
| 7507 | cur--; |
| 7508 | else |
| 7509 | cur++; |
| 7510 | |
| 7511 | if (cur != pos) |
| 7512 | continue; |
| 7513 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7514 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7515 | return -1; |
| 7516 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7517 | memcpy(out->area, data_ptr, data_len); |
| 7518 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7519 | return 1; |
| 7520 | } |
| 7521 | |
| 7522 | return 0; |
| 7523 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7524 | } |
| 7525 | |
| 7526 | /* |
| 7527 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 7528 | * Return 0; |
| 7529 | */ |
| 7530 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7531 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 7532 | { |
| 7533 | int i; |
| 7534 | char *str; |
| 7535 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 7536 | |
| 7537 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 7538 | if (names) { |
| 7539 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 7540 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 7541 | if (i > 0) |
| 7542 | chunk_appendf(out, ", "); |
| 7543 | if (name->type == GEN_DNS) { |
| 7544 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 7545 | chunk_appendf(out, "DNS:%s", str); |
| 7546 | OPENSSL_free(str); |
| 7547 | } |
| 7548 | } |
| 7549 | } |
| 7550 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 7551 | } |
| 7552 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7553 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7554 | #endif |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7555 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7556 | /* |
| 7557 | * Extract the DN in the specified format from the X509_NAME and copy result to a chunk. |
| 7558 | * Currently supports rfc2253 for returning LDAP V3 DNs. |
| 7559 | * Returns 1 if dn entries exist, 0 if no dn entry was found. |
| 7560 | */ |
| 7561 | static int |
| 7562 | ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out) |
| 7563 | { |
| 7564 | BIO *bio = NULL; |
| 7565 | int ret = 0; |
| 7566 | int data_len = 0; |
| 7567 | |
| 7568 | if (chunk_strcmp(format, "rfc2253") == 0) { |
| 7569 | bio = BIO_new(BIO_s_mem()); |
| 7570 | if (bio == NULL) |
| 7571 | goto out; |
| 7572 | |
| 7573 | if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0) |
| 7574 | goto out; |
| 7575 | |
| 7576 | if ((data_len = BIO_read(bio, out->area, out->size)) <= 0) |
| 7577 | goto out; |
| 7578 | |
| 7579 | out->data = data_len; |
| 7580 | |
| 7581 | ret = 1; |
| 7582 | } |
| 7583 | out: |
| 7584 | if (bio) |
| 7585 | BIO_free(bio); |
| 7586 | return ret; |
| 7587 | } |
| 7588 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7589 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 7590 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 7591 | */ |
| 7592 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7593 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7594 | { |
| 7595 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7596 | ASN1_OBJECT *obj; |
| 7597 | ASN1_STRING *data; |
| 7598 | const unsigned char *data_ptr; |
| 7599 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7600 | int i, n, ln; |
| 7601 | int l = 0; |
| 7602 | const char *s; |
| 7603 | char *p; |
| 7604 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7605 | int name_count; |
| 7606 | |
| 7607 | |
| 7608 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7609 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7610 | out->data = 0; |
| 7611 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7612 | for (i = 0; i < name_count; i++) { |
| 7613 | ne = X509_NAME_get_entry(a, i); |
| 7614 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7615 | data = X509_NAME_ENTRY_get_data(ne); |
| 7616 | data_ptr = ASN1_STRING_get0_data(data); |
| 7617 | data_len = ASN1_STRING_length(data); |
| 7618 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7619 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7620 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7621 | s = tmp; |
| 7622 | } |
| 7623 | ln = strlen(s); |
| 7624 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7625 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7626 | if (l > out->size) |
| 7627 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7628 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7629 | |
| 7630 | *(p++)='/'; |
| 7631 | memcpy(p, s, ln); |
| 7632 | p += ln; |
| 7633 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7634 | memcpy(p, data_ptr, data_len); |
| 7635 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7636 | } |
| 7637 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7638 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7639 | return 0; |
| 7640 | |
| 7641 | return 1; |
| 7642 | } |
| 7643 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7644 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7645 | { |
| 7646 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7647 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7648 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7649 | if (!ssl_sock_is_ssl(conn)) |
| 7650 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7651 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7652 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7653 | #endif |
| 7654 | } |
| 7655 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7656 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7657 | * to disable SNI. |
| 7658 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7659 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7660 | { |
| 7661 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7662 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7663 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7664 | char *prev_name; |
| 7665 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7666 | if (!ssl_sock_is_ssl(conn)) |
| 7667 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7668 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7669 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7670 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7671 | * new connection will present a new SNI. As an optimization we could |
| 7672 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7673 | * server. |
| 7674 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7675 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7676 | if ((!prev_name && hostname) || |
| 7677 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7678 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7679 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7680 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7681 | #endif |
| 7682 | } |
| 7683 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7684 | /* Extract peer certificate's common name into the chunk dest |
| 7685 | * Returns |
| 7686 | * the len of the extracted common name |
| 7687 | * or 0 if no CN found in DN |
| 7688 | * or -1 on error case (i.e. no peer certificate) |
| 7689 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7690 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7691 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7692 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7693 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7694 | X509 *crt = NULL; |
| 7695 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7696 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7697 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7698 | .area = (char *)&find_cn, |
| 7699 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7700 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7701 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7702 | |
| 7703 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7704 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7705 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7706 | |
| 7707 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7708 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7709 | if (!crt) |
| 7710 | goto out; |
| 7711 | |
| 7712 | name = X509_get_subject_name(crt); |
| 7713 | if (!name) |
| 7714 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7715 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7716 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7717 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7718 | if (crt) |
| 7719 | X509_free(crt); |
| 7720 | |
| 7721 | return result; |
| 7722 | } |
| 7723 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7724 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7725 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7726 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7727 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7728 | X509 *crt = NULL; |
| 7729 | |
| 7730 | if (!ssl_sock_is_ssl(conn)) |
| 7731 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7732 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7733 | |
| 7734 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7735 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7736 | if (!crt) |
| 7737 | return 0; |
| 7738 | |
| 7739 | X509_free(crt); |
| 7740 | return 1; |
| 7741 | } |
| 7742 | |
| 7743 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7744 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7745 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7746 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7747 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7748 | if (!ssl_sock_is_ssl(conn)) |
| 7749 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7750 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7751 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | /* returns result from SSL verify */ |
| 7755 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7756 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7757 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7758 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7759 | if (!ssl_sock_is_ssl(conn)) |
| 7760 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7761 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7762 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7763 | } |
| 7764 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7765 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7766 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7767 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7768 | * freed by the caller. NPN is also checked if available since older versions |
| 7769 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7770 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7771 | 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] | 7772 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7773 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7774 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7775 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7776 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7777 | return 0; |
| 7778 | |
| 7779 | *str = NULL; |
| 7780 | |
| 7781 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7782 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7783 | if (*str) |
| 7784 | return 1; |
| 7785 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7786 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7787 | 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] | 7788 | if (*str) |
| 7789 | return 1; |
| 7790 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7791 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7792 | return 0; |
| 7793 | } |
| 7794 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7795 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7796 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7797 | static int |
| 7798 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7799 | { |
| 7800 | struct connection *conn; |
| 7801 | |
| 7802 | conn = objt_conn(smp->sess->origin); |
| 7803 | if (!conn || conn->xprt != &ssl_sock) |
| 7804 | return 0; |
| 7805 | |
| 7806 | smp->flags = 0; |
| 7807 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7808 | #ifdef OPENSSL_IS_BORINGSSL |
| 7809 | { |
| 7810 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7811 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7812 | SSL_early_data_accepted(ctx->ssl)); |
| 7813 | } |
| 7814 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7815 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 7816 | (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] | 7817 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7818 | return 1; |
| 7819 | } |
| 7820 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7821 | /* boolean, returns true if client cert was present */ |
| 7822 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7823 | 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] | 7824 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7825 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7826 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7827 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7828 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7829 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7830 | return 0; |
| 7831 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7832 | ctx = conn->xprt_ctx; |
| 7833 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7834 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7835 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7836 | return 0; |
| 7837 | } |
| 7838 | |
| 7839 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7840 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7841 | 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] | 7842 | |
| 7843 | return 1; |
| 7844 | } |
| 7845 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7846 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7847 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7848 | * should be use. |
| 7849 | */ |
| 7850 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7851 | 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] | 7852 | { |
| 7853 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7854 | X509 *crt = NULL; |
| 7855 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7856 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7857 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7858 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7859 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7860 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7861 | if (!conn || conn->xprt != &ssl_sock) |
| 7862 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7863 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7864 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7865 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7866 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7867 | return 0; |
| 7868 | } |
| 7869 | |
| 7870 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7871 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7872 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7873 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7874 | |
| 7875 | if (!crt) |
| 7876 | goto out; |
| 7877 | |
| 7878 | smp_trash = get_trash_chunk(); |
| 7879 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7880 | goto out; |
| 7881 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7882 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7883 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7884 | ret = 1; |
| 7885 | out: |
| 7886 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7887 | if (cert_peer && crt) |
| 7888 | X509_free(crt); |
| 7889 | return ret; |
| 7890 | } |
| 7891 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7892 | /* binary, returns serial of certificate in a binary chunk. |
| 7893 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7894 | * should be use. |
| 7895 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7896 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7897 | 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] | 7898 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7899 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7900 | X509 *crt = NULL; |
| 7901 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7902 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7903 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7904 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7905 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7906 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7907 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7908 | return 0; |
| 7909 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7910 | ctx = conn->xprt_ctx; |
| 7911 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7912 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7913 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7914 | return 0; |
| 7915 | } |
| 7916 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7917 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7918 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7919 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7920 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7921 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7922 | if (!crt) |
| 7923 | goto out; |
| 7924 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7925 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7926 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 7927 | goto out; |
| 7928 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7929 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7930 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7931 | ret = 1; |
| 7932 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7933 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7934 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7935 | X509_free(crt); |
| 7936 | return ret; |
| 7937 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7938 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7939 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 7940 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7941 | * should be use. |
| 7942 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7943 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7944 | 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] | 7945 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7946 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7947 | X509 *crt = NULL; |
| 7948 | const EVP_MD *digest; |
| 7949 | int ret = 0; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7950 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7951 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7952 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7953 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7954 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7955 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7956 | if (!conn || conn->xprt != &ssl_sock) |
| 7957 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7958 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7959 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7960 | if (conn->flags & CO_FL_WAIT_XPRT) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7961 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7962 | return 0; |
| 7963 | } |
| 7964 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7965 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7966 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7967 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7968 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7969 | if (!crt) |
| 7970 | goto out; |
| 7971 | |
| 7972 | smp_trash = get_trash_chunk(); |
| 7973 | digest = EVP_sha1(); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 7974 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 7975 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7976 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7977 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7978 | ret = 1; |
| 7979 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7980 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7981 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7982 | X509_free(crt); |
| 7983 | return ret; |
| 7984 | } |
| 7985 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7986 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 7987 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7988 | * should be use. |
| 7989 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7990 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7991 | 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] | 7992 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7993 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7994 | X509 *crt = NULL; |
| 7995 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7996 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7997 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7998 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7999 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8000 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8001 | if (!conn || conn->xprt != &ssl_sock) |
| 8002 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8003 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8004 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8005 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8006 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8007 | return 0; |
| 8008 | } |
| 8009 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8010 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8011 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8012 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8013 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8014 | if (!crt) |
| 8015 | goto out; |
| 8016 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8017 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8018 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8019 | goto out; |
| 8020 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8021 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8022 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8023 | ret = 1; |
| 8024 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8025 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8026 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8027 | X509_free(crt); |
| 8028 | return ret; |
| 8029 | } |
| 8030 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8031 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 8032 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8033 | * should be use. |
| 8034 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8035 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8036 | 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] | 8037 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8038 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8039 | X509 *crt = NULL; |
| 8040 | X509_NAME *name; |
| 8041 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8042 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8043 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8044 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8045 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8046 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8047 | if (!conn || conn->xprt != &ssl_sock) |
| 8048 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8049 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8050 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8051 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8052 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8053 | return 0; |
| 8054 | } |
| 8055 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8056 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8057 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8058 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8059 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8060 | if (!crt) |
| 8061 | goto out; |
| 8062 | |
| 8063 | name = X509_get_issuer_name(crt); |
| 8064 | if (!name) |
| 8065 | goto out; |
| 8066 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8067 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8068 | 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] | 8069 | int pos = 1; |
| 8070 | |
| 8071 | if (args[1].type == ARGT_SINT) |
| 8072 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8073 | |
| 8074 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8075 | goto out; |
| 8076 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8077 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8078 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8079 | goto out; |
| 8080 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8081 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8082 | goto out; |
| 8083 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8084 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8085 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8086 | ret = 1; |
| 8087 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8088 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8089 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8090 | X509_free(crt); |
| 8091 | return ret; |
| 8092 | } |
| 8093 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8094 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 8095 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8096 | * should be use. |
| 8097 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8098 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8099 | 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] | 8100 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8101 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8102 | X509 *crt = NULL; |
| 8103 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8104 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8105 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8106 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8107 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8108 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8109 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8110 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8111 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8112 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8113 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8114 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8115 | return 0; |
| 8116 | } |
| 8117 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8118 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8119 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8120 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8121 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8122 | if (!crt) |
| 8123 | goto out; |
| 8124 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8125 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8126 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8127 | goto out; |
| 8128 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8129 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8130 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8131 | ret = 1; |
| 8132 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8133 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8134 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8135 | X509_free(crt); |
| 8136 | return ret; |
| 8137 | } |
| 8138 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8139 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 8140 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8141 | * should be use. |
| 8142 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8143 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8144 | 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] | 8145 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8146 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8147 | X509 *crt = NULL; |
| 8148 | X509_NAME *name; |
| 8149 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8150 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8151 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8152 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8153 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8154 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8155 | if (!conn || conn->xprt != &ssl_sock) |
| 8156 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8157 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8158 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8159 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8160 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8161 | return 0; |
| 8162 | } |
| 8163 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8164 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8165 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8166 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8167 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8168 | if (!crt) |
| 8169 | goto out; |
| 8170 | |
| 8171 | name = X509_get_subject_name(crt); |
| 8172 | if (!name) |
| 8173 | goto out; |
| 8174 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8175 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8176 | 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] | 8177 | int pos = 1; |
| 8178 | |
| 8179 | if (args[1].type == ARGT_SINT) |
| 8180 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8181 | |
| 8182 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8183 | goto out; |
| 8184 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8185 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8186 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8187 | goto out; |
| 8188 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8189 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8190 | goto out; |
| 8191 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8192 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8193 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8194 | ret = 1; |
| 8195 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8196 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8197 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8198 | X509_free(crt); |
| 8199 | return ret; |
| 8200 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8201 | |
| 8202 | /* integer, returns true if current session use a client certificate */ |
| 8203 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8204 | 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] | 8205 | { |
| 8206 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8207 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8208 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8209 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8210 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8211 | if (!conn || conn->xprt != &ssl_sock) |
| 8212 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8213 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8214 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8215 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8216 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8217 | return 0; |
| 8218 | } |
| 8219 | |
| 8220 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8221 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8222 | if (crt) { |
| 8223 | X509_free(crt); |
| 8224 | } |
| 8225 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8226 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8227 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8228 | return 1; |
| 8229 | } |
| 8230 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8231 | /* integer, returns the certificate version |
| 8232 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8233 | * should be use. |
| 8234 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8235 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8236 | 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] | 8237 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8238 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8239 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8240 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8241 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8242 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8243 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8244 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8245 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8246 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8247 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8248 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8249 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8250 | return 0; |
| 8251 | } |
| 8252 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8253 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8254 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8255 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8256 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8257 | if (!crt) |
| 8258 | return 0; |
| 8259 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8260 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8261 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8262 | if (cert_peer) |
| 8263 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8264 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8265 | |
| 8266 | return 1; |
| 8267 | } |
| 8268 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8269 | /* string, returns the certificate's signature algorithm. |
| 8270 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8271 | * should be use. |
| 8272 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8273 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8274 | 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] | 8275 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8276 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8277 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8278 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8279 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8280 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8281 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8282 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8283 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8284 | if (!conn || conn->xprt != &ssl_sock) |
| 8285 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8286 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8287 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8288 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8289 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8290 | return 0; |
| 8291 | } |
| 8292 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8293 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8294 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8295 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8296 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8297 | if (!crt) |
| 8298 | return 0; |
| 8299 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8300 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 8301 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8302 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8303 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8304 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8305 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8306 | if (cert_peer) |
| 8307 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8308 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8309 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8310 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8311 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8312 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8313 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8314 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8315 | if (cert_peer) |
| 8316 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8317 | |
| 8318 | return 1; |
| 8319 | } |
| 8320 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8321 | /* string, returns the certificate's key algorithm. |
| 8322 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8323 | * should be use. |
| 8324 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8325 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8326 | 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] | 8327 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8328 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8329 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8330 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8331 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8332 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8333 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8334 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8335 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8336 | if (!conn || conn->xprt != &ssl_sock) |
| 8337 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8338 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8339 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8340 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8341 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8342 | return 0; |
| 8343 | } |
| 8344 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8345 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8346 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8347 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8348 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8349 | if (!crt) |
| 8350 | return 0; |
| 8351 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8352 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 8353 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8354 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8355 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8356 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8357 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8358 | if (cert_peer) |
| 8359 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8360 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8361 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8362 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8363 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8364 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8365 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8366 | if (cert_peer) |
| 8367 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8368 | |
| 8369 | return 1; |
| 8370 | } |
| 8371 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8372 | /* boolean, returns true if front conn. transport layer is SSL. |
| 8373 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8374 | * char is 'b'. |
| 8375 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8376 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8377 | 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] | 8378 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8379 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8380 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8381 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8382 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8383 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8384 | return 1; |
| 8385 | } |
| 8386 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8387 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8388 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8389 | 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] | 8390 | { |
| 8391 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8392 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8393 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8394 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8395 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8396 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8397 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8398 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8399 | return 1; |
| 8400 | #else |
| 8401 | return 0; |
| 8402 | #endif |
| 8403 | } |
| 8404 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8405 | /* boolean, returns true if client session has been resumed. |
| 8406 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8407 | * char is 'b'. |
| 8408 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8409 | static int |
| 8410 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8411 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8412 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8413 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8414 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8415 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8416 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8417 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8418 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8419 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8420 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8421 | return 1; |
| 8422 | } |
| 8423 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8424 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 8425 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8426 | * char is 'b'. |
| 8427 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8428 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8429 | 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] | 8430 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8431 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8432 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8433 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8434 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8435 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8436 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8437 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8438 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8439 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8440 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8441 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8442 | return 0; |
| 8443 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8444 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8445 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8446 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8447 | |
| 8448 | return 1; |
| 8449 | } |
| 8450 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8451 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 8452 | * is SSL. |
| 8453 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8454 | * char is 'b'. |
| 8455 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8456 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8457 | 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] | 8458 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8459 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8460 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8461 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8462 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8463 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8464 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8465 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8466 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8467 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8468 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8469 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8470 | return 0; |
| 8471 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8472 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8473 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8474 | |
| 8475 | return 1; |
| 8476 | } |
| 8477 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8478 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 8479 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8480 | * char is 'b'. |
| 8481 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8482 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8483 | 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] | 8484 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8485 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8486 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8487 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8488 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8489 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8490 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8491 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8492 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8493 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8494 | 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] | 8495 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8496 | return 0; |
| 8497 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8498 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8499 | |
| 8500 | return 1; |
| 8501 | } |
| 8502 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8503 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8504 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8505 | 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] | 8506 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8507 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8508 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8509 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8510 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8511 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8512 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8513 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8514 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8515 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8516 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8517 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8518 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8519 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8520 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8521 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8522 | (const unsigned char **)&smp->data.u.str.area, |
| 8523 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8524 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8525 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8526 | return 0; |
| 8527 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8528 | smp->data.u.str.data = len; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8529 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8530 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8531 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8532 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8533 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8534 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8535 | 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] | 8536 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8537 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8538 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8539 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8540 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8541 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8542 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8543 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8544 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8545 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8546 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8547 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8548 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8549 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8550 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8551 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8552 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8553 | (const unsigned char **)&smp->data.u.str.area, |
| 8554 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8555 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8556 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8557 | return 0; |
| 8558 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8559 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8560 | return 1; |
| 8561 | } |
| 8562 | #endif |
| 8563 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8564 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 8565 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8566 | * char is 'b'. |
| 8567 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8568 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8569 | 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] | 8570 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8571 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8572 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8573 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8574 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8575 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8576 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8577 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8578 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8579 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8580 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8581 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8582 | return 0; |
| 8583 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8584 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8585 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8586 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8587 | |
| 8588 | return 1; |
| 8589 | } |
| 8590 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8591 | /* 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] | 8592 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8593 | * char is 'b'. |
| 8594 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8595 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8596 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8597 | 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] | 8598 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8599 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8600 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8601 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8602 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8603 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8604 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8605 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8606 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8607 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8608 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8609 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8610 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8611 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8612 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8613 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8614 | return 0; |
| 8615 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8616 | 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] | 8617 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8618 | return 0; |
| 8619 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8620 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8621 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8622 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8623 | #endif |
| 8624 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8625 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8626 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8627 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 8628 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8629 | { |
| 8630 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8631 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8632 | struct buffer *data; |
| 8633 | struct ssl_sock_ctx *ctx; |
| 8634 | |
| 8635 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8636 | return 0; |
| 8637 | ctx = conn->xprt_ctx; |
| 8638 | |
| 8639 | data = get_trash_chunk(); |
| 8640 | if (kw[7] == 'c') |
| 8641 | data->data = SSL_get_client_random(ctx->ssl, |
| 8642 | (unsigned char *) data->area, |
| 8643 | data->size); |
| 8644 | else |
| 8645 | data->data = SSL_get_server_random(ctx->ssl, |
| 8646 | (unsigned char *) data->area, |
| 8647 | data->size); |
| 8648 | if (!data->data) |
| 8649 | return 0; |
| 8650 | |
| 8651 | smp->flags = 0; |
| 8652 | smp->data.type = SMP_T_BIN; |
| 8653 | smp->data.u.str = *data; |
| 8654 | |
| 8655 | return 1; |
| 8656 | } |
| 8657 | |
| 8658 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8659 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8660 | { |
| 8661 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8662 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8663 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8664 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8665 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8666 | |
| 8667 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8668 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8669 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8670 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8671 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8672 | if (!ssl_sess) |
| 8673 | return 0; |
| 8674 | |
| 8675 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8676 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8677 | (unsigned char *) data->area, |
| 8678 | data->size); |
| 8679 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8680 | return 0; |
| 8681 | |
| 8682 | smp->flags = 0; |
| 8683 | smp->data.type = SMP_T_BIN; |
| 8684 | smp->data.u.str = *data; |
| 8685 | |
| 8686 | return 1; |
| 8687 | } |
| 8688 | #endif |
| 8689 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8690 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8691 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8692 | 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] | 8693 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8694 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8695 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8696 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8697 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8698 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8699 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8700 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8701 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8702 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8703 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8704 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8705 | 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] | 8706 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8707 | return 0; |
| 8708 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8709 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8710 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8711 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8712 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8713 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8714 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8715 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8716 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8717 | struct connection *conn; |
| 8718 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8719 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8720 | |
| 8721 | conn = objt_conn(smp->sess->origin); |
| 8722 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8723 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8724 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8725 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8726 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8727 | if (!capture) |
| 8728 | return 0; |
| 8729 | |
| 8730 | smp->flags = SMP_F_CONST; |
| 8731 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8732 | smp->data.u.str.area = capture->ciphersuite; |
| 8733 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8734 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | static int |
| 8738 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8739 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8740 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8741 | |
| 8742 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8743 | return 0; |
| 8744 | |
| 8745 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8746 | 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] | 8747 | smp->data.type = SMP_T_BIN; |
| 8748 | smp->data.u.str = *data; |
| 8749 | return 1; |
| 8750 | } |
| 8751 | |
| 8752 | static int |
| 8753 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8754 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8755 | struct connection *conn; |
| 8756 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8757 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8758 | |
| 8759 | conn = objt_conn(smp->sess->origin); |
| 8760 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8761 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8762 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8763 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8764 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8765 | if (!capture) |
| 8766 | return 0; |
| 8767 | |
| 8768 | smp->data.type = SMP_T_SINT; |
| 8769 | smp->data.u.sint = capture->xxh64; |
| 8770 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8771 | } |
| 8772 | |
| 8773 | static int |
| 8774 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8775 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8776 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8777 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8778 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8779 | |
| 8780 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8781 | return 0; |
| 8782 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8783 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8784 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8785 | const char *str; |
| 8786 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8787 | 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] | 8788 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8789 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8790 | cipher = SSL_get_cipher_by_value(id); |
| 8791 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8792 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8793 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8794 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8795 | #endif |
| 8796 | str = SSL_CIPHER_get_name(cipher); |
| 8797 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8798 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8799 | else |
| 8800 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8801 | } |
| 8802 | smp->data.type = SMP_T_STR; |
| 8803 | smp->data.u.str = *data; |
| 8804 | return 1; |
| 8805 | #else |
| 8806 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8807 | #endif |
| 8808 | } |
| 8809 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8810 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8811 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8812 | 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] | 8813 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8814 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8815 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8816 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8817 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8818 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8819 | |
| 8820 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8821 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8822 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8823 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8824 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8825 | if (conn->flags & CO_FL_WAIT_XPRT) { |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8826 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8827 | return 0; |
| 8828 | } |
| 8829 | |
| 8830 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8831 | if (!SSL_session_reused(ctx->ssl)) |
| 8832 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8833 | finished_trash->area, |
| 8834 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8835 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8836 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8837 | finished_trash->area, |
| 8838 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8839 | |
| 8840 | if (!finished_len) |
| 8841 | return 0; |
| 8842 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8843 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8844 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8845 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8846 | |
| 8847 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8848 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8849 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8850 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8851 | /* 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] | 8852 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8853 | 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] | 8854 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8855 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8856 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8857 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8858 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8859 | if (!conn || conn->xprt != &ssl_sock) |
| 8860 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8861 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8862 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8863 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8864 | smp->flags = SMP_F_MAY_CHANGE; |
| 8865 | return 0; |
| 8866 | } |
| 8867 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8868 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8869 | 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] | 8870 | smp->flags = 0; |
| 8871 | |
| 8872 | return 1; |
| 8873 | } |
| 8874 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8875 | /* 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] | 8876 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8877 | 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] | 8878 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8879 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8880 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8881 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8882 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8883 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8884 | return 0; |
| 8885 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8886 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8887 | smp->flags = SMP_F_MAY_CHANGE; |
| 8888 | return 0; |
| 8889 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8890 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8891 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8892 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8893 | 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] | 8894 | smp->flags = 0; |
| 8895 | |
| 8896 | return 1; |
| 8897 | } |
| 8898 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8899 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8900 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8901 | 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] | 8902 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8903 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8904 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8905 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8906 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8907 | if (!conn || conn->xprt != &ssl_sock) |
| 8908 | return 0; |
| 8909 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8910 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8911 | smp->flags = SMP_F_MAY_CHANGE; |
| 8912 | return 0; |
| 8913 | } |
| 8914 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8915 | ctx = conn->xprt_ctx; |
| 8916 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8917 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8918 | 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] | 8919 | smp->flags = 0; |
| 8920 | |
| 8921 | return 1; |
| 8922 | } |
| 8923 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8924 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8925 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8926 | 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] | 8927 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8928 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8929 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8930 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8931 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8932 | if (!conn || conn->xprt != &ssl_sock) |
| 8933 | return 0; |
| 8934 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8935 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8936 | smp->flags = SMP_F_MAY_CHANGE; |
| 8937 | return 0; |
| 8938 | } |
| 8939 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8940 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8941 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8942 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8943 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8944 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8945 | 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] | 8946 | smp->flags = 0; |
| 8947 | |
| 8948 | return 1; |
| 8949 | } |
| 8950 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8951 | /* for ca-file and ca-verify-file */ |
| 8952 | 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] | 8953 | { |
| 8954 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8955 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8956 | return ERR_ALERT | ERR_FATAL; |
| 8957 | } |
| 8958 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8959 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8960 | 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] | 8961 | else |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8962 | memprintf(ca_file_p, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8963 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8964 | if (!ssl_store_load_locations_file(*ca_file_p)) { |
| 8965 | 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] | 8966 | return ERR_ALERT | ERR_FATAL; |
| 8967 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8968 | return 0; |
| 8969 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8970 | |
| 8971 | /* parse the "ca-file" bind keyword */ |
| 8972 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8973 | { |
| 8974 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err); |
| 8975 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8976 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8977 | { |
| 8978 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8979 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8980 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 8981 | /* parse the "ca-verify-file" bind keyword */ |
| 8982 | static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8983 | { |
| 8984 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err); |
| 8985 | } |
| 8986 | static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8987 | { |
| 8988 | return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8989 | } |
| 8990 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8991 | /* parse the "ca-sign-file" bind keyword */ |
| 8992 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8993 | { |
| 8994 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8995 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8996 | return ERR_ALERT | ERR_FATAL; |
| 8997 | } |
| 8998 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8999 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9000 | 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] | 9001 | else |
| 9002 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 9003 | |
| 9004 | return 0; |
| 9005 | } |
| 9006 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9007 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9008 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9009 | { |
| 9010 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9011 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9012 | return ERR_ALERT | ERR_FATAL; |
| 9013 | } |
| 9014 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 9015 | return 0; |
| 9016 | } |
| 9017 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9018 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9019 | 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] | 9020 | { |
| 9021 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9022 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9023 | return ERR_ALERT | ERR_FATAL; |
| 9024 | } |
| 9025 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9026 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9027 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9028 | return 0; |
| 9029 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9030 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9031 | { |
| 9032 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 9033 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9034 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9035 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9036 | /* parse the "ciphersuites" bind keyword */ |
| 9037 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9038 | { |
| 9039 | if (!*args[cur_arg + 1]) { |
| 9040 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 9041 | return ERR_ALERT | ERR_FATAL; |
| 9042 | } |
| 9043 | |
| 9044 | free(conf->ciphersuites); |
| 9045 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 9046 | return 0; |
| 9047 | } |
| 9048 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9049 | { |
| 9050 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 9051 | } |
| 9052 | #endif |
| 9053 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9054 | /* 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] | 9055 | 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] | 9056 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 9057 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 9058 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9059 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9060 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9061 | return ERR_ALERT | ERR_FATAL; |
| 9062 | } |
| 9063 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9064 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 9065 | 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] | 9066 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 9067 | return ERR_ALERT | ERR_FATAL; |
| 9068 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9069 | 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] | 9070 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9071 | } |
| 9072 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9073 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9076 | /* 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] | 9077 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9078 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9079 | int err_code; |
| 9080 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9081 | if (!*args[cur_arg + 1]) { |
| 9082 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 9083 | return ERR_ALERT | ERR_FATAL; |
| 9084 | } |
| 9085 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 9086 | 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] | 9087 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 9088 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9089 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9090 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9091 | } |
| 9092 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 9093 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9094 | 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] | 9095 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9096 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9097 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9098 | return ERR_ALERT | ERR_FATAL; |
| 9099 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9100 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9101 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9102 | return ERR_ALERT | ERR_FATAL; |
| 9103 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9104 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9105 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9106 | 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] | 9107 | else |
| 9108 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9109 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9110 | if (!ssl_store_load_locations_file(conf->crl_file)) { |
| 9111 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file); |
| 9112 | return ERR_ALERT | ERR_FATAL; |
| 9113 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9114 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9115 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9116 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9117 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9118 | { |
| 9119 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9120 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9121 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9122 | /* parse the "curves" bind keyword keyword */ |
| 9123 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9124 | { |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 9125 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9126 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9127 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9128 | return ERR_ALERT | ERR_FATAL; |
| 9129 | } |
| 9130 | conf->curves = strdup(args[cur_arg + 1]); |
| 9131 | return 0; |
| 9132 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9133 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9134 | return ERR_ALERT | ERR_FATAL; |
| 9135 | #endif |
| 9136 | } |
| 9137 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9138 | { |
| 9139 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 9140 | } |
| 9141 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9142 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9143 | 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] | 9144 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9145 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9146 | 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] | 9147 | return ERR_ALERT | ERR_FATAL; |
| 9148 | #elif defined(OPENSSL_NO_ECDH) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9149 | 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] | 9150 | return ERR_ALERT | ERR_FATAL; |
| 9151 | #else |
| 9152 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9153 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9154 | return ERR_ALERT | ERR_FATAL; |
| 9155 | } |
| 9156 | |
| 9157 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9158 | |
| 9159 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9160 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9161 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9162 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9163 | { |
| 9164 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 9165 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9166 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9167 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9168 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9169 | { |
| 9170 | int code; |
| 9171 | char *p = args[cur_arg + 1]; |
| 9172 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 9173 | |
| 9174 | if (!*p) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9175 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9176 | return ERR_ALERT | ERR_FATAL; |
| 9177 | } |
| 9178 | |
| 9179 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 9180 | ignerr = &conf->ca_ignerr; |
| 9181 | |
| 9182 | if (strcmp(p, "all") == 0) { |
| 9183 | *ignerr = ~0ULL; |
| 9184 | return 0; |
| 9185 | } |
| 9186 | |
| 9187 | while (p) { |
| 9188 | code = atoi(p); |
| 9189 | if ((code <= 0) || (code > 63)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9190 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 9191 | args[cur_arg], code, args[cur_arg + 1]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9192 | return ERR_ALERT | ERR_FATAL; |
| 9193 | } |
| 9194 | *ignerr |= 1ULL << code; |
| 9195 | p = strchr(p, ','); |
| 9196 | if (p) |
| 9197 | p++; |
| 9198 | } |
| 9199 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9200 | return 0; |
| 9201 | } |
| 9202 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9203 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 9204 | 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] | 9205 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9206 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9207 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9208 | p = strchr(arg, '-'); |
| 9209 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9210 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9211 | p++; |
| 9212 | if (!strcmp(p, "sslv3")) |
| 9213 | v = CONF_SSLV3; |
| 9214 | else if (!strcmp(p, "tlsv10")) |
| 9215 | v = CONF_TLSV10; |
| 9216 | else if (!strcmp(p, "tlsv11")) |
| 9217 | v = CONF_TLSV11; |
| 9218 | else if (!strcmp(p, "tlsv12")) |
| 9219 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9220 | else if (!strcmp(p, "tlsv13")) |
| 9221 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9222 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9223 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9224 | if (!strncmp(arg, "no-", 3)) |
| 9225 | methods->flags |= methodVersions[v].flag; |
| 9226 | else if (!strncmp(arg, "force-", 6)) |
| 9227 | methods->min = methods->max = v; |
| 9228 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9229 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9230 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9231 | fail: |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9232 | memprintf(err, "'%s' : option not implemented", arg); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9233 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9234 | } |
| 9235 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9236 | 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] | 9237 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9238 | 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] | 9239 | } |
| 9240 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9241 | 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] | 9242 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9243 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 9244 | } |
| 9245 | |
| 9246 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 9247 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 9248 | { |
| 9249 | uint16_t i, v = 0; |
| 9250 | char *argv = args[cur_arg + 1]; |
| 9251 | if (!*argv) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9252 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9253 | return ERR_ALERT | ERR_FATAL; |
| 9254 | } |
| 9255 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9256 | if (!strcmp(argv, methodVersions[i].name)) |
| 9257 | v = i; |
| 9258 | if (!v) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9259 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9260 | return ERR_ALERT | ERR_FATAL; |
| 9261 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9262 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 9263 | methods->min = v; |
| 9264 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 9265 | methods->max = v; |
| 9266 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9267 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9268 | return ERR_ALERT | ERR_FATAL; |
| 9269 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9270 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9271 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9272 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9273 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9274 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9275 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9276 | 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] | 9277 | #endif |
| 9278 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 9279 | } |
| 9280 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9281 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9282 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9283 | 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] | 9284 | } |
| 9285 | |
| 9286 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9287 | { |
| 9288 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 9289 | } |
| 9290 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9291 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9292 | 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] | 9293 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 9294 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9295 | return 0; |
| 9296 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9297 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9298 | /* parse the "allow-0rtt" bind keyword */ |
| 9299 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9300 | { |
| 9301 | conf->early_data = 1; |
| 9302 | return 0; |
| 9303 | } |
| 9304 | |
| 9305 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9306 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 9307 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9308 | return 0; |
| 9309 | } |
| 9310 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9311 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9312 | 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] | 9313 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9314 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9315 | char *p1, *p2; |
| 9316 | |
| 9317 | if (!*args[cur_arg + 1]) { |
| 9318 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 9319 | return ERR_ALERT | ERR_FATAL; |
| 9320 | } |
| 9321 | |
| 9322 | free(conf->npn_str); |
| 9323 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9324 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9325 | * so we reuse each comma to store the next <len> and need |
| 9326 | * one more for the end of the string. |
| 9327 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9328 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9329 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9330 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 9331 | |
| 9332 | /* replace commas with the name length */ |
| 9333 | p1 = conf->npn_str; |
| 9334 | p2 = p1 + 1; |
| 9335 | while (1) { |
| 9336 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 9337 | if (!p2) |
| 9338 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9339 | |
| 9340 | if (p2 - (p1 + 1) > 255) { |
| 9341 | *p2 = '\0'; |
| 9342 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9343 | return ERR_ALERT | ERR_FATAL; |
| 9344 | } |
| 9345 | |
| 9346 | *p1 = p2 - (p1 + 1); |
| 9347 | p1 = p2; |
| 9348 | |
| 9349 | if (!*p2) |
| 9350 | break; |
| 9351 | |
| 9352 | *(p2++) = '\0'; |
| 9353 | } |
| 9354 | return 0; |
| 9355 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9356 | 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] | 9357 | return ERR_ALERT | ERR_FATAL; |
| 9358 | #endif |
| 9359 | } |
| 9360 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9361 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9362 | { |
| 9363 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9364 | } |
| 9365 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9366 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9367 | 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] | 9368 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9369 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9370 | char *p1, *p2; |
| 9371 | |
| 9372 | if (!*args[cur_arg + 1]) { |
| 9373 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 9374 | return ERR_ALERT | ERR_FATAL; |
| 9375 | } |
| 9376 | |
| 9377 | free(conf->alpn_str); |
| 9378 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9379 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9380 | * so we reuse each comma to store the next <len> and need |
| 9381 | * one more for the end of the string. |
| 9382 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9383 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9384 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9385 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 9386 | |
| 9387 | /* replace commas with the name length */ |
| 9388 | p1 = conf->alpn_str; |
| 9389 | p2 = p1 + 1; |
| 9390 | while (1) { |
| 9391 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 9392 | if (!p2) |
| 9393 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9394 | |
| 9395 | if (p2 - (p1 + 1) > 255) { |
| 9396 | *p2 = '\0'; |
| 9397 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9398 | return ERR_ALERT | ERR_FATAL; |
| 9399 | } |
| 9400 | |
| 9401 | *p1 = p2 - (p1 + 1); |
| 9402 | p1 = p2; |
| 9403 | |
| 9404 | if (!*p2) |
| 9405 | break; |
| 9406 | |
| 9407 | *(p2++) = '\0'; |
| 9408 | } |
| 9409 | return 0; |
| 9410 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9411 | 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] | 9412 | return ERR_ALERT | ERR_FATAL; |
| 9413 | #endif |
| 9414 | } |
| 9415 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9416 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9417 | { |
| 9418 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9419 | } |
| 9420 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9421 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9422 | 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] | 9423 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 9424 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9425 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9426 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9427 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 9428 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9429 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9430 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 9431 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9432 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9433 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9434 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 9435 | if (!conf->ssl_conf.ssl_methods.min) |
| 9436 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 9437 | if (!conf->ssl_conf.ssl_methods.max) |
| 9438 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9439 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9440 | return 0; |
| 9441 | } |
| 9442 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9443 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 9444 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9445 | { |
| 9446 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 9447 | return 0; |
| 9448 | } |
| 9449 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9450 | /* parse the "generate-certificates" bind keyword */ |
| 9451 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9452 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9453 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9454 | conf->generate_certs = 1; |
| 9455 | #else |
| 9456 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 9457 | err && *err ? *err : ""); |
| 9458 | #endif |
| 9459 | return 0; |
| 9460 | } |
| 9461 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9462 | /* parse the "strict-sni" bind keyword */ |
| 9463 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9464 | { |
| 9465 | conf->strict_sni = 1; |
| 9466 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9467 | } |
| 9468 | |
| 9469 | /* parse the "tls-ticket-keys" bind keyword */ |
| 9470 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9471 | { |
| 9472 | #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] | 9473 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9474 | int i = 0; |
| 9475 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9476 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9477 | |
| 9478 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9479 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9480 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9481 | } |
| 9482 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9483 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9484 | if (keys_ref) { |
| 9485 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9486 | conf->keys_ref = keys_ref; |
| 9487 | return 0; |
| 9488 | } |
| 9489 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9490 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9491 | if (!keys_ref) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9492 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9493 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9494 | } |
| 9495 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9496 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9497 | if (!keys_ref->tlskeys) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9498 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9499 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9500 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9501 | |
| 9502 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9503 | 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] | 9504 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9505 | } |
| 9506 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9507 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9508 | if (!keys_ref->filename) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9509 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9510 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9511 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9512 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9513 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9514 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 9515 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9516 | int dec_size; |
| 9517 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9518 | /* Strip newline characters from the end */ |
| 9519 | if(thisline[len - 1] == '\n') |
| 9520 | thisline[--len] = 0; |
| 9521 | |
| 9522 | if(thisline[len - 1] == '\r') |
| 9523 | thisline[--len] = 0; |
| 9524 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9525 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 9526 | if (dec_size < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9527 | 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] | 9528 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9529 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9530 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 9531 | keys_ref->key_size_bits = 128; |
| 9532 | } |
| 9533 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 9534 | keys_ref->key_size_bits = 256; |
| 9535 | } |
| 9536 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 9537 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 9538 | || ((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] | 9539 | 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] | 9540 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9541 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9542 | i++; |
| 9543 | } |
| 9544 | |
| 9545 | if (i < TLS_TICKETS_NO) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9546 | 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] | 9547 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9548 | } |
| 9549 | |
| 9550 | fclose(f); |
| 9551 | |
| 9552 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 9553 | i -= 2; |
| 9554 | 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] | 9555 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9556 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9557 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9558 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9559 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9560 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 9561 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9562 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9563 | |
| 9564 | fail: |
| 9565 | if (f) |
| 9566 | fclose(f); |
| 9567 | if (keys_ref) { |
| 9568 | free(keys_ref->filename); |
| 9569 | free(keys_ref->tlskeys); |
| 9570 | free(keys_ref); |
| 9571 | } |
| 9572 | return ERR_ALERT | ERR_FATAL; |
| 9573 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9574 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9575 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9576 | return ERR_ALERT | ERR_FATAL; |
| 9577 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9578 | } |
| 9579 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9580 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9581 | 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] | 9582 | { |
| 9583 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9584 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9585 | return ERR_ALERT | ERR_FATAL; |
| 9586 | } |
| 9587 | |
| 9588 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9589 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9590 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9591 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9592 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9593 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9594 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9595 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 9596 | args[cur_arg], args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9597 | return ERR_ALERT | ERR_FATAL; |
| 9598 | } |
| 9599 | |
| 9600 | return 0; |
| 9601 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9602 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9603 | { |
| 9604 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 9605 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9606 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9607 | /* parse the "no-ca-names" bind keyword */ |
| 9608 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9609 | { |
| 9610 | conf->no_ca_names = 1; |
| 9611 | return 0; |
| 9612 | } |
| 9613 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9614 | { |
| 9615 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 9616 | } |
| 9617 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9618 | /************** "server" keywords ****************/ |
| 9619 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9620 | /* parse the "npn" bind keyword */ |
| 9621 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9622 | { |
| 9623 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9624 | char *p1, *p2; |
| 9625 | |
| 9626 | if (!*args[*cur_arg + 1]) { |
| 9627 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 9628 | return ERR_ALERT | ERR_FATAL; |
| 9629 | } |
| 9630 | |
| 9631 | free(newsrv->ssl_ctx.npn_str); |
| 9632 | |
| 9633 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9634 | * so we reuse each comma to store the next <len> and need |
| 9635 | * one more for the end of the string. |
| 9636 | */ |
| 9637 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9638 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 9639 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 9640 | newsrv->ssl_ctx.npn_len); |
| 9641 | |
| 9642 | /* replace commas with the name length */ |
| 9643 | p1 = newsrv->ssl_ctx.npn_str; |
| 9644 | p2 = p1 + 1; |
| 9645 | while (1) { |
| 9646 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 9647 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 9648 | if (!p2) |
| 9649 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9650 | |
| 9651 | if (p2 - (p1 + 1) > 255) { |
| 9652 | *p2 = '\0'; |
| 9653 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9654 | return ERR_ALERT | ERR_FATAL; |
| 9655 | } |
| 9656 | |
| 9657 | *p1 = p2 - (p1 + 1); |
| 9658 | p1 = p2; |
| 9659 | |
| 9660 | if (!*p2) |
| 9661 | break; |
| 9662 | |
| 9663 | *(p2++) = '\0'; |
| 9664 | } |
| 9665 | return 0; |
| 9666 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9667 | 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] | 9668 | return ERR_ALERT | ERR_FATAL; |
| 9669 | #endif |
| 9670 | } |
| 9671 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9672 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9673 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9674 | { |
| 9675 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9676 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9677 | char **alpn_str; |
| 9678 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9679 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9680 | if (*args[*cur_arg] == 'c') { |
| 9681 | alpn_str = &newsrv->check.alpn_str; |
| 9682 | alpn_len = &newsrv->check.alpn_len; |
| 9683 | } else { |
| 9684 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9685 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9686 | |
| 9687 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9688 | if (!*args[*cur_arg + 1]) { |
| 9689 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 9690 | return ERR_ALERT | ERR_FATAL; |
| 9691 | } |
| 9692 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9693 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9694 | |
| 9695 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9696 | * so we reuse each comma to store the next <len> and need |
| 9697 | * one more for the end of the string. |
| 9698 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9699 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9700 | *alpn_str = calloc(1, *alpn_len + 1); |
| 9701 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9702 | |
| 9703 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9704 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9705 | p2 = p1 + 1; |
| 9706 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9707 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9708 | if (!p2) |
| 9709 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9710 | |
| 9711 | if (p2 - (p1 + 1) > 255) { |
| 9712 | *p2 = '\0'; |
| 9713 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9714 | return ERR_ALERT | ERR_FATAL; |
| 9715 | } |
| 9716 | |
| 9717 | *p1 = p2 - (p1 + 1); |
| 9718 | p1 = p2; |
| 9719 | |
| 9720 | if (!*p2) |
| 9721 | break; |
| 9722 | |
| 9723 | *(p2++) = '\0'; |
| 9724 | } |
| 9725 | return 0; |
| 9726 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9727 | 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] | 9728 | return ERR_ALERT | ERR_FATAL; |
| 9729 | #endif |
| 9730 | } |
| 9731 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9732 | /* parse the "ca-file" server keyword */ |
| 9733 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9734 | { |
| 9735 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9736 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9737 | return ERR_ALERT | ERR_FATAL; |
| 9738 | } |
| 9739 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9740 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9741 | 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] | 9742 | else |
| 9743 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9744 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 9745 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) { |
| 9746 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file); |
| 9747 | return ERR_ALERT | ERR_FATAL; |
| 9748 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9749 | return 0; |
| 9750 | } |
| 9751 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9752 | /* parse the "check-sni" server keyword */ |
| 9753 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9754 | { |
| 9755 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9756 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9757 | return ERR_ALERT | ERR_FATAL; |
| 9758 | } |
| 9759 | |
| 9760 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9761 | if (!newsrv->check.sni) { |
| 9762 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9763 | return ERR_ALERT | ERR_FATAL; |
| 9764 | } |
| 9765 | return 0; |
| 9766 | |
| 9767 | } |
| 9768 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9769 | /* parse the "check-ssl" server keyword */ |
| 9770 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9771 | { |
| 9772 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9773 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9774 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9775 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9776 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9777 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9778 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9779 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9780 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9781 | if (!newsrv->ssl_ctx.methods.min) |
| 9782 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9783 | if (!newsrv->ssl_ctx.methods.max) |
| 9784 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9785 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9786 | return 0; |
| 9787 | } |
| 9788 | |
| 9789 | /* parse the "ciphers" server keyword */ |
| 9790 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9791 | { |
| 9792 | if (!*args[*cur_arg + 1]) { |
| 9793 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9794 | return ERR_ALERT | ERR_FATAL; |
| 9795 | } |
| 9796 | |
| 9797 | free(newsrv->ssl_ctx.ciphers); |
| 9798 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9799 | return 0; |
| 9800 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9801 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9802 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9803 | /* parse the "ciphersuites" server keyword */ |
| 9804 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9805 | { |
| 9806 | if (!*args[*cur_arg + 1]) { |
| 9807 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9808 | return ERR_ALERT | ERR_FATAL; |
| 9809 | } |
| 9810 | |
| 9811 | free(newsrv->ssl_ctx.ciphersuites); |
| 9812 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9813 | return 0; |
| 9814 | } |
| 9815 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9816 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9817 | /* parse the "crl-file" server keyword */ |
| 9818 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9819 | { |
| 9820 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9821 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9822 | return ERR_ALERT | ERR_FATAL; |
| 9823 | #else |
| 9824 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9825 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9826 | return ERR_ALERT | ERR_FATAL; |
| 9827 | } |
| 9828 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9829 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9830 | 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] | 9831 | else |
| 9832 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9833 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9834 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) { |
| 9835 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file); |
| 9836 | return ERR_ALERT | ERR_FATAL; |
| 9837 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9838 | return 0; |
| 9839 | #endif |
| 9840 | } |
| 9841 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9842 | /* parse the "crt" server keyword */ |
| 9843 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9844 | { |
| 9845 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9846 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9847 | return ERR_ALERT | ERR_FATAL; |
| 9848 | } |
| 9849 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9850 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9851 | 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] | 9852 | else |
| 9853 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9854 | |
| 9855 | return 0; |
| 9856 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9857 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9858 | /* parse the "no-check-ssl" server keyword */ |
| 9859 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9860 | { |
| 9861 | newsrv->check.use_ssl = 0; |
| 9862 | free(newsrv->ssl_ctx.ciphers); |
| 9863 | newsrv->ssl_ctx.ciphers = NULL; |
| 9864 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9865 | return 0; |
| 9866 | } |
| 9867 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9868 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9869 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9870 | { |
| 9871 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9872 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9873 | return 0; |
| 9874 | } |
| 9875 | |
| 9876 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9877 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9878 | { |
| 9879 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9880 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9881 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9882 | return 0; |
| 9883 | } |
| 9884 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9885 | /* parse the "no-ssl" server keyword */ |
| 9886 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9887 | { |
| 9888 | newsrv->use_ssl = 0; |
| 9889 | free(newsrv->ssl_ctx.ciphers); |
| 9890 | newsrv->ssl_ctx.ciphers = NULL; |
| 9891 | return 0; |
| 9892 | } |
| 9893 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9894 | /* parse the "allow-0rtt" server keyword */ |
| 9895 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9896 | { |
| 9897 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9898 | return 0; |
| 9899 | } |
| 9900 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9901 | /* parse the "no-ssl-reuse" server keyword */ |
| 9902 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9903 | { |
| 9904 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9905 | return 0; |
| 9906 | } |
| 9907 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9908 | /* parse the "no-tls-tickets" server keyword */ |
| 9909 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9910 | { |
| 9911 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 9912 | return 0; |
| 9913 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 9914 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 9915 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9916 | { |
| 9917 | newsrv->pp_opts |= SRV_PP_V2; |
| 9918 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9919 | return 0; |
| 9920 | } |
| 9921 | |
| 9922 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 9923 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9924 | { |
| 9925 | newsrv->pp_opts |= SRV_PP_V2; |
| 9926 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9927 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 9928 | return 0; |
| 9929 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9930 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9931 | /* parse the "sni" server keyword */ |
| 9932 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9933 | { |
| 9934 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9935 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 9936 | return ERR_ALERT | ERR_FATAL; |
| 9937 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9938 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9939 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9940 | arg = args[*cur_arg + 1]; |
| 9941 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9942 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 9943 | return ERR_ALERT | ERR_FATAL; |
| 9944 | } |
| 9945 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9946 | free(newsrv->sni_expr); |
| 9947 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9948 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9949 | return 0; |
| 9950 | #endif |
| 9951 | } |
| 9952 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9953 | /* parse the "ssl" server keyword */ |
| 9954 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9955 | { |
| 9956 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9957 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9958 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9959 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9960 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9961 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9962 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9963 | return 0; |
| 9964 | } |
| 9965 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9966 | /* parse the "ssl-reuse" server keyword */ |
| 9967 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9968 | { |
| 9969 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 9970 | return 0; |
| 9971 | } |
| 9972 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9973 | /* parse the "tls-tickets" server keyword */ |
| 9974 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9975 | { |
| 9976 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 9977 | return 0; |
| 9978 | } |
| 9979 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9980 | /* parse the "verify" server keyword */ |
| 9981 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9982 | { |
| 9983 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9984 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9985 | return ERR_ALERT | ERR_FATAL; |
| 9986 | } |
| 9987 | |
| 9988 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9989 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9990 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9991 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9992 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9993 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 9994 | args[*cur_arg], args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9995 | return ERR_ALERT | ERR_FATAL; |
| 9996 | } |
| 9997 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9998 | return 0; |
| 9999 | } |
| 10000 | |
| 10001 | /* parse the "verifyhost" server keyword */ |
| 10002 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10003 | { |
| 10004 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10005 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10006 | return ERR_ALERT | ERR_FATAL; |
| 10007 | } |
| 10008 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 10009 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10010 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 10011 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10012 | return 0; |
| 10013 | } |
| 10014 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10015 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 10016 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 10017 | struct proxy *defpx, const char *file, int line, |
| 10018 | char **err) { |
| 10019 | int i = 1; |
| 10020 | |
| 10021 | if (*(args[i]) == 0) { |
| 10022 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10023 | return -1; |
| 10024 | } |
| 10025 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10026 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10027 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 10028 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 10029 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10030 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10031 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 10032 | i++; |
| 10033 | else { |
| 10034 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10035 | return -1; |
| 10036 | } |
| 10037 | } |
| 10038 | 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] | 10039 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10040 | return -1; |
| 10041 | } |
| 10042 | i++; |
| 10043 | } |
| 10044 | return 0; |
| 10045 | } |
| 10046 | |
| 10047 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 10048 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 10049 | struct proxy *defpx, const char *file, int line, |
| 10050 | char **err) { |
| 10051 | int i = 1; |
| 10052 | |
| 10053 | if (*(args[i]) == 0) { |
| 10054 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10055 | return -1; |
| 10056 | } |
| 10057 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10058 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10059 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10060 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10061 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 10062 | i++; |
| 10063 | else { |
| 10064 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10065 | return -1; |
| 10066 | } |
| 10067 | } |
| 10068 | 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] | 10069 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10070 | return -1; |
| 10071 | } |
| 10072 | i++; |
| 10073 | } |
| 10074 | return 0; |
| 10075 | } |
| 10076 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10077 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 10078 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10079 | */ |
| 10080 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 10081 | struct proxy *defpx, const char *file, int line, |
| 10082 | char **err) |
| 10083 | { |
| 10084 | char **target; |
| 10085 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10086 | 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] | 10087 | |
| 10088 | if (too_many_args(1, args, err, NULL)) |
| 10089 | return -1; |
| 10090 | |
| 10091 | if (*target) { |
| 10092 | memprintf(err, "'%s' already specified.", args[0]); |
| 10093 | return -1; |
| 10094 | } |
| 10095 | |
| 10096 | if (*(args[1]) == 0) { |
| 10097 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 10098 | return -1; |
| 10099 | } |
| 10100 | *target = strdup(args[1]); |
| 10101 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10102 | } |
| 10103 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10104 | /* "issuers-chain-path" load chain certificate in global */ |
| 10105 | static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
| 10106 | { |
| 10107 | X509 *ca; |
| 10108 | X509_NAME *name = NULL; |
| 10109 | ASN1_OCTET_STRING *skid = NULL; |
| 10110 | STACK_OF(X509) *chain = NULL; |
| 10111 | struct issuer_chain *issuer; |
| 10112 | struct eb64_node *node; |
| 10113 | char *path; |
| 10114 | u64 key; |
| 10115 | int ret = 0; |
| 10116 | |
| 10117 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 10118 | if (chain == NULL) { |
| 10119 | chain = sk_X509_new_null(); |
| 10120 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 10121 | name = X509_get_subject_name(ca); |
| 10122 | } |
| 10123 | if (!sk_X509_push(chain, ca)) { |
| 10124 | X509_free(ca); |
| 10125 | goto end; |
| 10126 | } |
| 10127 | } |
| 10128 | if (!chain) { |
| 10129 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 10130 | goto end; |
| 10131 | } |
| 10132 | if (!skid) { |
| 10133 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 10134 | goto end; |
| 10135 | } |
| 10136 | if (!name) { |
| 10137 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 10138 | goto end; |
| 10139 | } |
| 10140 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10141 | 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] | 10142 | issuer = container_of(node, typeof(*issuer), node); |
| 10143 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 10144 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 10145 | goto end; |
| 10146 | } |
| 10147 | } |
| 10148 | issuer = calloc(1, sizeof *issuer); |
| 10149 | path = strdup(fp); |
| 10150 | if (!issuer || !path) { |
| 10151 | free(issuer); |
| 10152 | free(path); |
| 10153 | goto end; |
| 10154 | } |
| 10155 | issuer->node.key = key; |
| 10156 | issuer->path = path; |
| 10157 | issuer->chain = chain; |
| 10158 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10159 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10160 | ret = 1; |
| 10161 | end: |
| 10162 | if (skid) |
| 10163 | ASN1_OCTET_STRING_free(skid); |
| 10164 | if (chain) |
| 10165 | sk_X509_pop_free(chain, X509_free); |
| 10166 | return ret; |
| 10167 | } |
| 10168 | |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 10169 | static struct issuer_chain* ssl_get_issuer_chain(X509 *cert) |
| 10170 | { |
| 10171 | AUTHORITY_KEYID *akid; |
| 10172 | struct issuer_chain *issuer = NULL; |
| 10173 | |
| 10174 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 10175 | if (akid) { |
| 10176 | struct eb64_node *node; |
| 10177 | u64 hk; |
| 10178 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 10179 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 10180 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 10181 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 10182 | issuer = ti; |
| 10183 | break; |
| 10184 | } |
| 10185 | } |
| 10186 | AUTHORITY_KEYID_free(akid); |
| 10187 | } |
| 10188 | return issuer; |
| 10189 | } |
| 10190 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10191 | static void ssl_free_global_issuers(void) |
| 10192 | { |
| 10193 | struct eb64_node *node, *back; |
| 10194 | struct issuer_chain *issuer; |
| 10195 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10196 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10197 | while (node) { |
| 10198 | issuer = container_of(node, typeof(*issuer), node); |
| 10199 | back = eb64_next(node); |
| 10200 | eb64_delete(node); |
| 10201 | free(issuer->path); |
| 10202 | sk_X509_pop_free(issuer->chain, X509_free); |
| 10203 | free(issuer); |
| 10204 | node = back; |
| 10205 | } |
| 10206 | } |
| 10207 | |
| 10208 | static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx, |
| 10209 | struct proxy *defpx, const char *file, int line, |
| 10210 | char **err) |
| 10211 | { |
| 10212 | char *path; |
| 10213 | struct dirent **de_list; |
| 10214 | int i, n; |
| 10215 | struct stat buf; |
| 10216 | char *end; |
| 10217 | char fp[MAXPATHLEN+1]; |
| 10218 | |
| 10219 | if (too_many_args(1, args, err, NULL)) |
| 10220 | return -1; |
| 10221 | |
| 10222 | path = args[1]; |
| 10223 | if (*path == 0 || stat(path, &buf)) { |
| 10224 | memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n", |
| 10225 | err && *err ? *err : "", args[0]); |
| 10226 | return -1; |
| 10227 | } |
| 10228 | if (S_ISDIR(buf.st_mode) == 0) { |
| 10229 | memprintf(err, "%sglobal statement '%s': %s is not a directory.\n", |
| 10230 | err && *err ? *err : "", args[0], path); |
| 10231 | return -1; |
| 10232 | } |
| 10233 | |
| 10234 | /* strip trailing slashes, including first one */ |
| 10235 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 10236 | *end = 0; |
| 10237 | /* path already parsed? */ |
| 10238 | if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0) |
| 10239 | return 0; |
| 10240 | /* overwrite old issuers_chain_path */ |
| 10241 | free(global_ssl.issuers_chain_path); |
| 10242 | global_ssl.issuers_chain_path = strdup(path); |
| 10243 | ssl_free_global_issuers(); |
| 10244 | |
| 10245 | n = scandir(path, &de_list, 0, alphasort); |
| 10246 | if (n < 0) { |
| 10247 | memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n", |
| 10248 | err && *err ? *err : "", args[0], path, strerror(errno)); |
| 10249 | return -1; |
| 10250 | } |
| 10251 | for (i = 0; i < n; i++) { |
| 10252 | struct dirent *de = de_list[i]; |
| 10253 | BIO *in = NULL; |
| 10254 | char *warn = NULL; |
| 10255 | |
| 10256 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 10257 | free(de); |
| 10258 | if (stat(fp, &buf) != 0) { |
| 10259 | ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno)); |
| 10260 | goto next; |
| 10261 | } |
| 10262 | if (!S_ISREG(buf.st_mode)) |
| 10263 | goto next; |
| 10264 | |
| 10265 | in = BIO_new(BIO_s_file()); |
| 10266 | if (in == NULL) |
| 10267 | goto next; |
| 10268 | if (BIO_read_filename(in, fp) <= 0) |
| 10269 | goto next; |
| 10270 | ssl_load_global_issuer_from_BIO(in, fp, &warn); |
| 10271 | if (warn) { |
Tim Duesterhus | e8aa5f2 | 2020-02-19 11:41:13 +0100 | [diff] [blame] | 10272 | ha_warning("%s", warn); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10273 | free(warn); |
| 10274 | warn = NULL; |
| 10275 | } |
| 10276 | next: |
| 10277 | if (in) |
| 10278 | BIO_free(in); |
| 10279 | } |
| 10280 | free(de_list); |
| 10281 | |
| 10282 | return 0; |
| 10283 | } |
| 10284 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10285 | /* parse the "ssl-mode-async" keyword in global section. |
| 10286 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10287 | */ |
| 10288 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 10289 | struct proxy *defpx, const char *file, int line, |
| 10290 | char **err) |
| 10291 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10292 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10293 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 10294 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10295 | return 0; |
| 10296 | #else |
| 10297 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 10298 | return -1; |
| 10299 | #endif |
| 10300 | } |
| 10301 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10302 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10303 | static int ssl_check_async_engine_count(void) { |
| 10304 | int err_code = 0; |
| 10305 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 10306 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 10307 | 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] | 10308 | err_code = ERR_ABORT; |
| 10309 | } |
| 10310 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10311 | } |
| 10312 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10313 | /* parse the "ssl-engine" keyword in global section. |
| 10314 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10315 | */ |
| 10316 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 10317 | struct proxy *defpx, const char *file, int line, |
| 10318 | char **err) |
| 10319 | { |
| 10320 | char *algo; |
| 10321 | int ret = -1; |
| 10322 | |
| 10323 | if (*(args[1]) == 0) { |
| 10324 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 10325 | return ret; |
| 10326 | } |
| 10327 | |
| 10328 | if (*(args[2]) == 0) { |
| 10329 | /* if no list of algorithms is given, it defaults to ALL */ |
| 10330 | algo = strdup("ALL"); |
| 10331 | goto add_engine; |
| 10332 | } |
| 10333 | |
| 10334 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 10335 | if (strcmp(args[2], "algo") != 0) { |
| 10336 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 10337 | return ret; |
| 10338 | } |
| 10339 | |
| 10340 | if (*(args[3]) == 0) { |
| 10341 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 10342 | return ret; |
| 10343 | } |
| 10344 | algo = strdup(args[3]); |
| 10345 | |
| 10346 | add_engine: |
| 10347 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 10348 | openssl_engines_initialized++; |
| 10349 | ret = 0; |
| 10350 | } |
| 10351 | free(algo); |
| 10352 | return ret; |
| 10353 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10354 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10355 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10356 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 10357 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10358 | */ |
| 10359 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 10360 | struct proxy *defpx, const char *file, int line, |
| 10361 | char **err) |
| 10362 | { |
| 10363 | char **target; |
| 10364 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10365 | 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] | 10366 | |
| 10367 | if (too_many_args(1, args, err, NULL)) |
| 10368 | return -1; |
| 10369 | |
| 10370 | if (*(args[1]) == 0) { |
| 10371 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10372 | return -1; |
| 10373 | } |
| 10374 | |
| 10375 | free(*target); |
| 10376 | *target = strdup(args[1]); |
| 10377 | return 0; |
| 10378 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10379 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10380 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10381 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 10382 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10383 | */ |
| 10384 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 10385 | struct proxy *defpx, const char *file, int line, |
| 10386 | char **err) |
| 10387 | { |
| 10388 | char **target; |
| 10389 | |
| 10390 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 10391 | |
| 10392 | if (too_many_args(1, args, err, NULL)) |
| 10393 | return -1; |
| 10394 | |
| 10395 | if (*(args[1]) == 0) { |
| 10396 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10397 | return -1; |
| 10398 | } |
| 10399 | |
| 10400 | free(*target); |
| 10401 | *target = strdup(args[1]); |
| 10402 | return 0; |
| 10403 | } |
| 10404 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10405 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10406 | /* parse various global tune.ssl settings consisting in positive integers. |
| 10407 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10408 | */ |
| 10409 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 10410 | struct proxy *defpx, const char *file, int line, |
| 10411 | char **err) |
| 10412 | { |
| 10413 | int *target; |
| 10414 | |
| 10415 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 10416 | target = &global.tune.sslcachesize; |
| 10417 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10418 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10419 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10420 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10421 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 10422 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10423 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 10424 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10425 | else { |
| 10426 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 10427 | return -1; |
| 10428 | } |
| 10429 | |
| 10430 | if (too_many_args(1, args, err, NULL)) |
| 10431 | return -1; |
| 10432 | |
| 10433 | if (*(args[1]) == 0) { |
| 10434 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10435 | return -1; |
| 10436 | } |
| 10437 | |
| 10438 | *target = atoi(args[1]); |
| 10439 | if (*target < 0) { |
| 10440 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 10441 | return -1; |
| 10442 | } |
| 10443 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10444 | } |
| 10445 | |
| 10446 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 10447 | struct proxy *defpx, const char *file, int line, |
| 10448 | char **err) |
| 10449 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10450 | int ret; |
| 10451 | |
| 10452 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 10453 | if (ret != 0) |
| 10454 | return ret; |
| 10455 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10456 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10457 | memprintf(err, "'%s' is already configured.", args[0]); |
| 10458 | return -1; |
| 10459 | } |
| 10460 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10461 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 10462 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10463 | memprintf(err, "Out of memory error."); |
| 10464 | return -1; |
| 10465 | } |
| 10466 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10467 | } |
| 10468 | |
| 10469 | /* parse "ssl.force-private-cache". |
| 10470 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10471 | */ |
| 10472 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 10473 | struct proxy *defpx, const char *file, int line, |
| 10474 | char **err) |
| 10475 | { |
| 10476 | if (too_many_args(0, args, err, NULL)) |
| 10477 | return -1; |
| 10478 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10479 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10480 | return 0; |
| 10481 | } |
| 10482 | |
| 10483 | /* parse "ssl.lifetime". |
| 10484 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10485 | */ |
| 10486 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 10487 | struct proxy *defpx, const char *file, int line, |
| 10488 | char **err) |
| 10489 | { |
| 10490 | const char *res; |
| 10491 | |
| 10492 | if (too_many_args(1, args, err, NULL)) |
| 10493 | return -1; |
| 10494 | |
| 10495 | if (*(args[1]) == 0) { |
| 10496 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 10497 | return -1; |
| 10498 | } |
| 10499 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10500 | 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] | 10501 | if (res == PARSE_TIME_OVER) { |
| 10502 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 10503 | args[1], args[0]); |
| 10504 | return -1; |
| 10505 | } |
| 10506 | else if (res == PARSE_TIME_UNDER) { |
| 10507 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 10508 | args[1], args[0]); |
| 10509 | return -1; |
| 10510 | } |
| 10511 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10512 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 10513 | return -1; |
| 10514 | } |
| 10515 | return 0; |
| 10516 | } |
| 10517 | |
| 10518 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10519 | /* parse "ssl-dh-param-file". |
| 10520 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10521 | */ |
| 10522 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 10523 | struct proxy *defpx, const char *file, int line, |
| 10524 | char **err) |
| 10525 | { |
| 10526 | if (too_many_args(1, args, err, NULL)) |
| 10527 | return -1; |
| 10528 | |
| 10529 | if (*(args[1]) == 0) { |
| 10530 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 10531 | return -1; |
| 10532 | } |
| 10533 | |
| 10534 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 10535 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 10536 | return -1; |
| 10537 | } |
| 10538 | return 0; |
| 10539 | } |
| 10540 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10541 | /* parse "ssl.default-dh-param". |
| 10542 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10543 | */ |
| 10544 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 10545 | struct proxy *defpx, const char *file, int line, |
| 10546 | char **err) |
| 10547 | { |
| 10548 | if (too_many_args(1, args, err, NULL)) |
| 10549 | return -1; |
| 10550 | |
| 10551 | if (*(args[1]) == 0) { |
| 10552 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10553 | return -1; |
| 10554 | } |
| 10555 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10556 | global_ssl.default_dh_param = atoi(args[1]); |
| 10557 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10558 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 10559 | return -1; |
| 10560 | } |
| 10561 | return 0; |
| 10562 | } |
| 10563 | #endif |
| 10564 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10565 | |
| 10566 | /* |
| 10567 | * parse "ssl-load-extra-files". |
| 10568 | * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none" |
| 10569 | */ |
| 10570 | static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx, |
| 10571 | struct proxy *defpx, const char *file, int line, |
| 10572 | char **err) |
| 10573 | { |
| 10574 | int i; |
| 10575 | int gf = SSL_GF_NONE; |
| 10576 | |
| 10577 | if (*(args[1]) == 0) |
| 10578 | goto err_arg; |
| 10579 | |
| 10580 | for (i = 1; *args[i]; i++) { |
| 10581 | |
| 10582 | if (!strcmp("bundle", args[i])) { |
| 10583 | gf |= SSL_GF_BUNDLE; |
| 10584 | |
| 10585 | } else if (!strcmp("sctl", args[i])) { |
| 10586 | gf |= SSL_GF_SCTL; |
| 10587 | |
| 10588 | } else if (!strcmp("ocsp", args[i])){ |
| 10589 | gf |= SSL_GF_OCSP; |
| 10590 | |
| 10591 | } else if (!strcmp("issuer", args[i])){ |
| 10592 | gf |= SSL_GF_OCSP_ISSUER; |
| 10593 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10594 | } else if (!strcmp("key", args[i])) { |
| 10595 | gf |= SSL_GF_KEY; |
| 10596 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10597 | } else if (!strcmp("none", args[i])) { |
| 10598 | if (gf != SSL_GF_NONE) |
| 10599 | goto err_alone; |
| 10600 | gf = SSL_GF_NONE; |
| 10601 | i++; |
| 10602 | break; |
| 10603 | |
| 10604 | } else if (!strcmp("all", args[i])) { |
| 10605 | if (gf != SSL_GF_NONE) |
| 10606 | goto err_alone; |
| 10607 | gf = SSL_GF_ALL; |
| 10608 | i++; |
| 10609 | break; |
| 10610 | } else { |
| 10611 | goto err_arg; |
| 10612 | } |
| 10613 | } |
| 10614 | /* break from loop but there are still arguments */ |
| 10615 | if (*args[i]) |
| 10616 | goto err_alone; |
| 10617 | |
| 10618 | global_ssl.extra_files = gf; |
| 10619 | |
| 10620 | return 0; |
| 10621 | |
| 10622 | err_alone: |
| 10623 | memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]); |
| 10624 | return -1; |
| 10625 | |
| 10626 | err_arg: |
| 10627 | memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]); |
| 10628 | return -1; |
| 10629 | } |
| 10630 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10631 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10632 | /* This function is used with TLS ticket keys management. It permits to browse |
| 10633 | * each reference. The variable <getnext> must contain the current node, |
| 10634 | * <end> point to the root node. |
| 10635 | */ |
| 10636 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10637 | static inline |
| 10638 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 10639 | { |
| 10640 | struct tls_keys_ref *ref = getnext; |
| 10641 | |
| 10642 | while (1) { |
| 10643 | |
| 10644 | /* Get next list entry. */ |
| 10645 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 10646 | |
| 10647 | /* If the entry is the last of the list, return NULL. */ |
| 10648 | if (&ref->list == end) |
| 10649 | return NULL; |
| 10650 | |
| 10651 | return ref; |
| 10652 | } |
| 10653 | } |
| 10654 | |
| 10655 | static inline |
| 10656 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 10657 | { |
| 10658 | int id; |
| 10659 | char *error; |
| 10660 | |
| 10661 | /* If the reference starts by a '#', this is numeric id. */ |
| 10662 | if (reference[0] == '#') { |
| 10663 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 10664 | id = strtol(reference + 1, &error, 10); |
| 10665 | if (*error != '\0') |
| 10666 | return NULL; |
| 10667 | |
| 10668 | /* Perform the unique id lookup. */ |
| 10669 | return tlskeys_ref_lookupid(id); |
| 10670 | } |
| 10671 | |
| 10672 | /* Perform the string lookup. */ |
| 10673 | return tlskeys_ref_lookup(reference); |
| 10674 | } |
| 10675 | #endif |
| 10676 | |
| 10677 | |
| 10678 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10679 | |
| 10680 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 10681 | |
| 10682 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 10683 | return cli_io_handler_tlskeys_files(appctx); |
| 10684 | } |
| 10685 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10686 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 10687 | * (next index to be dumped), and cli.p0 (next key reference). |
| 10688 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10689 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 10690 | |
| 10691 | struct stream_interface *si = appctx->owner; |
| 10692 | |
| 10693 | switch (appctx->st2) { |
| 10694 | case STAT_ST_INIT: |
| 10695 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 10696 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10697 | * later and restart at the state "STAT_ST_INIT". |
| 10698 | */ |
| 10699 | chunk_reset(&trash); |
| 10700 | |
| 10701 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 10702 | chunk_appendf(&trash, "# id secret\n"); |
| 10703 | else |
| 10704 | chunk_appendf(&trash, "# id (file)\n"); |
| 10705 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10706 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10707 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10708 | return 0; |
| 10709 | } |
| 10710 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10711 | /* Now, we start the browsing of the references lists. |
| 10712 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 10713 | * available field of this pointer is <list>. It is used with the function |
| 10714 | * tlskeys_list_get_next() for retruning the first available entry |
| 10715 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10716 | if (appctx->ctx.cli.p0 == NULL) { |
| 10717 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 10718 | 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] | 10719 | } |
| 10720 | |
| 10721 | appctx->st2 = STAT_ST_LIST; |
| 10722 | /* fall through */ |
| 10723 | |
| 10724 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10725 | while (appctx->ctx.cli.p0) { |
| 10726 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10727 | |
| 10728 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10729 | 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] | 10730 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10731 | |
| 10732 | if (appctx->ctx.cli.i1 == 0) |
| 10733 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 10734 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10735 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10736 | int head; |
| 10737 | |
| 10738 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 10739 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10740 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10741 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10742 | |
| 10743 | chunk_reset(t2); |
| 10744 | /* 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] | 10745 | if (ref->key_size_bits == 128) { |
| 10746 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10747 | sizeof(struct tls_sess_key_128), |
| 10748 | t2->area, t2->size); |
| 10749 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10750 | t2->area); |
| 10751 | } |
| 10752 | else if (ref->key_size_bits == 256) { |
| 10753 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10754 | sizeof(struct tls_sess_key_256), |
| 10755 | t2->area, t2->size); |
| 10756 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10757 | t2->area); |
| 10758 | } |
| 10759 | else { |
| 10760 | /* This case should never happen */ |
| 10761 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 10762 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10763 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10764 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10765 | /* let's try again later from this stream. We add ourselves into |
| 10766 | * this stream's users so that it can remove us upon termination. |
| 10767 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10768 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10769 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10770 | return 0; |
| 10771 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10772 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10773 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10774 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10775 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10776 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10777 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10778 | /* let's try again later from this stream. We add ourselves into |
| 10779 | * this stream's users so that it can remove us upon termination. |
| 10780 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10781 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10782 | return 0; |
| 10783 | } |
| 10784 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10785 | 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] | 10786 | break; |
| 10787 | |
| 10788 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10789 | 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] | 10790 | } |
| 10791 | |
| 10792 | appctx->st2 = STAT_ST_FIN; |
| 10793 | /* fall through */ |
| 10794 | |
| 10795 | default: |
| 10796 | appctx->st2 = STAT_ST_FIN; |
| 10797 | return 1; |
| 10798 | } |
| 10799 | return 0; |
| 10800 | } |
| 10801 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10802 | /* 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] | 10803 | 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] | 10804 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10805 | /* no parameter, shows only file list */ |
| 10806 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10807 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10808 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10809 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10810 | } |
| 10811 | |
| 10812 | if (args[2][0] == '*') { |
| 10813 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10814 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10815 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10816 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10817 | if (!appctx->ctx.cli.p0) |
| 10818 | 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] | 10819 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10820 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10821 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10822 | } |
| 10823 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10824 | 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] | 10825 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10826 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10827 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10828 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10829 | /* 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] | 10830 | if (!*args[3] || !*args[4]) |
| 10831 | 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] | 10832 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10833 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10834 | if (!ref) |
| 10835 | 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] | 10836 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10837 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10838 | if (ret < 0) |
| 10839 | 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] | 10840 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10841 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10842 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 10843 | 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] | 10844 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10845 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10846 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10847 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10848 | |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 10849 | /* |
| 10850 | * Take an ssl_bind_conf structure and append the configuration line used to |
| 10851 | * create it in the buffer |
| 10852 | */ |
| 10853 | static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf) |
| 10854 | { |
| 10855 | int space = 0; |
| 10856 | |
| 10857 | if (conf == NULL) |
| 10858 | return; |
| 10859 | |
| 10860 | chunk_appendf(buf, " ["); |
| 10861 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 10862 | if (conf->npn_str) { |
| 10863 | int len = conf->npn_len; |
| 10864 | char *ptr = conf->npn_str; |
| 10865 | int comma = 0; |
| 10866 | |
| 10867 | if (space) chunk_appendf(buf, " "); |
| 10868 | chunk_appendf(buf, "npn "); |
| 10869 | while (len) { |
| 10870 | unsigned short size; |
| 10871 | |
| 10872 | size = *ptr; |
| 10873 | ptr++; |
| 10874 | if (comma) |
| 10875 | chunk_memcat(buf, ",", 1); |
| 10876 | chunk_memcat(buf, ptr, size); |
| 10877 | ptr += size; |
| 10878 | len -= size + 1; |
| 10879 | comma = 1; |
| 10880 | } |
| 10881 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10882 | space++; |
| 10883 | } |
| 10884 | #endif |
| 10885 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 10886 | if (conf->alpn_str) { |
| 10887 | int len = conf->alpn_len; |
| 10888 | char *ptr = conf->alpn_str; |
| 10889 | int comma = 0; |
| 10890 | |
| 10891 | if (space) chunk_appendf(buf, " "); |
| 10892 | chunk_appendf(buf, "alpn "); |
| 10893 | while (len) { |
| 10894 | unsigned short size; |
| 10895 | |
| 10896 | size = *ptr; |
| 10897 | ptr++; |
| 10898 | if (comma) |
| 10899 | chunk_memcat(buf, ",", 1); |
| 10900 | chunk_memcat(buf, ptr, size); |
| 10901 | ptr += size; |
| 10902 | len -= size + 1; |
| 10903 | comma = 1; |
| 10904 | } |
| 10905 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10906 | space++; |
| 10907 | } |
| 10908 | #endif |
| 10909 | /* verify */ |
| 10910 | { |
| 10911 | if (conf->verify == SSL_SOCK_VERIFY_NONE) { |
| 10912 | if (space) chunk_appendf(buf, " "); |
| 10913 | chunk_appendf(buf, "verify none"); |
| 10914 | space++; |
| 10915 | } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) { |
| 10916 | if (space) chunk_appendf(buf, " "); |
| 10917 | chunk_appendf(buf, "verify optional"); |
| 10918 | space++; |
| 10919 | } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 10920 | if (space) chunk_appendf(buf, " "); |
| 10921 | chunk_appendf(buf, "verify required"); |
| 10922 | space++; |
| 10923 | } |
| 10924 | } |
| 10925 | |
| 10926 | if (conf->no_ca_names) { |
| 10927 | if (space) chunk_appendf(buf, " "); |
| 10928 | chunk_appendf(buf, "no-ca-names"); |
| 10929 | space++; |
| 10930 | } |
| 10931 | |
| 10932 | if (conf->early_data) { |
| 10933 | if (space) chunk_appendf(buf, " "); |
| 10934 | chunk_appendf(buf, "allow-0rtt"); |
| 10935 | space++; |
| 10936 | } |
| 10937 | if (conf->ca_file) { |
| 10938 | if (space) chunk_appendf(buf, " "); |
| 10939 | chunk_appendf(buf, "ca-file %s", conf->ca_file); |
| 10940 | space++; |
| 10941 | } |
| 10942 | if (conf->crl_file) { |
| 10943 | if (space) chunk_appendf(buf, " "); |
| 10944 | chunk_appendf(buf, "crl-file %s", conf->crl_file); |
| 10945 | space++; |
| 10946 | } |
| 10947 | if (conf->ciphers) { |
| 10948 | if (space) chunk_appendf(buf, " "); |
| 10949 | chunk_appendf(buf, "ciphers %s", conf->ciphers); |
| 10950 | space++; |
| 10951 | } |
| 10952 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 10953 | if (conf->ciphersuites) { |
| 10954 | if (space) chunk_appendf(buf, " "); |
| 10955 | chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites); |
| 10956 | space++; |
| 10957 | } |
| 10958 | #endif |
| 10959 | if (conf->curves) { |
| 10960 | if (space) chunk_appendf(buf, " "); |
| 10961 | chunk_appendf(buf, "curves %s", conf->curves); |
| 10962 | space++; |
| 10963 | } |
| 10964 | if (conf->ecdhe) { |
| 10965 | if (space) chunk_appendf(buf, " "); |
| 10966 | chunk_appendf(buf, "ecdhe %s", conf->ecdhe); |
| 10967 | space++; |
| 10968 | } |
| 10969 | |
| 10970 | /* the crt-lists only support ssl-min-ver and ssl-max-ver */ |
| 10971 | /* XXX: this part need to be revamp so we don't dump the default settings */ |
| 10972 | if (conf->ssl_methods.min) { |
| 10973 | if (space) chunk_appendf(buf, " "); |
| 10974 | chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name); |
| 10975 | space++; |
| 10976 | } |
| 10977 | |
| 10978 | if (conf->ssl_methods.max) { |
| 10979 | if (space) chunk_appendf(buf, " "); |
| 10980 | chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name); |
| 10981 | space++; |
| 10982 | } |
| 10983 | |
| 10984 | chunk_appendf(buf, "] "); |
| 10985 | |
| 10986 | return; |
| 10987 | } |
| 10988 | |
| 10989 | /* dump a list of filters */ |
| 10990 | static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry) |
| 10991 | { |
| 10992 | int space = 0; |
| 10993 | int i; |
| 10994 | |
| 10995 | if (!entry->fcount) |
| 10996 | return; |
| 10997 | |
| 10998 | for (i = 0; i < entry->fcount; i++) { |
| 10999 | if (space) |
| 11000 | chunk_appendf(buf, " "); |
| 11001 | chunk_appendf(buf, "%s", entry->filters[i]); |
| 11002 | space = 1; |
| 11003 | } |
| 11004 | return; |
| 11005 | } |
| 11006 | |
| 11007 | /* CLI IO handler for '(show|dump) ssl crt-list' */ |
| 11008 | static int cli_io_handler_dump_crtlist(struct appctx *appctx) |
| 11009 | { |
| 11010 | struct buffer *trash = alloc_trash_chunk(); |
| 11011 | struct stream_interface *si = appctx->owner; |
| 11012 | struct ebmb_node *lnode; |
| 11013 | |
| 11014 | if (trash == NULL) |
| 11015 | return 1; |
| 11016 | |
| 11017 | /* dump the list of crt-lists */ |
| 11018 | lnode = appctx->ctx.cli.p1; |
| 11019 | if (lnode == NULL) |
| 11020 | lnode = ebmb_first(&crtlists_tree); |
| 11021 | while (lnode) { |
| 11022 | chunk_appendf(trash, "%s\n", lnode->key); |
| 11023 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11024 | si_rx_room_blk(si); |
| 11025 | goto yield; |
| 11026 | } |
| 11027 | lnode = ebmb_next(lnode); |
| 11028 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11029 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11030 | return 1; |
| 11031 | yield: |
| 11032 | appctx->ctx.cli.p1 = lnode; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11033 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11034 | return 0; |
| 11035 | } |
| 11036 | |
| 11037 | /* CLI IO handler for '(show|dump) ssl crt-list <filename>' */ |
| 11038 | static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx) |
| 11039 | { |
| 11040 | struct buffer *trash = alloc_trash_chunk(); |
| 11041 | struct crtlist *crtlist; |
| 11042 | struct stream_interface *si = appctx->owner; |
| 11043 | struct crtlist_entry *entry; |
| 11044 | |
| 11045 | if (trash == NULL) |
| 11046 | return 1; |
| 11047 | |
| 11048 | crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node); |
| 11049 | |
| 11050 | entry = appctx->ctx.cli.p1; |
| 11051 | if (entry == NULL) { |
| 11052 | entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist); |
| 11053 | chunk_appendf(trash, "# %s\n", crtlist->node.key); |
| 11054 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11055 | si_rx_room_blk(si); |
| 11056 | goto yield; |
| 11057 | } |
| 11058 | } |
| 11059 | |
| 11060 | list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) { |
| 11061 | struct ckch_store *store; |
| 11062 | const char *filename; |
| 11063 | |
| 11064 | store = entry->node.key; |
| 11065 | filename = store->path; |
| 11066 | if (appctx->ctx.cli.i0 == 's') /* show */ |
| 11067 | chunk_appendf(trash, "%p ", entry); |
| 11068 | chunk_appendf(trash, "%s", filename); |
| 11069 | dump_crtlist_sslconf(trash, entry->ssl_conf); |
| 11070 | dump_crtlist_filters(trash, entry); |
| 11071 | chunk_appendf(trash, "\n"); |
| 11072 | |
| 11073 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11074 | si_rx_room_blk(si); |
| 11075 | goto yield; |
| 11076 | } |
| 11077 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11078 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11079 | return 1; |
| 11080 | yield: |
| 11081 | appctx->ctx.cli.p1 = entry; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11082 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11083 | return 0; |
| 11084 | } |
| 11085 | |
| 11086 | /* CLI argument parser for '(show|dump) ssl crt-list' */ |
| 11087 | static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11088 | { |
| 11089 | struct ebmb_node *lnode; |
| 11090 | int mode; |
| 11091 | |
| 11092 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11093 | return 1; |
| 11094 | |
| 11095 | appctx->ctx.cli.p0 = NULL; |
| 11096 | appctx->ctx.cli.p1 = NULL; |
| 11097 | mode = (int)args[0][0]; /* 'd' or 's' */ |
| 11098 | |
| 11099 | if (mode == 'd' && !*args[3]) |
| 11100 | return cli_err(appctx, "'dump ssl crt-list' expects a filename or a directory\n"); |
| 11101 | |
| 11102 | if (*args[3]) { |
| 11103 | lnode = ebst_lookup(&crtlists_tree, args[3]); |
| 11104 | if (lnode == NULL) |
| 11105 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 11106 | |
| 11107 | appctx->ctx.cli.p0 = lnode; |
| 11108 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 11109 | } |
| 11110 | appctx->ctx.cli.i0 = mode; |
| 11111 | |
| 11112 | return 0; |
| 11113 | } |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11114 | |
| 11115 | /* Type of SSL payloads that can be updated over the CLI */ |
| 11116 | |
| 11117 | enum { |
| 11118 | CERT_TYPE_PEM = 0, |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11119 | CERT_TYPE_KEY, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11120 | #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] | 11121 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11122 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11123 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11124 | #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] | 11125 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11126 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11127 | CERT_TYPE_MAX, |
| 11128 | }; |
| 11129 | |
| 11130 | struct { |
| 11131 | const char *ext; |
| 11132 | int type; |
| 11133 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 11134 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11135 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11136 | [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] | 11137 | [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] | 11138 | #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] | 11139 | [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] | 11140 | #endif |
| 11141 | #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] | 11142 | [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] | 11143 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11144 | [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] | 11145 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11146 | }; |
| 11147 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11148 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 11149 | enum { |
| 11150 | SETCERT_ST_INIT = 0, |
| 11151 | SETCERT_ST_GEN, |
| 11152 | SETCERT_ST_INSERT, |
| 11153 | SETCERT_ST_FIN, |
| 11154 | }; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11155 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11156 | /* release function of the `show ssl cert' command */ |
| 11157 | static void cli_release_show_cert(struct appctx *appctx) |
| 11158 | { |
| 11159 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11160 | } |
| 11161 | |
| 11162 | /* IO handler of "show ssl cert <filename>" */ |
| 11163 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 11164 | { |
| 11165 | struct buffer *trash = alloc_trash_chunk(); |
| 11166 | struct ebmb_node *node; |
| 11167 | struct stream_interface *si = appctx->owner; |
| 11168 | struct ckch_store *ckchs; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11169 | |
| 11170 | if (trash == NULL) |
| 11171 | return 1; |
| 11172 | |
| 11173 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11174 | if (ckchs_transaction.old_ckchs) { |
| 11175 | ckchs = ckchs_transaction.old_ckchs; |
| 11176 | chunk_appendf(trash, "# transaction\n"); |
| 11177 | if (!ckchs->multi) { |
| 11178 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11179 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11180 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11181 | int n; |
| 11182 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11183 | chunk_appendf(trash, "*%s:", ckchs->path); |
| 11184 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11185 | if (ckchs->ckch[n].cert) |
| 11186 | chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11187 | } |
| 11188 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11189 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11190 | } |
| 11191 | } |
| 11192 | } |
| 11193 | |
| 11194 | if (!appctx->ctx.cli.p0) { |
| 11195 | chunk_appendf(trash, "# filename\n"); |
| 11196 | node = ebmb_first(&ckchs_tree); |
| 11197 | } else { |
| 11198 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 11199 | } |
| 11200 | while (node) { |
| 11201 | ckchs = ebmb_entry(node, struct ckch_store, node); |
| 11202 | if (!ckchs->multi) { |
| 11203 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11204 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11205 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11206 | int n; |
| 11207 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11208 | chunk_appendf(trash, "%s:", ckchs->path); |
| 11209 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11210 | if (ckchs->ckch[n].cert) |
| 11211 | chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11212 | } |
| 11213 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11214 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | node = ebmb_next(node); |
| 11218 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11219 | si_rx_room_blk(si); |
| 11220 | goto yield; |
| 11221 | } |
| 11222 | } |
| 11223 | |
| 11224 | appctx->ctx.cli.p0 = NULL; |
| 11225 | free_trash_chunk(trash); |
| 11226 | return 1; |
| 11227 | yield: |
| 11228 | |
| 11229 | free_trash_chunk(trash); |
| 11230 | appctx->ctx.cli.p0 = ckchs; |
| 11231 | return 0; /* should come back */ |
| 11232 | } |
| 11233 | |
| 11234 | /* IO handler of the details "show ssl cert <filename>" */ |
| 11235 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 11236 | { |
| 11237 | struct stream_interface *si = appctx->owner; |
| 11238 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 11239 | struct buffer *out = alloc_trash_chunk(); |
| 11240 | struct buffer *tmp = alloc_trash_chunk(); |
| 11241 | X509_NAME *name = NULL; |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11242 | STACK_OF(X509) *chain; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11243 | unsigned int len = 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11244 | int write = -1; |
| 11245 | BIO *bio = NULL; |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11246 | int i; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11247 | |
| 11248 | if (!tmp || !out) |
| 11249 | goto end; |
| 11250 | |
| 11251 | if (!ckchs->multi) { |
| 11252 | chunk_appendf(out, "Filename: "); |
| 11253 | if (ckchs == ckchs_transaction.new_ckchs) |
| 11254 | chunk_appendf(out, "*"); |
| 11255 | chunk_appendf(out, "%s\n", ckchs->path); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11256 | |
| 11257 | chain = ckchs->ckch->chain; |
| 11258 | if (chain == NULL) { |
| 11259 | struct issuer_chain *issuer; |
| 11260 | issuer = ssl_get_issuer_chain(ckchs->ckch->cert); |
| 11261 | if (issuer) { |
| 11262 | chain = issuer->chain; |
| 11263 | chunk_appendf(out, "Chain Filename: "); |
| 11264 | chunk_appendf(out, "%s\n", issuer->path); |
| 11265 | } |
| 11266 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11267 | chunk_appendf(out, "Serial: "); |
| 11268 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 11269 | goto end; |
| 11270 | dump_binary(out, tmp->area, tmp->data); |
| 11271 | chunk_appendf(out, "\n"); |
| 11272 | |
| 11273 | chunk_appendf(out, "notBefore: "); |
| 11274 | chunk_reset(tmp); |
| 11275 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11276 | goto end; |
| 11277 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 11278 | goto end; |
| 11279 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 11280 | tmp->area[write] = '\0'; |
| 11281 | BIO_free(bio); |
| 11282 | chunk_appendf(out, "%s\n", tmp->area); |
| 11283 | |
| 11284 | chunk_appendf(out, "notAfter: "); |
| 11285 | chunk_reset(tmp); |
| 11286 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11287 | goto end; |
| 11288 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 11289 | goto end; |
| 11290 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 11291 | goto end; |
| 11292 | tmp->area[write] = '\0'; |
| 11293 | BIO_free(bio); |
| 11294 | chunk_appendf(out, "%s\n", tmp->area); |
| 11295 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11296 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11297 | chunk_appendf(out, "Subject Alternative Name: "); |
| 11298 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 11299 | goto end; |
| 11300 | *(out->area + out->data) = '\0'; |
| 11301 | chunk_appendf(out, "\n"); |
| 11302 | #endif |
| 11303 | chunk_reset(tmp); |
| 11304 | chunk_appendf(out, "Algorithm: "); |
| 11305 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 11306 | goto end; |
| 11307 | chunk_appendf(out, "%s\n", tmp->area); |
| 11308 | |
| 11309 | chunk_reset(tmp); |
| 11310 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11311 | 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] | 11312 | goto end; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11313 | tmp->data = len; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11314 | dump_binary(out, tmp->area, tmp->data); |
| 11315 | chunk_appendf(out, "\n"); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11316 | |
William Lallemand | a90e593 | 2020-02-25 14:07:58 +0100 | [diff] [blame] | 11317 | chunk_appendf(out, "Subject: "); |
| 11318 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 11319 | goto end; |
| 11320 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11321 | goto end; |
| 11322 | *(tmp->area + tmp->data) = '\0'; |
| 11323 | chunk_appendf(out, "%s\n", tmp->area); |
| 11324 | |
| 11325 | chunk_appendf(out, "Issuer: "); |
| 11326 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 11327 | goto end; |
| 11328 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11329 | goto end; |
| 11330 | *(tmp->area + tmp->data) = '\0'; |
| 11331 | chunk_appendf(out, "%s\n", tmp->area); |
| 11332 | |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11333 | /* Displays subject of each certificate in the chain */ |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11334 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 11335 | X509 *ca = sk_X509_value(chain, i); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11336 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11337 | chunk_appendf(out, "Chain Subject: "); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11338 | if ((name = X509_get_subject_name(ca)) == NULL) |
| 11339 | goto end; |
| 11340 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11341 | goto end; |
| 11342 | *(tmp->area + tmp->data) = '\0'; |
| 11343 | chunk_appendf(out, "%s\n", tmp->area); |
| 11344 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11345 | chunk_appendf(out, "Chain Issuer: "); |
| 11346 | if ((name = X509_get_issuer_name(ca)) == NULL) |
| 11347 | goto end; |
| 11348 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11349 | goto end; |
| 11350 | *(tmp->area + tmp->data) = '\0'; |
| 11351 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11352 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11353 | } |
| 11354 | |
| 11355 | if (ci_putchk(si_ic(si), out) == -1) { |
| 11356 | si_rx_room_blk(si); |
| 11357 | goto yield; |
| 11358 | } |
| 11359 | |
| 11360 | end: |
| 11361 | free_trash_chunk(tmp); |
| 11362 | free_trash_chunk(out); |
| 11363 | return 1; |
| 11364 | yield: |
| 11365 | free_trash_chunk(tmp); |
| 11366 | free_trash_chunk(out); |
| 11367 | return 0; /* should come back */ |
| 11368 | } |
| 11369 | |
| 11370 | /* parsing function for 'show ssl cert [certfile]' */ |
| 11371 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11372 | { |
| 11373 | struct ckch_store *ckchs; |
| 11374 | |
| 11375 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 11376 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 11377 | |
| 11378 | /* The operations on the CKCH architecture are locked so we can |
| 11379 | * manipulate ckch_store and ckch_inst */ |
| 11380 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11381 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 11382 | |
| 11383 | /* check if there is a certificate to lookup */ |
| 11384 | if (*args[3]) { |
| 11385 | if (*args[3] == '*') { |
| 11386 | if (!ckchs_transaction.new_ckchs) |
| 11387 | goto error; |
| 11388 | |
| 11389 | ckchs = ckchs_transaction.new_ckchs; |
| 11390 | |
| 11391 | if (strcmp(args[3] + 1, ckchs->path)) |
| 11392 | goto error; |
| 11393 | |
| 11394 | } else { |
| 11395 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 11396 | goto error; |
| 11397 | |
| 11398 | } |
| 11399 | |
| 11400 | if (ckchs->multi) |
| 11401 | goto error; |
| 11402 | |
| 11403 | appctx->ctx.cli.p0 = ckchs; |
| 11404 | /* use the IO handler that shows details */ |
| 11405 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 11406 | } |
| 11407 | |
| 11408 | return 0; |
| 11409 | |
| 11410 | error: |
| 11411 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11412 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 11413 | } |
| 11414 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11415 | /* 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] | 11416 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11417 | { |
| 11418 | struct ckch_store *new_ckchs; |
| 11419 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11420 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11421 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11422 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11423 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11424 | /* 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] | 11425 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11426 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11427 | if (!new_ckchs) |
| 11428 | return; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11429 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11430 | /* if the allocation failed, we need to free everything from the temporary list */ |
| 11431 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 11432 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11433 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11434 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 11435 | if (sc0->order == 0) /* we only free if it's the first inserted */ |
| 11436 | SSL_CTX_free(sc0->ctx); |
| 11437 | LIST_DEL(&sc0->by_ckch_inst); |
| 11438 | free(sc0); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11439 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11440 | LIST_DEL(&ckchi->by_ckchs); |
| 11441 | free(ckchi); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11442 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11443 | ckchs_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11444 | } |
| 11445 | } |
| 11446 | |
| 11447 | |
| 11448 | /* |
| 11449 | * This function tries to create the new ckch_inst and their SNIs |
| 11450 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11451 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11452 | { |
| 11453 | struct stream_interface *si = appctx->owner; |
| 11454 | int y = 0; |
| 11455 | char *err = NULL; |
| 11456 | int errcode = 0; |
| 11457 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 11458 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11459 | struct buffer *trash = alloc_trash_chunk(); |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11460 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11461 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 11462 | if (trash == NULL) |
| 11463 | goto error; |
| 11464 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11465 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 11466 | goto error; |
| 11467 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11468 | while (1) { |
| 11469 | switch (appctx->st2) { |
| 11470 | case SETCERT_ST_INIT: |
| 11471 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11472 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11473 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11474 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11475 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11476 | } |
| 11477 | appctx->st2 = SETCERT_ST_GEN; |
| 11478 | /* fallthrough */ |
| 11479 | case SETCERT_ST_GEN: |
| 11480 | /* |
| 11481 | * This state generates the ckch instances with their |
| 11482 | * sni_ctxs and SSL_CTX. |
| 11483 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11484 | * Since the SSL_CTX generation can be CPU consumer, we |
| 11485 | * yield every 10 instances. |
| 11486 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11487 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11488 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11489 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11490 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11491 | if (!new_ckchs) |
| 11492 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11493 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11494 | /* get the next ckchi to regenerate */ |
| 11495 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 11496 | /* we didn't start yet, set it to the first elem */ |
| 11497 | if (ckchi == NULL) |
| 11498 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11499 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11500 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11501 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11502 | struct ckch_inst *new_inst; |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11503 | char **sni_filter = NULL; |
| 11504 | int fcount = 0; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11505 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11506 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11507 | if (y >= 10) { |
| 11508 | /* save the next ckchi to compute */ |
| 11509 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 11510 | goto yield; |
| 11511 | } |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 11512 | if (ckchi->filters) { |
| 11513 | errcode |= ckch_inst_sni_ctx_to_sni_filters(ckchi, &sni_filter, &fcount, &err); |
| 11514 | if (errcode & ERR_CODE) |
| 11515 | goto error; |
| 11516 | } |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11517 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11518 | if (new_ckchs->multi) |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11519 | 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] | 11520 | else |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11521 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
| 11522 | |
| 11523 | free_sni_filters(sni_filter, fcount); |
| 11524 | sni_filter = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11525 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11526 | if (errcode & ERR_CODE) |
| 11527 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11528 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 11529 | /* if the previous ckchi was used as the default */ |
| 11530 | if (ckchi->is_default) |
| 11531 | new_inst->is_default = 1; |
| 11532 | |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11533 | /* we need to initialize the SSL_CTX generated */ |
William Lallemand | 696f317 | 2020-02-07 20:45:24 +0100 | [diff] [blame] | 11534 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 11535 | 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] | 11536 | 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] | 11537 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 11538 | if (errcode & ERR_CODE) |
| 11539 | goto error; |
| 11540 | } |
| 11541 | } |
| 11542 | |
| 11543 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11544 | /* display one dot per new instance */ |
| 11545 | chunk_appendf(trash, "."); |
| 11546 | /* link the new ckch_inst to the duplicate */ |
| 11547 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11548 | y++; |
| 11549 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11550 | appctx->st2 = SETCERT_ST_INSERT; |
| 11551 | /* fallthrough */ |
| 11552 | case SETCERT_ST_INSERT: |
| 11553 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11554 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11555 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11556 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11557 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11558 | if (!new_ckchs) |
| 11559 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11560 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 11561 | /* 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] | 11562 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 11563 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11564 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 11565 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11566 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11567 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11568 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 11569 | 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] | 11570 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11571 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11572 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 11573 | ebmb_delete(&sc0->name); |
| 11574 | LIST_DEL(&sc0->by_ckch_inst); |
| 11575 | free(sc0); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11576 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11577 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 11578 | LIST_DEL(&ckchi->by_ckchs); |
| 11579 | free(ckchi); |
| 11580 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11581 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11582 | /* Replace the old ckchs by the new one */ |
| 11583 | ebmb_delete(&old_ckchs->node); |
| 11584 | ckchs_free(old_ckchs); |
| 11585 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11586 | appctx->st2 = SETCERT_ST_FIN; |
| 11587 | /* fallthrough */ |
| 11588 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11589 | /* we achieved the transaction, we can set everything to NULL */ |
| 11590 | free(ckchs_transaction.path); |
| 11591 | ckchs_transaction.path = NULL; |
| 11592 | ckchs_transaction.new_ckchs = NULL; |
| 11593 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11594 | goto end; |
| 11595 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11596 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11597 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11598 | |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 11599 | chunk_appendf(trash, "\n"); |
| 11600 | if (errcode & ERR_WARN) |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 11601 | chunk_appendf(trash, "%s", err); |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 11602 | chunk_appendf(trash, "Success!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11603 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11604 | si_rx_room_blk(si); |
| 11605 | free_trash_chunk(trash); |
| 11606 | /* success: call the release function and don't come back */ |
| 11607 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11608 | yield: |
| 11609 | /* store the state */ |
| 11610 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11611 | si_rx_room_blk(si); |
| 11612 | free_trash_chunk(trash); |
| 11613 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11614 | return 0; /* should come back */ |
| 11615 | |
| 11616 | error: |
| 11617 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 11618 | if (trash) { |
| 11619 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 11620 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11621 | si_rx_room_blk(si); |
| 11622 | free_trash_chunk(trash); |
| 11623 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11624 | /* error: call the release function and don't come back */ |
| 11625 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11626 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11627 | |
| 11628 | /* |
| 11629 | * Parsing function of 'commit ssl cert' |
| 11630 | */ |
| 11631 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11632 | { |
| 11633 | char *err = NULL; |
| 11634 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11635 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11636 | return 1; |
| 11637 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11638 | if (!*args[3]) |
| 11639 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 11640 | |
| 11641 | /* The operations on the CKCH architecture are locked so we can |
| 11642 | * manipulate ckch_store and ckch_inst */ |
| 11643 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11644 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 11645 | |
| 11646 | if (!ckchs_transaction.path) { |
| 11647 | memprintf(&err, "No ongoing transaction! !\n"); |
| 11648 | goto error; |
| 11649 | } |
| 11650 | |
| 11651 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11652 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 11653 | goto error; |
| 11654 | } |
| 11655 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11656 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11657 | if (ckchs_transaction.new_ckchs->multi) { |
| 11658 | int n; |
| 11659 | |
| 11660 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11661 | 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)) { |
| 11662 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11663 | goto error; |
| 11664 | } |
| 11665 | } |
| 11666 | } else |
| 11667 | #endif |
| 11668 | { |
| 11669 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 11670 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11671 | goto error; |
| 11672 | } |
| 11673 | } |
| 11674 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11675 | /* init the appctx structure */ |
| 11676 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11677 | appctx->ctx.ssl.next_ckchi = NULL; |
| 11678 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 11679 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 11680 | |
| 11681 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 11682 | return 0; |
| 11683 | |
| 11684 | error: |
| 11685 | |
| 11686 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11687 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 11688 | |
| 11689 | return cli_dynerr(appctx, err); |
| 11690 | } |
| 11691 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11692 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11693 | * 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] | 11694 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11695 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11696 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 11697 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11698 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11699 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11700 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11701 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11702 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11703 | char *end; |
| 11704 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11705 | struct cert_key_and_chain *ckch; |
| 11706 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11707 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11708 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11709 | return 1; |
| 11710 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11711 | if ((buf = alloc_trash_chunk()) == NULL) |
| 11712 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11713 | |
| 11714 | if (!*args[3] || !payload) |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 11715 | 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] | 11716 | |
| 11717 | /* The operations on the CKCH architecture are locked so we can |
| 11718 | * manipulate ckch_store and ckch_inst */ |
| 11719 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11720 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 11721 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11722 | if (!chunk_strcpy(buf, args[3])) { |
| 11723 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11724 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11725 | goto end; |
| 11726 | } |
| 11727 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11728 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11729 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11730 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11731 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 11732 | *end = '\0'; |
| 11733 | type = cert_exts[i].type; |
| 11734 | break; |
| 11735 | } |
| 11736 | } |
| 11737 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11738 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11739 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11740 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11741 | /* if there is an ongoing transaction */ |
| 11742 | if (ckchs_transaction.path) { |
| 11743 | /* 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] | 11744 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11745 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11746 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11747 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11748 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11749 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11750 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11751 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11752 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11753 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11754 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11755 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11756 | break; |
| 11757 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11758 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11759 | if (bundle < 0) { |
| 11760 | 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); |
| 11761 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11762 | goto end; |
| 11763 | } |
| 11764 | } |
| 11765 | #endif |
| 11766 | |
| 11767 | /* if there is an ongoing transaction, check if this is the same file */ |
| 11768 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 11769 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 11770 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11771 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11772 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11773 | |
| 11774 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 11775 | |
| 11776 | } else { |
| 11777 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 11778 | |
| 11779 | /* lookup for the certificate in the tree: |
| 11780 | * check if this is used as a bundle AND as a unique certificate */ |
| 11781 | for (i = 0; i < 2; i++) { |
| 11782 | |
| 11783 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 11784 | /* only the bundle name is in the tree and you should |
| 11785 | * never update a bundle name, only a filename */ |
| 11786 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 11787 | /* we tried to look for a non-bundle and we found a bundle */ |
| 11788 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 11789 | err ? err : "", args[3], args[3]); |
| 11790 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11791 | goto end; |
| 11792 | } |
William Lallemand | 3246d94 | 2019-11-04 14:02:11 +0100 | [diff] [blame] | 11793 | /* If we want a bundle but this is not a bundle |
| 11794 | * example: When you try to update <file>.rsa, but |
| 11795 | * <file> is a regular file */ |
| 11796 | if (bundle >= 0 && find_ckchs[i]->multi == 0) { |
| 11797 | find_ckchs[i] = NULL; |
| 11798 | break; |
| 11799 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11800 | } |
| 11801 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11802 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11803 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11804 | int j; |
| 11805 | |
| 11806 | /* check if it was used in a bundle by removing the |
| 11807 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 11808 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11809 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11810 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11811 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11812 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11813 | break; |
| 11814 | } |
| 11815 | } |
William Lallemand | 37031b8 | 2019-11-04 13:38:53 +0100 | [diff] [blame] | 11816 | if (bundle < 0) /* we didn't find a bundle extension */ |
| 11817 | break; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11818 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11819 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11820 | /* bundles are not supported here, so we don't need to lookup again */ |
| 11821 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11822 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11823 | } |
| 11824 | |
| 11825 | if (find_ckchs[0] && find_ckchs[1]) { |
| 11826 | 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", |
| 11827 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 11828 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11829 | goto end; |
| 11830 | } |
| 11831 | |
| 11832 | 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] | 11833 | } |
| 11834 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11835 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11836 | 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] | 11837 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11838 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11839 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11840 | } |
| 11841 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame] | 11842 | if (!appctx->ctx.ssl.path) { |
| 11843 | /* this is a new transaction, set the path of the transaction */ |
| 11844 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 11845 | if (!appctx->ctx.ssl.path) { |
| 11846 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11847 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11848 | goto end; |
| 11849 | } |
| 11850 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11851 | |
| 11852 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11853 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11854 | /* duplicate the ckch store */ |
| 11855 | new_ckchs = ckchs_dup(old_ckchs); |
| 11856 | if (!new_ckchs) { |
| 11857 | memprintf(&err, "%sCannot allocate memory!\n", |
| 11858 | err ? err : ""); |
| 11859 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11860 | goto end; |
| 11861 | } |
| 11862 | |
| 11863 | if (!new_ckchs->multi) |
| 11864 | ckch = new_ckchs->ckch; |
| 11865 | else |
| 11866 | ckch = &new_ckchs->ckch[bundle]; |
| 11867 | |
| 11868 | /* appply the change on the duplicate */ |
| 11869 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 11870 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11871 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11872 | goto end; |
| 11873 | } |
| 11874 | |
| 11875 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 11876 | |
| 11877 | /* we succeed, we can save the ckchs in the transaction */ |
| 11878 | |
| 11879 | /* if there wasn't a transaction, update the old ckchs */ |
William Dauchy | c8bb153 | 2019-11-24 15:04:20 +0100 | [diff] [blame] | 11880 | if (!ckchs_transaction.old_ckchs) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11881 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11882 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 11883 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 11884 | } else { |
| 11885 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 11886 | |
| 11887 | } |
| 11888 | |
| 11889 | /* free the previous ckchs if there was a transaction */ |
| 11890 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11891 | |
| 11892 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 11893 | |
| 11894 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11895 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11896 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11897 | end: |
| 11898 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11899 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11900 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11901 | |
| 11902 | ckchs_free(appctx->ctx.ssl.new_ckchs); |
| 11903 | appctx->ctx.ssl.new_ckchs = NULL; |
| 11904 | |
| 11905 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11906 | |
| 11907 | free(appctx->ctx.ssl.path); |
| 11908 | appctx->ctx.ssl.path = NULL; |
| 11909 | |
| 11910 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11911 | 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] | 11912 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11913 | |
| 11914 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11915 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11916 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11917 | /* 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] | 11918 | } |
| 11919 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11920 | /* parsing function of 'abort ssl cert' */ |
| 11921 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11922 | { |
| 11923 | char *err = NULL; |
| 11924 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11925 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11926 | return 1; |
| 11927 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11928 | if (!*args[3]) |
| 11929 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 11930 | |
| 11931 | /* The operations on the CKCH architecture are locked so we can |
| 11932 | * manipulate ckch_store and ckch_inst */ |
| 11933 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11934 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 11935 | |
| 11936 | if (!ckchs_transaction.path) { |
| 11937 | memprintf(&err, "No ongoing transaction!\n"); |
| 11938 | goto error; |
| 11939 | } |
| 11940 | |
| 11941 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11942 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 11943 | goto error; |
| 11944 | } |
| 11945 | |
| 11946 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 11947 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11948 | ckchs_transaction.new_ckchs = NULL; |
| 11949 | ckchs_free(ckchs_transaction.old_ckchs); |
| 11950 | ckchs_transaction.old_ckchs = NULL; |
| 11951 | free(ckchs_transaction.path); |
| 11952 | ckchs_transaction.path = NULL; |
| 11953 | |
| 11954 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11955 | |
| 11956 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 11957 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 11958 | |
| 11959 | error: |
| 11960 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11961 | |
| 11962 | return cli_dynerr(appctx, err); |
| 11963 | } |
| 11964 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 11965 | 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] | 11966 | { |
| 11967 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 11968 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11969 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 11970 | |
| 11971 | if (!payload) |
| 11972 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11973 | |
| 11974 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11975 | if (!*payload) |
| 11976 | 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] | 11977 | |
| 11978 | /* remove \r and \n from the payload */ |
| 11979 | for (i = 0, j = 0; payload[i]; i++) { |
| 11980 | if (payload[i] == '\r' || payload[i] == '\n') |
| 11981 | continue; |
| 11982 | payload[j++] = payload[i]; |
| 11983 | } |
| 11984 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11985 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11986 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11987 | if (ret < 0) |
| 11988 | 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] | 11989 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11990 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11991 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11992 | if (err) |
| 11993 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 11994 | else |
| 11995 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11996 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11997 | |
| 11998 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11999 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12000 | 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] | 12001 | #endif |
| 12002 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12003 | } |
| 12004 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12005 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12006 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 12007 | { |
| 12008 | switch (arg->type) { |
| 12009 | case ARGT_STR: |
| 12010 | smp->data.type = SMP_T_STR; |
| 12011 | smp->data.u.str = arg->data.str; |
| 12012 | return 1; |
| 12013 | case ARGT_VAR: |
| 12014 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 12015 | return 0; |
| 12016 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 12017 | return 0; |
| 12018 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 12019 | return 0; |
| 12020 | return 1; |
| 12021 | default: |
| 12022 | return 0; |
| 12023 | } |
| 12024 | } |
| 12025 | |
| 12026 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 12027 | const char *file, int line, char **err) |
| 12028 | { |
| 12029 | switch(args[0].data.sint) { |
| 12030 | case 128: |
| 12031 | case 192: |
| 12032 | case 256: |
| 12033 | break; |
| 12034 | default: |
| 12035 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 12036 | return 0; |
| 12037 | } |
| 12038 | /* Try to decode a variable. */ |
| 12039 | vars_check_arg(&args[1], NULL); |
| 12040 | vars_check_arg(&args[2], NULL); |
| 12041 | vars_check_arg(&args[3], NULL); |
| 12042 | return 1; |
| 12043 | } |
| 12044 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12045 | /* 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] | 12046 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 12047 | { |
| 12048 | struct sample nonce, key, aead_tag; |
| 12049 | struct buffer *smp_trash, *smp_trash_alloc; |
| 12050 | EVP_CIPHER_CTX *ctx; |
| 12051 | int dec_size, ret; |
| 12052 | |
| 12053 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 12054 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 12055 | return 0; |
| 12056 | |
| 12057 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 12058 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 12059 | return 0; |
| 12060 | |
| 12061 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 12062 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 12063 | return 0; |
| 12064 | |
| 12065 | smp_trash = get_trash_chunk(); |
| 12066 | smp_trash_alloc = alloc_trash_chunk(); |
| 12067 | if (!smp_trash_alloc) |
| 12068 | return 0; |
| 12069 | |
| 12070 | ctx = EVP_CIPHER_CTX_new(); |
| 12071 | |
| 12072 | if (!ctx) |
| 12073 | goto err; |
| 12074 | |
| 12075 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12076 | if (dec_size < 0) |
| 12077 | goto err; |
| 12078 | smp_trash->data = dec_size; |
| 12079 | |
| 12080 | /* Set cipher type and mode */ |
| 12081 | switch(arg_p[0].data.sint) { |
| 12082 | case 128: |
| 12083 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 12084 | break; |
| 12085 | case 192: |
| 12086 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 12087 | break; |
| 12088 | case 256: |
| 12089 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 12090 | break; |
| 12091 | } |
| 12092 | |
| 12093 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 12094 | |
| 12095 | /* Initialise IV */ |
| 12096 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 12097 | goto err; |
| 12098 | |
| 12099 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12100 | if (dec_size < 0) |
| 12101 | goto err; |
| 12102 | smp_trash->data = dec_size; |
| 12103 | |
| 12104 | /* Initialise key */ |
| 12105 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 12106 | goto err; |
| 12107 | |
| 12108 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 12109 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 12110 | goto err; |
| 12111 | |
| 12112 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 12113 | if (dec_size < 0) |
| 12114 | goto err; |
| 12115 | smp_trash_alloc->data = dec_size; |
| 12116 | dec_size = smp_trash->data; |
| 12117 | |
| 12118 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 12119 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 12120 | |
| 12121 | if (ret <= 0) |
| 12122 | goto err; |
| 12123 | |
| 12124 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 12125 | smp->data.u.str.area = smp_trash->area; |
| 12126 | smp->data.type = SMP_T_BIN; |
| 12127 | smp->flags &= ~SMP_F_CONST; |
| 12128 | free_trash_chunk(smp_trash_alloc); |
| 12129 | return 1; |
| 12130 | |
| 12131 | err: |
| 12132 | free_trash_chunk(smp_trash_alloc); |
| 12133 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12134 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12135 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12136 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12137 | /* Argument validation functions */ |
| 12138 | |
| 12139 | /* This function is used to validate the arguments passed to any "x_dn" ssl |
| 12140 | * keywords. These keywords support specifying a third parameter that must be |
| 12141 | * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK. |
| 12142 | */ |
| 12143 | int val_dnfmt(struct arg *arg, char **err_msg) |
| 12144 | { |
| 12145 | if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) { |
| 12146 | memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument."); |
| 12147 | return 0; |
| 12148 | } |
| 12149 | return 1; |
| 12150 | } |
| 12151 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12152 | /* register cli keywords */ |
| 12153 | static struct cli_kw_list cli_kws = {{ },{ |
| 12154 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12155 | { { "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] | 12156 | { { "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] | 12157 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 12158 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12159 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 12160 | { { "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] | 12161 | { { "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] | 12162 | { { "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] | 12163 | { { "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 }, |
| 12164 | { { "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] | 12165 | { { NULL }, NULL, NULL, NULL } |
| 12166 | }}; |
| 12167 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12168 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12169 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12170 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12171 | * Please take care of keeping this list alphabetically sorted. |
| 12172 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12173 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12174 | { "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] | 12175 | { "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] | 12176 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 12177 | { "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] | 12178 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12179 | { "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] | 12180 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 12181 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 12182 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 12183 | { "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] | 12184 | { "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] | 12185 | { "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] | 12186 | { "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] | 12187 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12188 | { "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] | 12189 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12190 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12191 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12192 | { "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] | 12193 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12194 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12195 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12196 | { "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] | 12197 | { "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] | 12198 | { "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] | 12199 | { "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] | 12200 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12201 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12202 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12203 | { "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] | 12204 | { "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] | 12205 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12206 | { "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] | 12207 | { "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] | 12208 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12209 | { "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] | 12210 | { "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] | 12211 | { "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] | 12212 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12213 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12214 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12215 | { "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] | 12216 | { "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] | 12217 | { "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] | 12218 | { "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] | 12219 | { "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] | 12220 | { "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] | 12221 | { "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] | 12222 | { "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] | 12223 | { "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] | 12224 | { "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] | 12225 | { "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] | 12226 | { "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] | 12227 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12228 | { "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] | 12229 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 12230 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12231 | { "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] | 12232 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12233 | { "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] | 12234 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 12235 | { "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] | 12236 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12237 | { "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] | 12238 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12239 | { "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] | 12240 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12241 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12242 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12243 | { "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] | 12244 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12245 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 12246 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12247 | { "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] | 12248 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12249 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12250 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12251 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12252 | { "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] | 12253 | { NULL, NULL, 0, 0, 0 }, |
| 12254 | }}; |
| 12255 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12256 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 12257 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12258 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12259 | * Please take care of keeping this list alphabetically sorted. |
| 12260 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12261 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12262 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 12263 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12264 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12265 | }}; |
| 12266 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12267 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 12268 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12269 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12270 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12271 | * all code contributors. |
| 12272 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12273 | * the config parser can report an appropriate error when a known keyword was |
| 12274 | * not enabled. |
| 12275 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12276 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12277 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12278 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12279 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12280 | { "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] | 12281 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12282 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12283 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12284 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12285 | { "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] | 12286 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12287 | { "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] | 12288 | { "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] | 12289 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 12290 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 12291 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12292 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12293 | { NULL, NULL, 0 }, |
| 12294 | }; |
| 12295 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12296 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 12297 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 12298 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12299 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12300 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12301 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12302 | { "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] | 12303 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 12304 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 12305 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 12306 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12307 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12308 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12309 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12310 | { "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] | 12311 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12312 | { "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] | 12313 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 12314 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 12315 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 12316 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 12317 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 12318 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 12319 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12320 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12321 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 12322 | { "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] | 12323 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 12324 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 12325 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 12326 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12327 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12328 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 12329 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12330 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 12331 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12332 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 12333 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 12334 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12335 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 12336 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12337 | { NULL, NULL, 0 }, |
| 12338 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12339 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12340 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 12341 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12342 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12343 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12344 | * all code contributors. |
| 12345 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12346 | * the config parser can report an appropriate error when a known keyword was |
| 12347 | * not enabled. |
| 12348 | */ |
| 12349 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 12350 | { "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] | 12351 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12352 | { "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] | 12353 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 12354 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12355 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 12356 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the 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", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 12359 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12360 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 12361 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 12362 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 12363 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 12364 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 12365 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 12366 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 12367 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 12368 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 12369 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 12370 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 12371 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 12372 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 12373 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 12374 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 12375 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 12376 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 12377 | { "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] | 12378 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12379 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 12380 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 12381 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 12382 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 12383 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 12384 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 12385 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 12386 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 12387 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 12388 | { "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] | 12389 | { NULL, NULL, 0, 0 }, |
| 12390 | }}; |
| 12391 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12392 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 12393 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12394 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 12395 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 12396 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 12397 | { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 12398 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12399 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 12400 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 12401 | #ifndef OPENSSL_NO_DH |
| 12402 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 12403 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 12404 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12405 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12406 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12407 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 12408 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 12409 | #ifndef OPENSSL_NO_DH |
| 12410 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 12411 | #endif |
| 12412 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 12413 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 12414 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 12415 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12416 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 12417 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 12418 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12419 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12420 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12421 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12422 | #endif |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 12423 | { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12424 | { 0, NULL, NULL }, |
| 12425 | }}; |
| 12426 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12427 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12428 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12429 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12430 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12431 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12432 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 12433 | #endif |
| 12434 | { NULL, NULL, 0, 0, 0 }, |
| 12435 | }}; |
| 12436 | |
| 12437 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 12438 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 12439 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 12440 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12441 | .snd_buf = ssl_sock_from_buf, |
| 12442 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 12443 | .subscribe = ssl_subscribe, |
| 12444 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 12445 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 12446 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12447 | .rcv_pipe = NULL, |
| 12448 | .snd_pipe = NULL, |
| 12449 | .shutr = NULL, |
| 12450 | .shutw = ssl_sock_shutw, |
| 12451 | .close = ssl_sock_close, |
| 12452 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 12453 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 12454 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 12455 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 12456 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 12457 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 12458 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12459 | }; |
| 12460 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12461 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 12462 | struct session *sess, struct stream *s, int flags) |
| 12463 | { |
| 12464 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12465 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12466 | |
| 12467 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12468 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12469 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 12470 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12471 | 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] | 12472 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 12473 | s->req.flags |= CF_READ_NULL; |
| 12474 | return ACT_RET_YIELD; |
| 12475 | } |
| 12476 | } |
| 12477 | return (ACT_RET_CONT); |
| 12478 | } |
| 12479 | |
| 12480 | 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) |
| 12481 | { |
| 12482 | rule->action_ptr = ssl_action_wait_for_hs; |
| 12483 | |
| 12484 | return ACT_RET_PRS_OK; |
| 12485 | } |
| 12486 | |
| 12487 | static struct action_kw_list http_req_actions = {ILH, { |
| 12488 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 12489 | { /* END */ } |
| 12490 | }}; |
| 12491 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12492 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 12493 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12494 | #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] | 12495 | |
| 12496 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 12497 | { |
| 12498 | if (ptr) { |
| 12499 | chunk_destroy(ptr); |
| 12500 | free(ptr); |
| 12501 | } |
| 12502 | } |
| 12503 | |
| 12504 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 12505 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 12506 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12507 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 12508 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 12509 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12510 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12511 | static void __ssl_sock_init(void) |
| 12512 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12513 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12514 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12515 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12516 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12517 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 12518 | if (global_ssl.listen_default_ciphers) |
| 12519 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 12520 | if (global_ssl.connect_default_ciphers) |
| 12521 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12522 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12523 | if (global_ssl.listen_default_ciphersuites) |
| 12524 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 12525 | if (global_ssl.connect_default_ciphersuites) |
| 12526 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 12527 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 12528 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 12529 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 12530 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12531 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12532 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12533 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12534 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12535 | n = sk_SSL_COMP_num(cm); |
| 12536 | while (n--) { |
| 12537 | (void) sk_SSL_COMP_pop(cm); |
| 12538 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 12539 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 12540 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12541 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12542 | ssl_locking_init(); |
| 12543 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12544 | #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] | 12545 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 12546 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 12547 | 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] | 12548 | 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] | 12549 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12550 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 12551 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12552 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 12553 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12554 | hap_register_post_check(tlskeys_finalize_config); |
| 12555 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12556 | |
| 12557 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 12558 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 12559 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 12560 | hap_register_post_deinit(ssl_free_global_issuers); |
| 12561 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12562 | #ifndef OPENSSL_NO_DH |
| 12563 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 12564 | hap_register_post_deinit(ssl_free_dh); |
| 12565 | #endif |
| 12566 | #ifndef OPENSSL_NO_ENGINE |
| 12567 | hap_register_post_deinit(ssl_free_engines); |
| 12568 | #endif |
| 12569 | /* Load SSL string for the verbose & debug mode. */ |
| 12570 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 12571 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 12572 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 12573 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 12574 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 12575 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 12576 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 12577 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 12578 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12579 | |
| 12580 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12581 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 12582 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12583 | /* Compute and register the version string */ |
| 12584 | static void ssl_register_build_options() |
| 12585 | { |
| 12586 | char *ptr = NULL; |
| 12587 | int i; |
| 12588 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12589 | memprintf(&ptr, "Built with OpenSSL version : " |
| 12590 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 12591 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12592 | #else /* OPENSSL_IS_BORINGSSL */ |
| 12593 | OPENSSL_VERSION_TEXT |
| 12594 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12595 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 12596 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12597 | #endif |
| 12598 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 12599 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12600 | "no (library version too old)" |
| 12601 | #elif defined(OPENSSL_NO_TLSEXT) |
| 12602 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 12603 | #else |
| 12604 | "yes" |
| 12605 | #endif |
| 12606 | "", ptr); |
| 12607 | |
| 12608 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 12609 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 12610 | "yes" |
| 12611 | #else |
| 12612 | #ifdef OPENSSL_NO_TLSEXT |
| 12613 | "no (because of OPENSSL_NO_TLSEXT)" |
| 12614 | #else |
| 12615 | "no (version might be too old, 0.9.8f min needed)" |
| 12616 | #endif |
| 12617 | #endif |
| 12618 | "", ptr); |
| 12619 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 12620 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 12621 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 12622 | if (methodVersions[i].option) |
| 12623 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 12624 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12625 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12626 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 12627 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12628 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 12629 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12630 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12631 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12632 | void ssl_free_engines(void) { |
| 12633 | struct ssl_engine_list *wl, *wlb; |
| 12634 | /* free up engine list */ |
| 12635 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 12636 | ENGINE_finish(wl->e); |
| 12637 | ENGINE_free(wl->e); |
| 12638 | LIST_DEL(&wl->list); |
| 12639 | free(wl); |
| 12640 | } |
| 12641 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12642 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 12643 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12644 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12645 | void ssl_free_dh(void) { |
| 12646 | if (local_dh_1024) { |
| 12647 | DH_free(local_dh_1024); |
| 12648 | local_dh_1024 = NULL; |
| 12649 | } |
| 12650 | if (local_dh_2048) { |
| 12651 | DH_free(local_dh_2048); |
| 12652 | local_dh_2048 = NULL; |
| 12653 | } |
| 12654 | if (local_dh_4096) { |
| 12655 | DH_free(local_dh_4096); |
| 12656 | local_dh_4096 = NULL; |
| 12657 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 12658 | if (global_dh) { |
| 12659 | DH_free(global_dh); |
| 12660 | global_dh = NULL; |
| 12661 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12662 | } |
| 12663 | #endif |
| 12664 | |
| 12665 | __attribute__((destructor)) |
| 12666 | static void __ssl_sock_deinit(void) |
| 12667 | { |
| 12668 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12669 | if (ssl_ctx_lru_tree) { |
| 12670 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 12671 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12672 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12673 | #endif |
| 12674 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12675 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12676 | ERR_remove_state(0); |
| 12677 | ERR_free_strings(); |
| 12678 | |
| 12679 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12680 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12681 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12682 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12683 | CRYPTO_cleanup_all_ex_data(); |
| 12684 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 12685 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12686 | } |
| 12687 | |
| 12688 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12689 | /* |
| 12690 | * Local variables: |
| 12691 | * c-indent-level: 8 |
| 12692 | * c-basic-offset: 8 |
| 12693 | * End: |
| 12694 | */ |