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 | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 164 | static struct issuer_chain* ssl_get0_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 | c3b7e74 | 2020-04-22 11:06:19 +0200 | [diff] [blame] | 170 | int skip_self_issued_ca; |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 171 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 172 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 173 | |
| 174 | char *listen_default_ciphers; |
| 175 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 176 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 177 | char *listen_default_ciphersuites; |
| 178 | char *connect_default_ciphersuites; |
| 179 | #endif |
Jerome Magnin | b203ff6 | 2020-04-03 15:28:22 +0200 | [diff] [blame] | 180 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
| 181 | char *listen_default_curves; |
| 182 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 183 | int listen_default_ssloptions; |
| 184 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 185 | struct tls_version_filter listen_default_sslmethods; |
| 186 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 187 | |
| 188 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 189 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 190 | unsigned int max_record; /* SSL max record size */ |
| 191 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 192 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 193 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 194 | 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] | 195 | } global_ssl = { |
| 196 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 197 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 198 | #endif |
| 199 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 200 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 201 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 202 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 203 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 204 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 205 | #endif |
| 206 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 207 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 208 | #endif |
| 209 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 210 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 211 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 212 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 213 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 214 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 215 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 216 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 217 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 218 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 219 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 220 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 221 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 222 | #endif |
| 223 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 224 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 225 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 226 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 227 | }; |
| 228 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 229 | static BIO_METHOD *ha_meth; |
| 230 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 231 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 232 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 233 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 234 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 235 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 236 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 237 | struct wait_event wait_event; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 238 | struct wait_event *subs; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 239 | int xprt_st; /* transport layer state, initialized to zero */ |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 240 | struct buffer early_buf; /* buffer to store the early data received */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 241 | int sent_early_data; /* Amount of early data we sent so far */ |
| 242 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 246 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 247 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 248 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 249 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 250 | /* Methods to implement OpenSSL BIO */ |
| 251 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 252 | { |
| 253 | struct buffer tmpbuf; |
| 254 | struct ssl_sock_ctx *ctx; |
| 255 | int ret; |
| 256 | |
| 257 | ctx = BIO_get_data(h); |
| 258 | tmpbuf.size = num; |
| 259 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 260 | tmpbuf.data = num; |
| 261 | tmpbuf.head = 0; |
| 262 | 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] | 263 | 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] | 264 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 265 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 266 | } else if (ret == 0) |
| 267 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 272 | { |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int ha_ssl_puts(BIO *h, const char *str) |
| 278 | { |
| 279 | |
| 280 | return ha_ssl_write(h, str, strlen(str)); |
| 281 | } |
| 282 | |
| 283 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 284 | { |
| 285 | struct buffer tmpbuf; |
| 286 | struct ssl_sock_ctx *ctx; |
| 287 | int ret; |
| 288 | |
| 289 | ctx = BIO_get_data(h); |
| 290 | tmpbuf.size = size; |
| 291 | tmpbuf.area = buf; |
| 292 | tmpbuf.data = 0; |
| 293 | tmpbuf.head = 0; |
| 294 | 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] | 295 | 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] | 296 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 297 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 298 | } else if (ret == 0) |
| 299 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 305 | { |
| 306 | int ret = 0; |
| 307 | switch (cmd) { |
| 308 | case BIO_CTRL_DUP: |
| 309 | case BIO_CTRL_FLUSH: |
| 310 | ret = 1; |
| 311 | break; |
| 312 | } |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int ha_ssl_new(BIO *h) |
| 317 | { |
| 318 | BIO_set_init(h, 1); |
| 319 | BIO_set_data(h, NULL); |
| 320 | BIO_clear_flags(h, ~0); |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | static int ha_ssl_free(BIO *data) |
| 325 | { |
| 326 | |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 331 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 332 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 333 | static HA_RWLOCK_T *ssl_rwlocks; |
| 334 | |
| 335 | |
| 336 | unsigned long ssl_id_function(void) |
| 337 | { |
| 338 | return (unsigned long)tid; |
| 339 | } |
| 340 | |
| 341 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 342 | { |
| 343 | if (mode & CRYPTO_LOCK) { |
| 344 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 345 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 346 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 347 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 348 | } |
| 349 | else { |
| 350 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 351 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 352 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 353 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | static int ssl_locking_init(void) |
| 358 | { |
| 359 | int i; |
| 360 | |
| 361 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 362 | if (!ssl_rwlocks) |
| 363 | return -1; |
| 364 | |
| 365 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 366 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 367 | |
| 368 | CRYPTO_set_id_callback(ssl_id_function); |
| 369 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 370 | |
| 371 | return 0; |
| 372 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 373 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 374 | #endif |
| 375 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 376 | __decl_hathreads(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 377 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 378 | /* Uncommitted CKCH transaction */ |
| 379 | |
| 380 | static struct { |
| 381 | struct ckch_store *new_ckchs; |
| 382 | struct ckch_store *old_ckchs; |
| 383 | char *path; |
| 384 | } ckchs_transaction; |
| 385 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 386 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 387 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 388 | */ |
| 389 | struct cafile_entry { |
| 390 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 391 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 392 | struct ebmb_node node; |
| 393 | char path[0]; |
| 394 | }; |
| 395 | |
| 396 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 397 | |
| 398 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 399 | { |
| 400 | struct ebmb_node *eb; |
| 401 | |
| 402 | eb = ebst_lookup(&cafile_tree, path); |
| 403 | if (eb) { |
| 404 | struct cafile_entry *ca_e; |
| 405 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 406 | return ca_e->ca_store; |
| 407 | } |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | static int ssl_store_load_locations_file(char *path) |
| 412 | { |
| 413 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 414 | struct cafile_entry *ca_e; |
| 415 | X509_STORE *store = X509_STORE_new(); |
| 416 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 417 | int pathlen; |
| 418 | pathlen = strlen(path); |
| 419 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 420 | if (ca_e) { |
| 421 | memcpy(ca_e->path, path, pathlen + 1); |
| 422 | ca_e->ca_store = store; |
| 423 | ebst_insert(&cafile_tree, &ca_e->node); |
| 424 | return 1; |
| 425 | } |
| 426 | } |
| 427 | X509_STORE_free(store); |
| 428 | return 0; |
| 429 | } |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 434 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 435 | { |
| 436 | X509_STORE *store; |
| 437 | store = ssl_store_get0_locations_file(path); |
| 438 | if (store_ctx && store) { |
| 439 | int i; |
| 440 | X509_OBJECT *obj; |
| 441 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 442 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 443 | obj = sk_X509_OBJECT_value(objs, i); |
| 444 | switch (X509_OBJECT_get_type(obj)) { |
| 445 | case X509_LU_X509: |
| 446 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 447 | break; |
| 448 | case X509_LU_CRL: |
| 449 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 450 | break; |
| 451 | default: |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | return 1; |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 460 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 461 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 462 | { |
| 463 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 464 | return ssl_set_cert_crl_file(store_ctx, path); |
| 465 | } |
| 466 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 467 | /* |
| 468 | Extract CA_list from CA_file already in tree. |
| 469 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 470 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 471 | */ |
| 472 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 473 | { |
| 474 | struct ebmb_node *eb; |
| 475 | struct cafile_entry *ca_e; |
| 476 | |
| 477 | eb = ebst_lookup(&cafile_tree, path); |
| 478 | if (!eb) |
| 479 | return NULL; |
| 480 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 481 | |
| 482 | if (ca_e->ca_list == NULL) { |
| 483 | int i; |
| 484 | unsigned long key; |
| 485 | struct eb_root ca_name_tree = EB_ROOT; |
| 486 | struct eb64_node *node, *back; |
| 487 | struct { |
| 488 | struct eb64_node node; |
| 489 | X509_NAME *xname; |
| 490 | } *ca_name; |
| 491 | STACK_OF(X509_OBJECT) *objs; |
| 492 | STACK_OF(X509_NAME) *skn; |
| 493 | X509 *x; |
| 494 | X509_NAME *xn; |
| 495 | |
| 496 | skn = sk_X509_NAME_new_null(); |
| 497 | /* take x509 from cafile_tree */ |
| 498 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 499 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 500 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 501 | if (!x) |
| 502 | continue; |
| 503 | xn = X509_get_subject_name(x); |
| 504 | if (!xn) |
| 505 | continue; |
| 506 | /* Check for duplicates. */ |
| 507 | key = X509_NAME_hash(xn); |
| 508 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 509 | node && ca_name == NULL; |
| 510 | node = eb64_next(node)) { |
| 511 | ca_name = container_of(node, typeof(*ca_name), node); |
| 512 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 513 | ca_name = NULL; |
| 514 | } |
| 515 | /* find a duplicate */ |
| 516 | if (ca_name) |
| 517 | continue; |
| 518 | ca_name = calloc(1, sizeof *ca_name); |
| 519 | xn = X509_NAME_dup(xn); |
| 520 | if (!ca_name || |
| 521 | !xn || |
| 522 | !sk_X509_NAME_push(skn, xn)) { |
| 523 | free(ca_name); |
| 524 | X509_NAME_free(xn); |
| 525 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 526 | sk_X509_NAME_free(skn); |
| 527 | skn = NULL; |
| 528 | break; |
| 529 | } |
| 530 | ca_name->node.key = key; |
| 531 | ca_name->xname = xn; |
| 532 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 533 | } |
| 534 | ca_e->ca_list = skn; |
| 535 | /* remove temporary ca_name tree */ |
| 536 | node = eb64_first(&ca_name_tree); |
| 537 | while (node) { |
| 538 | ca_name = container_of(node, typeof(*ca_name), node); |
| 539 | back = eb64_next(node); |
| 540 | eb64_delete(node); |
| 541 | free(ca_name); |
| 542 | node = back; |
| 543 | } |
| 544 | } |
| 545 | return ca_e->ca_list; |
| 546 | } |
| 547 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 548 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 549 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 550 | unsigned long long int xxh64; |
| 551 | unsigned char ciphersuite_len; |
| 552 | char ciphersuite[0]; |
| 553 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 554 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 555 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 556 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 557 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 558 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 559 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 560 | #endif |
| 561 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 562 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 563 | static unsigned int openssl_engines_initialized; |
| 564 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 565 | struct ssl_engine_list { |
| 566 | struct list list; |
| 567 | ENGINE *e; |
| 568 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 569 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 570 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 571 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 572 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 573 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 574 | static DH *local_dh_1024 = NULL; |
| 575 | static DH *local_dh_2048 = NULL; |
| 576 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 577 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 578 | #endif /* OPENSSL_NO_DH */ |
| 579 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 580 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 581 | /* X509V3 Extensions that will be added on generated certificates */ |
| 582 | #define X509V3_EXT_SIZE 5 |
| 583 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 584 | "basicConstraints", |
| 585 | "nsComment", |
| 586 | "subjectKeyIdentifier", |
| 587 | "authorityKeyIdentifier", |
| 588 | "keyUsage", |
| 589 | }; |
| 590 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 591 | "CA:FALSE", |
| 592 | "\"OpenSSL Generated Certificate\"", |
| 593 | "hash", |
| 594 | "keyid,issuer:always", |
| 595 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 596 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 597 | /* LRU cache to store generated certificate */ |
| 598 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 599 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 600 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 601 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 602 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 603 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 604 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 605 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 606 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 607 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 608 | /* The order here matters for picking a default context, |
| 609 | * keep the most common keytype at the bottom of the list |
| 610 | */ |
| 611 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 612 | "dsa", |
| 613 | "ecdsa", |
| 614 | "rsa" |
| 615 | }; |
| 616 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 617 | #else |
| 618 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 619 | #endif |
| 620 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 621 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 622 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 623 | |
| 624 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 625 | |
| 626 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 627 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 628 | |
| 629 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 630 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 631 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 632 | /* |
| 633 | * This function gives the detail of the SSL error. It is used only |
| 634 | * if the debug mode and the verbose mode are activated. It dump all |
| 635 | * the SSL error until the stack was empty. |
| 636 | */ |
| 637 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 638 | { |
| 639 | unsigned long ret; |
| 640 | |
| 641 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 642 | while(1) { |
| 643 | ret = ERR_get_error(); |
| 644 | if (ret == 0) |
| 645 | return; |
| 646 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 647 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 648 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 653 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 654 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 655 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 656 | { |
| 657 | int err_code = ERR_ABORT; |
| 658 | ENGINE *engine; |
| 659 | struct ssl_engine_list *el; |
| 660 | |
| 661 | /* grab the structural reference to the engine */ |
| 662 | engine = ENGINE_by_id(engine_id); |
| 663 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 664 | 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] | 665 | goto fail_get; |
| 666 | } |
| 667 | |
| 668 | if (!ENGINE_init(engine)) { |
| 669 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 670 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 671 | goto fail_init; |
| 672 | } |
| 673 | |
| 674 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 675 | 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] | 676 | goto fail_set_method; |
| 677 | } |
| 678 | |
| 679 | el = calloc(1, sizeof(*el)); |
| 680 | el->e = engine; |
| 681 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 682 | nb_engines++; |
| 683 | if (global_ssl.async) |
| 684 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 685 | return 0; |
| 686 | |
| 687 | fail_set_method: |
| 688 | /* release the functional reference from ENGINE_init() */ |
| 689 | ENGINE_finish(engine); |
| 690 | |
| 691 | fail_init: |
| 692 | /* release the structural reference from ENGINE_by_id() */ |
| 693 | ENGINE_free(engine); |
| 694 | |
| 695 | fail_get: |
| 696 | return err_code; |
| 697 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 698 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 699 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 700 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 701 | /* |
| 702 | * openssl async fd handler |
| 703 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 704 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 705 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 706 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 707 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 708 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 709 | * to poll this fd until it is requested |
| 710 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 711 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 712 | fd_cant_recv(fd); |
| 713 | |
| 714 | /* crypto engine is available, let's notify the associated |
| 715 | * connection that it can pursue its processing. |
| 716 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 717 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 720 | /* |
| 721 | * openssl async delayed SSL_free handler |
| 722 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 723 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 724 | { |
| 725 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 726 | OSSL_ASYNC_FD all_fd[32]; |
| 727 | size_t num_all_fds = 0; |
| 728 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 729 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 730 | /* We suppose that the async job for a same SSL * |
| 731 | * are serialized. So if we are awake it is |
| 732 | * because the running job has just finished |
| 733 | * and we can remove all async fds safely |
| 734 | */ |
| 735 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 736 | if (num_all_fds > 32) { |
| 737 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 742 | for (i=0 ; i < num_all_fds ; i++) |
| 743 | fd_remove(all_fd[i]); |
| 744 | |
| 745 | /* 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] | 746 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 747 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 748 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 749 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 750 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 751 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 752 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 753 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 754 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 755 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 756 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 757 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 758 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 759 | size_t num_add_fds = 0; |
| 760 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 761 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 762 | |
| 763 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 764 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 765 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 766 | 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] | 767 | return; |
| 768 | } |
| 769 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 770 | 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] | 771 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 772 | /* We remove unused fds from the fdtab */ |
| 773 | for (i=0 ; i < num_del_fds ; i++) |
| 774 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 775 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 776 | /* We add new fds to the fdtab */ |
| 777 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 778 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 781 | num_add_fds = 0; |
| 782 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 783 | if (num_add_fds > 32) { |
| 784 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 785 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 786 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 787 | |
| 788 | /* We activate the polling for all known async fds */ |
| 789 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 790 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 791 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 792 | /* To ensure that the fd cache won't be used |
| 793 | * We'll prefer to catch a real RD event |
| 794 | * because handling an EAGAIN on this fd will |
| 795 | * result in a context switch and also |
| 796 | * some engines uses a fd in blocking mode. |
| 797 | */ |
| 798 | fd_cant_recv(add_fd[i]); |
| 799 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 800 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 801 | } |
| 802 | #endif |
| 803 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 804 | #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] | 805 | /* |
| 806 | * This function returns the number of seconds elapsed |
| 807 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 808 | * date presented un ASN1_GENERALIZEDTIME. |
| 809 | * |
| 810 | * In parsing error case, it returns -1. |
| 811 | */ |
| 812 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 813 | { |
| 814 | long epoch; |
| 815 | char *p, *end; |
| 816 | const unsigned short month_offset[12] = { |
| 817 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 818 | }; |
| 819 | int year, month; |
| 820 | |
| 821 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 822 | |
| 823 | p = (char *)d->data; |
| 824 | end = p + d->length; |
| 825 | |
| 826 | if (end - p < 4) return -1; |
| 827 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 828 | p += 4; |
| 829 | if (end - p < 2) return -1; |
| 830 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 831 | if (month < 1 || month > 12) return -1; |
| 832 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 833 | We consider leap years and the current month (<marsh or not) */ |
| 834 | epoch = ( ((year - 1970) * 365) |
| 835 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 836 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 837 | + month_offset[month-1] |
| 838 | ) * 24 * 60 * 60; |
| 839 | p += 2; |
| 840 | if (end - p < 2) return -1; |
| 841 | /* Add the number of seconds of completed days of current month */ |
| 842 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 843 | p += 2; |
| 844 | if (end - p < 2) return -1; |
| 845 | /* Add the completed hours of the current day */ |
| 846 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 847 | p += 2; |
| 848 | if (end - p < 2) return -1; |
| 849 | /* Add the completed minutes of the current hour */ |
| 850 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 851 | p += 2; |
| 852 | if (p == end) return -1; |
| 853 | /* Test if there is available seconds */ |
| 854 | if (p[0] < '0' || p[0] > '9') |
| 855 | goto nosec; |
| 856 | if (end - p < 2) return -1; |
| 857 | /* Add the seconds of the current minute */ |
| 858 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 859 | p += 2; |
| 860 | if (p == end) return -1; |
| 861 | /* Ignore seconds float part if present */ |
| 862 | if (p[0] == '.') { |
| 863 | do { |
| 864 | if (++p == end) return -1; |
| 865 | } while (p[0] >= '0' && p[0] <= '9'); |
| 866 | } |
| 867 | |
| 868 | nosec: |
| 869 | if (p[0] == 'Z') { |
| 870 | if (end - p != 1) return -1; |
| 871 | return epoch; |
| 872 | } |
| 873 | else if (p[0] == '+') { |
| 874 | if (end - p != 5) return -1; |
| 875 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 876 | 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] | 877 | } |
| 878 | else if (p[0] == '-') { |
| 879 | if (end - p != 5) return -1; |
| 880 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 881 | 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] | 882 | } |
| 883 | |
| 884 | return -1; |
| 885 | } |
| 886 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 887 | /* |
| 888 | * struct alignment works here such that the key.key is the same as key_data |
| 889 | * Do not change the placement of key_data |
| 890 | */ |
| 891 | struct certificate_ocsp { |
| 892 | struct ebmb_node key; |
| 893 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 894 | struct buffer response; |
| 895 | long expire; |
| 896 | }; |
| 897 | |
| 898 | struct ocsp_cbk_arg { |
| 899 | int is_single; |
| 900 | int single_kt; |
| 901 | union { |
| 902 | struct certificate_ocsp *s_ocsp; |
| 903 | /* |
| 904 | * m_ocsp will have multiple entries dependent on key type |
| 905 | * Entry 0 - DSA |
| 906 | * Entry 1 - ECDSA |
| 907 | * Entry 2 - RSA |
| 908 | */ |
| 909 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 910 | }; |
| 911 | }; |
| 912 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 913 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 914 | |
| 915 | /* This function starts to check if the OCSP response (in DER format) contained |
| 916 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 917 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 918 | * contained in the OCSP Response and exits on error if no match. |
| 919 | * If it's a valid OCSP Response: |
| 920 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 921 | * pointed by 'ocsp'. |
| 922 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 923 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 924 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 925 | * already present in the container, it will be overwritten. |
| 926 | * |
| 927 | * Note: OCSP response containing more than one OCSP Single response is not |
| 928 | * considered valid. |
| 929 | * |
| 930 | * Returns 0 on success, 1 in error case. |
| 931 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 932 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 933 | struct certificate_ocsp *ocsp, |
| 934 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 935 | { |
| 936 | OCSP_RESPONSE *resp; |
| 937 | OCSP_BASICRESP *bs = NULL; |
| 938 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 939 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 940 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 941 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 942 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 943 | int reason; |
| 944 | int ret = 1; |
| 945 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 946 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 947 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 948 | if (!resp) { |
| 949 | memprintf(err, "Unable to parse OCSP response"); |
| 950 | goto out; |
| 951 | } |
| 952 | |
| 953 | rc = OCSP_response_status(resp); |
| 954 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 955 | memprintf(err, "OCSP response status not successful"); |
| 956 | goto out; |
| 957 | } |
| 958 | |
| 959 | bs = OCSP_response_get1_basic(resp); |
| 960 | if (!bs) { |
| 961 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | count_sr = OCSP_resp_count(bs); |
| 966 | if (count_sr > 1) { |
| 967 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 968 | goto out; |
| 969 | } |
| 970 | |
| 971 | sr = OCSP_resp_get0(bs, 0); |
| 972 | if (!sr) { |
| 973 | memprintf(err, "Failed to get OCSP single response"); |
| 974 | goto out; |
| 975 | } |
| 976 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 977 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 978 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 979 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 980 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 981 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 982 | goto out; |
| 983 | } |
| 984 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 985 | if (!nextupd) { |
| 986 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 987 | goto out; |
| 988 | } |
| 989 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 990 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 991 | if (!rc) { |
| 992 | memprintf(err, "OCSP single response: no longer valid."); |
| 993 | goto out; |
| 994 | } |
| 995 | |
| 996 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 997 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 998 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 999 | goto out; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | if (!ocsp) { |
| 1004 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 1005 | unsigned char *p; |
| 1006 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1007 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1008 | if (!rc) { |
| 1009 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 1010 | goto out; |
| 1011 | } |
| 1012 | |
| 1013 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 1014 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 1015 | goto out; |
| 1016 | } |
| 1017 | |
| 1018 | p = key; |
| 1019 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1020 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1021 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1022 | if (!ocsp) { |
| 1023 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 1024 | goto out; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* According to comments on "chunk_dup", the |
| 1029 | previous chunk buffer will be freed */ |
| 1030 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1031 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1032 | goto out; |
| 1033 | } |
| 1034 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1035 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1036 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1037 | ret = 0; |
| 1038 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1039 | ERR_clear_error(); |
| 1040 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1041 | if (bs) |
| 1042 | OCSP_BASICRESP_free(bs); |
| 1043 | |
| 1044 | if (resp) |
| 1045 | OCSP_RESPONSE_free(resp); |
| 1046 | |
| 1047 | return ret; |
| 1048 | } |
| 1049 | /* |
| 1050 | * External function use to update the OCSP response in the OCSP response's |
| 1051 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1052 | * to update in DER format. |
| 1053 | * |
| 1054 | * Returns 0 on success, 1 in error case. |
| 1055 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1056 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1057 | { |
| 1058 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1059 | } |
| 1060 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1061 | #endif |
| 1062 | |
| 1063 | #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] | 1064 | /* |
| 1065 | * 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] | 1066 | * path 'ocsp_path' or base64 in a buffer <buf> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1067 | * |
| 1068 | * Returns 0 on success, 1 in error case. |
| 1069 | */ |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1070 | 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] | 1071 | { |
| 1072 | int fd = -1; |
| 1073 | int r = 0; |
| 1074 | int ret = 1; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1075 | struct buffer *ocsp_response; |
| 1076 | struct buffer *src = NULL; |
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 | if (buf) { |
| 1079 | int i, j; |
| 1080 | /* if it's from a buffer it will be base64 */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1081 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1082 | /* remove \r and \n from the payload */ |
| 1083 | for (i = 0, j = 0; buf[i]; i++) { |
| 1084 | if (buf[i] == '\r' || buf[i] == '\n') |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1085 | continue; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1086 | buf[j++] = buf[i]; |
| 1087 | } |
| 1088 | buf[j] = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1089 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1090 | ret = base64dec(buf, j, trash.area, trash.size); |
| 1091 | if (ret < 0) { |
| 1092 | memprintf(err, "Error reading OCSP response in base64 format"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1093 | goto end; |
| 1094 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1095 | trash.data = ret; |
| 1096 | src = &trash; |
| 1097 | } else { |
| 1098 | fd = open(ocsp_path, O_RDONLY); |
| 1099 | if (fd == -1) { |
| 1100 | memprintf(err, "Error opening OCSP response file"); |
| 1101 | goto end; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1102 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1103 | |
| 1104 | trash.data = 0; |
| 1105 | while (trash.data < trash.size) { |
| 1106 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1107 | if (r < 0) { |
| 1108 | if (errno == EINTR) |
| 1109 | continue; |
| 1110 | |
| 1111 | memprintf(err, "Error reading OCSP response from file"); |
| 1112 | goto end; |
| 1113 | } |
| 1114 | else if (r == 0) { |
| 1115 | break; |
| 1116 | } |
| 1117 | trash.data += r; |
| 1118 | } |
| 1119 | close(fd); |
| 1120 | fd = -1; |
| 1121 | src = &trash; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1124 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 1125 | if (!chunk_dup(ocsp_response, src)) { |
| 1126 | free(ocsp_response); |
| 1127 | ocsp_response = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1128 | goto end; |
| 1129 | } |
Emmanuel Hocdet | 0667fae | 2020-01-16 14:41:36 +0100 | [diff] [blame] | 1130 | /* no error, fill ckch with new context, old context must be free */ |
| 1131 | if (ckch->ocsp_response) { |
| 1132 | free(ckch->ocsp_response->area); |
| 1133 | ckch->ocsp_response->area = NULL; |
| 1134 | free(ckch->ocsp_response); |
| 1135 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1136 | ckch->ocsp_response = ocsp_response; |
William Lallemand | e0f48ae | 2019-10-15 13:44:57 +0200 | [diff] [blame] | 1137 | ret = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1138 | end: |
| 1139 | if (fd != -1) |
| 1140 | close(fd); |
| 1141 | |
| 1142 | return ret; |
| 1143 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1144 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1145 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1146 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1147 | 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) |
| 1148 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1149 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1150 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1151 | struct connection *conn; |
| 1152 | int head; |
| 1153 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1154 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1155 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1156 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1157 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1158 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1159 | |
| 1160 | keys = ref->tlskeys; |
| 1161 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1162 | |
| 1163 | if (enc) { |
| 1164 | memcpy(key_name, keys[head].name, 16); |
| 1165 | |
| 1166 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1167 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1168 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1169 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1170 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1171 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1172 | goto end; |
| 1173 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1174 | 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] | 1175 | ret = 1; |
| 1176 | } |
| 1177 | else if (ref->key_size_bits == 256 ) { |
| 1178 | |
| 1179 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1180 | goto end; |
| 1181 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1182 | 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] | 1183 | ret = 1; |
| 1184 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1185 | } else { |
| 1186 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1187 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1188 | goto found; |
| 1189 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1190 | ret = 0; |
| 1191 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1192 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1193 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1194 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1195 | 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] | 1196 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1197 | goto end; |
| 1198 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1199 | ret = i ? 2 : 1; |
| 1200 | } |
| 1201 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1202 | 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] | 1203 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1204 | goto end; |
| 1205 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1206 | ret = i ? 2 : 1; |
| 1207 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1208 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1209 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1210 | end: |
| 1211 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1212 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1216 | { |
| 1217 | struct tls_keys_ref *ref; |
| 1218 | |
| 1219 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1220 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1221 | return ref; |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | |
| 1225 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1226 | { |
| 1227 | struct tls_keys_ref *ref; |
| 1228 | |
| 1229 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1230 | if (ref->unique_id == unique_id) |
| 1231 | return ref; |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1235 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1236 | * match existing ones, this function returns -1 |
| 1237 | * else it returns 0 on success. |
| 1238 | */ |
| 1239 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1240 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1241 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1242 | if (ref->key_size_bits == 128) { |
| 1243 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1244 | return -1; |
| 1245 | } |
| 1246 | else if (ref->key_size_bits == 256) { |
| 1247 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1248 | return -1; |
| 1249 | } |
| 1250 | else |
| 1251 | return -1; |
| 1252 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1253 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1254 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1255 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1256 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1257 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1258 | |
| 1259 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1262 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1263 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1264 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1265 | |
| 1266 | if(!ref) { |
| 1267 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1268 | return 1; |
| 1269 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1270 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1271 | memprintf(err, "Invalid key size"); |
| 1272 | return 1; |
| 1273 | } |
| 1274 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1275 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1276 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1277 | |
| 1278 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1279 | * automatic ids. It's called just after the basic checks. It returns |
| 1280 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1281 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1282 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1283 | { |
| 1284 | int i = 0; |
| 1285 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1286 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1287 | |
| 1288 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1289 | if (ref->unique_id == -1) { |
| 1290 | /* Look for the first free id. */ |
| 1291 | while (1) { |
| 1292 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1293 | if (ref2->unique_id == i) { |
| 1294 | i++; |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | if (&ref2->list == &tlskeys_reference) |
| 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | /* Uses the unique id and increment it for the next entry. */ |
| 1303 | ref->unique_id = i; |
| 1304 | i++; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /* This sort the reference list by id. */ |
| 1309 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1310 | LIST_DEL(&ref->list); |
| 1311 | list_for_each_entry(ref3, &tkr, list) { |
| 1312 | if (ref->unique_id < ref3->unique_id) { |
| 1313 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | if (&ref3->list == &tkr) |
| 1318 | LIST_ADDQ(&tkr, &ref->list); |
| 1319 | } |
| 1320 | |
| 1321 | /* swap root */ |
| 1322 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1323 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1324 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1325 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1326 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1327 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1328 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1329 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1330 | { |
| 1331 | switch (evp_keytype) { |
| 1332 | case EVP_PKEY_RSA: |
| 1333 | return 2; |
| 1334 | case EVP_PKEY_DSA: |
| 1335 | return 0; |
| 1336 | case EVP_PKEY_EC: |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
| 1340 | return -1; |
| 1341 | } |
| 1342 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1343 | /* |
| 1344 | * Callback used to set OCSP status extension content in server hello. |
| 1345 | */ |
| 1346 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1347 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1348 | struct certificate_ocsp *ocsp; |
| 1349 | struct ocsp_cbk_arg *ocsp_arg; |
| 1350 | char *ssl_buf; |
| 1351 | EVP_PKEY *ssl_pkey; |
| 1352 | int key_type; |
| 1353 | int index; |
| 1354 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1355 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1356 | |
| 1357 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1358 | if (!ssl_pkey) |
| 1359 | return SSL_TLSEXT_ERR_NOACK; |
| 1360 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1361 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1362 | |
| 1363 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1364 | ocsp = ocsp_arg->s_ocsp; |
| 1365 | else { |
| 1366 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1367 | * the certificate type |
| 1368 | */ |
| 1369 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1370 | |
| 1371 | if (index < 0) |
| 1372 | return SSL_TLSEXT_ERR_NOACK; |
| 1373 | |
| 1374 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1375 | |
| 1376 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1379 | !ocsp->response.area || |
| 1380 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1381 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1382 | return SSL_TLSEXT_ERR_NOACK; |
| 1383 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1384 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1385 | if (!ssl_buf) |
| 1386 | return SSL_TLSEXT_ERR_NOACK; |
| 1387 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1388 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1389 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1390 | |
| 1391 | return SSL_TLSEXT_ERR_OK; |
| 1392 | } |
| 1393 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1394 | #endif |
| 1395 | |
| 1396 | #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] | 1397 | /* |
| 1398 | * 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] | 1399 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1400 | * status extension, the issuer's certificate is mandatory. It should be |
| 1401 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1402 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1403 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1404 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1405 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1406 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1407 | * |
| 1408 | * 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] | 1409 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1410 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1411 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1412 | 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] | 1413 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1414 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1415 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1416 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1417 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1418 | char *warn = NULL; |
| 1419 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1420 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1421 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1422 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1423 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1424 | if (!x) |
| 1425 | goto out; |
| 1426 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1427 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1428 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1429 | if (chain) { |
| 1430 | /* check if one of the certificate of the chain is the issuer */ |
| 1431 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1432 | X509 *ti = sk_X509_value(chain, i); |
| 1433 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1434 | issuer = ti; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1439 | if (!issuer) |
| 1440 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1441 | |
| 1442 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1443 | if (!cid) |
| 1444 | goto out; |
| 1445 | |
| 1446 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1447 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1448 | goto out; |
| 1449 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1450 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1451 | if (!ocsp) |
| 1452 | goto out; |
| 1453 | |
| 1454 | p = ocsp->key_data; |
| 1455 | i2d_OCSP_CERTID(cid, &p); |
| 1456 | |
| 1457 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1458 | if (iocsp == ocsp) |
| 1459 | ocsp = NULL; |
| 1460 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1461 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1462 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1463 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1464 | #endif |
| 1465 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1466 | |
| 1467 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1468 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1469 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1470 | |
| 1471 | cb_arg->is_single = 1; |
| 1472 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1473 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1474 | pkey = X509_get_pubkey(x); |
| 1475 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1476 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1477 | |
| 1478 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1479 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1480 | } else { |
| 1481 | /* |
| 1482 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1483 | * Update that cb_arg with the new cert's staple |
| 1484 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1485 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1486 | struct certificate_ocsp *tmp_ocsp; |
| 1487 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1488 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1489 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1490 | |
| 1491 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1492 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1493 | #else |
| 1494 | cb_arg = ctx->tlsext_status_arg; |
| 1495 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1496 | |
| 1497 | /* |
| 1498 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1499 | * the order of operations below matter, take care when changing it |
| 1500 | */ |
| 1501 | tmp_ocsp = cb_arg->s_ocsp; |
| 1502 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1503 | cb_arg->s_ocsp = NULL; |
| 1504 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1505 | cb_arg->is_single = 0; |
| 1506 | cb_arg->single_kt = 0; |
| 1507 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1508 | pkey = X509_get_pubkey(x); |
| 1509 | key_type = EVP_PKEY_base_id(pkey); |
| 1510 | EVP_PKEY_free(pkey); |
| 1511 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1512 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1513 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1514 | cb_arg->m_ocsp[index] = iocsp; |
| 1515 | |
| 1516 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1517 | |
| 1518 | ret = 0; |
| 1519 | |
| 1520 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1521 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1522 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1523 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1527 | if (cid) |
| 1528 | OCSP_CERTID_free(cid); |
| 1529 | |
| 1530 | if (ocsp) |
| 1531 | free(ocsp); |
| 1532 | |
| 1533 | if (warn) |
| 1534 | free(warn); |
| 1535 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1536 | return ret; |
| 1537 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1538 | #else /* OPENSSL_IS_BORINGSSL */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1539 | 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] | 1540 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1541 | 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] | 1542 | } |
| 1543 | #endif |
| 1544 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1545 | #endif |
| 1546 | |
| 1547 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1548 | #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] | 1549 | |
| 1550 | #define CT_EXTENSION_TYPE 18 |
| 1551 | |
| 1552 | static int sctl_ex_index = -1; |
| 1553 | |
| 1554 | /* |
| 1555 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1556 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1557 | * is performed. |
| 1558 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1559 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1560 | { |
| 1561 | int ret = 1; |
| 1562 | int len, pos, sct_len; |
| 1563 | unsigned char *data; |
| 1564 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1565 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1566 | goto out; |
| 1567 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1568 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1569 | len = (data[0] << 8) | data[1]; |
| 1570 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1571 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1572 | goto out; |
| 1573 | |
| 1574 | data = data + 2; |
| 1575 | pos = 0; |
| 1576 | while (pos < len) { |
| 1577 | if (len - pos < 2) |
| 1578 | goto out; |
| 1579 | |
| 1580 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1581 | if (pos + sct_len + 2 > len) |
| 1582 | goto out; |
| 1583 | |
| 1584 | pos += sct_len + 2; |
| 1585 | } |
| 1586 | |
| 1587 | ret = 0; |
| 1588 | |
| 1589 | out: |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1593 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 1594 | * It fills the ckch->sctl buffer |
| 1595 | * return 0 on success or != 0 on failure */ |
| 1596 | 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] | 1597 | { |
| 1598 | int fd = -1; |
| 1599 | int r = 0; |
| 1600 | int ret = 1; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1601 | struct buffer tmp; |
| 1602 | struct buffer *src; |
| 1603 | struct buffer *sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1604 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1605 | if (buf) { |
| 1606 | tmp.area = buf; |
| 1607 | tmp.data = strlen(buf); |
| 1608 | tmp.size = tmp.data + 1; |
| 1609 | src = &tmp; |
| 1610 | } else { |
| 1611 | fd = open(sctl_path, O_RDONLY); |
| 1612 | if (fd == -1) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1613 | goto end; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1614 | |
| 1615 | trash.data = 0; |
| 1616 | while (trash.data < trash.size) { |
| 1617 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1618 | if (r < 0) { |
| 1619 | if (errno == EINTR) |
| 1620 | continue; |
| 1621 | goto end; |
| 1622 | } |
| 1623 | else if (r == 0) { |
| 1624 | break; |
| 1625 | } |
| 1626 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1627 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1628 | src = &trash; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1629 | } |
| 1630 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1631 | ret = ssl_sock_parse_sctl(src); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1632 | if (ret) |
| 1633 | goto end; |
| 1634 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1635 | sctl = calloc(1, sizeof(*sctl)); |
| 1636 | if (!chunk_dup(sctl, src)) { |
| 1637 | free(sctl); |
| 1638 | sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1639 | goto end; |
| 1640 | } |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1641 | /* no error, fill ckch with new context, old context must be free */ |
| 1642 | if (ckch->sctl) { |
| 1643 | free(ckch->sctl->area); |
| 1644 | ckch->sctl->area = NULL; |
| 1645 | free(ckch->sctl); |
| 1646 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1647 | ckch->sctl = sctl; |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1648 | ret = 0; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1649 | end: |
| 1650 | if (fd != -1) |
| 1651 | close(fd); |
| 1652 | |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1657 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1658 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1659 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1660 | *out = (unsigned char *) sctl->area; |
| 1661 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1662 | |
| 1663 | return 1; |
| 1664 | } |
| 1665 | |
| 1666 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1667 | { |
| 1668 | return 1; |
| 1669 | } |
| 1670 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1671 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1672 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1673 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1674 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1675 | 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] | 1676 | goto out; |
| 1677 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1678 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1679 | |
| 1680 | ret = 0; |
| 1681 | |
| 1682 | out: |
| 1683 | return ret; |
| 1684 | } |
| 1685 | |
| 1686 | #endif |
| 1687 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1688 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1689 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1690 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1691 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1692 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1693 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1694 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1695 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1696 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1697 | * change this to #if and do not assign a default value to this macro! |
| 1698 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1699 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1700 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1701 | 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] | 1702 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1703 | conn->err_code = CO_ER_SSL_RENEG; |
| 1704 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1705 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1706 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1707 | |
| 1708 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1709 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1710 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1711 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1712 | consider that the buffering was activated, |
| 1713 | so we rise the output buffer size from 4k |
| 1714 | to 16k */ |
| 1715 | write_bio = SSL_get_wbio(ssl); |
| 1716 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1717 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1718 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1724 | /* Callback is called for each certificate of the chain during a verify |
| 1725 | ok is set to 1 if preverify detect no error on current certificate. |
| 1726 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1727 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1728 | { |
| 1729 | SSL *ssl; |
| 1730 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1731 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1732 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1733 | |
| 1734 | 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] | 1735 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1736 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1737 | ctx = conn->xprt_ctx; |
| 1738 | |
| 1739 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1740 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1741 | if (ok) /* no errors */ |
| 1742 | return ok; |
| 1743 | |
| 1744 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1745 | err = X509_STORE_CTX_get_error(x_store); |
| 1746 | |
| 1747 | /* check if CA error needs to be ignored */ |
| 1748 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1749 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1750 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1751 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1754 | 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] | 1755 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1756 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1757 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1758 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1759 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1760 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1761 | return 0; |
| 1762 | } |
| 1763 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1764 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1765 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1766 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1767 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1768 | 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] | 1769 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1770 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1771 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1772 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1773 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1774 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1775 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1778 | static inline |
| 1779 | 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] | 1780 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1781 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1782 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1783 | unsigned char *msg; |
| 1784 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1785 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1786 | |
| 1787 | /* This function is called for "from client" and "to server" |
| 1788 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1789 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1790 | */ |
| 1791 | |
| 1792 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1793 | * otherwise it is set to 1. |
| 1794 | */ |
| 1795 | if (write_p != 0) |
| 1796 | return; |
| 1797 | |
| 1798 | /* content_type contains the type of message received or sent |
| 1799 | * according with the SSL/TLS protocol spec. This message is |
| 1800 | * encoded with one byte. The value 256 (two bytes) is used |
| 1801 | * for designing the SSL/TLS record layer. According with the |
| 1802 | * rfc6101, the expected message (other than 256) are: |
| 1803 | * - change_cipher_spec(20) |
| 1804 | * - alert(21) |
| 1805 | * - handshake(22) |
| 1806 | * - application_data(23) |
| 1807 | * - (255) |
| 1808 | * We are interessed by the handshake and specially the client |
| 1809 | * hello. |
| 1810 | */ |
| 1811 | if (content_type != 22) |
| 1812 | return; |
| 1813 | |
| 1814 | /* The message length is at least 4 bytes, containing the |
| 1815 | * message type and the message length. |
| 1816 | */ |
| 1817 | if (len < 4) |
| 1818 | return; |
| 1819 | |
| 1820 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1821 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1822 | * - hello_request(0) |
| 1823 | * - client_hello(1) |
| 1824 | * - server_hello(2) |
| 1825 | * - certificate(11) |
| 1826 | * - server_key_exchange (12) |
| 1827 | * - certificate_request(13) |
| 1828 | * - server_hello_done(14) |
| 1829 | * We are interested by the client hello. |
| 1830 | */ |
| 1831 | msg = (unsigned char *)buf; |
| 1832 | if (msg[0] != 1) |
| 1833 | return; |
| 1834 | |
| 1835 | /* Next three bytes are the length of the message. The total length |
| 1836 | * must be this decoded length + 4. If the length given as argument |
| 1837 | * is not the same, we abort the protocol dissector. |
| 1838 | */ |
| 1839 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1840 | if (len < rec_len + 4) |
| 1841 | return; |
| 1842 | msg += 4; |
| 1843 | end = msg + rec_len; |
| 1844 | if (end < msg) |
| 1845 | return; |
| 1846 | |
| 1847 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1848 | * 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] | 1849 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1850 | */ |
| 1851 | msg += 1 + 1 + 4 + 28; |
| 1852 | if (msg > end) |
| 1853 | return; |
| 1854 | |
| 1855 | /* Next, is session id: |
| 1856 | * if present, we have to jump by length + 1 for the size information |
| 1857 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1858 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1859 | if (msg[0] > 0) |
| 1860 | msg += msg[0]; |
| 1861 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1862 | if (msg > end) |
| 1863 | return; |
| 1864 | |
| 1865 | /* Next two bytes are the ciphersuite length. */ |
| 1866 | if (msg + 2 > end) |
| 1867 | return; |
| 1868 | rec_len = (msg[0] << 8) + msg[1]; |
| 1869 | msg += 2; |
| 1870 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1871 | return; |
| 1872 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1873 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1874 | if (!capture) |
| 1875 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1876 | /* Compute the xxh64 of the ciphersuite. */ |
| 1877 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1878 | |
| 1879 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1880 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1881 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1882 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1883 | |
| 1884 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1887 | /* Callback is called for ssl protocol analyse */ |
| 1888 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1889 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1890 | #ifdef TLS1_RT_HEARTBEAT |
| 1891 | /* test heartbeat received (write_p is set to 0 |
| 1892 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1893 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1894 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1895 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1896 | const unsigned char *p = buf; |
| 1897 | unsigned int payload; |
| 1898 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1899 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1900 | |
| 1901 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1902 | if (*p != TLS1_HB_REQUEST) |
| 1903 | return; |
| 1904 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1905 | 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] | 1906 | goto kill_it; |
| 1907 | |
| 1908 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1909 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1910 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1911 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1912 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1913 | * advertised payload is larger than the advertised packet |
| 1914 | * length, so we have garbage in the buffer between the |
| 1915 | * payload and the end of the buffer (p+len). We can't know |
| 1916 | * if the SSL stack is patched, and we don't know if we can |
| 1917 | * safely wipe out the area between p+3+len and payload. |
| 1918 | * So instead, we prevent the response from being sent by |
| 1919 | * setting the max_send_fragment to 0 and we report an SSL |
| 1920 | * error, which will kill this connection. It will be reported |
| 1921 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1922 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1923 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1924 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1925 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1926 | return; |
| 1927 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1928 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1929 | if (global_ssl.capture_cipherlist > 0) |
| 1930 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1931 | } |
| 1932 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1933 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1934 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1935 | const unsigned char *in, unsigned int inlen, |
| 1936 | void *arg) |
| 1937 | { |
| 1938 | struct server *srv = arg; |
| 1939 | |
| 1940 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1941 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1942 | return SSL_TLSEXT_ERR_OK; |
| 1943 | return SSL_TLSEXT_ERR_NOACK; |
| 1944 | } |
| 1945 | #endif |
| 1946 | |
| 1947 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1948 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1949 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1950 | */ |
| 1951 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1952 | unsigned int *len, void *arg) |
| 1953 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1954 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1955 | |
| 1956 | *data = (const unsigned char *)conf->npn_str; |
| 1957 | *len = conf->npn_len; |
| 1958 | return SSL_TLSEXT_ERR_OK; |
| 1959 | } |
| 1960 | #endif |
| 1961 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1962 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1963 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1964 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1965 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1966 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1967 | unsigned char *outlen, |
| 1968 | const unsigned char *server, |
| 1969 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1970 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1971 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1972 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1973 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1974 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1975 | return SSL_TLSEXT_ERR_NOACK; |
| 1976 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1977 | return SSL_TLSEXT_ERR_OK; |
| 1978 | } |
| 1979 | #endif |
| 1980 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1981 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1982 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1983 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1984 | /* Create a X509 certificate with the specified servername and serial. This |
| 1985 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1986 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1987 | 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] | 1988 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1989 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1990 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1991 | SSL_CTX *ssl_ctx = NULL; |
| 1992 | X509 *newcrt = NULL; |
| 1993 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1994 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1995 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1996 | X509_NAME *name; |
| 1997 | const EVP_MD *digest; |
| 1998 | X509V3_CTX ctx; |
| 1999 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2000 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2001 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 2002 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2003 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2004 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 2005 | #else |
| 2006 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 2007 | if (tmp_ssl) |
| 2008 | pkey = SSL_get_privatekey(tmp_ssl); |
| 2009 | #endif |
| 2010 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2011 | goto mkcert_error; |
| 2012 | |
| 2013 | /* Create the certificate */ |
| 2014 | if (!(newcrt = X509_new())) |
| 2015 | goto mkcert_error; |
| 2016 | |
| 2017 | /* Set version number for the certificate (X509v3) and the serial |
| 2018 | * number */ |
| 2019 | if (X509_set_version(newcrt, 2L) != 1) |
| 2020 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 2021 | 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] | 2022 | |
| 2023 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 2024 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 2025 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2026 | goto mkcert_error; |
| 2027 | |
| 2028 | /* set public key in the certificate */ |
| 2029 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 2030 | goto mkcert_error; |
| 2031 | |
| 2032 | /* Set issuer name from the CA */ |
| 2033 | if (!(name = X509_get_subject_name(cacert))) |
| 2034 | goto mkcert_error; |
| 2035 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 2036 | goto mkcert_error; |
| 2037 | |
| 2038 | /* Set the subject name using the same, but the CN */ |
| 2039 | name = X509_NAME_dup(name); |
| 2040 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 2041 | (const unsigned char *)servername, |
| 2042 | -1, -1, 0) != 1) { |
| 2043 | X509_NAME_free(name); |
| 2044 | goto mkcert_error; |
| 2045 | } |
| 2046 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 2047 | X509_NAME_free(name); |
| 2048 | goto mkcert_error; |
| 2049 | } |
| 2050 | X509_NAME_free(name); |
| 2051 | |
| 2052 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2053 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2054 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 2055 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 2056 | X509_EXTENSION *ext; |
| 2057 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2058 | 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] | 2059 | goto mkcert_error; |
| 2060 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2061 | X509_EXTENSION_free(ext); |
| 2062 | goto mkcert_error; |
| 2063 | } |
| 2064 | X509_EXTENSION_free(ext); |
| 2065 | } |
| 2066 | |
| 2067 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2068 | |
| 2069 | key_type = EVP_PKEY_base_id(capkey); |
| 2070 | |
| 2071 | if (key_type == EVP_PKEY_DSA) |
| 2072 | digest = EVP_sha1(); |
| 2073 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2074 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2075 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2076 | digest = EVP_sha256(); |
| 2077 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2078 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2079 | int nid; |
| 2080 | |
| 2081 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2082 | goto mkcert_error; |
| 2083 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2084 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2085 | #else |
| 2086 | goto mkcert_error; |
| 2087 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2090 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2091 | goto mkcert_error; |
| 2092 | |
| 2093 | /* Create and set the new SSL_CTX */ |
| 2094 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2095 | goto mkcert_error; |
| 2096 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2097 | goto mkcert_error; |
| 2098 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2099 | goto mkcert_error; |
| 2100 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2101 | goto mkcert_error; |
| 2102 | |
| 2103 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2104 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2105 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2106 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2107 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2108 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2109 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2110 | 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] | 2111 | EC_KEY *ecc; |
| 2112 | int nid; |
| 2113 | |
| 2114 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2115 | goto end; |
| 2116 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2117 | goto end; |
| 2118 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2119 | EC_KEY_free(ecc); |
| 2120 | } |
| 2121 | #endif |
| 2122 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2123 | return ssl_ctx; |
| 2124 | |
| 2125 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2126 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2127 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2128 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2129 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2130 | return NULL; |
| 2131 | } |
| 2132 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2133 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2134 | 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] | 2135 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2136 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2137 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2138 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2139 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2142 | /* 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] | 2143 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2144 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2145 | 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] | 2146 | { |
| 2147 | struct lru64 *lru = NULL; |
| 2148 | |
| 2149 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2150 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2151 | 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] | 2152 | if (lru && lru->domain) { |
| 2153 | if (ssl) |
| 2154 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2155 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2156 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2157 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2158 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2159 | } |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2163 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2164 | * function is not thread-safe, it should only be used to check if a certificate |
| 2165 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2166 | * thread). It is kept for backward compatibility. */ |
| 2167 | SSL_CTX * |
| 2168 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2169 | { |
| 2170 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2171 | } |
| 2172 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2173 | /* Set a certificate int the LRU cache used to store generated |
| 2174 | * certificate. Return 0 on success, otherwise -1 */ |
| 2175 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2176 | 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] | 2177 | { |
| 2178 | struct lru64 *lru = NULL; |
| 2179 | |
| 2180 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2181 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2182 | 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] | 2183 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2184 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2185 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2186 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2187 | if (lru->domain && lru->data) |
| 2188 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2189 | 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] | 2190 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2191 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2192 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2193 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2196 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2197 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2198 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2199 | { |
| 2200 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2201 | } |
| 2202 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2203 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2204 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2205 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2206 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2207 | 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] | 2208 | { |
| 2209 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2210 | SSL_CTX *ssl_ctx = NULL; |
| 2211 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2212 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2213 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2214 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2215 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2216 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2217 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2218 | if (lru && lru->domain) |
| 2219 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2220 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2221 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2222 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2223 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2224 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2225 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2226 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2227 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2228 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2229 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2230 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2231 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2232 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2233 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2234 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2235 | return 0; |
| 2236 | } |
| 2237 | static int |
| 2238 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2239 | { |
| 2240 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2241 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2242 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2243 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2244 | 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] | 2245 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2246 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2247 | } |
| 2248 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2249 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2250 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2251 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2252 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2253 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2254 | |
| 2255 | 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] | 2256 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2257 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2258 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2259 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2260 | #endif |
| 2261 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2262 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2263 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2264 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2265 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2266 | 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] | 2267 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2268 | 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] | 2269 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2270 | #endif |
| 2271 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2272 | 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] | 2273 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2274 | 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] | 2275 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2276 | #endif |
| 2277 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2278 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2279 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2280 | /* Unusable in this context. */ |
| 2281 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2282 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2283 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2284 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2285 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2286 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2287 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2288 | |
| 2289 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2290 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2291 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2292 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2293 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2294 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2295 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2296 | } |
| 2297 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2298 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2299 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2300 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2301 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2302 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2303 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2304 | } |
| 2305 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2306 | 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] | 2307 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2308 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2309 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2310 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2311 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2312 | } |
| 2313 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2314 | 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] | 2315 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2316 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2317 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2318 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2319 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2320 | } |
| 2321 | 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] | 2322 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2323 | 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] | 2324 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2325 | #endif |
| 2326 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2327 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2328 | #if SSL_OP_NO_TLSv1_3 |
| 2329 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2330 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2331 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2332 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2333 | #endif |
| 2334 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2335 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2336 | |
| 2337 | static struct { |
| 2338 | int option; |
| 2339 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2340 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2341 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2342 | const char *name; |
| 2343 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2344 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2345 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2346 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2347 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2348 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2349 | {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] | 2350 | }; |
| 2351 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2352 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2353 | { |
| 2354 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2355 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2356 | SSL_set_SSL_CTX(ssl, ctx); |
| 2357 | } |
| 2358 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2359 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2360 | |
| 2361 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2362 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2363 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2364 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2366 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2367 | return SSL_TLSEXT_ERR_OK; |
| 2368 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2371 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2372 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2373 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2374 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2375 | #else |
| 2376 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2377 | { |
| 2378 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2379 | struct connection *conn; |
| 2380 | struct bind_conf *s; |
| 2381 | const uint8_t *extension_data; |
| 2382 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2383 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2384 | |
| 2385 | char *wildp = NULL; |
| 2386 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2387 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2388 | 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] | 2389 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | int i; |
| 2391 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2392 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2393 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2394 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2395 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2396 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2397 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2398 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2399 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2400 | #else |
| 2401 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2402 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2403 | /* |
| 2404 | * The server_name extension was given too much extensibility when it |
| 2405 | * was written, so parsing the normal case is a bit complex. |
| 2406 | */ |
| 2407 | size_t len; |
| 2408 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2409 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2410 | /* Extract the length of the supplied list of names. */ |
| 2411 | len = (*extension_data++) << 8; |
| 2412 | len |= *extension_data++; |
| 2413 | if (len + 2 != extension_len) |
| 2414 | goto abort; |
| 2415 | /* |
| 2416 | * The list in practice only has a single element, so we only consider |
| 2417 | * the first one. |
| 2418 | */ |
| 2419 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2420 | goto abort; |
| 2421 | extension_len = len - 1; |
| 2422 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2423 | if (extension_len <= 2) |
| 2424 | goto abort; |
| 2425 | len = (*extension_data++) << 8; |
| 2426 | len |= *extension_data++; |
| 2427 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2428 | || memchr(extension_data, 0, len) != NULL) |
| 2429 | goto abort; |
| 2430 | servername = extension_data; |
| 2431 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2432 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2433 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2434 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2435 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2436 | } |
| 2437 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2438 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2439 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2440 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2441 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2442 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2443 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2444 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2445 | goto abort; |
| 2446 | } |
| 2447 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2448 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2449 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2450 | 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] | 2451 | #else |
| 2452 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2453 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2454 | uint8_t sign; |
| 2455 | size_t len; |
| 2456 | if (extension_len < 2) |
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 | len = (*extension_data++) << 8; |
| 2459 | len |= *extension_data++; |
| 2460 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2461 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2462 | if (len % 2 != 0) |
| 2463 | goto abort; |
| 2464 | for (; len > 0; len -= 2) { |
| 2465 | extension_data++; /* hash */ |
| 2466 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2467 | switch (sign) { |
| 2468 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2469 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2470 | break; |
| 2471 | case TLSEXT_signature_ecdsa: |
| 2472 | has_ecdsa_sig = 1; |
| 2473 | break; |
| 2474 | default: |
| 2475 | continue; |
| 2476 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2477 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2478 | break; |
| 2479 | } |
| 2480 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2481 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2482 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2483 | } |
| 2484 | 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] | 2485 | const SSL_CIPHER *cipher; |
| 2486 | size_t len; |
| 2487 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2488 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2489 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2490 | len = ctx->cipher_suites_len; |
| 2491 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2492 | #else |
| 2493 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2494 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2495 | if (len % 2 != 0) |
| 2496 | goto abort; |
| 2497 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2498 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2499 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2500 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2501 | #else |
| 2502 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2503 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2504 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2505 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2506 | break; |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2511 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2512 | trash.area[i] = tolower(servername[i]); |
| 2513 | if (!wildp && (trash.area[i] == '.')) |
| 2514 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2515 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2516 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2517 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2518 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2519 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2520 | for (i = 0; i < 2; i++) { |
| 2521 | if (i == 0) /* lookup in full qualified names */ |
| 2522 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2523 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2524 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2525 | else |
| 2526 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2527 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2528 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2530 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2531 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2532 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2533 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2534 | break; |
| 2535 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2536 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2537 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2538 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2539 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2540 | if (!node_anonymous) |
| 2541 | node_anonymous = n; |
| 2542 | break; |
| 2543 | } |
| 2544 | } |
| 2545 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2546 | /* select by key_signature priority order */ |
| 2547 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2548 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2549 | : (node_anonymous ? node_anonymous |
| 2550 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2551 | : node_rsa /* no rsa signature case (far far away) */ |
| 2552 | ))); |
| 2553 | if (node) { |
| 2554 | /* switch ctx */ |
| 2555 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2556 | 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] | 2557 | if (conf) { |
| 2558 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2559 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2560 | if (conf->early_data) |
| 2561 | allow_early = 1; |
| 2562 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2563 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2564 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2565 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2566 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2567 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2568 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2569 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2570 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2571 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2572 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2573 | } |
| 2574 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2575 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2576 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2577 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2578 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2579 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2580 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2581 | allow_early: |
| 2582 | #ifdef OPENSSL_IS_BORINGSSL |
| 2583 | if (allow_early) |
| 2584 | SSL_set_early_data_enabled(ssl, 1); |
| 2585 | #else |
| 2586 | if (!allow_early) |
| 2587 | SSL_set_max_early_data(ssl, 0); |
| 2588 | #endif |
| 2589 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2590 | abort: |
| 2591 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2592 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2593 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2594 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2595 | #else |
| 2596 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2597 | return 0; |
| 2598 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2602 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2603 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2604 | * warning when no match is found, which implies the default (first) cert |
| 2605 | * will keep being used. |
| 2606 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2607 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2608 | { |
| 2609 | const char *servername; |
| 2610 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2611 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2612 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2613 | int i; |
| 2614 | (void)al; /* shut gcc stupid warning */ |
| 2615 | |
| 2616 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2617 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2618 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2619 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2620 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2621 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2622 | if (s->strict_sni) |
| 2623 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2624 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2625 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2626 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2627 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2628 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2630 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2631 | if (!servername[i]) |
| 2632 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2633 | trash.area[i] = tolower(servername[i]); |
| 2634 | if (!wildp && (trash.area[i] == '.')) |
| 2635 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2636 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2637 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2638 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2639 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2640 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2641 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2642 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2643 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2644 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2645 | node = n; |
| 2646 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2647 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2648 | } |
| 2649 | if (!node && wildp) { |
| 2650 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2651 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2652 | /* lookup a not neg filter */ |
| 2653 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2654 | node = n; |
| 2655 | break; |
| 2656 | } |
| 2657 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2658 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2659 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2660 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2661 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2662 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2663 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2664 | return SSL_TLSEXT_ERR_OK; |
| 2665 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2666 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2667 | if (s->strict_sni) { |
| 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_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2670 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2671 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2672 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2673 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2677 | 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] | 2678 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2679 | return SSL_TLSEXT_ERR_OK; |
| 2680 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2681 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2682 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2683 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2684 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2685 | |
| 2686 | static DH * ssl_get_dh_1024(void) |
| 2687 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2688 | static unsigned char dh1024_p[]={ |
| 2689 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2690 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2691 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2692 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2693 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2694 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2695 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2696 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2697 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2698 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2699 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2700 | }; |
| 2701 | static unsigned char dh1024_g[]={ |
| 2702 | 0x02, |
| 2703 | }; |
| 2704 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2705 | BIGNUM *p; |
| 2706 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2707 | DH *dh = DH_new(); |
| 2708 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2709 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2710 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2711 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2712 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2713 | DH_free(dh); |
| 2714 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2715 | } else { |
| 2716 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | return dh; |
| 2720 | } |
| 2721 | |
| 2722 | static DH *ssl_get_dh_2048(void) |
| 2723 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2724 | static unsigned char dh2048_p[]={ |
| 2725 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2726 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2727 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2728 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2729 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2730 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2731 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2732 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2733 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2734 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2735 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2736 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2737 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2738 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2739 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2740 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2741 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2742 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2743 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2744 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2745 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2746 | 0xB7,0x1F,0x77,0xF3, |
| 2747 | }; |
| 2748 | static unsigned char dh2048_g[]={ |
| 2749 | 0x02, |
| 2750 | }; |
| 2751 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2752 | BIGNUM *p; |
| 2753 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2754 | DH *dh = DH_new(); |
| 2755 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2756 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2757 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2758 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2759 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2760 | DH_free(dh); |
| 2761 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2762 | } else { |
| 2763 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | return dh; |
| 2767 | } |
| 2768 | |
| 2769 | static DH *ssl_get_dh_4096(void) |
| 2770 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2771 | static unsigned char dh4096_p[]={ |
| 2772 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2773 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2774 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2775 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2776 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2777 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2778 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2779 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2780 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2781 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2782 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2783 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2784 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2785 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2786 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2787 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2788 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2789 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2790 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2791 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2792 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2793 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2794 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2795 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2796 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2797 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2798 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2799 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2800 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2801 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2802 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2803 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2804 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2805 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2806 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2807 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2808 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2809 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2810 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2811 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2812 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2813 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2814 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2815 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2816 | static unsigned char dh4096_g[]={ |
| 2817 | 0x02, |
| 2818 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2819 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2820 | BIGNUM *p; |
| 2821 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2822 | DH *dh = DH_new(); |
| 2823 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2824 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2825 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2826 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2827 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2828 | DH_free(dh); |
| 2829 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2830 | } else { |
| 2831 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2832 | } |
| 2833 | } |
| 2834 | return dh; |
| 2835 | } |
| 2836 | |
| 2837 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2838 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2839 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2840 | { |
| 2841 | DH *dh = NULL; |
| 2842 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2843 | int type; |
| 2844 | |
| 2845 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2846 | |
| 2847 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2848 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2849 | */ |
| 2850 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2851 | keylen = EVP_PKEY_bits(pkey); |
| 2852 | } |
| 2853 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2854 | if (keylen > global_ssl.default_dh_param) { |
| 2855 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2856 | } |
| 2857 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2858 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2859 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2860 | } |
| 2861 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2862 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2863 | } |
| 2864 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2865 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | return dh; |
| 2869 | } |
| 2870 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2871 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2872 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2873 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2874 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2875 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2876 | if (in == NULL) |
| 2877 | goto end; |
| 2878 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2879 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2880 | goto end; |
| 2881 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2882 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2883 | |
| 2884 | end: |
| 2885 | if (in) |
| 2886 | BIO_free(in); |
| 2887 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2888 | ERR_clear_error(); |
| 2889 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2890 | return dh; |
| 2891 | } |
| 2892 | |
| 2893 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2894 | { |
| 2895 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2896 | |
| 2897 | if (global_dh) { |
| 2898 | return 0; |
| 2899 | } |
| 2900 | |
| 2901 | return -1; |
| 2902 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2903 | #endif |
| 2904 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 2905 | /* release ssl bind conf */ |
| 2906 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
| 2907 | { |
| 2908 | if (conf) { |
| 2909 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 2910 | free(conf->npn_str); |
| 2911 | conf->npn_str = NULL; |
| 2912 | #endif |
| 2913 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 2914 | free(conf->alpn_str); |
| 2915 | conf->alpn_str = NULL; |
| 2916 | #endif |
| 2917 | free(conf->ca_file); |
| 2918 | conf->ca_file = NULL; |
| 2919 | free(conf->ca_verify_file); |
| 2920 | conf->ca_verify_file = NULL; |
| 2921 | free(conf->crl_file); |
| 2922 | conf->crl_file = NULL; |
| 2923 | free(conf->ciphers); |
| 2924 | conf->ciphers = NULL; |
| 2925 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2926 | free(conf->ciphersuites); |
| 2927 | conf->ciphersuites = NULL; |
| 2928 | #endif |
| 2929 | free(conf->curves); |
| 2930 | conf->curves = NULL; |
| 2931 | free(conf->ecdhe); |
| 2932 | conf->ecdhe = NULL; |
| 2933 | } |
| 2934 | } |
| 2935 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 2936 | /* unlink a ckch_inst, free all SNIs, free the ckch_inst */ |
| 2937 | /* The caller must use the lock of the bind_conf if used with inserted SNIs */ |
| 2938 | static void ckch_inst_free(struct ckch_inst *inst) |
| 2939 | { |
| 2940 | struct sni_ctx *sni, *sni_s; |
| 2941 | |
| 2942 | if (inst == NULL) |
| 2943 | return; |
| 2944 | |
| 2945 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 2946 | SSL_CTX_free(sni->ctx); |
| 2947 | LIST_DEL(&sni->by_ckch_inst); |
| 2948 | ebmb_delete(&sni->name); |
| 2949 | free(sni); |
| 2950 | } |
| 2951 | LIST_DEL(&inst->by_ckchs); |
| 2952 | LIST_DEL(&inst->by_crtlist_entry); |
| 2953 | free(inst); |
| 2954 | } |
| 2955 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2956 | /* Alloc and init a ckch_inst */ |
| 2957 | static struct ckch_inst *ckch_inst_new() |
| 2958 | { |
| 2959 | struct ckch_inst *ckch_inst; |
| 2960 | |
| 2961 | ckch_inst = calloc(1, sizeof *ckch_inst); |
William Lallemand | 9cef2e2 | 2020-04-09 16:25:10 +0200 | [diff] [blame] | 2962 | if (!ckch_inst) |
| 2963 | return NULL; |
| 2964 | |
| 2965 | LIST_INIT(&ckch_inst->sni_ctx); |
| 2966 | LIST_INIT(&ckch_inst->by_ckchs); |
| 2967 | LIST_INIT(&ckch_inst->by_crtlist_entry); |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2968 | |
| 2969 | return ckch_inst; |
| 2970 | } |
| 2971 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 2972 | /* free sni filters */ |
| 2973 | static void crtlist_free_filters(char **args) |
| 2974 | { |
| 2975 | int i; |
| 2976 | |
| 2977 | if (!args) |
| 2978 | return; |
| 2979 | |
| 2980 | for (i = 0; args[i]; i++) |
| 2981 | free(args[i]); |
| 2982 | |
| 2983 | free(args); |
| 2984 | } |
| 2985 | |
| 2986 | /* Alloc and duplicate a char ** array */ |
| 2987 | static char **crtlist_dup_filters(char **args, int fcount) |
| 2988 | { |
| 2989 | char **dst; |
| 2990 | int i; |
| 2991 | |
| 2992 | if (fcount == 0) |
| 2993 | return NULL; |
| 2994 | |
| 2995 | dst = calloc(fcount + 1, sizeof(*dst)); |
| 2996 | if (!dst) |
| 2997 | return NULL; |
| 2998 | |
| 2999 | for (i = 0; i < fcount; i++) { |
| 3000 | dst[i] = strdup(args[i]); |
| 3001 | if (!dst[i]) |
| 3002 | goto error; |
| 3003 | } |
| 3004 | return dst; |
| 3005 | |
| 3006 | error: |
| 3007 | crtlist_free_filters(dst); |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 3011 | /* |
| 3012 | * Detach and free a crtlist_entry. |
| 3013 | * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst |
| 3014 | */ |
| 3015 | static void crtlist_entry_free(struct crtlist_entry *entry) |
| 3016 | { |
| 3017 | struct ckch_inst *inst, *inst_s; |
| 3018 | |
| 3019 | if (entry == NULL) |
| 3020 | return; |
| 3021 | |
| 3022 | ebpt_delete(&entry->node); |
| 3023 | LIST_DEL(&entry->by_crtlist); |
| 3024 | LIST_DEL(&entry->by_ckch_store); |
| 3025 | crtlist_free_filters(entry->filters); |
| 3026 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 3027 | free(entry->ssl_conf); |
| 3028 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 3029 | ckch_inst_free(inst); |
| 3030 | } |
| 3031 | free(entry); |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Allocate and initialize a crtlist_entry |
| 3036 | */ |
| 3037 | static struct crtlist_entry *crtlist_entry_new() |
| 3038 | { |
| 3039 | struct crtlist_entry *entry; |
| 3040 | |
| 3041 | entry = calloc(1, sizeof(*entry)); |
| 3042 | if (entry == NULL) |
| 3043 | return NULL; |
| 3044 | |
| 3045 | LIST_INIT(&entry->ckch_inst); |
| 3046 | |
| 3047 | /* initialize the nodes so we can LIST_DEL in any cases */ |
| 3048 | LIST_INIT(&entry->by_crtlist); |
| 3049 | LIST_INIT(&entry->by_ckch_store); |
| 3050 | |
| 3051 | return entry; |
| 3052 | } |
| 3053 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 3054 | /* Free a crtlist, from the crt_entry to the content of the ssl_conf */ |
| 3055 | static void crtlist_free(struct crtlist *crtlist) |
| 3056 | { |
| 3057 | struct crtlist_entry *entry, *s_entry; |
| 3058 | |
| 3059 | if (crtlist == NULL) |
| 3060 | return; |
| 3061 | |
| 3062 | list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) { |
| 3063 | crtlist_entry_free(entry); |
| 3064 | } |
| 3065 | ebmb_delete(&crtlist->node); |
| 3066 | free(crtlist); |
| 3067 | } |
| 3068 | |
| 3069 | /* Alloc and initialize a struct crtlist |
| 3070 | * <filename> is the key of the ebmb_node |
| 3071 | * <unique> initialize the list of entries to be unique (1) or not (0) |
| 3072 | */ |
| 3073 | static struct crtlist *crtlist_new(const char *filename, int unique) |
| 3074 | { |
| 3075 | struct crtlist *newlist; |
| 3076 | |
| 3077 | newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1); |
| 3078 | if (newlist == NULL) |
| 3079 | return NULL; |
| 3080 | |
| 3081 | memcpy(newlist->node.key, filename, strlen(filename) + 1); |
| 3082 | if (unique) |
| 3083 | newlist->entries = EB_ROOT_UNIQUE; |
| 3084 | else |
| 3085 | newlist->entries = EB_ROOT; |
| 3086 | |
| 3087 | LIST_INIT(&newlist->ord_entries); |
| 3088 | |
| 3089 | return newlist; |
| 3090 | } |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3091 | |
| 3092 | /* 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] | 3093 | 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] | 3094 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3095 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3096 | { |
| 3097 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3098 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3099 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3100 | if (*name == '!') { |
| 3101 | neg = 1; |
| 3102 | name++; |
| 3103 | } |
| 3104 | if (*name == '*') { |
| 3105 | wild = 1; |
| 3106 | name++; |
| 3107 | } |
| 3108 | /* !* filter is a nop */ |
| 3109 | if (neg && wild) |
| 3110 | return order; |
| 3111 | if (*name) { |
| 3112 | int j, len; |
| 3113 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3114 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3115 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3116 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3117 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3118 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3119 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3120 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3121 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3122 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3123 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3124 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3125 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3126 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3127 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3128 | sc->order = order++; |
| 3129 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3130 | sc->wild = wild; |
| 3131 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3132 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3133 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3134 | } |
| 3135 | return order; |
| 3136 | } |
| 3137 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3138 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3139 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3140 | * This function can't return an error. |
| 3141 | * |
| 3142 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3143 | */ |
| 3144 | static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
| 3145 | { |
| 3146 | |
| 3147 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3148 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3149 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3150 | |
| 3151 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3152 | |
| 3153 | /* ignore if sc0 was already inserted in a tree */ |
| 3154 | if (sc0->name.node.leaf_p) |
| 3155 | continue; |
| 3156 | |
| 3157 | /* Check for duplicates. */ |
| 3158 | if (sc0->wild) |
| 3159 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3160 | else |
| 3161 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3162 | |
| 3163 | for (; node; node = ebmb_next_dup(node)) { |
| 3164 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3165 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3166 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3167 | /* it's a duplicate, we should remove and free it */ |
| 3168 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3169 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3170 | free(sc0); |
| 3171 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3172 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | /* if duplicate, ignore the insertion */ |
| 3177 | if (!sc0) |
| 3178 | continue; |
| 3179 | |
| 3180 | if (sc0->wild) |
| 3181 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3182 | else |
| 3183 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3184 | |
| 3185 | /* replace the default_ctx if required with the first ctx */ |
| 3186 | if (ckch_inst->is_default && !def) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3187 | SSL_CTX_free(bind_conf->default_ctx); |
| 3188 | SSL_CTX_up_ref(sc0->ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3189 | bind_conf->default_ctx = sc0->ctx; |
| 3190 | def = 1; |
| 3191 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3196 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3197 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3198 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3199 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3200 | /* tree of crtlist (crt-list/directory) */ |
| 3201 | static struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3202 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3203 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3204 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3205 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3206 | * DH parameter available in ckchs nor in global, the default |
| 3207 | * DH parameters are applied on the SSL_CTX. |
| 3208 | * Returns a bitfield containing the flags: |
| 3209 | * ERR_FATAL in any fatal error case |
| 3210 | * ERR_ALERT if a reason of the error is availabine in err |
| 3211 | * ERR_WARN if a warning is available into err |
| 3212 | * The value 0 means there is no error nor warning and |
| 3213 | * the operation succeed. |
| 3214 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3215 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3216 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 3217 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3218 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3219 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3220 | DH *dh = NULL; |
| 3221 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 3222 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3223 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3224 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 3225 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3226 | err && *err ? *err : "", path); |
| 3227 | #if defined(SSL_CTX_set_dh_auto) |
| 3228 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3229 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3230 | err && *err ? *err : ""); |
| 3231 | #else |
| 3232 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3233 | err && *err ? *err : ""); |
| 3234 | #endif |
| 3235 | ret |= ERR_WARN; |
| 3236 | goto end; |
| 3237 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3238 | |
| 3239 | if (ssl_dh_ptr_index >= 0) { |
| 3240 | /* store a pointer to the DH params to avoid complaining about |
| 3241 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3242 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3243 | } |
| 3244 | } |
| 3245 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3246 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 3247 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3248 | err && *err ? *err : "", path); |
| 3249 | #if defined(SSL_CTX_set_dh_auto) |
| 3250 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3251 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3252 | err && *err ? *err : ""); |
| 3253 | #else |
| 3254 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3255 | err && *err ? *err : ""); |
| 3256 | #endif |
| 3257 | ret |= ERR_WARN; |
| 3258 | goto end; |
| 3259 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3260 | } |
| 3261 | else { |
| 3262 | /* Clear openssl global errors stack */ |
| 3263 | ERR_clear_error(); |
| 3264 | |
| 3265 | if (global_ssl.default_dh_param <= 1024) { |
| 3266 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3267 | if (local_dh_1024 == NULL) |
| 3268 | local_dh_1024 = ssl_get_dh_1024(); |
| 3269 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3270 | if (local_dh_1024 == NULL) { |
| 3271 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3272 | err && *err ? *err : "", path); |
| 3273 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3274 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3275 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3276 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3277 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3278 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3279 | err && *err ? *err : "", path); |
| 3280 | #if defined(SSL_CTX_set_dh_auto) |
| 3281 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3282 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3283 | err && *err ? *err : ""); |
| 3284 | #else |
| 3285 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3286 | err && *err ? *err : ""); |
| 3287 | #endif |
| 3288 | ret |= ERR_WARN; |
| 3289 | goto end; |
| 3290 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3291 | } |
| 3292 | else { |
| 3293 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3294 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | end: |
William Lallemand | 4dd145a | 2020-02-05 11:46:33 +0100 | [diff] [blame] | 3298 | ERR_clear_error(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3299 | return ret; |
| 3300 | } |
| 3301 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3302 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3303 | /* Frees the contents of a cert_key_and_chain |
| 3304 | */ |
| 3305 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 3306 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3307 | if (!ckch) |
| 3308 | return; |
| 3309 | |
| 3310 | /* Free the certificate and set pointer to NULL */ |
| 3311 | if (ckch->cert) |
| 3312 | X509_free(ckch->cert); |
| 3313 | ckch->cert = NULL; |
| 3314 | |
| 3315 | /* Free the key and set pointer to NULL */ |
| 3316 | if (ckch->key) |
| 3317 | EVP_PKEY_free(ckch->key); |
| 3318 | ckch->key = NULL; |
| 3319 | |
| 3320 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3321 | if (ckch->chain) |
| 3322 | sk_X509_pop_free(ckch->chain, X509_free); |
| 3323 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3324 | |
William Lallemand | 455af50 | 2019-10-17 18:04:45 +0200 | [diff] [blame] | 3325 | if (ckch->dh) |
| 3326 | DH_free(ckch->dh); |
| 3327 | ckch->dh = NULL; |
| 3328 | |
| 3329 | if (ckch->sctl) { |
| 3330 | free(ckch->sctl->area); |
| 3331 | ckch->sctl->area = NULL; |
| 3332 | free(ckch->sctl); |
| 3333 | ckch->sctl = NULL; |
| 3334 | } |
| 3335 | |
| 3336 | if (ckch->ocsp_response) { |
| 3337 | free(ckch->ocsp_response->area); |
| 3338 | ckch->ocsp_response->area = NULL; |
| 3339 | free(ckch->ocsp_response); |
| 3340 | ckch->ocsp_response = NULL; |
| 3341 | } |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3342 | |
| 3343 | if (ckch->ocsp_issuer) |
William Lallemand | dad239d | 2020-01-23 11:59:02 +0100 | [diff] [blame] | 3344 | X509_free(ckch->ocsp_issuer); |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3345 | ckch->ocsp_issuer = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3346 | } |
| 3347 | |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3348 | /* |
| 3349 | * |
| 3350 | * This function copy a cert_key_and_chain in memory |
| 3351 | * |
| 3352 | * It's used to try to apply changes on a ckch before committing them, because |
| 3353 | * most of the time it's not possible to revert those changes |
| 3354 | * |
| 3355 | * Return a the dst or NULL |
| 3356 | */ |
| 3357 | static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 3358 | struct cert_key_and_chain *dst) |
| 3359 | { |
| 3360 | if (src->cert) { |
| 3361 | dst->cert = src->cert; |
| 3362 | X509_up_ref(src->cert); |
| 3363 | } |
| 3364 | |
| 3365 | if (src->key) { |
| 3366 | dst->key = src->key; |
| 3367 | EVP_PKEY_up_ref(src->key); |
| 3368 | } |
| 3369 | |
| 3370 | if (src->chain) { |
| 3371 | dst->chain = X509_chain_up_ref(src->chain); |
| 3372 | } |
| 3373 | |
| 3374 | if (src->dh) { |
| 3375 | DH_up_ref(src->dh); |
| 3376 | dst->dh = src->dh; |
| 3377 | } |
| 3378 | |
| 3379 | if (src->sctl) { |
| 3380 | struct buffer *sctl; |
| 3381 | |
| 3382 | sctl = calloc(1, sizeof(*sctl)); |
| 3383 | if (!chunk_dup(sctl, src->sctl)) { |
| 3384 | free(sctl); |
| 3385 | sctl = NULL; |
| 3386 | goto error; |
| 3387 | } |
| 3388 | dst->sctl = sctl; |
| 3389 | } |
| 3390 | |
| 3391 | if (src->ocsp_response) { |
| 3392 | struct buffer *ocsp_response; |
| 3393 | |
| 3394 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 3395 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 3396 | free(ocsp_response); |
| 3397 | ocsp_response = NULL; |
| 3398 | goto error; |
| 3399 | } |
| 3400 | dst->ocsp_response = ocsp_response; |
| 3401 | } |
| 3402 | |
| 3403 | if (src->ocsp_issuer) { |
| 3404 | X509_up_ref(src->ocsp_issuer); |
| 3405 | dst->ocsp_issuer = src->ocsp_issuer; |
| 3406 | } |
| 3407 | |
| 3408 | return dst; |
| 3409 | |
| 3410 | error: |
| 3411 | |
| 3412 | /* free everything */ |
| 3413 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 3414 | |
| 3415 | return NULL; |
| 3416 | } |
| 3417 | |
| 3418 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3419 | /* checks if a key and cert exists in the ckch |
| 3420 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3421 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3422 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 3423 | { |
| 3424 | return (ckch->cert != NULL && ckch->key != NULL); |
| 3425 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3426 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3427 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3428 | /* |
| 3429 | * return 0 on success or != 0 on failure |
| 3430 | */ |
| 3431 | static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 3432 | { |
| 3433 | int ret = 1; |
| 3434 | BIO *in = NULL; |
| 3435 | X509 *issuer; |
| 3436 | |
| 3437 | if (buf) { |
| 3438 | /* reading from a buffer */ |
| 3439 | in = BIO_new_mem_buf(buf, -1); |
| 3440 | if (in == NULL) { |
| 3441 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3442 | goto end; |
| 3443 | } |
| 3444 | |
| 3445 | } else { |
| 3446 | /* reading from a file */ |
| 3447 | in = BIO_new(BIO_s_file()); |
| 3448 | if (in == NULL) |
| 3449 | goto end; |
| 3450 | |
| 3451 | if (BIO_read_filename(in, path) <= 0) |
| 3452 | goto end; |
| 3453 | } |
| 3454 | |
| 3455 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3456 | if (!issuer) { |
| 3457 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3458 | err && *err ? *err : "", path); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3459 | goto end; |
| 3460 | } |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3461 | /* no error, fill ckch with new context, old context must be free */ |
| 3462 | if (ckch->ocsp_issuer) |
| 3463 | X509_free(ckch->ocsp_issuer); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3464 | ckch->ocsp_issuer = issuer; |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3465 | ret = 0; |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3466 | |
| 3467 | end: |
| 3468 | |
| 3469 | ERR_clear_error(); |
| 3470 | if (in) |
| 3471 | BIO_free(in); |
| 3472 | |
| 3473 | return ret; |
| 3474 | } |
| 3475 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3476 | |
| 3477 | /* |
| 3478 | * 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] | 3479 | * The PEM must contain at least a Certificate, |
| 3480 | * It could contain a DH, a certificate chain and a PrivateKey. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3481 | * |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3482 | * If it failed you should not attempt to use the ckch but free it. |
| 3483 | * |
| 3484 | * Return 0 on success or != 0 on failure |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3485 | */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3486 | 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] | 3487 | { |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3488 | BIO *in = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3489 | int ret = 1; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3490 | X509 *ca; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3491 | X509 *cert = NULL; |
| 3492 | EVP_PKEY *key = NULL; |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3493 | DH *dh = NULL; |
| 3494 | STACK_OF(X509) *chain = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3495 | |
| 3496 | if (buf) { |
| 3497 | /* reading from a buffer */ |
| 3498 | in = BIO_new_mem_buf(buf, -1); |
| 3499 | if (in == NULL) { |
| 3500 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3501 | goto end; |
| 3502 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3503 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3504 | } else { |
| 3505 | /* reading from a file */ |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3506 | in = BIO_new(BIO_s_file()); |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3507 | if (in == NULL) { |
| 3508 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3509 | goto end; |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3510 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3511 | |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3512 | if (BIO_read_filename(in, path) <= 0) { |
| 3513 | memprintf(err, "%scannot open the file '%s'.\n", |
| 3514 | err && *err ? *err : "", path); |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3515 | goto end; |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3516 | } |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3517 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3518 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3519 | /* Read Private Key */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3520 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3521 | /* 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] | 3522 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3523 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3524 | /* Seek back to beginning of file */ |
| 3525 | if (BIO_reset(in) == -1) { |
| 3526 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3527 | err && *err ? *err : "", path); |
| 3528 | goto end; |
| 3529 | } |
| 3530 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3531 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 3532 | /* no need to return an error there, dh is not mandatory */ |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3533 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3534 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3535 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3536 | if (BIO_reset(in) == -1) { |
| 3537 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3538 | err && *err ? *err : "", path); |
| 3539 | goto end; |
| 3540 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3541 | |
| 3542 | /* Read Certificate */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3543 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3544 | if (cert == NULL) { |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3545 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3546 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3547 | goto end; |
| 3548 | } |
| 3549 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3550 | /* Look for a Certificate Chain */ |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3551 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3552 | if (chain == NULL) |
| 3553 | chain = sk_X509_new_null(); |
| 3554 | if (!sk_X509_push(chain, ca)) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3555 | X509_free(ca); |
| 3556 | goto end; |
| 3557 | } |
| 3558 | } |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3559 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3560 | ret = ERR_get_error(); |
| 3561 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3562 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3563 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3564 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
William Lallemand | 75b15f7 | 2020-01-23 10:56:05 +0100 | [diff] [blame] | 3567 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 3568 | if (ckch->ocsp_response) { |
| 3569 | free(ckch->ocsp_response->area); |
| 3570 | ckch->ocsp_response->area = NULL; |
| 3571 | free(ckch->ocsp_response); |
| 3572 | ckch->ocsp_response = NULL; |
| 3573 | } |
| 3574 | |
| 3575 | if (ckch->sctl) { |
| 3576 | free(ckch->sctl->area); |
| 3577 | ckch->sctl->area = NULL; |
| 3578 | free(ckch->sctl); |
| 3579 | ckch->sctl = NULL; |
| 3580 | } |
| 3581 | |
| 3582 | if (ckch->ocsp_issuer) { |
| 3583 | X509_free(ckch->ocsp_issuer); |
| 3584 | ckch->ocsp_issuer = NULL; |
| 3585 | } |
| 3586 | |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3587 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 3588 | SWAP(ckch->key, key); |
| 3589 | SWAP(ckch->dh, dh); |
| 3590 | SWAP(ckch->cert, cert); |
| 3591 | SWAP(ckch->chain, chain); |
| 3592 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3593 | ret = 0; |
| 3594 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3595 | end: |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3596 | |
| 3597 | ERR_clear_error(); |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3598 | if (in) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3599 | BIO_free(in); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3600 | if (key) |
| 3601 | EVP_PKEY_free(key); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3602 | if (dh) |
| 3603 | DH_free(dh); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3604 | if (cert) |
| 3605 | X509_free(cert); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3606 | if (chain) |
| 3607 | sk_X509_pop_free(chain, X509_free); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3608 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3609 | return ret; |
| 3610 | } |
| 3611 | |
| 3612 | /* |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3613 | * Try to load a private key file from a <path> or a buffer <buf> |
| 3614 | * |
| 3615 | * If it failed you should not attempt to use the ckch but free it. |
| 3616 | * |
| 3617 | * Return 0 on success or != 0 on failure |
| 3618 | */ |
| 3619 | static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 3620 | { |
| 3621 | BIO *in = NULL; |
| 3622 | int ret = 1; |
| 3623 | EVP_PKEY *key = NULL; |
| 3624 | |
| 3625 | if (buf) { |
| 3626 | /* reading from a buffer */ |
| 3627 | in = BIO_new_mem_buf(buf, -1); |
| 3628 | if (in == NULL) { |
| 3629 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3630 | goto end; |
| 3631 | } |
| 3632 | |
| 3633 | } else { |
| 3634 | /* reading from a file */ |
| 3635 | in = BIO_new(BIO_s_file()); |
| 3636 | if (in == NULL) |
| 3637 | goto end; |
| 3638 | |
| 3639 | if (BIO_read_filename(in, path) <= 0) |
| 3640 | goto end; |
| 3641 | } |
| 3642 | |
| 3643 | /* Read Private Key */ |
| 3644 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3645 | if (key == NULL) { |
| 3646 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3647 | err && *err ? *err : "", path); |
| 3648 | goto end; |
| 3649 | } |
| 3650 | |
| 3651 | ret = 0; |
| 3652 | |
| 3653 | SWAP(ckch->key, key); |
| 3654 | |
| 3655 | end: |
| 3656 | |
| 3657 | ERR_clear_error(); |
| 3658 | if (in) |
| 3659 | BIO_free(in); |
| 3660 | if (key) |
| 3661 | EVP_PKEY_free(key); |
| 3662 | |
| 3663 | return ret; |
| 3664 | } |
| 3665 | |
| 3666 | /* |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3667 | * Try to load in a ckch every files related to a ckch. |
| 3668 | * (PEM, sctl, ocsp, issuer etc.) |
| 3669 | * |
| 3670 | * This function is only used to load files during the configuration parsing, |
| 3671 | * it is not used with the CLI. |
| 3672 | * |
| 3673 | * This allows us to carry the contents of the file without having to read the |
| 3674 | * file multiple times. The caller must call |
| 3675 | * ssl_sock_free_cert_key_and_chain_contents. |
| 3676 | * |
| 3677 | * returns: |
| 3678 | * 0 on Success |
| 3679 | * 1 on SSL Failure |
| 3680 | */ |
| 3681 | static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3682 | { |
| 3683 | int ret = 1; |
| 3684 | |
| 3685 | /* try to load the PEM */ |
| 3686 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 3687 | goto end; |
| 3688 | } |
| 3689 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3690 | /* try to load an external private key if it wasn't in the PEM */ |
| 3691 | if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) { |
| 3692 | char fp[MAXPATHLEN+1]; |
| 3693 | struct stat st; |
| 3694 | |
| 3695 | snprintf(fp, MAXPATHLEN+1, "%s.key", path); |
| 3696 | if (stat(fp, &st) == 0) { |
| 3697 | if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) { |
| 3698 | memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n", |
| 3699 | err && *err ? *err : "", fp); |
| 3700 | goto end; |
| 3701 | } |
| 3702 | } |
| 3703 | } |
| 3704 | |
| 3705 | if (ckch->key == NULL) { |
| 3706 | memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path); |
| 3707 | goto end; |
| 3708 | } |
| 3709 | |
| 3710 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 3711 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 3712 | err && *err ? *err : "", path); |
| 3713 | goto end; |
| 3714 | } |
| 3715 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3716 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3717 | /* try to load the sctl file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3718 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3719 | char fp[MAXPATHLEN+1]; |
| 3720 | struct stat st; |
| 3721 | |
| 3722 | snprintf(fp, MAXPATHLEN+1, "%s.sctl", path); |
| 3723 | if (stat(fp, &st) == 0) { |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 3724 | if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3725 | 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] | 3726 | err && *err ? *err : "", fp); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3727 | ret = 1; |
| 3728 | goto end; |
| 3729 | } |
| 3730 | } |
| 3731 | } |
| 3732 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3733 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3734 | /* try to load an ocsp response file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3735 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3736 | char fp[MAXPATHLEN+1]; |
| 3737 | struct stat st; |
| 3738 | |
| 3739 | snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path); |
| 3740 | if (stat(fp, &st) == 0) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 3741 | if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3742 | ret = 1; |
| 3743 | goto end; |
| 3744 | } |
| 3745 | } |
| 3746 | } |
| 3747 | |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3748 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3749 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3750 | /* 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] | 3751 | if (!ckch->ocsp_issuer) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3752 | struct stat st; |
| 3753 | char fp[MAXPATHLEN+1]; |
| 3754 | |
| 3755 | snprintf(fp, MAXPATHLEN+1, "%s.issuer", path); |
| 3756 | if (stat(fp, &st) == 0) { |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3757 | if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3758 | ret = 1; |
| 3759 | goto end; |
| 3760 | } |
| 3761 | |
| 3762 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
William Lallemand | 786188f | 2019-10-15 10:05:37 +0200 | [diff] [blame] | 3763 | memprintf(err, "%s '%s' is not an issuer'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3764 | err && *err ? *err : "", fp); |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3765 | ret = 1; |
| 3766 | goto end; |
| 3767 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | } |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3771 | #endif |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3772 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3773 | ret = 0; |
| 3774 | |
| 3775 | end: |
| 3776 | |
| 3777 | ERR_clear_error(); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3778 | |
| 3779 | /* Something went wrong in one of the reads */ |
| 3780 | if (ret != 0) |
| 3781 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3782 | |
| 3783 | return ret; |
| 3784 | } |
| 3785 | |
| 3786 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3787 | * Returns a bitfield containing the flags: |
| 3788 | * ERR_FATAL in any fatal error case |
| 3789 | * ERR_ALERT if the reason of the error is available in err |
| 3790 | * ERR_WARN if a warning is available into err |
| 3791 | * The value 0 means there is no error nor warning and |
| 3792 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3793 | */ |
| 3794 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3795 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3796 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3797 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3798 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3799 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3800 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3801 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3802 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3803 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3807 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3808 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3809 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3810 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3811 | } |
| 3812 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3813 | if (ckch->chain) { |
| 3814 | find_chain = ckch->chain; |
| 3815 | } else { |
| 3816 | /* Find Certificate Chain in global */ |
| 3817 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 3818 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3819 | if (issuer) |
| 3820 | find_chain = issuer->chain; |
| 3821 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3822 | |
Emmanuel Hocdet | 1673977 | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3823 | /* Load all certs from chain, except Root, in the ssl_ctx */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3824 | if (find_chain) { |
| 3825 | int i; |
| 3826 | X509 *ca; |
| 3827 | for (i = 0; i < sk_X509_num(find_chain); i++) { |
| 3828 | ca = sk_X509_value(find_chain, i); |
Emmanuel Hocdet | 1673977 | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3829 | /* skip self issued (Root CA) */ |
Emmanuel Hocdet | c3b7e74 | 2020-04-22 11:06:19 +0200 | [diff] [blame] | 3830 | if (global_ssl.skip_self_issued_ca && !X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca))) |
Emmanuel Hocdet | 1673977 | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3831 | continue; |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3832 | /* |
| 3833 | SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2 |
| 3834 | Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert) |
| 3835 | */ |
| 3836 | X509_up_ref(ca); |
| 3837 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3838 | X509_free(ca); |
| 3839 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3840 | err && *err ? *err : "", path); |
| 3841 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3842 | goto end; |
| 3843 | } |
Emmanuel Hocdet | f4f14ea | 2020-03-23 10:31:47 +0100 | [diff] [blame] | 3844 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3845 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3846 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3847 | #ifndef OPENSSL_NO_DH |
| 3848 | /* store a NULL pointer to indicate we have not yet loaded |
| 3849 | a custom DH param file */ |
| 3850 | if (ssl_dh_ptr_index >= 0) { |
| 3851 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3852 | } |
| 3853 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3854 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3855 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3856 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3857 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3858 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3859 | } |
| 3860 | #endif |
| 3861 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3862 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3863 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3864 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3865 | 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] | 3866 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3867 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3868 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | #endif |
| 3872 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 3873 | #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] | 3874 | /* Load OCSP Info into context */ |
| 3875 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 3876 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3877 | 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", |
| 3878 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3879 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3880 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3881 | } |
| 3882 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3883 | #endif |
| 3884 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3885 | end: |
| 3886 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3887 | } |
| 3888 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3889 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3890 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3891 | 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] | 3892 | { |
| 3893 | struct sni_keytype *s_kt = NULL; |
| 3894 | struct ebmb_node *node; |
| 3895 | int i; |
| 3896 | |
| 3897 | for (i = 0; i < trash.size; i++) { |
| 3898 | if (!str[i]) |
| 3899 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3900 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3901 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3902 | trash.area[i] = 0; |
| 3903 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3904 | if (!node) { |
| 3905 | /* CN not found in tree */ |
| 3906 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3907 | /* Using memcpy here instead of strncpy. |
| 3908 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3909 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3910 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3911 | if (!s_kt) |
| 3912 | return -1; |
| 3913 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3914 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3915 | s_kt->keytypes = 0; |
| 3916 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3917 | } else { |
| 3918 | /* CN found in tree */ |
| 3919 | s_kt = container_of(node, struct sni_keytype, name); |
| 3920 | } |
| 3921 | |
| 3922 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3923 | s_kt->keytypes |= 1<<key_index; |
| 3924 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3925 | return 0; |
| 3926 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3927 | } |
| 3928 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3929 | #endif |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3930 | /* |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3931 | * Free a ckch_store, its ckch, its instances and remove it from the ebtree |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3932 | */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3933 | static void ckch_store_free(struct ckch_store *store) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3934 | { |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3935 | struct ckch_inst *inst, *inst_s; |
| 3936 | |
| 3937 | if (!store) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3938 | return; |
| 3939 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3940 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L |
| 3941 | if (store->multi) { |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3942 | int n; |
| 3943 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3944 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3945 | ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3946 | } else |
| 3947 | #endif |
| 3948 | { |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3949 | ssl_sock_free_cert_key_and_chain_contents(store->ckch); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3952 | free(store->ckch); |
| 3953 | store->ckch = NULL; |
| 3954 | |
| 3955 | list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) { |
| 3956 | ckch_inst_free(inst); |
| 3957 | } |
| 3958 | ebmb_delete(&store->node); |
| 3959 | free(store); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3962 | /* |
| 3963 | * create and initialize a ckch_store |
| 3964 | * <path> is the key name |
| 3965 | * <nmemb> is the number of store->ckch objects to allocate |
| 3966 | * |
| 3967 | * Return a ckch_store or NULL upon failure. |
| 3968 | */ |
| 3969 | static struct ckch_store *ckch_store_new(const char *filename, int nmemb) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3970 | { |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3971 | struct ckch_store *store; |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3972 | int pathlen; |
| 3973 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3974 | pathlen = strlen(filename); |
| 3975 | store = calloc(1, sizeof(*store) + pathlen + 1); |
| 3976 | if (!store) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3977 | return NULL; |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3978 | |
| 3979 | if (nmemb > 1) |
| 3980 | store->multi = 1; |
| 3981 | else |
| 3982 | store->multi = 0; |
| 3983 | |
| 3984 | memcpy(store->path, filename, pathlen + 1); |
| 3985 | |
| 3986 | LIST_INIT(&store->ckch_inst); |
| 3987 | LIST_INIT(&store->crtlist_entry); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3988 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3989 | store->ckch = calloc(nmemb, sizeof(*store->ckch)); |
| 3990 | if (!store->ckch) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3991 | goto error; |
| 3992 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3993 | return store; |
| 3994 | error: |
| 3995 | ckch_store_free(store); |
| 3996 | return NULL; |
| 3997 | } |
| 3998 | |
| 3999 | /* allocate and duplicate a ckch_store |
| 4000 | * Return a new ckch_store or NULL */ |
| 4001 | static struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 4002 | { |
| 4003 | struct ckch_store *dst; |
| 4004 | |
| 4005 | dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1); |
| 4006 | |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 4007 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4008 | if (src->multi) { |
| 4009 | int n; |
| 4010 | |
| 4011 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4012 | if (&src->ckch[n]) { |
| 4013 | if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n])) |
| 4014 | goto error; |
| 4015 | } |
| 4016 | } |
| 4017 | } else |
| 4018 | #endif |
| 4019 | { |
| 4020 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 4021 | goto error; |
| 4022 | } |
| 4023 | |
| 4024 | return dst; |
| 4025 | |
| 4026 | error: |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 4027 | ckch_store_free(dst); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 4028 | |
| 4029 | return NULL; |
| 4030 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4031 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4032 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4033 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4034 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4035 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4036 | { |
| 4037 | struct ebmb_node *eb; |
| 4038 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4039 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4040 | if (!eb) |
| 4041 | return NULL; |
| 4042 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4043 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4047 | * 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] | 4048 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4049 | 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] | 4050 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4051 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4052 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 4053 | ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1); |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4054 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4055 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 4056 | goto end; |
| 4057 | } |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4058 | if (!multi) { |
| 4059 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 4060 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4061 | goto end; |
| 4062 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4063 | /* insert into the ckchs tree */ |
| 4064 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 4065 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4066 | } else { |
| 4067 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4068 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4069 | char fp[MAXPATHLEN+1] = {0}; |
| 4070 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4071 | |
| 4072 | /* Load all possible certs and keys */ |
| 4073 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4074 | struct stat buf; |
| 4075 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 4076 | if (stat(fp, &buf) == 0) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 4077 | 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] | 4078 | goto end; |
| 4079 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4080 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4081 | } |
| 4082 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4083 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4084 | |
| 4085 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 4086 | 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] | 4087 | goto end; |
| 4088 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4089 | /* insert into the ckchs tree */ |
| 4090 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 4091 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4092 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4093 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4094 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4095 | end: |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 4096 | ckch_store_free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4097 | |
| 4098 | return NULL; |
| 4099 | } |
| 4100 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4101 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4102 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4103 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4104 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4105 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4106 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 4107 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4108 | * 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] | 4109 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4110 | * Returns a bitfield containing the flags: |
| 4111 | * ERR_FATAL in any fatal error case |
| 4112 | * ERR_ALERT if the reason of the error is available in err |
| 4113 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4114 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4115 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4116 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4117 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4118 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4119 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4120 | int i = 0, n = 0; |
| 4121 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 4122 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4123 | struct ebmb_node *node; |
| 4124 | struct ebmb_node *next; |
| 4125 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 4126 | * of keytypes |
| 4127 | */ |
| 4128 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4129 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4130 | X509_NAME *xname = NULL; |
| 4131 | char *str = NULL; |
| 4132 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4133 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 4134 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4135 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4136 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4137 | *ckchi = NULL; |
| 4138 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4139 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4140 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4141 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4142 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | ckch_inst = ckch_inst_new(); |
| 4146 | if (!ckch_inst) { |
| 4147 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4148 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4149 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4150 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4151 | } |
| 4152 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4153 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4154 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4155 | /* Process each ckch and update keytypes for each CN/SAN |
| 4156 | * for example, if CN/SAN www.a.com is associated with |
| 4157 | * certs with keytype 0 and 2, then at the end of the loop, |
| 4158 | * www.a.com will have: |
| 4159 | * keyindex = 0 | 1 | 4 = 5 |
| 4160 | */ |
| 4161 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4162 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4163 | |
| 4164 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 4165 | continue; |
| 4166 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4167 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4168 | for (i = 0; i < fcount; i++) { |
| 4169 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 4170 | if (ret < 0) { |
| 4171 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4172 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4173 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4174 | goto end; |
| 4175 | } |
| 4176 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4177 | } else { |
| 4178 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 4179 | * so the line that contains logic is marked via comments |
| 4180 | */ |
| 4181 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 4182 | i = -1; |
| 4183 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4184 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4185 | ASN1_STRING *value; |
| 4186 | value = X509_NAME_ENTRY_get_data(entry); |
| 4187 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4188 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4189 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4190 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4191 | OPENSSL_free(str); |
| 4192 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4193 | if (ret < 0) { |
| 4194 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4195 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4196 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4197 | goto end; |
| 4198 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4199 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4200 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4201 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4202 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4203 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4204 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 4205 | if (names) { |
| 4206 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4207 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4208 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4209 | if (name->type == GEN_DNS) { |
| 4210 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 4211 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4212 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4213 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4214 | OPENSSL_free(str); |
| 4215 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4216 | if (ret < 0) { |
| 4217 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4218 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4219 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4220 | goto end; |
| 4221 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4222 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4223 | } |
| 4224 | } |
| 4225 | } |
| 4226 | } |
| 4227 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 4228 | } |
| 4229 | |
| 4230 | /* If no files found, return error */ |
| 4231 | if (eb_is_empty(&sni_keytypes_map)) { |
| 4232 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4233 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4234 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4235 | goto end; |
| 4236 | } |
| 4237 | |
| 4238 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 4239 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 4240 | * and add each CTX to the SNI tree |
| 4241 | * |
| 4242 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4243 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4244 | * combination is denoted by the key in the map. Each key |
| 4245 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 4246 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 4247 | * entry in the array to correspond to the unique combo (key) |
| 4248 | * associated with i. This unique key combo (i) will be associated |
| 4249 | * with combos[i-1] |
| 4250 | */ |
| 4251 | |
| 4252 | node = ebmb_first(&sni_keytypes_map); |
| 4253 | while (node) { |
| 4254 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4255 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4256 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4257 | |
| 4258 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 4259 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 4260 | cur_ctx = key_combos[i-1].ctx; |
| 4261 | |
| 4262 | if (cur_ctx == NULL) { |
| 4263 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4264 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4265 | if (cur_ctx == NULL) { |
| 4266 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4267 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4268 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4269 | goto end; |
| 4270 | } |
| 4271 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 4272 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4273 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4274 | if (i & (1<<n)) { |
| 4275 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4276 | 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] | 4277 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 4278 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4279 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4283 | /* Update key_combos */ |
| 4284 | key_combos[i-1].ctx = cur_ctx; |
| 4285 | } |
| 4286 | |
| 4287 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4288 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4289 | 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] | 4290 | kinfo, str, key_combos[i-1].order); |
| 4291 | if (key_combos[i-1].order < 0) { |
| 4292 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4293 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4294 | goto end; |
| 4295 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4296 | node = ebmb_next(node); |
| 4297 | } |
| 4298 | |
| 4299 | |
| 4300 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 4301 | if (!bind_conf->default_ctx) { |
| 4302 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 4303 | if (key_combos[i].ctx) { |
| 4304 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4305 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4306 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4307 | SSL_CTX_up_ref(bind_conf->default_ctx); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4308 | break; |
| 4309 | } |
| 4310 | } |
| 4311 | } |
| 4312 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4313 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4314 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4315 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4316 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4317 | end: |
| 4318 | |
| 4319 | if (names) |
| 4320 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 4321 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4322 | node = ebmb_first(&sni_keytypes_map); |
| 4323 | while (node) { |
| 4324 | next = ebmb_next(node); |
| 4325 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 4326 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4327 | node = next; |
| 4328 | } |
| 4329 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4330 | /* we need to free the ctx since we incremented the refcount where it's used */ |
| 4331 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 4332 | if (key_combos[i].ctx) |
| 4333 | SSL_CTX_free(key_combos[i].ctx); |
| 4334 | } |
| 4335 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4336 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4337 | if (ckch_inst->is_default) { |
| 4338 | SSL_CTX_free(bind_conf->default_ctx); |
| 4339 | bind_conf->default_ctx = NULL; |
| 4340 | } |
| 4341 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4342 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4343 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4344 | } |
| 4345 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4346 | *ckchi = ckch_inst; |
| 4347 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4348 | } |
| 4349 | #else |
| 4350 | /* This is a dummy, that just logs an error and returns error */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4351 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4352 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4353 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4354 | { |
| 4355 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4356 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4357 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4358 | } |
| 4359 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4360 | #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] | 4361 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4362 | /* |
| 4363 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4364 | * |
| 4365 | * Returns a bitfield containing the flags: |
| 4366 | * ERR_FATAL in any fatal error case |
| 4367 | * ERR_ALERT if the reason of the error is available in err |
| 4368 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4369 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4370 | static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
| 4371 | 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] | 4372 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4373 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4374 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4375 | int order = 0; |
| 4376 | X509_NAME *xname; |
| 4377 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4378 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4379 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4380 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4381 | STACK_OF(GENERAL_NAME) *names; |
| 4382 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4383 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4384 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4385 | int errcode = 0; |
| 4386 | |
| 4387 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 4388 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4389 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4390 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4391 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4392 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4393 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4394 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 4395 | if (!ctx) { |
| 4396 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4397 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4398 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4399 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4402 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 4403 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4404 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4405 | |
| 4406 | ckch_inst = ckch_inst_new(); |
| 4407 | if (!ckch_inst) { |
| 4408 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4409 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4410 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4411 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4412 | } |
| 4413 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4414 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4415 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4416 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4417 | switch(EVP_PKEY_base_id(pkey)) { |
| 4418 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4419 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4420 | break; |
| 4421 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4422 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 4423 | break; |
| 4424 | case EVP_PKEY_DSA: |
| 4425 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4426 | break; |
| 4427 | } |
| 4428 | EVP_PKEY_free(pkey); |
| 4429 | } |
| 4430 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4431 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4432 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4433 | 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] | 4434 | if (order < 0) { |
| 4435 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4436 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4437 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4438 | } |
| 4439 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4440 | } |
| 4441 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4442 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4443 | 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] | 4444 | if (names) { |
| 4445 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4446 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 4447 | if (name->type == GEN_DNS) { |
| 4448 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4449 | 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] | 4450 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4451 | if (order < 0) { |
| 4452 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4453 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4454 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4455 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4456 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4457 | } |
| 4458 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4459 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4460 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4461 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4462 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4463 | i = -1; |
| 4464 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4465 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4466 | ASN1_STRING *value; |
| 4467 | |
| 4468 | value = X509_NAME_ENTRY_get_data(entry); |
| 4469 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4470 | 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] | 4471 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4472 | if (order < 0) { |
| 4473 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4474 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4475 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4476 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4477 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4478 | } |
| 4479 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4480 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4481 | * the tree, so it will be discovered and cleaned in time. |
| 4482 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4483 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4484 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4485 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4486 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4487 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4488 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4489 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4490 | } |
| 4491 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4492 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4493 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4494 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4495 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4496 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4497 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4498 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4499 | /* everything succeed, the ckch instance can be used */ |
| 4500 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4501 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4502 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4503 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4504 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 4505 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4506 | *ckchi = ckch_inst; |
| 4507 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4508 | |
| 4509 | error: |
| 4510 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4511 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4512 | if (ckch_inst->is_default) |
| 4513 | SSL_CTX_free(ctx); |
| 4514 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4515 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4516 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4517 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4518 | SSL_CTX_free(ctx); |
| 4519 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4520 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4521 | } |
| 4522 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4523 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4524 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4525 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4526 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4527 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4528 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4529 | |
| 4530 | /* we found the ckchs in the tree, we can use it directly */ |
| 4531 | if (ckchs->multi) |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4532 | 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] | 4533 | else |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4534 | 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] | 4535 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4536 | if (errcode & ERR_CODE) |
| 4537 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4538 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4539 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4540 | |
| 4541 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4542 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4543 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4544 | } |
| 4545 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4546 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 4547 | |
| 4548 | |
| 4549 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4550 | * done once. Zero is returned if the operation fails. No error is returned |
| 4551 | * if the random is said as not implemented, because we expect that openssl |
| 4552 | * will use another method once needed. |
| 4553 | */ |
| 4554 | static int ssl_initialize_random() |
| 4555 | { |
| 4556 | unsigned char random; |
| 4557 | static int random_initialized = 0; |
| 4558 | |
| 4559 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4560 | random_initialized = 1; |
| 4561 | |
| 4562 | return random_initialized; |
| 4563 | } |
| 4564 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4565 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4566 | /* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure |
| 4567 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4568 | * |
| 4569 | * This function tries to open and store certificate files. |
| 4570 | */ |
| 4571 | 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] | 4572 | { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4573 | struct crtlist *dir; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4574 | struct dirent **de_list; |
| 4575 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4576 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 4577 | char *end; |
| 4578 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4579 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4580 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4581 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4582 | int is_bundle; |
| 4583 | int j; |
| 4584 | #endif |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4585 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4586 | dir = crtlist_new(path, 1); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4587 | if (dir == NULL) { |
| 4588 | memprintf(err, "not enough memory"); |
| 4589 | return ERR_ALERT | ERR_FATAL; |
| 4590 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4591 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4592 | n = scandir(path, &de_list, 0, alphasort); |
| 4593 | if (n < 0) { |
| 4594 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 4595 | err && *err ? *err : "", path, strerror(errno)); |
| 4596 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4597 | } |
| 4598 | else { |
| 4599 | for (i = 0; i < n; i++) { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4600 | struct crtlist_entry *entry; |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4601 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 4602 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4603 | end = strrchr(de->d_name, '.'); |
| 4604 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key"))) |
| 4605 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4606 | |
William Lallemand | a64593c | 2020-03-17 20:11:41 +0100 | [diff] [blame] | 4607 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 4608 | if (stat(fp, &buf) != 0) { |
| 4609 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4610 | err && *err ? *err : "", fp, strerror(errno)); |
| 4611 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4612 | goto ignore_entry; |
| 4613 | } |
| 4614 | if (!S_ISREG(buf.st_mode)) |
| 4615 | goto ignore_entry; |
| 4616 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4617 | entry = crtlist_entry_new(); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4618 | if (entry == NULL) { |
| 4619 | memprintf(err, "not enough memory '%s'", fp); |
| 4620 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4621 | goto ignore_entry; |
| 4622 | } |
| 4623 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4624 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4625 | is_bundle = 0; |
| 4626 | /* Check if current entry in directory is part of a multi-cert bundle */ |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4627 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4628 | if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) { |
| 4629 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 4630 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 4631 | is_bundle = 1; |
| 4632 | break; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4633 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4634 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4635 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4636 | if (is_bundle) { |
| 4637 | int dp_len; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4638 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4639 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4640 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4641 | /* increment i and free de until we get to a non-bundle cert |
| 4642 | * Note here that we look at de_list[i + 1] before freeing de |
| 4643 | * this is important since ignore_entry will free de. This also |
| 4644 | * guarantees that de->d_name continues to hold the same prefix. |
| 4645 | */ |
| 4646 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
| 4647 | free(de); |
| 4648 | i++; |
| 4649 | de = de_list[i]; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4650 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4651 | |
| 4652 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4653 | ckchs = ckchs_lookup(fp); |
| 4654 | if (ckchs == NULL) |
| 4655 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 4656 | if (ckchs == NULL) { |
| 4657 | free(de); |
| 4658 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4659 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4660 | goto end; |
| 4661 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4662 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4663 | entry->crtlist = dir; |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4664 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4665 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4666 | ebpt_insert(&dir->entries, &entry->node); |
| 4667 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4668 | /* Successfully processed the bundle */ |
| 4669 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4670 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4671 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4672 | |
| 4673 | #endif |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4674 | ckchs = ckchs_lookup(fp); |
| 4675 | if (ckchs == NULL) |
| 4676 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 4677 | if (ckchs == NULL) { |
| 4678 | free(de); |
| 4679 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4680 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4681 | goto end; |
| 4682 | } |
| 4683 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4684 | entry->crtlist = dir; |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4685 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4686 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4687 | ebpt_insert(&dir->entries, &entry->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4688 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4689 | ignore_entry: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4690 | free(de); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4691 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4692 | end: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4693 | free(de_list); |
| 4694 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4695 | |
| 4696 | if (cfgerr & ERR_CODE) { |
| 4697 | /* free the dir and entries on error */ |
William Lallemand | 09bd5a0 | 2020-03-30 18:19:43 +0200 | [diff] [blame] | 4698 | crtlist_free(dir); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4699 | } else { |
| 4700 | *crtlist = dir; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4701 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4702 | return cfgerr; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4703 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4704 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4705 | |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4706 | /* |
| 4707 | * Read a single crt-list line. /!\ alter the <line> string. |
| 4708 | * Fill <crt_path> and <crtlist_entry> |
| 4709 | * <crtlist_entry> must be alloc and free by the caller |
| 4710 | * <crtlist_entry->ssl_conf> is alloc by the function |
| 4711 | * <crtlist_entry->filters> is alloc by the function |
| 4712 | * <crt_path> is a ptr in <line> |
| 4713 | * Return an error code |
| 4714 | */ |
| 4715 | static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err) |
| 4716 | { |
| 4717 | int cfgerr = 0; |
| 4718 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
| 4719 | char *end; |
| 4720 | char *args[MAX_CRT_ARGS + 1]; |
| 4721 | struct ssl_bind_conf *ssl_conf = NULL; |
| 4722 | |
| 4723 | if (!line || !crt_path || !entry) |
| 4724 | return ERR_ALERT | ERR_FATAL; |
| 4725 | |
| 4726 | end = line + strlen(line); |
| 4727 | if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') { |
| 4728 | /* Check if we reached the limit and the last char is not \n. |
| 4729 | * Watch out for the last line without the terminating '\n'! |
| 4730 | */ |
| 4731 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4732 | linenum, file, CRT_LINESIZE-1); |
| 4733 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4734 | goto error; |
| 4735 | } |
| 4736 | arg = 0; |
| 4737 | newarg = 1; |
| 4738 | while (*line) { |
| 4739 | if (isspace((unsigned char)*line)) { |
| 4740 | newarg = 1; |
| 4741 | *line = 0; |
| 4742 | } else if (*line == '[') { |
| 4743 | if (ssl_b) { |
| 4744 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 4745 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4746 | goto error; |
| 4747 | } |
| 4748 | if (!arg) { |
| 4749 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 4750 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4751 | goto error; |
| 4752 | } |
| 4753 | ssl_b = arg; |
| 4754 | newarg = 1; |
| 4755 | *line = 0; |
| 4756 | } else if (*line == ']') { |
| 4757 | if (ssl_e) { |
| 4758 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
| 4759 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4760 | goto error; |
| 4761 | } |
| 4762 | if (!ssl_b) { |
| 4763 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 4764 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4765 | goto error; |
| 4766 | } |
| 4767 | ssl_e = arg; |
| 4768 | newarg = 1; |
| 4769 | *line = 0; |
| 4770 | } else if (newarg) { |
| 4771 | if (arg == MAX_CRT_ARGS) { |
| 4772 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 4773 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4774 | goto error; |
| 4775 | } |
| 4776 | newarg = 0; |
| 4777 | args[arg++] = line; |
| 4778 | } |
| 4779 | line++; |
| 4780 | } |
| 4781 | args[arg++] = line; |
| 4782 | |
| 4783 | /* empty line */ |
| 4784 | if (!*args[0]) { |
| 4785 | cfgerr |= ERR_NONE; |
| 4786 | goto error; |
| 4787 | } |
| 4788 | |
| 4789 | *crt_path = args[0]; |
| 4790 | |
William Lallemand | 1b2988b | 2020-04-10 17:20:45 +0200 | [diff] [blame] | 4791 | if (ssl_b) { |
| 4792 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4793 | if (!ssl_conf) { |
| 4794 | memprintf(err, "not enough memory!"); |
| 4795 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4796 | goto error; |
| 4797 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4798 | } |
| 4799 | cur_arg = ssl_b ? ssl_b : 1; |
| 4800 | while (cur_arg < ssl_e) { |
| 4801 | newarg = 0; |
| 4802 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4803 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4804 | newarg = 1; |
| 4805 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err); |
| 4806 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4807 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4808 | args[cur_arg], linenum, file); |
| 4809 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4810 | goto error; |
| 4811 | } |
| 4812 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4813 | break; |
| 4814 | } |
| 4815 | } |
| 4816 | if (!cfgerr && !newarg) { |
| 4817 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4818 | args[cur_arg], linenum, file); |
| 4819 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4820 | goto error; |
| 4821 | } |
| 4822 | } |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 4823 | entry->linenum = linenum; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4824 | entry->ssl_conf = ssl_conf; |
| 4825 | entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1); |
| 4826 | entry->fcount = arg - cur_arg - 1; |
| 4827 | |
| 4828 | return cfgerr; |
| 4829 | |
| 4830 | error: |
| 4831 | crtlist_free_filters(entry->filters); |
| 4832 | entry->filters = NULL; |
| 4833 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 4834 | free(entry->ssl_conf); |
| 4835 | entry->ssl_conf = NULL; |
| 4836 | return cfgerr; |
| 4837 | } |
| 4838 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4839 | /* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure |
| 4840 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4841 | * |
| 4842 | * This function tries to open and store certificate files. |
| 4843 | */ |
| 4844 | static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err) |
| 4845 | { |
| 4846 | struct crtlist *newlist; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4847 | struct crtlist_entry *entry = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4848 | char thisline[CRT_LINESIZE]; |
| 4849 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4850 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4851 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4852 | int linenum = 0; |
| 4853 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4854 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4855 | if ((f = fopen(file, "r")) == NULL) { |
| 4856 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4857 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4858 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4859 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4860 | newlist = crtlist_new(file, 0); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4861 | if (newlist == NULL) { |
| 4862 | memprintf(err, "Not enough memory!"); |
| 4863 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4864 | goto error; |
| 4865 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4866 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4867 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4868 | char *end; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4869 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4870 | char *crt_path; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4871 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4872 | |
| 4873 | linenum++; |
| 4874 | end = line + strlen(line); |
| 4875 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4876 | /* Check if we reached the limit and the last char is not \n. |
| 4877 | * Watch out for the last line without the terminating '\n'! |
| 4878 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4879 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4880 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4881 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4882 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4883 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4884 | |
| 4885 | if (*line == '#' || *line == '\n' || *line == '\r') |
| 4886 | continue; |
| 4887 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4888 | entry = crtlist_entry_new(); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4889 | if (entry == NULL) { |
| 4890 | memprintf(err, "Not enough memory!"); |
| 4891 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4892 | goto error; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4893 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4894 | |
| 4895 | *(end - 1) = '\0'; /* line parser mustn't receive any \n */ |
| 4896 | cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4897 | if (cfgerr) |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4898 | goto error; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4899 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4900 | /* empty line */ |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4901 | if (!crt_path || !*crt_path) { |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4902 | crtlist_entry_free(entry); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4903 | entry = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4904 | continue; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4905 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4906 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4907 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4908 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4909 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4910 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4911 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4912 | goto error; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4913 | } |
| 4914 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4915 | crt_path = path; |
| 4916 | } |
| 4917 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4918 | /* Look for a ckch_store or create one */ |
| 4919 | ckchs = ckchs_lookup(crt_path); |
| 4920 | if (ckchs == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4921 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4922 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4923 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4924 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4925 | } |
William Lallemand | 909086e | 2020-03-17 16:53:27 +0100 | [diff] [blame] | 4926 | if (ckchs == NULL) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4927 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4928 | |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4929 | if (cfgerr & ERR_CODE) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4930 | goto error; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4931 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4932 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4933 | entry->crtlist = newlist; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4934 | ebpt_insert(&newlist->entries, &entry->node); |
| 4935 | LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist); |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4936 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4937 | |
| 4938 | entry = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4939 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4940 | if (cfgerr & ERR_CODE) |
| 4941 | goto error; |
| 4942 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 4943 | newlist->linecount = linenum; |
| 4944 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4945 | fclose(f); |
| 4946 | *crtlist = newlist; |
| 4947 | |
| 4948 | return cfgerr; |
| 4949 | error: |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4950 | crtlist_entry_free(entry); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4951 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4952 | fclose(f); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4953 | crtlist_free(newlist); |
| 4954 | return cfgerr; |
| 4955 | } |
| 4956 | |
| 4957 | /* Load a crt-list file, this is done in 2 parts: |
| 4958 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 4959 | * - generate the instances by iterating on entries in the crtlist struct |
| 4960 | * |
| 4961 | * Nothing is locked there, this function is used in the configuration parser. |
| 4962 | * |
| 4963 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4964 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4965 | 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] | 4966 | { |
| 4967 | struct crtlist *crtlist = NULL; |
| 4968 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4969 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4970 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4971 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4972 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4973 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4974 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 4975 | if (!bind_conf_node) { |
| 4976 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 4977 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4978 | goto error; |
| 4979 | } |
| 4980 | bind_conf_node->next = NULL; |
| 4981 | bind_conf_node->bind_conf = bind_conf; |
| 4982 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4983 | /* strip trailing slashes, including first one */ |
| 4984 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 4985 | *end = 0; |
| 4986 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4987 | /* look for an existing crtlist or create one */ |
| 4988 | eb = ebst_lookup(&crtlists_tree, file); |
| 4989 | if (eb) { |
| 4990 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 4991 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4992 | /* load a crt-list OR a directory */ |
| 4993 | if (dir) |
| 4994 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 4995 | else |
| 4996 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 4997 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4998 | if (!(cfgerr & ERR_CODE)) |
| 4999 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 5000 | } |
| 5001 | |
| 5002 | if (cfgerr & ERR_CODE) { |
| 5003 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 5004 | goto error; |
| 5005 | } |
| 5006 | |
| 5007 | /* generates ckch instance from the crtlist_entry */ |
| 5008 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 5009 | struct ckch_store *store; |
| 5010 | struct ckch_inst *ckch_inst = NULL; |
| 5011 | |
| 5012 | store = entry->node.key; |
| 5013 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 5014 | if (cfgerr & ERR_CODE) { |
| 5015 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 5016 | goto error; |
| 5017 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5018 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 5019 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5020 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5021 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 5022 | /* add the bind_conf to the list */ |
| 5023 | bind_conf_node->next = crtlist->bind_conf; |
| 5024 | crtlist->bind_conf = bind_conf_node; |
| 5025 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5026 | return cfgerr; |
| 5027 | error: |
| 5028 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5029 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5030 | struct ckch_inst *inst, *s_inst; |
| 5031 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5032 | lastentry = entry; /* which entry we tried to generate last */ |
| 5033 | if (lastentry) { |
| 5034 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 5035 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 5036 | break; |
| 5037 | |
| 5038 | list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5039 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5040 | /* this was not generated for this bind_conf, skip */ |
| 5041 | if (inst->bind_conf != bind_conf) |
| 5042 | continue; |
| 5043 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 5044 | /* free the sni_ctx and instance */ |
| 5045 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5046 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5047 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5048 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 5049 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5050 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5051 | return cfgerr; |
| 5052 | } |
| 5053 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 5054 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 5055 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 5056 | { |
| 5057 | struct stat buf; |
| 5058 | char fp[MAXPATHLEN+1]; |
| 5059 | int cfgerr = 0; |
| 5060 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5061 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 5062 | |
| 5063 | if ((ckchs = ckchs_lookup(path))) { |
| 5064 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5065 | 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] | 5066 | } |
| 5067 | if (stat(path, &buf) == 0) { |
| 5068 | if (S_ISDIR(buf.st_mode) == 0) { |
| 5069 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 5070 | if (!ckchs) |
| 5071 | return ERR_ALERT | ERR_FATAL; |
| 5072 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5073 | 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] | 5074 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 5075 | 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] | 5076 | } |
| 5077 | } else { |
| 5078 | /* stat failed, could be a bundle */ |
| 5079 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 5080 | /* try to load a bundle if it is permitted */ |
| 5081 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 5082 | if (!ckchs) |
| 5083 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5084 | 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] | 5085 | } else { |
| 5086 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 5087 | err && *err ? *err : "", fp, strerror(errno)); |
| 5088 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | return cfgerr; |
| 5093 | } |
| 5094 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5095 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5096 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5097 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5098 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5099 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5100 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5101 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5102 | SSL_OP_NO_SSLv2 | |
| 5103 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 5104 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5105 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5106 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 5107 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5108 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5109 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5110 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5111 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5112 | SSL_MODE_RELEASE_BUFFERS | |
| 5113 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 5114 | 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] | 5115 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5116 | int flags = MC_SSL_O_ALL; |
| 5117 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5118 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5119 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5120 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5121 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5122 | 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] | 5123 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 5124 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5125 | 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] | 5126 | else |
| 5127 | flags = conf_ssl_methods->flags; |
| 5128 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 5129 | min = conf_ssl_methods->min; |
| 5130 | max = conf_ssl_methods->max; |
| 5131 | /* start with TLSv10 to remove SSLv3 per default */ |
| 5132 | if (!min && (!max || max >= CONF_TLSV10)) |
| 5133 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5134 | /* 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] | 5135 | if (min) |
| 5136 | flags |= (methodVersions[min].flag - 1); |
| 5137 | if (max) |
| 5138 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5139 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5140 | min = max = CONF_TLSV_NONE; |
| 5141 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5142 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5143 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5144 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5145 | if (min) { |
| 5146 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5147 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 5148 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5149 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 5150 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5151 | hole = 0; |
| 5152 | } |
| 5153 | max = i; |
| 5154 | } |
| 5155 | else { |
| 5156 | min = max = i; |
| 5157 | } |
| 5158 | } |
| 5159 | else { |
| 5160 | if (min) |
| 5161 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5162 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5163 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5164 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5165 | 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] | 5166 | cfgerr += 1; |
| 5167 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5168 | /* save real min/max in bind_conf */ |
| 5169 | conf_ssl_methods->min = min; |
| 5170 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5171 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5172 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5173 | /* 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] | 5174 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5175 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5176 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5177 | else |
| 5178 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5179 | if (flags & methodVersions[i].flag) |
| 5180 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5181 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5182 | /* 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] | 5183 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5184 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 5185 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5186 | |
| 5187 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 5188 | options |= SSL_OP_NO_TICKET; |
| 5189 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 5190 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 5191 | |
| 5192 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5193 | options |= SSL_OP_NO_RENEGOTIATION; |
| 5194 | #endif |
| 5195 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5196 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5197 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5198 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5199 | if (global_ssl.async) |
| 5200 | mode |= SSL_MODE_ASYNC; |
| 5201 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5202 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5203 | if (global_ssl.life_time) |
| 5204 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5205 | |
| 5206 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5207 | #ifdef OPENSSL_IS_BORINGSSL |
| 5208 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 5209 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 5210 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 5211 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 5212 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 5213 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 5214 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5215 | #else |
| 5216 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5217 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 5218 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5219 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5220 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5221 | } |
| 5222 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5223 | |
| 5224 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 5225 | { |
| 5226 | if (first == block) { |
| 5227 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5228 | if (first->len > 0) |
| 5229 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 5230 | } |
| 5231 | } |
| 5232 | |
| 5233 | /* return first block from sh_ssl_sess */ |
| 5234 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 5235 | { |
| 5236 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 5237 | |
| 5238 | } |
| 5239 | |
| 5240 | /* store a session into the cache |
| 5241 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 5242 | * data: asn1 encoded session |
| 5243 | * data_len: asn1 encoded session length |
| 5244 | * Returns 1 id session was stored (else 0) |
| 5245 | */ |
| 5246 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 5247 | { |
| 5248 | struct shared_block *first; |
| 5249 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 5250 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5251 | 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] | 5252 | if (!first) { |
| 5253 | /* Could not retrieve enough free blocks to store that session */ |
| 5254 | return 0; |
| 5255 | } |
| 5256 | |
| 5257 | /* STORE the key in the first elem */ |
| 5258 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5259 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 5260 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5261 | |
| 5262 | /* it returns the already existing node |
| 5263 | or current node if none, never returns null */ |
| 5264 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 5265 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 5266 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 5267 | /* release the reserved row */ |
| 5268 | shctx_row_dec_hot(ssl_shctx, first); |
| 5269 | /* replace the previous session already in the tree */ |
| 5270 | sh_ssl_sess = oldsh_ssl_sess; |
| 5271 | /* ignore the previous session data, only use the header */ |
| 5272 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 5273 | shctx_row_inc_hot(ssl_shctx, first); |
| 5274 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5275 | } |
| 5276 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5277 | 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] | 5278 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5279 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5283 | |
| 5284 | return 1; |
| 5285 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5286 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5287 | /* SSL callback used when a new session is created while connecting to a server */ |
| 5288 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 5289 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5290 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5291 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5292 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5293 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5294 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5295 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 5296 | int len; |
| 5297 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5298 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5299 | len = i2d_SSL_SESSION(sess, NULL); |
| 5300 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 5301 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 5302 | } else { |
| 5303 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5304 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 5305 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 5306 | } |
| 5307 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 5308 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 5309 | &ptr); |
| 5310 | } |
| 5311 | } else { |
| 5312 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5313 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 5314 | } |
| 5315 | |
| 5316 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5317 | } |
| 5318 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5319 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5320 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5321 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5322 | { |
| 5323 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 5324 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 5325 | unsigned char *p; |
| 5326 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5327 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5328 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5329 | |
| 5330 | /* 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] | 5331 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5332 | * note: SSL_SESSION_set1_id is using |
| 5333 | * a memcpy so we need to use a different pointer |
| 5334 | * than sid_data or sid_ctx_data to avoid valgrind |
| 5335 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5336 | */ |
| 5337 | |
| 5338 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5339 | |
| 5340 | /* copy value in an other buffer */ |
| 5341 | memcpy(encid, sid_data, sid_length); |
| 5342 | |
| 5343 | /* pad with 0 */ |
| 5344 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 5345 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 5346 | |
| 5347 | /* force length to zero to avoid ASN1 encoding */ |
| 5348 | SSL_SESSION_set1_id(sess, encid, 0); |
| 5349 | |
| 5350 | /* force length to zero to avoid ASN1 encoding */ |
| 5351 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5352 | |
| 5353 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 5354 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 5355 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 5356 | goto err; |
| 5357 | |
| 5358 | p = encsess; |
| 5359 | |
| 5360 | /* process ASN1 session encoding before the lock */ |
| 5361 | i2d_SSL_SESSION(sess, &p); |
| 5362 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5363 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5364 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5365 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5366 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5367 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5368 | err: |
| 5369 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5370 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 5371 | 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] | 5372 | |
| 5373 | return 0; /* do not increment session reference count */ |
| 5374 | } |
| 5375 | |
| 5376 | /* 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] | 5377 | 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] | 5378 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5379 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5380 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 5381 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5382 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5383 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5384 | |
| 5385 | global.shctx_lookups++; |
| 5386 | |
| 5387 | /* allow the session to be freed automatically by openssl */ |
| 5388 | *do_copy = 0; |
| 5389 | |
| 5390 | /* tree key is zeros padded sessionid */ |
| 5391 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5392 | memcpy(tmpkey, key, key_len); |
| 5393 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 5394 | key = tmpkey; |
| 5395 | } |
| 5396 | |
| 5397 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5398 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5399 | |
| 5400 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5401 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 5402 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5403 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5404 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5405 | global.shctx_misses++; |
| 5406 | return NULL; |
| 5407 | } |
| 5408 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5409 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 5410 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5411 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5412 | 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] | 5413 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5414 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5415 | |
| 5416 | /* decode ASN1 session */ |
| 5417 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5418 | 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] | 5419 | /* Reset session id and session id contenxt */ |
| 5420 | if (sess) { |
| 5421 | SSL_SESSION_set1_id(sess, key, key_len); |
| 5422 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5423 | } |
| 5424 | |
| 5425 | return sess; |
| 5426 | } |
| 5427 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5428 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5429 | /* 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] | 5430 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5431 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5432 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5433 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 5434 | unsigned int sid_length; |
| 5435 | const unsigned char *sid_data; |
| 5436 | (void)ctx; |
| 5437 | |
| 5438 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 5439 | /* tree key is zeros padded sessionid */ |
| 5440 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5441 | memcpy(tmpkey, sid_data, sid_length); |
| 5442 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 5443 | sid_data = tmpkey; |
| 5444 | } |
| 5445 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5446 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5447 | |
| 5448 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5449 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 5450 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5451 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5452 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5456 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /* Set session cache mode to server and disable openssl internal cache. |
| 5460 | * Set shared cache callbacks on an ssl context. |
| 5461 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5462 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5463 | { |
| 5464 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5465 | |
| 5466 | if (!ssl_shctx) { |
| 5467 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 5468 | return; |
| 5469 | } |
| 5470 | |
| 5471 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 5472 | SSL_SESS_CACHE_NO_INTERNAL | |
| 5473 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 5474 | |
| 5475 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5476 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 5477 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 5478 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5479 | } |
| 5480 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5481 | /* |
| 5482 | * This function applies the SSL configuration on a SSL_CTX |
| 5483 | * It returns an error code and fills the <err> buffer |
| 5484 | */ |
| 5485 | 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] | 5486 | { |
| 5487 | struct proxy *curproxy = bind_conf->frontend; |
| 5488 | int cfgerr = 0; |
| 5489 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5490 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5491 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5492 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5493 | const char *conf_ciphersuites; |
| 5494 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5495 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5496 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5497 | if (ssl_conf) { |
| 5498 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5499 | int i, min, max; |
| 5500 | int flags = MC_SSL_O_ALL; |
| 5501 | |
| 5502 | /* 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] | 5503 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5504 | 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] | 5505 | if (min) |
| 5506 | flags |= (methodVersions[min].flag - 1); |
| 5507 | if (max) |
| 5508 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5509 | min = max = CONF_TLSV_NONE; |
| 5510 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5511 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5512 | if (min) |
| 5513 | max = i; |
| 5514 | else |
| 5515 | min = max = i; |
| 5516 | } |
| 5517 | /* save real min/max */ |
| 5518 | conf_ssl_methods->min = min; |
| 5519 | conf_ssl_methods->max = max; |
| 5520 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5521 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5522 | 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] | 5523 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5524 | } |
| 5525 | } |
| 5526 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5527 | 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] | 5528 | case SSL_SOCK_VERIFY_NONE: |
| 5529 | verify = SSL_VERIFY_NONE; |
| 5530 | break; |
| 5531 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5532 | verify = SSL_VERIFY_PEER; |
| 5533 | break; |
| 5534 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5535 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5536 | break; |
| 5537 | } |
| 5538 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5539 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5540 | 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] | 5541 | 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] | 5542 | 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] | 5543 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5544 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5545 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5546 | 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] | 5547 | 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] | 5548 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5549 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5550 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 5551 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 5552 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5553 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5554 | } |
| 5555 | 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] | 5556 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5557 | 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] | 5558 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5559 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5560 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5561 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5562 | 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] | 5563 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5564 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5565 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5566 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5567 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5568 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5569 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5570 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5571 | 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] | 5572 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5573 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5574 | else { |
| 5575 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5576 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5577 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5578 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5579 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5580 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5581 | #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] | 5582 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5583 | 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] | 5584 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5585 | 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] | 5586 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5587 | } |
| 5588 | } |
| 5589 | #endif |
| 5590 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5591 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5592 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5593 | if (conf_ciphers && |
| 5594 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5595 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5596 | 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] | 5597 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5598 | } |
| 5599 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5600 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5601 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5602 | if (conf_ciphersuites && |
| 5603 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5604 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5605 | 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] | 5606 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5607 | } |
| 5608 | #endif |
| 5609 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5610 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5611 | /* If tune.ssl.default-dh-param has not been set, |
| 5612 | neither has ssl-default-dh-file and no static DH |
| 5613 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5614 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5615 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 5616 | (ssl_dh_ptr_index == -1 || |
| 5617 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5618 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 5619 | const SSL_CIPHER * cipher = NULL; |
| 5620 | char cipher_description[128]; |
| 5621 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 5622 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 5623 | which is not ephemeral DH. */ |
| 5624 | const char dhe_description[] = " Kx=DH "; |
| 5625 | const char dhe_export_description[] = " Kx=DH("; |
| 5626 | int idx = 0; |
| 5627 | int dhe_found = 0; |
| 5628 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5629 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5630 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5631 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5632 | if (ssl) { |
| 5633 | ciphers = SSL_get_ciphers(ssl); |
| 5634 | |
| 5635 | if (ciphers) { |
| 5636 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 5637 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 5638 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 5639 | if (strstr(cipher_description, dhe_description) != NULL || |
| 5640 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 5641 | dhe_found = 1; |
| 5642 | break; |
| 5643 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 5644 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5645 | } |
| 5646 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5647 | SSL_free(ssl); |
| 5648 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5649 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5650 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5651 | if (dhe_found) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5652 | 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", |
| 5653 | err && *err ? *err : ""); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5654 | cfgerr |= ERR_WARN; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5655 | } |
| 5656 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5657 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5658 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5659 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5660 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5661 | if (local_dh_1024 == NULL) { |
| 5662 | local_dh_1024 = ssl_get_dh_1024(); |
| 5663 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5664 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5665 | if (local_dh_2048 == NULL) { |
| 5666 | local_dh_2048 = ssl_get_dh_2048(); |
| 5667 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5668 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5669 | if (local_dh_4096 == NULL) { |
| 5670 | local_dh_4096 = ssl_get_dh_4096(); |
| 5671 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5672 | } |
| 5673 | } |
| 5674 | } |
| 5675 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5676 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5677 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5678 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5679 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5680 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5681 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5682 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5683 | ssl_conf_cur = NULL; |
| 5684 | if (ssl_conf && ssl_conf->npn_str) |
| 5685 | ssl_conf_cur = ssl_conf; |
| 5686 | else if (bind_conf->ssl_conf.npn_str) |
| 5687 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5688 | if (ssl_conf_cur) |
| 5689 | 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] | 5690 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5691 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5692 | ssl_conf_cur = NULL; |
| 5693 | if (ssl_conf && ssl_conf->alpn_str) |
| 5694 | ssl_conf_cur = ssl_conf; |
| 5695 | else if (bind_conf->ssl_conf.alpn_str) |
| 5696 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5697 | if (ssl_conf_cur) |
| 5698 | 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] | 5699 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 5700 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5701 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5702 | if (conf_curves) { |
| 5703 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5704 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5705 | 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] | 5706 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5707 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5708 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5709 | } |
| 5710 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5711 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5712 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5713 | int i; |
| 5714 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5715 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5716 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5717 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5718 | NULL); |
| 5719 | |
| 5720 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 5721 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5722 | return cfgerr; |
| 5723 | } |
| 5724 | #else |
| 5725 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5726 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5727 | ECDHE_DEFAULT_CURVE); |
| 5728 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5729 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5730 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5731 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5732 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5733 | 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] | 5734 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5735 | } |
| 5736 | else { |
| 5737 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5738 | EC_KEY_free(ecdh); |
| 5739 | } |
| 5740 | } |
| 5741 | #endif |
| 5742 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5743 | return cfgerr; |
| 5744 | } |
| 5745 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5746 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5747 | { |
| 5748 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5749 | size_t prefixlen, suffixlen; |
| 5750 | |
| 5751 | /* Trivial case */ |
| 5752 | if (strcmp(pattern, hostname) == 0) |
| 5753 | return 1; |
| 5754 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5755 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5756 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5757 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5758 | pattern_wildcard = NULL; |
| 5759 | pattern_left_label_end = pattern; |
| 5760 | while (*pattern_left_label_end != '.') { |
| 5761 | switch (*pattern_left_label_end) { |
| 5762 | case 0: |
| 5763 | /* End of label not found */ |
| 5764 | return 0; |
| 5765 | case '*': |
| 5766 | /* If there is more than one wildcards */ |
| 5767 | if (pattern_wildcard) |
| 5768 | return 0; |
| 5769 | pattern_wildcard = pattern_left_label_end; |
| 5770 | break; |
| 5771 | } |
| 5772 | pattern_left_label_end++; |
| 5773 | } |
| 5774 | |
| 5775 | /* If it's not trivial and there is no wildcard, it can't |
| 5776 | * match */ |
| 5777 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5778 | return 0; |
| 5779 | |
| 5780 | /* Make sure all labels match except the leftmost */ |
| 5781 | hostname_left_label_end = strchr(hostname, '.'); |
| 5782 | if (!hostname_left_label_end |
| 5783 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5784 | return 0; |
| 5785 | |
| 5786 | /* Make sure the leftmost label of the hostname is long enough |
| 5787 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5788 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5789 | return 0; |
| 5790 | |
| 5791 | /* Finally compare the string on either side of the |
| 5792 | * wildcard */ |
| 5793 | prefixlen = pattern_wildcard - pattern; |
| 5794 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5795 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5796 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5797 | return 0; |
| 5798 | |
| 5799 | return 1; |
| 5800 | } |
| 5801 | |
| 5802 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5803 | { |
| 5804 | SSL *ssl; |
| 5805 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5806 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5807 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5808 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5809 | |
| 5810 | int depth; |
| 5811 | X509 *cert; |
| 5812 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5813 | int i; |
| 5814 | X509_NAME *cert_subject; |
| 5815 | char *str; |
| 5816 | |
| 5817 | if (ok == 0) |
| 5818 | return ok; |
| 5819 | |
| 5820 | 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] | 5821 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5822 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5823 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5824 | /* We're checking if the provided hostnames match the desired one. The |
| 5825 | * desired hostname comes from the SNI we presented if any, or if not |
| 5826 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5827 | * directive. If neither is set, we don't care about the name so the |
| 5828 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5829 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5830 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5831 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5832 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5833 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5834 | if (!servername) |
| 5835 | return ok; |
| 5836 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5837 | |
| 5838 | /* We only need to verify the CN on the actual server cert, |
| 5839 | * not the indirect CAs */ |
| 5840 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5841 | if (depth != 0) |
| 5842 | return ok; |
| 5843 | |
| 5844 | /* At this point, the cert is *not* OK unless we can find a |
| 5845 | * hostname match */ |
| 5846 | ok = 0; |
| 5847 | |
| 5848 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5849 | /* It seems like this might happen if verify peer isn't set */ |
| 5850 | if (!cert) |
| 5851 | return ok; |
| 5852 | |
| 5853 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5854 | if (alt_names) { |
| 5855 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5856 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5857 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5858 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5859 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5860 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5861 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5862 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5863 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5864 | OPENSSL_free(str); |
| 5865 | } |
| 5866 | } |
| 5867 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5868 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | cert_subject = X509_get_subject_name(cert); |
| 5872 | i = -1; |
| 5873 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5874 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5875 | ASN1_STRING *value; |
| 5876 | value = X509_NAME_ENTRY_get_data(entry); |
| 5877 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5878 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5879 | OPENSSL_free(str); |
| 5880 | } |
| 5881 | } |
| 5882 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5883 | /* report the mismatch and indicate if SNI was used or not */ |
| 5884 | if (!ok && !conn->err_code) |
| 5885 | 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] | 5886 | return ok; |
| 5887 | } |
| 5888 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5889 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5890 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5891 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5892 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5893 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5894 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5895 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5896 | SSL_OP_NO_SSLv2 | |
| 5897 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5898 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5899 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5900 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5901 | SSL_MODE_RELEASE_BUFFERS | |
| 5902 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5903 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5904 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5905 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5906 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5907 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5908 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5909 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5910 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5911 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5912 | cfgerr++; |
| 5913 | } |
| 5914 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5915 | /* Automatic memory computations need to know we use SSL there */ |
| 5916 | global.ssl_used_backend = 1; |
| 5917 | |
| 5918 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5919 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5920 | 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] | 5921 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5922 | curproxy->id, srv->id, |
| 5923 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5924 | cfgerr++; |
| 5925 | return cfgerr; |
| 5926 | } |
| 5927 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 5928 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5929 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5930 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5931 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5932 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5933 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5934 | proxy_type_str(curproxy), curproxy->id, |
| 5935 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5936 | cfgerr++; |
| 5937 | return cfgerr; |
| 5938 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5939 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5940 | 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] | 5941 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5942 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5943 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5944 | else |
| 5945 | flags = conf_ssl_methods->flags; |
| 5946 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5947 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5948 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5949 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5950 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5951 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5952 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5953 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5954 | min = max = CONF_TLSV_NONE; |
| 5955 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5956 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5957 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5958 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5959 | if (min) { |
| 5960 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5961 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5962 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5963 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5964 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5965 | hole = 0; |
| 5966 | } |
| 5967 | max = i; |
| 5968 | } |
| 5969 | else { |
| 5970 | min = max = i; |
| 5971 | } |
| 5972 | } |
| 5973 | else { |
| 5974 | if (min) |
| 5975 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5976 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5977 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5978 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5979 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5980 | cfgerr += 1; |
| 5981 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5982 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5983 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5984 | /* 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] | 5985 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5986 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5987 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5988 | else |
| 5989 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5990 | if (flags & methodVersions[i].flag) |
| 5991 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5992 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5993 | /* 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] | 5994 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5995 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5996 | #endif |
| 5997 | |
| 5998 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5999 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6000 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6001 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6002 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6003 | if (global_ssl.async) |
| 6004 | mode |= SSL_MODE_ASYNC; |
| 6005 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6006 | SSL_CTX_set_mode(ctx, mode); |
| 6007 | srv->ssl_ctx.ctx = ctx; |
| 6008 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6009 | if (srv->ssl_ctx.client_crt) { |
| 6010 | 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] | 6011 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 6012 | proxy_type_str(curproxy), curproxy->id, |
| 6013 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6014 | cfgerr++; |
| 6015 | } |
| 6016 | 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] | 6017 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 6018 | proxy_type_str(curproxy), curproxy->id, |
| 6019 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6020 | cfgerr++; |
| 6021 | } |
| 6022 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6023 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 6024 | proxy_type_str(curproxy), curproxy->id, |
| 6025 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6026 | cfgerr++; |
| 6027 | } |
| 6028 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6029 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6030 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 6031 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6032 | switch (srv->ssl_ctx.verify) { |
| 6033 | case SSL_SOCK_VERIFY_NONE: |
| 6034 | verify = SSL_VERIFY_NONE; |
| 6035 | break; |
| 6036 | case SSL_SOCK_VERIFY_REQUIRED: |
| 6037 | verify = SSL_VERIFY_PEER; |
| 6038 | break; |
| 6039 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6040 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6041 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 6042 | (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] | 6043 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6044 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 6045 | /* set CAfile to verify */ |
| 6046 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 6047 | 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] | 6048 | curproxy->id, srv->id, |
| 6049 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6050 | cfgerr++; |
| 6051 | } |
| 6052 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6053 | else { |
| 6054 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6055 | 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", |
| 6056 | curproxy->id, srv->id, |
| 6057 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6058 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6059 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 6060 | curproxy->id, srv->id, |
| 6061 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6062 | cfgerr++; |
| 6063 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6064 | #ifdef X509_V_FLAG_CRL_CHECK |
| 6065 | if (srv->ssl_ctx.crl_file) { |
| 6066 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 6067 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 6068 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6069 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 6070 | curproxy->id, srv->id, |
| 6071 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6072 | cfgerr++; |
| 6073 | } |
| 6074 | else { |
| 6075 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6076 | } |
| 6077 | } |
| 6078 | #endif |
| 6079 | } |
| 6080 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 6081 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 6082 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 6083 | 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] | 6084 | if (srv->ssl_ctx.ciphers && |
| 6085 | !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] | 6086 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 6087 | curproxy->id, srv->id, |
| 6088 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6089 | cfgerr++; |
| 6090 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6091 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6092 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6093 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 6094 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6095 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 6096 | curproxy->id, srv->id, |
| 6097 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 6098 | cfgerr++; |
| 6099 | } |
| 6100 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6101 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 6102 | if (srv->ssl_ctx.npn_str) |
| 6103 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 6104 | #endif |
| 6105 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6106 | if (srv->ssl_ctx.alpn_str) |
| 6107 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 6108 | #endif |
| 6109 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6110 | |
| 6111 | return cfgerr; |
| 6112 | } |
| 6113 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6114 | /* 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] | 6115 | * be NULL, in which case nothing is done. Returns the number of errors |
| 6116 | * encountered. |
| 6117 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6118 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6119 | { |
| 6120 | struct ebmb_node *node; |
| 6121 | struct sni_ctx *sni; |
| 6122 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6123 | int errcode = 0; |
| 6124 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6125 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 6126 | /* Automatic memory computations need to know we use SSL there */ |
| 6127 | global.ssl_used_frontend = 1; |
| 6128 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6129 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 6130 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6131 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6132 | err++; |
| 6133 | } |
| 6134 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 6135 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 6136 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6137 | /* It should not be necessary to call this function, but it's |
| 6138 | necessary first to check and move all initialisation related |
| 6139 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6140 | 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] | 6141 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6142 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6143 | 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] | 6144 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6145 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6146 | while (node) { |
| 6147 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6148 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 6149 | /* only initialize the CTX on its first occurrence and |
| 6150 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6151 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6152 | node = ebmb_next(node); |
| 6153 | } |
| 6154 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6155 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6156 | while (node) { |
| 6157 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6158 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6159 | /* only initialize the CTX on its first occurrence and |
| 6160 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6161 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 6162 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6163 | node = ebmb_next(node); |
| 6164 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6165 | |
| 6166 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6167 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6168 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6169 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6170 | err++; |
| 6171 | } |
| 6172 | |
| 6173 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6174 | return err; |
| 6175 | } |
| 6176 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6177 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 6178 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 6179 | * alerts are directly emitted since the rest of the stack does it below. |
| 6180 | */ |
| 6181 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 6182 | { |
| 6183 | struct proxy *px = bind_conf->frontend; |
| 6184 | int alloc_ctx; |
| 6185 | int err; |
| 6186 | |
| 6187 | if (!bind_conf->is_ssl) { |
| 6188 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6189 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 6190 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6191 | } |
| 6192 | return 0; |
| 6193 | } |
| 6194 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6195 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6196 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 6197 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6198 | } |
| 6199 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6200 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 6201 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6202 | return -1; |
| 6203 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6204 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 6205 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6206 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 6207 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6208 | sizeof(*sh_ssl_sess_tree), |
| 6209 | ((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] | 6210 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6211 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 6212 | 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"); |
| 6213 | else |
| 6214 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 6215 | return -1; |
| 6216 | } |
| 6217 | /* free block callback */ |
| 6218 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 6219 | /* init the root tree within the extra space */ |
| 6220 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 6221 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6222 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6223 | err = 0; |
| 6224 | /* initialize all certificate contexts */ |
| 6225 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 6226 | |
| 6227 | /* initialize CA variables if the certificates generation is enabled */ |
| 6228 | err += ssl_sock_load_ca(bind_conf); |
| 6229 | |
| 6230 | return -err; |
| 6231 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6232 | |
| 6233 | /* release ssl context allocated for servers. */ |
| 6234 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 6235 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6236 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6237 | if (srv->ssl_ctx.alpn_str) |
| 6238 | free(srv->ssl_ctx.alpn_str); |
| 6239 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 6240 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6241 | if (srv->ssl_ctx.npn_str) |
| 6242 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 6243 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6244 | if (srv->ssl_ctx.ctx) |
| 6245 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 6246 | } |
| 6247 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6248 | /* 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] | 6249 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 6250 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6251 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6252 | { |
| 6253 | struct ebmb_node *node, *back; |
| 6254 | struct sni_ctx *sni; |
| 6255 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6256 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6257 | while (node) { |
| 6258 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6259 | back = ebmb_next(node); |
| 6260 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6261 | SSL_CTX_free(sni->ctx); |
| 6262 | if (!sni->order) { /* only free the CTX conf on its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6263 | ssl_sock_free_ssl_conf(sni->conf); |
| 6264 | free(sni->conf); |
| 6265 | sni->conf = NULL; |
| 6266 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6267 | free(sni); |
| 6268 | node = back; |
| 6269 | } |
| 6270 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6271 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6272 | while (node) { |
| 6273 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6274 | back = ebmb_next(node); |
| 6275 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6276 | SSL_CTX_free(sni->ctx); |
| 6277 | if (!sni->order) { /* only free the SSL conf its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6278 | ssl_sock_free_ssl_conf(sni->conf); |
| 6279 | free(sni->conf); |
| 6280 | sni->conf = NULL; |
| 6281 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6282 | free(sni); |
| 6283 | node = back; |
| 6284 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6285 | SSL_CTX_free(bind_conf->initial_ctx); |
| 6286 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6287 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6288 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6289 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6290 | } |
| 6291 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6292 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 6293 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 6294 | { |
| 6295 | ssl_sock_free_ca(bind_conf); |
| 6296 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6297 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6298 | free(bind_conf->ca_sign_file); |
| 6299 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 6300 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6301 | free(bind_conf->keys_ref->filename); |
| 6302 | free(bind_conf->keys_ref->tlskeys); |
| 6303 | LIST_DEL(&bind_conf->keys_ref->list); |
| 6304 | free(bind_conf->keys_ref); |
| 6305 | } |
| 6306 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6307 | bind_conf->ca_sign_pass = NULL; |
| 6308 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6309 | } |
| 6310 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6311 | /* Load CA cert file and private key used to generate certificates */ |
| 6312 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6313 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6314 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6315 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6316 | FILE *fp; |
| 6317 | X509 *cacert = NULL; |
| 6318 | EVP_PKEY *capkey = NULL; |
| 6319 | int err = 0; |
| 6320 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 6321 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6322 | return err; |
| 6323 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6324 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6325 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6326 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6327 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6328 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6329 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6330 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6331 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6332 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6333 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 6334 | "no CA certificate File configured at [%s:%d].\n", |
| 6335 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6336 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6337 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6338 | |
| 6339 | /* read in the CA certificate */ |
| 6340 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6341 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6342 | 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] | 6343 | goto load_error; |
| 6344 | } |
| 6345 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6346 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6347 | 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] | 6348 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6349 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6350 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6351 | 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] | 6352 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 6353 | 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] | 6354 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6355 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6356 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6357 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6358 | bind_conf->ca_sign_cert = cacert; |
| 6359 | bind_conf->ca_sign_pkey = capkey; |
| 6360 | return err; |
| 6361 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6362 | read_error: |
| 6363 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6364 | if (capkey) EVP_PKEY_free(capkey); |
| 6365 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6366 | load_error: |
| 6367 | bind_conf->generate_certs = 0; |
| 6368 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6369 | return err; |
| 6370 | } |
| 6371 | |
| 6372 | /* Release CA cert and private key used to generate certificated */ |
| 6373 | void |
| 6374 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 6375 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6376 | if (bind_conf->ca_sign_pkey) |
| 6377 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 6378 | if (bind_conf->ca_sign_cert) |
| 6379 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 6380 | bind_conf->ca_sign_pkey = NULL; |
| 6381 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6382 | } |
| 6383 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6384 | /* |
| 6385 | * This function is called if SSL * context is not yet allocated. The function |
| 6386 | * is designed to be called before any other data-layer operation and sets the |
| 6387 | * handshake flag on the connection. It is safe to call it multiple times. |
| 6388 | * It returns 0 on success and -1 in error case. |
| 6389 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6390 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6391 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6392 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6393 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6394 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6395 | return 0; |
| 6396 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6397 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6398 | return 0; |
| 6399 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6400 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6401 | if (!ctx) { |
| 6402 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6403 | return -1; |
| 6404 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6405 | ctx->wait_event.tasklet = tasklet_new(); |
| 6406 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6407 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6408 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6409 | return -1; |
| 6410 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6411 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6412 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6413 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6414 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6415 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6416 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6417 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6418 | ctx->xprt_st = 0; |
| 6419 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6420 | |
| 6421 | /* Only work with sockets for now, this should be adapted when we'll |
| 6422 | * add QUIC support. |
| 6423 | */ |
| 6424 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6425 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6426 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6427 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6428 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6429 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6430 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 6431 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6432 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6433 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6434 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6435 | /* If it is in client mode initiate SSL session |
| 6436 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6437 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6438 | int may_retry = 1; |
| 6439 | |
| 6440 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6441 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6442 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 6443 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6444 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6445 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6446 | goto retry_connect; |
| 6447 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6448 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6449 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6450 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6451 | ctx->bio = BIO_new(ha_meth); |
| 6452 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6453 | SSL_free(ctx->ssl); |
| 6454 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6455 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6456 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6457 | goto retry_connect; |
| 6458 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6459 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6460 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6461 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6462 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6463 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6464 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6465 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6466 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6467 | SSL_free(ctx->ssl); |
| 6468 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6469 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6470 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6471 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6472 | goto retry_connect; |
| 6473 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6474 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6475 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6476 | } |
| 6477 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6478 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6479 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6480 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6481 | 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] | 6482 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6483 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6484 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6485 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6486 | } else if (sess) { |
| 6487 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6488 | } |
| 6489 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6490 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6491 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6492 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6493 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6494 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6495 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6496 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6497 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6498 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6499 | return 0; |
| 6500 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6501 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6502 | int may_retry = 1; |
| 6503 | |
| 6504 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6505 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6506 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 6507 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6508 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6509 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6510 | goto retry_accept; |
| 6511 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6512 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6513 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6514 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6515 | ctx->bio = BIO_new(ha_meth); |
| 6516 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6517 | SSL_free(ctx->ssl); |
| 6518 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6519 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6520 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6521 | goto retry_accept; |
| 6522 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6523 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6524 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6525 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6526 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6527 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6528 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6529 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6530 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6531 | SSL_free(ctx->ssl); |
| 6532 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6533 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6534 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6535 | goto retry_accept; |
| 6536 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6537 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6538 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6539 | } |
| 6540 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6541 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6542 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 6543 | b_alloc(&ctx->early_buf); |
| 6544 | SSL_set_max_early_data(ctx->ssl, |
| 6545 | /* Only allow early data if we managed to allocate |
| 6546 | * a buffer. |
| 6547 | */ |
| 6548 | (!b_is_null(&ctx->early_buf)) ? |
| 6549 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6550 | } |
| 6551 | #endif |
| 6552 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6553 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6554 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6555 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6556 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6557 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6558 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 6559 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6560 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6561 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6562 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6563 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6564 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6565 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6566 | return 0; |
| 6567 | } |
| 6568 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6569 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6570 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6571 | if (ctx && ctx->wait_event.tasklet) |
| 6572 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6573 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6574 | return -1; |
| 6575 | } |
| 6576 | |
| 6577 | |
| 6578 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6579 | * updates the FD status if it wants some polling before being called again. |
| 6580 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6581 | * otherwise it returns non-zero and removes itself from the connection's |
| 6582 | * flags (the bit is provided in <flag> by the caller). |
| 6583 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6584 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6585 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6586 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6587 | int ret; |
| 6588 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6589 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6590 | return 0; |
| 6591 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6592 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6593 | goto out_error; |
| 6594 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6595 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6596 | /* |
| 6597 | * Check if we have early data. If we do, we have to read them |
| 6598 | * before SSL_do_handshake() is called, And there's no way to |
| 6599 | * detect early data, except to try to read them |
| 6600 | */ |
| 6601 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6602 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6603 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6604 | while (1) { |
| 6605 | ret = SSL_read_early_data(ctx->ssl, |
| 6606 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6607 | &read_data); |
| 6608 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6609 | goto check_error; |
| 6610 | if (read_data > 0) { |
| 6611 | conn->flags |= CO_FL_EARLY_DATA; |
| 6612 | b_add(&ctx->early_buf, read_data); |
| 6613 | } |
| 6614 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6615 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6616 | if (!b_data(&ctx->early_buf)) |
| 6617 | b_free(&ctx->early_buf); |
| 6618 | break; |
| 6619 | } |
| 6620 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6621 | } |
| 6622 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6623 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6624 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6625 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6626 | * the reneg handshake. |
| 6627 | * Here we use SSL_peek as a workaround for reneg. |
| 6628 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6629 | 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] | 6630 | char c; |
| 6631 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6632 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6633 | if (ret <= 0) { |
| 6634 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6635 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6636 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6637 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6638 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6639 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6640 | 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] | 6641 | return 0; |
| 6642 | } |
| 6643 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6644 | /* handshake may have been completed but we have |
| 6645 | * no more data to read. |
| 6646 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6647 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6648 | ret = 1; |
| 6649 | goto reneg_ok; |
| 6650 | } |
| 6651 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6652 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6653 | 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] | 6654 | return 0; |
| 6655 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6656 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6657 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6658 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6659 | return 0; |
| 6660 | } |
| 6661 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6662 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6663 | /* if errno is null, then connection was successfully established */ |
| 6664 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6665 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6666 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6667 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6668 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6669 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6670 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6671 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6672 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6673 | /* 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] | 6674 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6675 | empty_handshake = state == TLS_ST_BEFORE; |
| 6676 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6677 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6678 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6679 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6680 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6681 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6682 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6683 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6684 | else |
| 6685 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6686 | } |
| 6687 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6688 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6689 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6690 | else |
| 6691 | conn->err_code = CO_ER_SSL_ABORT; |
| 6692 | } |
| 6693 | } |
| 6694 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6695 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6696 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6697 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6698 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6699 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6700 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6701 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6702 | goto out_error; |
| 6703 | } |
| 6704 | else { |
| 6705 | /* Fail on all other handshake errors */ |
| 6706 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6707 | * buffer, causing an RST to be emitted upon close() on |
| 6708 | * TCP sockets. We first try to drain possibly pending |
| 6709 | * data to avoid this as much as possible. |
| 6710 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6711 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6712 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6713 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6714 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6715 | goto out_error; |
| 6716 | } |
| 6717 | } |
| 6718 | /* read some data: consider handshake completed */ |
| 6719 | goto reneg_ok; |
| 6720 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6721 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6722 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6723 | if (ret != 1) { |
| 6724 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6725 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6726 | |
| 6727 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6728 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6729 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6730 | 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] | 6731 | return 0; |
| 6732 | } |
| 6733 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6734 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6735 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6736 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6737 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6738 | return 0; |
| 6739 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6740 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6741 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6742 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6743 | return 0; |
| 6744 | } |
| 6745 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6746 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6747 | /* if errno is null, then connection was successfully established */ |
| 6748 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6749 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6750 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6751 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6752 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6753 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6754 | #else |
| 6755 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6756 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6757 | /* 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] | 6758 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6759 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6760 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6761 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6762 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6763 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6764 | if (empty_handshake) { |
| 6765 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6766 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6767 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6768 | else |
| 6769 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6770 | } |
| 6771 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6772 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6773 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6774 | else |
| 6775 | conn->err_code = CO_ER_SSL_ABORT; |
| 6776 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6777 | } |
| 6778 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6779 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6780 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6781 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6782 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6783 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6784 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6785 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6786 | goto out_error; |
| 6787 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6788 | else { |
| 6789 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6790 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6791 | * buffer, causing an RST to be emitted upon close() on |
| 6792 | * TCP sockets. We first try to drain possibly pending |
| 6793 | * data to avoid this as much as possible. |
| 6794 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6795 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6796 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6797 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6798 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6799 | goto out_error; |
| 6800 | } |
| 6801 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6802 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6803 | else { |
| 6804 | /* |
| 6805 | * If the server refused the early data, we have to send a |
| 6806 | * 425 to the client, as we no longer have the data to sent |
| 6807 | * them again. |
| 6808 | */ |
| 6809 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6810 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6811 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6812 | goto out_error; |
| 6813 | } |
| 6814 | } |
| 6815 | } |
| 6816 | #endif |
| 6817 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6818 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6819 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6820 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6821 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6822 | /* ASYNC engine API doesn't support moving read/write |
| 6823 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6824 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6825 | */ |
| 6826 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6827 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6828 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6829 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6830 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6831 | if (objt_server(conn->target)) { |
| 6832 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6833 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6834 | 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] | 6835 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6836 | else { |
| 6837 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6838 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6839 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6840 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /* The connection is now established at both layers, it's time to leave */ |
| 6844 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6845 | return 1; |
| 6846 | |
| 6847 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6848 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6849 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6850 | ERR_clear_error(); |
| 6851 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6852 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6853 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6854 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6855 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6856 | } |
| 6857 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6858 | /* Fail on all other handshake errors */ |
| 6859 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6860 | if (!conn->err_code) |
| 6861 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6862 | return 0; |
| 6863 | } |
| 6864 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6865 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6866 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6867 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6868 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6869 | * unless the transport layer was already released. |
| 6870 | */ |
| 6871 | 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] | 6872 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6873 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6874 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6875 | if (!ctx) |
| 6876 | return -1; |
| 6877 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6878 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6879 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6880 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6881 | ctx->subs = es; |
| 6882 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6883 | |
| 6884 | /* we may have to subscribe to lower layers for new events */ |
| 6885 | event_type &= ~ctx->wait_event.events; |
| 6886 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6887 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6888 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6889 | } |
| 6890 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6891 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6892 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6893 | * subscribe() call. It always returns zero. |
| 6894 | */ |
| 6895 | 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] | 6896 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6897 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6898 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6899 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6900 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6901 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6902 | es->events &= ~event_type; |
| 6903 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6904 | ctx->subs = NULL; |
| 6905 | |
| 6906 | /* If we subscribed, and we're not doing the handshake, |
| 6907 | * then we subscribed because the upper layer asked for it, |
| 6908 | * as the upper layer is no longer interested, we can |
| 6909 | * unsubscribe too. |
| 6910 | */ |
| 6911 | event_type &= ctx->wait_event.events; |
| 6912 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6913 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6914 | |
| 6915 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6916 | } |
| 6917 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6918 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6919 | * Returns 0 on success, and non-zero on failure. |
| 6920 | */ |
| 6921 | 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) |
| 6922 | { |
| 6923 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6924 | |
| 6925 | if (oldxprt_ops != NULL) |
| 6926 | *oldxprt_ops = ctx->xprt; |
| 6927 | if (oldxprt_ctx != NULL) |
| 6928 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6929 | ctx->xprt = toadd_ops; |
| 6930 | ctx->xprt_ctx = toadd_ctx; |
| 6931 | return 0; |
| 6932 | } |
| 6933 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6934 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6935 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6936 | * XPRT. |
| 6937 | */ |
| 6938 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6939 | { |
| 6940 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6941 | |
| 6942 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6943 | ctx->xprt_ctx = newctx; |
| 6944 | ctx->xprt = newops; |
| 6945 | return 0; |
| 6946 | } |
| 6947 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6948 | } |
| 6949 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6950 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6951 | { |
| 6952 | struct ssl_sock_ctx *ctx = context; |
| 6953 | |
| 6954 | /* First if we're doing an handshake, try that */ |
| 6955 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6956 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6957 | /* If we had an error, or the handshake is done and I/O is available, |
| 6958 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6959 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6960 | * we can't be sure conn_fd_handler() will be called again. |
| 6961 | */ |
| 6962 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6963 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6964 | int ret = 0; |
| 6965 | int woke = 0; |
| 6966 | |
| 6967 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6968 | if (ctx->subs) { |
| 6969 | tasklet_wakeup(ctx->subs->tasklet); |
| 6970 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6971 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6972 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6973 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6974 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6975 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6976 | * upper layers know. If we have no mux, create it, |
| 6977 | * and once we have a mux, call its wake method if we didn't |
| 6978 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6979 | */ |
| 6980 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6981 | if (!ctx->conn->mux) |
| 6982 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6983 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6984 | ctx->conn->mux->wake(ctx->conn); |
| 6985 | return NULL; |
| 6986 | } |
| 6987 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6988 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6989 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6990 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6991 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6992 | tasklet_wakeup(ctx->subs->tasklet); |
| 6993 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6994 | if (!ctx->subs->events) |
| 6995 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6996 | } |
| 6997 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6998 | return NULL; |
| 6999 | } |
| 7000 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7001 | /* 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] | 7002 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7003 | * buffer wraps, in which case a second call may be performed. The connection's |
| 7004 | * flags are updated with whatever special event is detected (error, read0, |
| 7005 | * empty). The caller is responsible for taking care of those events and |
| 7006 | * avoiding the call if inappropriate. The function does not call the |
| 7007 | * connection's polling update function, so the caller is responsible for this. |
| 7008 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7009 | 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] | 7010 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7011 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 7012 | ssize_t ret; |
| 7013 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7014 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7015 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7016 | goto out_error; |
| 7017 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7018 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 7019 | if (b_data(&ctx->early_buf)) { |
| 7020 | try = b_contig_space(buf); |
| 7021 | if (try > b_data(&ctx->early_buf)) |
| 7022 | try = b_data(&ctx->early_buf); |
| 7023 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 7024 | b_add(buf, try); |
| 7025 | b_del(&ctx->early_buf, try); |
| 7026 | if (b_data(&ctx->early_buf) == 0) |
| 7027 | b_free(&ctx->early_buf); |
| 7028 | return try; |
| 7029 | } |
| 7030 | #endif |
| 7031 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7032 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7033 | /* a handshake was requested */ |
| 7034 | return 0; |
| 7035 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7036 | /* read the largest possible block. For this, we perform only one call |
| 7037 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 7038 | * in which case we accept to do it once again. A new attempt is made on |
| 7039 | * EINTR too. |
| 7040 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 7041 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7042 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 7043 | try = b_contig_space(buf); |
| 7044 | if (!try) |
| 7045 | break; |
| 7046 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 7047 | if (try > count) |
| 7048 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 7049 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7050 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 7051 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7052 | if (conn->flags & CO_FL_ERROR) { |
| 7053 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7054 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7055 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7056 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 7057 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7058 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7059 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7060 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7061 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7062 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7063 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7064 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7065 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7066 | 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] | 7067 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7068 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7069 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7070 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7071 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7072 | break; |
| 7073 | } |
| 7074 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7075 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7076 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7077 | SUB_RETRY_RECV, |
| 7078 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7079 | /* handshake is running, and it may need to re-enable read */ |
| 7080 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7081 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7082 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7083 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7084 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7085 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7086 | break; |
| 7087 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7088 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7089 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 7090 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7091 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 7092 | * stack before shutting down the connection for |
| 7093 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7094 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 7095 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7096 | /* otherwise it's a real error */ |
| 7097 | goto out_error; |
| 7098 | } |
| 7099 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7100 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7101 | return done; |
| 7102 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7103 | clear_ssl_error: |
| 7104 | /* Clear openssl global errors stack */ |
| 7105 | ssl_sock_dump_errors(conn); |
| 7106 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7107 | read0: |
| 7108 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7109 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7110 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7111 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7112 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7113 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7114 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7115 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7116 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7117 | } |
| 7118 | |
| 7119 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7120 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 7121 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 7122 | * 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] | 7123 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 7124 | * a second call may be performed. The connection's flags are updated with |
| 7125 | * whatever special event is detected (error, empty). The caller is responsible |
| 7126 | * for taking care of those events and avoiding the call if inappropriate. The |
| 7127 | * 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] | 7128 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 7129 | * caller to take care of this. It's up to the caller to update the buffer's |
| 7130 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7131 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7132 | 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] | 7133 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7134 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7135 | ssize_t ret; |
| 7136 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7137 | |
| 7138 | done = 0; |
| 7139 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7140 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7141 | goto out_error; |
| 7142 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7143 | 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] | 7144 | /* a handshake was requested */ |
| 7145 | return 0; |
| 7146 | |
| 7147 | /* send the largest possible block. For this we perform only one call |
| 7148 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 7149 | * in which case we accept to do it once again. |
| 7150 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7151 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7152 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7153 | size_t written_data; |
| 7154 | #endif |
| 7155 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7156 | try = b_contig_data(buf, done); |
| 7157 | if (try > count) |
| 7158 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7159 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 7160 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7161 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7162 | global_ssl.max_record && try > global_ssl.max_record) { |
| 7163 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7164 | } |
| 7165 | else { |
| 7166 | /* we need to keep the information about the fact that |
| 7167 | * we're not limiting the upcoming send(), because if it |
| 7168 | * fails, we'll have to retry with at least as many data. |
| 7169 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7170 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7171 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7172 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7173 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7174 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7175 | unsigned int max_early; |
| 7176 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7177 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7178 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7179 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7180 | if (SSL_get0_session(ctx->ssl)) |
| 7181 | 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] | 7182 | else |
| 7183 | max_early = 0; |
| 7184 | } |
| 7185 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7186 | if (try + ctx->sent_early_data > max_early) { |
| 7187 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7188 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7189 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7190 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7191 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7192 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7193 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7194 | 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] | 7195 | if (ret == 1) { |
| 7196 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7197 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7198 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7199 | 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] | 7200 | /* Initiate the handshake, now */ |
| 7201 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 7202 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7203 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | } else |
| 7207 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7208 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7209 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7210 | if (conn->flags & CO_FL_ERROR) { |
| 7211 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7212 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7213 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7214 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 7215 | /* A send succeeded, so we can consider ourself connected */ |
| 7216 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7217 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7218 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7219 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7220 | } |
| 7221 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7222 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7223 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7224 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7225 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7226 | /* handshake is running, and it may need to re-enable write */ |
| 7227 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7228 | 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] | 7229 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7230 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7231 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7232 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7233 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7234 | break; |
| 7235 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7236 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7237 | break; |
| 7238 | } |
| 7239 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7240 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7241 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7242 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7243 | SUB_RETRY_RECV, |
| 7244 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7245 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7246 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7247 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7248 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7249 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7250 | break; |
| 7251 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7252 | goto out_error; |
| 7253 | } |
| 7254 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7255 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7256 | return done; |
| 7257 | |
| 7258 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7259 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7260 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7261 | ERR_clear_error(); |
| 7262 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7263 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7264 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7265 | } |
| 7266 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7267 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7268 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7269 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7270 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7271 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7272 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7273 | if (ctx->wait_event.events != 0) |
| 7274 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 7275 | ctx->wait_event.events, |
| 7276 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7277 | if (ctx->subs) { |
| 7278 | ctx->subs->events = 0; |
| 7279 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7280 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7281 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 7282 | if (ctx->xprt->close) |
| 7283 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7284 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7285 | if (global_ssl.async) { |
| 7286 | OSSL_ASYNC_FD all_fd[32], afd; |
| 7287 | size_t num_all_fds = 0; |
| 7288 | int i; |
| 7289 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7290 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7291 | if (num_all_fds > 32) { |
| 7292 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 7293 | return; |
| 7294 | } |
| 7295 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7296 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7297 | |
| 7298 | /* If an async job is pending, we must try to |
| 7299 | to catch the end using polling before calling |
| 7300 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7301 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7302 | for (i=0 ; i < num_all_fds ; i++) { |
| 7303 | /* switch on an handler designed to |
| 7304 | * handle the SSL_free |
| 7305 | */ |
| 7306 | afd = all_fd[i]; |
| 7307 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7308 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7309 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 7310 | /* To ensure that the fd cache won't be used |
| 7311 | * and we'll catch a real RD event. |
| 7312 | */ |
| 7313 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7314 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7315 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7316 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7317 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7318 | return; |
| 7319 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7320 | /* Else we can remove the fds from the fdtab |
| 7321 | * and call SSL_free. |
| 7322 | * note: we do a fd_remove and not a delete |
| 7323 | * because the fd is owned by the engine. |
| 7324 | * the engine is responsible to close |
| 7325 | */ |
| 7326 | for (i=0 ; i < num_all_fds ; i++) |
| 7327 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7328 | } |
| 7329 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7330 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7331 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7332 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7333 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7334 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7335 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 7339 | * any case, flags the connection as reusable if no handshake was in progress. |
| 7340 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7341 | 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] | 7342 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7343 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7344 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7345 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7346 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 7347 | if (!clean) |
| 7348 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7349 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7350 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7351 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7352 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7353 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7354 | ERR_clear_error(); |
| 7355 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7356 | } |
| 7357 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7358 | /* fill a buffer with the algorithm and size of a public key */ |
| 7359 | static int cert_get_pkey_algo(X509 *crt, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7360 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7361 | int bits = 0; |
| 7362 | int sig = TLSEXT_signature_anonymous; |
| 7363 | int len = -1; |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7364 | EVP_PKEY *pkey; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7365 | |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7366 | pkey = X509_get_pubkey(crt); |
| 7367 | if (pkey) { |
| 7368 | bits = EVP_PKEY_bits(pkey); |
| 7369 | switch(EVP_PKEY_base_id(pkey)) { |
| 7370 | case EVP_PKEY_RSA: |
| 7371 | sig = TLSEXT_signature_rsa; |
| 7372 | break; |
| 7373 | case EVP_PKEY_EC: |
| 7374 | sig = TLSEXT_signature_ecdsa; |
| 7375 | break; |
| 7376 | case EVP_PKEY_DSA: |
| 7377 | sig = TLSEXT_signature_dsa; |
| 7378 | break; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7379 | } |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7380 | EVP_PKEY_free(pkey); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | switch(sig) { |
| 7384 | case TLSEXT_signature_rsa: |
| 7385 | len = chunk_printf(out, "RSA%d", bits); |
| 7386 | break; |
| 7387 | case TLSEXT_signature_ecdsa: |
| 7388 | len = chunk_printf(out, "EC%d", bits); |
| 7389 | break; |
| 7390 | case TLSEXT_signature_dsa: |
| 7391 | len = chunk_printf(out, "DSA%d", bits); |
| 7392 | break; |
| 7393 | default: |
| 7394 | return 0; |
| 7395 | } |
| 7396 | if (len < 0) |
| 7397 | return 0; |
| 7398 | return 1; |
| 7399 | } |
| 7400 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 7401 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7402 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7403 | { |
| 7404 | struct ssl_sock_ctx *ctx; |
| 7405 | X509 *crt; |
| 7406 | |
| 7407 | if (!ssl_sock_is_ssl(conn)) |
| 7408 | return 0; |
| 7409 | |
| 7410 | ctx = conn->xprt_ctx; |
| 7411 | |
| 7412 | crt = SSL_get_certificate(ctx->ssl); |
| 7413 | if (!crt) |
| 7414 | return 0; |
| 7415 | |
| 7416 | return cert_get_pkey_algo(crt, out); |
| 7417 | } |
| 7418 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7419 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7420 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7421 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7422 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7423 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7424 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7425 | X509 *crt; |
| 7426 | |
| 7427 | if (!ssl_sock_is_ssl(conn)) |
| 7428 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7429 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7430 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7431 | if (!crt) |
| 7432 | return NULL; |
| 7433 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7434 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7435 | } |
| 7436 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7437 | /* used for ppv2 authority */ |
| 7438 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7439 | { |
| 7440 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7441 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7442 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7443 | if (!ssl_sock_is_ssl(conn)) |
| 7444 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7445 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7446 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7447 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7448 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7449 | #endif |
| 7450 | } |
| 7451 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7452 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7453 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7454 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7455 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7456 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7457 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7458 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7459 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7460 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7461 | } |
| 7462 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7463 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7464 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7465 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7466 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7467 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7468 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7469 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7470 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7471 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7472 | } |
| 7473 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7474 | /* Extract a serial from a cert, and copy it to a chunk. |
| 7475 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 7476 | * -1 if output is not large enough. |
| 7477 | */ |
| 7478 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7479 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7480 | { |
| 7481 | ASN1_INTEGER *serial; |
| 7482 | |
| 7483 | serial = X509_get_serialNumber(crt); |
| 7484 | if (!serial) |
| 7485 | return 0; |
| 7486 | |
| 7487 | if (out->size < serial->length) |
| 7488 | return -1; |
| 7489 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7490 | memcpy(out->area, serial->data, serial->length); |
| 7491 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7492 | return 1; |
| 7493 | } |
| 7494 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7495 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7496 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 7497 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7498 | */ |
| 7499 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7500 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7501 | { |
| 7502 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7503 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7504 | |
| 7505 | len =i2d_X509(crt, NULL); |
| 7506 | if (len <= 0) |
| 7507 | return 1; |
| 7508 | |
| 7509 | if (out->size < len) |
| 7510 | return -1; |
| 7511 | |
| 7512 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7513 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7514 | return 1; |
| 7515 | } |
| 7516 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7517 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7518 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7519 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 7520 | * and -1 if output is not large enough. |
| 7521 | */ |
| 7522 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7523 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7524 | { |
| 7525 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 7526 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 7527 | |
| 7528 | if (gentm->length < 12) |
| 7529 | return 0; |
| 7530 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 7531 | return 0; |
| 7532 | if (out->size < gentm->length-2) |
| 7533 | return -1; |
| 7534 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7535 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 7536 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7537 | return 1; |
| 7538 | } |
| 7539 | else if (tm->type == V_ASN1_UTCTIME) { |
| 7540 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 7541 | |
| 7542 | if (utctm->length < 10) |
| 7543 | return 0; |
| 7544 | if (utctm->data[0] >= 0x35) |
| 7545 | return 0; |
| 7546 | if (out->size < utctm->length) |
| 7547 | return -1; |
| 7548 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7549 | memcpy(out->area, utctm->data, utctm->length); |
| 7550 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7551 | return 1; |
| 7552 | } |
| 7553 | |
| 7554 | return 0; |
| 7555 | } |
| 7556 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7557 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 7558 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 7559 | */ |
| 7560 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7561 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 7562 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7563 | { |
| 7564 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7565 | ASN1_OBJECT *obj; |
| 7566 | ASN1_STRING *data; |
| 7567 | const unsigned char *data_ptr; |
| 7568 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7569 | int i, j, n; |
| 7570 | int cur = 0; |
| 7571 | const char *s; |
| 7572 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7573 | int name_count; |
| 7574 | |
| 7575 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7576 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7577 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7578 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7579 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7580 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7581 | else |
| 7582 | j = i; |
| 7583 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7584 | ne = X509_NAME_get_entry(a, j); |
| 7585 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7586 | data = X509_NAME_ENTRY_get_data(ne); |
| 7587 | data_ptr = ASN1_STRING_get0_data(data); |
| 7588 | data_len = ASN1_STRING_length(data); |
| 7589 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7590 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7591 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7592 | s = tmp; |
| 7593 | } |
| 7594 | |
| 7595 | if (chunk_strcasecmp(entry, s) != 0) |
| 7596 | continue; |
| 7597 | |
| 7598 | if (pos < 0) |
| 7599 | cur--; |
| 7600 | else |
| 7601 | cur++; |
| 7602 | |
| 7603 | if (cur != pos) |
| 7604 | continue; |
| 7605 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7606 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7607 | return -1; |
| 7608 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7609 | memcpy(out->area, data_ptr, data_len); |
| 7610 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7611 | return 1; |
| 7612 | } |
| 7613 | |
| 7614 | return 0; |
| 7615 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7616 | } |
| 7617 | |
| 7618 | /* |
| 7619 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 7620 | * Return 0; |
| 7621 | */ |
| 7622 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7623 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 7624 | { |
| 7625 | int i; |
| 7626 | char *str; |
| 7627 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 7628 | |
| 7629 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 7630 | if (names) { |
| 7631 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 7632 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 7633 | if (i > 0) |
| 7634 | chunk_appendf(out, ", "); |
| 7635 | if (name->type == GEN_DNS) { |
| 7636 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 7637 | chunk_appendf(out, "DNS:%s", str); |
| 7638 | OPENSSL_free(str); |
| 7639 | } |
| 7640 | } |
| 7641 | } |
| 7642 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 7643 | } |
| 7644 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7645 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7646 | #endif |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7647 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7648 | /* |
| 7649 | * Extract the DN in the specified format from the X509_NAME and copy result to a chunk. |
| 7650 | * Currently supports rfc2253 for returning LDAP V3 DNs. |
| 7651 | * Returns 1 if dn entries exist, 0 if no dn entry was found. |
| 7652 | */ |
| 7653 | static int |
| 7654 | ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out) |
| 7655 | { |
| 7656 | BIO *bio = NULL; |
| 7657 | int ret = 0; |
| 7658 | int data_len = 0; |
| 7659 | |
| 7660 | if (chunk_strcmp(format, "rfc2253") == 0) { |
| 7661 | bio = BIO_new(BIO_s_mem()); |
| 7662 | if (bio == NULL) |
| 7663 | goto out; |
| 7664 | |
| 7665 | if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0) |
| 7666 | goto out; |
| 7667 | |
| 7668 | if ((data_len = BIO_read(bio, out->area, out->size)) <= 0) |
| 7669 | goto out; |
| 7670 | |
| 7671 | out->data = data_len; |
| 7672 | |
| 7673 | ret = 1; |
| 7674 | } |
| 7675 | out: |
| 7676 | if (bio) |
| 7677 | BIO_free(bio); |
| 7678 | return ret; |
| 7679 | } |
| 7680 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7681 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 7682 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 7683 | */ |
| 7684 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7685 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7686 | { |
| 7687 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7688 | ASN1_OBJECT *obj; |
| 7689 | ASN1_STRING *data; |
| 7690 | const unsigned char *data_ptr; |
| 7691 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7692 | int i, n, ln; |
| 7693 | int l = 0; |
| 7694 | const char *s; |
| 7695 | char *p; |
| 7696 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7697 | int name_count; |
| 7698 | |
| 7699 | |
| 7700 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7701 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7702 | out->data = 0; |
| 7703 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7704 | for (i = 0; i < name_count; i++) { |
| 7705 | ne = X509_NAME_get_entry(a, i); |
| 7706 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7707 | data = X509_NAME_ENTRY_get_data(ne); |
| 7708 | data_ptr = ASN1_STRING_get0_data(data); |
| 7709 | data_len = ASN1_STRING_length(data); |
| 7710 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7711 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7712 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7713 | s = tmp; |
| 7714 | } |
| 7715 | ln = strlen(s); |
| 7716 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7717 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7718 | if (l > out->size) |
| 7719 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7720 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7721 | |
| 7722 | *(p++)='/'; |
| 7723 | memcpy(p, s, ln); |
| 7724 | p += ln; |
| 7725 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7726 | memcpy(p, data_ptr, data_len); |
| 7727 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7728 | } |
| 7729 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7730 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7731 | return 0; |
| 7732 | |
| 7733 | return 1; |
| 7734 | } |
| 7735 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7736 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7737 | { |
| 7738 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7739 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7740 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7741 | if (!ssl_sock_is_ssl(conn)) |
| 7742 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7743 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7744 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7745 | #endif |
| 7746 | } |
| 7747 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7748 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7749 | * to disable SNI. |
| 7750 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7751 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7752 | { |
| 7753 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7754 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7755 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7756 | char *prev_name; |
| 7757 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7758 | if (!ssl_sock_is_ssl(conn)) |
| 7759 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7760 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7761 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7762 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7763 | * new connection will present a new SNI. As an optimization we could |
| 7764 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7765 | * server. |
| 7766 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7767 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7768 | if ((!prev_name && hostname) || |
| 7769 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7770 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7771 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7772 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7773 | #endif |
| 7774 | } |
| 7775 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7776 | /* Extract peer certificate's common name into the chunk dest |
| 7777 | * Returns |
| 7778 | * the len of the extracted common name |
| 7779 | * or 0 if no CN found in DN |
| 7780 | * or -1 on error case (i.e. no peer certificate) |
| 7781 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7782 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7783 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7784 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7785 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7786 | X509 *crt = NULL; |
| 7787 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7788 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7789 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7790 | .area = (char *)&find_cn, |
| 7791 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7792 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7793 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7794 | |
| 7795 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7796 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7797 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7798 | |
| 7799 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7800 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7801 | if (!crt) |
| 7802 | goto out; |
| 7803 | |
| 7804 | name = X509_get_subject_name(crt); |
| 7805 | if (!name) |
| 7806 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7807 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7808 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7809 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7810 | if (crt) |
| 7811 | X509_free(crt); |
| 7812 | |
| 7813 | return result; |
| 7814 | } |
| 7815 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7816 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7817 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7818 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7819 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7820 | X509 *crt = NULL; |
| 7821 | |
| 7822 | if (!ssl_sock_is_ssl(conn)) |
| 7823 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7824 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7825 | |
| 7826 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7827 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7828 | if (!crt) |
| 7829 | return 0; |
| 7830 | |
| 7831 | X509_free(crt); |
| 7832 | return 1; |
| 7833 | } |
| 7834 | |
| 7835 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7836 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7837 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7838 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7839 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7840 | if (!ssl_sock_is_ssl(conn)) |
| 7841 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7842 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7843 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7844 | } |
| 7845 | |
| 7846 | /* returns result from SSL verify */ |
| 7847 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7848 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7849 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7850 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7851 | if (!ssl_sock_is_ssl(conn)) |
| 7852 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7853 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7854 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7855 | } |
| 7856 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7857 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7858 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7859 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7860 | * freed by the caller. NPN is also checked if available since older versions |
| 7861 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7862 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7863 | 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] | 7864 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7865 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7866 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7867 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7868 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7869 | return 0; |
| 7870 | |
| 7871 | *str = NULL; |
| 7872 | |
| 7873 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7874 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7875 | if (*str) |
| 7876 | return 1; |
| 7877 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7878 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7879 | 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] | 7880 | if (*str) |
| 7881 | return 1; |
| 7882 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7883 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7884 | return 0; |
| 7885 | } |
| 7886 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7887 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7888 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7889 | static int |
| 7890 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7891 | { |
| 7892 | struct connection *conn; |
| 7893 | |
| 7894 | conn = objt_conn(smp->sess->origin); |
| 7895 | if (!conn || conn->xprt != &ssl_sock) |
| 7896 | return 0; |
| 7897 | |
| 7898 | smp->flags = 0; |
| 7899 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7900 | #ifdef OPENSSL_IS_BORINGSSL |
| 7901 | { |
| 7902 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7903 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7904 | SSL_early_data_accepted(ctx->ssl)); |
| 7905 | } |
| 7906 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7907 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 7908 | (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] | 7909 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7910 | return 1; |
| 7911 | } |
| 7912 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7913 | /* boolean, returns true if client cert was present */ |
| 7914 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7915 | 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] | 7916 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7917 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7918 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7919 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7920 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7921 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7922 | return 0; |
| 7923 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7924 | ctx = conn->xprt_ctx; |
| 7925 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7926 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7927 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7928 | return 0; |
| 7929 | } |
| 7930 | |
| 7931 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7932 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7933 | 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] | 7934 | |
| 7935 | return 1; |
| 7936 | } |
| 7937 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7938 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7939 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7940 | * should be use. |
| 7941 | */ |
| 7942 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7943 | 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] | 7944 | { |
| 7945 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7946 | X509 *crt = NULL; |
| 7947 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7948 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7949 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7950 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7951 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7952 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7953 | if (!conn || conn->xprt != &ssl_sock) |
| 7954 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7955 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7956 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7957 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7958 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7959 | return 0; |
| 7960 | } |
| 7961 | |
| 7962 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7963 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7964 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7965 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7966 | |
| 7967 | if (!crt) |
| 7968 | goto out; |
| 7969 | |
| 7970 | smp_trash = get_trash_chunk(); |
| 7971 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7972 | goto out; |
| 7973 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7974 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7975 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7976 | ret = 1; |
| 7977 | out: |
| 7978 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7979 | if (cert_peer && crt) |
| 7980 | X509_free(crt); |
| 7981 | return ret; |
| 7982 | } |
| 7983 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7984 | /* binary, returns serial of certificate in a binary chunk. |
| 7985 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7986 | * should be use. |
| 7987 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7989 | 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] | 7990 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7991 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7992 | X509 *crt = NULL; |
| 7993 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7994 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7995 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7996 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7997 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7998 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7999 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8000 | return 0; |
| 8001 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8002 | ctx = conn->xprt_ctx; |
| 8003 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8004 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8005 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8006 | return 0; |
| 8007 | } |
| 8008 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8009 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8010 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8011 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8012 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8013 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +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(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8018 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 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_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +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) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8027 | X509_free(crt); |
| 8028 | return ret; |
| 8029 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 8030 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8031 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 8032 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8033 | * should be use. |
| 8034 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8035 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8036 | 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] | 8037 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8038 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8039 | X509 *crt = NULL; |
| 8040 | const EVP_MD *digest; |
| 8041 | int ret = 0; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8042 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8043 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8044 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8045 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8046 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8047 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8048 | if (!conn || conn->xprt != &ssl_sock) |
| 8049 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8050 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8051 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8052 | if (conn->flags & CO_FL_WAIT_XPRT) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8053 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8054 | return 0; |
| 8055 | } |
| 8056 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8057 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8058 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8059 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8060 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8061 | if (!crt) |
| 8062 | goto out; |
| 8063 | |
| 8064 | smp_trash = get_trash_chunk(); |
| 8065 | digest = EVP_sha1(); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8066 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 8067 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8068 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8069 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8070 | ret = 1; |
| 8071 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8072 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8073 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8074 | X509_free(crt); |
| 8075 | return ret; |
| 8076 | } |
| 8077 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8078 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 8079 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8080 | * should be use. |
| 8081 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8082 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8083 | 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] | 8084 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8085 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8086 | X509 *crt = NULL; |
| 8087 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8088 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8089 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8090 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8091 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8092 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8093 | if (!conn || conn->xprt != &ssl_sock) |
| 8094 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8095 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8096 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8097 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8098 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8099 | return 0; |
| 8100 | } |
| 8101 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8102 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8103 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8104 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8105 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8106 | if (!crt) |
| 8107 | goto out; |
| 8108 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8109 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8110 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8111 | goto out; |
| 8112 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8113 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8114 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8115 | ret = 1; |
| 8116 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8117 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8118 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8119 | X509_free(crt); |
| 8120 | return ret; |
| 8121 | } |
| 8122 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8123 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 8124 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8125 | * should be use. |
| 8126 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8127 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8128 | 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] | 8129 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8130 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8131 | X509 *crt = NULL; |
| 8132 | X509_NAME *name; |
| 8133 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8134 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8135 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8136 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8137 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8138 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8139 | if (!conn || conn->xprt != &ssl_sock) |
| 8140 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8141 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8142 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8143 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8144 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8148 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8149 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8150 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8151 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8152 | if (!crt) |
| 8153 | goto out; |
| 8154 | |
| 8155 | name = X509_get_issuer_name(crt); |
| 8156 | if (!name) |
| 8157 | goto out; |
| 8158 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8159 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8160 | 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] | 8161 | int pos = 1; |
| 8162 | |
| 8163 | if (args[1].type == ARGT_SINT) |
| 8164 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8165 | |
| 8166 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8167 | goto out; |
| 8168 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8169 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8170 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8171 | goto out; |
| 8172 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8173 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8174 | goto out; |
| 8175 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8176 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8177 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8178 | ret = 1; |
| 8179 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8180 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8181 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8182 | X509_free(crt); |
| 8183 | return ret; |
| 8184 | } |
| 8185 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8186 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 8187 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8188 | * should be use. |
| 8189 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8190 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8191 | 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] | 8192 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8193 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8194 | X509 *crt = NULL; |
| 8195 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8196 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8197 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8198 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8199 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8200 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8201 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8202 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8203 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8204 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8205 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8206 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8207 | return 0; |
| 8208 | } |
| 8209 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8210 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8211 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8212 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8213 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8214 | if (!crt) |
| 8215 | goto out; |
| 8216 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8217 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8218 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8219 | goto out; |
| 8220 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8221 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8222 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8223 | ret = 1; |
| 8224 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8225 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8226 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8227 | X509_free(crt); |
| 8228 | return ret; |
| 8229 | } |
| 8230 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8231 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 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 | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8235 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8236 | 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] | 8237 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8238 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8239 | X509 *crt = NULL; |
| 8240 | X509_NAME *name; |
| 8241 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8242 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8243 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8244 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8245 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8246 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8247 | if (!conn || conn->xprt != &ssl_sock) |
| 8248 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8249 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8250 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8251 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8252 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8253 | return 0; |
| 8254 | } |
| 8255 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8256 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8257 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8258 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8259 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8260 | if (!crt) |
| 8261 | goto out; |
| 8262 | |
| 8263 | name = X509_get_subject_name(crt); |
| 8264 | if (!name) |
| 8265 | goto out; |
| 8266 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8267 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8268 | 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] | 8269 | int pos = 1; |
| 8270 | |
| 8271 | if (args[1].type == ARGT_SINT) |
| 8272 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8273 | |
| 8274 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8275 | goto out; |
| 8276 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8277 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8278 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8279 | goto out; |
| 8280 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8281 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8282 | goto out; |
| 8283 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8284 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8285 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8286 | ret = 1; |
| 8287 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8288 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8289 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8290 | X509_free(crt); |
| 8291 | return ret; |
| 8292 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8293 | |
| 8294 | /* integer, returns true if current session use a client certificate */ |
| 8295 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8296 | 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] | 8297 | { |
| 8298 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8299 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8300 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8301 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8302 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8303 | if (!conn || conn->xprt != &ssl_sock) |
| 8304 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8305 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8306 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8307 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8308 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8309 | return 0; |
| 8310 | } |
| 8311 | |
| 8312 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8313 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8314 | if (crt) { |
| 8315 | X509_free(crt); |
| 8316 | } |
| 8317 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8318 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8319 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8320 | return 1; |
| 8321 | } |
| 8322 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8323 | /* integer, returns the certificate version |
| 8324 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8325 | * should be use. |
| 8326 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8327 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8328 | 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] | 8329 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8330 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8331 | X509 *crt; |
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; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +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) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8337 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8338 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +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 | a7359fd | 2012-10-17 15:03:11 +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 | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8349 | if (!crt) |
| 8350 | return 0; |
| 8351 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8352 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8353 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8354 | if (cert_peer) |
| 8355 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8356 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8357 | |
| 8358 | return 1; |
| 8359 | } |
| 8360 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8361 | /* string, returns the certificate's signature algorithm. |
| 8362 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8363 | * should be use. |
| 8364 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8365 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8366 | 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] | 8367 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8368 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8369 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8370 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8371 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8372 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8373 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8374 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8375 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8376 | if (!conn || conn->xprt != &ssl_sock) |
| 8377 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8378 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8379 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8380 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8381 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8382 | return 0; |
| 8383 | } |
| 8384 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8385 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8386 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8387 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8388 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8389 | if (!crt) |
| 8390 | return 0; |
| 8391 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8392 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 8393 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8394 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8395 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8396 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8397 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8398 | if (cert_peer) |
| 8399 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8400 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8401 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8402 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8403 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8404 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8405 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8406 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8407 | if (cert_peer) |
| 8408 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8409 | |
| 8410 | return 1; |
| 8411 | } |
| 8412 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8413 | /* string, returns the certificate's key algorithm. |
| 8414 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8415 | * should be use. |
| 8416 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8417 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8418 | 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] | 8419 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8420 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8421 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8422 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8423 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8424 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8425 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8426 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8427 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8428 | if (!conn || conn->xprt != &ssl_sock) |
| 8429 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8430 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8431 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8432 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8433 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8434 | return 0; |
| 8435 | } |
| 8436 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8437 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8438 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8439 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8440 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8441 | if (!crt) |
| 8442 | return 0; |
| 8443 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8444 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 8445 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8446 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8447 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8448 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8449 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8450 | if (cert_peer) |
| 8451 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8452 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8453 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8454 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8455 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8456 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8457 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8458 | if (cert_peer) |
| 8459 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8460 | |
| 8461 | return 1; |
| 8462 | } |
| 8463 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8464 | /* boolean, returns true if front conn. transport layer is SSL. |
| 8465 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8466 | * char is 'b'. |
| 8467 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8468 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8469 | 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] | 8470 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8471 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8472 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8473 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8474 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8475 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8476 | return 1; |
| 8477 | } |
| 8478 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8479 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8480 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8481 | 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] | 8482 | { |
| 8483 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8484 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8485 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8486 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8487 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8488 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8489 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8490 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8491 | return 1; |
| 8492 | #else |
| 8493 | return 0; |
| 8494 | #endif |
| 8495 | } |
| 8496 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8497 | /* boolean, returns true if client session has been resumed. |
| 8498 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8499 | * char is 'b'. |
| 8500 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8501 | static int |
| 8502 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8503 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8504 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8505 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8506 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8507 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8508 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8509 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8510 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8511 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8512 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8513 | return 1; |
| 8514 | } |
| 8515 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8516 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 8517 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8518 | * char is 'b'. |
| 8519 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8520 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8521 | 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] | 8522 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8523 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8524 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8525 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8526 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8527 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8528 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8529 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8530 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8531 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8532 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8533 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8534 | return 0; |
| 8535 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8536 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8537 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8538 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8539 | |
| 8540 | return 1; |
| 8541 | } |
| 8542 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8543 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 8544 | * is SSL. |
| 8545 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8546 | * char is 'b'. |
| 8547 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8548 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8549 | 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] | 8550 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8551 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8552 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8553 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8554 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8555 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8556 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8557 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8558 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8559 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8560 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8561 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8562 | return 0; |
| 8563 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8564 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8565 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8566 | |
| 8567 | return 1; |
| 8568 | } |
| 8569 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8570 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 8571 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8572 | * char is 'b'. |
| 8573 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8574 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8575 | 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] | 8576 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8577 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8578 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8579 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8580 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8581 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8582 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8583 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8584 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8585 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8586 | 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] | 8587 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8588 | return 0; |
| 8589 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8590 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8591 | |
| 8592 | return 1; |
| 8593 | } |
| 8594 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8595 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8596 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8597 | 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] | 8598 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8599 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8600 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8601 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8602 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8603 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8604 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8605 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8606 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8607 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
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 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8612 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8613 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8614 | (const unsigned char **)&smp->data.u.str.area, |
| 8615 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8616 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8617 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +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; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8621 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8622 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8623 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8624 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8625 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8626 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8627 | 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] | 8628 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8629 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8630 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8631 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8632 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8633 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8634 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8635 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8636 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8637 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8638 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8639 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8640 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8641 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8642 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8643 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8644 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8645 | (const unsigned char **)&smp->data.u.str.area, |
| 8646 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8647 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8648 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8649 | return 0; |
| 8650 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8651 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8652 | return 1; |
| 8653 | } |
| 8654 | #endif |
| 8655 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8656 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 8657 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8658 | * char is 'b'. |
| 8659 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8660 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8661 | 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] | 8662 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8663 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8664 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8665 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8666 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8667 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8668 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8669 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8670 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8671 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8672 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8673 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8674 | return 0; |
| 8675 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8676 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8677 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8678 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8679 | |
| 8680 | return 1; |
| 8681 | } |
| 8682 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8683 | /* 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] | 8684 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8685 | * char is 'b'. |
| 8686 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8687 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8688 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8689 | 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] | 8690 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8691 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8692 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8693 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8694 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8695 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [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_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8699 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8700 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8701 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8702 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8703 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8704 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8705 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8706 | return 0; |
| 8707 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8708 | 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] | 8709 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8710 | return 0; |
| 8711 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8712 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8713 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8714 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8715 | #endif |
| 8716 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8717 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8718 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8719 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 8720 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8721 | { |
| 8722 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8723 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8724 | struct buffer *data; |
| 8725 | struct ssl_sock_ctx *ctx; |
| 8726 | |
| 8727 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8728 | return 0; |
| 8729 | ctx = conn->xprt_ctx; |
| 8730 | |
| 8731 | data = get_trash_chunk(); |
| 8732 | if (kw[7] == 'c') |
| 8733 | data->data = SSL_get_client_random(ctx->ssl, |
| 8734 | (unsigned char *) data->area, |
| 8735 | data->size); |
| 8736 | else |
| 8737 | data->data = SSL_get_server_random(ctx->ssl, |
| 8738 | (unsigned char *) data->area, |
| 8739 | data->size); |
| 8740 | if (!data->data) |
| 8741 | return 0; |
| 8742 | |
| 8743 | smp->flags = 0; |
| 8744 | smp->data.type = SMP_T_BIN; |
| 8745 | smp->data.u.str = *data; |
| 8746 | |
| 8747 | return 1; |
| 8748 | } |
| 8749 | |
| 8750 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8751 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8752 | { |
| 8753 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8754 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8755 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8756 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8757 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8758 | |
| 8759 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8760 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8761 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8762 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8763 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8764 | if (!ssl_sess) |
| 8765 | return 0; |
| 8766 | |
| 8767 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8768 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8769 | (unsigned char *) data->area, |
| 8770 | data->size); |
| 8771 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8772 | return 0; |
| 8773 | |
| 8774 | smp->flags = 0; |
| 8775 | smp->data.type = SMP_T_BIN; |
| 8776 | smp->data.u.str = *data; |
| 8777 | |
| 8778 | return 1; |
| 8779 | } |
| 8780 | #endif |
| 8781 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8782 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8783 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8784 | 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] | 8785 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8786 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8787 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8788 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8789 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8790 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8791 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8792 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8793 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8794 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8795 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8796 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8797 | 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] | 8798 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8799 | return 0; |
| 8800 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8801 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8802 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8803 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8804 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8805 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8806 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8807 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8808 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8809 | struct connection *conn; |
| 8810 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8811 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8812 | |
| 8813 | conn = objt_conn(smp->sess->origin); |
| 8814 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8815 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8816 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8817 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8818 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8819 | if (!capture) |
| 8820 | return 0; |
| 8821 | |
| 8822 | smp->flags = SMP_F_CONST; |
| 8823 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8824 | smp->data.u.str.area = capture->ciphersuite; |
| 8825 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8826 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8827 | } |
| 8828 | |
| 8829 | static int |
| 8830 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8831 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8832 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8833 | |
| 8834 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8835 | return 0; |
| 8836 | |
| 8837 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8838 | 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] | 8839 | smp->data.type = SMP_T_BIN; |
| 8840 | smp->data.u.str = *data; |
| 8841 | return 1; |
| 8842 | } |
| 8843 | |
| 8844 | static int |
| 8845 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8846 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8847 | struct connection *conn; |
| 8848 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8849 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8850 | |
| 8851 | conn = objt_conn(smp->sess->origin); |
| 8852 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8853 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8854 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8855 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8856 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8857 | if (!capture) |
| 8858 | return 0; |
| 8859 | |
| 8860 | smp->data.type = SMP_T_SINT; |
| 8861 | smp->data.u.sint = capture->xxh64; |
| 8862 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8863 | } |
| 8864 | |
| 8865 | static int |
| 8866 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8867 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8868 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8869 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8870 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8871 | |
| 8872 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8873 | return 0; |
| 8874 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8875 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8876 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8877 | const char *str; |
| 8878 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8879 | 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] | 8880 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8881 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8882 | cipher = SSL_get_cipher_by_value(id); |
| 8883 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8884 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8885 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8886 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8887 | #endif |
| 8888 | str = SSL_CIPHER_get_name(cipher); |
| 8889 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8890 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8891 | else |
| 8892 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8893 | } |
| 8894 | smp->data.type = SMP_T_STR; |
| 8895 | smp->data.u.str = *data; |
| 8896 | return 1; |
| 8897 | #else |
| 8898 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8899 | #endif |
| 8900 | } |
| 8901 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8902 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8903 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8904 | 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] | 8905 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8906 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8907 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8908 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8909 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8910 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8911 | |
| 8912 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8913 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8914 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8915 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8916 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8917 | if (conn->flags & CO_FL_WAIT_XPRT) { |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8918 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8919 | return 0; |
| 8920 | } |
| 8921 | |
| 8922 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8923 | if (!SSL_session_reused(ctx->ssl)) |
| 8924 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8925 | finished_trash->area, |
| 8926 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8927 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8928 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8929 | finished_trash->area, |
| 8930 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8931 | |
| 8932 | if (!finished_len) |
| 8933 | return 0; |
| 8934 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8935 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8936 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8937 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8938 | |
| 8939 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8940 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8941 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8942 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8943 | /* 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] | 8944 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8945 | 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] | 8946 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8947 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8948 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8949 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8950 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8951 | if (!conn || conn->xprt != &ssl_sock) |
| 8952 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8953 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8954 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8955 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8956 | smp->flags = SMP_F_MAY_CHANGE; |
| 8957 | return 0; |
| 8958 | } |
| 8959 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8960 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8961 | 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] | 8962 | smp->flags = 0; |
| 8963 | |
| 8964 | return 1; |
| 8965 | } |
| 8966 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8967 | /* 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] | 8968 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8969 | 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] | 8970 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8971 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8972 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8973 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8974 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8975 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8976 | return 0; |
| 8977 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8978 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8979 | smp->flags = SMP_F_MAY_CHANGE; |
| 8980 | return 0; |
| 8981 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8982 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8983 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8984 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8985 | 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] | 8986 | smp->flags = 0; |
| 8987 | |
| 8988 | return 1; |
| 8989 | } |
| 8990 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8991 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8992 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8993 | 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] | 8994 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8995 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8996 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8997 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8998 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8999 | if (!conn || conn->xprt != &ssl_sock) |
| 9000 | return 0; |
| 9001 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 9002 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 9003 | smp->flags = SMP_F_MAY_CHANGE; |
| 9004 | return 0; |
| 9005 | } |
| 9006 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 9007 | ctx = conn->xprt_ctx; |
| 9008 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9009 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 9010 | 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] | 9011 | smp->flags = 0; |
| 9012 | |
| 9013 | return 1; |
| 9014 | } |
| 9015 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 9016 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9017 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9018 | 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] | 9019 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9020 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9021 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9022 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9023 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9024 | if (!conn || conn->xprt != &ssl_sock) |
| 9025 | return 0; |
| 9026 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 9027 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9028 | smp->flags = SMP_F_MAY_CHANGE; |
| 9029 | return 0; |
| 9030 | } |
| 9031 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9032 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9033 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9034 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9035 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9036 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9037 | 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] | 9038 | smp->flags = 0; |
| 9039 | |
| 9040 | return 1; |
| 9041 | } |
| 9042 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9043 | /* for ca-file and ca-verify-file */ |
| 9044 | 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] | 9045 | { |
| 9046 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9047 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9048 | return ERR_ALERT | ERR_FATAL; |
| 9049 | } |
| 9050 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9051 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9052 | 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] | 9053 | else |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9054 | memprintf(ca_file_p, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9055 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9056 | if (!ssl_store_load_locations_file(*ca_file_p)) { |
| 9057 | 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] | 9058 | return ERR_ALERT | ERR_FATAL; |
| 9059 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9060 | return 0; |
| 9061 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9062 | |
| 9063 | /* parse the "ca-file" bind keyword */ |
| 9064 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9065 | { |
| 9066 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err); |
| 9067 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9068 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9069 | { |
| 9070 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9071 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9072 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9073 | /* parse the "ca-verify-file" bind keyword */ |
| 9074 | static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9075 | { |
| 9076 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err); |
| 9077 | } |
| 9078 | static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9079 | { |
| 9080 | return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9081 | } |
| 9082 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9083 | /* parse the "ca-sign-file" bind keyword */ |
| 9084 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9085 | { |
| 9086 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9087 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9088 | return ERR_ALERT | ERR_FATAL; |
| 9089 | } |
| 9090 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9091 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9092 | 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] | 9093 | else |
| 9094 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 9095 | |
| 9096 | return 0; |
| 9097 | } |
| 9098 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9099 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9100 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9101 | { |
| 9102 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9103 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9104 | return ERR_ALERT | ERR_FATAL; |
| 9105 | } |
| 9106 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 9107 | return 0; |
| 9108 | } |
| 9109 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9110 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9111 | 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] | 9112 | { |
| 9113 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9114 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9115 | return ERR_ALERT | ERR_FATAL; |
| 9116 | } |
| 9117 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9118 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9119 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9120 | return 0; |
| 9121 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9122 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9123 | { |
| 9124 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 9125 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9126 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9127 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9128 | /* parse the "ciphersuites" bind keyword */ |
| 9129 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9130 | { |
| 9131 | if (!*args[cur_arg + 1]) { |
| 9132 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 9133 | return ERR_ALERT | ERR_FATAL; |
| 9134 | } |
| 9135 | |
| 9136 | free(conf->ciphersuites); |
| 9137 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 9138 | return 0; |
| 9139 | } |
| 9140 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9141 | { |
| 9142 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 9143 | } |
| 9144 | #endif |
| 9145 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9146 | /* 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] | 9147 | 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] | 9148 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 9149 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 9150 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9151 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9152 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9153 | return ERR_ALERT | ERR_FATAL; |
| 9154 | } |
| 9155 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9156 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 9157 | 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] | 9158 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 9159 | return ERR_ALERT | ERR_FATAL; |
| 9160 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9161 | 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] | 9162 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9163 | } |
| 9164 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9165 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9166 | } |
| 9167 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9168 | /* 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] | 9169 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9170 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9171 | int err_code; |
| 9172 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9173 | if (!*args[cur_arg + 1]) { |
| 9174 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 9175 | return ERR_ALERT | ERR_FATAL; |
| 9176 | } |
| 9177 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 9178 | 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] | 9179 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 9180 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9181 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9182 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9183 | } |
| 9184 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 9185 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9186 | 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] | 9187 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9188 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9189 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9190 | return ERR_ALERT | ERR_FATAL; |
| 9191 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9192 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9193 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9194 | return ERR_ALERT | ERR_FATAL; |
| 9195 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9196 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9197 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9198 | 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] | 9199 | else |
| 9200 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9201 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9202 | if (!ssl_store_load_locations_file(conf->crl_file)) { |
| 9203 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file); |
| 9204 | return ERR_ALERT | ERR_FATAL; |
| 9205 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9206 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9207 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9208 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9209 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9210 | { |
| 9211 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9212 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9213 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9214 | /* parse the "curves" bind keyword keyword */ |
| 9215 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9216 | { |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 9217 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9218 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9219 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9220 | return ERR_ALERT | ERR_FATAL; |
| 9221 | } |
| 9222 | conf->curves = strdup(args[cur_arg + 1]); |
| 9223 | return 0; |
| 9224 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9225 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9226 | return ERR_ALERT | ERR_FATAL; |
| 9227 | #endif |
| 9228 | } |
| 9229 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9230 | { |
| 9231 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 9232 | } |
| 9233 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9234 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9235 | 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] | 9236 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9237 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9238 | 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] | 9239 | return ERR_ALERT | ERR_FATAL; |
| 9240 | #elif defined(OPENSSL_NO_ECDH) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9241 | 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] | 9242 | return ERR_ALERT | ERR_FATAL; |
| 9243 | #else |
| 9244 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9245 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9246 | return ERR_ALERT | ERR_FATAL; |
| 9247 | } |
| 9248 | |
| 9249 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9250 | |
| 9251 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9252 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9253 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9254 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9255 | { |
| 9256 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 9257 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9258 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9259 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9260 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9261 | { |
| 9262 | int code; |
| 9263 | char *p = args[cur_arg + 1]; |
| 9264 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 9265 | |
| 9266 | if (!*p) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9267 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9268 | return ERR_ALERT | ERR_FATAL; |
| 9269 | } |
| 9270 | |
| 9271 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 9272 | ignerr = &conf->ca_ignerr; |
| 9273 | |
| 9274 | if (strcmp(p, "all") == 0) { |
| 9275 | *ignerr = ~0ULL; |
| 9276 | return 0; |
| 9277 | } |
| 9278 | |
| 9279 | while (p) { |
| 9280 | code = atoi(p); |
| 9281 | if ((code <= 0) || (code > 63)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9282 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 9283 | args[cur_arg], code, args[cur_arg + 1]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9284 | return ERR_ALERT | ERR_FATAL; |
| 9285 | } |
| 9286 | *ignerr |= 1ULL << code; |
| 9287 | p = strchr(p, ','); |
| 9288 | if (p) |
| 9289 | p++; |
| 9290 | } |
| 9291 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9292 | return 0; |
| 9293 | } |
| 9294 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9295 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 9296 | 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] | 9297 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9298 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9299 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9300 | p = strchr(arg, '-'); |
| 9301 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9302 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9303 | p++; |
| 9304 | if (!strcmp(p, "sslv3")) |
| 9305 | v = CONF_SSLV3; |
| 9306 | else if (!strcmp(p, "tlsv10")) |
| 9307 | v = CONF_TLSV10; |
| 9308 | else if (!strcmp(p, "tlsv11")) |
| 9309 | v = CONF_TLSV11; |
| 9310 | else if (!strcmp(p, "tlsv12")) |
| 9311 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9312 | else if (!strcmp(p, "tlsv13")) |
| 9313 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9314 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9315 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9316 | if (!strncmp(arg, "no-", 3)) |
| 9317 | methods->flags |= methodVersions[v].flag; |
| 9318 | else if (!strncmp(arg, "force-", 6)) |
| 9319 | methods->min = methods->max = v; |
| 9320 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9321 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9322 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9323 | fail: |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9324 | memprintf(err, "'%s' : option not implemented", arg); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9325 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9326 | } |
| 9327 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9328 | 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] | 9329 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9330 | 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] | 9331 | } |
| 9332 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9333 | 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] | 9334 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9335 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 9336 | } |
| 9337 | |
| 9338 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 9339 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 9340 | { |
| 9341 | uint16_t i, v = 0; |
| 9342 | char *argv = args[cur_arg + 1]; |
| 9343 | if (!*argv) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9344 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9345 | return ERR_ALERT | ERR_FATAL; |
| 9346 | } |
| 9347 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9348 | if (!strcmp(argv, methodVersions[i].name)) |
| 9349 | v = i; |
| 9350 | if (!v) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9351 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9352 | return ERR_ALERT | ERR_FATAL; |
| 9353 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9354 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 9355 | methods->min = v; |
| 9356 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 9357 | methods->max = v; |
| 9358 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9359 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9360 | return ERR_ALERT | ERR_FATAL; |
| 9361 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9362 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9363 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9364 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9365 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9366 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9367 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9368 | 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] | 9369 | #endif |
| 9370 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 9371 | } |
| 9372 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9373 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9374 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9375 | 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] | 9376 | } |
| 9377 | |
| 9378 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9379 | { |
| 9380 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 9381 | } |
| 9382 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9383 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9384 | 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] | 9385 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 9386 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9387 | return 0; |
| 9388 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9389 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9390 | /* parse the "allow-0rtt" bind keyword */ |
| 9391 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9392 | { |
| 9393 | conf->early_data = 1; |
| 9394 | return 0; |
| 9395 | } |
| 9396 | |
| 9397 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9398 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 9399 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9400 | return 0; |
| 9401 | } |
| 9402 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9403 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9404 | 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] | 9405 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9406 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9407 | char *p1, *p2; |
| 9408 | |
| 9409 | if (!*args[cur_arg + 1]) { |
| 9410 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 9411 | return ERR_ALERT | ERR_FATAL; |
| 9412 | } |
| 9413 | |
| 9414 | free(conf->npn_str); |
| 9415 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9416 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9417 | * so we reuse each comma to store the next <len> and need |
| 9418 | * one more for the end of the string. |
| 9419 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9420 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9421 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9422 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 9423 | |
| 9424 | /* replace commas with the name length */ |
| 9425 | p1 = conf->npn_str; |
| 9426 | p2 = p1 + 1; |
| 9427 | while (1) { |
| 9428 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 9429 | if (!p2) |
| 9430 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9431 | |
| 9432 | if (p2 - (p1 + 1) > 255) { |
| 9433 | *p2 = '\0'; |
| 9434 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9435 | return ERR_ALERT | ERR_FATAL; |
| 9436 | } |
| 9437 | |
| 9438 | *p1 = p2 - (p1 + 1); |
| 9439 | p1 = p2; |
| 9440 | |
| 9441 | if (!*p2) |
| 9442 | break; |
| 9443 | |
| 9444 | *(p2++) = '\0'; |
| 9445 | } |
| 9446 | return 0; |
| 9447 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9448 | 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] | 9449 | return ERR_ALERT | ERR_FATAL; |
| 9450 | #endif |
| 9451 | } |
| 9452 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9453 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9454 | { |
| 9455 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9456 | } |
| 9457 | |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9458 | |
| 9459 | /* Parses a alpn string and converts it to the right format for the SSL api */ |
| 9460 | int ssl_sock_parse_alpn(char *arg, char **alpn_str, int *alpn_len, char **err) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9461 | { |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9462 | char *p1, *p2, *alpn = NULL; |
| 9463 | int len, ret = 0; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9464 | |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9465 | *alpn_str = NULL; |
| 9466 | *alpn_len = 0; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9467 | |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9468 | if (!*arg) { |
| 9469 | memprintf(err, "missing the comma-delimited ALPN protocol suite"); |
| 9470 | goto error; |
| 9471 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9472 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9473 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9474 | * so we reuse each comma to store the next <len> and need |
| 9475 | * one more for the end of the string. |
| 9476 | */ |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9477 | len = strlen(arg) + 1; |
| 9478 | alpn = calloc(1, len+1); |
| 9479 | if (!alpn) { |
| 9480 | memprintf(err, "'%s' : out of memory", arg); |
| 9481 | goto error; |
| 9482 | } |
| 9483 | memcpy(alpn+1, arg, len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9484 | |
| 9485 | /* replace commas with the name length */ |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9486 | p1 = alpn; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9487 | p2 = p1 + 1; |
| 9488 | while (1) { |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9489 | p2 = memchr(p1 + 1, ',', alpn + len - (p1 + 1)); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9490 | if (!p2) |
| 9491 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9492 | |
| 9493 | if (p2 - (p1 + 1) > 255) { |
| 9494 | *p2 = '\0'; |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9495 | memprintf(err, "ALPN protocol name too long : '%s'", p1 + 1); |
| 9496 | goto error; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9497 | } |
| 9498 | |
| 9499 | *p1 = p2 - (p1 + 1); |
| 9500 | p1 = p2; |
| 9501 | |
| 9502 | if (!*p2) |
| 9503 | break; |
| 9504 | |
| 9505 | *(p2++) = '\0'; |
| 9506 | } |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9507 | |
| 9508 | *alpn_str = alpn; |
| 9509 | *alpn_len = len; |
| 9510 | |
| 9511 | out: |
| 9512 | return ret; |
| 9513 | |
| 9514 | error: |
| 9515 | free(alpn); |
| 9516 | ret = ERR_ALERT | ERR_FATAL; |
| 9517 | goto out; |
| 9518 | } |
| 9519 | |
| 9520 | /* parse the "alpn" bind keyword */ |
| 9521 | static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9522 | { |
| 9523 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9524 | int ret; |
| 9525 | |
| 9526 | free(conf->alpn_str); |
| 9527 | |
| 9528 | ret = ssl_sock_parse_alpn(args[cur_arg + 1], &conf->alpn_str, &conf->alpn_len, err); |
| 9529 | if (ret) |
| 9530 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
| 9531 | return ret; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9532 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9533 | 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] | 9534 | return ERR_ALERT | ERR_FATAL; |
| 9535 | #endif |
| 9536 | } |
| 9537 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9538 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9539 | { |
| 9540 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9541 | } |
| 9542 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9543 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9544 | 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] | 9545 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 9546 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9547 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9548 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9549 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 9550 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Jerome Magnin | b203ff6 | 2020-04-03 15:28:22 +0200 | [diff] [blame] | 9551 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
| 9552 | if (global_ssl.listen_default_curves && !conf->ssl_conf.curves) |
| 9553 | conf->ssl_conf.curves = strdup(global_ssl.listen_default_curves); |
| 9554 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9555 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9556 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 9557 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9558 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9559 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9560 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 9561 | if (!conf->ssl_conf.ssl_methods.min) |
| 9562 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 9563 | if (!conf->ssl_conf.ssl_methods.max) |
| 9564 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9565 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9566 | return 0; |
| 9567 | } |
| 9568 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9569 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 9570 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9571 | { |
| 9572 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 9573 | return 0; |
| 9574 | } |
| 9575 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9576 | /* parse the "generate-certificates" bind keyword */ |
| 9577 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9578 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9579 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9580 | conf->generate_certs = 1; |
| 9581 | #else |
| 9582 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 9583 | err && *err ? *err : ""); |
| 9584 | #endif |
| 9585 | return 0; |
| 9586 | } |
| 9587 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9588 | /* parse the "strict-sni" bind keyword */ |
| 9589 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9590 | { |
| 9591 | conf->strict_sni = 1; |
| 9592 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9593 | } |
| 9594 | |
| 9595 | /* parse the "tls-ticket-keys" bind keyword */ |
| 9596 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9597 | { |
| 9598 | #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] | 9599 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9600 | int i = 0; |
| 9601 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9602 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9603 | |
| 9604 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9605 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9606 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9607 | } |
| 9608 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9609 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9610 | if (keys_ref) { |
| 9611 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9612 | conf->keys_ref = keys_ref; |
| 9613 | return 0; |
| 9614 | } |
| 9615 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9616 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9617 | if (!keys_ref) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9618 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9619 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9620 | } |
| 9621 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9622 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9623 | if (!keys_ref->tlskeys) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9624 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9625 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9626 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9627 | |
| 9628 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9629 | 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] | 9630 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9631 | } |
| 9632 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9633 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9634 | if (!keys_ref->filename) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9635 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9636 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9637 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9638 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9639 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9640 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 9641 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9642 | int dec_size; |
| 9643 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9644 | /* Strip newline characters from the end */ |
| 9645 | if(thisline[len - 1] == '\n') |
| 9646 | thisline[--len] = 0; |
| 9647 | |
| 9648 | if(thisline[len - 1] == '\r') |
| 9649 | thisline[--len] = 0; |
| 9650 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9651 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 9652 | if (dec_size < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9653 | 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] | 9654 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9655 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9656 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 9657 | keys_ref->key_size_bits = 128; |
| 9658 | } |
| 9659 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 9660 | keys_ref->key_size_bits = 256; |
| 9661 | } |
| 9662 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 9663 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 9664 | || ((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] | 9665 | 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] | 9666 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9667 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9668 | i++; |
| 9669 | } |
| 9670 | |
| 9671 | if (i < TLS_TICKETS_NO) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9672 | 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] | 9673 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9674 | } |
| 9675 | |
| 9676 | fclose(f); |
| 9677 | |
| 9678 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 9679 | i -= 2; |
| 9680 | 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] | 9681 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9682 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9683 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9684 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9685 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9686 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 9687 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9688 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9689 | |
| 9690 | fail: |
| 9691 | if (f) |
| 9692 | fclose(f); |
| 9693 | if (keys_ref) { |
| 9694 | free(keys_ref->filename); |
| 9695 | free(keys_ref->tlskeys); |
| 9696 | free(keys_ref); |
| 9697 | } |
| 9698 | return ERR_ALERT | ERR_FATAL; |
| 9699 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9700 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9701 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9702 | return ERR_ALERT | ERR_FATAL; |
| 9703 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9704 | } |
| 9705 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9706 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9707 | 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] | 9708 | { |
| 9709 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9710 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9711 | return ERR_ALERT | ERR_FATAL; |
| 9712 | } |
| 9713 | |
| 9714 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9715 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9716 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9717 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9718 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9719 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9720 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9721 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 9722 | args[cur_arg], args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9723 | return ERR_ALERT | ERR_FATAL; |
| 9724 | } |
| 9725 | |
| 9726 | return 0; |
| 9727 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9728 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9729 | { |
| 9730 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 9731 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9732 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9733 | /* parse the "no-ca-names" bind keyword */ |
| 9734 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9735 | { |
| 9736 | conf->no_ca_names = 1; |
| 9737 | return 0; |
| 9738 | } |
| 9739 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9740 | { |
| 9741 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 9742 | } |
| 9743 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9744 | /************** "server" keywords ****************/ |
| 9745 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9746 | /* parse the "npn" bind keyword */ |
| 9747 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9748 | { |
| 9749 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9750 | char *p1, *p2; |
| 9751 | |
| 9752 | if (!*args[*cur_arg + 1]) { |
| 9753 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 9754 | return ERR_ALERT | ERR_FATAL; |
| 9755 | } |
| 9756 | |
| 9757 | free(newsrv->ssl_ctx.npn_str); |
| 9758 | |
| 9759 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9760 | * so we reuse each comma to store the next <len> and need |
| 9761 | * one more for the end of the string. |
| 9762 | */ |
| 9763 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9764 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 9765 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 9766 | newsrv->ssl_ctx.npn_len); |
| 9767 | |
| 9768 | /* replace commas with the name length */ |
| 9769 | p1 = newsrv->ssl_ctx.npn_str; |
| 9770 | p2 = p1 + 1; |
| 9771 | while (1) { |
| 9772 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 9773 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 9774 | if (!p2) |
| 9775 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9776 | |
| 9777 | if (p2 - (p1 + 1) > 255) { |
| 9778 | *p2 = '\0'; |
| 9779 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9780 | return ERR_ALERT | ERR_FATAL; |
| 9781 | } |
| 9782 | |
| 9783 | *p1 = p2 - (p1 + 1); |
| 9784 | p1 = p2; |
| 9785 | |
| 9786 | if (!*p2) |
| 9787 | break; |
| 9788 | |
| 9789 | *(p2++) = '\0'; |
| 9790 | } |
| 9791 | return 0; |
| 9792 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9793 | 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] | 9794 | return ERR_ALERT | ERR_FATAL; |
| 9795 | #endif |
| 9796 | } |
| 9797 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9798 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9799 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9800 | { |
| 9801 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9802 | char **alpn_str; |
| 9803 | int *alpn_len; |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9804 | int ret; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9805 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9806 | if (*args[*cur_arg] == 'c') { |
| 9807 | alpn_str = &newsrv->check.alpn_str; |
| 9808 | alpn_len = &newsrv->check.alpn_len; |
| 9809 | } else { |
| 9810 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9811 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9812 | |
| 9813 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9814 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9815 | free(*alpn_str); |
Christopher Faulet | d75f57e | 2020-04-20 18:32:29 +0200 | [diff] [blame] | 9816 | ret = ssl_sock_parse_alpn(args[*cur_arg + 1], alpn_str, alpn_len, err); |
| 9817 | if (ret) |
| 9818 | memprintf(err, "'%s' : %s", args[*cur_arg], *err); |
| 9819 | return ret; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9820 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9821 | 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] | 9822 | return ERR_ALERT | ERR_FATAL; |
| 9823 | #endif |
| 9824 | } |
| 9825 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9826 | /* parse the "ca-file" server keyword */ |
| 9827 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9828 | { |
| 9829 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9830 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9831 | return ERR_ALERT | ERR_FATAL; |
| 9832 | } |
| 9833 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9834 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9835 | 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] | 9836 | else |
| 9837 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9838 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 9839 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) { |
| 9840 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file); |
| 9841 | return ERR_ALERT | ERR_FATAL; |
| 9842 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9843 | return 0; |
| 9844 | } |
| 9845 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9846 | /* parse the "check-sni" server keyword */ |
| 9847 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9848 | { |
| 9849 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9850 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9851 | return ERR_ALERT | ERR_FATAL; |
| 9852 | } |
| 9853 | |
| 9854 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9855 | if (!newsrv->check.sni) { |
| 9856 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9857 | return ERR_ALERT | ERR_FATAL; |
| 9858 | } |
| 9859 | return 0; |
| 9860 | |
| 9861 | } |
| 9862 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9863 | /* parse the "check-ssl" server keyword */ |
| 9864 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9865 | { |
| 9866 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9867 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9868 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9869 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9870 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9871 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9872 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9873 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9874 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9875 | if (!newsrv->ssl_ctx.methods.min) |
| 9876 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9877 | if (!newsrv->ssl_ctx.methods.max) |
| 9878 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9879 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9880 | return 0; |
| 9881 | } |
| 9882 | |
| 9883 | /* parse the "ciphers" server keyword */ |
| 9884 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9885 | { |
| 9886 | if (!*args[*cur_arg + 1]) { |
| 9887 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9888 | return ERR_ALERT | ERR_FATAL; |
| 9889 | } |
| 9890 | |
| 9891 | free(newsrv->ssl_ctx.ciphers); |
| 9892 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9893 | return 0; |
| 9894 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9895 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9896 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9897 | /* parse the "ciphersuites" server keyword */ |
| 9898 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9899 | { |
| 9900 | if (!*args[*cur_arg + 1]) { |
| 9901 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9902 | return ERR_ALERT | ERR_FATAL; |
| 9903 | } |
| 9904 | |
| 9905 | free(newsrv->ssl_ctx.ciphersuites); |
| 9906 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9907 | return 0; |
| 9908 | } |
| 9909 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9910 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9911 | /* parse the "crl-file" server keyword */ |
| 9912 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9913 | { |
| 9914 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9915 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9916 | return ERR_ALERT | ERR_FATAL; |
| 9917 | #else |
| 9918 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9919 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9920 | return ERR_ALERT | ERR_FATAL; |
| 9921 | } |
| 9922 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9923 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9924 | 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] | 9925 | else |
| 9926 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9927 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9928 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) { |
| 9929 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file); |
| 9930 | return ERR_ALERT | ERR_FATAL; |
| 9931 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9932 | return 0; |
| 9933 | #endif |
| 9934 | } |
| 9935 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9936 | /* parse the "crt" server keyword */ |
| 9937 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9938 | { |
| 9939 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9940 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9941 | return ERR_ALERT | ERR_FATAL; |
| 9942 | } |
| 9943 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9944 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9945 | 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] | 9946 | else |
| 9947 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9948 | |
| 9949 | return 0; |
| 9950 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9951 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9952 | /* parse the "no-check-ssl" server keyword */ |
| 9953 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9954 | { |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 9955 | newsrv->check.use_ssl = -1; |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9956 | free(newsrv->ssl_ctx.ciphers); |
| 9957 | newsrv->ssl_ctx.ciphers = NULL; |
| 9958 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9959 | return 0; |
| 9960 | } |
| 9961 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9962 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9963 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9964 | { |
| 9965 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9966 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9967 | return 0; |
| 9968 | } |
| 9969 | |
| 9970 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9971 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9972 | { |
| 9973 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9974 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9975 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9976 | return 0; |
| 9977 | } |
| 9978 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9979 | /* parse the "no-ssl" server keyword */ |
| 9980 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9981 | { |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 9982 | newsrv->use_ssl = -1; |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9983 | free(newsrv->ssl_ctx.ciphers); |
| 9984 | newsrv->ssl_ctx.ciphers = NULL; |
| 9985 | return 0; |
| 9986 | } |
| 9987 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9988 | /* parse the "allow-0rtt" server keyword */ |
| 9989 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9990 | { |
| 9991 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9992 | return 0; |
| 9993 | } |
| 9994 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9995 | /* parse the "no-ssl-reuse" server keyword */ |
| 9996 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9997 | { |
| 9998 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9999 | return 0; |
| 10000 | } |
| 10001 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 10002 | /* parse the "no-tls-tickets" server keyword */ |
| 10003 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10004 | { |
| 10005 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 10006 | return 0; |
| 10007 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 10008 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 10009 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10010 | { |
| 10011 | newsrv->pp_opts |= SRV_PP_V2; |
| 10012 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 10013 | return 0; |
| 10014 | } |
| 10015 | |
| 10016 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 10017 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10018 | { |
| 10019 | newsrv->pp_opts |= SRV_PP_V2; |
| 10020 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 10021 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 10022 | return 0; |
| 10023 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 10024 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10025 | /* parse the "sni" server keyword */ |
| 10026 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10027 | { |
| 10028 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 10029 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 10030 | return ERR_ALERT | ERR_FATAL; |
| 10031 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10032 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10033 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10034 | arg = args[*cur_arg + 1]; |
| 10035 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10036 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 10037 | return ERR_ALERT | ERR_FATAL; |
| 10038 | } |
| 10039 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10040 | free(newsrv->sni_expr); |
| 10041 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10042 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10043 | return 0; |
| 10044 | #endif |
| 10045 | } |
| 10046 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10047 | /* parse the "ssl" server keyword */ |
| 10048 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10049 | { |
| 10050 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10051 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 10052 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10053 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10054 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 10055 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 10056 | #endif |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 10057 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
| 10058 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 10059 | |
| 10060 | if (!newsrv->ssl_ctx.methods.min) |
| 10061 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 10062 | |
| 10063 | if (!newsrv->ssl_ctx.methods.max) |
| 10064 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 10065 | |
| 10066 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10067 | return 0; |
| 10068 | } |
| 10069 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 10070 | /* parse the "ssl-reuse" server keyword */ |
| 10071 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10072 | { |
| 10073 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 10074 | return 0; |
| 10075 | } |
| 10076 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 10077 | /* parse the "tls-tickets" server keyword */ |
| 10078 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10079 | { |
| 10080 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 10081 | return 0; |
| 10082 | } |
| 10083 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10084 | /* parse the "verify" server keyword */ |
| 10085 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10086 | { |
| 10087 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10088 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10089 | return ERR_ALERT | ERR_FATAL; |
| 10090 | } |
| 10091 | |
| 10092 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 10093 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10094 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 10095 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10096 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10097 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 10098 | args[*cur_arg], args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10099 | return ERR_ALERT | ERR_FATAL; |
| 10100 | } |
| 10101 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10102 | return 0; |
| 10103 | } |
| 10104 | |
| 10105 | /* parse the "verifyhost" server keyword */ |
| 10106 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10107 | { |
| 10108 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10109 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10110 | return ERR_ALERT | ERR_FATAL; |
| 10111 | } |
| 10112 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 10113 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10114 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 10115 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10116 | return 0; |
| 10117 | } |
| 10118 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10119 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 10120 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 10121 | struct proxy *defpx, const char *file, int line, |
| 10122 | char **err) { |
| 10123 | int i = 1; |
| 10124 | |
| 10125 | if (*(args[i]) == 0) { |
| 10126 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10127 | return -1; |
| 10128 | } |
| 10129 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10130 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10131 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 10132 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 10133 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10134 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10135 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 10136 | i++; |
| 10137 | else { |
| 10138 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10139 | return -1; |
| 10140 | } |
| 10141 | } |
| 10142 | 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] | 10143 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10144 | return -1; |
| 10145 | } |
| 10146 | i++; |
| 10147 | } |
| 10148 | return 0; |
| 10149 | } |
| 10150 | |
| 10151 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 10152 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 10153 | struct proxy *defpx, const char *file, int line, |
| 10154 | char **err) { |
| 10155 | int i = 1; |
| 10156 | |
| 10157 | if (*(args[i]) == 0) { |
| 10158 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10159 | return -1; |
| 10160 | } |
| 10161 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10162 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10163 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10164 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10165 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 10166 | i++; |
| 10167 | else { |
| 10168 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10169 | return -1; |
| 10170 | } |
| 10171 | } |
| 10172 | 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] | 10173 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10174 | return -1; |
| 10175 | } |
| 10176 | i++; |
| 10177 | } |
| 10178 | return 0; |
| 10179 | } |
| 10180 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10181 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 10182 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10183 | */ |
| 10184 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 10185 | struct proxy *defpx, const char *file, int line, |
| 10186 | char **err) |
| 10187 | { |
| 10188 | char **target; |
| 10189 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10190 | 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] | 10191 | |
| 10192 | if (too_many_args(1, args, err, NULL)) |
| 10193 | return -1; |
| 10194 | |
| 10195 | if (*target) { |
| 10196 | memprintf(err, "'%s' already specified.", args[0]); |
| 10197 | return -1; |
| 10198 | } |
| 10199 | |
| 10200 | if (*(args[1]) == 0) { |
| 10201 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 10202 | return -1; |
| 10203 | } |
| 10204 | *target = strdup(args[1]); |
| 10205 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
Emmanuel Hocdet | c3b7e74 | 2020-04-22 11:06:19 +0200 | [diff] [blame] | 10208 | /* parse the "ssl-skip-self-issued-ca" keyword in global section. */ |
| 10209 | static int ssl_parse_skip_self_issued_ca(char **args, int section_type, struct proxy *curpx, |
| 10210 | struct proxy *defpx, const char *file, int line, |
| 10211 | char **err) |
| 10212 | { |
| 10213 | global_ssl.skip_self_issued_ca = 1; |
| 10214 | return 0; |
| 10215 | } |
| 10216 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10217 | /* "issuers-chain-path" load chain certificate in global */ |
| 10218 | static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
| 10219 | { |
| 10220 | X509 *ca; |
| 10221 | X509_NAME *name = NULL; |
| 10222 | ASN1_OCTET_STRING *skid = NULL; |
| 10223 | STACK_OF(X509) *chain = NULL; |
| 10224 | struct issuer_chain *issuer; |
| 10225 | struct eb64_node *node; |
| 10226 | char *path; |
| 10227 | u64 key; |
| 10228 | int ret = 0; |
| 10229 | |
| 10230 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 10231 | if (chain == NULL) { |
| 10232 | chain = sk_X509_new_null(); |
| 10233 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 10234 | name = X509_get_subject_name(ca); |
| 10235 | } |
| 10236 | if (!sk_X509_push(chain, ca)) { |
| 10237 | X509_free(ca); |
| 10238 | goto end; |
| 10239 | } |
| 10240 | } |
| 10241 | if (!chain) { |
| 10242 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 10243 | goto end; |
| 10244 | } |
| 10245 | if (!skid) { |
| 10246 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 10247 | goto end; |
| 10248 | } |
| 10249 | if (!name) { |
| 10250 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 10251 | goto end; |
| 10252 | } |
| 10253 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10254 | 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] | 10255 | issuer = container_of(node, typeof(*issuer), node); |
| 10256 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 10257 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 10258 | goto end; |
| 10259 | } |
| 10260 | } |
| 10261 | issuer = calloc(1, sizeof *issuer); |
| 10262 | path = strdup(fp); |
| 10263 | if (!issuer || !path) { |
| 10264 | free(issuer); |
| 10265 | free(path); |
| 10266 | goto end; |
| 10267 | } |
| 10268 | issuer->node.key = key; |
| 10269 | issuer->path = path; |
| 10270 | issuer->chain = chain; |
| 10271 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10272 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10273 | ret = 1; |
| 10274 | end: |
| 10275 | if (skid) |
| 10276 | ASN1_OCTET_STRING_free(skid); |
| 10277 | if (chain) |
| 10278 | sk_X509_pop_free(chain, X509_free); |
| 10279 | return ret; |
| 10280 | } |
| 10281 | |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 10282 | static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 10283 | { |
| 10284 | AUTHORITY_KEYID *akid; |
| 10285 | struct issuer_chain *issuer = NULL; |
| 10286 | |
| 10287 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 10288 | if (akid) { |
| 10289 | struct eb64_node *node; |
| 10290 | u64 hk; |
| 10291 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 10292 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 10293 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 10294 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 10295 | issuer = ti; |
| 10296 | break; |
| 10297 | } |
| 10298 | } |
| 10299 | AUTHORITY_KEYID_free(akid); |
| 10300 | } |
| 10301 | return issuer; |
| 10302 | } |
| 10303 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10304 | static void ssl_free_global_issuers(void) |
| 10305 | { |
| 10306 | struct eb64_node *node, *back; |
| 10307 | struct issuer_chain *issuer; |
| 10308 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10309 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10310 | while (node) { |
| 10311 | issuer = container_of(node, typeof(*issuer), node); |
| 10312 | back = eb64_next(node); |
| 10313 | eb64_delete(node); |
| 10314 | free(issuer->path); |
| 10315 | sk_X509_pop_free(issuer->chain, X509_free); |
| 10316 | free(issuer); |
| 10317 | node = back; |
| 10318 | } |
| 10319 | } |
| 10320 | |
| 10321 | static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx, |
| 10322 | struct proxy *defpx, const char *file, int line, |
| 10323 | char **err) |
| 10324 | { |
| 10325 | char *path; |
| 10326 | struct dirent **de_list; |
| 10327 | int i, n; |
| 10328 | struct stat buf; |
| 10329 | char *end; |
| 10330 | char fp[MAXPATHLEN+1]; |
| 10331 | |
| 10332 | if (too_many_args(1, args, err, NULL)) |
| 10333 | return -1; |
| 10334 | |
| 10335 | path = args[1]; |
| 10336 | if (*path == 0 || stat(path, &buf)) { |
| 10337 | memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n", |
| 10338 | err && *err ? *err : "", args[0]); |
| 10339 | return -1; |
| 10340 | } |
| 10341 | if (S_ISDIR(buf.st_mode) == 0) { |
| 10342 | memprintf(err, "%sglobal statement '%s': %s is not a directory.\n", |
| 10343 | err && *err ? *err : "", args[0], path); |
| 10344 | return -1; |
| 10345 | } |
| 10346 | |
| 10347 | /* strip trailing slashes, including first one */ |
| 10348 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 10349 | *end = 0; |
| 10350 | /* path already parsed? */ |
| 10351 | if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0) |
| 10352 | return 0; |
| 10353 | /* overwrite old issuers_chain_path */ |
| 10354 | free(global_ssl.issuers_chain_path); |
| 10355 | global_ssl.issuers_chain_path = strdup(path); |
| 10356 | ssl_free_global_issuers(); |
| 10357 | |
| 10358 | n = scandir(path, &de_list, 0, alphasort); |
| 10359 | if (n < 0) { |
| 10360 | memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n", |
| 10361 | err && *err ? *err : "", args[0], path, strerror(errno)); |
| 10362 | return -1; |
| 10363 | } |
| 10364 | for (i = 0; i < n; i++) { |
| 10365 | struct dirent *de = de_list[i]; |
| 10366 | BIO *in = NULL; |
| 10367 | char *warn = NULL; |
| 10368 | |
| 10369 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 10370 | free(de); |
| 10371 | if (stat(fp, &buf) != 0) { |
| 10372 | ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno)); |
| 10373 | goto next; |
| 10374 | } |
| 10375 | if (!S_ISREG(buf.st_mode)) |
| 10376 | goto next; |
| 10377 | |
| 10378 | in = BIO_new(BIO_s_file()); |
| 10379 | if (in == NULL) |
| 10380 | goto next; |
| 10381 | if (BIO_read_filename(in, fp) <= 0) |
| 10382 | goto next; |
| 10383 | ssl_load_global_issuer_from_BIO(in, fp, &warn); |
| 10384 | if (warn) { |
Tim Duesterhus | e8aa5f2 | 2020-02-19 11:41:13 +0100 | [diff] [blame] | 10385 | ha_warning("%s", warn); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10386 | free(warn); |
| 10387 | warn = NULL; |
| 10388 | } |
| 10389 | next: |
| 10390 | if (in) |
| 10391 | BIO_free(in); |
| 10392 | } |
| 10393 | free(de_list); |
| 10394 | |
| 10395 | return 0; |
| 10396 | } |
| 10397 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10398 | /* parse the "ssl-mode-async" keyword in global section. |
| 10399 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10400 | */ |
| 10401 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 10402 | struct proxy *defpx, const char *file, int line, |
| 10403 | char **err) |
| 10404 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10405 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10406 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 10407 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10408 | return 0; |
| 10409 | #else |
| 10410 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 10411 | return -1; |
| 10412 | #endif |
| 10413 | } |
| 10414 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10415 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10416 | static int ssl_check_async_engine_count(void) { |
| 10417 | int err_code = 0; |
| 10418 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 10419 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 10420 | 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] | 10421 | err_code = ERR_ABORT; |
| 10422 | } |
| 10423 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10424 | } |
| 10425 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10426 | /* parse the "ssl-engine" keyword in global section. |
| 10427 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10428 | */ |
| 10429 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 10430 | struct proxy *defpx, const char *file, int line, |
| 10431 | char **err) |
| 10432 | { |
| 10433 | char *algo; |
| 10434 | int ret = -1; |
| 10435 | |
| 10436 | if (*(args[1]) == 0) { |
| 10437 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 10438 | return ret; |
| 10439 | } |
| 10440 | |
| 10441 | if (*(args[2]) == 0) { |
| 10442 | /* if no list of algorithms is given, it defaults to ALL */ |
| 10443 | algo = strdup("ALL"); |
| 10444 | goto add_engine; |
| 10445 | } |
| 10446 | |
| 10447 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 10448 | if (strcmp(args[2], "algo") != 0) { |
| 10449 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 10450 | return ret; |
| 10451 | } |
| 10452 | |
| 10453 | if (*(args[3]) == 0) { |
| 10454 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 10455 | return ret; |
| 10456 | } |
| 10457 | algo = strdup(args[3]); |
| 10458 | |
| 10459 | add_engine: |
| 10460 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 10461 | openssl_engines_initialized++; |
| 10462 | ret = 0; |
| 10463 | } |
| 10464 | free(algo); |
| 10465 | return ret; |
| 10466 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10467 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10468 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10469 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 10470 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10471 | */ |
| 10472 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 10473 | struct proxy *defpx, const char *file, int line, |
| 10474 | char **err) |
| 10475 | { |
| 10476 | char **target; |
| 10477 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10478 | 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] | 10479 | |
| 10480 | if (too_many_args(1, args, err, NULL)) |
| 10481 | return -1; |
| 10482 | |
| 10483 | if (*(args[1]) == 0) { |
| 10484 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10485 | return -1; |
| 10486 | } |
| 10487 | |
| 10488 | free(*target); |
| 10489 | *target = strdup(args[1]); |
| 10490 | return 0; |
| 10491 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10492 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10493 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10494 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 10495 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10496 | */ |
| 10497 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 10498 | struct proxy *defpx, const char *file, int line, |
| 10499 | char **err) |
| 10500 | { |
| 10501 | char **target; |
| 10502 | |
| 10503 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 10504 | |
| 10505 | if (too_many_args(1, args, err, NULL)) |
| 10506 | return -1; |
| 10507 | |
| 10508 | if (*(args[1]) == 0) { |
| 10509 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10510 | return -1; |
| 10511 | } |
| 10512 | |
| 10513 | free(*target); |
| 10514 | *target = strdup(args[1]); |
| 10515 | return 0; |
| 10516 | } |
| 10517 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10518 | |
Jerome Magnin | b203ff6 | 2020-04-03 15:28:22 +0200 | [diff] [blame] | 10519 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
| 10520 | /* |
| 10521 | * parse the "ssl-default-bind-curves" keyword in a global section. |
| 10522 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10523 | */ |
| 10524 | static int ssl_parse_global_curves(char **args, int section_type, struct proxy *curpx, |
| 10525 | struct proxy *defpx, const char *file, int line, |
| 10526 | char **err) |
| 10527 | { |
| 10528 | char **target; |
| 10529 | target = &global_ssl.listen_default_curves; |
| 10530 | |
| 10531 | if (too_many_args(1, args, err, NULL)) |
| 10532 | return -1; |
| 10533 | |
| 10534 | if (*(args[1]) == 0) { |
| 10535 | memprintf(err, "global statement '%s' expects a curves suite as an arguments.", args[0]); |
| 10536 | return -1; |
| 10537 | } |
| 10538 | |
| 10539 | free(*target); |
| 10540 | *target = strdup(args[1]); |
| 10541 | return 0; |
| 10542 | } |
| 10543 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10544 | /* parse various global tune.ssl settings consisting in positive integers. |
| 10545 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10546 | */ |
| 10547 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 10548 | struct proxy *defpx, const char *file, int line, |
| 10549 | char **err) |
| 10550 | { |
| 10551 | int *target; |
| 10552 | |
| 10553 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 10554 | target = &global.tune.sslcachesize; |
| 10555 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10556 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10557 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10558 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10559 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 10560 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10561 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 10562 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10563 | else { |
| 10564 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 10565 | return -1; |
| 10566 | } |
| 10567 | |
| 10568 | if (too_many_args(1, args, err, NULL)) |
| 10569 | return -1; |
| 10570 | |
| 10571 | if (*(args[1]) == 0) { |
| 10572 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10573 | return -1; |
| 10574 | } |
| 10575 | |
| 10576 | *target = atoi(args[1]); |
| 10577 | if (*target < 0) { |
| 10578 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 10579 | return -1; |
| 10580 | } |
| 10581 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10582 | } |
| 10583 | |
| 10584 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 10585 | struct proxy *defpx, const char *file, int line, |
| 10586 | char **err) |
| 10587 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10588 | int ret; |
| 10589 | |
| 10590 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 10591 | if (ret != 0) |
| 10592 | return ret; |
| 10593 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10594 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10595 | memprintf(err, "'%s' is already configured.", args[0]); |
| 10596 | return -1; |
| 10597 | } |
| 10598 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10599 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 10600 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10601 | memprintf(err, "Out of memory error."); |
| 10602 | return -1; |
| 10603 | } |
| 10604 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10605 | } |
| 10606 | |
| 10607 | /* parse "ssl.force-private-cache". |
| 10608 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10609 | */ |
| 10610 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 10611 | struct proxy *defpx, const char *file, int line, |
| 10612 | char **err) |
| 10613 | { |
| 10614 | if (too_many_args(0, args, err, NULL)) |
| 10615 | return -1; |
| 10616 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10617 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10618 | return 0; |
| 10619 | } |
| 10620 | |
| 10621 | /* parse "ssl.lifetime". |
| 10622 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10623 | */ |
| 10624 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 10625 | struct proxy *defpx, const char *file, int line, |
| 10626 | char **err) |
| 10627 | { |
| 10628 | const char *res; |
| 10629 | |
| 10630 | if (too_many_args(1, args, err, NULL)) |
| 10631 | return -1; |
| 10632 | |
| 10633 | if (*(args[1]) == 0) { |
| 10634 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 10635 | return -1; |
| 10636 | } |
| 10637 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10638 | 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] | 10639 | if (res == PARSE_TIME_OVER) { |
| 10640 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 10641 | args[1], args[0]); |
| 10642 | return -1; |
| 10643 | } |
| 10644 | else if (res == PARSE_TIME_UNDER) { |
| 10645 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 10646 | args[1], args[0]); |
| 10647 | return -1; |
| 10648 | } |
| 10649 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10650 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 10651 | return -1; |
| 10652 | } |
| 10653 | return 0; |
| 10654 | } |
| 10655 | |
| 10656 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10657 | /* parse "ssl-dh-param-file". |
| 10658 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10659 | */ |
| 10660 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 10661 | struct proxy *defpx, const char *file, int line, |
| 10662 | char **err) |
| 10663 | { |
| 10664 | if (too_many_args(1, args, err, NULL)) |
| 10665 | return -1; |
| 10666 | |
| 10667 | if (*(args[1]) == 0) { |
| 10668 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 10669 | return -1; |
| 10670 | } |
| 10671 | |
| 10672 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 10673 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 10674 | return -1; |
| 10675 | } |
| 10676 | return 0; |
| 10677 | } |
| 10678 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10679 | /* parse "ssl.default-dh-param". |
| 10680 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10681 | */ |
| 10682 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 10683 | struct proxy *defpx, const char *file, int line, |
| 10684 | char **err) |
| 10685 | { |
| 10686 | if (too_many_args(1, args, err, NULL)) |
| 10687 | return -1; |
| 10688 | |
| 10689 | if (*(args[1]) == 0) { |
| 10690 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10691 | return -1; |
| 10692 | } |
| 10693 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10694 | global_ssl.default_dh_param = atoi(args[1]); |
| 10695 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10696 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 10697 | return -1; |
| 10698 | } |
| 10699 | return 0; |
| 10700 | } |
| 10701 | #endif |
| 10702 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10703 | |
| 10704 | /* |
| 10705 | * parse "ssl-load-extra-files". |
| 10706 | * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none" |
| 10707 | */ |
| 10708 | static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx, |
| 10709 | struct proxy *defpx, const char *file, int line, |
| 10710 | char **err) |
| 10711 | { |
| 10712 | int i; |
| 10713 | int gf = SSL_GF_NONE; |
| 10714 | |
| 10715 | if (*(args[1]) == 0) |
| 10716 | goto err_arg; |
| 10717 | |
| 10718 | for (i = 1; *args[i]; i++) { |
| 10719 | |
| 10720 | if (!strcmp("bundle", args[i])) { |
| 10721 | gf |= SSL_GF_BUNDLE; |
| 10722 | |
| 10723 | } else if (!strcmp("sctl", args[i])) { |
| 10724 | gf |= SSL_GF_SCTL; |
| 10725 | |
| 10726 | } else if (!strcmp("ocsp", args[i])){ |
| 10727 | gf |= SSL_GF_OCSP; |
| 10728 | |
| 10729 | } else if (!strcmp("issuer", args[i])){ |
| 10730 | gf |= SSL_GF_OCSP_ISSUER; |
| 10731 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10732 | } else if (!strcmp("key", args[i])) { |
| 10733 | gf |= SSL_GF_KEY; |
| 10734 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10735 | } else if (!strcmp("none", args[i])) { |
| 10736 | if (gf != SSL_GF_NONE) |
| 10737 | goto err_alone; |
| 10738 | gf = SSL_GF_NONE; |
| 10739 | i++; |
| 10740 | break; |
| 10741 | |
| 10742 | } else if (!strcmp("all", args[i])) { |
| 10743 | if (gf != SSL_GF_NONE) |
| 10744 | goto err_alone; |
| 10745 | gf = SSL_GF_ALL; |
| 10746 | i++; |
| 10747 | break; |
| 10748 | } else { |
| 10749 | goto err_arg; |
| 10750 | } |
| 10751 | } |
| 10752 | /* break from loop but there are still arguments */ |
| 10753 | if (*args[i]) |
| 10754 | goto err_alone; |
| 10755 | |
| 10756 | global_ssl.extra_files = gf; |
| 10757 | |
| 10758 | return 0; |
| 10759 | |
| 10760 | err_alone: |
| 10761 | memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]); |
| 10762 | return -1; |
| 10763 | |
| 10764 | err_arg: |
| 10765 | memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]); |
| 10766 | return -1; |
| 10767 | } |
| 10768 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10769 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10770 | /* This function is used with TLS ticket keys management. It permits to browse |
| 10771 | * each reference. The variable <getnext> must contain the current node, |
| 10772 | * <end> point to the root node. |
| 10773 | */ |
| 10774 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10775 | static inline |
| 10776 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 10777 | { |
| 10778 | struct tls_keys_ref *ref = getnext; |
| 10779 | |
| 10780 | while (1) { |
| 10781 | |
| 10782 | /* Get next list entry. */ |
| 10783 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 10784 | |
| 10785 | /* If the entry is the last of the list, return NULL. */ |
| 10786 | if (&ref->list == end) |
| 10787 | return NULL; |
| 10788 | |
| 10789 | return ref; |
| 10790 | } |
| 10791 | } |
| 10792 | |
| 10793 | static inline |
| 10794 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 10795 | { |
| 10796 | int id; |
| 10797 | char *error; |
| 10798 | |
| 10799 | /* If the reference starts by a '#', this is numeric id. */ |
| 10800 | if (reference[0] == '#') { |
| 10801 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 10802 | id = strtol(reference + 1, &error, 10); |
| 10803 | if (*error != '\0') |
| 10804 | return NULL; |
| 10805 | |
| 10806 | /* Perform the unique id lookup. */ |
| 10807 | return tlskeys_ref_lookupid(id); |
| 10808 | } |
| 10809 | |
| 10810 | /* Perform the string lookup. */ |
| 10811 | return tlskeys_ref_lookup(reference); |
| 10812 | } |
| 10813 | #endif |
| 10814 | |
| 10815 | |
| 10816 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10817 | |
| 10818 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 10819 | |
| 10820 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 10821 | return cli_io_handler_tlskeys_files(appctx); |
| 10822 | } |
| 10823 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10824 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 10825 | * (next index to be dumped), and cli.p0 (next key reference). |
| 10826 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10827 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 10828 | |
| 10829 | struct stream_interface *si = appctx->owner; |
| 10830 | |
| 10831 | switch (appctx->st2) { |
| 10832 | case STAT_ST_INIT: |
| 10833 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 10834 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10835 | * later and restart at the state "STAT_ST_INIT". |
| 10836 | */ |
| 10837 | chunk_reset(&trash); |
| 10838 | |
| 10839 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 10840 | chunk_appendf(&trash, "# id secret\n"); |
| 10841 | else |
| 10842 | chunk_appendf(&trash, "# id (file)\n"); |
| 10843 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10844 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10845 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10846 | return 0; |
| 10847 | } |
| 10848 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10849 | /* Now, we start the browsing of the references lists. |
| 10850 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 10851 | * available field of this pointer is <list>. It is used with the function |
| 10852 | * tlskeys_list_get_next() for retruning the first available entry |
| 10853 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10854 | if (appctx->ctx.cli.p0 == NULL) { |
| 10855 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 10856 | 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] | 10857 | } |
| 10858 | |
| 10859 | appctx->st2 = STAT_ST_LIST; |
| 10860 | /* fall through */ |
| 10861 | |
| 10862 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10863 | while (appctx->ctx.cli.p0) { |
| 10864 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10865 | |
| 10866 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10867 | 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] | 10868 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10869 | |
| 10870 | if (appctx->ctx.cli.i1 == 0) |
| 10871 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 10872 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10873 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10874 | int head; |
| 10875 | |
| 10876 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 10877 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10878 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10879 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10880 | |
| 10881 | chunk_reset(t2); |
| 10882 | /* 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] | 10883 | if (ref->key_size_bits == 128) { |
| 10884 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10885 | sizeof(struct tls_sess_key_128), |
| 10886 | t2->area, t2->size); |
| 10887 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10888 | t2->area); |
| 10889 | } |
| 10890 | else if (ref->key_size_bits == 256) { |
| 10891 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10892 | sizeof(struct tls_sess_key_256), |
| 10893 | t2->area, t2->size); |
| 10894 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10895 | t2->area); |
| 10896 | } |
| 10897 | else { |
| 10898 | /* This case should never happen */ |
| 10899 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 10900 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10901 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10902 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10903 | /* let's try again later from this stream. We add ourselves into |
| 10904 | * this stream's users so that it can remove us upon termination. |
| 10905 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10906 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10907 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10908 | return 0; |
| 10909 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10910 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10911 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10912 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10913 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10914 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10915 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10916 | /* let's try again later from this stream. We add ourselves into |
| 10917 | * this stream's users so that it can remove us upon termination. |
| 10918 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10919 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10920 | return 0; |
| 10921 | } |
| 10922 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10923 | 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] | 10924 | break; |
| 10925 | |
| 10926 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10927 | 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] | 10928 | } |
| 10929 | |
| 10930 | appctx->st2 = STAT_ST_FIN; |
| 10931 | /* fall through */ |
| 10932 | |
| 10933 | default: |
| 10934 | appctx->st2 = STAT_ST_FIN; |
| 10935 | return 1; |
| 10936 | } |
| 10937 | return 0; |
| 10938 | } |
| 10939 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10940 | /* 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] | 10941 | 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] | 10942 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10943 | /* no parameter, shows only file list */ |
| 10944 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10945 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10946 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10947 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10948 | } |
| 10949 | |
| 10950 | if (args[2][0] == '*') { |
| 10951 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10952 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10953 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10954 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10955 | if (!appctx->ctx.cli.p0) |
| 10956 | 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] | 10957 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10958 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10959 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10960 | } |
| 10961 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10962 | 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] | 10963 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10964 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10965 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10966 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10967 | /* 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] | 10968 | if (!*args[3] || !*args[4]) |
| 10969 | 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] | 10970 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10971 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10972 | if (!ref) |
| 10973 | 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] | 10974 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10975 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10976 | if (ret < 0) |
| 10977 | 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] | 10978 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10979 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10980 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 10981 | 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] | 10982 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10983 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10984 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10985 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10986 | |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 10987 | /* |
| 10988 | * Take an ssl_bind_conf structure and append the configuration line used to |
| 10989 | * create it in the buffer |
| 10990 | */ |
| 10991 | static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf) |
| 10992 | { |
| 10993 | int space = 0; |
| 10994 | |
| 10995 | if (conf == NULL) |
| 10996 | return; |
| 10997 | |
| 10998 | chunk_appendf(buf, " ["); |
| 10999 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 11000 | if (conf->npn_str) { |
| 11001 | int len = conf->npn_len; |
| 11002 | char *ptr = conf->npn_str; |
| 11003 | int comma = 0; |
| 11004 | |
| 11005 | if (space) chunk_appendf(buf, " "); |
| 11006 | chunk_appendf(buf, "npn "); |
| 11007 | while (len) { |
| 11008 | unsigned short size; |
| 11009 | |
| 11010 | size = *ptr; |
| 11011 | ptr++; |
| 11012 | if (comma) |
| 11013 | chunk_memcat(buf, ",", 1); |
| 11014 | chunk_memcat(buf, ptr, size); |
| 11015 | ptr += size; |
| 11016 | len -= size + 1; |
| 11017 | comma = 1; |
| 11018 | } |
| 11019 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 11020 | space++; |
| 11021 | } |
| 11022 | #endif |
| 11023 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 11024 | if (conf->alpn_str) { |
| 11025 | int len = conf->alpn_len; |
| 11026 | char *ptr = conf->alpn_str; |
| 11027 | int comma = 0; |
| 11028 | |
| 11029 | if (space) chunk_appendf(buf, " "); |
| 11030 | chunk_appendf(buf, "alpn "); |
| 11031 | while (len) { |
| 11032 | unsigned short size; |
| 11033 | |
| 11034 | size = *ptr; |
| 11035 | ptr++; |
| 11036 | if (comma) |
| 11037 | chunk_memcat(buf, ",", 1); |
| 11038 | chunk_memcat(buf, ptr, size); |
| 11039 | ptr += size; |
| 11040 | len -= size + 1; |
| 11041 | comma = 1; |
| 11042 | } |
| 11043 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 11044 | space++; |
| 11045 | } |
| 11046 | #endif |
| 11047 | /* verify */ |
| 11048 | { |
| 11049 | if (conf->verify == SSL_SOCK_VERIFY_NONE) { |
| 11050 | if (space) chunk_appendf(buf, " "); |
| 11051 | chunk_appendf(buf, "verify none"); |
| 11052 | space++; |
| 11053 | } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) { |
| 11054 | if (space) chunk_appendf(buf, " "); |
| 11055 | chunk_appendf(buf, "verify optional"); |
| 11056 | space++; |
| 11057 | } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 11058 | if (space) chunk_appendf(buf, " "); |
| 11059 | chunk_appendf(buf, "verify required"); |
| 11060 | space++; |
| 11061 | } |
| 11062 | } |
| 11063 | |
| 11064 | if (conf->no_ca_names) { |
| 11065 | if (space) chunk_appendf(buf, " "); |
| 11066 | chunk_appendf(buf, "no-ca-names"); |
| 11067 | space++; |
| 11068 | } |
| 11069 | |
| 11070 | if (conf->early_data) { |
| 11071 | if (space) chunk_appendf(buf, " "); |
| 11072 | chunk_appendf(buf, "allow-0rtt"); |
| 11073 | space++; |
| 11074 | } |
| 11075 | if (conf->ca_file) { |
| 11076 | if (space) chunk_appendf(buf, " "); |
| 11077 | chunk_appendf(buf, "ca-file %s", conf->ca_file); |
| 11078 | space++; |
| 11079 | } |
| 11080 | if (conf->crl_file) { |
| 11081 | if (space) chunk_appendf(buf, " "); |
| 11082 | chunk_appendf(buf, "crl-file %s", conf->crl_file); |
| 11083 | space++; |
| 11084 | } |
| 11085 | if (conf->ciphers) { |
| 11086 | if (space) chunk_appendf(buf, " "); |
| 11087 | chunk_appendf(buf, "ciphers %s", conf->ciphers); |
| 11088 | space++; |
| 11089 | } |
| 11090 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 11091 | if (conf->ciphersuites) { |
| 11092 | if (space) chunk_appendf(buf, " "); |
| 11093 | chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites); |
| 11094 | space++; |
| 11095 | } |
| 11096 | #endif |
| 11097 | if (conf->curves) { |
| 11098 | if (space) chunk_appendf(buf, " "); |
| 11099 | chunk_appendf(buf, "curves %s", conf->curves); |
| 11100 | space++; |
| 11101 | } |
| 11102 | if (conf->ecdhe) { |
| 11103 | if (space) chunk_appendf(buf, " "); |
| 11104 | chunk_appendf(buf, "ecdhe %s", conf->ecdhe); |
| 11105 | space++; |
| 11106 | } |
| 11107 | |
| 11108 | /* the crt-lists only support ssl-min-ver and ssl-max-ver */ |
| 11109 | /* XXX: this part need to be revamp so we don't dump the default settings */ |
| 11110 | if (conf->ssl_methods.min) { |
| 11111 | if (space) chunk_appendf(buf, " "); |
| 11112 | chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name); |
| 11113 | space++; |
| 11114 | } |
| 11115 | |
| 11116 | if (conf->ssl_methods.max) { |
| 11117 | if (space) chunk_appendf(buf, " "); |
| 11118 | chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name); |
| 11119 | space++; |
| 11120 | } |
| 11121 | |
William Lallemand | 58a5222 | 2020-04-02 18:11:47 +0200 | [diff] [blame] | 11122 | chunk_appendf(buf, "]"); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11123 | |
| 11124 | return; |
| 11125 | } |
| 11126 | |
| 11127 | /* dump a list of filters */ |
| 11128 | static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry) |
| 11129 | { |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11130 | int i; |
| 11131 | |
| 11132 | if (!entry->fcount) |
| 11133 | return; |
| 11134 | |
| 11135 | for (i = 0; i < entry->fcount; i++) { |
William Lallemand | 58a5222 | 2020-04-02 18:11:47 +0200 | [diff] [blame] | 11136 | chunk_appendf(buf, " %s", entry->filters[i]); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11137 | } |
| 11138 | return; |
| 11139 | } |
| 11140 | |
| 11141 | /* CLI IO handler for '(show|dump) ssl crt-list' */ |
| 11142 | static int cli_io_handler_dump_crtlist(struct appctx *appctx) |
| 11143 | { |
| 11144 | struct buffer *trash = alloc_trash_chunk(); |
| 11145 | struct stream_interface *si = appctx->owner; |
| 11146 | struct ebmb_node *lnode; |
| 11147 | |
| 11148 | if (trash == NULL) |
| 11149 | return 1; |
| 11150 | |
| 11151 | /* dump the list of crt-lists */ |
| 11152 | lnode = appctx->ctx.cli.p1; |
| 11153 | if (lnode == NULL) |
| 11154 | lnode = ebmb_first(&crtlists_tree); |
| 11155 | while (lnode) { |
| 11156 | chunk_appendf(trash, "%s\n", lnode->key); |
| 11157 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11158 | si_rx_room_blk(si); |
| 11159 | goto yield; |
| 11160 | } |
| 11161 | lnode = ebmb_next(lnode); |
| 11162 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11163 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11164 | return 1; |
| 11165 | yield: |
| 11166 | appctx->ctx.cli.p1 = lnode; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11167 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11168 | return 0; |
| 11169 | } |
| 11170 | |
| 11171 | /* CLI IO handler for '(show|dump) ssl crt-list <filename>' */ |
| 11172 | static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx) |
| 11173 | { |
| 11174 | struct buffer *trash = alloc_trash_chunk(); |
| 11175 | struct crtlist *crtlist; |
| 11176 | struct stream_interface *si = appctx->owner; |
| 11177 | struct crtlist_entry *entry; |
| 11178 | |
| 11179 | if (trash == NULL) |
| 11180 | return 1; |
| 11181 | |
| 11182 | crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node); |
| 11183 | |
| 11184 | entry = appctx->ctx.cli.p1; |
| 11185 | if (entry == NULL) { |
| 11186 | entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist); |
| 11187 | chunk_appendf(trash, "# %s\n", crtlist->node.key); |
| 11188 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11189 | si_rx_room_blk(si); |
| 11190 | goto yield; |
| 11191 | } |
| 11192 | } |
| 11193 | |
| 11194 | list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) { |
| 11195 | struct ckch_store *store; |
| 11196 | const char *filename; |
| 11197 | |
| 11198 | store = entry->node.key; |
| 11199 | filename = store->path; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11200 | chunk_appendf(trash, "%s", filename); |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11201 | if (appctx->ctx.cli.i0 == 's') /* show */ |
| 11202 | chunk_appendf(trash, ":%d", entry->linenum); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11203 | dump_crtlist_sslconf(trash, entry->ssl_conf); |
| 11204 | dump_crtlist_filters(trash, entry); |
| 11205 | chunk_appendf(trash, "\n"); |
| 11206 | |
| 11207 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11208 | si_rx_room_blk(si); |
| 11209 | goto yield; |
| 11210 | } |
| 11211 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11212 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11213 | return 1; |
| 11214 | yield: |
| 11215 | appctx->ctx.cli.p1 = entry; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11216 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11217 | return 0; |
| 11218 | } |
| 11219 | |
| 11220 | /* CLI argument parser for '(show|dump) ssl crt-list' */ |
| 11221 | static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11222 | { |
| 11223 | struct ebmb_node *lnode; |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11224 | char *filename = NULL; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11225 | int mode; |
| 11226 | |
| 11227 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11228 | return 1; |
| 11229 | |
| 11230 | appctx->ctx.cli.p0 = NULL; |
| 11231 | appctx->ctx.cli.p1 = NULL; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11232 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11233 | if (*args[3] && !strcmp(args[3], "-n")) { |
| 11234 | mode = 's'; |
| 11235 | filename = args[4]; |
| 11236 | } else { |
| 11237 | mode = 'd'; |
| 11238 | filename = args[3]; |
| 11239 | } |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11240 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11241 | if (mode == 's' && !*args[4]) |
| 11242 | return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n"); |
| 11243 | |
| 11244 | if (filename && *filename) { |
| 11245 | lnode = ebst_lookup(&crtlists_tree, filename); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11246 | if (lnode == NULL) |
| 11247 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 11248 | |
| 11249 | appctx->ctx.cli.p0 = lnode; |
| 11250 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 11251 | } |
| 11252 | appctx->ctx.cli.i0 = mode; |
| 11253 | |
| 11254 | return 0; |
| 11255 | } |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11256 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11257 | /* release function of the "add ssl crt-list' command, free things and unlock |
| 11258 | the spinlock */ |
| 11259 | static void cli_release_add_crtlist(struct appctx *appctx) |
| 11260 | { |
| 11261 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 11262 | |
| 11263 | if (appctx->st2 != SETCERT_ST_FIN) { |
| 11264 | struct ckch_inst *inst, *inst_s; |
| 11265 | /* upon error free the ckch_inst and everything inside */ |
| 11266 | ebpt_delete(&entry->node); |
| 11267 | LIST_DEL(&entry->by_crtlist); |
| 11268 | LIST_DEL(&entry->by_ckch_store); |
| 11269 | |
| 11270 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) { |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 11271 | ckch_inst_free(inst); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11272 | } |
| 11273 | crtlist_free_filters(entry->filters); |
| 11274 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 11275 | free(entry->ssl_conf); |
| 11276 | free(entry); |
| 11277 | } |
| 11278 | |
| 11279 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11280 | } |
| 11281 | |
| 11282 | |
| 11283 | /* IO Handler for the "add ssl crt-list" command It adds a new entry in the |
| 11284 | * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list |
| 11285 | * |
| 11286 | * The logic is the same as the "commit ssl cert" command but without the |
| 11287 | * freeing of the old structures, because there are none. |
| 11288 | */ |
| 11289 | static int cli_io_handler_add_crtlist(struct appctx *appctx) |
| 11290 | { |
| 11291 | struct bind_conf_list *bind_conf_node; |
| 11292 | struct stream_interface *si = appctx->owner; |
| 11293 | struct crtlist *crtlist = appctx->ctx.cli.p0; |
| 11294 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 11295 | struct ckch_store *store = entry->node.key; |
| 11296 | struct buffer *trash = alloc_trash_chunk(); |
| 11297 | struct ckch_inst *new_inst; |
| 11298 | char *err = NULL; |
| 11299 | int i = 0; |
| 11300 | int errcode = 0; |
| 11301 | |
| 11302 | if (trash == NULL) |
| 11303 | goto error; |
| 11304 | |
| 11305 | /* for each bind_conf which use the crt-list, a new ckch_inst must be |
| 11306 | * created. |
| 11307 | */ |
| 11308 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 11309 | goto error; |
| 11310 | |
| 11311 | while (1) { |
| 11312 | switch (appctx->st2) { |
| 11313 | case SETCERT_ST_INIT: |
| 11314 | /* This state just print the update message */ |
| 11315 | chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key); |
| 11316 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11317 | si_rx_room_blk(si); |
| 11318 | goto yield; |
| 11319 | } |
| 11320 | appctx->st2 = SETCERT_ST_GEN; |
| 11321 | /* fallthrough */ |
| 11322 | case SETCERT_ST_GEN: |
| 11323 | bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */ |
| 11324 | if (bind_conf_node == NULL) |
| 11325 | bind_conf_node = crtlist->bind_conf; |
| 11326 | for (; bind_conf_node; bind_conf_node = bind_conf_node->next) { |
| 11327 | struct bind_conf *bind_conf = bind_conf_node->bind_conf; |
| 11328 | struct sni_ctx *sni; |
| 11329 | |
| 11330 | /* yield every 10 generations */ |
| 11331 | if (i > 10) { |
| 11332 | appctx->ctx.cli.p2 = bind_conf_node; |
| 11333 | goto yield; |
| 11334 | } |
| 11335 | |
| 11336 | /* we don't support multi-cert bundles, only simple ones */ |
| 11337 | errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err); |
| 11338 | if (errcode & ERR_CODE) |
| 11339 | goto error; |
| 11340 | |
| 11341 | /* we need to initialize the SSL_CTX generated */ |
| 11342 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 11343 | list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) { |
| 11344 | if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 11345 | errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err); |
| 11346 | if (errcode & ERR_CODE) |
| 11347 | goto error; |
| 11348 | } |
| 11349 | } |
| 11350 | /* display one dot for each new instance */ |
| 11351 | chunk_appendf(trash, "."); |
| 11352 | i++; |
| 11353 | LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs); |
| 11354 | } |
| 11355 | appctx->st2 = SETCERT_ST_INSERT; |
| 11356 | /* fallthrough */ |
| 11357 | case SETCERT_ST_INSERT: |
| 11358 | /* insert SNIs in bind_conf */ |
| 11359 | list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) { |
| 11360 | HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 11361 | ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf); |
| 11362 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 11363 | } |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11364 | entry->linenum = ++crtlist->linecount; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11365 | appctx->st2 = SETCERT_ST_FIN; |
| 11366 | goto end; |
| 11367 | } |
| 11368 | } |
| 11369 | |
| 11370 | end: |
| 11371 | chunk_appendf(trash, "\n"); |
| 11372 | if (errcode & ERR_WARN) |
| 11373 | chunk_appendf(trash, "%s", err); |
| 11374 | chunk_appendf(trash, "Success!\n"); |
| 11375 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11376 | si_rx_room_blk(si); |
| 11377 | free_trash_chunk(trash); |
| 11378 | /* success: call the release function and don't come back */ |
| 11379 | return 1; |
| 11380 | yield: |
| 11381 | /* store the state */ |
| 11382 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11383 | si_rx_room_blk(si); |
| 11384 | free_trash_chunk(trash); |
| 11385 | si_rx_endp_more(si); /* let's come back later */ |
| 11386 | return 0; /* should come back */ |
| 11387 | |
| 11388 | error: |
| 11389 | /* spin unlock and free are done in the release function */ |
| 11390 | if (trash) { |
| 11391 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 11392 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11393 | si_rx_room_blk(si); |
| 11394 | free_trash_chunk(trash); |
| 11395 | } |
| 11396 | /* error: call the release function and don't come back */ |
| 11397 | return 1; |
| 11398 | } |
| 11399 | |
| 11400 | |
| 11401 | /* |
| 11402 | * Parse a "add ssl crt-list <crt-list> <certfile>" line. |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11403 | * Filters and option must be passed through payload: |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11404 | */ |
| 11405 | static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11406 | { |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11407 | int cfgerr = 0; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11408 | struct ckch_store *store; |
| 11409 | char *err = NULL; |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11410 | char path[MAXPATHLEN+1]; |
| 11411 | char *crtlist_path; |
| 11412 | char *cert_path = NULL; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11413 | struct ebmb_node *eb; |
| 11414 | struct ebpt_node *inserted; |
| 11415 | struct crtlist *crtlist; |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11416 | struct crtlist_entry *entry = NULL; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11417 | |
| 11418 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11419 | return 1; |
| 11420 | |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11421 | if (!*args[3] || (!payload && !*args[4])) |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11422 | return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n"); |
| 11423 | |
| 11424 | crtlist_path = args[3]; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11425 | |
| 11426 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11427 | return cli_err(appctx, "Operations on certificates are currently locked!\n"); |
| 11428 | |
| 11429 | eb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 11430 | if (!eb) { |
| 11431 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 11432 | goto error; |
| 11433 | } |
| 11434 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 11435 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 11436 | entry = crtlist_entry_new(); |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11437 | if (entry == NULL) { |
| 11438 | memprintf(&err, "Not enough memory!"); |
| 11439 | goto error; |
| 11440 | } |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11441 | |
| 11442 | if (payload) { |
| 11443 | char *lf; |
| 11444 | |
| 11445 | lf = strrchr(payload, '\n'); |
| 11446 | if (lf) { |
| 11447 | memprintf(&err, "only one line of payload is supported!"); |
| 11448 | goto error; |
| 11449 | } |
| 11450 | /* cert_path is filled here */ |
| 11451 | cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err); |
| 11452 | if (cfgerr & ERR_CODE) |
| 11453 | goto error; |
| 11454 | } else { |
| 11455 | cert_path = args[4]; |
| 11456 | } |
| 11457 | |
| 11458 | if (!cert_path) { |
| 11459 | memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload"); |
| 11460 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 11461 | goto error; |
| 11462 | } |
| 11463 | |
William Lallemand | 916d0b5 | 2020-04-21 18:29:12 +0200 | [diff] [blame] | 11464 | if (eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 11465 | char *slash; |
| 11466 | |
| 11467 | slash = strrchr(cert_path, '/'); |
| 11468 | if (!slash) { |
| 11469 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 11470 | goto error; |
| 11471 | } |
| 11472 | /* temporary replace / by 0 to do an strcmp */ |
| 11473 | *slash = '\0'; |
| 11474 | if (strcmp(cert_path, (char*)crtlist->node.key) != 0) { |
| 11475 | *slash = '/'; |
| 11476 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 11477 | goto error; |
| 11478 | } |
| 11479 | *slash = '/'; |
| 11480 | } |
| 11481 | |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11482 | if (*cert_path != '/' && global_ssl.crt_base) { |
| 11483 | if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) { |
| 11484 | memprintf(&err, "'%s' : path too long", cert_path); |
| 11485 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 11486 | goto error; |
| 11487 | } |
| 11488 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path); |
| 11489 | cert_path = path; |
| 11490 | } |
| 11491 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11492 | store = ckchs_lookup(cert_path); |
| 11493 | if (store == NULL) { |
| 11494 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 11495 | goto error; |
| 11496 | } |
William Lallemand | 36ccc39 | 2020-04-08 10:57:24 +0200 | [diff] [blame] | 11497 | if (store->multi) { |
| 11498 | memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path); |
| 11499 | goto error; |
| 11500 | } |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11501 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 11502 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 11503 | goto error; |
| 11504 | } |
| 11505 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11506 | /* check if it's possible to insert this new crtlist_entry */ |
| 11507 | entry->node.key = store; |
| 11508 | inserted = ebpt_insert(&crtlist->entries, &entry->node); |
| 11509 | if (inserted != &entry->node) { |
| 11510 | memprintf(&err, "file already exists in this directory!"); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11511 | goto error; |
| 11512 | } |
| 11513 | |
William Lallemand | b74d564 | 2020-04-21 16:54:19 +0200 | [diff] [blame] | 11514 | /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */ |
| 11515 | if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 11516 | memprintf(&err, "this is a directory, SSL configuration and filters are not allowed"); |
| 11517 | goto error; |
| 11518 | } |
| 11519 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11520 | LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11521 | entry->crtlist = crtlist; |
| 11522 | LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store); |
| 11523 | |
| 11524 | appctx->st2 = SETCERT_ST_INIT; |
| 11525 | appctx->ctx.cli.p0 = crtlist; |
| 11526 | appctx->ctx.cli.p1 = entry; |
| 11527 | |
| 11528 | /* unlock is done in the release handler */ |
| 11529 | return 0; |
| 11530 | |
| 11531 | error: |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 11532 | crtlist_entry_free(entry); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11533 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11534 | err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : ""); |
| 11535 | return cli_dynerr(appctx, err); |
| 11536 | } |
| 11537 | |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11538 | /* Parse a "del ssl crt-list <crt-list> <certfile>" line. */ |
| 11539 | static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11540 | { |
| 11541 | struct ckch_store *store; |
| 11542 | char *err = NULL; |
| 11543 | char *crtlist_path, *cert_path; |
| 11544 | struct ebmb_node *ebmb; |
| 11545 | struct ebpt_node *ebpt; |
| 11546 | struct crtlist *crtlist; |
| 11547 | struct crtlist_entry *entry = NULL; |
| 11548 | struct ckch_inst *inst, *inst_s; |
| 11549 | int linenum = 0; |
| 11550 | char *colons; |
| 11551 | |
| 11552 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11553 | return 1; |
| 11554 | |
| 11555 | if (!*args[3] || !*args[4]) |
| 11556 | return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n"); |
| 11557 | |
William Lallemand | 463b524 | 2020-04-08 10:30:44 +0200 | [diff] [blame] | 11558 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11559 | return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n"); |
| 11560 | |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11561 | crtlist_path = args[3]; |
| 11562 | cert_path = args[4]; |
| 11563 | |
| 11564 | colons = strchr(cert_path, ':'); |
| 11565 | if (colons) { |
| 11566 | char *endptr; |
| 11567 | |
| 11568 | linenum = strtol(colons + 1, &endptr, 10); |
| 11569 | if (colons + 1 == endptr || *endptr != '\0') { |
| 11570 | memprintf(&err, "wrong line number after colons in '%s'!", cert_path); |
| 11571 | goto error; |
| 11572 | } |
| 11573 | *colons = '\0'; |
| 11574 | } |
| 11575 | /* look for crtlist */ |
| 11576 | ebmb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 11577 | if (!ebmb) { |
| 11578 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 11579 | goto error; |
| 11580 | } |
| 11581 | crtlist = ebmb_entry(ebmb, struct crtlist, node); |
| 11582 | |
| 11583 | /* look for store */ |
| 11584 | store = ckchs_lookup(cert_path); |
| 11585 | if (store == NULL) { |
| 11586 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 11587 | goto error; |
| 11588 | } |
William Lallemand | 36ccc39 | 2020-04-08 10:57:24 +0200 | [diff] [blame] | 11589 | if (store->multi) { |
| 11590 | memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path); |
| 11591 | goto error; |
| 11592 | } |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11593 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 11594 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 11595 | goto error; |
| 11596 | } |
| 11597 | |
| 11598 | ebpt = ebpt_lookup(&crtlist->entries, store); |
| 11599 | if (!ebpt) { |
| 11600 | memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path); |
| 11601 | goto error; |
| 11602 | } |
| 11603 | |
| 11604 | /* list the line number of entries for errors in err, and select the right ebpt */ |
| 11605 | for (; ebpt; ebpt = ebpt_next_dup(ebpt)) { |
| 11606 | struct crtlist_entry *tmp; |
| 11607 | |
| 11608 | tmp = ebpt_entry(ebpt, struct crtlist_entry, node); |
| 11609 | memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum); |
| 11610 | |
| 11611 | /* select the entry we wanted */ |
| 11612 | if (linenum == 0 || tmp->linenum == linenum) { |
| 11613 | if (!entry) |
| 11614 | entry = tmp; |
| 11615 | } |
| 11616 | } |
| 11617 | |
| 11618 | /* we didn't found the specified entry */ |
| 11619 | if (!entry) { |
| 11620 | memprintf(&err, "found a certificate '%s' but the line number is incorrect, please specify a correct line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 11621 | goto error; |
| 11622 | } |
| 11623 | |
| 11624 | /* we didn't specified a line number but there were several entries */ |
| 11625 | if (linenum == 0 && ebpt_next_dup(&entry->node)) { |
| 11626 | memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 11627 | goto error; |
| 11628 | } |
| 11629 | |
| 11630 | /* upon error free the ckch_inst and everything inside */ |
| 11631 | |
| 11632 | ebpt_delete(&entry->node); |
| 11633 | LIST_DEL(&entry->by_crtlist); |
| 11634 | LIST_DEL(&entry->by_ckch_store); |
| 11635 | |
| 11636 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 11637 | struct sni_ctx *sni, *sni_s; |
| 11638 | |
| 11639 | HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 11640 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 11641 | ebmb_delete(&sni->name); |
| 11642 | LIST_DEL(&sni->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 11643 | SSL_CTX_free(sni->ctx); |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11644 | free(sni); |
| 11645 | } |
| 11646 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 11647 | LIST_DEL(&inst->by_ckchs); |
| 11648 | free(inst); |
| 11649 | } |
| 11650 | |
| 11651 | crtlist_free_filters(entry->filters); |
| 11652 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 11653 | free(entry->ssl_conf); |
| 11654 | free(entry); |
| 11655 | |
| 11656 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11657 | err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path); |
| 11658 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 11659 | |
| 11660 | error: |
| 11661 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11662 | err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : ""); |
| 11663 | return cli_dynerr(appctx, err); |
| 11664 | } |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11665 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11666 | /* Type of SSL payloads that can be updated over the CLI */ |
| 11667 | |
| 11668 | enum { |
| 11669 | CERT_TYPE_PEM = 0, |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11670 | CERT_TYPE_KEY, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11671 | #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] | 11672 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11673 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11674 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11675 | #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] | 11676 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11677 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11678 | CERT_TYPE_MAX, |
| 11679 | }; |
| 11680 | |
| 11681 | struct { |
| 11682 | const char *ext; |
| 11683 | int type; |
| 11684 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 11685 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11686 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11687 | [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] | 11688 | [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] | 11689 | #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] | 11690 | [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] | 11691 | #endif |
| 11692 | #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] | 11693 | [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] | 11694 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11695 | [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] | 11696 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11697 | }; |
| 11698 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11699 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11700 | /* release function of the `show ssl cert' command */ |
| 11701 | static void cli_release_show_cert(struct appctx *appctx) |
| 11702 | { |
| 11703 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11704 | } |
| 11705 | |
| 11706 | /* IO handler of "show ssl cert <filename>" */ |
| 11707 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 11708 | { |
| 11709 | struct buffer *trash = alloc_trash_chunk(); |
| 11710 | struct ebmb_node *node; |
| 11711 | struct stream_interface *si = appctx->owner; |
| 11712 | struct ckch_store *ckchs; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11713 | |
| 11714 | if (trash == NULL) |
| 11715 | return 1; |
| 11716 | |
| 11717 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11718 | if (ckchs_transaction.old_ckchs) { |
| 11719 | ckchs = ckchs_transaction.old_ckchs; |
| 11720 | chunk_appendf(trash, "# transaction\n"); |
| 11721 | if (!ckchs->multi) { |
| 11722 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11723 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11724 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11725 | int n; |
| 11726 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11727 | chunk_appendf(trash, "*%s:", ckchs->path); |
| 11728 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11729 | if (ckchs->ckch[n].cert) |
| 11730 | chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11731 | } |
| 11732 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11733 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11734 | } |
| 11735 | } |
| 11736 | } |
| 11737 | |
| 11738 | if (!appctx->ctx.cli.p0) { |
| 11739 | chunk_appendf(trash, "# filename\n"); |
| 11740 | node = ebmb_first(&ckchs_tree); |
| 11741 | } else { |
| 11742 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 11743 | } |
| 11744 | while (node) { |
| 11745 | ckchs = ebmb_entry(node, struct ckch_store, node); |
| 11746 | if (!ckchs->multi) { |
| 11747 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11748 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11749 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11750 | int n; |
| 11751 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11752 | chunk_appendf(trash, "%s:", ckchs->path); |
| 11753 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11754 | if (ckchs->ckch[n].cert) |
| 11755 | chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11756 | } |
| 11757 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11758 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11759 | } |
| 11760 | |
| 11761 | node = ebmb_next(node); |
| 11762 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11763 | si_rx_room_blk(si); |
| 11764 | goto yield; |
| 11765 | } |
| 11766 | } |
| 11767 | |
| 11768 | appctx->ctx.cli.p0 = NULL; |
| 11769 | free_trash_chunk(trash); |
| 11770 | return 1; |
| 11771 | yield: |
| 11772 | |
| 11773 | free_trash_chunk(trash); |
| 11774 | appctx->ctx.cli.p0 = ckchs; |
| 11775 | return 0; /* should come back */ |
| 11776 | } |
| 11777 | |
| 11778 | /* IO handler of the details "show ssl cert <filename>" */ |
| 11779 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 11780 | { |
| 11781 | struct stream_interface *si = appctx->owner; |
| 11782 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 11783 | struct buffer *out = alloc_trash_chunk(); |
| 11784 | struct buffer *tmp = alloc_trash_chunk(); |
| 11785 | X509_NAME *name = NULL; |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11786 | STACK_OF(X509) *chain; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11787 | unsigned int len = 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11788 | int write = -1; |
| 11789 | BIO *bio = NULL; |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11790 | int i; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11791 | |
| 11792 | if (!tmp || !out) |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11793 | goto end_no_putchk; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11794 | |
| 11795 | if (!ckchs->multi) { |
| 11796 | chunk_appendf(out, "Filename: "); |
| 11797 | if (ckchs == ckchs_transaction.new_ckchs) |
| 11798 | chunk_appendf(out, "*"); |
| 11799 | chunk_appendf(out, "%s\n", ckchs->path); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11800 | |
William Lallemand | 59c16fc | 2020-03-19 20:26:02 +0100 | [diff] [blame] | 11801 | chunk_appendf(out, "Status: "); |
| 11802 | if (ckchs->ckch->cert == NULL) |
| 11803 | chunk_appendf(out, "Empty\n"); |
| 11804 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 11805 | chunk_appendf(out, "Unused\n"); |
| 11806 | else |
| 11807 | chunk_appendf(out, "Used\n"); |
| 11808 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11809 | if (ckchs->ckch->cert == NULL) |
| 11810 | goto end; |
| 11811 | |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11812 | chain = ckchs->ckch->chain; |
| 11813 | if (chain == NULL) { |
| 11814 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 11815 | issuer = ssl_get0_issuer_chain(ckchs->ckch->cert); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11816 | if (issuer) { |
| 11817 | chain = issuer->chain; |
| 11818 | chunk_appendf(out, "Chain Filename: "); |
| 11819 | chunk_appendf(out, "%s\n", issuer->path); |
| 11820 | } |
| 11821 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11822 | chunk_appendf(out, "Serial: "); |
| 11823 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 11824 | goto end; |
| 11825 | dump_binary(out, tmp->area, tmp->data); |
| 11826 | chunk_appendf(out, "\n"); |
| 11827 | |
| 11828 | chunk_appendf(out, "notBefore: "); |
| 11829 | chunk_reset(tmp); |
| 11830 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11831 | goto end; |
| 11832 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 11833 | goto end; |
| 11834 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 11835 | tmp->area[write] = '\0'; |
| 11836 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11837 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11838 | chunk_appendf(out, "%s\n", tmp->area); |
| 11839 | |
| 11840 | chunk_appendf(out, "notAfter: "); |
| 11841 | chunk_reset(tmp); |
| 11842 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11843 | goto end; |
| 11844 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 11845 | goto end; |
| 11846 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 11847 | goto end; |
| 11848 | tmp->area[write] = '\0'; |
| 11849 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11850 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11851 | chunk_appendf(out, "%s\n", tmp->area); |
| 11852 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11853 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11854 | chunk_appendf(out, "Subject Alternative Name: "); |
| 11855 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 11856 | goto end; |
| 11857 | *(out->area + out->data) = '\0'; |
| 11858 | chunk_appendf(out, "\n"); |
| 11859 | #endif |
| 11860 | chunk_reset(tmp); |
| 11861 | chunk_appendf(out, "Algorithm: "); |
| 11862 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 11863 | goto end; |
| 11864 | chunk_appendf(out, "%s\n", tmp->area); |
| 11865 | |
| 11866 | chunk_reset(tmp); |
| 11867 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11868 | 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] | 11869 | goto end; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11870 | tmp->data = len; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11871 | dump_binary(out, tmp->area, tmp->data); |
| 11872 | chunk_appendf(out, "\n"); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11873 | |
William Lallemand | a90e593 | 2020-02-25 14:07:58 +0100 | [diff] [blame] | 11874 | chunk_appendf(out, "Subject: "); |
| 11875 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 11876 | goto end; |
| 11877 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11878 | goto end; |
| 11879 | *(tmp->area + tmp->data) = '\0'; |
| 11880 | chunk_appendf(out, "%s\n", tmp->area); |
| 11881 | |
| 11882 | chunk_appendf(out, "Issuer: "); |
| 11883 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 11884 | goto end; |
| 11885 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11886 | goto end; |
| 11887 | *(tmp->area + tmp->data) = '\0'; |
| 11888 | chunk_appendf(out, "%s\n", tmp->area); |
| 11889 | |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11890 | /* Displays subject of each certificate in the chain */ |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11891 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 11892 | X509 *ca = sk_X509_value(chain, i); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11893 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11894 | chunk_appendf(out, "Chain Subject: "); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11895 | if ((name = X509_get_subject_name(ca)) == NULL) |
| 11896 | goto end; |
| 11897 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11898 | goto end; |
| 11899 | *(tmp->area + tmp->data) = '\0'; |
| 11900 | chunk_appendf(out, "%s\n", tmp->area); |
| 11901 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11902 | chunk_appendf(out, "Chain Issuer: "); |
| 11903 | if ((name = X509_get_issuer_name(ca)) == NULL) |
| 11904 | goto end; |
| 11905 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11906 | goto end; |
| 11907 | *(tmp->area + tmp->data) = '\0'; |
| 11908 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11909 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11910 | } |
| 11911 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11912 | end: |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11913 | if (ci_putchk(si_ic(si), out) == -1) { |
| 11914 | si_rx_room_blk(si); |
| 11915 | goto yield; |
| 11916 | } |
| 11917 | |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11918 | end_no_putchk: |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11919 | if (bio) |
| 11920 | BIO_free(bio); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11921 | free_trash_chunk(tmp); |
| 11922 | free_trash_chunk(out); |
| 11923 | return 1; |
| 11924 | yield: |
| 11925 | free_trash_chunk(tmp); |
| 11926 | free_trash_chunk(out); |
| 11927 | return 0; /* should come back */ |
| 11928 | } |
| 11929 | |
| 11930 | /* parsing function for 'show ssl cert [certfile]' */ |
| 11931 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11932 | { |
| 11933 | struct ckch_store *ckchs; |
| 11934 | |
| 11935 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 11936 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 11937 | |
| 11938 | /* The operations on the CKCH architecture are locked so we can |
| 11939 | * manipulate ckch_store and ckch_inst */ |
| 11940 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11941 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 11942 | |
| 11943 | /* check if there is a certificate to lookup */ |
| 11944 | if (*args[3]) { |
| 11945 | if (*args[3] == '*') { |
| 11946 | if (!ckchs_transaction.new_ckchs) |
| 11947 | goto error; |
| 11948 | |
| 11949 | ckchs = ckchs_transaction.new_ckchs; |
| 11950 | |
| 11951 | if (strcmp(args[3] + 1, ckchs->path)) |
| 11952 | goto error; |
| 11953 | |
| 11954 | } else { |
| 11955 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 11956 | goto error; |
| 11957 | |
| 11958 | } |
| 11959 | |
| 11960 | if (ckchs->multi) |
| 11961 | goto error; |
| 11962 | |
| 11963 | appctx->ctx.cli.p0 = ckchs; |
| 11964 | /* use the IO handler that shows details */ |
| 11965 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 11966 | } |
| 11967 | |
| 11968 | return 0; |
| 11969 | |
| 11970 | error: |
| 11971 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11972 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 11973 | } |
| 11974 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11975 | /* 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] | 11976 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11977 | { |
| 11978 | struct ckch_store *new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11979 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11980 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11981 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11982 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11983 | /* 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] | 11984 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11985 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11986 | /* if the allocation failed, we need to free everything from the temporary list */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 11987 | ckch_store_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11988 | } |
| 11989 | } |
| 11990 | |
| 11991 | |
| 11992 | /* |
| 11993 | * This function tries to create the new ckch_inst and their SNIs |
| 11994 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11995 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11996 | { |
| 11997 | struct stream_interface *si = appctx->owner; |
| 11998 | int y = 0; |
| 11999 | char *err = NULL; |
| 12000 | int errcode = 0; |
| 12001 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 12002 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12003 | struct buffer *trash = alloc_trash_chunk(); |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 12004 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 90afe90 | 2020-03-30 19:29:45 +0200 | [diff] [blame] | 12005 | struct crtlist_entry *entry; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12006 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 12007 | if (trash == NULL) |
| 12008 | goto error; |
| 12009 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12010 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12011 | goto error; |
| 12012 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12013 | while (1) { |
| 12014 | switch (appctx->st2) { |
| 12015 | case SETCERT_ST_INIT: |
| 12016 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12017 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12018 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 12019 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12020 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12021 | } |
| 12022 | appctx->st2 = SETCERT_ST_GEN; |
| 12023 | /* fallthrough */ |
| 12024 | case SETCERT_ST_GEN: |
| 12025 | /* |
| 12026 | * This state generates the ckch instances with their |
| 12027 | * sni_ctxs and SSL_CTX. |
| 12028 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12029 | * Since the SSL_CTX generation can be CPU consumer, we |
| 12030 | * yield every 10 instances. |
| 12031 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12032 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12033 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12034 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12035 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12036 | if (!new_ckchs) |
| 12037 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12038 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12039 | /* get the next ckchi to regenerate */ |
| 12040 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 12041 | /* we didn't start yet, set it to the first elem */ |
| 12042 | if (ckchi == NULL) |
| 12043 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12044 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12045 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 12046 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 12047 | struct ckch_inst *new_inst; |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 12048 | char **sni_filter = NULL; |
| 12049 | int fcount = 0; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12050 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12051 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 12052 | if (y >= 10) { |
| 12053 | /* save the next ckchi to compute */ |
| 12054 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 12055 | goto yield; |
| 12056 | } |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 12057 | |
| 12058 | if (ckchi->crtlist_entry) { |
| 12059 | sni_filter = ckchi->crtlist_entry->filters; |
| 12060 | fcount = ckchi->crtlist_entry->fcount; |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 12061 | } |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 12062 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12063 | if (new_ckchs->multi) |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 12064 | 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] | 12065 | else |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 12066 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
| 12067 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12068 | if (errcode & ERR_CODE) |
| 12069 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12070 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 12071 | /* if the previous ckchi was used as the default */ |
| 12072 | if (ckchi->is_default) |
| 12073 | new_inst->is_default = 1; |
| 12074 | |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 12075 | /* we need to initialize the SSL_CTX generated */ |
William Lallemand | 696f317 | 2020-02-07 20:45:24 +0100 | [diff] [blame] | 12076 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 12077 | 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] | 12078 | 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] | 12079 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 12080 | if (errcode & ERR_CODE) |
| 12081 | goto error; |
| 12082 | } |
| 12083 | } |
| 12084 | |
| 12085 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12086 | /* display one dot per new instance */ |
| 12087 | chunk_appendf(trash, "."); |
| 12088 | /* link the new ckch_inst to the duplicate */ |
| 12089 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12090 | y++; |
| 12091 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12092 | appctx->st2 = SETCERT_ST_INSERT; |
| 12093 | /* fallthrough */ |
| 12094 | case SETCERT_ST_INSERT: |
| 12095 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12096 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12097 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12098 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12099 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12100 | if (!new_ckchs) |
| 12101 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12102 | |
William Lallemand | 90afe90 | 2020-03-30 19:29:45 +0200 | [diff] [blame] | 12103 | /* get the list of crtlist_entry in the old store, and update the pointers to the store */ |
| 12104 | LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry); |
| 12105 | list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) { |
| 12106 | ebpt_delete(&entry->node); |
| 12107 | /* change the ptr and reinsert the node */ |
| 12108 | entry->node.key = new_ckchs; |
| 12109 | ebpt_insert(&entry->crtlist->entries, &entry->node); |
| 12110 | } |
| 12111 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 12112 | /* 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] | 12113 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 12114 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 12115 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 12116 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 12117 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12118 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12119 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 12120 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
Willy Tarreau | a6cd078 | 2020-05-01 11:38:39 +0200 | [diff] [blame] | 12121 | struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf; |
William Lallemand | d5e9377 | 2020-04-09 17:12:16 +0200 | [diff] [blame] | 12122 | |
| 12123 | HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock); |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 12124 | ckch_inst_free(ckchi); |
William Lallemand | d5e9377 | 2020-04-09 17:12:16 +0200 | [diff] [blame] | 12125 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock); |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12126 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12127 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12128 | /* Replace the old ckchs by the new one */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12129 | ckch_store_free(old_ckchs); |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12130 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12131 | appctx->st2 = SETCERT_ST_FIN; |
| 12132 | /* fallthrough */ |
| 12133 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12134 | /* we achieved the transaction, we can set everything to NULL */ |
| 12135 | free(ckchs_transaction.path); |
| 12136 | ckchs_transaction.path = NULL; |
| 12137 | ckchs_transaction.new_ckchs = NULL; |
| 12138 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12139 | goto end; |
| 12140 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12141 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12142 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12143 | |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 12144 | chunk_appendf(trash, "\n"); |
| 12145 | if (errcode & ERR_WARN) |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 12146 | chunk_appendf(trash, "%s", err); |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 12147 | chunk_appendf(trash, "Success!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12148 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12149 | si_rx_room_blk(si); |
| 12150 | free_trash_chunk(trash); |
| 12151 | /* success: call the release function and don't come back */ |
| 12152 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12153 | yield: |
| 12154 | /* store the state */ |
| 12155 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12156 | si_rx_room_blk(si); |
| 12157 | free_trash_chunk(trash); |
| 12158 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12159 | return 0; /* should come back */ |
| 12160 | |
| 12161 | error: |
| 12162 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 12163 | if (trash) { |
| 12164 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 12165 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12166 | si_rx_room_blk(si); |
| 12167 | free_trash_chunk(trash); |
| 12168 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12169 | /* error: call the release function and don't come back */ |
| 12170 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12171 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12172 | |
| 12173 | /* |
| 12174 | * Parsing function of 'commit ssl cert' |
| 12175 | */ |
| 12176 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12177 | { |
| 12178 | char *err = NULL; |
| 12179 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12180 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12181 | return 1; |
| 12182 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12183 | if (!*args[3]) |
| 12184 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 12185 | |
| 12186 | /* The operations on the CKCH architecture are locked so we can |
| 12187 | * manipulate ckch_store and ckch_inst */ |
| 12188 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12189 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 12190 | |
| 12191 | if (!ckchs_transaction.path) { |
| 12192 | memprintf(&err, "No ongoing transaction! !\n"); |
| 12193 | goto error; |
| 12194 | } |
| 12195 | |
| 12196 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 12197 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 12198 | goto error; |
| 12199 | } |
| 12200 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 12201 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 12202 | if (ckchs_transaction.new_ckchs->multi) { |
| 12203 | int n; |
| 12204 | |
| 12205 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 12206 | 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)) { |
| 12207 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 12208 | goto error; |
| 12209 | } |
| 12210 | } |
| 12211 | } else |
| 12212 | #endif |
| 12213 | { |
| 12214 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 12215 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 12216 | goto error; |
| 12217 | } |
| 12218 | } |
| 12219 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12220 | /* init the appctx structure */ |
| 12221 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12222 | appctx->ctx.ssl.next_ckchi = NULL; |
| 12223 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 12224 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 12225 | |
| 12226 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 12227 | return 0; |
| 12228 | |
| 12229 | error: |
| 12230 | |
| 12231 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12232 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 12233 | |
| 12234 | return cli_dynerr(appctx, err); |
| 12235 | } |
| 12236 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12237 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12238 | * 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] | 12239 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12240 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12241 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 12242 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12243 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12244 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12245 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 12246 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12247 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12248 | char *end; |
| 12249 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12250 | struct cert_key_and_chain *ckch; |
| 12251 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12252 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12253 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12254 | return 1; |
| 12255 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12256 | if ((buf = alloc_trash_chunk()) == NULL) |
| 12257 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12258 | |
| 12259 | if (!*args[3] || !payload) |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12260 | 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] | 12261 | |
| 12262 | /* The operations on the CKCH architecture are locked so we can |
| 12263 | * manipulate ckch_store and ckch_inst */ |
| 12264 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12265 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 12266 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12267 | if (!chunk_strcpy(buf, args[3])) { |
| 12268 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 12269 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12270 | goto end; |
| 12271 | } |
| 12272 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12273 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 12274 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12275 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12276 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 12277 | *end = '\0'; |
| 12278 | type = cert_exts[i].type; |
| 12279 | break; |
| 12280 | } |
| 12281 | } |
| 12282 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12283 | appctx->ctx.ssl.old_ckchs = NULL; |
| 12284 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 12285 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12286 | /* if there is an ongoing transaction */ |
| 12287 | if (ckchs_transaction.path) { |
| 12288 | /* 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] | 12289 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12290 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12291 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12292 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12293 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12294 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12295 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12296 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12297 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12298 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 12299 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 12300 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 12301 | break; |
| 12302 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12303 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12304 | if (bundle < 0) { |
| 12305 | 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); |
| 12306 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12307 | goto end; |
| 12308 | } |
| 12309 | } |
| 12310 | #endif |
| 12311 | |
| 12312 | /* if there is an ongoing transaction, check if this is the same file */ |
| 12313 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 12314 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 12315 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12316 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12317 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12318 | |
| 12319 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 12320 | |
| 12321 | } else { |
| 12322 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 12323 | |
| 12324 | /* lookup for the certificate in the tree: |
| 12325 | * check if this is used as a bundle AND as a unique certificate */ |
| 12326 | for (i = 0; i < 2; i++) { |
| 12327 | |
| 12328 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 12329 | /* only the bundle name is in the tree and you should |
| 12330 | * never update a bundle name, only a filename */ |
| 12331 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 12332 | /* we tried to look for a non-bundle and we found a bundle */ |
| 12333 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 12334 | err ? err : "", args[3], args[3]); |
| 12335 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12336 | goto end; |
| 12337 | } |
William Lallemand | 3246d94 | 2019-11-04 14:02:11 +0100 | [diff] [blame] | 12338 | /* If we want a bundle but this is not a bundle |
| 12339 | * example: When you try to update <file>.rsa, but |
| 12340 | * <file> is a regular file */ |
| 12341 | if (bundle >= 0 && find_ckchs[i]->multi == 0) { |
| 12342 | find_ckchs[i] = NULL; |
| 12343 | break; |
| 12344 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12345 | } |
| 12346 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 12347 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12348 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12349 | int j; |
| 12350 | |
| 12351 | /* check if it was used in a bundle by removing the |
| 12352 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 12353 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12354 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12355 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 12356 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 12357 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 12358 | break; |
| 12359 | } |
| 12360 | } |
William Lallemand | 37031b8 | 2019-11-04 13:38:53 +0100 | [diff] [blame] | 12361 | if (bundle < 0) /* we didn't find a bundle extension */ |
| 12362 | break; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12363 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12364 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12365 | /* bundles are not supported here, so we don't need to lookup again */ |
| 12366 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12367 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12368 | } |
| 12369 | |
| 12370 | if (find_ckchs[0] && find_ckchs[1]) { |
| 12371 | 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", |
| 12372 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 12373 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12374 | goto end; |
| 12375 | } |
| 12376 | |
| 12377 | 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] | 12378 | } |
| 12379 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12380 | if (!appctx->ctx.ssl.old_ckchs) { |
| 12381 | 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] | 12382 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12383 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12384 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12385 | } |
| 12386 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame] | 12387 | if (!appctx->ctx.ssl.path) { |
| 12388 | /* this is a new transaction, set the path of the transaction */ |
| 12389 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 12390 | if (!appctx->ctx.ssl.path) { |
| 12391 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 12392 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12393 | goto end; |
| 12394 | } |
| 12395 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12396 | |
| 12397 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12398 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12399 | /* duplicate the ckch store */ |
| 12400 | new_ckchs = ckchs_dup(old_ckchs); |
| 12401 | if (!new_ckchs) { |
| 12402 | memprintf(&err, "%sCannot allocate memory!\n", |
| 12403 | err ? err : ""); |
| 12404 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12405 | goto end; |
| 12406 | } |
| 12407 | |
| 12408 | if (!new_ckchs->multi) |
| 12409 | ckch = new_ckchs->ckch; |
| 12410 | else |
| 12411 | ckch = &new_ckchs->ckch[bundle]; |
| 12412 | |
| 12413 | /* appply the change on the duplicate */ |
| 12414 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 12415 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 12416 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12417 | goto end; |
| 12418 | } |
| 12419 | |
| 12420 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 12421 | |
| 12422 | /* we succeed, we can save the ckchs in the transaction */ |
| 12423 | |
| 12424 | /* if there wasn't a transaction, update the old ckchs */ |
William Dauchy | c8bb153 | 2019-11-24 15:04:20 +0100 | [diff] [blame] | 12425 | if (!ckchs_transaction.old_ckchs) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12426 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12427 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 12428 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 12429 | } else { |
| 12430 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 12431 | |
| 12432 | } |
| 12433 | |
| 12434 | /* free the previous ckchs if there was a transaction */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12435 | ckch_store_free(ckchs_transaction.new_ckchs); |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12436 | |
| 12437 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 12438 | |
| 12439 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12440 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12441 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12442 | end: |
| 12443 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12444 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12445 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12446 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12447 | ckch_store_free(appctx->ctx.ssl.new_ckchs); |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12448 | appctx->ctx.ssl.new_ckchs = NULL; |
| 12449 | |
| 12450 | appctx->ctx.ssl.old_ckchs = NULL; |
| 12451 | |
| 12452 | free(appctx->ctx.ssl.path); |
| 12453 | appctx->ctx.ssl.path = NULL; |
| 12454 | |
| 12455 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12456 | 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] | 12457 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12458 | |
| 12459 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12460 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12461 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12462 | /* 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] | 12463 | } |
| 12464 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12465 | /* parsing function of 'abort ssl cert' */ |
| 12466 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12467 | { |
| 12468 | char *err = NULL; |
| 12469 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12470 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12471 | return 1; |
| 12472 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12473 | if (!*args[3]) |
| 12474 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 12475 | |
| 12476 | /* The operations on the CKCH architecture are locked so we can |
| 12477 | * manipulate ckch_store and ckch_inst */ |
| 12478 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12479 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 12480 | |
| 12481 | if (!ckchs_transaction.path) { |
| 12482 | memprintf(&err, "No ongoing transaction!\n"); |
| 12483 | goto error; |
| 12484 | } |
| 12485 | |
| 12486 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 12487 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 12488 | goto error; |
| 12489 | } |
| 12490 | |
| 12491 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12492 | ckch_store_free(ckchs_transaction.new_ckchs); |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12493 | ckchs_transaction.new_ckchs = NULL; |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12494 | ckch_store_free(ckchs_transaction.old_ckchs); |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12495 | ckchs_transaction.old_ckchs = NULL; |
| 12496 | free(ckchs_transaction.path); |
| 12497 | ckchs_transaction.path = NULL; |
| 12498 | |
| 12499 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12500 | |
| 12501 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 12502 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12503 | |
| 12504 | error: |
| 12505 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12506 | |
| 12507 | return cli_dynerr(appctx, err); |
| 12508 | } |
| 12509 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12510 | /* parsing function of 'new ssl cert' */ |
| 12511 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12512 | { |
| 12513 | struct ckch_store *store; |
| 12514 | char *err = NULL; |
| 12515 | char *path; |
| 12516 | |
| 12517 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12518 | return 1; |
| 12519 | |
| 12520 | if (!*args[3]) |
| 12521 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 12522 | |
| 12523 | path = args[3]; |
| 12524 | |
| 12525 | /* The operations on the CKCH architecture are locked so we can |
| 12526 | * manipulate ckch_store and ckch_inst */ |
| 12527 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12528 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 12529 | |
| 12530 | store = ckchs_lookup(path); |
| 12531 | if (store != NULL) { |
| 12532 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 12533 | store = NULL; /* we don't want to free it */ |
| 12534 | goto error; |
| 12535 | } |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 12536 | /* we won't support multi-certificate bundle here */ |
| 12537 | store = ckch_store_new(path, 1); |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12538 | if (!store) { |
| 12539 | memprintf(&err, "unable to allocate memory.\n"); |
| 12540 | goto error; |
| 12541 | } |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12542 | |
| 12543 | /* insert into the ckchs tree */ |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12544 | ebst_insert(&ckchs_tree, &store->node); |
| 12545 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 12546 | |
| 12547 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12548 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12549 | error: |
| 12550 | free(store); |
| 12551 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12552 | return cli_dynerr(appctx, err); |
| 12553 | } |
| 12554 | |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 12555 | /* parsing function of 'del ssl cert' */ |
| 12556 | static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12557 | { |
| 12558 | struct ckch_store *store; |
| 12559 | char *err = NULL; |
| 12560 | char *filename; |
| 12561 | |
| 12562 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12563 | return 1; |
| 12564 | |
| 12565 | if (!*args[3]) |
| 12566 | return cli_err(appctx, "'del ssl cert' expects a certificate name\n"); |
| 12567 | |
| 12568 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12569 | return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n"); |
| 12570 | |
| 12571 | filename = args[3]; |
| 12572 | |
| 12573 | store = ckchs_lookup(filename); |
| 12574 | if (store == NULL) { |
| 12575 | memprintf(&err, "certificate '%s' doesn't exist!\n", filename); |
| 12576 | goto error; |
| 12577 | } |
| 12578 | if (!LIST_ISEMPTY(&store->ckch_inst)) { |
| 12579 | memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename); |
| 12580 | goto error; |
| 12581 | } |
| 12582 | |
| 12583 | ebmb_delete(&store->node); |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12584 | ckch_store_free(store); |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 12585 | |
| 12586 | memprintf(&err, "Certificate '%s' deleted!\n", filename); |
| 12587 | |
| 12588 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12589 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12590 | |
| 12591 | error: |
| 12592 | memprintf(&err, "Can't remove the certificate: %s\n", err ? err : ""); |
| 12593 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12594 | return cli_dynerr(appctx, err); |
| 12595 | } |
| 12596 | |
| 12597 | |
| 12598 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 12599 | 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] | 12600 | { |
| 12601 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 12602 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12603 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 12604 | |
| 12605 | if (!payload) |
| 12606 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12607 | |
| 12608 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12609 | if (!*payload) |
| 12610 | 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] | 12611 | |
| 12612 | /* remove \r and \n from the payload */ |
| 12613 | for (i = 0, j = 0; payload[i]; i++) { |
| 12614 | if (payload[i] == '\r' || payload[i] == '\n') |
| 12615 | continue; |
| 12616 | payload[j++] = payload[i]; |
| 12617 | } |
| 12618 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12619 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12620 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12621 | if (ret < 0) |
| 12622 | 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] | 12623 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12624 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12625 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12626 | if (err) |
| 12627 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 12628 | else |
| 12629 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12630 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12631 | |
| 12632 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12633 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12634 | 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] | 12635 | #endif |
| 12636 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12637 | } |
| 12638 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12639 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12640 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 12641 | { |
| 12642 | switch (arg->type) { |
| 12643 | case ARGT_STR: |
| 12644 | smp->data.type = SMP_T_STR; |
| 12645 | smp->data.u.str = arg->data.str; |
| 12646 | return 1; |
| 12647 | case ARGT_VAR: |
| 12648 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 12649 | return 0; |
| 12650 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 12651 | return 0; |
| 12652 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 12653 | return 0; |
| 12654 | return 1; |
| 12655 | default: |
| 12656 | return 0; |
| 12657 | } |
| 12658 | } |
| 12659 | |
| 12660 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 12661 | const char *file, int line, char **err) |
| 12662 | { |
| 12663 | switch(args[0].data.sint) { |
| 12664 | case 128: |
| 12665 | case 192: |
| 12666 | case 256: |
| 12667 | break; |
| 12668 | default: |
| 12669 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 12670 | return 0; |
| 12671 | } |
| 12672 | /* Try to decode a variable. */ |
| 12673 | vars_check_arg(&args[1], NULL); |
| 12674 | vars_check_arg(&args[2], NULL); |
| 12675 | vars_check_arg(&args[3], NULL); |
| 12676 | return 1; |
| 12677 | } |
| 12678 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12679 | /* 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] | 12680 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 12681 | { |
| 12682 | struct sample nonce, key, aead_tag; |
| 12683 | struct buffer *smp_trash, *smp_trash_alloc; |
| 12684 | EVP_CIPHER_CTX *ctx; |
| 12685 | int dec_size, ret; |
| 12686 | |
| 12687 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 12688 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 12689 | return 0; |
| 12690 | |
| 12691 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 12692 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 12693 | return 0; |
| 12694 | |
| 12695 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 12696 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 12697 | return 0; |
| 12698 | |
| 12699 | smp_trash = get_trash_chunk(); |
| 12700 | smp_trash_alloc = alloc_trash_chunk(); |
| 12701 | if (!smp_trash_alloc) |
| 12702 | return 0; |
| 12703 | |
| 12704 | ctx = EVP_CIPHER_CTX_new(); |
| 12705 | |
| 12706 | if (!ctx) |
| 12707 | goto err; |
| 12708 | |
| 12709 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12710 | if (dec_size < 0) |
| 12711 | goto err; |
| 12712 | smp_trash->data = dec_size; |
| 12713 | |
| 12714 | /* Set cipher type and mode */ |
| 12715 | switch(arg_p[0].data.sint) { |
| 12716 | case 128: |
| 12717 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 12718 | break; |
| 12719 | case 192: |
| 12720 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 12721 | break; |
| 12722 | case 256: |
| 12723 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 12724 | break; |
| 12725 | } |
| 12726 | |
| 12727 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 12728 | |
| 12729 | /* Initialise IV */ |
| 12730 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 12731 | goto err; |
| 12732 | |
| 12733 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12734 | if (dec_size < 0) |
| 12735 | goto err; |
| 12736 | smp_trash->data = dec_size; |
| 12737 | |
| 12738 | /* Initialise key */ |
| 12739 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 12740 | goto err; |
| 12741 | |
| 12742 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 12743 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 12744 | goto err; |
| 12745 | |
| 12746 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 12747 | if (dec_size < 0) |
| 12748 | goto err; |
| 12749 | smp_trash_alloc->data = dec_size; |
| 12750 | dec_size = smp_trash->data; |
| 12751 | |
| 12752 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 12753 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 12754 | |
| 12755 | if (ret <= 0) |
| 12756 | goto err; |
| 12757 | |
| 12758 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 12759 | smp->data.u.str.area = smp_trash->area; |
| 12760 | smp->data.type = SMP_T_BIN; |
| 12761 | smp->flags &= ~SMP_F_CONST; |
| 12762 | free_trash_chunk(smp_trash_alloc); |
| 12763 | return 1; |
| 12764 | |
| 12765 | err: |
| 12766 | free_trash_chunk(smp_trash_alloc); |
| 12767 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12768 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12769 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12770 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12771 | /* Argument validation functions */ |
| 12772 | |
| 12773 | /* This function is used to validate the arguments passed to any "x_dn" ssl |
| 12774 | * keywords. These keywords support specifying a third parameter that must be |
| 12775 | * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK. |
| 12776 | */ |
| 12777 | int val_dnfmt(struct arg *arg, char **err_msg) |
| 12778 | { |
| 12779 | if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) { |
| 12780 | memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument."); |
| 12781 | return 0; |
| 12782 | } |
| 12783 | return 1; |
| 12784 | } |
| 12785 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12786 | /* register cli keywords */ |
| 12787 | static struct cli_kw_list cli_kws = {{ },{ |
| 12788 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12789 | { { "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] | 12790 | { { "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] | 12791 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 12792 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12793 | { { "new", "ssl", "cert", NULL }, "new ssl cert <certfile> : create a new certificate file to be used in a crt-list or a directory", cli_parse_new_cert, NULL, NULL }, |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12794 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 12795 | { { "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] | 12796 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 12797 | { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL }, |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 12798 | { { "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 | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 12799 | { { "add", "ssl", "crt-list", NULL }, "add ssl crt-list <filename> <certfile> [options] : add a line <certfile> to a crt-list <filename>", cli_parse_add_crtlist, cli_io_handler_add_crtlist, cli_release_add_crtlist }, |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 12800 | { { "del", "ssl", "crt-list", NULL }, "del ssl crt-list <filename> <certfile[:line]> : delete a line <certfile> in a crt-list <filename>", cli_parse_del_crtlist, NULL, NULL }, |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 12801 | { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [-n] [<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] | 12802 | { { NULL }, NULL, NULL, NULL } |
| 12803 | }}; |
| 12804 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12805 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12806 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12807 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12808 | * Please take care of keeping this list alphabetically sorted. |
| 12809 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12810 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12811 | { "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] | 12812 | { "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] | 12813 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 12814 | { "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] | 12815 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12816 | { "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] | 12817 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 12818 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 12819 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 12820 | { "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] | 12821 | { "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] | 12822 | { "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] | 12823 | { "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] | 12824 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12825 | { "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] | 12826 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12827 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12828 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12829 | { "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] | 12830 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12831 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12832 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12833 | { "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] | 12834 | { "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] | 12835 | { "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] | 12836 | { "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] | 12837 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12838 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12839 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12840 | { "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] | 12841 | { "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] | 12842 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12843 | { "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] | 12844 | { "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] | 12845 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12846 | { "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] | 12847 | { "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] | 12848 | { "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] | 12849 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12850 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12851 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12852 | { "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] | 12853 | { "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] | 12854 | { "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] | 12855 | { "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] | 12856 | { "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] | 12857 | { "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] | 12858 | { "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] | 12859 | { "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] | 12860 | { "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] | 12861 | { "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] | 12862 | { "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] | 12863 | { "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] | 12864 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12865 | { "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] | 12866 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 12867 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12868 | { "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] | 12869 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12870 | { "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] | 12871 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 12872 | { "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] | 12873 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12874 | { "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] | 12875 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12876 | { "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] | 12877 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12878 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12879 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12880 | { "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] | 12881 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12882 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 12883 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12884 | { "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] | 12885 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12886 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12887 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12888 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12889 | { "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] | 12890 | { NULL, NULL, 0, 0, 0 }, |
| 12891 | }}; |
| 12892 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12893 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 12894 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12895 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12896 | * Please take care of keeping this list alphabetically sorted. |
| 12897 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12898 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12899 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 12900 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12901 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12902 | }}; |
| 12903 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12904 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 12905 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12906 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12907 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12908 | * all code contributors. |
| 12909 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12910 | * the config parser can report an appropriate error when a known keyword was |
| 12911 | * not enabled. |
| 12912 | */ |
William Lallemand | 557823f | 2020-04-01 17:42:47 +0200 | [diff] [blame] | 12913 | |
| 12914 | /* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe |
| 12915 | * with their proxy argument NULL and must only fill the ssl_bind_conf */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12916 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12917 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12918 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12919 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12920 | { "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] | 12921 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12922 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12923 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12924 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12925 | { "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] | 12926 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12927 | { "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] | 12928 | { "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] | 12929 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 12930 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 12931 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12932 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12933 | { NULL, NULL, 0 }, |
| 12934 | }; |
| 12935 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12936 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 12937 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 12938 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12939 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12940 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12941 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12942 | { "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] | 12943 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 12944 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 12945 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 12946 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12947 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12948 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12949 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12950 | { "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] | 12951 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12952 | { "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] | 12953 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 12954 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 12955 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 12956 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 12957 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 12958 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 12959 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12960 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12961 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 12962 | { "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] | 12963 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 12964 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 12965 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 12966 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12967 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12968 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 12969 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12970 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 12971 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12972 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 12973 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 12974 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12975 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 12976 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12977 | { NULL, NULL, 0 }, |
| 12978 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12979 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12980 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 12981 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12982 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12983 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12984 | * all code contributors. |
| 12985 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12986 | * the config parser can report an appropriate error when a known keyword was |
| 12987 | * not enabled. |
| 12988 | */ |
| 12989 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 12990 | { "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] | 12991 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12992 | { "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] | 12993 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 12994 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12995 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 12996 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12997 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12998 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 12999 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 13000 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 13001 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 13002 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 13003 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 13004 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 13005 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 13006 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 13007 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 13008 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 13009 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 13010 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 13011 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 13012 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 13013 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 13014 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 13015 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 13016 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 13017 | { "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] | 13018 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 13019 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 13020 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 13021 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 13022 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 13023 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 13024 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 13025 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 13026 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 13027 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 13028 | { "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] | 13029 | { NULL, NULL, 0, 0 }, |
| 13030 | }}; |
| 13031 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 13032 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 13033 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 13034 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 13035 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 13036 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 13037 | { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 13038 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 13039 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 13040 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 13041 | #ifndef OPENSSL_NO_DH |
| 13042 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 13043 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 13044 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13045 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13046 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13047 | #endif |
Emmanuel Hocdet | c3b7e74 | 2020-04-22 11:06:19 +0200 | [diff] [blame] | 13048 | { CFG_GLOBAL, "ssl-skip-self-issued-ca", ssl_parse_skip_self_issued_ca }, |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 13049 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 13050 | #ifndef OPENSSL_NO_DH |
| 13051 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 13052 | #endif |
| 13053 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 13054 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 13055 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 13056 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 13057 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 13058 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 13059 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Jerome Magnin | b203ff6 | 2020-04-03 15:28:22 +0200 | [diff] [blame] | 13060 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
| 13061 | { CFG_GLOBAL, "ssl-default-bind-curves", ssl_parse_global_curves }, |
| 13062 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 13063 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 13064 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 13065 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 13066 | #endif |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 13067 | { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 13068 | { 0, NULL, NULL }, |
| 13069 | }}; |
| 13070 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 13071 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 13072 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 13073 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13074 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 13075 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 13076 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 13077 | #endif |
| 13078 | { NULL, NULL, 0, 0, 0 }, |
| 13079 | }}; |
| 13080 | |
| 13081 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 13082 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 13083 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 13084 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13085 | .snd_buf = ssl_sock_from_buf, |
| 13086 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 13087 | .subscribe = ssl_subscribe, |
| 13088 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 13089 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 13090 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13091 | .rcv_pipe = NULL, |
| 13092 | .snd_pipe = NULL, |
| 13093 | .shutr = NULL, |
| 13094 | .shutw = ssl_sock_shutw, |
| 13095 | .close = ssl_sock_close, |
| 13096 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 13097 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 13098 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 13099 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 13100 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 13101 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 13102 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13103 | }; |
| 13104 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13105 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 13106 | struct session *sess, struct stream *s, int flags) |
| 13107 | { |
| 13108 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13109 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13110 | |
| 13111 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13112 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13113 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13114 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13115 | 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] | 13116 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13117 | s->req.flags |= CF_READ_NULL; |
| 13118 | return ACT_RET_YIELD; |
| 13119 | } |
| 13120 | } |
| 13121 | return (ACT_RET_CONT); |
| 13122 | } |
| 13123 | |
| 13124 | 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) |
| 13125 | { |
| 13126 | rule->action_ptr = ssl_action_wait_for_hs; |
| 13127 | |
| 13128 | return ACT_RET_PRS_OK; |
| 13129 | } |
| 13130 | |
| 13131 | static struct action_kw_list http_req_actions = {ILH, { |
| 13132 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 13133 | { /* END */ } |
| 13134 | }}; |
| 13135 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 13136 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 13137 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13138 | #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] | 13139 | |
| 13140 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 13141 | { |
| 13142 | if (ptr) { |
| 13143 | chunk_destroy(ptr); |
| 13144 | free(ptr); |
| 13145 | } |
| 13146 | } |
| 13147 | |
| 13148 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 13149 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 13150 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 13151 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 13152 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 13153 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13154 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 13155 | static void __ssl_sock_init(void) |
| 13156 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13157 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13158 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13159 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13160 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13161 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 13162 | if (global_ssl.listen_default_ciphers) |
| 13163 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 13164 | if (global_ssl.connect_default_ciphers) |
| 13165 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 13166 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 13167 | if (global_ssl.listen_default_ciphersuites) |
| 13168 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 13169 | if (global_ssl.connect_default_ciphersuites) |
| 13170 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 13171 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 13172 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 13173 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 13174 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13175 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13176 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13177 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13178 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13179 | n = sk_SSL_COMP_num(cm); |
| 13180 | while (n--) { |
| 13181 | (void) sk_SSL_COMP_pop(cm); |
| 13182 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13183 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13184 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13185 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13186 | ssl_locking_init(); |
| 13187 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13188 | #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] | 13189 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 13190 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 13191 | 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] | 13192 | 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] | 13193 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13194 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 13195 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13196 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 13197 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 13198 | hap_register_post_check(tlskeys_finalize_config); |
| 13199 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13200 | |
| 13201 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 13202 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 13203 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 13204 | hap_register_post_deinit(ssl_free_global_issuers); |
| 13205 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13206 | #ifndef OPENSSL_NO_DH |
| 13207 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 13208 | hap_register_post_deinit(ssl_free_dh); |
| 13209 | #endif |
| 13210 | #ifndef OPENSSL_NO_ENGINE |
| 13211 | hap_register_post_deinit(ssl_free_engines); |
| 13212 | #endif |
| 13213 | /* Load SSL string for the verbose & debug mode. */ |
| 13214 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 13215 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 13216 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 13217 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 13218 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 13219 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 13220 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 13221 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 13222 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 13223 | |
| 13224 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13225 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 13226 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13227 | /* Compute and register the version string */ |
| 13228 | static void ssl_register_build_options() |
| 13229 | { |
| 13230 | char *ptr = NULL; |
| 13231 | int i; |
| 13232 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13233 | memprintf(&ptr, "Built with OpenSSL version : " |
| 13234 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 13235 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13236 | #else /* OPENSSL_IS_BORINGSSL */ |
| 13237 | OPENSSL_VERSION_TEXT |
| 13238 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13239 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 13240 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13241 | #endif |
| 13242 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 13243 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13244 | "no (library version too old)" |
| 13245 | #elif defined(OPENSSL_NO_TLSEXT) |
| 13246 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 13247 | #else |
| 13248 | "yes" |
| 13249 | #endif |
| 13250 | "", ptr); |
| 13251 | |
| 13252 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 13253 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 13254 | "yes" |
| 13255 | #else |
| 13256 | #ifdef OPENSSL_NO_TLSEXT |
| 13257 | "no (because of OPENSSL_NO_TLSEXT)" |
| 13258 | #else |
| 13259 | "no (version might be too old, 0.9.8f min needed)" |
| 13260 | #endif |
| 13261 | #endif |
| 13262 | "", ptr); |
| 13263 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 13264 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 13265 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 13266 | if (methodVersions[i].option) |
| 13267 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 13268 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13269 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13270 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13271 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13272 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 13273 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13274 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13275 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13276 | void ssl_free_engines(void) { |
| 13277 | struct ssl_engine_list *wl, *wlb; |
| 13278 | /* free up engine list */ |
| 13279 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 13280 | ENGINE_finish(wl->e); |
| 13281 | ENGINE_free(wl->e); |
| 13282 | LIST_DEL(&wl->list); |
| 13283 | free(wl); |
| 13284 | } |
| 13285 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13286 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 13287 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13288 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13289 | void ssl_free_dh(void) { |
| 13290 | if (local_dh_1024) { |
| 13291 | DH_free(local_dh_1024); |
| 13292 | local_dh_1024 = NULL; |
| 13293 | } |
| 13294 | if (local_dh_2048) { |
| 13295 | DH_free(local_dh_2048); |
| 13296 | local_dh_2048 = NULL; |
| 13297 | } |
| 13298 | if (local_dh_4096) { |
| 13299 | DH_free(local_dh_4096); |
| 13300 | local_dh_4096 = NULL; |
| 13301 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 13302 | if (global_dh) { |
| 13303 | DH_free(global_dh); |
| 13304 | global_dh = NULL; |
| 13305 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13306 | } |
| 13307 | #endif |
| 13308 | |
| 13309 | __attribute__((destructor)) |
| 13310 | static void __ssl_sock_deinit(void) |
| 13311 | { |
| 13312 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13313 | if (ssl_ctx_lru_tree) { |
| 13314 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 13315 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13316 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13317 | #endif |
| 13318 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13319 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13320 | ERR_remove_state(0); |
| 13321 | ERR_free_strings(); |
| 13322 | |
| 13323 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13324 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13325 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13326 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13327 | CRYPTO_cleanup_all_ex_data(); |
| 13328 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 13329 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13330 | } |
| 13331 | |
| 13332 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13333 | /* |
| 13334 | * Local variables: |
| 13335 | * c-indent-level: 8 |
| 13336 | * c-basic-offset: 8 |
| 13337 | * End: |
| 13338 | */ |