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 | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 170 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 171 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 172 | |
| 173 | char *listen_default_ciphers; |
| 174 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 175 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 176 | char *listen_default_ciphersuites; |
| 177 | char *connect_default_ciphersuites; |
| 178 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 179 | int listen_default_ssloptions; |
| 180 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 181 | struct tls_version_filter listen_default_sslmethods; |
| 182 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 183 | |
| 184 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 185 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 186 | unsigned int max_record; /* SSL max record size */ |
| 187 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 188 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 189 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 190 | int extra_files; /* which files not defined in the configuration file are we looking for */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 191 | } global_ssl = { |
| 192 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 193 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 194 | #endif |
| 195 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 196 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 197 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 198 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 199 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 200 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 201 | #endif |
| 202 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 203 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 204 | #endif |
| 205 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 206 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 207 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 208 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 209 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 210 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 211 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 212 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 213 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 214 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 215 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 216 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 217 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 218 | #endif |
| 219 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 220 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 221 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 222 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 225 | static BIO_METHOD *ha_meth; |
| 226 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 227 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 228 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 229 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 230 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 231 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 232 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 233 | struct wait_event wait_event; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 234 | struct wait_event *subs; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 235 | int xprt_st; /* transport layer state, initialized to zero */ |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 236 | struct buffer early_buf; /* buffer to store the early data received */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 237 | int sent_early_data; /* Amount of early data we sent so far */ |
| 238 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 242 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 243 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 244 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 245 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 246 | /* Methods to implement OpenSSL BIO */ |
| 247 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 248 | { |
| 249 | struct buffer tmpbuf; |
| 250 | struct ssl_sock_ctx *ctx; |
| 251 | int ret; |
| 252 | |
| 253 | ctx = BIO_get_data(h); |
| 254 | tmpbuf.size = num; |
| 255 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 256 | tmpbuf.data = num; |
| 257 | tmpbuf.head = 0; |
| 258 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 259 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 260 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 261 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 262 | } else if (ret == 0) |
| 263 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 268 | { |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int ha_ssl_puts(BIO *h, const char *str) |
| 274 | { |
| 275 | |
| 276 | return ha_ssl_write(h, str, strlen(str)); |
| 277 | } |
| 278 | |
| 279 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 280 | { |
| 281 | struct buffer tmpbuf; |
| 282 | struct ssl_sock_ctx *ctx; |
| 283 | int ret; |
| 284 | |
| 285 | ctx = BIO_get_data(h); |
| 286 | tmpbuf.size = size; |
| 287 | tmpbuf.area = buf; |
| 288 | tmpbuf.data = 0; |
| 289 | tmpbuf.head = 0; |
| 290 | ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 291 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 292 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 293 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 294 | } else if (ret == 0) |
| 295 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 301 | { |
| 302 | int ret = 0; |
| 303 | switch (cmd) { |
| 304 | case BIO_CTRL_DUP: |
| 305 | case BIO_CTRL_FLUSH: |
| 306 | ret = 1; |
| 307 | break; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int ha_ssl_new(BIO *h) |
| 313 | { |
| 314 | BIO_set_init(h, 1); |
| 315 | BIO_set_data(h, NULL); |
| 316 | BIO_clear_flags(h, ~0); |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | static int ha_ssl_free(BIO *data) |
| 321 | { |
| 322 | |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 327 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 328 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 329 | static HA_RWLOCK_T *ssl_rwlocks; |
| 330 | |
| 331 | |
| 332 | unsigned long ssl_id_function(void) |
| 333 | { |
| 334 | return (unsigned long)tid; |
| 335 | } |
| 336 | |
| 337 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 338 | { |
| 339 | if (mode & CRYPTO_LOCK) { |
| 340 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 341 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 342 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 343 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 344 | } |
| 345 | else { |
| 346 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 347 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 348 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 349 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | static int ssl_locking_init(void) |
| 354 | { |
| 355 | int i; |
| 356 | |
| 357 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 358 | if (!ssl_rwlocks) |
| 359 | return -1; |
| 360 | |
| 361 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 362 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 363 | |
| 364 | CRYPTO_set_id_callback(ssl_id_function); |
| 365 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 366 | |
| 367 | return 0; |
| 368 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 369 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 372 | __decl_hathreads(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 373 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 374 | /* Uncommitted CKCH transaction */ |
| 375 | |
| 376 | static struct { |
| 377 | struct ckch_store *new_ckchs; |
| 378 | struct ckch_store *old_ckchs; |
| 379 | char *path; |
| 380 | } ckchs_transaction; |
| 381 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 382 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 383 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 384 | */ |
| 385 | struct cafile_entry { |
| 386 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 387 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 388 | struct ebmb_node node; |
| 389 | char path[0]; |
| 390 | }; |
| 391 | |
| 392 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 393 | |
| 394 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 395 | { |
| 396 | struct ebmb_node *eb; |
| 397 | |
| 398 | eb = ebst_lookup(&cafile_tree, path); |
| 399 | if (eb) { |
| 400 | struct cafile_entry *ca_e; |
| 401 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 402 | return ca_e->ca_store; |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | static int ssl_store_load_locations_file(char *path) |
| 408 | { |
| 409 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 410 | struct cafile_entry *ca_e; |
| 411 | X509_STORE *store = X509_STORE_new(); |
| 412 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 413 | int pathlen; |
| 414 | pathlen = strlen(path); |
| 415 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 416 | if (ca_e) { |
| 417 | memcpy(ca_e->path, path, pathlen + 1); |
| 418 | ca_e->ca_store = store; |
| 419 | ebst_insert(&cafile_tree, &ca_e->node); |
| 420 | return 1; |
| 421 | } |
| 422 | } |
| 423 | X509_STORE_free(store); |
| 424 | return 0; |
| 425 | } |
| 426 | return 1; |
| 427 | } |
| 428 | |
| 429 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 430 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 431 | { |
| 432 | X509_STORE *store; |
| 433 | store = ssl_store_get0_locations_file(path); |
| 434 | if (store_ctx && store) { |
| 435 | int i; |
| 436 | X509_OBJECT *obj; |
| 437 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 438 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 439 | obj = sk_X509_OBJECT_value(objs, i); |
| 440 | switch (X509_OBJECT_get_type(obj)) { |
| 441 | case X509_LU_X509: |
| 442 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 443 | break; |
| 444 | case X509_LU_CRL: |
| 445 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 446 | break; |
| 447 | default: |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 456 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 457 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 458 | { |
| 459 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 460 | return ssl_set_cert_crl_file(store_ctx, path); |
| 461 | } |
| 462 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 463 | /* |
| 464 | Extract CA_list from CA_file already in tree. |
| 465 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 466 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 467 | */ |
| 468 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 469 | { |
| 470 | struct ebmb_node *eb; |
| 471 | struct cafile_entry *ca_e; |
| 472 | |
| 473 | eb = ebst_lookup(&cafile_tree, path); |
| 474 | if (!eb) |
| 475 | return NULL; |
| 476 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 477 | |
| 478 | if (ca_e->ca_list == NULL) { |
| 479 | int i; |
| 480 | unsigned long key; |
| 481 | struct eb_root ca_name_tree = EB_ROOT; |
| 482 | struct eb64_node *node, *back; |
| 483 | struct { |
| 484 | struct eb64_node node; |
| 485 | X509_NAME *xname; |
| 486 | } *ca_name; |
| 487 | STACK_OF(X509_OBJECT) *objs; |
| 488 | STACK_OF(X509_NAME) *skn; |
| 489 | X509 *x; |
| 490 | X509_NAME *xn; |
| 491 | |
| 492 | skn = sk_X509_NAME_new_null(); |
| 493 | /* take x509 from cafile_tree */ |
| 494 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 495 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 496 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 497 | if (!x) |
| 498 | continue; |
| 499 | xn = X509_get_subject_name(x); |
| 500 | if (!xn) |
| 501 | continue; |
| 502 | /* Check for duplicates. */ |
| 503 | key = X509_NAME_hash(xn); |
| 504 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 505 | node && ca_name == NULL; |
| 506 | node = eb64_next(node)) { |
| 507 | ca_name = container_of(node, typeof(*ca_name), node); |
| 508 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 509 | ca_name = NULL; |
| 510 | } |
| 511 | /* find a duplicate */ |
| 512 | if (ca_name) |
| 513 | continue; |
| 514 | ca_name = calloc(1, sizeof *ca_name); |
| 515 | xn = X509_NAME_dup(xn); |
| 516 | if (!ca_name || |
| 517 | !xn || |
| 518 | !sk_X509_NAME_push(skn, xn)) { |
| 519 | free(ca_name); |
| 520 | X509_NAME_free(xn); |
| 521 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 522 | sk_X509_NAME_free(skn); |
| 523 | skn = NULL; |
| 524 | break; |
| 525 | } |
| 526 | ca_name->node.key = key; |
| 527 | ca_name->xname = xn; |
| 528 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 529 | } |
| 530 | ca_e->ca_list = skn; |
| 531 | /* remove temporary ca_name tree */ |
| 532 | node = eb64_first(&ca_name_tree); |
| 533 | while (node) { |
| 534 | ca_name = container_of(node, typeof(*ca_name), node); |
| 535 | back = eb64_next(node); |
| 536 | eb64_delete(node); |
| 537 | free(ca_name); |
| 538 | node = back; |
| 539 | } |
| 540 | } |
| 541 | return ca_e->ca_list; |
| 542 | } |
| 543 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 544 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 545 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 546 | unsigned long long int xxh64; |
| 547 | unsigned char ciphersuite_len; |
| 548 | char ciphersuite[0]; |
| 549 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 550 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 551 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 552 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 553 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 554 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 555 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 556 | #endif |
| 557 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 558 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 559 | static unsigned int openssl_engines_initialized; |
| 560 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 561 | struct ssl_engine_list { |
| 562 | struct list list; |
| 563 | ENGINE *e; |
| 564 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 565 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 566 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 567 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 568 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 569 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 570 | static DH *local_dh_1024 = NULL; |
| 571 | static DH *local_dh_2048 = NULL; |
| 572 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 573 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 574 | #endif /* OPENSSL_NO_DH */ |
| 575 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 576 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 577 | /* X509V3 Extensions that will be added on generated certificates */ |
| 578 | #define X509V3_EXT_SIZE 5 |
| 579 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 580 | "basicConstraints", |
| 581 | "nsComment", |
| 582 | "subjectKeyIdentifier", |
| 583 | "authorityKeyIdentifier", |
| 584 | "keyUsage", |
| 585 | }; |
| 586 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 587 | "CA:FALSE", |
| 588 | "\"OpenSSL Generated Certificate\"", |
| 589 | "hash", |
| 590 | "keyid,issuer:always", |
| 591 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 592 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 593 | /* LRU cache to store generated certificate */ |
| 594 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 595 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 596 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 597 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 598 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 599 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 600 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 601 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 602 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 603 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 604 | /* The order here matters for picking a default context, |
| 605 | * keep the most common keytype at the bottom of the list |
| 606 | */ |
| 607 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 608 | "dsa", |
| 609 | "ecdsa", |
| 610 | "rsa" |
| 611 | }; |
| 612 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 613 | #else |
| 614 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 615 | #endif |
| 616 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 617 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 618 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 619 | |
| 620 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 621 | |
| 622 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 623 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 624 | |
| 625 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 626 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 627 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 628 | /* |
| 629 | * This function gives the detail of the SSL error. It is used only |
| 630 | * if the debug mode and the verbose mode are activated. It dump all |
| 631 | * the SSL error until the stack was empty. |
| 632 | */ |
| 633 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 634 | { |
| 635 | unsigned long ret; |
| 636 | |
| 637 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 638 | while(1) { |
| 639 | ret = ERR_get_error(); |
| 640 | if (ret == 0) |
| 641 | return; |
| 642 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 643 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 644 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 649 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 650 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 651 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 652 | { |
| 653 | int err_code = ERR_ABORT; |
| 654 | ENGINE *engine; |
| 655 | struct ssl_engine_list *el; |
| 656 | |
| 657 | /* grab the structural reference to the engine */ |
| 658 | engine = ENGINE_by_id(engine_id); |
| 659 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 660 | ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 661 | goto fail_get; |
| 662 | } |
| 663 | |
| 664 | if (!ENGINE_init(engine)) { |
| 665 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 666 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 667 | goto fail_init; |
| 668 | } |
| 669 | |
| 670 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 671 | ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 672 | goto fail_set_method; |
| 673 | } |
| 674 | |
| 675 | el = calloc(1, sizeof(*el)); |
| 676 | el->e = engine; |
| 677 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 678 | nb_engines++; |
| 679 | if (global_ssl.async) |
| 680 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | |
| 683 | fail_set_method: |
| 684 | /* release the functional reference from ENGINE_init() */ |
| 685 | ENGINE_finish(engine); |
| 686 | |
| 687 | fail_init: |
| 688 | /* release the structural reference from ENGINE_by_id() */ |
| 689 | ENGINE_free(engine); |
| 690 | |
| 691 | fail_get: |
| 692 | return err_code; |
| 693 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 694 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 695 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 696 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 697 | /* |
| 698 | * openssl async fd handler |
| 699 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 700 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 701 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 702 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 703 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 704 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 705 | * to poll this fd until it is requested |
| 706 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 707 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 708 | fd_cant_recv(fd); |
| 709 | |
| 710 | /* crypto engine is available, let's notify the associated |
| 711 | * connection that it can pursue its processing. |
| 712 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 713 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 716 | /* |
| 717 | * openssl async delayed SSL_free handler |
| 718 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 719 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 720 | { |
| 721 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 722 | OSSL_ASYNC_FD all_fd[32]; |
| 723 | size_t num_all_fds = 0; |
| 724 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 725 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 726 | /* We suppose that the async job for a same SSL * |
| 727 | * are serialized. So if we are awake it is |
| 728 | * because the running job has just finished |
| 729 | * and we can remove all async fds safely |
| 730 | */ |
| 731 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 732 | if (num_all_fds > 32) { |
| 733 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 738 | for (i=0 ; i < num_all_fds ; i++) |
| 739 | fd_remove(all_fd[i]); |
| 740 | |
| 741 | /* Now we can safely call SSL_free, no more pending job in engines */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 742 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 743 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 744 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 745 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 746 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 747 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 748 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 749 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 750 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 751 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 752 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 753 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 754 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 755 | size_t num_add_fds = 0; |
| 756 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 757 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 758 | |
| 759 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 760 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 761 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 762 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 763 | return; |
| 764 | } |
| 765 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 766 | SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 767 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 768 | /* We remove unused fds from the fdtab */ |
| 769 | for (i=0 ; i < num_del_fds ; i++) |
| 770 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 771 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 772 | /* We add new fds to the fdtab */ |
| 773 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 774 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 777 | num_add_fds = 0; |
| 778 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 779 | if (num_add_fds > 32) { |
| 780 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 781 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 782 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 783 | |
| 784 | /* We activate the polling for all known async fds */ |
| 785 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 786 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 787 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 788 | /* To ensure that the fd cache won't be used |
| 789 | * We'll prefer to catch a real RD event |
| 790 | * because handling an EAGAIN on this fd will |
| 791 | * result in a context switch and also |
| 792 | * some engines uses a fd in blocking mode. |
| 793 | */ |
| 794 | fd_cant_recv(add_fd[i]); |
| 795 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 796 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 797 | } |
| 798 | #endif |
| 799 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 800 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 801 | /* |
| 802 | * This function returns the number of seconds elapsed |
| 803 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 804 | * date presented un ASN1_GENERALIZEDTIME. |
| 805 | * |
| 806 | * In parsing error case, it returns -1. |
| 807 | */ |
| 808 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 809 | { |
| 810 | long epoch; |
| 811 | char *p, *end; |
| 812 | const unsigned short month_offset[12] = { |
| 813 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 814 | }; |
| 815 | int year, month; |
| 816 | |
| 817 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 818 | |
| 819 | p = (char *)d->data; |
| 820 | end = p + d->length; |
| 821 | |
| 822 | if (end - p < 4) return -1; |
| 823 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 824 | p += 4; |
| 825 | if (end - p < 2) return -1; |
| 826 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 827 | if (month < 1 || month > 12) return -1; |
| 828 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 829 | We consider leap years and the current month (<marsh or not) */ |
| 830 | epoch = ( ((year - 1970) * 365) |
| 831 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 832 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 833 | + month_offset[month-1] |
| 834 | ) * 24 * 60 * 60; |
| 835 | p += 2; |
| 836 | if (end - p < 2) return -1; |
| 837 | /* Add the number of seconds of completed days of current month */ |
| 838 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 839 | p += 2; |
| 840 | if (end - p < 2) return -1; |
| 841 | /* Add the completed hours of the current day */ |
| 842 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 843 | p += 2; |
| 844 | if (end - p < 2) return -1; |
| 845 | /* Add the completed minutes of the current hour */ |
| 846 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 847 | p += 2; |
| 848 | if (p == end) return -1; |
| 849 | /* Test if there is available seconds */ |
| 850 | if (p[0] < '0' || p[0] > '9') |
| 851 | goto nosec; |
| 852 | if (end - p < 2) return -1; |
| 853 | /* Add the seconds of the current minute */ |
| 854 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 855 | p += 2; |
| 856 | if (p == end) return -1; |
| 857 | /* Ignore seconds float part if present */ |
| 858 | if (p[0] == '.') { |
| 859 | do { |
| 860 | if (++p == end) return -1; |
| 861 | } while (p[0] >= '0' && p[0] <= '9'); |
| 862 | } |
| 863 | |
| 864 | nosec: |
| 865 | if (p[0] == 'Z') { |
| 866 | if (end - p != 1) return -1; |
| 867 | return epoch; |
| 868 | } |
| 869 | else if (p[0] == '+') { |
| 870 | if (end - p != 5) return -1; |
| 871 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 872 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 873 | } |
| 874 | else if (p[0] == '-') { |
| 875 | if (end - p != 5) return -1; |
| 876 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 877 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | return -1; |
| 881 | } |
| 882 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 883 | /* |
| 884 | * struct alignment works here such that the key.key is the same as key_data |
| 885 | * Do not change the placement of key_data |
| 886 | */ |
| 887 | struct certificate_ocsp { |
| 888 | struct ebmb_node key; |
| 889 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 890 | struct buffer response; |
| 891 | long expire; |
| 892 | }; |
| 893 | |
| 894 | struct ocsp_cbk_arg { |
| 895 | int is_single; |
| 896 | int single_kt; |
| 897 | union { |
| 898 | struct certificate_ocsp *s_ocsp; |
| 899 | /* |
| 900 | * m_ocsp will have multiple entries dependent on key type |
| 901 | * Entry 0 - DSA |
| 902 | * Entry 1 - ECDSA |
| 903 | * Entry 2 - RSA |
| 904 | */ |
| 905 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 906 | }; |
| 907 | }; |
| 908 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 909 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 910 | |
| 911 | /* This function starts to check if the OCSP response (in DER format) contained |
| 912 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 913 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 914 | * contained in the OCSP Response and exits on error if no match. |
| 915 | * If it's a valid OCSP Response: |
| 916 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 917 | * pointed by 'ocsp'. |
| 918 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 919 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 920 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 921 | * already present in the container, it will be overwritten. |
| 922 | * |
| 923 | * Note: OCSP response containing more than one OCSP Single response is not |
| 924 | * considered valid. |
| 925 | * |
| 926 | * Returns 0 on success, 1 in error case. |
| 927 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 928 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 929 | struct certificate_ocsp *ocsp, |
| 930 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 931 | { |
| 932 | OCSP_RESPONSE *resp; |
| 933 | OCSP_BASICRESP *bs = NULL; |
| 934 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 935 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 936 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 937 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 938 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 939 | int reason; |
| 940 | int ret = 1; |
| 941 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 942 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 943 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 944 | if (!resp) { |
| 945 | memprintf(err, "Unable to parse OCSP response"); |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | rc = OCSP_response_status(resp); |
| 950 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 951 | memprintf(err, "OCSP response status not successful"); |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | bs = OCSP_response_get1_basic(resp); |
| 956 | if (!bs) { |
| 957 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | count_sr = OCSP_resp_count(bs); |
| 962 | if (count_sr > 1) { |
| 963 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | sr = OCSP_resp_get0(bs, 0); |
| 968 | if (!sr) { |
| 969 | memprintf(err, "Failed to get OCSP single response"); |
| 970 | goto out; |
| 971 | } |
| 972 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 973 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 974 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 975 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 976 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 977 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 978 | goto out; |
| 979 | } |
| 980 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 981 | if (!nextupd) { |
| 982 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 983 | goto out; |
| 984 | } |
| 985 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 986 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 987 | if (!rc) { |
| 988 | memprintf(err, "OCSP single response: no longer valid."); |
| 989 | goto out; |
| 990 | } |
| 991 | |
| 992 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 993 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 994 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 995 | goto out; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (!ocsp) { |
| 1000 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 1001 | unsigned char *p; |
| 1002 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1003 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1004 | if (!rc) { |
| 1005 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | |
| 1009 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 1010 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 1011 | goto out; |
| 1012 | } |
| 1013 | |
| 1014 | p = key; |
| 1015 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1016 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1017 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1018 | if (!ocsp) { |
| 1019 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 1020 | goto out; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* According to comments on "chunk_dup", the |
| 1025 | previous chunk buffer will be freed */ |
| 1026 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1027 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1031 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1032 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1033 | ret = 0; |
| 1034 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1035 | ERR_clear_error(); |
| 1036 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1037 | if (bs) |
| 1038 | OCSP_BASICRESP_free(bs); |
| 1039 | |
| 1040 | if (resp) |
| 1041 | OCSP_RESPONSE_free(resp); |
| 1042 | |
| 1043 | return ret; |
| 1044 | } |
| 1045 | /* |
| 1046 | * External function use to update the OCSP response in the OCSP response's |
| 1047 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1048 | * to update in DER format. |
| 1049 | * |
| 1050 | * Returns 0 on success, 1 in error case. |
| 1051 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1052 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1053 | { |
| 1054 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1055 | } |
| 1056 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1057 | #endif |
| 1058 | |
| 1059 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1060 | /* |
| 1061 | * This function load the OCSP Resonse in DER format contained in file at |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1062 | * path 'ocsp_path' or base64 in a buffer <buf> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1063 | * |
| 1064 | * Returns 0 on success, 1 in error case. |
| 1065 | */ |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1066 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1067 | { |
| 1068 | int fd = -1; |
| 1069 | int r = 0; |
| 1070 | int ret = 1; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1071 | struct buffer *ocsp_response; |
| 1072 | struct buffer *src = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1073 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1074 | if (buf) { |
| 1075 | int i, j; |
| 1076 | /* if it's from a buffer it will be base64 */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1077 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1078 | /* remove \r and \n from the payload */ |
| 1079 | for (i = 0, j = 0; buf[i]; i++) { |
| 1080 | if (buf[i] == '\r' || buf[i] == '\n') |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1081 | continue; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1082 | buf[j++] = buf[i]; |
| 1083 | } |
| 1084 | buf[j] = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1085 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1086 | ret = base64dec(buf, j, trash.area, trash.size); |
| 1087 | if (ret < 0) { |
| 1088 | memprintf(err, "Error reading OCSP response in base64 format"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1089 | goto end; |
| 1090 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1091 | trash.data = ret; |
| 1092 | src = &trash; |
| 1093 | } else { |
| 1094 | fd = open(ocsp_path, O_RDONLY); |
| 1095 | if (fd == -1) { |
| 1096 | memprintf(err, "Error opening OCSP response file"); |
| 1097 | goto end; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1098 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1099 | |
| 1100 | trash.data = 0; |
| 1101 | while (trash.data < trash.size) { |
| 1102 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1103 | if (r < 0) { |
| 1104 | if (errno == EINTR) |
| 1105 | continue; |
| 1106 | |
| 1107 | memprintf(err, "Error reading OCSP response from file"); |
| 1108 | goto end; |
| 1109 | } |
| 1110 | else if (r == 0) { |
| 1111 | break; |
| 1112 | } |
| 1113 | trash.data += r; |
| 1114 | } |
| 1115 | close(fd); |
| 1116 | fd = -1; |
| 1117 | src = &trash; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1120 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 1121 | if (!chunk_dup(ocsp_response, src)) { |
| 1122 | free(ocsp_response); |
| 1123 | ocsp_response = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1124 | goto end; |
| 1125 | } |
Emmanuel Hocdet | 0667fae | 2020-01-16 14:41:36 +0100 | [diff] [blame] | 1126 | /* no error, fill ckch with new context, old context must be free */ |
| 1127 | if (ckch->ocsp_response) { |
| 1128 | free(ckch->ocsp_response->area); |
| 1129 | ckch->ocsp_response->area = NULL; |
| 1130 | free(ckch->ocsp_response); |
| 1131 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1132 | ckch->ocsp_response = ocsp_response; |
William Lallemand | e0f48ae | 2019-10-15 13:44:57 +0200 | [diff] [blame] | 1133 | ret = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1134 | end: |
| 1135 | if (fd != -1) |
| 1136 | close(fd); |
| 1137 | |
| 1138 | return ret; |
| 1139 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1140 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1141 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1142 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1143 | static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) |
| 1144 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1145 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1146 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1147 | struct connection *conn; |
| 1148 | int head; |
| 1149 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1150 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1151 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1152 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1153 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1154 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1155 | |
| 1156 | keys = ref->tlskeys; |
| 1157 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1158 | |
| 1159 | if (enc) { |
| 1160 | memcpy(key_name, keys[head].name, 16); |
| 1161 | |
| 1162 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1163 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1164 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1165 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1166 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1167 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1168 | goto end; |
| 1169 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1170 | HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1171 | ret = 1; |
| 1172 | } |
| 1173 | else if (ref->key_size_bits == 256 ) { |
| 1174 | |
| 1175 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1176 | goto end; |
| 1177 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1178 | HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1179 | ret = 1; |
| 1180 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1183 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1184 | goto found; |
| 1185 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1186 | ret = 0; |
| 1187 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1188 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1189 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1190 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1191 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1192 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1193 | goto end; |
| 1194 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1195 | ret = i ? 2 : 1; |
| 1196 | } |
| 1197 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1198 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1199 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1200 | goto end; |
| 1201 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1202 | ret = i ? 2 : 1; |
| 1203 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1204 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1205 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1206 | end: |
| 1207 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1208 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1212 | { |
| 1213 | struct tls_keys_ref *ref; |
| 1214 | |
| 1215 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1216 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1217 | return ref; |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1222 | { |
| 1223 | struct tls_keys_ref *ref; |
| 1224 | |
| 1225 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1226 | if (ref->unique_id == unique_id) |
| 1227 | return ref; |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1231 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1232 | * match existing ones, this function returns -1 |
| 1233 | * else it returns 0 on success. |
| 1234 | */ |
| 1235 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1236 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1237 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1238 | if (ref->key_size_bits == 128) { |
| 1239 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1240 | return -1; |
| 1241 | } |
| 1242 | else if (ref->key_size_bits == 256) { |
| 1243 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1244 | return -1; |
| 1245 | } |
| 1246 | else |
| 1247 | return -1; |
| 1248 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1249 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1250 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1251 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1252 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1253 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1254 | |
| 1255 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1258 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1259 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1260 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1261 | |
| 1262 | if(!ref) { |
| 1263 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1264 | return 1; |
| 1265 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1266 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1267 | memprintf(err, "Invalid key size"); |
| 1268 | return 1; |
| 1269 | } |
| 1270 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1271 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1272 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1273 | |
| 1274 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1275 | * automatic ids. It's called just after the basic checks. It returns |
| 1276 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1277 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1278 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1279 | { |
| 1280 | int i = 0; |
| 1281 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1282 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1283 | |
| 1284 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1285 | if (ref->unique_id == -1) { |
| 1286 | /* Look for the first free id. */ |
| 1287 | while (1) { |
| 1288 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1289 | if (ref2->unique_id == i) { |
| 1290 | i++; |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | if (&ref2->list == &tlskeys_reference) |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | /* Uses the unique id and increment it for the next entry. */ |
| 1299 | ref->unique_id = i; |
| 1300 | i++; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /* This sort the reference list by id. */ |
| 1305 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1306 | LIST_DEL(&ref->list); |
| 1307 | list_for_each_entry(ref3, &tkr, list) { |
| 1308 | if (ref->unique_id < ref3->unique_id) { |
| 1309 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | if (&ref3->list == &tkr) |
| 1314 | LIST_ADDQ(&tkr, &ref->list); |
| 1315 | } |
| 1316 | |
| 1317 | /* swap root */ |
| 1318 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1319 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1320 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1321 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1322 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1323 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1324 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1325 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1326 | { |
| 1327 | switch (evp_keytype) { |
| 1328 | case EVP_PKEY_RSA: |
| 1329 | return 2; |
| 1330 | case EVP_PKEY_DSA: |
| 1331 | return 0; |
| 1332 | case EVP_PKEY_EC: |
| 1333 | return 1; |
| 1334 | } |
| 1335 | |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1339 | /* |
| 1340 | * Callback used to set OCSP status extension content in server hello. |
| 1341 | */ |
| 1342 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1343 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1344 | struct certificate_ocsp *ocsp; |
| 1345 | struct ocsp_cbk_arg *ocsp_arg; |
| 1346 | char *ssl_buf; |
| 1347 | EVP_PKEY *ssl_pkey; |
| 1348 | int key_type; |
| 1349 | int index; |
| 1350 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1351 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1352 | |
| 1353 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1354 | if (!ssl_pkey) |
| 1355 | return SSL_TLSEXT_ERR_NOACK; |
| 1356 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1357 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1358 | |
| 1359 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1360 | ocsp = ocsp_arg->s_ocsp; |
| 1361 | else { |
| 1362 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1363 | * the certificate type |
| 1364 | */ |
| 1365 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1366 | |
| 1367 | if (index < 0) |
| 1368 | return SSL_TLSEXT_ERR_NOACK; |
| 1369 | |
| 1370 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1371 | |
| 1372 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1373 | |
| 1374 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1375 | !ocsp->response.area || |
| 1376 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1377 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1378 | return SSL_TLSEXT_ERR_NOACK; |
| 1379 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1380 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1381 | if (!ssl_buf) |
| 1382 | return SSL_TLSEXT_ERR_NOACK; |
| 1383 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1384 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1385 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1386 | |
| 1387 | return SSL_TLSEXT_ERR_OK; |
| 1388 | } |
| 1389 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1390 | #endif |
| 1391 | |
| 1392 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1393 | /* |
| 1394 | * This function enables the handling of OCSP status extension on 'ctx' if a |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1395 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1396 | * status extension, the issuer's certificate is mandatory. It should be |
| 1397 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1398 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1399 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1400 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1401 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1402 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1403 | * |
| 1404 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1405 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1406 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1407 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1408 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1409 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1410 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1411 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1412 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1413 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1414 | char *warn = NULL; |
| 1415 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1416 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1417 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1418 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1419 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1420 | if (!x) |
| 1421 | goto out; |
| 1422 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1423 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1424 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1425 | if (chain) { |
| 1426 | /* check if one of the certificate of the chain is the issuer */ |
| 1427 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1428 | X509 *ti = sk_X509_value(chain, i); |
| 1429 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1430 | issuer = ti; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1435 | if (!issuer) |
| 1436 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1437 | |
| 1438 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1439 | if (!cid) |
| 1440 | goto out; |
| 1441 | |
| 1442 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1443 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1444 | goto out; |
| 1445 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1446 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1447 | if (!ocsp) |
| 1448 | goto out; |
| 1449 | |
| 1450 | p = ocsp->key_data; |
| 1451 | i2d_OCSP_CERTID(cid, &p); |
| 1452 | |
| 1453 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1454 | if (iocsp == ocsp) |
| 1455 | ocsp = NULL; |
| 1456 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1457 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1458 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1459 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1460 | #endif |
| 1461 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1462 | |
| 1463 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1464 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1465 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1466 | |
| 1467 | cb_arg->is_single = 1; |
| 1468 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1469 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1470 | pkey = X509_get_pubkey(x); |
| 1471 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1472 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1473 | |
| 1474 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1475 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1476 | } else { |
| 1477 | /* |
| 1478 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1479 | * Update that cb_arg with the new cert's staple |
| 1480 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1481 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1482 | struct certificate_ocsp *tmp_ocsp; |
| 1483 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1484 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1485 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1486 | |
| 1487 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1488 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1489 | #else |
| 1490 | cb_arg = ctx->tlsext_status_arg; |
| 1491 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 1494 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1495 | * the order of operations below matter, take care when changing it |
| 1496 | */ |
| 1497 | tmp_ocsp = cb_arg->s_ocsp; |
| 1498 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1499 | cb_arg->s_ocsp = NULL; |
| 1500 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1501 | cb_arg->is_single = 0; |
| 1502 | cb_arg->single_kt = 0; |
| 1503 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1504 | pkey = X509_get_pubkey(x); |
| 1505 | key_type = EVP_PKEY_base_id(pkey); |
| 1506 | EVP_PKEY_free(pkey); |
| 1507 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1508 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1509 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1510 | cb_arg->m_ocsp[index] = iocsp; |
| 1511 | |
| 1512 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1513 | |
| 1514 | ret = 0; |
| 1515 | |
| 1516 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1517 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1518 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1519 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1523 | if (cid) |
| 1524 | OCSP_CERTID_free(cid); |
| 1525 | |
| 1526 | if (ocsp) |
| 1527 | free(ocsp); |
| 1528 | |
| 1529 | if (warn) |
| 1530 | free(warn); |
| 1531 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1532 | return ret; |
| 1533 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1534 | #else /* OPENSSL_IS_BORINGSSL */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1535 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1536 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1537 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1538 | } |
| 1539 | #endif |
| 1540 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1541 | #endif |
| 1542 | |
| 1543 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1544 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1545 | |
| 1546 | #define CT_EXTENSION_TYPE 18 |
| 1547 | |
| 1548 | static int sctl_ex_index = -1; |
| 1549 | |
| 1550 | /* |
| 1551 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1552 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1553 | * is performed. |
| 1554 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1555 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1556 | { |
| 1557 | int ret = 1; |
| 1558 | int len, pos, sct_len; |
| 1559 | unsigned char *data; |
| 1560 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1561 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1562 | goto out; |
| 1563 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1564 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1565 | len = (data[0] << 8) | data[1]; |
| 1566 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1567 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1568 | goto out; |
| 1569 | |
| 1570 | data = data + 2; |
| 1571 | pos = 0; |
| 1572 | while (pos < len) { |
| 1573 | if (len - pos < 2) |
| 1574 | goto out; |
| 1575 | |
| 1576 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1577 | if (pos + sct_len + 2 > len) |
| 1578 | goto out; |
| 1579 | |
| 1580 | pos += sct_len + 2; |
| 1581 | } |
| 1582 | |
| 1583 | ret = 0; |
| 1584 | |
| 1585 | out: |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1589 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 1590 | * It fills the ckch->sctl buffer |
| 1591 | * return 0 on success or != 0 on failure */ |
| 1592 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1593 | { |
| 1594 | int fd = -1; |
| 1595 | int r = 0; |
| 1596 | int ret = 1; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1597 | struct buffer tmp; |
| 1598 | struct buffer *src; |
| 1599 | struct buffer *sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1600 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1601 | if (buf) { |
| 1602 | tmp.area = buf; |
| 1603 | tmp.data = strlen(buf); |
| 1604 | tmp.size = tmp.data + 1; |
| 1605 | src = &tmp; |
| 1606 | } else { |
| 1607 | fd = open(sctl_path, O_RDONLY); |
| 1608 | if (fd == -1) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1609 | goto end; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1610 | |
| 1611 | trash.data = 0; |
| 1612 | while (trash.data < trash.size) { |
| 1613 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1614 | if (r < 0) { |
| 1615 | if (errno == EINTR) |
| 1616 | continue; |
| 1617 | goto end; |
| 1618 | } |
| 1619 | else if (r == 0) { |
| 1620 | break; |
| 1621 | } |
| 1622 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1623 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1624 | src = &trash; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1627 | ret = ssl_sock_parse_sctl(src); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1628 | if (ret) |
| 1629 | goto end; |
| 1630 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1631 | sctl = calloc(1, sizeof(*sctl)); |
| 1632 | if (!chunk_dup(sctl, src)) { |
| 1633 | free(sctl); |
| 1634 | sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1635 | goto end; |
| 1636 | } |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1637 | /* no error, fill ckch with new context, old context must be free */ |
| 1638 | if (ckch->sctl) { |
| 1639 | free(ckch->sctl->area); |
| 1640 | ckch->sctl->area = NULL; |
| 1641 | free(ckch->sctl); |
| 1642 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1643 | ckch->sctl = sctl; |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1644 | ret = 0; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1645 | end: |
| 1646 | if (fd != -1) |
| 1647 | close(fd); |
| 1648 | |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
| 1652 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1653 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1654 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1655 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1656 | *out = (unsigned char *) sctl->area; |
| 1657 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1658 | |
| 1659 | return 1; |
| 1660 | } |
| 1661 | |
| 1662 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1663 | { |
| 1664 | return 1; |
| 1665 | } |
| 1666 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1667 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1668 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1669 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1670 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1671 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1672 | goto out; |
| 1673 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1674 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1675 | |
| 1676 | ret = 0; |
| 1677 | |
| 1678 | out: |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | #endif |
| 1683 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1684 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1685 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1686 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1687 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1688 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1689 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1690 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1691 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1692 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1693 | * change this to #if and do not assign a default value to this macro! |
| 1694 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1695 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1696 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1697 | if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1698 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1699 | conn->err_code = CO_ER_SSL_RENEG; |
| 1700 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1701 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1702 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1703 | |
| 1704 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1705 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1706 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1707 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1708 | consider that the buffering was activated, |
| 1709 | so we rise the output buffer size from 4k |
| 1710 | to 16k */ |
| 1711 | write_bio = SSL_get_wbio(ssl); |
| 1712 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1713 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1714 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1720 | /* Callback is called for each certificate of the chain during a verify |
| 1721 | ok is set to 1 if preverify detect no error on current certificate. |
| 1722 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1723 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1724 | { |
| 1725 | SSL *ssl; |
| 1726 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1727 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1728 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1729 | |
| 1730 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1731 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1732 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1733 | ctx = conn->xprt_ctx; |
| 1734 | |
| 1735 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1736 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1737 | if (ok) /* no errors */ |
| 1738 | return ok; |
| 1739 | |
| 1740 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1741 | err = X509_STORE_CTX_get_error(x_store); |
| 1742 | |
| 1743 | /* check if CA error needs to be ignored */ |
| 1744 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1745 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1746 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1747 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1750 | if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1751 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1752 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1753 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1754 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1756 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1760 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1761 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1762 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1763 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1764 | if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1765 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1766 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1767 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1768 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1769 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1770 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1771 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1774 | static inline |
| 1775 | void ssl_sock_parse_clienthello(int write_p, int version, int content_type, |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1776 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1777 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1778 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1779 | unsigned char *msg; |
| 1780 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1781 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1782 | |
| 1783 | /* This function is called for "from client" and "to server" |
| 1784 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1785 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1786 | */ |
| 1787 | |
| 1788 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1789 | * otherwise it is set to 1. |
| 1790 | */ |
| 1791 | if (write_p != 0) |
| 1792 | return; |
| 1793 | |
| 1794 | /* content_type contains the type of message received or sent |
| 1795 | * according with the SSL/TLS protocol spec. This message is |
| 1796 | * encoded with one byte. The value 256 (two bytes) is used |
| 1797 | * for designing the SSL/TLS record layer. According with the |
| 1798 | * rfc6101, the expected message (other than 256) are: |
| 1799 | * - change_cipher_spec(20) |
| 1800 | * - alert(21) |
| 1801 | * - handshake(22) |
| 1802 | * - application_data(23) |
| 1803 | * - (255) |
| 1804 | * We are interessed by the handshake and specially the client |
| 1805 | * hello. |
| 1806 | */ |
| 1807 | if (content_type != 22) |
| 1808 | return; |
| 1809 | |
| 1810 | /* The message length is at least 4 bytes, containing the |
| 1811 | * message type and the message length. |
| 1812 | */ |
| 1813 | if (len < 4) |
| 1814 | return; |
| 1815 | |
| 1816 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1817 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1818 | * - hello_request(0) |
| 1819 | * - client_hello(1) |
| 1820 | * - server_hello(2) |
| 1821 | * - certificate(11) |
| 1822 | * - server_key_exchange (12) |
| 1823 | * - certificate_request(13) |
| 1824 | * - server_hello_done(14) |
| 1825 | * We are interested by the client hello. |
| 1826 | */ |
| 1827 | msg = (unsigned char *)buf; |
| 1828 | if (msg[0] != 1) |
| 1829 | return; |
| 1830 | |
| 1831 | /* Next three bytes are the length of the message. The total length |
| 1832 | * must be this decoded length + 4. If the length given as argument |
| 1833 | * is not the same, we abort the protocol dissector. |
| 1834 | */ |
| 1835 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1836 | if (len < rec_len + 4) |
| 1837 | return; |
| 1838 | msg += 4; |
| 1839 | end = msg + rec_len; |
| 1840 | if (end < msg) |
| 1841 | return; |
| 1842 | |
| 1843 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1844 | * for minor, the random, composed by 4 bytes for the unix time and |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1845 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1846 | */ |
| 1847 | msg += 1 + 1 + 4 + 28; |
| 1848 | if (msg > end) |
| 1849 | return; |
| 1850 | |
| 1851 | /* Next, is session id: |
| 1852 | * if present, we have to jump by length + 1 for the size information |
| 1853 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1854 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1855 | if (msg[0] > 0) |
| 1856 | msg += msg[0]; |
| 1857 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1858 | if (msg > end) |
| 1859 | return; |
| 1860 | |
| 1861 | /* Next two bytes are the ciphersuite length. */ |
| 1862 | if (msg + 2 > end) |
| 1863 | return; |
| 1864 | rec_len = (msg[0] << 8) + msg[1]; |
| 1865 | msg += 2; |
| 1866 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1867 | return; |
| 1868 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1869 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1870 | if (!capture) |
| 1871 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1872 | /* Compute the xxh64 of the ciphersuite. */ |
| 1873 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1874 | |
| 1875 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1876 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1877 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1878 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1879 | |
| 1880 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1883 | /* Callback is called for ssl protocol analyse */ |
| 1884 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1885 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1886 | #ifdef TLS1_RT_HEARTBEAT |
| 1887 | /* test heartbeat received (write_p is set to 0 |
| 1888 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1889 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1890 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1891 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1892 | const unsigned char *p = buf; |
| 1893 | unsigned int payload; |
| 1894 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1895 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1896 | |
| 1897 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1898 | if (*p != TLS1_HB_REQUEST) |
| 1899 | return; |
| 1900 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1901 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1902 | goto kill_it; |
| 1903 | |
| 1904 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1905 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1906 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1907 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1908 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1909 | * advertised payload is larger than the advertised packet |
| 1910 | * length, so we have garbage in the buffer between the |
| 1911 | * payload and the end of the buffer (p+len). We can't know |
| 1912 | * if the SSL stack is patched, and we don't know if we can |
| 1913 | * safely wipe out the area between p+3+len and payload. |
| 1914 | * So instead, we prevent the response from being sent by |
| 1915 | * setting the max_send_fragment to 0 and we report an SSL |
| 1916 | * error, which will kill this connection. It will be reported |
| 1917 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1918 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1919 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1920 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1921 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1922 | return; |
| 1923 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1924 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1925 | if (global_ssl.capture_cipherlist > 0) |
| 1926 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1927 | } |
| 1928 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1929 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1930 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1931 | const unsigned char *in, unsigned int inlen, |
| 1932 | void *arg) |
| 1933 | { |
| 1934 | struct server *srv = arg; |
| 1935 | |
| 1936 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1937 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1938 | return SSL_TLSEXT_ERR_OK; |
| 1939 | return SSL_TLSEXT_ERR_NOACK; |
| 1940 | } |
| 1941 | #endif |
| 1942 | |
| 1943 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1944 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1945 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1946 | */ |
| 1947 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1948 | unsigned int *len, void *arg) |
| 1949 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1950 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1951 | |
| 1952 | *data = (const unsigned char *)conf->npn_str; |
| 1953 | *len = conf->npn_len; |
| 1954 | return SSL_TLSEXT_ERR_OK; |
| 1955 | } |
| 1956 | #endif |
| 1957 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1958 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1959 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1960 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1961 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1962 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1963 | unsigned char *outlen, |
| 1964 | const unsigned char *server, |
| 1965 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1966 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1967 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1968 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1969 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1970 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1971 | return SSL_TLSEXT_ERR_NOACK; |
| 1972 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1973 | return SSL_TLSEXT_ERR_OK; |
| 1974 | } |
| 1975 | #endif |
| 1976 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1977 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1978 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1979 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1980 | /* Create a X509 certificate with the specified servername and serial. This |
| 1981 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1982 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1983 | ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1984 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1985 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1986 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1987 | SSL_CTX *ssl_ctx = NULL; |
| 1988 | X509 *newcrt = NULL; |
| 1989 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1990 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1991 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1992 | X509_NAME *name; |
| 1993 | const EVP_MD *digest; |
| 1994 | X509V3_CTX ctx; |
| 1995 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1996 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1997 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1998 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1999 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2000 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 2001 | #else |
| 2002 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 2003 | if (tmp_ssl) |
| 2004 | pkey = SSL_get_privatekey(tmp_ssl); |
| 2005 | #endif |
| 2006 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2007 | goto mkcert_error; |
| 2008 | |
| 2009 | /* Create the certificate */ |
| 2010 | if (!(newcrt = X509_new())) |
| 2011 | goto mkcert_error; |
| 2012 | |
| 2013 | /* Set version number for the certificate (X509v3) and the serial |
| 2014 | * number */ |
| 2015 | if (X509_set_version(newcrt, 2L) != 1) |
| 2016 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 2017 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2018 | |
| 2019 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 2020 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 2021 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2022 | goto mkcert_error; |
| 2023 | |
| 2024 | /* set public key in the certificate */ |
| 2025 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 2026 | goto mkcert_error; |
| 2027 | |
| 2028 | /* Set issuer name from the CA */ |
| 2029 | if (!(name = X509_get_subject_name(cacert))) |
| 2030 | goto mkcert_error; |
| 2031 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 2032 | goto mkcert_error; |
| 2033 | |
| 2034 | /* Set the subject name using the same, but the CN */ |
| 2035 | name = X509_NAME_dup(name); |
| 2036 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 2037 | (const unsigned char *)servername, |
| 2038 | -1, -1, 0) != 1) { |
| 2039 | X509_NAME_free(name); |
| 2040 | goto mkcert_error; |
| 2041 | } |
| 2042 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 2043 | X509_NAME_free(name); |
| 2044 | goto mkcert_error; |
| 2045 | } |
| 2046 | X509_NAME_free(name); |
| 2047 | |
| 2048 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2049 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2050 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 2051 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 2052 | X509_EXTENSION *ext; |
| 2053 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2054 | if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2055 | goto mkcert_error; |
| 2056 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2057 | X509_EXTENSION_free(ext); |
| 2058 | goto mkcert_error; |
| 2059 | } |
| 2060 | X509_EXTENSION_free(ext); |
| 2061 | } |
| 2062 | |
| 2063 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2064 | |
| 2065 | key_type = EVP_PKEY_base_id(capkey); |
| 2066 | |
| 2067 | if (key_type == EVP_PKEY_DSA) |
| 2068 | digest = EVP_sha1(); |
| 2069 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2070 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2071 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2072 | digest = EVP_sha256(); |
| 2073 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2074 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2075 | int nid; |
| 2076 | |
| 2077 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2078 | goto mkcert_error; |
| 2079 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2080 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2081 | #else |
| 2082 | goto mkcert_error; |
| 2083 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2086 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2087 | goto mkcert_error; |
| 2088 | |
| 2089 | /* Create and set the new SSL_CTX */ |
| 2090 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2091 | goto mkcert_error; |
| 2092 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2093 | goto mkcert_error; |
| 2094 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2095 | goto mkcert_error; |
| 2096 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2097 | goto mkcert_error; |
| 2098 | |
| 2099 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2100 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2101 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2102 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2103 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2104 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2105 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2106 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2107 | EC_KEY *ecc; |
| 2108 | int nid; |
| 2109 | |
| 2110 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2111 | goto end; |
| 2112 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2113 | goto end; |
| 2114 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2115 | EC_KEY_free(ecc); |
| 2116 | } |
| 2117 | #endif |
| 2118 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2119 | return ssl_ctx; |
| 2120 | |
| 2121 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2122 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2123 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2124 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2125 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2126 | return NULL; |
| 2127 | } |
| 2128 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2129 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2130 | ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2131 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2132 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2133 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2134 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2135 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2138 | /* Do a lookup for a certificate in the LRU cache used to store generated |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2139 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2140 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2141 | ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2142 | { |
| 2143 | struct lru64 *lru = NULL; |
| 2144 | |
| 2145 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2146 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2147 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2148 | if (lru && lru->domain) { |
| 2149 | if (ssl) |
| 2150 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2151 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2152 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2153 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2154 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2155 | } |
| 2156 | return NULL; |
| 2157 | } |
| 2158 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2159 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2160 | * function is not thread-safe, it should only be used to check if a certificate |
| 2161 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2162 | * thread). It is kept for backward compatibility. */ |
| 2163 | SSL_CTX * |
| 2164 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2165 | { |
| 2166 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2167 | } |
| 2168 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2169 | /* Set a certificate int the LRU cache used to store generated |
| 2170 | * certificate. Return 0 on success, otherwise -1 */ |
| 2171 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2172 | ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2173 | { |
| 2174 | struct lru64 *lru = NULL; |
| 2175 | |
| 2176 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2177 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2178 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2179 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2180 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2181 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2182 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2183 | if (lru->domain && lru->data) |
| 2184 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2185 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2186 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2187 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2188 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2189 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2192 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2193 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2194 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2195 | { |
| 2196 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2197 | } |
| 2198 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2199 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2200 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2201 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2202 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2203 | ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2204 | { |
| 2205 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2206 | SSL_CTX *ssl_ctx = NULL; |
| 2207 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2208 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2209 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2210 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2211 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2212 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2213 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2214 | if (lru && lru->domain) |
| 2215 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2216 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2217 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2218 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2219 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2220 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2221 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2222 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2223 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2224 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2225 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2226 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2227 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2228 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2229 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2230 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | static int |
| 2234 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2235 | { |
| 2236 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2237 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2238 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2239 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2240 | key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2241 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2242 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2243 | } |
| 2244 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2245 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2246 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2247 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2248 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2249 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2250 | |
| 2251 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2252 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2253 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2254 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2255 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2256 | #endif |
| 2257 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2258 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2259 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2260 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2261 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2262 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2263 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2264 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2265 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2266 | #endif |
| 2267 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2268 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2269 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2270 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2271 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2272 | #endif |
| 2273 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2274 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2275 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2276 | /* Unusable in this context. */ |
| 2277 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2278 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2279 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2280 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2281 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2282 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2283 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2284 | |
| 2285 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2286 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2287 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2288 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2289 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2290 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2291 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2292 | } |
| 2293 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2294 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2295 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2296 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2297 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2298 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2299 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2300 | } |
| 2301 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2302 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2303 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2304 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2305 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2306 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2307 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2308 | } |
| 2309 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2310 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2311 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2312 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2313 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2314 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2315 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2316 | } |
| 2317 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2318 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2319 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2320 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2321 | #endif |
| 2322 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2323 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2324 | #if SSL_OP_NO_TLSv1_3 |
| 2325 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2326 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2327 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2328 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2329 | #endif |
| 2330 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2331 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2332 | |
| 2333 | static struct { |
| 2334 | int option; |
| 2335 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2336 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2337 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2338 | const char *name; |
| 2339 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2340 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2341 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2342 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2343 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2344 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2345 | {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2346 | }; |
| 2347 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2348 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2349 | { |
| 2350 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2351 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2352 | SSL_set_SSL_CTX(ssl, ctx); |
| 2353 | } |
| 2354 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2355 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | |
| 2357 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2358 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2359 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2360 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2362 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2363 | return SSL_TLSEXT_ERR_OK; |
| 2364 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2367 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2368 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2369 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2370 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2371 | #else |
| 2372 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2373 | { |
| 2374 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2375 | struct connection *conn; |
| 2376 | struct bind_conf *s; |
| 2377 | const uint8_t *extension_data; |
| 2378 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2379 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2380 | |
| 2381 | char *wildp = NULL; |
| 2382 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2383 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2384 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2385 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2386 | int i; |
| 2387 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2388 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2389 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2391 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2392 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2393 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2394 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2395 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2396 | #else |
| 2397 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2398 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2399 | /* |
| 2400 | * The server_name extension was given too much extensibility when it |
| 2401 | * was written, so parsing the normal case is a bit complex. |
| 2402 | */ |
| 2403 | size_t len; |
| 2404 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2405 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2406 | /* Extract the length of the supplied list of names. */ |
| 2407 | len = (*extension_data++) << 8; |
| 2408 | len |= *extension_data++; |
| 2409 | if (len + 2 != extension_len) |
| 2410 | goto abort; |
| 2411 | /* |
| 2412 | * The list in practice only has a single element, so we only consider |
| 2413 | * the first one. |
| 2414 | */ |
| 2415 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2416 | goto abort; |
| 2417 | extension_len = len - 1; |
| 2418 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2419 | if (extension_len <= 2) |
| 2420 | goto abort; |
| 2421 | len = (*extension_data++) << 8; |
| 2422 | len |= *extension_data++; |
| 2423 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2424 | || memchr(extension_data, 0, len) != NULL) |
| 2425 | goto abort; |
| 2426 | servername = extension_data; |
| 2427 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2428 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2429 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2430 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2431 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2432 | } |
| 2433 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2434 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2435 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2436 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2437 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2438 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2439 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2440 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2441 | goto abort; |
| 2442 | } |
| 2443 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2444 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2445 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2446 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2447 | #else |
| 2448 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2449 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2450 | uint8_t sign; |
| 2451 | size_t len; |
| 2452 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2453 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2454 | len = (*extension_data++) << 8; |
| 2455 | len |= *extension_data++; |
| 2456 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2457 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2458 | if (len % 2 != 0) |
| 2459 | goto abort; |
| 2460 | for (; len > 0; len -= 2) { |
| 2461 | extension_data++; /* hash */ |
| 2462 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2463 | switch (sign) { |
| 2464 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2465 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | case TLSEXT_signature_ecdsa: |
| 2468 | has_ecdsa_sig = 1; |
| 2469 | break; |
| 2470 | default: |
| 2471 | continue; |
| 2472 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2473 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2474 | break; |
| 2475 | } |
| 2476 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2477 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2478 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2479 | } |
| 2480 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2481 | const SSL_CIPHER *cipher; |
| 2482 | size_t len; |
| 2483 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2484 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2485 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2486 | len = ctx->cipher_suites_len; |
| 2487 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2488 | #else |
| 2489 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2490 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2491 | if (len % 2 != 0) |
| 2492 | goto abort; |
| 2493 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2494 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2495 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2496 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2497 | #else |
| 2498 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2499 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2500 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2501 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2502 | break; |
| 2503 | } |
| 2504 | } |
| 2505 | } |
| 2506 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2507 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2508 | trash.area[i] = tolower(servername[i]); |
| 2509 | if (!wildp && (trash.area[i] == '.')) |
| 2510 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2511 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2512 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2513 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2514 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2515 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2516 | for (i = 0; i < 2; i++) { |
| 2517 | if (i == 0) /* lookup in full qualified names */ |
| 2518 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2519 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2520 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2521 | else |
| 2522 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2523 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2524 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2525 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2526 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2527 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2528 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2530 | break; |
| 2531 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2532 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2533 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2534 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2535 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2536 | if (!node_anonymous) |
| 2537 | node_anonymous = n; |
| 2538 | break; |
| 2539 | } |
| 2540 | } |
| 2541 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2542 | /* select by key_signature priority order */ |
| 2543 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2544 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2545 | : (node_anonymous ? node_anonymous |
| 2546 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2547 | : node_rsa /* no rsa signature case (far far away) */ |
| 2548 | ))); |
| 2549 | if (node) { |
| 2550 | /* switch ctx */ |
| 2551 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2552 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2553 | if (conf) { |
| 2554 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2555 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2556 | if (conf->early_data) |
| 2557 | allow_early = 1; |
| 2558 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2559 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2560 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2561 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2562 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2563 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2564 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2565 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2566 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2567 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2568 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2569 | } |
| 2570 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2571 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2572 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2573 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2574 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2575 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2576 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2577 | allow_early: |
| 2578 | #ifdef OPENSSL_IS_BORINGSSL |
| 2579 | if (allow_early) |
| 2580 | SSL_set_early_data_enabled(ssl, 1); |
| 2581 | #else |
| 2582 | if (!allow_early) |
| 2583 | SSL_set_max_early_data(ssl, 0); |
| 2584 | #endif |
| 2585 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2586 | abort: |
| 2587 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2588 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2589 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2590 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2591 | #else |
| 2592 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2593 | return 0; |
| 2594 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2598 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2599 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2600 | * warning when no match is found, which implies the default (first) cert |
| 2601 | * will keep being used. |
| 2602 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2603 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2604 | { |
| 2605 | const char *servername; |
| 2606 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2607 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2608 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2609 | int i; |
| 2610 | (void)al; /* shut gcc stupid warning */ |
| 2611 | |
| 2612 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2613 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2614 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2615 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2616 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2617 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2618 | if (s->strict_sni) |
| 2619 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2620 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2621 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2622 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2623 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2624 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2625 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2626 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2627 | if (!servername[i]) |
| 2628 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2629 | trash.area[i] = tolower(servername[i]); |
| 2630 | if (!wildp && (trash.area[i] == '.')) |
| 2631 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2632 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2633 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2634 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2635 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2636 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2637 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2638 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2639 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2640 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2641 | node = n; |
| 2642 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2643 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2644 | } |
| 2645 | if (!node && wildp) { |
| 2646 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2647 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2648 | /* lookup a not neg filter */ |
| 2649 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2650 | node = n; |
| 2651 | break; |
| 2652 | } |
| 2653 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2654 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2655 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2656 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2657 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2658 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2659 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2660 | return SSL_TLSEXT_ERR_OK; |
| 2661 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2662 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2663 | if (s->strict_sni) { |
| 2664 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2665 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2666 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2667 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2668 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2669 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2673 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2674 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2675 | return SSL_TLSEXT_ERR_OK; |
| 2676 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2677 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2678 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2679 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2680 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2681 | |
| 2682 | static DH * ssl_get_dh_1024(void) |
| 2683 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2684 | static unsigned char dh1024_p[]={ |
| 2685 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2686 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2687 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2688 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2689 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2690 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2691 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2692 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2693 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2694 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2695 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2696 | }; |
| 2697 | static unsigned char dh1024_g[]={ |
| 2698 | 0x02, |
| 2699 | }; |
| 2700 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2701 | BIGNUM *p; |
| 2702 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2703 | DH *dh = DH_new(); |
| 2704 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2705 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2706 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2707 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2708 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2709 | DH_free(dh); |
| 2710 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2711 | } else { |
| 2712 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | return dh; |
| 2716 | } |
| 2717 | |
| 2718 | static DH *ssl_get_dh_2048(void) |
| 2719 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2720 | static unsigned char dh2048_p[]={ |
| 2721 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2722 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2723 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2724 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2725 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2726 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2727 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2728 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2729 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2730 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2731 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2732 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2733 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2734 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2735 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2736 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2737 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2738 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2739 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2740 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2741 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2742 | 0xB7,0x1F,0x77,0xF3, |
| 2743 | }; |
| 2744 | static unsigned char dh2048_g[]={ |
| 2745 | 0x02, |
| 2746 | }; |
| 2747 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2748 | BIGNUM *p; |
| 2749 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2750 | DH *dh = DH_new(); |
| 2751 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2752 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2753 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2754 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2755 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2756 | DH_free(dh); |
| 2757 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2758 | } else { |
| 2759 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | return dh; |
| 2763 | } |
| 2764 | |
| 2765 | static DH *ssl_get_dh_4096(void) |
| 2766 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2767 | static unsigned char dh4096_p[]={ |
| 2768 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2769 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2770 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2771 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2772 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2773 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2774 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2775 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2776 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2777 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2778 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2779 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2780 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2781 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2782 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2783 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2784 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2785 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2786 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2787 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2788 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2789 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2790 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2791 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2792 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2793 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2794 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2795 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2796 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2797 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2798 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2799 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2800 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2801 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2802 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2803 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2804 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2805 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2806 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2807 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2808 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2809 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2810 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2811 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2812 | static unsigned char dh4096_g[]={ |
| 2813 | 0x02, |
| 2814 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2815 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2816 | BIGNUM *p; |
| 2817 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2818 | DH *dh = DH_new(); |
| 2819 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2820 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2821 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2822 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2823 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2824 | DH_free(dh); |
| 2825 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2826 | } else { |
| 2827 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | return dh; |
| 2831 | } |
| 2832 | |
| 2833 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2834 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2835 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2836 | { |
| 2837 | DH *dh = NULL; |
| 2838 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2839 | int type; |
| 2840 | |
| 2841 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2842 | |
| 2843 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2844 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2845 | */ |
| 2846 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2847 | keylen = EVP_PKEY_bits(pkey); |
| 2848 | } |
| 2849 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2850 | if (keylen > global_ssl.default_dh_param) { |
| 2851 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2854 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2855 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2856 | } |
| 2857 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2858 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2859 | } |
| 2860 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2861 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | return dh; |
| 2865 | } |
| 2866 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2867 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2868 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2869 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2870 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2871 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2872 | if (in == NULL) |
| 2873 | goto end; |
| 2874 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2875 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2876 | goto end; |
| 2877 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2878 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2879 | |
| 2880 | end: |
| 2881 | if (in) |
| 2882 | BIO_free(in); |
| 2883 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2884 | ERR_clear_error(); |
| 2885 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2886 | return dh; |
| 2887 | } |
| 2888 | |
| 2889 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2890 | { |
| 2891 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2892 | |
| 2893 | if (global_dh) { |
| 2894 | return 0; |
| 2895 | } |
| 2896 | |
| 2897 | return -1; |
| 2898 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2899 | #endif |
| 2900 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 2901 | /* release ssl bind conf */ |
| 2902 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
| 2903 | { |
| 2904 | if (conf) { |
| 2905 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 2906 | free(conf->npn_str); |
| 2907 | conf->npn_str = NULL; |
| 2908 | #endif |
| 2909 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 2910 | free(conf->alpn_str); |
| 2911 | conf->alpn_str = NULL; |
| 2912 | #endif |
| 2913 | free(conf->ca_file); |
| 2914 | conf->ca_file = NULL; |
| 2915 | free(conf->ca_verify_file); |
| 2916 | conf->ca_verify_file = NULL; |
| 2917 | free(conf->crl_file); |
| 2918 | conf->crl_file = NULL; |
| 2919 | free(conf->ciphers); |
| 2920 | conf->ciphers = NULL; |
| 2921 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2922 | free(conf->ciphersuites); |
| 2923 | conf->ciphersuites = NULL; |
| 2924 | #endif |
| 2925 | free(conf->curves); |
| 2926 | conf->curves = NULL; |
| 2927 | free(conf->ecdhe); |
| 2928 | conf->ecdhe = NULL; |
| 2929 | } |
| 2930 | } |
| 2931 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 2932 | /* unlink a ckch_inst, free all SNIs, free the ckch_inst */ |
| 2933 | /* The caller must use the lock of the bind_conf if used with inserted SNIs */ |
| 2934 | static void ckch_inst_free(struct ckch_inst *inst) |
| 2935 | { |
| 2936 | struct sni_ctx *sni, *sni_s; |
| 2937 | |
| 2938 | if (inst == NULL) |
| 2939 | return; |
| 2940 | |
| 2941 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 2942 | SSL_CTX_free(sni->ctx); |
| 2943 | LIST_DEL(&sni->by_ckch_inst); |
| 2944 | ebmb_delete(&sni->name); |
| 2945 | free(sni); |
| 2946 | } |
| 2947 | LIST_DEL(&inst->by_ckchs); |
| 2948 | LIST_DEL(&inst->by_crtlist_entry); |
| 2949 | free(inst); |
| 2950 | } |
| 2951 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2952 | /* Alloc and init a ckch_inst */ |
| 2953 | static struct ckch_inst *ckch_inst_new() |
| 2954 | { |
| 2955 | struct ckch_inst *ckch_inst; |
| 2956 | |
| 2957 | ckch_inst = calloc(1, sizeof *ckch_inst); |
William Lallemand | 9cef2e2 | 2020-04-09 16:25:10 +0200 | [diff] [blame] | 2958 | if (!ckch_inst) |
| 2959 | return NULL; |
| 2960 | |
| 2961 | LIST_INIT(&ckch_inst->sni_ctx); |
| 2962 | LIST_INIT(&ckch_inst->by_ckchs); |
| 2963 | LIST_INIT(&ckch_inst->by_crtlist_entry); |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2964 | |
| 2965 | return ckch_inst; |
| 2966 | } |
| 2967 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 2968 | /* free sni filters */ |
| 2969 | static void crtlist_free_filters(char **args) |
| 2970 | { |
| 2971 | int i; |
| 2972 | |
| 2973 | if (!args) |
| 2974 | return; |
| 2975 | |
| 2976 | for (i = 0; args[i]; i++) |
| 2977 | free(args[i]); |
| 2978 | |
| 2979 | free(args); |
| 2980 | } |
| 2981 | |
| 2982 | /* Alloc and duplicate a char ** array */ |
| 2983 | static char **crtlist_dup_filters(char **args, int fcount) |
| 2984 | { |
| 2985 | char **dst; |
| 2986 | int i; |
| 2987 | |
| 2988 | if (fcount == 0) |
| 2989 | return NULL; |
| 2990 | |
| 2991 | dst = calloc(fcount + 1, sizeof(*dst)); |
| 2992 | if (!dst) |
| 2993 | return NULL; |
| 2994 | |
| 2995 | for (i = 0; i < fcount; i++) { |
| 2996 | dst[i] = strdup(args[i]); |
| 2997 | if (!dst[i]) |
| 2998 | goto error; |
| 2999 | } |
| 3000 | return dst; |
| 3001 | |
| 3002 | error: |
| 3003 | crtlist_free_filters(dst); |
| 3004 | return NULL; |
| 3005 | } |
| 3006 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 3007 | /* |
| 3008 | * Detach and free a crtlist_entry. |
| 3009 | * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst |
| 3010 | */ |
| 3011 | static void crtlist_entry_free(struct crtlist_entry *entry) |
| 3012 | { |
| 3013 | struct ckch_inst *inst, *inst_s; |
| 3014 | |
| 3015 | if (entry == NULL) |
| 3016 | return; |
| 3017 | |
| 3018 | ebpt_delete(&entry->node); |
| 3019 | LIST_DEL(&entry->by_crtlist); |
| 3020 | LIST_DEL(&entry->by_ckch_store); |
| 3021 | crtlist_free_filters(entry->filters); |
| 3022 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 3023 | free(entry->ssl_conf); |
| 3024 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 3025 | ckch_inst_free(inst); |
| 3026 | } |
| 3027 | free(entry); |
| 3028 | } |
| 3029 | |
| 3030 | /* |
| 3031 | * Allocate and initialize a crtlist_entry |
| 3032 | */ |
| 3033 | static struct crtlist_entry *crtlist_entry_new() |
| 3034 | { |
| 3035 | struct crtlist_entry *entry; |
| 3036 | |
| 3037 | entry = calloc(1, sizeof(*entry)); |
| 3038 | if (entry == NULL) |
| 3039 | return NULL; |
| 3040 | |
| 3041 | LIST_INIT(&entry->ckch_inst); |
| 3042 | |
| 3043 | /* initialize the nodes so we can LIST_DEL in any cases */ |
| 3044 | LIST_INIT(&entry->by_crtlist); |
| 3045 | LIST_INIT(&entry->by_ckch_store); |
| 3046 | |
| 3047 | return entry; |
| 3048 | } |
| 3049 | |
William Lallemand | 82b21bb | 2020-04-10 10:26:27 +0200 | [diff] [blame] | 3050 | /* Free a crtlist, from the crt_entry to the content of the ssl_conf */ |
| 3051 | static void crtlist_free(struct crtlist *crtlist) |
| 3052 | { |
| 3053 | struct crtlist_entry *entry, *s_entry; |
| 3054 | |
| 3055 | if (crtlist == NULL) |
| 3056 | return; |
| 3057 | |
| 3058 | list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) { |
| 3059 | crtlist_entry_free(entry); |
| 3060 | } |
| 3061 | ebmb_delete(&crtlist->node); |
| 3062 | free(crtlist); |
| 3063 | } |
| 3064 | |
| 3065 | /* Alloc and initialize a struct crtlist |
| 3066 | * <filename> is the key of the ebmb_node |
| 3067 | * <unique> initialize the list of entries to be unique (1) or not (0) |
| 3068 | */ |
| 3069 | static struct crtlist *crtlist_new(const char *filename, int unique) |
| 3070 | { |
| 3071 | struct crtlist *newlist; |
| 3072 | |
| 3073 | newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1); |
| 3074 | if (newlist == NULL) |
| 3075 | return NULL; |
| 3076 | |
| 3077 | memcpy(newlist->node.key, filename, strlen(filename) + 1); |
| 3078 | if (unique) |
| 3079 | newlist->entries = EB_ROOT_UNIQUE; |
| 3080 | else |
| 3081 | newlist->entries = EB_ROOT; |
| 3082 | |
| 3083 | LIST_INIT(&newlist->ord_entries); |
| 3084 | |
| 3085 | return newlist; |
| 3086 | } |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3087 | |
| 3088 | /* 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] | 3089 | 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] | 3090 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3091 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3092 | { |
| 3093 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3094 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3095 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3096 | if (*name == '!') { |
| 3097 | neg = 1; |
| 3098 | name++; |
| 3099 | } |
| 3100 | if (*name == '*') { |
| 3101 | wild = 1; |
| 3102 | name++; |
| 3103 | } |
| 3104 | /* !* filter is a nop */ |
| 3105 | if (neg && wild) |
| 3106 | return order; |
| 3107 | if (*name) { |
| 3108 | int j, len; |
| 3109 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3110 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3111 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3112 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3113 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3114 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3115 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3116 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3117 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3118 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3119 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3120 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3121 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3122 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3123 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3124 | sc->order = order++; |
| 3125 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3126 | sc->wild = wild; |
| 3127 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3128 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3129 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3130 | } |
| 3131 | return order; |
| 3132 | } |
| 3133 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3134 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3135 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3136 | * This function can't return an error. |
| 3137 | * |
| 3138 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3139 | */ |
| 3140 | static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
| 3141 | { |
| 3142 | |
| 3143 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3144 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3145 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3146 | |
| 3147 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3148 | |
| 3149 | /* ignore if sc0 was already inserted in a tree */ |
| 3150 | if (sc0->name.node.leaf_p) |
| 3151 | continue; |
| 3152 | |
| 3153 | /* Check for duplicates. */ |
| 3154 | if (sc0->wild) |
| 3155 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3156 | else |
| 3157 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3158 | |
| 3159 | for (; node; node = ebmb_next_dup(node)) { |
| 3160 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3161 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3162 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3163 | /* it's a duplicate, we should remove and free it */ |
| 3164 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3165 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3166 | free(sc0); |
| 3167 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3168 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* if duplicate, ignore the insertion */ |
| 3173 | if (!sc0) |
| 3174 | continue; |
| 3175 | |
| 3176 | if (sc0->wild) |
| 3177 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3178 | else |
| 3179 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3180 | |
| 3181 | /* replace the default_ctx if required with the first ctx */ |
| 3182 | if (ckch_inst->is_default && !def) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3183 | SSL_CTX_free(bind_conf->default_ctx); |
| 3184 | SSL_CTX_up_ref(sc0->ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3185 | bind_conf->default_ctx = sc0->ctx; |
| 3186 | def = 1; |
| 3187 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3192 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3193 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3194 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3195 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3196 | /* tree of crtlist (crt-list/directory) */ |
| 3197 | static struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3198 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3199 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3200 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3201 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3202 | * DH parameter available in ckchs nor in global, the default |
| 3203 | * DH parameters are applied on the SSL_CTX. |
| 3204 | * Returns a bitfield containing the flags: |
| 3205 | * ERR_FATAL in any fatal error case |
| 3206 | * ERR_ALERT if a reason of the error is availabine in err |
| 3207 | * ERR_WARN if a warning is available into err |
| 3208 | * The value 0 means there is no error nor warning and |
| 3209 | * the operation succeed. |
| 3210 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3211 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3212 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 3213 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3214 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3215 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3216 | DH *dh = NULL; |
| 3217 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 3218 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3219 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3220 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 3221 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3222 | err && *err ? *err : "", path); |
| 3223 | #if defined(SSL_CTX_set_dh_auto) |
| 3224 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3225 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3226 | err && *err ? *err : ""); |
| 3227 | #else |
| 3228 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3229 | err && *err ? *err : ""); |
| 3230 | #endif |
| 3231 | ret |= ERR_WARN; |
| 3232 | goto end; |
| 3233 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3234 | |
| 3235 | if (ssl_dh_ptr_index >= 0) { |
| 3236 | /* store a pointer to the DH params to avoid complaining about |
| 3237 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3238 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3239 | } |
| 3240 | } |
| 3241 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3242 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 3243 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3244 | err && *err ? *err : "", path); |
| 3245 | #if defined(SSL_CTX_set_dh_auto) |
| 3246 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3247 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3248 | err && *err ? *err : ""); |
| 3249 | #else |
| 3250 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3251 | err && *err ? *err : ""); |
| 3252 | #endif |
| 3253 | ret |= ERR_WARN; |
| 3254 | goto end; |
| 3255 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3256 | } |
| 3257 | else { |
| 3258 | /* Clear openssl global errors stack */ |
| 3259 | ERR_clear_error(); |
| 3260 | |
| 3261 | if (global_ssl.default_dh_param <= 1024) { |
| 3262 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3263 | if (local_dh_1024 == NULL) |
| 3264 | local_dh_1024 = ssl_get_dh_1024(); |
| 3265 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3266 | if (local_dh_1024 == NULL) { |
| 3267 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3268 | err && *err ? *err : "", path); |
| 3269 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3270 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3271 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3272 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3273 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3274 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3275 | err && *err ? *err : "", path); |
| 3276 | #if defined(SSL_CTX_set_dh_auto) |
| 3277 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3278 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3279 | err && *err ? *err : ""); |
| 3280 | #else |
| 3281 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3282 | err && *err ? *err : ""); |
| 3283 | #endif |
| 3284 | ret |= ERR_WARN; |
| 3285 | goto end; |
| 3286 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3287 | } |
| 3288 | else { |
| 3289 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3290 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | end: |
William Lallemand | 4dd145a | 2020-02-05 11:46:33 +0100 | [diff] [blame] | 3294 | ERR_clear_error(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3295 | return ret; |
| 3296 | } |
| 3297 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3298 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3299 | /* Frees the contents of a cert_key_and_chain |
| 3300 | */ |
| 3301 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 3302 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3303 | if (!ckch) |
| 3304 | return; |
| 3305 | |
| 3306 | /* Free the certificate and set pointer to NULL */ |
| 3307 | if (ckch->cert) |
| 3308 | X509_free(ckch->cert); |
| 3309 | ckch->cert = NULL; |
| 3310 | |
| 3311 | /* Free the key and set pointer to NULL */ |
| 3312 | if (ckch->key) |
| 3313 | EVP_PKEY_free(ckch->key); |
| 3314 | ckch->key = NULL; |
| 3315 | |
| 3316 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3317 | if (ckch->chain) |
| 3318 | sk_X509_pop_free(ckch->chain, X509_free); |
| 3319 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3320 | |
William Lallemand | 455af50 | 2019-10-17 18:04:45 +0200 | [diff] [blame] | 3321 | if (ckch->dh) |
| 3322 | DH_free(ckch->dh); |
| 3323 | ckch->dh = NULL; |
| 3324 | |
| 3325 | if (ckch->sctl) { |
| 3326 | free(ckch->sctl->area); |
| 3327 | ckch->sctl->area = NULL; |
| 3328 | free(ckch->sctl); |
| 3329 | ckch->sctl = NULL; |
| 3330 | } |
| 3331 | |
| 3332 | if (ckch->ocsp_response) { |
| 3333 | free(ckch->ocsp_response->area); |
| 3334 | ckch->ocsp_response->area = NULL; |
| 3335 | free(ckch->ocsp_response); |
| 3336 | ckch->ocsp_response = NULL; |
| 3337 | } |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3338 | |
| 3339 | if (ckch->ocsp_issuer) |
William Lallemand | dad239d | 2020-01-23 11:59:02 +0100 | [diff] [blame] | 3340 | X509_free(ckch->ocsp_issuer); |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3341 | ckch->ocsp_issuer = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3342 | } |
| 3343 | |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3344 | /* |
| 3345 | * |
| 3346 | * This function copy a cert_key_and_chain in memory |
| 3347 | * |
| 3348 | * It's used to try to apply changes on a ckch before committing them, because |
| 3349 | * most of the time it's not possible to revert those changes |
| 3350 | * |
| 3351 | * Return a the dst or NULL |
| 3352 | */ |
| 3353 | static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 3354 | struct cert_key_and_chain *dst) |
| 3355 | { |
| 3356 | if (src->cert) { |
| 3357 | dst->cert = src->cert; |
| 3358 | X509_up_ref(src->cert); |
| 3359 | } |
| 3360 | |
| 3361 | if (src->key) { |
| 3362 | dst->key = src->key; |
| 3363 | EVP_PKEY_up_ref(src->key); |
| 3364 | } |
| 3365 | |
| 3366 | if (src->chain) { |
| 3367 | dst->chain = X509_chain_up_ref(src->chain); |
| 3368 | } |
| 3369 | |
| 3370 | if (src->dh) { |
| 3371 | DH_up_ref(src->dh); |
| 3372 | dst->dh = src->dh; |
| 3373 | } |
| 3374 | |
| 3375 | if (src->sctl) { |
| 3376 | struct buffer *sctl; |
| 3377 | |
| 3378 | sctl = calloc(1, sizeof(*sctl)); |
| 3379 | if (!chunk_dup(sctl, src->sctl)) { |
| 3380 | free(sctl); |
| 3381 | sctl = NULL; |
| 3382 | goto error; |
| 3383 | } |
| 3384 | dst->sctl = sctl; |
| 3385 | } |
| 3386 | |
| 3387 | if (src->ocsp_response) { |
| 3388 | struct buffer *ocsp_response; |
| 3389 | |
| 3390 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 3391 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 3392 | free(ocsp_response); |
| 3393 | ocsp_response = NULL; |
| 3394 | goto error; |
| 3395 | } |
| 3396 | dst->ocsp_response = ocsp_response; |
| 3397 | } |
| 3398 | |
| 3399 | if (src->ocsp_issuer) { |
| 3400 | X509_up_ref(src->ocsp_issuer); |
| 3401 | dst->ocsp_issuer = src->ocsp_issuer; |
| 3402 | } |
| 3403 | |
| 3404 | return dst; |
| 3405 | |
| 3406 | error: |
| 3407 | |
| 3408 | /* free everything */ |
| 3409 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 3410 | |
| 3411 | return NULL; |
| 3412 | } |
| 3413 | |
| 3414 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3415 | /* checks if a key and cert exists in the ckch |
| 3416 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3417 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3418 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 3419 | { |
| 3420 | return (ckch->cert != NULL && ckch->key != NULL); |
| 3421 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3422 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3423 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3424 | /* |
| 3425 | * return 0 on success or != 0 on failure |
| 3426 | */ |
| 3427 | static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 3428 | { |
| 3429 | int ret = 1; |
| 3430 | BIO *in = NULL; |
| 3431 | X509 *issuer; |
| 3432 | |
| 3433 | if (buf) { |
| 3434 | /* reading from a buffer */ |
| 3435 | in = BIO_new_mem_buf(buf, -1); |
| 3436 | if (in == NULL) { |
| 3437 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3438 | goto end; |
| 3439 | } |
| 3440 | |
| 3441 | } else { |
| 3442 | /* reading from a file */ |
| 3443 | in = BIO_new(BIO_s_file()); |
| 3444 | if (in == NULL) |
| 3445 | goto end; |
| 3446 | |
| 3447 | if (BIO_read_filename(in, path) <= 0) |
| 3448 | goto end; |
| 3449 | } |
| 3450 | |
| 3451 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3452 | if (!issuer) { |
| 3453 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3454 | err && *err ? *err : "", path); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3455 | goto end; |
| 3456 | } |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3457 | /* no error, fill ckch with new context, old context must be free */ |
| 3458 | if (ckch->ocsp_issuer) |
| 3459 | X509_free(ckch->ocsp_issuer); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3460 | ckch->ocsp_issuer = issuer; |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3461 | ret = 0; |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3462 | |
| 3463 | end: |
| 3464 | |
| 3465 | ERR_clear_error(); |
| 3466 | if (in) |
| 3467 | BIO_free(in); |
| 3468 | |
| 3469 | return ret; |
| 3470 | } |
| 3471 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3472 | |
| 3473 | /* |
| 3474 | * 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] | 3475 | * The PEM must contain at least a Certificate, |
| 3476 | * It could contain a DH, a certificate chain and a PrivateKey. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3477 | * |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3478 | * If it failed you should not attempt to use the ckch but free it. |
| 3479 | * |
| 3480 | * Return 0 on success or != 0 on failure |
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 | 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] | 3483 | { |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3484 | BIO *in = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3485 | int ret = 1; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3486 | X509 *ca; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3487 | X509 *cert = NULL; |
| 3488 | EVP_PKEY *key = NULL; |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3489 | DH *dh = NULL; |
| 3490 | STACK_OF(X509) *chain = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3491 | |
| 3492 | if (buf) { |
| 3493 | /* reading from a buffer */ |
| 3494 | in = BIO_new_mem_buf(buf, -1); |
| 3495 | if (in == NULL) { |
| 3496 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3497 | goto end; |
| 3498 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3499 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3500 | } else { |
| 3501 | /* reading from a file */ |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3502 | in = BIO_new(BIO_s_file()); |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3503 | if (in == NULL) { |
| 3504 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3505 | goto end; |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3506 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3507 | |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3508 | if (BIO_read_filename(in, path) <= 0) { |
| 3509 | memprintf(err, "%scannot open the file '%s'.\n", |
| 3510 | err && *err ? *err : "", path); |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3511 | goto end; |
William Lallemand | 7fd01b3 | 2020-04-07 14:16:32 +0200 | [diff] [blame] | 3512 | } |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3513 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3514 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3515 | /* Read Private Key */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3516 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3517 | /* 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] | 3518 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3519 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3520 | /* Seek back to beginning of file */ |
| 3521 | if (BIO_reset(in) == -1) { |
| 3522 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3523 | err && *err ? *err : "", path); |
| 3524 | goto end; |
| 3525 | } |
| 3526 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3527 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 3528 | /* no need to return an error there, dh is not mandatory */ |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3529 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3530 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3531 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3532 | if (BIO_reset(in) == -1) { |
| 3533 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3534 | err && *err ? *err : "", path); |
| 3535 | goto end; |
| 3536 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3537 | |
| 3538 | /* Read Certificate */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3539 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3540 | if (cert == NULL) { |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3541 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3542 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3543 | goto end; |
| 3544 | } |
| 3545 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3546 | /* Look for a Certificate Chain */ |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3547 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3548 | if (chain == NULL) |
| 3549 | chain = sk_X509_new_null(); |
| 3550 | if (!sk_X509_push(chain, ca)) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3551 | X509_free(ca); |
| 3552 | goto end; |
| 3553 | } |
| 3554 | } |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3555 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3556 | ret = ERR_get_error(); |
| 3557 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3558 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3559 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3560 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3561 | } |
| 3562 | |
William Lallemand | 75b15f7 | 2020-01-23 10:56:05 +0100 | [diff] [blame] | 3563 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 3564 | if (ckch->ocsp_response) { |
| 3565 | free(ckch->ocsp_response->area); |
| 3566 | ckch->ocsp_response->area = NULL; |
| 3567 | free(ckch->ocsp_response); |
| 3568 | ckch->ocsp_response = NULL; |
| 3569 | } |
| 3570 | |
| 3571 | if (ckch->sctl) { |
| 3572 | free(ckch->sctl->area); |
| 3573 | ckch->sctl->area = NULL; |
| 3574 | free(ckch->sctl); |
| 3575 | ckch->sctl = NULL; |
| 3576 | } |
| 3577 | |
| 3578 | if (ckch->ocsp_issuer) { |
| 3579 | X509_free(ckch->ocsp_issuer); |
| 3580 | ckch->ocsp_issuer = NULL; |
| 3581 | } |
| 3582 | |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3583 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 3584 | SWAP(ckch->key, key); |
| 3585 | SWAP(ckch->dh, dh); |
| 3586 | SWAP(ckch->cert, cert); |
| 3587 | SWAP(ckch->chain, chain); |
| 3588 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3589 | ret = 0; |
| 3590 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3591 | end: |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3592 | |
| 3593 | ERR_clear_error(); |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3594 | if (in) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3595 | BIO_free(in); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3596 | if (key) |
| 3597 | EVP_PKEY_free(key); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3598 | if (dh) |
| 3599 | DH_free(dh); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3600 | if (cert) |
| 3601 | X509_free(cert); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3602 | if (chain) |
| 3603 | sk_X509_pop_free(chain, X509_free); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3604 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3605 | return ret; |
| 3606 | } |
| 3607 | |
| 3608 | /* |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3609 | * Try to load a private key file from a <path> or a buffer <buf> |
| 3610 | * |
| 3611 | * If it failed you should not attempt to use the ckch but free it. |
| 3612 | * |
| 3613 | * Return 0 on success or != 0 on failure |
| 3614 | */ |
| 3615 | static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 3616 | { |
| 3617 | BIO *in = NULL; |
| 3618 | int ret = 1; |
| 3619 | EVP_PKEY *key = NULL; |
| 3620 | |
| 3621 | if (buf) { |
| 3622 | /* reading from a buffer */ |
| 3623 | in = BIO_new_mem_buf(buf, -1); |
| 3624 | if (in == NULL) { |
| 3625 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3626 | goto end; |
| 3627 | } |
| 3628 | |
| 3629 | } else { |
| 3630 | /* reading from a file */ |
| 3631 | in = BIO_new(BIO_s_file()); |
| 3632 | if (in == NULL) |
| 3633 | goto end; |
| 3634 | |
| 3635 | if (BIO_read_filename(in, path) <= 0) |
| 3636 | goto end; |
| 3637 | } |
| 3638 | |
| 3639 | /* Read Private Key */ |
| 3640 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3641 | if (key == NULL) { |
| 3642 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3643 | err && *err ? *err : "", path); |
| 3644 | goto end; |
| 3645 | } |
| 3646 | |
| 3647 | ret = 0; |
| 3648 | |
| 3649 | SWAP(ckch->key, key); |
| 3650 | |
| 3651 | end: |
| 3652 | |
| 3653 | ERR_clear_error(); |
| 3654 | if (in) |
| 3655 | BIO_free(in); |
| 3656 | if (key) |
| 3657 | EVP_PKEY_free(key); |
| 3658 | |
| 3659 | return ret; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3663 | * Try to load in a ckch every files related to a ckch. |
| 3664 | * (PEM, sctl, ocsp, issuer etc.) |
| 3665 | * |
| 3666 | * This function is only used to load files during the configuration parsing, |
| 3667 | * it is not used with the CLI. |
| 3668 | * |
| 3669 | * This allows us to carry the contents of the file without having to read the |
| 3670 | * file multiple times. The caller must call |
| 3671 | * ssl_sock_free_cert_key_and_chain_contents. |
| 3672 | * |
| 3673 | * returns: |
| 3674 | * 0 on Success |
| 3675 | * 1 on SSL Failure |
| 3676 | */ |
| 3677 | static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3678 | { |
| 3679 | int ret = 1; |
| 3680 | |
| 3681 | /* try to load the PEM */ |
| 3682 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 3683 | goto end; |
| 3684 | } |
| 3685 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3686 | /* try to load an external private key if it wasn't in the PEM */ |
| 3687 | if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) { |
| 3688 | char fp[MAXPATHLEN+1]; |
| 3689 | struct stat st; |
| 3690 | |
| 3691 | snprintf(fp, MAXPATHLEN+1, "%s.key", path); |
| 3692 | if (stat(fp, &st) == 0) { |
| 3693 | if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) { |
| 3694 | memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n", |
| 3695 | err && *err ? *err : "", fp); |
| 3696 | goto end; |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | if (ckch->key == NULL) { |
| 3702 | memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path); |
| 3703 | goto end; |
| 3704 | } |
| 3705 | |
| 3706 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 3707 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 3708 | err && *err ? *err : "", path); |
| 3709 | goto end; |
| 3710 | } |
| 3711 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3712 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3713 | /* try to load the sctl file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3714 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3715 | char fp[MAXPATHLEN+1]; |
| 3716 | struct stat st; |
| 3717 | |
| 3718 | snprintf(fp, MAXPATHLEN+1, "%s.sctl", path); |
| 3719 | if (stat(fp, &st) == 0) { |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 3720 | if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3721 | 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] | 3722 | err && *err ? *err : "", fp); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3723 | ret = 1; |
| 3724 | goto end; |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3729 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3730 | /* try to load an ocsp response file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3731 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3732 | char fp[MAXPATHLEN+1]; |
| 3733 | struct stat st; |
| 3734 | |
| 3735 | snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path); |
| 3736 | if (stat(fp, &st) == 0) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 3737 | if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3738 | ret = 1; |
| 3739 | goto end; |
| 3740 | } |
| 3741 | } |
| 3742 | } |
| 3743 | |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3744 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3745 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3746 | /* 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] | 3747 | if (!ckch->ocsp_issuer) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3748 | struct stat st; |
| 3749 | char fp[MAXPATHLEN+1]; |
| 3750 | |
| 3751 | snprintf(fp, MAXPATHLEN+1, "%s.issuer", path); |
| 3752 | if (stat(fp, &st) == 0) { |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3753 | if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3754 | ret = 1; |
| 3755 | goto end; |
| 3756 | } |
| 3757 | |
| 3758 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
William Lallemand | 786188f | 2019-10-15 10:05:37 +0200 | [diff] [blame] | 3759 | memprintf(err, "%s '%s' is not an issuer'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3760 | err && *err ? *err : "", fp); |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3761 | ret = 1; |
| 3762 | goto end; |
| 3763 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | } |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3767 | #endif |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3768 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3769 | ret = 0; |
| 3770 | |
| 3771 | end: |
| 3772 | |
| 3773 | ERR_clear_error(); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3774 | |
| 3775 | /* Something went wrong in one of the reads */ |
| 3776 | if (ret != 0) |
| 3777 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3778 | |
| 3779 | return ret; |
| 3780 | } |
| 3781 | |
| 3782 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3783 | * Returns a bitfield containing the flags: |
| 3784 | * ERR_FATAL in any fatal error case |
| 3785 | * ERR_ALERT if the reason of the error is available in err |
| 3786 | * ERR_WARN if a warning is available into err |
| 3787 | * The value 0 means there is no error nor warning and |
| 3788 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3789 | */ |
| 3790 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3791 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3792 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3793 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3794 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3795 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3796 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3797 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3798 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3799 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3803 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3804 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3805 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3806 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3807 | } |
| 3808 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3809 | if (ckch->chain) { |
| 3810 | find_chain = ckch->chain; |
| 3811 | } else { |
| 3812 | /* Find Certificate Chain in global */ |
| 3813 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 3814 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3815 | if (issuer) |
| 3816 | find_chain = issuer->chain; |
| 3817 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3818 | |
Emmanuel Hocdet | 1673977 | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3819 | /* Load all certs from chain, except Root, in the ssl_ctx */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3820 | if (find_chain) { |
| 3821 | int i; |
| 3822 | X509 *ca; |
| 3823 | for (i = 0; i < sk_X509_num(find_chain); i++) { |
| 3824 | ca = sk_X509_value(find_chain, i); |
Emmanuel Hocdet | 1673977 | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3825 | /* skip self issued (Root CA) */ |
| 3826 | if (!X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca))) |
| 3827 | continue; |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3828 | /* |
| 3829 | SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2 |
| 3830 | Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert) |
| 3831 | */ |
| 3832 | X509_up_ref(ca); |
| 3833 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3834 | X509_free(ca); |
| 3835 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3836 | err && *err ? *err : "", path); |
| 3837 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3838 | goto end; |
| 3839 | } |
Emmanuel Hocdet | f4f14ea | 2020-03-23 10:31:47 +0100 | [diff] [blame] | 3840 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3841 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3842 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3843 | #ifndef OPENSSL_NO_DH |
| 3844 | /* store a NULL pointer to indicate we have not yet loaded |
| 3845 | a custom DH param file */ |
| 3846 | if (ssl_dh_ptr_index >= 0) { |
| 3847 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3848 | } |
| 3849 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3850 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3851 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3852 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3853 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3854 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3855 | } |
| 3856 | #endif |
| 3857 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3858 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3859 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3860 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3861 | 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] | 3862 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3863 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3864 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | #endif |
| 3868 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 3869 | #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] | 3870 | /* Load OCSP Info into context */ |
| 3871 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 3872 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3873 | 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", |
| 3874 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3875 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3876 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3877 | } |
| 3878 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3879 | #endif |
| 3880 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3881 | end: |
| 3882 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3883 | } |
| 3884 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3885 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3886 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3887 | 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] | 3888 | { |
| 3889 | struct sni_keytype *s_kt = NULL; |
| 3890 | struct ebmb_node *node; |
| 3891 | int i; |
| 3892 | |
| 3893 | for (i = 0; i < trash.size; i++) { |
| 3894 | if (!str[i]) |
| 3895 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3896 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3897 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3898 | trash.area[i] = 0; |
| 3899 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3900 | if (!node) { |
| 3901 | /* CN not found in tree */ |
| 3902 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3903 | /* Using memcpy here instead of strncpy. |
| 3904 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3905 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3906 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3907 | if (!s_kt) |
| 3908 | return -1; |
| 3909 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3910 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3911 | s_kt->keytypes = 0; |
| 3912 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3913 | } else { |
| 3914 | /* CN found in tree */ |
| 3915 | s_kt = container_of(node, struct sni_keytype, name); |
| 3916 | } |
| 3917 | |
| 3918 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3919 | s_kt->keytypes |= 1<<key_index; |
| 3920 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3921 | return 0; |
| 3922 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3923 | } |
| 3924 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3925 | #endif |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3926 | /* |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3927 | * 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] | 3928 | */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3929 | static void ckch_store_free(struct ckch_store *store) |
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 | struct ckch_inst *inst, *inst_s; |
| 3932 | |
| 3933 | if (!store) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3934 | return; |
| 3935 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3936 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L |
| 3937 | if (store->multi) { |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3938 | int n; |
| 3939 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3940 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3941 | ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3942 | } else |
| 3943 | #endif |
| 3944 | { |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3945 | ssl_sock_free_cert_key_and_chain_contents(store->ckch); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 3948 | free(store->ckch); |
| 3949 | store->ckch = NULL; |
| 3950 | |
| 3951 | list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) { |
| 3952 | ckch_inst_free(inst); |
| 3953 | } |
| 3954 | ebmb_delete(&store->node); |
| 3955 | free(store); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3956 | } |
| 3957 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3958 | /* |
| 3959 | * create and initialize a ckch_store |
| 3960 | * <path> is the key name |
| 3961 | * <nmemb> is the number of store->ckch objects to allocate |
| 3962 | * |
| 3963 | * Return a ckch_store or NULL upon failure. |
| 3964 | */ |
| 3965 | static struct ckch_store *ckch_store_new(const char *filename, int nmemb) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3966 | { |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3967 | struct ckch_store *store; |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3968 | int pathlen; |
| 3969 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3970 | pathlen = strlen(filename); |
| 3971 | store = calloc(1, sizeof(*store) + pathlen + 1); |
| 3972 | if (!store) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3973 | return NULL; |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3974 | |
| 3975 | if (nmemb > 1) |
| 3976 | store->multi = 1; |
| 3977 | else |
| 3978 | store->multi = 0; |
| 3979 | |
| 3980 | memcpy(store->path, filename, pathlen + 1); |
| 3981 | |
| 3982 | LIST_INIT(&store->ckch_inst); |
| 3983 | LIST_INIT(&store->crtlist_entry); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3984 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3985 | store->ckch = calloc(nmemb, sizeof(*store->ckch)); |
| 3986 | if (!store->ckch) |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3987 | goto error; |
| 3988 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 3989 | return store; |
| 3990 | error: |
| 3991 | ckch_store_free(store); |
| 3992 | return NULL; |
| 3993 | } |
| 3994 | |
| 3995 | /* allocate and duplicate a ckch_store |
| 3996 | * Return a new ckch_store or NULL */ |
| 3997 | static struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 3998 | { |
| 3999 | struct ckch_store *dst; |
| 4000 | |
| 4001 | dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1); |
| 4002 | |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 4003 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4004 | if (src->multi) { |
| 4005 | int n; |
| 4006 | |
| 4007 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4008 | if (&src->ckch[n]) { |
| 4009 | if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n])) |
| 4010 | goto error; |
| 4011 | } |
| 4012 | } |
| 4013 | } else |
| 4014 | #endif |
| 4015 | { |
| 4016 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 4017 | goto error; |
| 4018 | } |
| 4019 | |
| 4020 | return dst; |
| 4021 | |
| 4022 | error: |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 4023 | ckch_store_free(dst); |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 4024 | |
| 4025 | return NULL; |
| 4026 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4027 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4028 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4029 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4030 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4031 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4032 | { |
| 4033 | struct ebmb_node *eb; |
| 4034 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4035 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4036 | if (!eb) |
| 4037 | return NULL; |
| 4038 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4039 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4043 | * 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] | 4044 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4045 | 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] | 4046 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4047 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4048 | |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 4049 | ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1); |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4050 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4051 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 4052 | goto end; |
| 4053 | } |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4054 | if (!multi) { |
| 4055 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 4056 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4057 | goto end; |
| 4058 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4059 | /* insert into the ckchs tree */ |
| 4060 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 4061 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4062 | } else { |
| 4063 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4064 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4065 | char fp[MAXPATHLEN+1] = {0}; |
| 4066 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4067 | |
| 4068 | /* Load all possible certs and keys */ |
| 4069 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4070 | struct stat buf; |
| 4071 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 4072 | if (stat(fp, &buf) == 0) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 4073 | 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] | 4074 | goto end; |
| 4075 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4076 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4077 | } |
| 4078 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4079 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4080 | |
| 4081 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 4082 | 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] | 4083 | goto end; |
| 4084 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4085 | /* insert into the ckchs tree */ |
| 4086 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 4087 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4088 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4089 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4090 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4091 | end: |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 4092 | ckch_store_free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4093 | |
| 4094 | return NULL; |
| 4095 | } |
| 4096 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 4097 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4098 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4099 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4100 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4101 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4102 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 4103 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4104 | * 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] | 4105 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4106 | * Returns a bitfield containing the flags: |
| 4107 | * ERR_FATAL in any fatal error case |
| 4108 | * ERR_ALERT if the reason of the error is available in err |
| 4109 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4110 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4111 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4112 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4113 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4114 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4115 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4116 | int i = 0, n = 0; |
| 4117 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 4118 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4119 | struct ebmb_node *node; |
| 4120 | struct ebmb_node *next; |
| 4121 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 4122 | * of keytypes |
| 4123 | */ |
| 4124 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4125 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4126 | X509_NAME *xname = NULL; |
| 4127 | char *str = NULL; |
| 4128 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4129 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 4130 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4131 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4132 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4133 | *ckchi = NULL; |
| 4134 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4135 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4136 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4137 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4138 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | ckch_inst = ckch_inst_new(); |
| 4142 | if (!ckch_inst) { |
| 4143 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4144 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4145 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4146 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4147 | } |
| 4148 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4149 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4150 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4151 | /* Process each ckch and update keytypes for each CN/SAN |
| 4152 | * for example, if CN/SAN www.a.com is associated with |
| 4153 | * certs with keytype 0 and 2, then at the end of the loop, |
| 4154 | * www.a.com will have: |
| 4155 | * keyindex = 0 | 1 | 4 = 5 |
| 4156 | */ |
| 4157 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4158 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4159 | |
| 4160 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 4161 | continue; |
| 4162 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4163 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4164 | for (i = 0; i < fcount; i++) { |
| 4165 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 4166 | if (ret < 0) { |
| 4167 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4168 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4169 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4170 | goto end; |
| 4171 | } |
| 4172 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4173 | } else { |
| 4174 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 4175 | * so the line that contains logic is marked via comments |
| 4176 | */ |
| 4177 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 4178 | i = -1; |
| 4179 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4180 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4181 | ASN1_STRING *value; |
| 4182 | value = X509_NAME_ENTRY_get_data(entry); |
| 4183 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4184 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4185 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4186 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4187 | OPENSSL_free(str); |
| 4188 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4189 | if (ret < 0) { |
| 4190 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4191 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4192 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4193 | goto end; |
| 4194 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4195 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4196 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4197 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4198 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4199 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4200 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 4201 | if (names) { |
| 4202 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4203 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4204 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4205 | if (name->type == GEN_DNS) { |
| 4206 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 4207 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4208 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4209 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4210 | OPENSSL_free(str); |
| 4211 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4212 | if (ret < 0) { |
| 4213 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4214 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4215 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4216 | goto end; |
| 4217 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4218 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | } |
| 4223 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 4224 | } |
| 4225 | |
| 4226 | /* If no files found, return error */ |
| 4227 | if (eb_is_empty(&sni_keytypes_map)) { |
| 4228 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4229 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4230 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4231 | goto end; |
| 4232 | } |
| 4233 | |
| 4234 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 4235 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 4236 | * and add each CTX to the SNI tree |
| 4237 | * |
| 4238 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4239 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4240 | * combination is denoted by the key in the map. Each key |
| 4241 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 4242 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 4243 | * entry in the array to correspond to the unique combo (key) |
| 4244 | * associated with i. This unique key combo (i) will be associated |
| 4245 | * with combos[i-1] |
| 4246 | */ |
| 4247 | |
| 4248 | node = ebmb_first(&sni_keytypes_map); |
| 4249 | while (node) { |
| 4250 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4251 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4252 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4253 | |
| 4254 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 4255 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 4256 | cur_ctx = key_combos[i-1].ctx; |
| 4257 | |
| 4258 | if (cur_ctx == NULL) { |
| 4259 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4260 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4261 | if (cur_ctx == NULL) { |
| 4262 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4263 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4264 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4265 | goto end; |
| 4266 | } |
| 4267 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 4268 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4269 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4270 | if (i & (1<<n)) { |
| 4271 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4272 | 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] | 4273 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 4274 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4275 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4279 | /* Update key_combos */ |
| 4280 | key_combos[i-1].ctx = cur_ctx; |
| 4281 | } |
| 4282 | |
| 4283 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4284 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4285 | 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] | 4286 | kinfo, str, key_combos[i-1].order); |
| 4287 | if (key_combos[i-1].order < 0) { |
| 4288 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4289 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4290 | goto end; |
| 4291 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4292 | node = ebmb_next(node); |
| 4293 | } |
| 4294 | |
| 4295 | |
| 4296 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 4297 | if (!bind_conf->default_ctx) { |
| 4298 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 4299 | if (key_combos[i].ctx) { |
| 4300 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4301 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4302 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4303 | SSL_CTX_up_ref(bind_conf->default_ctx); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4304 | break; |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4309 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4310 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4311 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4312 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4313 | end: |
| 4314 | |
| 4315 | if (names) |
| 4316 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 4317 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4318 | node = ebmb_first(&sni_keytypes_map); |
| 4319 | while (node) { |
| 4320 | next = ebmb_next(node); |
| 4321 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 4322 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4323 | node = next; |
| 4324 | } |
| 4325 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4326 | /* we need to free the ctx since we incremented the refcount where it's used */ |
| 4327 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 4328 | if (key_combos[i].ctx) |
| 4329 | SSL_CTX_free(key_combos[i].ctx); |
| 4330 | } |
| 4331 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4332 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4333 | if (ckch_inst->is_default) { |
| 4334 | SSL_CTX_free(bind_conf->default_ctx); |
| 4335 | bind_conf->default_ctx = NULL; |
| 4336 | } |
| 4337 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4338 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4339 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4342 | *ckchi = ckch_inst; |
| 4343 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4344 | } |
| 4345 | #else |
| 4346 | /* This is a dummy, that just logs an error and returns error */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4347 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4348 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4349 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4350 | { |
| 4351 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4352 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4353 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4354 | } |
| 4355 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4356 | #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] | 4357 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4358 | /* |
| 4359 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4360 | * |
| 4361 | * Returns a bitfield containing the flags: |
| 4362 | * ERR_FATAL in any fatal error case |
| 4363 | * ERR_ALERT if the reason of the error is available in err |
| 4364 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4365 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4366 | static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
| 4367 | 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] | 4368 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4369 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4370 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4371 | int order = 0; |
| 4372 | X509_NAME *xname; |
| 4373 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4374 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4375 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4376 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4377 | STACK_OF(GENERAL_NAME) *names; |
| 4378 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4379 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4380 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4381 | int errcode = 0; |
| 4382 | |
| 4383 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 4384 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4385 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4386 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4387 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4388 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4389 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4390 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 4391 | if (!ctx) { |
| 4392 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4393 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4394 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4395 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4396 | } |
| 4397 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4398 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 4399 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4400 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4401 | |
| 4402 | ckch_inst = ckch_inst_new(); |
| 4403 | if (!ckch_inst) { |
| 4404 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4405 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4406 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4407 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4408 | } |
| 4409 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4410 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4411 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4412 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4413 | switch(EVP_PKEY_base_id(pkey)) { |
| 4414 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4415 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4416 | break; |
| 4417 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4418 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 4419 | break; |
| 4420 | case EVP_PKEY_DSA: |
| 4421 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4422 | break; |
| 4423 | } |
| 4424 | EVP_PKEY_free(pkey); |
| 4425 | } |
| 4426 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4427 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4428 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4429 | 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] | 4430 | if (order < 0) { |
| 4431 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4432 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4433 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4434 | } |
| 4435 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4436 | } |
| 4437 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4438 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4439 | 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] | 4440 | if (names) { |
| 4441 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4442 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 4443 | if (name->type == GEN_DNS) { |
| 4444 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4445 | 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] | 4446 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4447 | if (order < 0) { |
| 4448 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4449 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4450 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4451 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4452 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4453 | } |
| 4454 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4455 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
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 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4458 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4459 | i = -1; |
| 4460 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4461 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4462 | ASN1_STRING *value; |
| 4463 | |
| 4464 | value = X509_NAME_ENTRY_get_data(entry); |
| 4465 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4466 | 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] | 4467 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4468 | if (order < 0) { |
| 4469 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4470 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4471 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4472 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4473 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4474 | } |
| 4475 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4476 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4477 | * the tree, so it will be discovered and cleaned in time. |
| 4478 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4479 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4480 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4481 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4482 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4483 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4484 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4485 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4486 | } |
| 4487 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4488 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4489 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4490 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4491 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4492 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4493 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4494 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4495 | /* everything succeed, the ckch instance can be used */ |
| 4496 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4497 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4498 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4499 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4500 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 4501 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4502 | *ckchi = ckch_inst; |
| 4503 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4504 | |
| 4505 | error: |
| 4506 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4507 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4508 | if (ckch_inst->is_default) |
| 4509 | SSL_CTX_free(ctx); |
| 4510 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4511 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4512 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4513 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4514 | SSL_CTX_free(ctx); |
| 4515 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4516 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4519 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4520 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4521 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4522 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4523 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4524 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4525 | |
| 4526 | /* we found the ckchs in the tree, we can use it directly */ |
| 4527 | if (ckchs->multi) |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4528 | 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] | 4529 | else |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4530 | 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] | 4531 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4532 | if (errcode & ERR_CODE) |
| 4533 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4534 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4535 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4536 | |
| 4537 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4538 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4539 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4540 | } |
| 4541 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4542 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 4543 | |
| 4544 | |
| 4545 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4546 | * done once. Zero is returned if the operation fails. No error is returned |
| 4547 | * if the random is said as not implemented, because we expect that openssl |
| 4548 | * will use another method once needed. |
| 4549 | */ |
| 4550 | static int ssl_initialize_random() |
| 4551 | { |
| 4552 | unsigned char random; |
| 4553 | static int random_initialized = 0; |
| 4554 | |
| 4555 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4556 | random_initialized = 1; |
| 4557 | |
| 4558 | return random_initialized; |
| 4559 | } |
| 4560 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4561 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4562 | /* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure |
| 4563 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4564 | * |
| 4565 | * This function tries to open and store certificate files. |
| 4566 | */ |
| 4567 | 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] | 4568 | { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4569 | struct crtlist *dir; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4570 | struct dirent **de_list; |
| 4571 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4572 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 4573 | char *end; |
| 4574 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4575 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4576 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4577 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4578 | int is_bundle; |
| 4579 | int j; |
| 4580 | #endif |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4581 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4582 | dir = crtlist_new(path, 1); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4583 | if (dir == NULL) { |
| 4584 | memprintf(err, "not enough memory"); |
| 4585 | return ERR_ALERT | ERR_FATAL; |
| 4586 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4587 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4588 | n = scandir(path, &de_list, 0, alphasort); |
| 4589 | if (n < 0) { |
| 4590 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 4591 | err && *err ? *err : "", path, strerror(errno)); |
| 4592 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4593 | } |
| 4594 | else { |
| 4595 | for (i = 0; i < n; i++) { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4596 | struct crtlist_entry *entry; |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4597 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 4598 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4599 | end = strrchr(de->d_name, '.'); |
| 4600 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key"))) |
| 4601 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4602 | |
William Lallemand | a64593c | 2020-03-17 20:11:41 +0100 | [diff] [blame] | 4603 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 4604 | if (stat(fp, &buf) != 0) { |
| 4605 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4606 | err && *err ? *err : "", fp, strerror(errno)); |
| 4607 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4608 | goto ignore_entry; |
| 4609 | } |
| 4610 | if (!S_ISREG(buf.st_mode)) |
| 4611 | goto ignore_entry; |
| 4612 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4613 | entry = crtlist_entry_new(); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4614 | if (entry == NULL) { |
| 4615 | memprintf(err, "not enough memory '%s'", fp); |
| 4616 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4617 | goto ignore_entry; |
| 4618 | } |
| 4619 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4620 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4621 | is_bundle = 0; |
| 4622 | /* Check if current entry in directory is part of a multi-cert bundle */ |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4623 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4624 | if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) { |
| 4625 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 4626 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 4627 | is_bundle = 1; |
| 4628 | break; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4629 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4630 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4631 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4632 | if (is_bundle) { |
| 4633 | int dp_len; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4634 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4635 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4636 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4637 | /* increment i and free de until we get to a non-bundle cert |
| 4638 | * Note here that we look at de_list[i + 1] before freeing de |
| 4639 | * this is important since ignore_entry will free de. This also |
| 4640 | * guarantees that de->d_name continues to hold the same prefix. |
| 4641 | */ |
| 4642 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
| 4643 | free(de); |
| 4644 | i++; |
| 4645 | de = de_list[i]; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4646 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4647 | |
| 4648 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4649 | ckchs = ckchs_lookup(fp); |
| 4650 | if (ckchs == NULL) |
| 4651 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 4652 | if (ckchs == NULL) { |
| 4653 | free(de); |
| 4654 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4655 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4656 | goto end; |
| 4657 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4658 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4659 | entry->crtlist = dir; |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4660 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4661 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4662 | ebpt_insert(&dir->entries, &entry->node); |
| 4663 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4664 | /* Successfully processed the bundle */ |
| 4665 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4666 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4667 | } |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4668 | |
| 4669 | #endif |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4670 | ckchs = ckchs_lookup(fp); |
| 4671 | if (ckchs == NULL) |
| 4672 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 4673 | if (ckchs == NULL) { |
| 4674 | free(de); |
| 4675 | free(entry); |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4676 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4677 | goto end; |
| 4678 | } |
| 4679 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4680 | entry->crtlist = dir; |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4681 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4682 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4683 | ebpt_insert(&dir->entries, &entry->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4684 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4685 | ignore_entry: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4686 | free(de); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4687 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4688 | end: |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4689 | free(de_list); |
| 4690 | } |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4691 | |
| 4692 | if (cfgerr & ERR_CODE) { |
| 4693 | /* free the dir and entries on error */ |
William Lallemand | 09bd5a0 | 2020-03-30 18:19:43 +0200 | [diff] [blame] | 4694 | crtlist_free(dir); |
William Lallemand | 83918e2 | 2020-03-16 17:21:51 +0100 | [diff] [blame] | 4695 | } else { |
| 4696 | *crtlist = dir; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4697 | } |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4698 | return cfgerr; |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4699 | |
William Lallemand | 2d232c2 | 2020-03-06 22:12:35 +0100 | [diff] [blame] | 4700 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4701 | |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4702 | /* |
| 4703 | * Read a single crt-list line. /!\ alter the <line> string. |
| 4704 | * Fill <crt_path> and <crtlist_entry> |
| 4705 | * <crtlist_entry> must be alloc and free by the caller |
| 4706 | * <crtlist_entry->ssl_conf> is alloc by the function |
| 4707 | * <crtlist_entry->filters> is alloc by the function |
| 4708 | * <crt_path> is a ptr in <line> |
| 4709 | * Return an error code |
| 4710 | */ |
| 4711 | static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err) |
| 4712 | { |
| 4713 | int cfgerr = 0; |
| 4714 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
| 4715 | char *end; |
| 4716 | char *args[MAX_CRT_ARGS + 1]; |
| 4717 | struct ssl_bind_conf *ssl_conf = NULL; |
| 4718 | |
| 4719 | if (!line || !crt_path || !entry) |
| 4720 | return ERR_ALERT | ERR_FATAL; |
| 4721 | |
| 4722 | end = line + strlen(line); |
| 4723 | if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') { |
| 4724 | /* Check if we reached the limit and the last char is not \n. |
| 4725 | * Watch out for the last line without the terminating '\n'! |
| 4726 | */ |
| 4727 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4728 | linenum, file, CRT_LINESIZE-1); |
| 4729 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4730 | goto error; |
| 4731 | } |
| 4732 | arg = 0; |
| 4733 | newarg = 1; |
| 4734 | while (*line) { |
| 4735 | if (isspace((unsigned char)*line)) { |
| 4736 | newarg = 1; |
| 4737 | *line = 0; |
| 4738 | } else if (*line == '[') { |
| 4739 | if (ssl_b) { |
| 4740 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 4741 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4742 | goto error; |
| 4743 | } |
| 4744 | if (!arg) { |
| 4745 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 4746 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4747 | goto error; |
| 4748 | } |
| 4749 | ssl_b = arg; |
| 4750 | newarg = 1; |
| 4751 | *line = 0; |
| 4752 | } else if (*line == ']') { |
| 4753 | if (ssl_e) { |
| 4754 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
| 4755 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4756 | goto error; |
| 4757 | } |
| 4758 | if (!ssl_b) { |
| 4759 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 4760 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4761 | goto error; |
| 4762 | } |
| 4763 | ssl_e = arg; |
| 4764 | newarg = 1; |
| 4765 | *line = 0; |
| 4766 | } else if (newarg) { |
| 4767 | if (arg == MAX_CRT_ARGS) { |
| 4768 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 4769 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4770 | goto error; |
| 4771 | } |
| 4772 | newarg = 0; |
| 4773 | args[arg++] = line; |
| 4774 | } |
| 4775 | line++; |
| 4776 | } |
| 4777 | args[arg++] = line; |
| 4778 | |
| 4779 | /* empty line */ |
| 4780 | if (!*args[0]) { |
| 4781 | cfgerr |= ERR_NONE; |
| 4782 | goto error; |
| 4783 | } |
| 4784 | |
| 4785 | *crt_path = args[0]; |
| 4786 | |
William Lallemand | 1b2988b | 2020-04-10 17:20:45 +0200 | [diff] [blame^] | 4787 | if (ssl_b) { |
| 4788 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4789 | if (!ssl_conf) { |
| 4790 | memprintf(err, "not enough memory!"); |
| 4791 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4792 | goto error; |
| 4793 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4794 | } |
| 4795 | cur_arg = ssl_b ? ssl_b : 1; |
| 4796 | while (cur_arg < ssl_e) { |
| 4797 | newarg = 0; |
| 4798 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4799 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4800 | newarg = 1; |
| 4801 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err); |
| 4802 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4803 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4804 | args[cur_arg], linenum, file); |
| 4805 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4806 | goto error; |
| 4807 | } |
| 4808 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4809 | break; |
| 4810 | } |
| 4811 | } |
| 4812 | if (!cfgerr && !newarg) { |
| 4813 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4814 | args[cur_arg], linenum, file); |
| 4815 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4816 | goto error; |
| 4817 | } |
| 4818 | } |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 4819 | entry->linenum = linenum; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4820 | entry->ssl_conf = ssl_conf; |
| 4821 | entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1); |
| 4822 | entry->fcount = arg - cur_arg - 1; |
| 4823 | |
| 4824 | return cfgerr; |
| 4825 | |
| 4826 | error: |
| 4827 | crtlist_free_filters(entry->filters); |
| 4828 | entry->filters = NULL; |
| 4829 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 4830 | free(entry->ssl_conf); |
| 4831 | entry->ssl_conf = NULL; |
| 4832 | return cfgerr; |
| 4833 | } |
| 4834 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4835 | /* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure |
| 4836 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 4837 | * |
| 4838 | * This function tries to open and store certificate files. |
| 4839 | */ |
| 4840 | static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err) |
| 4841 | { |
| 4842 | struct crtlist *newlist; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4843 | struct crtlist_entry *entry = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4844 | char thisline[CRT_LINESIZE]; |
| 4845 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4846 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4847 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4848 | int linenum = 0; |
| 4849 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4850 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4851 | if ((f = fopen(file, "r")) == NULL) { |
| 4852 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4853 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4854 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4855 | |
William Lallemand | ec2d493 | 2020-04-09 13:44:21 +0200 | [diff] [blame] | 4856 | newlist = crtlist_new(file, 0); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4857 | if (newlist == NULL) { |
| 4858 | memprintf(err, "Not enough memory!"); |
| 4859 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4860 | goto error; |
| 4861 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4862 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4863 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4864 | char *end; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4865 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4866 | char *crt_path; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4867 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4868 | |
| 4869 | linenum++; |
| 4870 | end = line + strlen(line); |
| 4871 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4872 | /* Check if we reached the limit and the last char is not \n. |
| 4873 | * Watch out for the last line without the terminating '\n'! |
| 4874 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4875 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4876 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4877 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4878 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4879 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4880 | |
| 4881 | if (*line == '#' || *line == '\n' || *line == '\r') |
| 4882 | continue; |
| 4883 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4884 | entry = crtlist_entry_new(); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4885 | if (entry == NULL) { |
| 4886 | memprintf(err, "Not enough memory!"); |
| 4887 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4888 | goto error; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4889 | } |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4890 | |
| 4891 | *(end - 1) = '\0'; /* line parser mustn't receive any \n */ |
| 4892 | cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4893 | if (cfgerr) |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4894 | goto error; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4895 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4896 | /* empty line */ |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4897 | if (!crt_path || !*crt_path) { |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4898 | crtlist_entry_free(entry); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4899 | entry = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4900 | continue; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4901 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4902 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4903 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4904 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4905 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4906 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4907 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4908 | goto error; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4909 | } |
| 4910 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4911 | crt_path = path; |
| 4912 | } |
| 4913 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4914 | /* Look for a ckch_store or create one */ |
| 4915 | ckchs = ckchs_lookup(crt_path); |
| 4916 | if (ckchs == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4917 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4918 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4919 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4920 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4921 | } |
William Lallemand | 909086e | 2020-03-17 16:53:27 +0100 | [diff] [blame] | 4922 | if (ckchs == NULL) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4923 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4924 | |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4925 | if (cfgerr & ERR_CODE) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4926 | goto error; |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4927 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4928 | entry->node.key = ckchs; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 4929 | entry->crtlist = newlist; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4930 | ebpt_insert(&newlist->entries, &entry->node); |
| 4931 | LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist); |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 4932 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4933 | |
| 4934 | entry = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4935 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4936 | if (cfgerr & ERR_CODE) |
| 4937 | goto error; |
| 4938 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 4939 | newlist->linecount = linenum; |
| 4940 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4941 | fclose(f); |
| 4942 | *crtlist = newlist; |
| 4943 | |
| 4944 | return cfgerr; |
| 4945 | error: |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 4946 | crtlist_entry_free(entry); |
William Lallemand | 97b0810 | 2020-04-01 10:14:00 +0200 | [diff] [blame] | 4947 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4948 | fclose(f); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4949 | crtlist_free(newlist); |
| 4950 | return cfgerr; |
| 4951 | } |
| 4952 | |
| 4953 | /* Load a crt-list file, this is done in 2 parts: |
| 4954 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 4955 | * - generate the instances by iterating on entries in the crtlist struct |
| 4956 | * |
| 4957 | * Nothing is locked there, this function is used in the configuration parser. |
| 4958 | * |
| 4959 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4960 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4961 | 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] | 4962 | { |
| 4963 | struct crtlist *crtlist = NULL; |
| 4964 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4965 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4966 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4967 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4968 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4969 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4970 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 4971 | if (!bind_conf_node) { |
| 4972 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 4973 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4974 | goto error; |
| 4975 | } |
| 4976 | bind_conf_node->next = NULL; |
| 4977 | bind_conf_node->bind_conf = bind_conf; |
| 4978 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4979 | /* strip trailing slashes, including first one */ |
| 4980 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 4981 | *end = 0; |
| 4982 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4983 | /* look for an existing crtlist or create one */ |
| 4984 | eb = ebst_lookup(&crtlists_tree, file); |
| 4985 | if (eb) { |
| 4986 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 4987 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4988 | /* load a crt-list OR a directory */ |
| 4989 | if (dir) |
| 4990 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 4991 | else |
| 4992 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 4993 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4994 | if (!(cfgerr & ERR_CODE)) |
| 4995 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 4996 | } |
| 4997 | |
| 4998 | if (cfgerr & ERR_CODE) { |
| 4999 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 5000 | goto error; |
| 5001 | } |
| 5002 | |
| 5003 | /* generates ckch instance from the crtlist_entry */ |
| 5004 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 5005 | struct ckch_store *store; |
| 5006 | struct ckch_inst *ckch_inst = NULL; |
| 5007 | |
| 5008 | store = entry->node.key; |
| 5009 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 5010 | if (cfgerr & ERR_CODE) { |
| 5011 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 5012 | goto error; |
| 5013 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5014 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 5015 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5016 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5017 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 5018 | /* add the bind_conf to the list */ |
| 5019 | bind_conf_node->next = crtlist->bind_conf; |
| 5020 | crtlist->bind_conf = bind_conf_node; |
| 5021 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5022 | return cfgerr; |
| 5023 | error: |
| 5024 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5025 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5026 | struct ckch_inst *inst, *s_inst; |
| 5027 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5028 | lastentry = entry; /* which entry we tried to generate last */ |
| 5029 | if (lastentry) { |
| 5030 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 5031 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 5032 | break; |
| 5033 | |
| 5034 | 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] | 5035 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5036 | /* this was not generated for this bind_conf, skip */ |
| 5037 | if (inst->bind_conf != bind_conf) |
| 5038 | continue; |
| 5039 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 5040 | /* free the sni_ctx and instance */ |
| 5041 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 5042 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5043 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5044 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 5045 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 5046 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5047 | return cfgerr; |
| 5048 | } |
| 5049 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 5050 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 5051 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 5052 | { |
| 5053 | struct stat buf; |
| 5054 | char fp[MAXPATHLEN+1]; |
| 5055 | int cfgerr = 0; |
| 5056 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5057 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 5058 | |
| 5059 | if ((ckchs = ckchs_lookup(path))) { |
| 5060 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5061 | 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] | 5062 | } |
| 5063 | if (stat(path, &buf) == 0) { |
| 5064 | if (S_ISDIR(buf.st_mode) == 0) { |
| 5065 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 5066 | if (!ckchs) |
| 5067 | return ERR_ALERT | ERR_FATAL; |
| 5068 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5069 | 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] | 5070 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 5071 | 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] | 5072 | } |
| 5073 | } else { |
| 5074 | /* stat failed, could be a bundle */ |
| 5075 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 5076 | /* try to load a bundle if it is permitted */ |
| 5077 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 5078 | if (!ckchs) |
| 5079 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 5080 | 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] | 5081 | } else { |
| 5082 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 5083 | err && *err ? *err : "", fp, strerror(errno)); |
| 5084 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5085 | } |
| 5086 | } |
| 5087 | |
| 5088 | return cfgerr; |
| 5089 | } |
| 5090 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5091 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5092 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5093 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5094 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5095 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5096 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5097 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5098 | SSL_OP_NO_SSLv2 | |
| 5099 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 5100 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5101 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5102 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 5103 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 5104 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5105 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5106 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5107 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5108 | SSL_MODE_RELEASE_BUFFERS | |
| 5109 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 5110 | 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] | 5111 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5112 | int flags = MC_SSL_O_ALL; |
| 5113 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5114 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5115 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5116 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5117 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5118 | 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] | 5119 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 5120 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5121 | 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] | 5122 | else |
| 5123 | flags = conf_ssl_methods->flags; |
| 5124 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 5125 | min = conf_ssl_methods->min; |
| 5126 | max = conf_ssl_methods->max; |
| 5127 | /* start with TLSv10 to remove SSLv3 per default */ |
| 5128 | if (!min && (!max || max >= CONF_TLSV10)) |
| 5129 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5130 | /* 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] | 5131 | if (min) |
| 5132 | flags |= (methodVersions[min].flag - 1); |
| 5133 | if (max) |
| 5134 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5135 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5136 | min = max = CONF_TLSV_NONE; |
| 5137 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5138 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5139 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5140 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5141 | if (min) { |
| 5142 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5143 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 5144 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5145 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 5146 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5147 | hole = 0; |
| 5148 | } |
| 5149 | max = i; |
| 5150 | } |
| 5151 | else { |
| 5152 | min = max = i; |
| 5153 | } |
| 5154 | } |
| 5155 | else { |
| 5156 | if (min) |
| 5157 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5158 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5159 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5160 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5161 | 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] | 5162 | cfgerr += 1; |
| 5163 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5164 | /* save real min/max in bind_conf */ |
| 5165 | conf_ssl_methods->min = min; |
| 5166 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5167 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5168 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5169 | /* 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] | 5170 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5171 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5172 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5173 | else |
| 5174 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5175 | if (flags & methodVersions[i].flag) |
| 5176 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5177 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5178 | /* 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] | 5179 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5180 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 5181 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5182 | |
| 5183 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 5184 | options |= SSL_OP_NO_TICKET; |
| 5185 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 5186 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 5187 | |
| 5188 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 5189 | options |= SSL_OP_NO_RENEGOTIATION; |
| 5190 | #endif |
| 5191 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5192 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5193 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5194 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5195 | if (global_ssl.async) |
| 5196 | mode |= SSL_MODE_ASYNC; |
| 5197 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5198 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5199 | if (global_ssl.life_time) |
| 5200 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5201 | |
| 5202 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5203 | #ifdef OPENSSL_IS_BORINGSSL |
| 5204 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 5205 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 5206 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 5207 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 5208 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 5209 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 5210 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5211 | #else |
| 5212 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5213 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 5214 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5215 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5216 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5219 | |
| 5220 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 5221 | { |
| 5222 | if (first == block) { |
| 5223 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5224 | if (first->len > 0) |
| 5225 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | /* return first block from sh_ssl_sess */ |
| 5230 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 5231 | { |
| 5232 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 5233 | |
| 5234 | } |
| 5235 | |
| 5236 | /* store a session into the cache |
| 5237 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 5238 | * data: asn1 encoded session |
| 5239 | * data_len: asn1 encoded session length |
| 5240 | * Returns 1 id session was stored (else 0) |
| 5241 | */ |
| 5242 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 5243 | { |
| 5244 | struct shared_block *first; |
| 5245 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 5246 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5247 | 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] | 5248 | if (!first) { |
| 5249 | /* Could not retrieve enough free blocks to store that session */ |
| 5250 | return 0; |
| 5251 | } |
| 5252 | |
| 5253 | /* STORE the key in the first elem */ |
| 5254 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 5255 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 5256 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5257 | |
| 5258 | /* it returns the already existing node |
| 5259 | or current node if none, never returns null */ |
| 5260 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 5261 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 5262 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 5263 | /* release the reserved row */ |
| 5264 | shctx_row_dec_hot(ssl_shctx, first); |
| 5265 | /* replace the previous session already in the tree */ |
| 5266 | sh_ssl_sess = oldsh_ssl_sess; |
| 5267 | /* ignore the previous session data, only use the header */ |
| 5268 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 5269 | shctx_row_inc_hot(ssl_shctx, first); |
| 5270 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 5271 | } |
| 5272 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 5273 | 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] | 5274 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5275 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5279 | |
| 5280 | return 1; |
| 5281 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5282 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5283 | /* SSL callback used when a new session is created while connecting to a server */ |
| 5284 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 5285 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5286 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5287 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5288 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5289 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5290 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5291 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 5292 | int len; |
| 5293 | unsigned char *ptr; |
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 | len = i2d_SSL_SESSION(sess, NULL); |
| 5296 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 5297 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 5298 | } else { |
| 5299 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5300 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 5301 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 5302 | } |
| 5303 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 5304 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 5305 | &ptr); |
| 5306 | } |
| 5307 | } else { |
| 5308 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 5309 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 5310 | } |
| 5311 | |
| 5312 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5313 | } |
| 5314 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5315 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5316 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5317 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5318 | { |
| 5319 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 5320 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 5321 | unsigned char *p; |
| 5322 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5323 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5324 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5325 | |
| 5326 | /* 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] | 5327 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5328 | * note: SSL_SESSION_set1_id is using |
| 5329 | * a memcpy so we need to use a different pointer |
| 5330 | * than sid_data or sid_ctx_data to avoid valgrind |
| 5331 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5332 | */ |
| 5333 | |
| 5334 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5335 | |
| 5336 | /* copy value in an other buffer */ |
| 5337 | memcpy(encid, sid_data, sid_length); |
| 5338 | |
| 5339 | /* pad with 0 */ |
| 5340 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 5341 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 5342 | |
| 5343 | /* force length to zero to avoid ASN1 encoding */ |
| 5344 | SSL_SESSION_set1_id(sess, encid, 0); |
| 5345 | |
| 5346 | /* force length to zero to avoid ASN1 encoding */ |
| 5347 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5348 | |
| 5349 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 5350 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 5351 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 5352 | goto err; |
| 5353 | |
| 5354 | p = encsess; |
| 5355 | |
| 5356 | /* process ASN1 session encoding before the lock */ |
| 5357 | i2d_SSL_SESSION(sess, &p); |
| 5358 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5359 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5360 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5361 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5362 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5363 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5364 | err: |
| 5365 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 5366 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 5367 | 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] | 5368 | |
| 5369 | return 0; /* do not increment session reference count */ |
| 5370 | } |
| 5371 | |
| 5372 | /* 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] | 5373 | 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] | 5374 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5375 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5376 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 5377 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5378 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5379 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5380 | |
| 5381 | global.shctx_lookups++; |
| 5382 | |
| 5383 | /* allow the session to be freed automatically by openssl */ |
| 5384 | *do_copy = 0; |
| 5385 | |
| 5386 | /* tree key is zeros padded sessionid */ |
| 5387 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5388 | memcpy(tmpkey, key, key_len); |
| 5389 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 5390 | key = tmpkey; |
| 5391 | } |
| 5392 | |
| 5393 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5394 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5395 | |
| 5396 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5397 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 5398 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5399 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5400 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5401 | global.shctx_misses++; |
| 5402 | return NULL; |
| 5403 | } |
| 5404 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5405 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 5406 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5407 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5408 | 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] | 5409 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5410 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5411 | |
| 5412 | /* decode ASN1 session */ |
| 5413 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5414 | 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] | 5415 | /* Reset session id and session id contenxt */ |
| 5416 | if (sess) { |
| 5417 | SSL_SESSION_set1_id(sess, key, key_len); |
| 5418 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5419 | } |
| 5420 | |
| 5421 | return sess; |
| 5422 | } |
| 5423 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5424 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5425 | /* 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] | 5426 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5427 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5428 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5429 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 5430 | unsigned int sid_length; |
| 5431 | const unsigned char *sid_data; |
| 5432 | (void)ctx; |
| 5433 | |
| 5434 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 5435 | /* tree key is zeros padded sessionid */ |
| 5436 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5437 | memcpy(tmpkey, sid_data, sid_length); |
| 5438 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 5439 | sid_data = tmpkey; |
| 5440 | } |
| 5441 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5442 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5443 | |
| 5444 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5445 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 5446 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5447 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5448 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5452 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* Set session cache mode to server and disable openssl internal cache. |
| 5456 | * Set shared cache callbacks on an ssl context. |
| 5457 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5458 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5459 | { |
| 5460 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5461 | |
| 5462 | if (!ssl_shctx) { |
| 5463 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 5464 | return; |
| 5465 | } |
| 5466 | |
| 5467 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 5468 | SSL_SESS_CACHE_NO_INTERNAL | |
| 5469 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 5470 | |
| 5471 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5472 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 5473 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 5474 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5475 | } |
| 5476 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5477 | /* |
| 5478 | * This function applies the SSL configuration on a SSL_CTX |
| 5479 | * It returns an error code and fills the <err> buffer |
| 5480 | */ |
| 5481 | 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] | 5482 | { |
| 5483 | struct proxy *curproxy = bind_conf->frontend; |
| 5484 | int cfgerr = 0; |
| 5485 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5486 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5487 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5488 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5489 | const char *conf_ciphersuites; |
| 5490 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5491 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5492 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5493 | if (ssl_conf) { |
| 5494 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5495 | int i, min, max; |
| 5496 | int flags = MC_SSL_O_ALL; |
| 5497 | |
| 5498 | /* 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] | 5499 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5500 | 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] | 5501 | if (min) |
| 5502 | flags |= (methodVersions[min].flag - 1); |
| 5503 | if (max) |
| 5504 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5505 | min = max = CONF_TLSV_NONE; |
| 5506 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5507 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5508 | if (min) |
| 5509 | max = i; |
| 5510 | else |
| 5511 | min = max = i; |
| 5512 | } |
| 5513 | /* save real min/max */ |
| 5514 | conf_ssl_methods->min = min; |
| 5515 | conf_ssl_methods->max = max; |
| 5516 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5517 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5518 | 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] | 5519 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5523 | 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] | 5524 | case SSL_SOCK_VERIFY_NONE: |
| 5525 | verify = SSL_VERIFY_NONE; |
| 5526 | break; |
| 5527 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5528 | verify = SSL_VERIFY_PEER; |
| 5529 | break; |
| 5530 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5531 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5532 | break; |
| 5533 | } |
| 5534 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5535 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5536 | 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] | 5537 | 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] | 5538 | 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] | 5539 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5540 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5541 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5542 | 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] | 5543 | 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] | 5544 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5545 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5546 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 5547 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 5548 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5549 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5550 | } |
| 5551 | 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] | 5552 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5553 | 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] | 5554 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5555 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5556 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5557 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5558 | 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] | 5559 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5560 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5561 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5562 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5563 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5564 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5565 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5566 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5567 | 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] | 5568 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5569 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5570 | else { |
| 5571 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5572 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5573 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5574 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5575 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5576 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5577 | #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] | 5578 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5579 | 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] | 5580 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5581 | 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] | 5582 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | #endif |
| 5586 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5587 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5588 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5589 | if (conf_ciphers && |
| 5590 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5591 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5592 | 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] | 5593 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5594 | } |
| 5595 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5596 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5597 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5598 | if (conf_ciphersuites && |
| 5599 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5600 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5601 | 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] | 5602 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5603 | } |
| 5604 | #endif |
| 5605 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5606 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5607 | /* If tune.ssl.default-dh-param has not been set, |
| 5608 | neither has ssl-default-dh-file and no static DH |
| 5609 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5610 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5611 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 5612 | (ssl_dh_ptr_index == -1 || |
| 5613 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5614 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 5615 | const SSL_CIPHER * cipher = NULL; |
| 5616 | char cipher_description[128]; |
| 5617 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 5618 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 5619 | which is not ephemeral DH. */ |
| 5620 | const char dhe_description[] = " Kx=DH "; |
| 5621 | const char dhe_export_description[] = " Kx=DH("; |
| 5622 | int idx = 0; |
| 5623 | int dhe_found = 0; |
| 5624 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5625 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5626 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5627 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5628 | if (ssl) { |
| 5629 | ciphers = SSL_get_ciphers(ssl); |
| 5630 | |
| 5631 | if (ciphers) { |
| 5632 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 5633 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 5634 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 5635 | if (strstr(cipher_description, dhe_description) != NULL || |
| 5636 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 5637 | dhe_found = 1; |
| 5638 | break; |
| 5639 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 5640 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5641 | } |
| 5642 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5643 | SSL_free(ssl); |
| 5644 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5645 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5646 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5647 | if (dhe_found) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5648 | 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", |
| 5649 | err && *err ? *err : ""); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5650 | cfgerr |= ERR_WARN; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5653 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5654 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5655 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5656 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5657 | if (local_dh_1024 == NULL) { |
| 5658 | local_dh_1024 = ssl_get_dh_1024(); |
| 5659 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5660 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5661 | if (local_dh_2048 == NULL) { |
| 5662 | local_dh_2048 = ssl_get_dh_2048(); |
| 5663 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5664 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5665 | if (local_dh_4096 == NULL) { |
| 5666 | local_dh_4096 = ssl_get_dh_4096(); |
| 5667 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5668 | } |
| 5669 | } |
| 5670 | } |
| 5671 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5672 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5673 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5674 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5675 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5676 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5677 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5678 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5679 | ssl_conf_cur = NULL; |
| 5680 | if (ssl_conf && ssl_conf->npn_str) |
| 5681 | ssl_conf_cur = ssl_conf; |
| 5682 | else if (bind_conf->ssl_conf.npn_str) |
| 5683 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5684 | if (ssl_conf_cur) |
| 5685 | 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] | 5686 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5687 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5688 | ssl_conf_cur = NULL; |
| 5689 | if (ssl_conf && ssl_conf->alpn_str) |
| 5690 | ssl_conf_cur = ssl_conf; |
| 5691 | else if (bind_conf->ssl_conf.alpn_str) |
| 5692 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5693 | if (ssl_conf_cur) |
| 5694 | 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] | 5695 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 5696 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5697 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5698 | if (conf_curves) { |
| 5699 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5700 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5701 | 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] | 5702 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5703 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5704 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5705 | } |
| 5706 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5707 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5708 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5709 | int i; |
| 5710 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5711 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5712 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5713 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5714 | NULL); |
| 5715 | |
| 5716 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 5717 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5718 | return cfgerr; |
| 5719 | } |
| 5720 | #else |
| 5721 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5722 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5723 | ECDHE_DEFAULT_CURVE); |
| 5724 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5725 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5726 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5727 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5728 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5729 | 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] | 5730 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5731 | } |
| 5732 | else { |
| 5733 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5734 | EC_KEY_free(ecdh); |
| 5735 | } |
| 5736 | } |
| 5737 | #endif |
| 5738 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5739 | return cfgerr; |
| 5740 | } |
| 5741 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5742 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5743 | { |
| 5744 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5745 | size_t prefixlen, suffixlen; |
| 5746 | |
| 5747 | /* Trivial case */ |
| 5748 | if (strcmp(pattern, hostname) == 0) |
| 5749 | return 1; |
| 5750 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5751 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5752 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5753 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5754 | pattern_wildcard = NULL; |
| 5755 | pattern_left_label_end = pattern; |
| 5756 | while (*pattern_left_label_end != '.') { |
| 5757 | switch (*pattern_left_label_end) { |
| 5758 | case 0: |
| 5759 | /* End of label not found */ |
| 5760 | return 0; |
| 5761 | case '*': |
| 5762 | /* If there is more than one wildcards */ |
| 5763 | if (pattern_wildcard) |
| 5764 | return 0; |
| 5765 | pattern_wildcard = pattern_left_label_end; |
| 5766 | break; |
| 5767 | } |
| 5768 | pattern_left_label_end++; |
| 5769 | } |
| 5770 | |
| 5771 | /* If it's not trivial and there is no wildcard, it can't |
| 5772 | * match */ |
| 5773 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5774 | return 0; |
| 5775 | |
| 5776 | /* Make sure all labels match except the leftmost */ |
| 5777 | hostname_left_label_end = strchr(hostname, '.'); |
| 5778 | if (!hostname_left_label_end |
| 5779 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5780 | return 0; |
| 5781 | |
| 5782 | /* Make sure the leftmost label of the hostname is long enough |
| 5783 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5784 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5785 | return 0; |
| 5786 | |
| 5787 | /* Finally compare the string on either side of the |
| 5788 | * wildcard */ |
| 5789 | prefixlen = pattern_wildcard - pattern; |
| 5790 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5791 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5792 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5793 | return 0; |
| 5794 | |
| 5795 | return 1; |
| 5796 | } |
| 5797 | |
| 5798 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5799 | { |
| 5800 | SSL *ssl; |
| 5801 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5802 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5803 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5804 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5805 | |
| 5806 | int depth; |
| 5807 | X509 *cert; |
| 5808 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5809 | int i; |
| 5810 | X509_NAME *cert_subject; |
| 5811 | char *str; |
| 5812 | |
| 5813 | if (ok == 0) |
| 5814 | return ok; |
| 5815 | |
| 5816 | 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] | 5817 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5818 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5819 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5820 | /* We're checking if the provided hostnames match the desired one. The |
| 5821 | * desired hostname comes from the SNI we presented if any, or if not |
| 5822 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5823 | * directive. If neither is set, we don't care about the name so the |
| 5824 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5825 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5826 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5827 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5828 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5829 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5830 | if (!servername) |
| 5831 | return ok; |
| 5832 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5833 | |
| 5834 | /* We only need to verify the CN on the actual server cert, |
| 5835 | * not the indirect CAs */ |
| 5836 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5837 | if (depth != 0) |
| 5838 | return ok; |
| 5839 | |
| 5840 | /* At this point, the cert is *not* OK unless we can find a |
| 5841 | * hostname match */ |
| 5842 | ok = 0; |
| 5843 | |
| 5844 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5845 | /* It seems like this might happen if verify peer isn't set */ |
| 5846 | if (!cert) |
| 5847 | return ok; |
| 5848 | |
| 5849 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5850 | if (alt_names) { |
| 5851 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5852 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5853 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5854 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5855 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5856 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5857 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5858 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5859 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5860 | OPENSSL_free(str); |
| 5861 | } |
| 5862 | } |
| 5863 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5864 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | cert_subject = X509_get_subject_name(cert); |
| 5868 | i = -1; |
| 5869 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5870 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5871 | ASN1_STRING *value; |
| 5872 | value = X509_NAME_ENTRY_get_data(entry); |
| 5873 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5874 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5875 | OPENSSL_free(str); |
| 5876 | } |
| 5877 | } |
| 5878 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5879 | /* report the mismatch and indicate if SNI was used or not */ |
| 5880 | if (!ok && !conn->err_code) |
| 5881 | 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] | 5882 | return ok; |
| 5883 | } |
| 5884 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5885 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5886 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5887 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5888 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5889 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5890 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5891 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5892 | SSL_OP_NO_SSLv2 | |
| 5893 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5894 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5895 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5896 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5897 | SSL_MODE_RELEASE_BUFFERS | |
| 5898 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5899 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5900 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5901 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5902 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5903 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5904 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5905 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5906 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5907 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5908 | cfgerr++; |
| 5909 | } |
| 5910 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5911 | /* Automatic memory computations need to know we use SSL there */ |
| 5912 | global.ssl_used_backend = 1; |
| 5913 | |
| 5914 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5915 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5916 | 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] | 5917 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5918 | curproxy->id, srv->id, |
| 5919 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5920 | cfgerr++; |
| 5921 | return cfgerr; |
| 5922 | } |
| 5923 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5924 | if (srv->use_ssl) |
| 5925 | srv->xprt = &ssl_sock; |
| 5926 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 5927 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5928 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5929 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5930 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5931 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5932 | proxy_type_str(curproxy), curproxy->id, |
| 5933 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5934 | cfgerr++; |
| 5935 | return cfgerr; |
| 5936 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5937 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5938 | 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] | 5939 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5940 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5941 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5942 | else |
| 5943 | flags = conf_ssl_methods->flags; |
| 5944 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5945 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5946 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5947 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5948 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5949 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5950 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5951 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5952 | min = max = CONF_TLSV_NONE; |
| 5953 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5954 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5955 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5956 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5957 | if (min) { |
| 5958 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5959 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5960 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5961 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5962 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5963 | hole = 0; |
| 5964 | } |
| 5965 | max = i; |
| 5966 | } |
| 5967 | else { |
| 5968 | min = max = i; |
| 5969 | } |
| 5970 | } |
| 5971 | else { |
| 5972 | if (min) |
| 5973 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5974 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5975 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5976 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5977 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5978 | cfgerr += 1; |
| 5979 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5980 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5981 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5982 | /* 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] | 5983 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5984 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5985 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5986 | else |
| 5987 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5988 | if (flags & methodVersions[i].flag) |
| 5989 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5990 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5991 | /* 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] | 5992 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5993 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5994 | #endif |
| 5995 | |
| 5996 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5997 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5998 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5999 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6000 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6001 | if (global_ssl.async) |
| 6002 | mode |= SSL_MODE_ASYNC; |
| 6003 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6004 | SSL_CTX_set_mode(ctx, mode); |
| 6005 | srv->ssl_ctx.ctx = ctx; |
| 6006 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6007 | if (srv->ssl_ctx.client_crt) { |
| 6008 | 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] | 6009 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 6010 | proxy_type_str(curproxy), curproxy->id, |
| 6011 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6012 | cfgerr++; |
| 6013 | } |
| 6014 | 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] | 6015 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 6016 | proxy_type_str(curproxy), curproxy->id, |
| 6017 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6018 | cfgerr++; |
| 6019 | } |
| 6020 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6021 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 6022 | proxy_type_str(curproxy), curproxy->id, |
| 6023 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6024 | cfgerr++; |
| 6025 | } |
| 6026 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6027 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6028 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 6029 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6030 | switch (srv->ssl_ctx.verify) { |
| 6031 | case SSL_SOCK_VERIFY_NONE: |
| 6032 | verify = SSL_VERIFY_NONE; |
| 6033 | break; |
| 6034 | case SSL_SOCK_VERIFY_REQUIRED: |
| 6035 | verify = SSL_VERIFY_PEER; |
| 6036 | break; |
| 6037 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6038 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6039 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 6040 | (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] | 6041 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6042 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 6043 | /* set CAfile to verify */ |
| 6044 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 6045 | 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] | 6046 | curproxy->id, srv->id, |
| 6047 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6048 | cfgerr++; |
| 6049 | } |
| 6050 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6051 | else { |
| 6052 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6053 | 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", |
| 6054 | curproxy->id, srv->id, |
| 6055 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6056 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6057 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 6058 | curproxy->id, srv->id, |
| 6059 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6060 | cfgerr++; |
| 6061 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6062 | #ifdef X509_V_FLAG_CRL_CHECK |
| 6063 | if (srv->ssl_ctx.crl_file) { |
| 6064 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 6065 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 6066 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6067 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 6068 | curproxy->id, srv->id, |
| 6069 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6070 | cfgerr++; |
| 6071 | } |
| 6072 | else { |
| 6073 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 6074 | } |
| 6075 | } |
| 6076 | #endif |
| 6077 | } |
| 6078 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 6079 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 6080 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 6081 | 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] | 6082 | if (srv->ssl_ctx.ciphers && |
| 6083 | !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] | 6084 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 6085 | curproxy->id, srv->id, |
| 6086 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6087 | cfgerr++; |
| 6088 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6089 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6090 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6091 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 6092 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6093 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 6094 | curproxy->id, srv->id, |
| 6095 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 6096 | cfgerr++; |
| 6097 | } |
| 6098 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6099 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 6100 | if (srv->ssl_ctx.npn_str) |
| 6101 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 6102 | #endif |
| 6103 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6104 | if (srv->ssl_ctx.alpn_str) |
| 6105 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 6106 | #endif |
| 6107 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 6108 | |
| 6109 | return cfgerr; |
| 6110 | } |
| 6111 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6112 | /* 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] | 6113 | * be NULL, in which case nothing is done. Returns the number of errors |
| 6114 | * encountered. |
| 6115 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6116 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6117 | { |
| 6118 | struct ebmb_node *node; |
| 6119 | struct sni_ctx *sni; |
| 6120 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6121 | int errcode = 0; |
| 6122 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6123 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 6124 | /* Automatic memory computations need to know we use SSL there */ |
| 6125 | global.ssl_used_frontend = 1; |
| 6126 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6127 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 6128 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6129 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6130 | err++; |
| 6131 | } |
| 6132 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 6133 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 6134 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6135 | /* It should not be necessary to call this function, but it's |
| 6136 | necessary first to check and move all initialisation related |
| 6137 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6138 | 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] | 6139 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6140 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6141 | 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] | 6142 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6143 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6144 | while (node) { |
| 6145 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6146 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 6147 | /* only initialize the CTX on its first occurrence and |
| 6148 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6149 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6150 | node = ebmb_next(node); |
| 6151 | } |
| 6152 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6153 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6154 | while (node) { |
| 6155 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6156 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 6157 | /* only initialize the CTX on its first occurrence and |
| 6158 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6159 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 6160 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6161 | node = ebmb_next(node); |
| 6162 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6163 | |
| 6164 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6165 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6166 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 6167 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 6168 | err++; |
| 6169 | } |
| 6170 | |
| 6171 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6172 | return err; |
| 6173 | } |
| 6174 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6175 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 6176 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 6177 | * alerts are directly emitted since the rest of the stack does it below. |
| 6178 | */ |
| 6179 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 6180 | { |
| 6181 | struct proxy *px = bind_conf->frontend; |
| 6182 | int alloc_ctx; |
| 6183 | int err; |
| 6184 | |
| 6185 | if (!bind_conf->is_ssl) { |
| 6186 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6187 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 6188 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6189 | } |
| 6190 | return 0; |
| 6191 | } |
| 6192 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6193 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6194 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 6195 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6196 | } |
| 6197 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6198 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 6199 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 6200 | return -1; |
| 6201 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6202 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 6203 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6204 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 6205 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6206 | sizeof(*sh_ssl_sess_tree), |
| 6207 | ((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] | 6208 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 6209 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 6210 | 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"); |
| 6211 | else |
| 6212 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 6213 | return -1; |
| 6214 | } |
| 6215 | /* free block callback */ |
| 6216 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 6217 | /* init the root tree within the extra space */ |
| 6218 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 6219 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6220 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6221 | err = 0; |
| 6222 | /* initialize all certificate contexts */ |
| 6223 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 6224 | |
| 6225 | /* initialize CA variables if the certificates generation is enabled */ |
| 6226 | err += ssl_sock_load_ca(bind_conf); |
| 6227 | |
| 6228 | return -err; |
| 6229 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6230 | |
| 6231 | /* release ssl context allocated for servers. */ |
| 6232 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 6233 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6234 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6235 | if (srv->ssl_ctx.alpn_str) |
| 6236 | free(srv->ssl_ctx.alpn_str); |
| 6237 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 6238 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 6239 | if (srv->ssl_ctx.npn_str) |
| 6240 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 6241 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 6242 | if (srv->ssl_ctx.ctx) |
| 6243 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 6244 | } |
| 6245 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6246 | /* 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] | 6247 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 6248 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6249 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6250 | { |
| 6251 | struct ebmb_node *node, *back; |
| 6252 | struct sni_ctx *sni; |
| 6253 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6254 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6255 | while (node) { |
| 6256 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6257 | back = ebmb_next(node); |
| 6258 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6259 | SSL_CTX_free(sni->ctx); |
| 6260 | if (!sni->order) { /* only free the CTX conf on its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6261 | ssl_sock_free_ssl_conf(sni->conf); |
| 6262 | free(sni->conf); |
| 6263 | sni->conf = NULL; |
| 6264 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6265 | free(sni); |
| 6266 | node = back; |
| 6267 | } |
| 6268 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6269 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6270 | while (node) { |
| 6271 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 6272 | back = ebmb_next(node); |
| 6273 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6274 | SSL_CTX_free(sni->ctx); |
| 6275 | if (!sni->order) { /* only free the SSL conf its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6276 | ssl_sock_free_ssl_conf(sni->conf); |
| 6277 | free(sni->conf); |
| 6278 | sni->conf = NULL; |
| 6279 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 6280 | free(sni); |
| 6281 | node = back; |
| 6282 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 6283 | SSL_CTX_free(bind_conf->initial_ctx); |
| 6284 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 6285 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 6286 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6287 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6288 | } |
| 6289 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6290 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 6291 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 6292 | { |
| 6293 | ssl_sock_free_ca(bind_conf); |
| 6294 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6295 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6296 | free(bind_conf->ca_sign_file); |
| 6297 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 6298 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6299 | free(bind_conf->keys_ref->filename); |
| 6300 | free(bind_conf->keys_ref->tlskeys); |
| 6301 | LIST_DEL(&bind_conf->keys_ref->list); |
| 6302 | free(bind_conf->keys_ref); |
| 6303 | } |
| 6304 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6305 | bind_conf->ca_sign_pass = NULL; |
| 6306 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6307 | } |
| 6308 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6309 | /* Load CA cert file and private key used to generate certificates */ |
| 6310 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6311 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6312 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6313 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6314 | FILE *fp; |
| 6315 | X509 *cacert = NULL; |
| 6316 | EVP_PKEY *capkey = NULL; |
| 6317 | int err = 0; |
| 6318 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 6319 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6320 | return err; |
| 6321 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6322 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6323 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6324 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6325 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6326 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6327 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6328 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 6329 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6330 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6331 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 6332 | "no CA certificate File configured at [%s:%d].\n", |
| 6333 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6334 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6335 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6336 | |
| 6337 | /* read in the CA certificate */ |
| 6338 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6339 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6340 | 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] | 6341 | goto load_error; |
| 6342 | } |
| 6343 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6344 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 6345 | 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] | 6346 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6347 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6348 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6349 | 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] | 6350 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 6351 | 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] | 6352 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6353 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6354 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6355 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6356 | bind_conf->ca_sign_cert = cacert; |
| 6357 | bind_conf->ca_sign_pkey = capkey; |
| 6358 | return err; |
| 6359 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6360 | read_error: |
| 6361 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6362 | if (capkey) EVP_PKEY_free(capkey); |
| 6363 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 6364 | load_error: |
| 6365 | bind_conf->generate_certs = 0; |
| 6366 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6367 | return err; |
| 6368 | } |
| 6369 | |
| 6370 | /* Release CA cert and private key used to generate certificated */ |
| 6371 | void |
| 6372 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 6373 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6374 | if (bind_conf->ca_sign_pkey) |
| 6375 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 6376 | if (bind_conf->ca_sign_cert) |
| 6377 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 6378 | bind_conf->ca_sign_pkey = NULL; |
| 6379 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6380 | } |
| 6381 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6382 | /* |
| 6383 | * This function is called if SSL * context is not yet allocated. The function |
| 6384 | * is designed to be called before any other data-layer operation and sets the |
| 6385 | * handshake flag on the connection. It is safe to call it multiple times. |
| 6386 | * It returns 0 on success and -1 in error case. |
| 6387 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6388 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6389 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6390 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6391 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6392 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6393 | return 0; |
| 6394 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6395 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6396 | return 0; |
| 6397 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6398 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6399 | if (!ctx) { |
| 6400 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6401 | return -1; |
| 6402 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6403 | ctx->wait_event.tasklet = tasklet_new(); |
| 6404 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6405 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6406 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6407 | return -1; |
| 6408 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6409 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6410 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6411 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6412 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6413 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6414 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6415 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6416 | ctx->xprt_st = 0; |
| 6417 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6418 | |
| 6419 | /* Only work with sockets for now, this should be adapted when we'll |
| 6420 | * add QUIC support. |
| 6421 | */ |
| 6422 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6423 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6424 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6425 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6426 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6428 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 6429 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6430 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6431 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6432 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6433 | /* If it is in client mode initiate SSL session |
| 6434 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6435 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6436 | int may_retry = 1; |
| 6437 | |
| 6438 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6439 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6440 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 6441 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6442 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6443 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6444 | goto retry_connect; |
| 6445 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6446 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6447 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6448 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6449 | ctx->bio = BIO_new(ha_meth); |
| 6450 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6451 | SSL_free(ctx->ssl); |
| 6452 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6453 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6454 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6455 | goto retry_connect; |
| 6456 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6457 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6458 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6459 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6460 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6461 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6462 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6463 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6464 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6465 | SSL_free(ctx->ssl); |
| 6466 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6467 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6468 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6469 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6470 | goto retry_connect; |
| 6471 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6472 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6473 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6474 | } |
| 6475 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6476 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6477 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6478 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6479 | 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] | 6480 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6481 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6482 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6483 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6484 | } else if (sess) { |
| 6485 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6486 | } |
| 6487 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6488 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6489 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6490 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6491 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6492 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6493 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6494 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6495 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6496 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6497 | return 0; |
| 6498 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6499 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6500 | int may_retry = 1; |
| 6501 | |
| 6502 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6503 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6504 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 6505 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6506 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6507 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6508 | goto retry_accept; |
| 6509 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6510 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6511 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6512 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6513 | ctx->bio = BIO_new(ha_meth); |
| 6514 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6515 | SSL_free(ctx->ssl); |
| 6516 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6517 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6518 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6519 | goto retry_accept; |
| 6520 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6521 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6522 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6523 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6524 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6525 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6526 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6527 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6528 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6529 | SSL_free(ctx->ssl); |
| 6530 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6531 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6532 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6533 | goto retry_accept; |
| 6534 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6535 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6536 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6537 | } |
| 6538 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6539 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6540 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 6541 | b_alloc(&ctx->early_buf); |
| 6542 | SSL_set_max_early_data(ctx->ssl, |
| 6543 | /* Only allow early data if we managed to allocate |
| 6544 | * a buffer. |
| 6545 | */ |
| 6546 | (!b_is_null(&ctx->early_buf)) ? |
| 6547 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6548 | } |
| 6549 | #endif |
| 6550 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6551 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6552 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6553 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6554 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6555 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6556 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 6557 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6558 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6559 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6560 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6561 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6562 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6563 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6564 | return 0; |
| 6565 | } |
| 6566 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6567 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6568 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6569 | if (ctx && ctx->wait_event.tasklet) |
| 6570 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6571 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6572 | return -1; |
| 6573 | } |
| 6574 | |
| 6575 | |
| 6576 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6577 | * updates the FD status if it wants some polling before being called again. |
| 6578 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6579 | * otherwise it returns non-zero and removes itself from the connection's |
| 6580 | * flags (the bit is provided in <flag> by the caller). |
| 6581 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6582 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6583 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6584 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6585 | int ret; |
| 6586 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6587 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6588 | return 0; |
| 6589 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6590 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6591 | goto out_error; |
| 6592 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6593 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6594 | /* |
| 6595 | * Check if we have early data. If we do, we have to read them |
| 6596 | * before SSL_do_handshake() is called, And there's no way to |
| 6597 | * detect early data, except to try to read them |
| 6598 | */ |
| 6599 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6600 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6601 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6602 | while (1) { |
| 6603 | ret = SSL_read_early_data(ctx->ssl, |
| 6604 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6605 | &read_data); |
| 6606 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6607 | goto check_error; |
| 6608 | if (read_data > 0) { |
| 6609 | conn->flags |= CO_FL_EARLY_DATA; |
| 6610 | b_add(&ctx->early_buf, read_data); |
| 6611 | } |
| 6612 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6613 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6614 | if (!b_data(&ctx->early_buf)) |
| 6615 | b_free(&ctx->early_buf); |
| 6616 | break; |
| 6617 | } |
| 6618 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6619 | } |
| 6620 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6621 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6622 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6623 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6624 | * the reneg handshake. |
| 6625 | * Here we use SSL_peek as a workaround for reneg. |
| 6626 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6627 | 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] | 6628 | char c; |
| 6629 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6630 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6631 | if (ret <= 0) { |
| 6632 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6633 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6634 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6635 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6636 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6637 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6638 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6639 | return 0; |
| 6640 | } |
| 6641 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6642 | /* handshake may have been completed but we have |
| 6643 | * no more data to read. |
| 6644 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6645 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6646 | ret = 1; |
| 6647 | goto reneg_ok; |
| 6648 | } |
| 6649 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6650 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6651 | 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] | 6652 | return 0; |
| 6653 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6654 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6655 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6656 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6657 | return 0; |
| 6658 | } |
| 6659 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6660 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6661 | /* if errno is null, then connection was successfully established */ |
| 6662 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6663 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6664 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6665 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6666 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6667 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6668 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6669 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6670 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6671 | /* 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] | 6672 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6673 | empty_handshake = state == TLS_ST_BEFORE; |
| 6674 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6675 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6676 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6677 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6678 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6679 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6680 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6681 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6682 | else |
| 6683 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6684 | } |
| 6685 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6686 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6687 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6688 | else |
| 6689 | conn->err_code = CO_ER_SSL_ABORT; |
| 6690 | } |
| 6691 | } |
| 6692 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6693 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6694 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6695 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6696 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6697 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6698 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6699 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6700 | goto out_error; |
| 6701 | } |
| 6702 | else { |
| 6703 | /* Fail on all other handshake errors */ |
| 6704 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6705 | * buffer, causing an RST to be emitted upon close() on |
| 6706 | * TCP sockets. We first try to drain possibly pending |
| 6707 | * data to avoid this as much as possible. |
| 6708 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6709 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6710 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6711 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6712 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6713 | goto out_error; |
| 6714 | } |
| 6715 | } |
| 6716 | /* read some data: consider handshake completed */ |
| 6717 | goto reneg_ok; |
| 6718 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6719 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6720 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6721 | if (ret != 1) { |
| 6722 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6723 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6724 | |
| 6725 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6726 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6727 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6728 | 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] | 6729 | return 0; |
| 6730 | } |
| 6731 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6732 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6733 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6734 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6735 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6736 | return 0; |
| 6737 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6738 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6739 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6740 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6741 | return 0; |
| 6742 | } |
| 6743 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6744 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6745 | /* if errno is null, then connection was successfully established */ |
| 6746 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6747 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6748 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6749 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6750 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6751 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6752 | #else |
| 6753 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6754 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6755 | /* 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] | 6756 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6757 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6758 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6759 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6760 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6761 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6762 | if (empty_handshake) { |
| 6763 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6764 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6765 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6766 | else |
| 6767 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6768 | } |
| 6769 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6770 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6771 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6772 | else |
| 6773 | conn->err_code = CO_ER_SSL_ABORT; |
| 6774 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6775 | } |
| 6776 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6777 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6778 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6779 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6780 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6781 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6782 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6783 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6784 | goto out_error; |
| 6785 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6786 | else { |
| 6787 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6788 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6789 | * buffer, causing an RST to be emitted upon close() on |
| 6790 | * TCP sockets. We first try to drain possibly pending |
| 6791 | * data to avoid this as much as possible. |
| 6792 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6793 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6794 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6795 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6796 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6797 | goto out_error; |
| 6798 | } |
| 6799 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6800 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6801 | else { |
| 6802 | /* |
| 6803 | * If the server refused the early data, we have to send a |
| 6804 | * 425 to the client, as we no longer have the data to sent |
| 6805 | * them again. |
| 6806 | */ |
| 6807 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6808 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6809 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6810 | goto out_error; |
| 6811 | } |
| 6812 | } |
| 6813 | } |
| 6814 | #endif |
| 6815 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6816 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6817 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6818 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6819 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6820 | /* ASYNC engine API doesn't support moving read/write |
| 6821 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6822 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6823 | */ |
| 6824 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6825 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6826 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6827 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6828 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6829 | if (objt_server(conn->target)) { |
| 6830 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6831 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6832 | 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] | 6833 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6834 | else { |
| 6835 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6836 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6837 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6838 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6839 | } |
| 6840 | |
| 6841 | /* The connection is now established at both layers, it's time to leave */ |
| 6842 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6843 | return 1; |
| 6844 | |
| 6845 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6846 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6847 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6848 | ERR_clear_error(); |
| 6849 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6850 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6851 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6852 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6853 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6854 | } |
| 6855 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6856 | /* Fail on all other handshake errors */ |
| 6857 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6858 | if (!conn->err_code) |
| 6859 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6860 | return 0; |
| 6861 | } |
| 6862 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6863 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6864 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6865 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6866 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6867 | * unless the transport layer was already released. |
| 6868 | */ |
| 6869 | 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] | 6870 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6871 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6872 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6873 | if (!ctx) |
| 6874 | return -1; |
| 6875 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6876 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6877 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6878 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6879 | ctx->subs = es; |
| 6880 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6881 | |
| 6882 | /* we may have to subscribe to lower layers for new events */ |
| 6883 | event_type &= ~ctx->wait_event.events; |
| 6884 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6885 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6886 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6887 | } |
| 6888 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6889 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6890 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6891 | * subscribe() call. It always returns zero. |
| 6892 | */ |
| 6893 | 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] | 6894 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6895 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6896 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6897 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6898 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6899 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6900 | es->events &= ~event_type; |
| 6901 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6902 | ctx->subs = NULL; |
| 6903 | |
| 6904 | /* If we subscribed, and we're not doing the handshake, |
| 6905 | * then we subscribed because the upper layer asked for it, |
| 6906 | * as the upper layer is no longer interested, we can |
| 6907 | * unsubscribe too. |
| 6908 | */ |
| 6909 | event_type &= ctx->wait_event.events; |
| 6910 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6911 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6912 | |
| 6913 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6914 | } |
| 6915 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6916 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6917 | * Returns 0 on success, and non-zero on failure. |
| 6918 | */ |
| 6919 | 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) |
| 6920 | { |
| 6921 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6922 | |
| 6923 | if (oldxprt_ops != NULL) |
| 6924 | *oldxprt_ops = ctx->xprt; |
| 6925 | if (oldxprt_ctx != NULL) |
| 6926 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6927 | ctx->xprt = toadd_ops; |
| 6928 | ctx->xprt_ctx = toadd_ctx; |
| 6929 | return 0; |
| 6930 | } |
| 6931 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6932 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6933 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6934 | * XPRT. |
| 6935 | */ |
| 6936 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6937 | { |
| 6938 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6939 | |
| 6940 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6941 | ctx->xprt_ctx = newctx; |
| 6942 | ctx->xprt = newops; |
| 6943 | return 0; |
| 6944 | } |
| 6945 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6946 | } |
| 6947 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6948 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6949 | { |
| 6950 | struct ssl_sock_ctx *ctx = context; |
| 6951 | |
| 6952 | /* First if we're doing an handshake, try that */ |
| 6953 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6954 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6955 | /* If we had an error, or the handshake is done and I/O is available, |
| 6956 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6957 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6958 | * we can't be sure conn_fd_handler() will be called again. |
| 6959 | */ |
| 6960 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6961 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6962 | int ret = 0; |
| 6963 | int woke = 0; |
| 6964 | |
| 6965 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6966 | if (ctx->subs) { |
| 6967 | tasklet_wakeup(ctx->subs->tasklet); |
| 6968 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6969 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6970 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6971 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6972 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6973 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6974 | * upper layers know. If we have no mux, create it, |
| 6975 | * and once we have a mux, call its wake method if we didn't |
| 6976 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6977 | */ |
| 6978 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6979 | if (!ctx->conn->mux) |
| 6980 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6981 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6982 | ctx->conn->mux->wake(ctx->conn); |
| 6983 | return NULL; |
| 6984 | } |
| 6985 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6986 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6987 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6988 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6989 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6990 | tasklet_wakeup(ctx->subs->tasklet); |
| 6991 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6992 | if (!ctx->subs->events) |
| 6993 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6994 | } |
| 6995 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6996 | return NULL; |
| 6997 | } |
| 6998 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6999 | /* 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] | 7000 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7001 | * buffer wraps, in which case a second call may be performed. The connection's |
| 7002 | * flags are updated with whatever special event is detected (error, read0, |
| 7003 | * empty). The caller is responsible for taking care of those events and |
| 7004 | * avoiding the call if inappropriate. The function does not call the |
| 7005 | * connection's polling update function, so the caller is responsible for this. |
| 7006 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7007 | 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] | 7008 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7009 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 7010 | ssize_t ret; |
| 7011 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7012 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7013 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7014 | goto out_error; |
| 7015 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7016 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 7017 | if (b_data(&ctx->early_buf)) { |
| 7018 | try = b_contig_space(buf); |
| 7019 | if (try > b_data(&ctx->early_buf)) |
| 7020 | try = b_data(&ctx->early_buf); |
| 7021 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 7022 | b_add(buf, try); |
| 7023 | b_del(&ctx->early_buf, try); |
| 7024 | if (b_data(&ctx->early_buf) == 0) |
| 7025 | b_free(&ctx->early_buf); |
| 7026 | return try; |
| 7027 | } |
| 7028 | #endif |
| 7029 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7030 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7031 | /* a handshake was requested */ |
| 7032 | return 0; |
| 7033 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7034 | /* read the largest possible block. For this, we perform only one call |
| 7035 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 7036 | * in which case we accept to do it once again. A new attempt is made on |
| 7037 | * EINTR too. |
| 7038 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 7039 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7040 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 7041 | try = b_contig_space(buf); |
| 7042 | if (!try) |
| 7043 | break; |
| 7044 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 7045 | if (try > count) |
| 7046 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 7047 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7048 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 7049 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7050 | if (conn->flags & CO_FL_ERROR) { |
| 7051 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7052 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7053 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7054 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 7055 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7056 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7057 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7058 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7059 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7060 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7061 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7062 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7063 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7064 | 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] | 7065 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7066 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7067 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7068 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7069 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7070 | break; |
| 7071 | } |
| 7072 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7073 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7074 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7075 | SUB_RETRY_RECV, |
| 7076 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7077 | /* handshake is running, and it may need to re-enable read */ |
| 7078 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7079 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7080 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7081 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7082 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7083 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7084 | break; |
| 7085 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7086 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7087 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 7088 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7089 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 7090 | * stack before shutting down the connection for |
| 7091 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7092 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 7093 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7094 | /* otherwise it's a real error */ |
| 7095 | goto out_error; |
| 7096 | } |
| 7097 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7098 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7099 | return done; |
| 7100 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7101 | clear_ssl_error: |
| 7102 | /* Clear openssl global errors stack */ |
| 7103 | ssl_sock_dump_errors(conn); |
| 7104 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7105 | read0: |
| 7106 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7107 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 7108 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7109 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 7110 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7111 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7112 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7113 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7114 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7118 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 7119 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 7120 | * 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] | 7121 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 7122 | * a second call may be performed. The connection's flags are updated with |
| 7123 | * whatever special event is detected (error, empty). The caller is responsible |
| 7124 | * for taking care of those events and avoiding the call if inappropriate. The |
| 7125 | * 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] | 7126 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 7127 | * caller to take care of this. It's up to the caller to update the buffer's |
| 7128 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7129 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7130 | 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] | 7131 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7132 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7133 | ssize_t ret; |
| 7134 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7135 | |
| 7136 | done = 0; |
| 7137 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7138 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7139 | goto out_error; |
| 7140 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7141 | 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] | 7142 | /* a handshake was requested */ |
| 7143 | return 0; |
| 7144 | |
| 7145 | /* send the largest possible block. For this we perform only one call |
| 7146 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 7147 | * in which case we accept to do it once again. |
| 7148 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7149 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7150 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7151 | size_t written_data; |
| 7152 | #endif |
| 7153 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7154 | try = b_contig_data(buf, done); |
| 7155 | if (try > count) |
| 7156 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7157 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 7158 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7159 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7160 | global_ssl.max_record && try > global_ssl.max_record) { |
| 7161 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7162 | } |
| 7163 | else { |
| 7164 | /* we need to keep the information about the fact that |
| 7165 | * we're not limiting the upcoming send(), because if it |
| 7166 | * fails, we'll have to retry with at least as many data. |
| 7167 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7168 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7169 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 7170 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7171 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7172 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7173 | unsigned int max_early; |
| 7174 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7175 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7176 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7177 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7178 | if (SSL_get0_session(ctx->ssl)) |
| 7179 | 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] | 7180 | else |
| 7181 | max_early = 0; |
| 7182 | } |
| 7183 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7184 | if (try + ctx->sent_early_data > max_early) { |
| 7185 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7186 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 7187 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7188 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7189 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7190 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7191 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7192 | 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] | 7193 | if (ret == 1) { |
| 7194 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7195 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7196 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7197 | 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] | 7198 | /* Initiate the handshake, now */ |
| 7199 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 7200 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7201 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7202 | } |
| 7203 | |
| 7204 | } else |
| 7205 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7206 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7207 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7208 | if (conn->flags & CO_FL_ERROR) { |
| 7209 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7210 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7211 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7212 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 7213 | /* A send succeeded, so we can consider ourself connected */ |
| 7214 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7215 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7216 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7217 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7218 | } |
| 7219 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7220 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7221 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7222 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7223 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7224 | /* handshake is running, and it may need to re-enable write */ |
| 7225 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7226 | 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] | 7227 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7228 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7229 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7230 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7231 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7232 | break; |
| 7233 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7234 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7235 | break; |
| 7236 | } |
| 7237 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7238 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7239 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7240 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7241 | SUB_RETRY_RECV, |
| 7242 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7243 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7244 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7245 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7246 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7247 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7248 | break; |
| 7249 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7250 | goto out_error; |
| 7251 | } |
| 7252 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7253 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7254 | return done; |
| 7255 | |
| 7256 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7257 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7258 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7259 | ERR_clear_error(); |
| 7260 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7261 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7262 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7263 | } |
| 7264 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7265 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7266 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7267 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7268 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7269 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7270 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7271 | if (ctx->wait_event.events != 0) |
| 7272 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 7273 | ctx->wait_event.events, |
| 7274 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7275 | if (ctx->subs) { |
| 7276 | ctx->subs->events = 0; |
| 7277 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7278 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7279 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 7280 | if (ctx->xprt->close) |
| 7281 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7282 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7283 | if (global_ssl.async) { |
| 7284 | OSSL_ASYNC_FD all_fd[32], afd; |
| 7285 | size_t num_all_fds = 0; |
| 7286 | int i; |
| 7287 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7288 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7289 | if (num_all_fds > 32) { |
| 7290 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 7291 | return; |
| 7292 | } |
| 7293 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7294 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7295 | |
| 7296 | /* If an async job is pending, we must try to |
| 7297 | to catch the end using polling before calling |
| 7298 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7299 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7300 | for (i=0 ; i < num_all_fds ; i++) { |
| 7301 | /* switch on an handler designed to |
| 7302 | * handle the SSL_free |
| 7303 | */ |
| 7304 | afd = all_fd[i]; |
| 7305 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7306 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7307 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 7308 | /* To ensure that the fd cache won't be used |
| 7309 | * and we'll catch a real RD event. |
| 7310 | */ |
| 7311 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7312 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7313 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7314 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7315 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7316 | return; |
| 7317 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7318 | /* Else we can remove the fds from the fdtab |
| 7319 | * and call SSL_free. |
| 7320 | * note: we do a fd_remove and not a delete |
| 7321 | * because the fd is owned by the engine. |
| 7322 | * the engine is responsible to close |
| 7323 | */ |
| 7324 | for (i=0 ; i < num_all_fds ; i++) |
| 7325 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7326 | } |
| 7327 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7328 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7329 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7330 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7331 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 7332 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7333 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 7337 | * any case, flags the connection as reusable if no handshake was in progress. |
| 7338 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7339 | 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] | 7340 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7341 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7342 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7343 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7344 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 7345 | if (!clean) |
| 7346 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7347 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7348 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7349 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7350 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7351 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7352 | ERR_clear_error(); |
| 7353 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7354 | } |
| 7355 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7356 | /* fill a buffer with the algorithm and size of a public key */ |
| 7357 | static int cert_get_pkey_algo(X509 *crt, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7358 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7359 | int bits = 0; |
| 7360 | int sig = TLSEXT_signature_anonymous; |
| 7361 | int len = -1; |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7362 | EVP_PKEY *pkey; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7363 | |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7364 | pkey = X509_get_pubkey(crt); |
| 7365 | if (pkey) { |
| 7366 | bits = EVP_PKEY_bits(pkey); |
| 7367 | switch(EVP_PKEY_base_id(pkey)) { |
| 7368 | case EVP_PKEY_RSA: |
| 7369 | sig = TLSEXT_signature_rsa; |
| 7370 | break; |
| 7371 | case EVP_PKEY_EC: |
| 7372 | sig = TLSEXT_signature_ecdsa; |
| 7373 | break; |
| 7374 | case EVP_PKEY_DSA: |
| 7375 | sig = TLSEXT_signature_dsa; |
| 7376 | break; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7377 | } |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 7378 | EVP_PKEY_free(pkey); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | switch(sig) { |
| 7382 | case TLSEXT_signature_rsa: |
| 7383 | len = chunk_printf(out, "RSA%d", bits); |
| 7384 | break; |
| 7385 | case TLSEXT_signature_ecdsa: |
| 7386 | len = chunk_printf(out, "EC%d", bits); |
| 7387 | break; |
| 7388 | case TLSEXT_signature_dsa: |
| 7389 | len = chunk_printf(out, "DSA%d", bits); |
| 7390 | break; |
| 7391 | default: |
| 7392 | return 0; |
| 7393 | } |
| 7394 | if (len < 0) |
| 7395 | return 0; |
| 7396 | return 1; |
| 7397 | } |
| 7398 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 7399 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7400 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7401 | { |
| 7402 | struct ssl_sock_ctx *ctx; |
| 7403 | X509 *crt; |
| 7404 | |
| 7405 | if (!ssl_sock_is_ssl(conn)) |
| 7406 | return 0; |
| 7407 | |
| 7408 | ctx = conn->xprt_ctx; |
| 7409 | |
| 7410 | crt = SSL_get_certificate(ctx->ssl); |
| 7411 | if (!crt) |
| 7412 | return 0; |
| 7413 | |
| 7414 | return cert_get_pkey_algo(crt, out); |
| 7415 | } |
| 7416 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7417 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7418 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7419 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7420 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7421 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7422 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7423 | X509 *crt; |
| 7424 | |
| 7425 | if (!ssl_sock_is_ssl(conn)) |
| 7426 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7427 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7428 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7429 | if (!crt) |
| 7430 | return NULL; |
| 7431 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7432 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7433 | } |
| 7434 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7435 | /* used for ppv2 authority */ |
| 7436 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7437 | { |
| 7438 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7439 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7440 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7441 | if (!ssl_sock_is_ssl(conn)) |
| 7442 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7443 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7444 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7445 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7446 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7447 | #endif |
| 7448 | } |
| 7449 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7450 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7451 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7452 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7453 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7454 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7455 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7456 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7457 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7458 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7459 | } |
| 7460 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7461 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7462 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7463 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7464 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7465 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7466 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7467 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7468 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7469 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7470 | } |
| 7471 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7472 | /* Extract a serial from a cert, and copy it to a chunk. |
| 7473 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 7474 | * -1 if output is not large enough. |
| 7475 | */ |
| 7476 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7477 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7478 | { |
| 7479 | ASN1_INTEGER *serial; |
| 7480 | |
| 7481 | serial = X509_get_serialNumber(crt); |
| 7482 | if (!serial) |
| 7483 | return 0; |
| 7484 | |
| 7485 | if (out->size < serial->length) |
| 7486 | return -1; |
| 7487 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7488 | memcpy(out->area, serial->data, serial->length); |
| 7489 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7490 | return 1; |
| 7491 | } |
| 7492 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7493 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7494 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 7495 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7496 | */ |
| 7497 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7498 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7499 | { |
| 7500 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7501 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7502 | |
| 7503 | len =i2d_X509(crt, NULL); |
| 7504 | if (len <= 0) |
| 7505 | return 1; |
| 7506 | |
| 7507 | if (out->size < len) |
| 7508 | return -1; |
| 7509 | |
| 7510 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7511 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7512 | return 1; |
| 7513 | } |
| 7514 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7515 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7516 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7517 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 7518 | * and -1 if output is not large enough. |
| 7519 | */ |
| 7520 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7521 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7522 | { |
| 7523 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 7524 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 7525 | |
| 7526 | if (gentm->length < 12) |
| 7527 | return 0; |
| 7528 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 7529 | return 0; |
| 7530 | if (out->size < gentm->length-2) |
| 7531 | return -1; |
| 7532 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7533 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 7534 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7535 | return 1; |
| 7536 | } |
| 7537 | else if (tm->type == V_ASN1_UTCTIME) { |
| 7538 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 7539 | |
| 7540 | if (utctm->length < 10) |
| 7541 | return 0; |
| 7542 | if (utctm->data[0] >= 0x35) |
| 7543 | return 0; |
| 7544 | if (out->size < utctm->length) |
| 7545 | return -1; |
| 7546 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7547 | memcpy(out->area, utctm->data, utctm->length); |
| 7548 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7549 | return 1; |
| 7550 | } |
| 7551 | |
| 7552 | return 0; |
| 7553 | } |
| 7554 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7555 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 7556 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 7557 | */ |
| 7558 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7559 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 7560 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7561 | { |
| 7562 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7563 | ASN1_OBJECT *obj; |
| 7564 | ASN1_STRING *data; |
| 7565 | const unsigned char *data_ptr; |
| 7566 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7567 | int i, j, n; |
| 7568 | int cur = 0; |
| 7569 | const char *s; |
| 7570 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7571 | int name_count; |
| 7572 | |
| 7573 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7574 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7575 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7576 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7577 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7578 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7579 | else |
| 7580 | j = i; |
| 7581 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7582 | ne = X509_NAME_get_entry(a, j); |
| 7583 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7584 | data = X509_NAME_ENTRY_get_data(ne); |
| 7585 | data_ptr = ASN1_STRING_get0_data(data); |
| 7586 | data_len = ASN1_STRING_length(data); |
| 7587 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7588 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7589 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7590 | s = tmp; |
| 7591 | } |
| 7592 | |
| 7593 | if (chunk_strcasecmp(entry, s) != 0) |
| 7594 | continue; |
| 7595 | |
| 7596 | if (pos < 0) |
| 7597 | cur--; |
| 7598 | else |
| 7599 | cur++; |
| 7600 | |
| 7601 | if (cur != pos) |
| 7602 | continue; |
| 7603 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7604 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7605 | return -1; |
| 7606 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7607 | memcpy(out->area, data_ptr, data_len); |
| 7608 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7609 | return 1; |
| 7610 | } |
| 7611 | |
| 7612 | return 0; |
| 7613 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7614 | } |
| 7615 | |
| 7616 | /* |
| 7617 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 7618 | * Return 0; |
| 7619 | */ |
| 7620 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7621 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 7622 | { |
| 7623 | int i; |
| 7624 | char *str; |
| 7625 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 7626 | |
| 7627 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 7628 | if (names) { |
| 7629 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 7630 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 7631 | if (i > 0) |
| 7632 | chunk_appendf(out, ", "); |
| 7633 | if (name->type == GEN_DNS) { |
| 7634 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 7635 | chunk_appendf(out, "DNS:%s", str); |
| 7636 | OPENSSL_free(str); |
| 7637 | } |
| 7638 | } |
| 7639 | } |
| 7640 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 7641 | } |
| 7642 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7643 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7644 | #endif |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7645 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7646 | /* |
| 7647 | * Extract the DN in the specified format from the X509_NAME and copy result to a chunk. |
| 7648 | * Currently supports rfc2253 for returning LDAP V3 DNs. |
| 7649 | * Returns 1 if dn entries exist, 0 if no dn entry was found. |
| 7650 | */ |
| 7651 | static int |
| 7652 | ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out) |
| 7653 | { |
| 7654 | BIO *bio = NULL; |
| 7655 | int ret = 0; |
| 7656 | int data_len = 0; |
| 7657 | |
| 7658 | if (chunk_strcmp(format, "rfc2253") == 0) { |
| 7659 | bio = BIO_new(BIO_s_mem()); |
| 7660 | if (bio == NULL) |
| 7661 | goto out; |
| 7662 | |
| 7663 | if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0) |
| 7664 | goto out; |
| 7665 | |
| 7666 | if ((data_len = BIO_read(bio, out->area, out->size)) <= 0) |
| 7667 | goto out; |
| 7668 | |
| 7669 | out->data = data_len; |
| 7670 | |
| 7671 | ret = 1; |
| 7672 | } |
| 7673 | out: |
| 7674 | if (bio) |
| 7675 | BIO_free(bio); |
| 7676 | return ret; |
| 7677 | } |
| 7678 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7679 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 7680 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 7681 | */ |
| 7682 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7683 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7684 | { |
| 7685 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7686 | ASN1_OBJECT *obj; |
| 7687 | ASN1_STRING *data; |
| 7688 | const unsigned char *data_ptr; |
| 7689 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7690 | int i, n, ln; |
| 7691 | int l = 0; |
| 7692 | const char *s; |
| 7693 | char *p; |
| 7694 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7695 | int name_count; |
| 7696 | |
| 7697 | |
| 7698 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7699 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7700 | out->data = 0; |
| 7701 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7702 | for (i = 0; i < name_count; i++) { |
| 7703 | ne = X509_NAME_get_entry(a, i); |
| 7704 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7705 | data = X509_NAME_ENTRY_get_data(ne); |
| 7706 | data_ptr = ASN1_STRING_get0_data(data); |
| 7707 | data_len = ASN1_STRING_length(data); |
| 7708 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7709 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7710 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7711 | s = tmp; |
| 7712 | } |
| 7713 | ln = strlen(s); |
| 7714 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7715 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7716 | if (l > out->size) |
| 7717 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7718 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7719 | |
| 7720 | *(p++)='/'; |
| 7721 | memcpy(p, s, ln); |
| 7722 | p += ln; |
| 7723 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7724 | memcpy(p, data_ptr, data_len); |
| 7725 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7726 | } |
| 7727 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7728 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7729 | return 0; |
| 7730 | |
| 7731 | return 1; |
| 7732 | } |
| 7733 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7734 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7735 | { |
| 7736 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7737 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7738 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7739 | if (!ssl_sock_is_ssl(conn)) |
| 7740 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7741 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7742 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7743 | #endif |
| 7744 | } |
| 7745 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7746 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7747 | * to disable SNI. |
| 7748 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7749 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7750 | { |
| 7751 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7752 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7753 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7754 | char *prev_name; |
| 7755 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7756 | if (!ssl_sock_is_ssl(conn)) |
| 7757 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7758 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7759 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7760 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7761 | * new connection will present a new SNI. As an optimization we could |
| 7762 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7763 | * server. |
| 7764 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7765 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7766 | if ((!prev_name && hostname) || |
| 7767 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7768 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7769 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7770 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7771 | #endif |
| 7772 | } |
| 7773 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7774 | /* Extract peer certificate's common name into the chunk dest |
| 7775 | * Returns |
| 7776 | * the len of the extracted common name |
| 7777 | * or 0 if no CN found in DN |
| 7778 | * or -1 on error case (i.e. no peer certificate) |
| 7779 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7780 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7781 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7782 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7783 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7784 | X509 *crt = NULL; |
| 7785 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7786 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7787 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7788 | .area = (char *)&find_cn, |
| 7789 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7790 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7791 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7792 | |
| 7793 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7794 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7795 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7796 | |
| 7797 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7798 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7799 | if (!crt) |
| 7800 | goto out; |
| 7801 | |
| 7802 | name = X509_get_subject_name(crt); |
| 7803 | if (!name) |
| 7804 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7805 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7806 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7807 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7808 | if (crt) |
| 7809 | X509_free(crt); |
| 7810 | |
| 7811 | return result; |
| 7812 | } |
| 7813 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7814 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7815 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7816 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7817 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7818 | X509 *crt = NULL; |
| 7819 | |
| 7820 | if (!ssl_sock_is_ssl(conn)) |
| 7821 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7822 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7823 | |
| 7824 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7825 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7826 | if (!crt) |
| 7827 | return 0; |
| 7828 | |
| 7829 | X509_free(crt); |
| 7830 | return 1; |
| 7831 | } |
| 7832 | |
| 7833 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7834 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7835 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7836 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7837 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7838 | if (!ssl_sock_is_ssl(conn)) |
| 7839 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7840 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7841 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7842 | } |
| 7843 | |
| 7844 | /* returns result from SSL verify */ |
| 7845 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7846 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7847 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7848 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7849 | if (!ssl_sock_is_ssl(conn)) |
| 7850 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7851 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7852 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7853 | } |
| 7854 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7855 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7856 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7857 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7858 | * freed by the caller. NPN is also checked if available since older versions |
| 7859 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7860 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7861 | 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] | 7862 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7863 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7864 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7865 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7866 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7867 | return 0; |
| 7868 | |
| 7869 | *str = NULL; |
| 7870 | |
| 7871 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7872 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7873 | if (*str) |
| 7874 | return 1; |
| 7875 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7876 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7877 | 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] | 7878 | if (*str) |
| 7879 | return 1; |
| 7880 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7881 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7882 | return 0; |
| 7883 | } |
| 7884 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7885 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7886 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7887 | static int |
| 7888 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7889 | { |
| 7890 | struct connection *conn; |
| 7891 | |
| 7892 | conn = objt_conn(smp->sess->origin); |
| 7893 | if (!conn || conn->xprt != &ssl_sock) |
| 7894 | return 0; |
| 7895 | |
| 7896 | smp->flags = 0; |
| 7897 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7898 | #ifdef OPENSSL_IS_BORINGSSL |
| 7899 | { |
| 7900 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7901 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7902 | SSL_early_data_accepted(ctx->ssl)); |
| 7903 | } |
| 7904 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7905 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 7906 | (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] | 7907 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7908 | return 1; |
| 7909 | } |
| 7910 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7911 | /* boolean, returns true if client cert was present */ |
| 7912 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7913 | 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] | 7914 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7915 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7916 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7917 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7918 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7919 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7920 | return 0; |
| 7921 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7922 | ctx = conn->xprt_ctx; |
| 7923 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7924 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7925 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7926 | return 0; |
| 7927 | } |
| 7928 | |
| 7929 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7930 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7931 | 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] | 7932 | |
| 7933 | return 1; |
| 7934 | } |
| 7935 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7936 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7937 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7938 | * should be use. |
| 7939 | */ |
| 7940 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7941 | 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] | 7942 | { |
| 7943 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7944 | X509 *crt = NULL; |
| 7945 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7946 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7947 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7948 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7949 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7950 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7951 | if (!conn || conn->xprt != &ssl_sock) |
| 7952 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7953 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7954 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7955 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7956 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7957 | return 0; |
| 7958 | } |
| 7959 | |
| 7960 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7961 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7962 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7963 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7964 | |
| 7965 | if (!crt) |
| 7966 | goto out; |
| 7967 | |
| 7968 | smp_trash = get_trash_chunk(); |
| 7969 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7970 | goto out; |
| 7971 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7972 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7973 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7974 | ret = 1; |
| 7975 | out: |
| 7976 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7977 | if (cert_peer && crt) |
| 7978 | X509_free(crt); |
| 7979 | return ret; |
| 7980 | } |
| 7981 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7982 | /* binary, returns serial of certificate in a binary chunk. |
| 7983 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7984 | * should be use. |
| 7985 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7986 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7987 | 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] | 7988 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7989 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7990 | X509 *crt = NULL; |
| 7991 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7992 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7993 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7994 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7995 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7996 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7997 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7998 | return 0; |
| 7999 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8000 | ctx = conn->xprt_ctx; |
| 8001 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8002 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8003 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8004 | return 0; |
| 8005 | } |
| 8006 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8007 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8008 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8009 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8010 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8011 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8012 | if (!crt) |
| 8013 | goto out; |
| 8014 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8015 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8016 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 8017 | goto out; |
| 8018 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8019 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8020 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8021 | ret = 1; |
| 8022 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8023 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8024 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 8025 | X509_free(crt); |
| 8026 | return ret; |
| 8027 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 8028 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8029 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 8030 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8031 | * should be use. |
| 8032 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8033 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8034 | 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] | 8035 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8036 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8037 | X509 *crt = NULL; |
| 8038 | const EVP_MD *digest; |
| 8039 | int ret = 0; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8040 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8041 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8042 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8043 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8044 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8045 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8046 | if (!conn || conn->xprt != &ssl_sock) |
| 8047 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8048 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8049 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8050 | if (conn->flags & CO_FL_WAIT_XPRT) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8051 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8052 | return 0; |
| 8053 | } |
| 8054 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8055 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8056 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8057 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8058 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8059 | if (!crt) |
| 8060 | goto out; |
| 8061 | |
| 8062 | smp_trash = get_trash_chunk(); |
| 8063 | digest = EVP_sha1(); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8064 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 8065 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8066 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8067 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8068 | ret = 1; |
| 8069 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8070 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8071 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 8072 | X509_free(crt); |
| 8073 | return ret; |
| 8074 | } |
| 8075 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8076 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 8077 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8078 | * should be use. |
| 8079 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8080 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8081 | 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] | 8082 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8083 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8084 | X509 *crt = NULL; |
| 8085 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8086 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8087 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8088 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8089 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8090 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8091 | if (!conn || conn->xprt != &ssl_sock) |
| 8092 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8093 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8094 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8095 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8096 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8097 | return 0; |
| 8098 | } |
| 8099 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8100 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8101 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8102 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8103 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8104 | if (!crt) |
| 8105 | goto out; |
| 8106 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8107 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8108 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8109 | goto out; |
| 8110 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8111 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8112 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8113 | ret = 1; |
| 8114 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8115 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8116 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8117 | X509_free(crt); |
| 8118 | return ret; |
| 8119 | } |
| 8120 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8121 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 8122 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8123 | * should be use. |
| 8124 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8125 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8126 | 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] | 8127 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8128 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8129 | X509 *crt = NULL; |
| 8130 | X509_NAME *name; |
| 8131 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8132 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8133 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8134 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8135 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8136 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8137 | if (!conn || conn->xprt != &ssl_sock) |
| 8138 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8139 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8140 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8141 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8142 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8143 | return 0; |
| 8144 | } |
| 8145 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8146 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8147 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8148 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8149 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8150 | if (!crt) |
| 8151 | goto out; |
| 8152 | |
| 8153 | name = X509_get_issuer_name(crt); |
| 8154 | if (!name) |
| 8155 | goto out; |
| 8156 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8157 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8158 | 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] | 8159 | int pos = 1; |
| 8160 | |
| 8161 | if (args[1].type == ARGT_SINT) |
| 8162 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8163 | |
| 8164 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8165 | goto out; |
| 8166 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8167 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8168 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8169 | goto out; |
| 8170 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8171 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8172 | goto out; |
| 8173 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8174 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8175 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8176 | ret = 1; |
| 8177 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8178 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8179 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8180 | X509_free(crt); |
| 8181 | return ret; |
| 8182 | } |
| 8183 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8184 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 8185 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8186 | * should be use. |
| 8187 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8188 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8189 | 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] | 8190 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8191 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8192 | X509 *crt = NULL; |
| 8193 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8194 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8195 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8196 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8197 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8198 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8199 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8200 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8201 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8202 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8203 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8204 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8205 | return 0; |
| 8206 | } |
| 8207 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8208 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8209 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8210 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8211 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8212 | if (!crt) |
| 8213 | goto out; |
| 8214 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8215 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8216 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8217 | goto out; |
| 8218 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8219 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8220 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8221 | ret = 1; |
| 8222 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8223 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8224 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 8225 | X509_free(crt); |
| 8226 | return ret; |
| 8227 | } |
| 8228 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8229 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 8230 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8231 | * should be use. |
| 8232 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8233 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8234 | 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] | 8235 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8236 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8237 | X509 *crt = NULL; |
| 8238 | X509_NAME *name; |
| 8239 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8240 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8241 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8242 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8243 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8244 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8245 | if (!conn || conn->xprt != &ssl_sock) |
| 8246 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8247 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8248 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8249 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8250 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8251 | return 0; |
| 8252 | } |
| 8253 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8254 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8255 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8256 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8257 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8258 | if (!crt) |
| 8259 | goto out; |
| 8260 | |
| 8261 | name = X509_get_subject_name(crt); |
| 8262 | if (!name) |
| 8263 | goto out; |
| 8264 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8265 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8266 | 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] | 8267 | int pos = 1; |
| 8268 | |
| 8269 | if (args[1].type == ARGT_SINT) |
| 8270 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8271 | |
| 8272 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 8273 | goto out; |
| 8274 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 8275 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 8276 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 8277 | goto out; |
| 8278 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8279 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 8280 | goto out; |
| 8281 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8282 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8283 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8284 | ret = 1; |
| 8285 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8286 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 8287 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 8288 | X509_free(crt); |
| 8289 | return ret; |
| 8290 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8291 | |
| 8292 | /* integer, returns true if current session use a client certificate */ |
| 8293 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8294 | 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] | 8295 | { |
| 8296 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8297 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8298 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8299 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8300 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8301 | if (!conn || conn->xprt != &ssl_sock) |
| 8302 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8303 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8304 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8305 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8306 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8307 | return 0; |
| 8308 | } |
| 8309 | |
| 8310 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8311 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8312 | if (crt) { |
| 8313 | X509_free(crt); |
| 8314 | } |
| 8315 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8316 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8317 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 8318 | return 1; |
| 8319 | } |
| 8320 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8321 | /* integer, returns the certificate version |
| 8322 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8323 | * should be use. |
| 8324 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8325 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8326 | 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] | 8327 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8328 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8329 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8330 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8331 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8332 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8333 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8334 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8335 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8336 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8337 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8338 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8339 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8340 | return 0; |
| 8341 | } |
| 8342 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8343 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8344 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8345 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8346 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8347 | if (!crt) |
| 8348 | return 0; |
| 8349 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8350 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8351 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8352 | if (cert_peer) |
| 8353 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8354 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 8355 | |
| 8356 | return 1; |
| 8357 | } |
| 8358 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8359 | /* string, returns the certificate's signature algorithm. |
| 8360 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8361 | * should be use. |
| 8362 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8363 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8364 | 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] | 8365 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8366 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8367 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8368 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8369 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8370 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8371 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8372 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8373 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8374 | if (!conn || conn->xprt != &ssl_sock) |
| 8375 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8376 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8377 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8378 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8379 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8380 | return 0; |
| 8381 | } |
| 8382 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8383 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8384 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8385 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8386 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8387 | if (!crt) |
| 8388 | return 0; |
| 8389 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8390 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 8391 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8392 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8393 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8394 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8395 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8396 | if (cert_peer) |
| 8397 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8398 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8399 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8400 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8401 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8402 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8403 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8404 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8405 | if (cert_peer) |
| 8406 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8407 | |
| 8408 | return 1; |
| 8409 | } |
| 8410 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8411 | /* string, returns the certificate's key algorithm. |
| 8412 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8413 | * should be use. |
| 8414 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8415 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8416 | 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] | 8417 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8418 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8419 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8420 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8421 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8422 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8423 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8424 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8425 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8426 | if (!conn || conn->xprt != &ssl_sock) |
| 8427 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8428 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8429 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8430 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8431 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8432 | return 0; |
| 8433 | } |
| 8434 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8435 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8436 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8437 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8438 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8439 | if (!crt) |
| 8440 | return 0; |
| 8441 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8442 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 8443 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8444 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8445 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8446 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8447 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8448 | if (cert_peer) |
| 8449 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8450 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8451 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8452 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8453 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8454 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8455 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8456 | if (cert_peer) |
| 8457 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8458 | |
| 8459 | return 1; |
| 8460 | } |
| 8461 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8462 | /* boolean, returns true if front conn. transport layer is SSL. |
| 8463 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8464 | * char is 'b'. |
| 8465 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8466 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8467 | 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] | 8468 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8469 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8470 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8471 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8472 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8473 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8474 | return 1; |
| 8475 | } |
| 8476 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8477 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8478 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8479 | 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] | 8480 | { |
| 8481 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8482 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8483 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8484 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8485 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8486 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8487 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8488 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8489 | return 1; |
| 8490 | #else |
| 8491 | return 0; |
| 8492 | #endif |
| 8493 | } |
| 8494 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8495 | /* boolean, returns true if client session has been resumed. |
| 8496 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8497 | * char is 'b'. |
| 8498 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8499 | static int |
| 8500 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8501 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8502 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8503 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8504 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8505 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8506 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8507 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8508 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8509 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8510 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8511 | return 1; |
| 8512 | } |
| 8513 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8514 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 8515 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8516 | * char is 'b'. |
| 8517 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8518 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8519 | 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] | 8520 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8521 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8522 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8523 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8524 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8525 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8526 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8527 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8528 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8529 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8530 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8531 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8532 | return 0; |
| 8533 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8534 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8535 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8536 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8537 | |
| 8538 | return 1; |
| 8539 | } |
| 8540 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8541 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 8542 | * is SSL. |
| 8543 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8544 | * char is 'b'. |
| 8545 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8546 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8547 | 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] | 8548 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8549 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8550 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8551 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8552 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8553 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8554 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8555 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8556 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8557 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8558 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8559 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8560 | return 0; |
| 8561 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8562 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8563 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8564 | |
| 8565 | return 1; |
| 8566 | } |
| 8567 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8568 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 8569 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8570 | * char is 'b'. |
| 8571 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8572 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8573 | 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] | 8574 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8575 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8576 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8577 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8578 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8579 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8580 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8581 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8582 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8583 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8584 | 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] | 8585 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8586 | return 0; |
| 8587 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8588 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8589 | |
| 8590 | return 1; |
| 8591 | } |
| 8592 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8593 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8594 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8595 | 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] | 8596 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8597 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8598 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8599 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8600 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8601 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8602 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8603 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8604 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8605 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8606 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8607 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8608 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8609 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8610 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8611 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8612 | (const unsigned char **)&smp->data.u.str.area, |
| 8613 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8614 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8615 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8616 | return 0; |
| 8617 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8618 | smp->data.u.str.data = len; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8619 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8620 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8621 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8622 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8623 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8624 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8625 | 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] | 8626 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8627 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8628 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8629 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8630 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8631 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8632 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8633 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8634 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8635 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8636 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8637 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8638 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8639 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8640 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8641 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8642 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8643 | (const unsigned char **)&smp->data.u.str.area, |
| 8644 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8645 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8646 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8647 | return 0; |
| 8648 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8649 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8650 | return 1; |
| 8651 | } |
| 8652 | #endif |
| 8653 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8654 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 8655 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8656 | * char is 'b'. |
| 8657 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8658 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8659 | 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] | 8660 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8661 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8662 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8663 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8664 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8665 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8666 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8667 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8668 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8669 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8670 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8671 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8672 | return 0; |
| 8673 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8674 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8675 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8676 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8677 | |
| 8678 | return 1; |
| 8679 | } |
| 8680 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8681 | /* 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] | 8682 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8683 | * char is 'b'. |
| 8684 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8685 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8686 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8687 | 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] | 8688 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8689 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8690 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8691 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8692 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8693 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8694 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8695 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8696 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8697 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8698 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8699 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8700 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8701 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8702 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8703 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8704 | return 0; |
| 8705 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8706 | 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] | 8707 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8708 | return 0; |
| 8709 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 8710 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8711 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8712 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8713 | #endif |
| 8714 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8715 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8716 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8717 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 8718 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8719 | { |
| 8720 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8721 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8722 | struct buffer *data; |
| 8723 | struct ssl_sock_ctx *ctx; |
| 8724 | |
| 8725 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8726 | return 0; |
| 8727 | ctx = conn->xprt_ctx; |
| 8728 | |
| 8729 | data = get_trash_chunk(); |
| 8730 | if (kw[7] == 'c') |
| 8731 | data->data = SSL_get_client_random(ctx->ssl, |
| 8732 | (unsigned char *) data->area, |
| 8733 | data->size); |
| 8734 | else |
| 8735 | data->data = SSL_get_server_random(ctx->ssl, |
| 8736 | (unsigned char *) data->area, |
| 8737 | data->size); |
| 8738 | if (!data->data) |
| 8739 | return 0; |
| 8740 | |
| 8741 | smp->flags = 0; |
| 8742 | smp->data.type = SMP_T_BIN; |
| 8743 | smp->data.u.str = *data; |
| 8744 | |
| 8745 | return 1; |
| 8746 | } |
| 8747 | |
| 8748 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8749 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8750 | { |
| 8751 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8752 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8753 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8754 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8755 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8756 | |
| 8757 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8758 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8759 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8760 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8761 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8762 | if (!ssl_sess) |
| 8763 | return 0; |
| 8764 | |
| 8765 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8766 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8767 | (unsigned char *) data->area, |
| 8768 | data->size); |
| 8769 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8770 | return 0; |
| 8771 | |
| 8772 | smp->flags = 0; |
| 8773 | smp->data.type = SMP_T_BIN; |
| 8774 | smp->data.u.str = *data; |
| 8775 | |
| 8776 | return 1; |
| 8777 | } |
| 8778 | #endif |
| 8779 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8780 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8781 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8782 | 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] | 8783 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8784 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8785 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8786 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8787 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8788 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8789 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8790 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8791 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8792 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8793 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8794 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8795 | 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] | 8796 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8797 | return 0; |
| 8798 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8799 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8800 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8801 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8802 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8803 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8804 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8805 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8806 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8807 | struct connection *conn; |
| 8808 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8809 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8810 | |
| 8811 | conn = objt_conn(smp->sess->origin); |
| 8812 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8813 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8814 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8815 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8816 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8817 | if (!capture) |
| 8818 | return 0; |
| 8819 | |
| 8820 | smp->flags = SMP_F_CONST; |
| 8821 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8822 | smp->data.u.str.area = capture->ciphersuite; |
| 8823 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8824 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8825 | } |
| 8826 | |
| 8827 | static int |
| 8828 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8829 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8830 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8831 | |
| 8832 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8833 | return 0; |
| 8834 | |
| 8835 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8836 | 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] | 8837 | smp->data.type = SMP_T_BIN; |
| 8838 | smp->data.u.str = *data; |
| 8839 | return 1; |
| 8840 | } |
| 8841 | |
| 8842 | static int |
| 8843 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8844 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8845 | struct connection *conn; |
| 8846 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8847 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8848 | |
| 8849 | conn = objt_conn(smp->sess->origin); |
| 8850 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8851 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8852 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8853 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8854 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8855 | if (!capture) |
| 8856 | return 0; |
| 8857 | |
| 8858 | smp->data.type = SMP_T_SINT; |
| 8859 | smp->data.u.sint = capture->xxh64; |
| 8860 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8861 | } |
| 8862 | |
| 8863 | static int |
| 8864 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8865 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8866 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8867 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8868 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8869 | |
| 8870 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8871 | return 0; |
| 8872 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8873 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8874 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8875 | const char *str; |
| 8876 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8877 | 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] | 8878 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8879 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8880 | cipher = SSL_get_cipher_by_value(id); |
| 8881 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8882 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8883 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8884 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8885 | #endif |
| 8886 | str = SSL_CIPHER_get_name(cipher); |
| 8887 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8888 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8889 | else |
| 8890 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8891 | } |
| 8892 | smp->data.type = SMP_T_STR; |
| 8893 | smp->data.u.str = *data; |
| 8894 | return 1; |
| 8895 | #else |
| 8896 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8897 | #endif |
| 8898 | } |
| 8899 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8900 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8901 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8902 | 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] | 8903 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8904 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8905 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8906 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8907 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8908 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8909 | |
| 8910 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8911 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8912 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8913 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8914 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8915 | if (conn->flags & CO_FL_WAIT_XPRT) { |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8916 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8917 | return 0; |
| 8918 | } |
| 8919 | |
| 8920 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8921 | if (!SSL_session_reused(ctx->ssl)) |
| 8922 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8923 | finished_trash->area, |
| 8924 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8925 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8926 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8927 | finished_trash->area, |
| 8928 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8929 | |
| 8930 | if (!finished_len) |
| 8931 | return 0; |
| 8932 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8933 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8934 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8935 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8936 | |
| 8937 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8938 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8939 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8940 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8941 | /* 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] | 8942 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8943 | 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] | 8944 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8945 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8946 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8947 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8948 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8949 | if (!conn || conn->xprt != &ssl_sock) |
| 8950 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8951 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8952 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8953 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8954 | smp->flags = SMP_F_MAY_CHANGE; |
| 8955 | return 0; |
| 8956 | } |
| 8957 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8958 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8959 | 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] | 8960 | smp->flags = 0; |
| 8961 | |
| 8962 | return 1; |
| 8963 | } |
| 8964 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8965 | /* 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] | 8966 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8967 | 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] | 8968 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8969 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8970 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8971 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8972 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8973 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8974 | return 0; |
| 8975 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8976 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8977 | smp->flags = SMP_F_MAY_CHANGE; |
| 8978 | return 0; |
| 8979 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8980 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8981 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8982 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8983 | 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] | 8984 | smp->flags = 0; |
| 8985 | |
| 8986 | return 1; |
| 8987 | } |
| 8988 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8989 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8990 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8991 | 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] | 8992 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8993 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8994 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8995 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8996 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8997 | if (!conn || conn->xprt != &ssl_sock) |
| 8998 | return 0; |
| 8999 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 9000 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 9001 | smp->flags = SMP_F_MAY_CHANGE; |
| 9002 | return 0; |
| 9003 | } |
| 9004 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 9005 | ctx = conn->xprt_ctx; |
| 9006 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9007 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 9008 | 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] | 9009 | smp->flags = 0; |
| 9010 | |
| 9011 | return 1; |
| 9012 | } |
| 9013 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 9014 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9015 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9016 | 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] | 9017 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9018 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9019 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9020 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9021 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9022 | if (!conn || conn->xprt != &ssl_sock) |
| 9023 | return 0; |
| 9024 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 9025 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9026 | smp->flags = SMP_F_MAY_CHANGE; |
| 9027 | return 0; |
| 9028 | } |
| 9029 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9030 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9031 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9032 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 9033 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9034 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 9035 | 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] | 9036 | smp->flags = 0; |
| 9037 | |
| 9038 | return 1; |
| 9039 | } |
| 9040 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9041 | /* for ca-file and ca-verify-file */ |
| 9042 | 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] | 9043 | { |
| 9044 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9045 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9046 | return ERR_ALERT | ERR_FATAL; |
| 9047 | } |
| 9048 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9049 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9050 | 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] | 9051 | else |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9052 | memprintf(ca_file_p, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9053 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9054 | if (!ssl_store_load_locations_file(*ca_file_p)) { |
| 9055 | 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] | 9056 | return ERR_ALERT | ERR_FATAL; |
| 9057 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9058 | return 0; |
| 9059 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9060 | |
| 9061 | /* parse the "ca-file" bind keyword */ |
| 9062 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9063 | { |
| 9064 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err); |
| 9065 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9066 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9067 | { |
| 9068 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9069 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9070 | |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 9071 | /* parse the "ca-verify-file" bind keyword */ |
| 9072 | static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9073 | { |
| 9074 | return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err); |
| 9075 | } |
| 9076 | static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9077 | { |
| 9078 | return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9079 | } |
| 9080 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9081 | /* parse the "ca-sign-file" bind keyword */ |
| 9082 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9083 | { |
| 9084 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9085 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9086 | return ERR_ALERT | ERR_FATAL; |
| 9087 | } |
| 9088 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9089 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9090 | 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] | 9091 | else |
| 9092 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 9093 | |
| 9094 | return 0; |
| 9095 | } |
| 9096 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9097 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9098 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9099 | { |
| 9100 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9101 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9102 | return ERR_ALERT | ERR_FATAL; |
| 9103 | } |
| 9104 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 9105 | return 0; |
| 9106 | } |
| 9107 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9108 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9109 | 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] | 9110 | { |
| 9111 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9112 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9113 | return ERR_ALERT | ERR_FATAL; |
| 9114 | } |
| 9115 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9116 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9117 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9118 | return 0; |
| 9119 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9120 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9121 | { |
| 9122 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 9123 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9124 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9125 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9126 | /* parse the "ciphersuites" bind keyword */ |
| 9127 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9128 | { |
| 9129 | if (!*args[cur_arg + 1]) { |
| 9130 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 9131 | return ERR_ALERT | ERR_FATAL; |
| 9132 | } |
| 9133 | |
| 9134 | free(conf->ciphersuites); |
| 9135 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 9136 | return 0; |
| 9137 | } |
| 9138 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9139 | { |
| 9140 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 9141 | } |
| 9142 | #endif |
| 9143 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9144 | /* 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] | 9145 | 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] | 9146 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 9147 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 9148 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9149 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 9150 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9151 | return ERR_ALERT | ERR_FATAL; |
| 9152 | } |
| 9153 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9154 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 9155 | 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] | 9156 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 9157 | return ERR_ALERT | ERR_FATAL; |
| 9158 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9159 | 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] | 9160 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9161 | } |
| 9162 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9163 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9164 | } |
| 9165 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9166 | /* 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] | 9167 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9168 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9169 | int err_code; |
| 9170 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9171 | if (!*args[cur_arg + 1]) { |
| 9172 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 9173 | return ERR_ALERT | ERR_FATAL; |
| 9174 | } |
| 9175 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 9176 | 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] | 9177 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 9178 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9179 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 9180 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 9181 | } |
| 9182 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 9183 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9184 | 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] | 9185 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9186 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9187 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9188 | return ERR_ALERT | ERR_FATAL; |
| 9189 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9190 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9191 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9192 | return ERR_ALERT | ERR_FATAL; |
| 9193 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9194 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9195 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9196 | 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] | 9197 | else |
| 9198 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 9199 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9200 | if (!ssl_store_load_locations_file(conf->crl_file)) { |
| 9201 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file); |
| 9202 | return ERR_ALERT | ERR_FATAL; |
| 9203 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9204 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 9205 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9206 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9207 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9208 | { |
| 9209 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 9210 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9211 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9212 | /* parse the "curves" bind keyword keyword */ |
| 9213 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9214 | { |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 9215 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9216 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9217 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9218 | return ERR_ALERT | ERR_FATAL; |
| 9219 | } |
| 9220 | conf->curves = strdup(args[cur_arg + 1]); |
| 9221 | return 0; |
| 9222 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9223 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 9224 | return ERR_ALERT | ERR_FATAL; |
| 9225 | #endif |
| 9226 | } |
| 9227 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9228 | { |
| 9229 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 9230 | } |
| 9231 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9232 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9233 | 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] | 9234 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9235 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9236 | 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] | 9237 | return ERR_ALERT | ERR_FATAL; |
| 9238 | #elif defined(OPENSSL_NO_ECDH) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9239 | 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] | 9240 | return ERR_ALERT | ERR_FATAL; |
| 9241 | #else |
| 9242 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9243 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9244 | return ERR_ALERT | ERR_FATAL; |
| 9245 | } |
| 9246 | |
| 9247 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9248 | |
| 9249 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 9250 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9251 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9252 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9253 | { |
| 9254 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 9255 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9256 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 9257 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9258 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9259 | { |
| 9260 | int code; |
| 9261 | char *p = args[cur_arg + 1]; |
| 9262 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 9263 | |
| 9264 | if (!*p) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9265 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9266 | return ERR_ALERT | ERR_FATAL; |
| 9267 | } |
| 9268 | |
| 9269 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 9270 | ignerr = &conf->ca_ignerr; |
| 9271 | |
| 9272 | if (strcmp(p, "all") == 0) { |
| 9273 | *ignerr = ~0ULL; |
| 9274 | return 0; |
| 9275 | } |
| 9276 | |
| 9277 | while (p) { |
| 9278 | code = atoi(p); |
| 9279 | if ((code <= 0) || (code > 63)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9280 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 9281 | args[cur_arg], code, args[cur_arg + 1]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9282 | return ERR_ALERT | ERR_FATAL; |
| 9283 | } |
| 9284 | *ignerr |= 1ULL << code; |
| 9285 | p = strchr(p, ','); |
| 9286 | if (p) |
| 9287 | p++; |
| 9288 | } |
| 9289 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9290 | return 0; |
| 9291 | } |
| 9292 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9293 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 9294 | 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] | 9295 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9296 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9297 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9298 | p = strchr(arg, '-'); |
| 9299 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9300 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9301 | p++; |
| 9302 | if (!strcmp(p, "sslv3")) |
| 9303 | v = CONF_SSLV3; |
| 9304 | else if (!strcmp(p, "tlsv10")) |
| 9305 | v = CONF_TLSV10; |
| 9306 | else if (!strcmp(p, "tlsv11")) |
| 9307 | v = CONF_TLSV11; |
| 9308 | else if (!strcmp(p, "tlsv12")) |
| 9309 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9310 | else if (!strcmp(p, "tlsv13")) |
| 9311 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9312 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9313 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9314 | if (!strncmp(arg, "no-", 3)) |
| 9315 | methods->flags |= methodVersions[v].flag; |
| 9316 | else if (!strncmp(arg, "force-", 6)) |
| 9317 | methods->min = methods->max = v; |
| 9318 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9319 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9320 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9321 | fail: |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9322 | memprintf(err, "'%s' : option not implemented", arg); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9323 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9324 | } |
| 9325 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9326 | 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] | 9327 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9328 | 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] | 9329 | } |
| 9330 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9331 | 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] | 9332 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9333 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 9334 | } |
| 9335 | |
| 9336 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 9337 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 9338 | { |
| 9339 | uint16_t i, v = 0; |
| 9340 | char *argv = args[cur_arg + 1]; |
| 9341 | if (!*argv) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9342 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9343 | return ERR_ALERT | ERR_FATAL; |
| 9344 | } |
| 9345 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9346 | if (!strcmp(argv, methodVersions[i].name)) |
| 9347 | v = i; |
| 9348 | if (!v) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9349 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9350 | return ERR_ALERT | ERR_FATAL; |
| 9351 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9352 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 9353 | methods->min = v; |
| 9354 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 9355 | methods->max = v; |
| 9356 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9357 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9358 | return ERR_ALERT | ERR_FATAL; |
| 9359 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9360 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9361 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 9362 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9363 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9364 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9365 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9366 | 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] | 9367 | #endif |
| 9368 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 9369 | } |
| 9370 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9371 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9372 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9373 | 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] | 9374 | } |
| 9375 | |
| 9376 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9377 | { |
| 9378 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 9379 | } |
| 9380 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9381 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9382 | 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] | 9383 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 9384 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 9385 | return 0; |
| 9386 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 9387 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9388 | /* parse the "allow-0rtt" bind keyword */ |
| 9389 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9390 | { |
| 9391 | conf->early_data = 1; |
| 9392 | return 0; |
| 9393 | } |
| 9394 | |
| 9395 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9396 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 9397 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9398 | return 0; |
| 9399 | } |
| 9400 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9401 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9402 | 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] | 9403 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9404 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9405 | char *p1, *p2; |
| 9406 | |
| 9407 | if (!*args[cur_arg + 1]) { |
| 9408 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 9409 | return ERR_ALERT | ERR_FATAL; |
| 9410 | } |
| 9411 | |
| 9412 | free(conf->npn_str); |
| 9413 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9414 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9415 | * so we reuse each comma to store the next <len> and need |
| 9416 | * one more for the end of the string. |
| 9417 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9418 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9419 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9420 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 9421 | |
| 9422 | /* replace commas with the name length */ |
| 9423 | p1 = conf->npn_str; |
| 9424 | p2 = p1 + 1; |
| 9425 | while (1) { |
| 9426 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 9427 | if (!p2) |
| 9428 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9429 | |
| 9430 | if (p2 - (p1 + 1) > 255) { |
| 9431 | *p2 = '\0'; |
| 9432 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9433 | return ERR_ALERT | ERR_FATAL; |
| 9434 | } |
| 9435 | |
| 9436 | *p1 = p2 - (p1 + 1); |
| 9437 | p1 = p2; |
| 9438 | |
| 9439 | if (!*p2) |
| 9440 | break; |
| 9441 | |
| 9442 | *(p2++) = '\0'; |
| 9443 | } |
| 9444 | return 0; |
| 9445 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9446 | 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] | 9447 | return ERR_ALERT | ERR_FATAL; |
| 9448 | #endif |
| 9449 | } |
| 9450 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9451 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9452 | { |
| 9453 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9454 | } |
| 9455 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9456 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9457 | static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9458 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9459 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9460 | char *p1, *p2; |
| 9461 | |
| 9462 | if (!*args[cur_arg + 1]) { |
| 9463 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 9464 | return ERR_ALERT | ERR_FATAL; |
| 9465 | } |
| 9466 | |
| 9467 | free(conf->alpn_str); |
| 9468 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9469 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9470 | * so we reuse each comma to store the next <len> and need |
| 9471 | * one more for the end of the string. |
| 9472 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9473 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9474 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9475 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 9476 | |
| 9477 | /* replace commas with the name length */ |
| 9478 | p1 = conf->alpn_str; |
| 9479 | p2 = p1 + 1; |
| 9480 | while (1) { |
| 9481 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 9482 | if (!p2) |
| 9483 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9484 | |
| 9485 | if (p2 - (p1 + 1) > 255) { |
| 9486 | *p2 = '\0'; |
| 9487 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9488 | return ERR_ALERT | ERR_FATAL; |
| 9489 | } |
| 9490 | |
| 9491 | *p1 = p2 - (p1 + 1); |
| 9492 | p1 = p2; |
| 9493 | |
| 9494 | if (!*p2) |
| 9495 | break; |
| 9496 | |
| 9497 | *(p2++) = '\0'; |
| 9498 | } |
| 9499 | return 0; |
| 9500 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9501 | 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] | 9502 | return ERR_ALERT | ERR_FATAL; |
| 9503 | #endif |
| 9504 | } |
| 9505 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9506 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9507 | { |
| 9508 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9509 | } |
| 9510 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9511 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9512 | 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] | 9513 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 9514 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9515 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9516 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9517 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 9518 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9519 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9520 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 9521 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9522 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9523 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9524 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 9525 | if (!conf->ssl_conf.ssl_methods.min) |
| 9526 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 9527 | if (!conf->ssl_conf.ssl_methods.max) |
| 9528 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9529 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9530 | return 0; |
| 9531 | } |
| 9532 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9533 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 9534 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9535 | { |
| 9536 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 9537 | return 0; |
| 9538 | } |
| 9539 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9540 | /* parse the "generate-certificates" bind keyword */ |
| 9541 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9542 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9543 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9544 | conf->generate_certs = 1; |
| 9545 | #else |
| 9546 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 9547 | err && *err ? *err : ""); |
| 9548 | #endif |
| 9549 | return 0; |
| 9550 | } |
| 9551 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9552 | /* parse the "strict-sni" bind keyword */ |
| 9553 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9554 | { |
| 9555 | conf->strict_sni = 1; |
| 9556 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9557 | } |
| 9558 | |
| 9559 | /* parse the "tls-ticket-keys" bind keyword */ |
| 9560 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9561 | { |
| 9562 | #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] | 9563 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9564 | int i = 0; |
| 9565 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9566 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9567 | |
| 9568 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9569 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9570 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9571 | } |
| 9572 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9573 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9574 | if (keys_ref) { |
| 9575 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9576 | conf->keys_ref = keys_ref; |
| 9577 | return 0; |
| 9578 | } |
| 9579 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9580 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9581 | if (!keys_ref) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9582 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9583 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9584 | } |
| 9585 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9586 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9587 | if (!keys_ref->tlskeys) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9588 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9589 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9590 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9591 | |
| 9592 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9593 | 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] | 9594 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9595 | } |
| 9596 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9597 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9598 | if (!keys_ref->filename) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9599 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9600 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9601 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9602 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9603 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9604 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 9605 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9606 | int dec_size; |
| 9607 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9608 | /* Strip newline characters from the end */ |
| 9609 | if(thisline[len - 1] == '\n') |
| 9610 | thisline[--len] = 0; |
| 9611 | |
| 9612 | if(thisline[len - 1] == '\r') |
| 9613 | thisline[--len] = 0; |
| 9614 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9615 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 9616 | if (dec_size < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9617 | 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] | 9618 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9619 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9620 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 9621 | keys_ref->key_size_bits = 128; |
| 9622 | } |
| 9623 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 9624 | keys_ref->key_size_bits = 256; |
| 9625 | } |
| 9626 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 9627 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 9628 | || ((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] | 9629 | 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] | 9630 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9631 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9632 | i++; |
| 9633 | } |
| 9634 | |
| 9635 | if (i < TLS_TICKETS_NO) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9636 | 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] | 9637 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9638 | } |
| 9639 | |
| 9640 | fclose(f); |
| 9641 | |
| 9642 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 9643 | i -= 2; |
| 9644 | 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] | 9645 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9646 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9647 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9648 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9649 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9650 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 9651 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9652 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9653 | |
| 9654 | fail: |
| 9655 | if (f) |
| 9656 | fclose(f); |
| 9657 | if (keys_ref) { |
| 9658 | free(keys_ref->filename); |
| 9659 | free(keys_ref->tlskeys); |
| 9660 | free(keys_ref); |
| 9661 | } |
| 9662 | return ERR_ALERT | ERR_FATAL; |
| 9663 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9664 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9665 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9666 | return ERR_ALERT | ERR_FATAL; |
| 9667 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9668 | } |
| 9669 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9670 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9671 | 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] | 9672 | { |
| 9673 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9674 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9675 | return ERR_ALERT | ERR_FATAL; |
| 9676 | } |
| 9677 | |
| 9678 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9679 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9680 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9681 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9682 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9683 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9684 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9685 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 9686 | args[cur_arg], args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9687 | return ERR_ALERT | ERR_FATAL; |
| 9688 | } |
| 9689 | |
| 9690 | return 0; |
| 9691 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9692 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9693 | { |
| 9694 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 9695 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9696 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9697 | /* parse the "no-ca-names" bind keyword */ |
| 9698 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9699 | { |
| 9700 | conf->no_ca_names = 1; |
| 9701 | return 0; |
| 9702 | } |
| 9703 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9704 | { |
| 9705 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 9706 | } |
| 9707 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9708 | /************** "server" keywords ****************/ |
| 9709 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9710 | /* parse the "npn" bind keyword */ |
| 9711 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9712 | { |
| 9713 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9714 | char *p1, *p2; |
| 9715 | |
| 9716 | if (!*args[*cur_arg + 1]) { |
| 9717 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 9718 | return ERR_ALERT | ERR_FATAL; |
| 9719 | } |
| 9720 | |
| 9721 | free(newsrv->ssl_ctx.npn_str); |
| 9722 | |
| 9723 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9724 | * so we reuse each comma to store the next <len> and need |
| 9725 | * one more for the end of the string. |
| 9726 | */ |
| 9727 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9728 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 9729 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 9730 | newsrv->ssl_ctx.npn_len); |
| 9731 | |
| 9732 | /* replace commas with the name length */ |
| 9733 | p1 = newsrv->ssl_ctx.npn_str; |
| 9734 | p2 = p1 + 1; |
| 9735 | while (1) { |
| 9736 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 9737 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 9738 | if (!p2) |
| 9739 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9740 | |
| 9741 | if (p2 - (p1 + 1) > 255) { |
| 9742 | *p2 = '\0'; |
| 9743 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9744 | return ERR_ALERT | ERR_FATAL; |
| 9745 | } |
| 9746 | |
| 9747 | *p1 = p2 - (p1 + 1); |
| 9748 | p1 = p2; |
| 9749 | |
| 9750 | if (!*p2) |
| 9751 | break; |
| 9752 | |
| 9753 | *(p2++) = '\0'; |
| 9754 | } |
| 9755 | return 0; |
| 9756 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9757 | 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] | 9758 | return ERR_ALERT | ERR_FATAL; |
| 9759 | #endif |
| 9760 | } |
| 9761 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9762 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9763 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9764 | { |
| 9765 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9766 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9767 | char **alpn_str; |
| 9768 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9769 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9770 | if (*args[*cur_arg] == 'c') { |
| 9771 | alpn_str = &newsrv->check.alpn_str; |
| 9772 | alpn_len = &newsrv->check.alpn_len; |
| 9773 | } else { |
| 9774 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9775 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9776 | |
| 9777 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9778 | if (!*args[*cur_arg + 1]) { |
| 9779 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 9780 | return ERR_ALERT | ERR_FATAL; |
| 9781 | } |
| 9782 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9783 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9784 | |
| 9785 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9786 | * so we reuse each comma to store the next <len> and need |
| 9787 | * one more for the end of the string. |
| 9788 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9789 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9790 | *alpn_str = calloc(1, *alpn_len + 1); |
| 9791 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9792 | |
| 9793 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9794 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9795 | p2 = p1 + 1; |
| 9796 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9797 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9798 | if (!p2) |
| 9799 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9800 | |
| 9801 | if (p2 - (p1 + 1) > 255) { |
| 9802 | *p2 = '\0'; |
| 9803 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9804 | return ERR_ALERT | ERR_FATAL; |
| 9805 | } |
| 9806 | |
| 9807 | *p1 = p2 - (p1 + 1); |
| 9808 | p1 = p2; |
| 9809 | |
| 9810 | if (!*p2) |
| 9811 | break; |
| 9812 | |
| 9813 | *(p2++) = '\0'; |
| 9814 | } |
| 9815 | return 0; |
| 9816 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9817 | 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] | 9818 | return ERR_ALERT | ERR_FATAL; |
| 9819 | #endif |
| 9820 | } |
| 9821 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9822 | /* parse the "ca-file" server keyword */ |
| 9823 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9824 | { |
| 9825 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9826 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9827 | return ERR_ALERT | ERR_FATAL; |
| 9828 | } |
| 9829 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9830 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9831 | 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] | 9832 | else |
| 9833 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9834 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 9835 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) { |
| 9836 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file); |
| 9837 | return ERR_ALERT | ERR_FATAL; |
| 9838 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9839 | return 0; |
| 9840 | } |
| 9841 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9842 | /* parse the "check-sni" server keyword */ |
| 9843 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9844 | { |
| 9845 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9846 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9847 | return ERR_ALERT | ERR_FATAL; |
| 9848 | } |
| 9849 | |
| 9850 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9851 | if (!newsrv->check.sni) { |
| 9852 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9853 | return ERR_ALERT | ERR_FATAL; |
| 9854 | } |
| 9855 | return 0; |
| 9856 | |
| 9857 | } |
| 9858 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9859 | /* parse the "check-ssl" server keyword */ |
| 9860 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9861 | { |
| 9862 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9863 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9864 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9865 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9866 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9867 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9868 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9869 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9870 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9871 | if (!newsrv->ssl_ctx.methods.min) |
| 9872 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9873 | if (!newsrv->ssl_ctx.methods.max) |
| 9874 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9875 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9876 | return 0; |
| 9877 | } |
| 9878 | |
| 9879 | /* parse the "ciphers" server keyword */ |
| 9880 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9881 | { |
| 9882 | if (!*args[*cur_arg + 1]) { |
| 9883 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9884 | return ERR_ALERT | ERR_FATAL; |
| 9885 | } |
| 9886 | |
| 9887 | free(newsrv->ssl_ctx.ciphers); |
| 9888 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9889 | return 0; |
| 9890 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9891 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9892 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9893 | /* parse the "ciphersuites" server keyword */ |
| 9894 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9895 | { |
| 9896 | if (!*args[*cur_arg + 1]) { |
| 9897 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9898 | return ERR_ALERT | ERR_FATAL; |
| 9899 | } |
| 9900 | |
| 9901 | free(newsrv->ssl_ctx.ciphersuites); |
| 9902 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9903 | return 0; |
| 9904 | } |
| 9905 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9906 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9907 | /* parse the "crl-file" server keyword */ |
| 9908 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9909 | { |
| 9910 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9911 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9912 | return ERR_ALERT | ERR_FATAL; |
| 9913 | #else |
| 9914 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9915 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9916 | return ERR_ALERT | ERR_FATAL; |
| 9917 | } |
| 9918 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9919 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9920 | 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] | 9921 | else |
| 9922 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9923 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9924 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) { |
| 9925 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file); |
| 9926 | return ERR_ALERT | ERR_FATAL; |
| 9927 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9928 | return 0; |
| 9929 | #endif |
| 9930 | } |
| 9931 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9932 | /* parse the "crt" server keyword */ |
| 9933 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9934 | { |
| 9935 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9936 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9937 | return ERR_ALERT | ERR_FATAL; |
| 9938 | } |
| 9939 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9940 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9941 | 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] | 9942 | else |
| 9943 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9944 | |
| 9945 | return 0; |
| 9946 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9947 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9948 | /* parse the "no-check-ssl" server keyword */ |
| 9949 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9950 | { |
| 9951 | newsrv->check.use_ssl = 0; |
| 9952 | free(newsrv->ssl_ctx.ciphers); |
| 9953 | newsrv->ssl_ctx.ciphers = NULL; |
| 9954 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9955 | return 0; |
| 9956 | } |
| 9957 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9958 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9959 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9960 | { |
| 9961 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9962 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9963 | return 0; |
| 9964 | } |
| 9965 | |
| 9966 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9967 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9968 | { |
| 9969 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9970 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9971 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9972 | return 0; |
| 9973 | } |
| 9974 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9975 | /* parse the "no-ssl" server keyword */ |
| 9976 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9977 | { |
| 9978 | newsrv->use_ssl = 0; |
| 9979 | free(newsrv->ssl_ctx.ciphers); |
| 9980 | newsrv->ssl_ctx.ciphers = NULL; |
| 9981 | return 0; |
| 9982 | } |
| 9983 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9984 | /* parse the "allow-0rtt" server keyword */ |
| 9985 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9986 | { |
| 9987 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9988 | return 0; |
| 9989 | } |
| 9990 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9991 | /* parse the "no-ssl-reuse" server keyword */ |
| 9992 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9993 | { |
| 9994 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9995 | return 0; |
| 9996 | } |
| 9997 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9998 | /* parse the "no-tls-tickets" server keyword */ |
| 9999 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10000 | { |
| 10001 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 10002 | return 0; |
| 10003 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 10004 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 10005 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10006 | { |
| 10007 | newsrv->pp_opts |= SRV_PP_V2; |
| 10008 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 10009 | return 0; |
| 10010 | } |
| 10011 | |
| 10012 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 10013 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10014 | { |
| 10015 | newsrv->pp_opts |= SRV_PP_V2; |
| 10016 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 10017 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 10018 | return 0; |
| 10019 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 10020 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10021 | /* parse the "sni" server keyword */ |
| 10022 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10023 | { |
| 10024 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 10025 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 10026 | return ERR_ALERT | ERR_FATAL; |
| 10027 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10028 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10029 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10030 | arg = args[*cur_arg + 1]; |
| 10031 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10032 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 10033 | return ERR_ALERT | ERR_FATAL; |
| 10034 | } |
| 10035 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 10036 | free(newsrv->sni_expr); |
| 10037 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10038 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 10039 | return 0; |
| 10040 | #endif |
| 10041 | } |
| 10042 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10043 | /* parse the "ssl" server keyword */ |
| 10044 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10045 | { |
| 10046 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10047 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 10048 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10049 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10050 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 10051 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 10052 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10053 | return 0; |
| 10054 | } |
| 10055 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 10056 | /* parse the "ssl-reuse" server keyword */ |
| 10057 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10058 | { |
| 10059 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 10060 | return 0; |
| 10061 | } |
| 10062 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 10063 | /* parse the "tls-tickets" server keyword */ |
| 10064 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10065 | { |
| 10066 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 10067 | return 0; |
| 10068 | } |
| 10069 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10070 | /* parse the "verify" server keyword */ |
| 10071 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10072 | { |
| 10073 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10074 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10075 | return ERR_ALERT | ERR_FATAL; |
| 10076 | } |
| 10077 | |
| 10078 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 10079 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10080 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 10081 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10082 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10083 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 10084 | args[*cur_arg], args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10085 | return ERR_ALERT | ERR_FATAL; |
| 10086 | } |
| 10087 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10088 | return 0; |
| 10089 | } |
| 10090 | |
| 10091 | /* parse the "verifyhost" server keyword */ |
| 10092 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 10093 | { |
| 10094 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 10095 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10096 | return ERR_ALERT | ERR_FATAL; |
| 10097 | } |
| 10098 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 10099 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 10100 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 10101 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 10102 | return 0; |
| 10103 | } |
| 10104 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10105 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 10106 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 10107 | struct proxy *defpx, const char *file, int line, |
| 10108 | char **err) { |
| 10109 | int i = 1; |
| 10110 | |
| 10111 | if (*(args[i]) == 0) { |
| 10112 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10113 | return -1; |
| 10114 | } |
| 10115 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10116 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10117 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 10118 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 10119 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10120 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10121 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 10122 | i++; |
| 10123 | else { |
| 10124 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10125 | return -1; |
| 10126 | } |
| 10127 | } |
| 10128 | 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] | 10129 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10130 | return -1; |
| 10131 | } |
| 10132 | i++; |
| 10133 | } |
| 10134 | return 0; |
| 10135 | } |
| 10136 | |
| 10137 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 10138 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 10139 | struct proxy *defpx, const char *file, int line, |
| 10140 | char **err) { |
| 10141 | int i = 1; |
| 10142 | |
| 10143 | if (*(args[i]) == 0) { |
| 10144 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 10145 | return -1; |
| 10146 | } |
| 10147 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10148 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10149 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10150 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 10151 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 10152 | i++; |
| 10153 | else { |
| 10154 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 10155 | return -1; |
| 10156 | } |
| 10157 | } |
| 10158 | 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] | 10159 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 10160 | return -1; |
| 10161 | } |
| 10162 | i++; |
| 10163 | } |
| 10164 | return 0; |
| 10165 | } |
| 10166 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10167 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 10168 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10169 | */ |
| 10170 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 10171 | struct proxy *defpx, const char *file, int line, |
| 10172 | char **err) |
| 10173 | { |
| 10174 | char **target; |
| 10175 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10176 | 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] | 10177 | |
| 10178 | if (too_many_args(1, args, err, NULL)) |
| 10179 | return -1; |
| 10180 | |
| 10181 | if (*target) { |
| 10182 | memprintf(err, "'%s' already specified.", args[0]); |
| 10183 | return -1; |
| 10184 | } |
| 10185 | |
| 10186 | if (*(args[1]) == 0) { |
| 10187 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 10188 | return -1; |
| 10189 | } |
| 10190 | *target = strdup(args[1]); |
| 10191 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10192 | } |
| 10193 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10194 | /* "issuers-chain-path" load chain certificate in global */ |
| 10195 | static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
| 10196 | { |
| 10197 | X509 *ca; |
| 10198 | X509_NAME *name = NULL; |
| 10199 | ASN1_OCTET_STRING *skid = NULL; |
| 10200 | STACK_OF(X509) *chain = NULL; |
| 10201 | struct issuer_chain *issuer; |
| 10202 | struct eb64_node *node; |
| 10203 | char *path; |
| 10204 | u64 key; |
| 10205 | int ret = 0; |
| 10206 | |
| 10207 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 10208 | if (chain == NULL) { |
| 10209 | chain = sk_X509_new_null(); |
| 10210 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 10211 | name = X509_get_subject_name(ca); |
| 10212 | } |
| 10213 | if (!sk_X509_push(chain, ca)) { |
| 10214 | X509_free(ca); |
| 10215 | goto end; |
| 10216 | } |
| 10217 | } |
| 10218 | if (!chain) { |
| 10219 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 10220 | goto end; |
| 10221 | } |
| 10222 | if (!skid) { |
| 10223 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 10224 | goto end; |
| 10225 | } |
| 10226 | if (!name) { |
| 10227 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 10228 | goto end; |
| 10229 | } |
| 10230 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10231 | 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] | 10232 | issuer = container_of(node, typeof(*issuer), node); |
| 10233 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 10234 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 10235 | goto end; |
| 10236 | } |
| 10237 | } |
| 10238 | issuer = calloc(1, sizeof *issuer); |
| 10239 | path = strdup(fp); |
| 10240 | if (!issuer || !path) { |
| 10241 | free(issuer); |
| 10242 | free(path); |
| 10243 | goto end; |
| 10244 | } |
| 10245 | issuer->node.key = key; |
| 10246 | issuer->path = path; |
| 10247 | issuer->chain = chain; |
| 10248 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10249 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10250 | ret = 1; |
| 10251 | end: |
| 10252 | if (skid) |
| 10253 | ASN1_OCTET_STRING_free(skid); |
| 10254 | if (chain) |
| 10255 | sk_X509_pop_free(chain, X509_free); |
| 10256 | return ret; |
| 10257 | } |
| 10258 | |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 10259 | static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 10260 | { |
| 10261 | AUTHORITY_KEYID *akid; |
| 10262 | struct issuer_chain *issuer = NULL; |
| 10263 | |
| 10264 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 10265 | if (akid) { |
| 10266 | struct eb64_node *node; |
| 10267 | u64 hk; |
| 10268 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 10269 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 10270 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 10271 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 10272 | issuer = ti; |
| 10273 | break; |
| 10274 | } |
| 10275 | } |
| 10276 | AUTHORITY_KEYID_free(akid); |
| 10277 | } |
| 10278 | return issuer; |
| 10279 | } |
| 10280 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10281 | static void ssl_free_global_issuers(void) |
| 10282 | { |
| 10283 | struct eb64_node *node, *back; |
| 10284 | struct issuer_chain *issuer; |
| 10285 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 10286 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10287 | while (node) { |
| 10288 | issuer = container_of(node, typeof(*issuer), node); |
| 10289 | back = eb64_next(node); |
| 10290 | eb64_delete(node); |
| 10291 | free(issuer->path); |
| 10292 | sk_X509_pop_free(issuer->chain, X509_free); |
| 10293 | free(issuer); |
| 10294 | node = back; |
| 10295 | } |
| 10296 | } |
| 10297 | |
| 10298 | static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx, |
| 10299 | struct proxy *defpx, const char *file, int line, |
| 10300 | char **err) |
| 10301 | { |
| 10302 | char *path; |
| 10303 | struct dirent **de_list; |
| 10304 | int i, n; |
| 10305 | struct stat buf; |
| 10306 | char *end; |
| 10307 | char fp[MAXPATHLEN+1]; |
| 10308 | |
| 10309 | if (too_many_args(1, args, err, NULL)) |
| 10310 | return -1; |
| 10311 | |
| 10312 | path = args[1]; |
| 10313 | if (*path == 0 || stat(path, &buf)) { |
| 10314 | memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n", |
| 10315 | err && *err ? *err : "", args[0]); |
| 10316 | return -1; |
| 10317 | } |
| 10318 | if (S_ISDIR(buf.st_mode) == 0) { |
| 10319 | memprintf(err, "%sglobal statement '%s': %s is not a directory.\n", |
| 10320 | err && *err ? *err : "", args[0], path); |
| 10321 | return -1; |
| 10322 | } |
| 10323 | |
| 10324 | /* strip trailing slashes, including first one */ |
| 10325 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 10326 | *end = 0; |
| 10327 | /* path already parsed? */ |
| 10328 | if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0) |
| 10329 | return 0; |
| 10330 | /* overwrite old issuers_chain_path */ |
| 10331 | free(global_ssl.issuers_chain_path); |
| 10332 | global_ssl.issuers_chain_path = strdup(path); |
| 10333 | ssl_free_global_issuers(); |
| 10334 | |
| 10335 | n = scandir(path, &de_list, 0, alphasort); |
| 10336 | if (n < 0) { |
| 10337 | memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n", |
| 10338 | err && *err ? *err : "", args[0], path, strerror(errno)); |
| 10339 | return -1; |
| 10340 | } |
| 10341 | for (i = 0; i < n; i++) { |
| 10342 | struct dirent *de = de_list[i]; |
| 10343 | BIO *in = NULL; |
| 10344 | char *warn = NULL; |
| 10345 | |
| 10346 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 10347 | free(de); |
| 10348 | if (stat(fp, &buf) != 0) { |
| 10349 | ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno)); |
| 10350 | goto next; |
| 10351 | } |
| 10352 | if (!S_ISREG(buf.st_mode)) |
| 10353 | goto next; |
| 10354 | |
| 10355 | in = BIO_new(BIO_s_file()); |
| 10356 | if (in == NULL) |
| 10357 | goto next; |
| 10358 | if (BIO_read_filename(in, fp) <= 0) |
| 10359 | goto next; |
| 10360 | ssl_load_global_issuer_from_BIO(in, fp, &warn); |
| 10361 | if (warn) { |
Tim Duesterhus | e8aa5f2 | 2020-02-19 11:41:13 +0100 | [diff] [blame] | 10362 | ha_warning("%s", warn); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 10363 | free(warn); |
| 10364 | warn = NULL; |
| 10365 | } |
| 10366 | next: |
| 10367 | if (in) |
| 10368 | BIO_free(in); |
| 10369 | } |
| 10370 | free(de_list); |
| 10371 | |
| 10372 | return 0; |
| 10373 | } |
| 10374 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10375 | /* parse the "ssl-mode-async" keyword in global section. |
| 10376 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10377 | */ |
| 10378 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 10379 | struct proxy *defpx, const char *file, int line, |
| 10380 | char **err) |
| 10381 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10382 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10383 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 10384 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10385 | return 0; |
| 10386 | #else |
| 10387 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 10388 | return -1; |
| 10389 | #endif |
| 10390 | } |
| 10391 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10392 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10393 | static int ssl_check_async_engine_count(void) { |
| 10394 | int err_code = 0; |
| 10395 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 10396 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 10397 | 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] | 10398 | err_code = ERR_ABORT; |
| 10399 | } |
| 10400 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10401 | } |
| 10402 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10403 | /* parse the "ssl-engine" keyword in global section. |
| 10404 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10405 | */ |
| 10406 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 10407 | struct proxy *defpx, const char *file, int line, |
| 10408 | char **err) |
| 10409 | { |
| 10410 | char *algo; |
| 10411 | int ret = -1; |
| 10412 | |
| 10413 | if (*(args[1]) == 0) { |
| 10414 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 10415 | return ret; |
| 10416 | } |
| 10417 | |
| 10418 | if (*(args[2]) == 0) { |
| 10419 | /* if no list of algorithms is given, it defaults to ALL */ |
| 10420 | algo = strdup("ALL"); |
| 10421 | goto add_engine; |
| 10422 | } |
| 10423 | |
| 10424 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 10425 | if (strcmp(args[2], "algo") != 0) { |
| 10426 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 10427 | return ret; |
| 10428 | } |
| 10429 | |
| 10430 | if (*(args[3]) == 0) { |
| 10431 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 10432 | return ret; |
| 10433 | } |
| 10434 | algo = strdup(args[3]); |
| 10435 | |
| 10436 | add_engine: |
| 10437 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 10438 | openssl_engines_initialized++; |
| 10439 | ret = 0; |
| 10440 | } |
| 10441 | free(algo); |
| 10442 | return ret; |
| 10443 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10444 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10445 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10446 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 10447 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10448 | */ |
| 10449 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 10450 | struct proxy *defpx, const char *file, int line, |
| 10451 | char **err) |
| 10452 | { |
| 10453 | char **target; |
| 10454 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10455 | 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] | 10456 | |
| 10457 | if (too_many_args(1, args, err, NULL)) |
| 10458 | return -1; |
| 10459 | |
| 10460 | if (*(args[1]) == 0) { |
| 10461 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10462 | return -1; |
| 10463 | } |
| 10464 | |
| 10465 | free(*target); |
| 10466 | *target = strdup(args[1]); |
| 10467 | return 0; |
| 10468 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10469 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10470 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10471 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 10472 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10473 | */ |
| 10474 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 10475 | struct proxy *defpx, const char *file, int line, |
| 10476 | char **err) |
| 10477 | { |
| 10478 | char **target; |
| 10479 | |
| 10480 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 10481 | |
| 10482 | if (too_many_args(1, args, err, NULL)) |
| 10483 | return -1; |
| 10484 | |
| 10485 | if (*(args[1]) == 0) { |
| 10486 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10487 | return -1; |
| 10488 | } |
| 10489 | |
| 10490 | free(*target); |
| 10491 | *target = strdup(args[1]); |
| 10492 | return 0; |
| 10493 | } |
| 10494 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10495 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10496 | /* parse various global tune.ssl settings consisting in positive integers. |
| 10497 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10498 | */ |
| 10499 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 10500 | struct proxy *defpx, const char *file, int line, |
| 10501 | char **err) |
| 10502 | { |
| 10503 | int *target; |
| 10504 | |
| 10505 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 10506 | target = &global.tune.sslcachesize; |
| 10507 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10508 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10509 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10510 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10511 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 10512 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10513 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 10514 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10515 | else { |
| 10516 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 10517 | return -1; |
| 10518 | } |
| 10519 | |
| 10520 | if (too_many_args(1, args, err, NULL)) |
| 10521 | return -1; |
| 10522 | |
| 10523 | if (*(args[1]) == 0) { |
| 10524 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10525 | return -1; |
| 10526 | } |
| 10527 | |
| 10528 | *target = atoi(args[1]); |
| 10529 | if (*target < 0) { |
| 10530 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 10531 | return -1; |
| 10532 | } |
| 10533 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10534 | } |
| 10535 | |
| 10536 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 10537 | struct proxy *defpx, const char *file, int line, |
| 10538 | char **err) |
| 10539 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10540 | int ret; |
| 10541 | |
| 10542 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 10543 | if (ret != 0) |
| 10544 | return ret; |
| 10545 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10546 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10547 | memprintf(err, "'%s' is already configured.", args[0]); |
| 10548 | return -1; |
| 10549 | } |
| 10550 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10551 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 10552 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10553 | memprintf(err, "Out of memory error."); |
| 10554 | return -1; |
| 10555 | } |
| 10556 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10557 | } |
| 10558 | |
| 10559 | /* parse "ssl.force-private-cache". |
| 10560 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10561 | */ |
| 10562 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 10563 | struct proxy *defpx, const char *file, int line, |
| 10564 | char **err) |
| 10565 | { |
| 10566 | if (too_many_args(0, args, err, NULL)) |
| 10567 | return -1; |
| 10568 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10569 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10570 | return 0; |
| 10571 | } |
| 10572 | |
| 10573 | /* parse "ssl.lifetime". |
| 10574 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10575 | */ |
| 10576 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 10577 | struct proxy *defpx, const char *file, int line, |
| 10578 | char **err) |
| 10579 | { |
| 10580 | const char *res; |
| 10581 | |
| 10582 | if (too_many_args(1, args, err, NULL)) |
| 10583 | return -1; |
| 10584 | |
| 10585 | if (*(args[1]) == 0) { |
| 10586 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 10587 | return -1; |
| 10588 | } |
| 10589 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10590 | 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] | 10591 | if (res == PARSE_TIME_OVER) { |
| 10592 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 10593 | args[1], args[0]); |
| 10594 | return -1; |
| 10595 | } |
| 10596 | else if (res == PARSE_TIME_UNDER) { |
| 10597 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 10598 | args[1], args[0]); |
| 10599 | return -1; |
| 10600 | } |
| 10601 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10602 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 10603 | return -1; |
| 10604 | } |
| 10605 | return 0; |
| 10606 | } |
| 10607 | |
| 10608 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10609 | /* parse "ssl-dh-param-file". |
| 10610 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10611 | */ |
| 10612 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 10613 | struct proxy *defpx, const char *file, int line, |
| 10614 | char **err) |
| 10615 | { |
| 10616 | if (too_many_args(1, args, err, NULL)) |
| 10617 | return -1; |
| 10618 | |
| 10619 | if (*(args[1]) == 0) { |
| 10620 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 10621 | return -1; |
| 10622 | } |
| 10623 | |
| 10624 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 10625 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 10626 | return -1; |
| 10627 | } |
| 10628 | return 0; |
| 10629 | } |
| 10630 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10631 | /* parse "ssl.default-dh-param". |
| 10632 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10633 | */ |
| 10634 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 10635 | struct proxy *defpx, const char *file, int line, |
| 10636 | char **err) |
| 10637 | { |
| 10638 | if (too_many_args(1, args, err, NULL)) |
| 10639 | return -1; |
| 10640 | |
| 10641 | if (*(args[1]) == 0) { |
| 10642 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10643 | return -1; |
| 10644 | } |
| 10645 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10646 | global_ssl.default_dh_param = atoi(args[1]); |
| 10647 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10648 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 10649 | return -1; |
| 10650 | } |
| 10651 | return 0; |
| 10652 | } |
| 10653 | #endif |
| 10654 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10655 | |
| 10656 | /* |
| 10657 | * parse "ssl-load-extra-files". |
| 10658 | * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none" |
| 10659 | */ |
| 10660 | static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx, |
| 10661 | struct proxy *defpx, const char *file, int line, |
| 10662 | char **err) |
| 10663 | { |
| 10664 | int i; |
| 10665 | int gf = SSL_GF_NONE; |
| 10666 | |
| 10667 | if (*(args[1]) == 0) |
| 10668 | goto err_arg; |
| 10669 | |
| 10670 | for (i = 1; *args[i]; i++) { |
| 10671 | |
| 10672 | if (!strcmp("bundle", args[i])) { |
| 10673 | gf |= SSL_GF_BUNDLE; |
| 10674 | |
| 10675 | } else if (!strcmp("sctl", args[i])) { |
| 10676 | gf |= SSL_GF_SCTL; |
| 10677 | |
| 10678 | } else if (!strcmp("ocsp", args[i])){ |
| 10679 | gf |= SSL_GF_OCSP; |
| 10680 | |
| 10681 | } else if (!strcmp("issuer", args[i])){ |
| 10682 | gf |= SSL_GF_OCSP_ISSUER; |
| 10683 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10684 | } else if (!strcmp("key", args[i])) { |
| 10685 | gf |= SSL_GF_KEY; |
| 10686 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10687 | } else if (!strcmp("none", args[i])) { |
| 10688 | if (gf != SSL_GF_NONE) |
| 10689 | goto err_alone; |
| 10690 | gf = SSL_GF_NONE; |
| 10691 | i++; |
| 10692 | break; |
| 10693 | |
| 10694 | } else if (!strcmp("all", args[i])) { |
| 10695 | if (gf != SSL_GF_NONE) |
| 10696 | goto err_alone; |
| 10697 | gf = SSL_GF_ALL; |
| 10698 | i++; |
| 10699 | break; |
| 10700 | } else { |
| 10701 | goto err_arg; |
| 10702 | } |
| 10703 | } |
| 10704 | /* break from loop but there are still arguments */ |
| 10705 | if (*args[i]) |
| 10706 | goto err_alone; |
| 10707 | |
| 10708 | global_ssl.extra_files = gf; |
| 10709 | |
| 10710 | return 0; |
| 10711 | |
| 10712 | err_alone: |
| 10713 | memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]); |
| 10714 | return -1; |
| 10715 | |
| 10716 | err_arg: |
| 10717 | memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]); |
| 10718 | return -1; |
| 10719 | } |
| 10720 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10721 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10722 | /* This function is used with TLS ticket keys management. It permits to browse |
| 10723 | * each reference. The variable <getnext> must contain the current node, |
| 10724 | * <end> point to the root node. |
| 10725 | */ |
| 10726 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10727 | static inline |
| 10728 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 10729 | { |
| 10730 | struct tls_keys_ref *ref = getnext; |
| 10731 | |
| 10732 | while (1) { |
| 10733 | |
| 10734 | /* Get next list entry. */ |
| 10735 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 10736 | |
| 10737 | /* If the entry is the last of the list, return NULL. */ |
| 10738 | if (&ref->list == end) |
| 10739 | return NULL; |
| 10740 | |
| 10741 | return ref; |
| 10742 | } |
| 10743 | } |
| 10744 | |
| 10745 | static inline |
| 10746 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 10747 | { |
| 10748 | int id; |
| 10749 | char *error; |
| 10750 | |
| 10751 | /* If the reference starts by a '#', this is numeric id. */ |
| 10752 | if (reference[0] == '#') { |
| 10753 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 10754 | id = strtol(reference + 1, &error, 10); |
| 10755 | if (*error != '\0') |
| 10756 | return NULL; |
| 10757 | |
| 10758 | /* Perform the unique id lookup. */ |
| 10759 | return tlskeys_ref_lookupid(id); |
| 10760 | } |
| 10761 | |
| 10762 | /* Perform the string lookup. */ |
| 10763 | return tlskeys_ref_lookup(reference); |
| 10764 | } |
| 10765 | #endif |
| 10766 | |
| 10767 | |
| 10768 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10769 | |
| 10770 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 10771 | |
| 10772 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 10773 | return cli_io_handler_tlskeys_files(appctx); |
| 10774 | } |
| 10775 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10776 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 10777 | * (next index to be dumped), and cli.p0 (next key reference). |
| 10778 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10779 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 10780 | |
| 10781 | struct stream_interface *si = appctx->owner; |
| 10782 | |
| 10783 | switch (appctx->st2) { |
| 10784 | case STAT_ST_INIT: |
| 10785 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 10786 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10787 | * later and restart at the state "STAT_ST_INIT". |
| 10788 | */ |
| 10789 | chunk_reset(&trash); |
| 10790 | |
| 10791 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 10792 | chunk_appendf(&trash, "# id secret\n"); |
| 10793 | else |
| 10794 | chunk_appendf(&trash, "# id (file)\n"); |
| 10795 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10796 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10797 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10798 | return 0; |
| 10799 | } |
| 10800 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10801 | /* Now, we start the browsing of the references lists. |
| 10802 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 10803 | * available field of this pointer is <list>. It is used with the function |
| 10804 | * tlskeys_list_get_next() for retruning the first available entry |
| 10805 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10806 | if (appctx->ctx.cli.p0 == NULL) { |
| 10807 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 10808 | 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] | 10809 | } |
| 10810 | |
| 10811 | appctx->st2 = STAT_ST_LIST; |
| 10812 | /* fall through */ |
| 10813 | |
| 10814 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10815 | while (appctx->ctx.cli.p0) { |
| 10816 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10817 | |
| 10818 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10819 | 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] | 10820 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10821 | |
| 10822 | if (appctx->ctx.cli.i1 == 0) |
| 10823 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 10824 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10825 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10826 | int head; |
| 10827 | |
| 10828 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 10829 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10830 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10831 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10832 | |
| 10833 | chunk_reset(t2); |
| 10834 | /* 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] | 10835 | if (ref->key_size_bits == 128) { |
| 10836 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10837 | sizeof(struct tls_sess_key_128), |
| 10838 | t2->area, t2->size); |
| 10839 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10840 | t2->area); |
| 10841 | } |
| 10842 | else if (ref->key_size_bits == 256) { |
| 10843 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10844 | sizeof(struct tls_sess_key_256), |
| 10845 | t2->area, t2->size); |
| 10846 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10847 | t2->area); |
| 10848 | } |
| 10849 | else { |
| 10850 | /* This case should never happen */ |
| 10851 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 10852 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10853 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10854 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10855 | /* let's try again later from this stream. We add ourselves into |
| 10856 | * this stream's users so that it can remove us upon termination. |
| 10857 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10858 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10859 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10860 | return 0; |
| 10861 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10862 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10863 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10864 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10865 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10866 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10867 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10868 | /* let's try again later from this stream. We add ourselves into |
| 10869 | * this stream's users so that it can remove us upon termination. |
| 10870 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10871 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10872 | return 0; |
| 10873 | } |
| 10874 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10875 | 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] | 10876 | break; |
| 10877 | |
| 10878 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10879 | 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] | 10880 | } |
| 10881 | |
| 10882 | appctx->st2 = STAT_ST_FIN; |
| 10883 | /* fall through */ |
| 10884 | |
| 10885 | default: |
| 10886 | appctx->st2 = STAT_ST_FIN; |
| 10887 | return 1; |
| 10888 | } |
| 10889 | return 0; |
| 10890 | } |
| 10891 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10892 | /* 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] | 10893 | 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] | 10894 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10895 | /* no parameter, shows only file list */ |
| 10896 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10897 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10898 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10899 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10900 | } |
| 10901 | |
| 10902 | if (args[2][0] == '*') { |
| 10903 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10904 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10905 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10906 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10907 | if (!appctx->ctx.cli.p0) |
| 10908 | 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] | 10909 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10910 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10911 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10912 | } |
| 10913 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10914 | 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] | 10915 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10916 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10917 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10918 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10919 | /* 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] | 10920 | if (!*args[3] || !*args[4]) |
| 10921 | 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] | 10922 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10923 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10924 | if (!ref) |
| 10925 | 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] | 10926 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10927 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10928 | if (ret < 0) |
| 10929 | 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] | 10930 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10931 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10932 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 10933 | 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] | 10934 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10935 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10936 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10937 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10938 | |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 10939 | /* |
| 10940 | * Take an ssl_bind_conf structure and append the configuration line used to |
| 10941 | * create it in the buffer |
| 10942 | */ |
| 10943 | static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf) |
| 10944 | { |
| 10945 | int space = 0; |
| 10946 | |
| 10947 | if (conf == NULL) |
| 10948 | return; |
| 10949 | |
| 10950 | chunk_appendf(buf, " ["); |
| 10951 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 10952 | if (conf->npn_str) { |
| 10953 | int len = conf->npn_len; |
| 10954 | char *ptr = conf->npn_str; |
| 10955 | int comma = 0; |
| 10956 | |
| 10957 | if (space) chunk_appendf(buf, " "); |
| 10958 | chunk_appendf(buf, "npn "); |
| 10959 | while (len) { |
| 10960 | unsigned short size; |
| 10961 | |
| 10962 | size = *ptr; |
| 10963 | ptr++; |
| 10964 | if (comma) |
| 10965 | chunk_memcat(buf, ",", 1); |
| 10966 | chunk_memcat(buf, ptr, size); |
| 10967 | ptr += size; |
| 10968 | len -= size + 1; |
| 10969 | comma = 1; |
| 10970 | } |
| 10971 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10972 | space++; |
| 10973 | } |
| 10974 | #endif |
| 10975 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 10976 | if (conf->alpn_str) { |
| 10977 | int len = conf->alpn_len; |
| 10978 | char *ptr = conf->alpn_str; |
| 10979 | int comma = 0; |
| 10980 | |
| 10981 | if (space) chunk_appendf(buf, " "); |
| 10982 | chunk_appendf(buf, "alpn "); |
| 10983 | while (len) { |
| 10984 | unsigned short size; |
| 10985 | |
| 10986 | size = *ptr; |
| 10987 | ptr++; |
| 10988 | if (comma) |
| 10989 | chunk_memcat(buf, ",", 1); |
| 10990 | chunk_memcat(buf, ptr, size); |
| 10991 | ptr += size; |
| 10992 | len -= size + 1; |
| 10993 | comma = 1; |
| 10994 | } |
| 10995 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 10996 | space++; |
| 10997 | } |
| 10998 | #endif |
| 10999 | /* verify */ |
| 11000 | { |
| 11001 | if (conf->verify == SSL_SOCK_VERIFY_NONE) { |
| 11002 | if (space) chunk_appendf(buf, " "); |
| 11003 | chunk_appendf(buf, "verify none"); |
| 11004 | space++; |
| 11005 | } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) { |
| 11006 | if (space) chunk_appendf(buf, " "); |
| 11007 | chunk_appendf(buf, "verify optional"); |
| 11008 | space++; |
| 11009 | } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 11010 | if (space) chunk_appendf(buf, " "); |
| 11011 | chunk_appendf(buf, "verify required"); |
| 11012 | space++; |
| 11013 | } |
| 11014 | } |
| 11015 | |
| 11016 | if (conf->no_ca_names) { |
| 11017 | if (space) chunk_appendf(buf, " "); |
| 11018 | chunk_appendf(buf, "no-ca-names"); |
| 11019 | space++; |
| 11020 | } |
| 11021 | |
| 11022 | if (conf->early_data) { |
| 11023 | if (space) chunk_appendf(buf, " "); |
| 11024 | chunk_appendf(buf, "allow-0rtt"); |
| 11025 | space++; |
| 11026 | } |
| 11027 | if (conf->ca_file) { |
| 11028 | if (space) chunk_appendf(buf, " "); |
| 11029 | chunk_appendf(buf, "ca-file %s", conf->ca_file); |
| 11030 | space++; |
| 11031 | } |
| 11032 | if (conf->crl_file) { |
| 11033 | if (space) chunk_appendf(buf, " "); |
| 11034 | chunk_appendf(buf, "crl-file %s", conf->crl_file); |
| 11035 | space++; |
| 11036 | } |
| 11037 | if (conf->ciphers) { |
| 11038 | if (space) chunk_appendf(buf, " "); |
| 11039 | chunk_appendf(buf, "ciphers %s", conf->ciphers); |
| 11040 | space++; |
| 11041 | } |
| 11042 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 11043 | if (conf->ciphersuites) { |
| 11044 | if (space) chunk_appendf(buf, " "); |
| 11045 | chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites); |
| 11046 | space++; |
| 11047 | } |
| 11048 | #endif |
| 11049 | if (conf->curves) { |
| 11050 | if (space) chunk_appendf(buf, " "); |
| 11051 | chunk_appendf(buf, "curves %s", conf->curves); |
| 11052 | space++; |
| 11053 | } |
| 11054 | if (conf->ecdhe) { |
| 11055 | if (space) chunk_appendf(buf, " "); |
| 11056 | chunk_appendf(buf, "ecdhe %s", conf->ecdhe); |
| 11057 | space++; |
| 11058 | } |
| 11059 | |
| 11060 | /* the crt-lists only support ssl-min-ver and ssl-max-ver */ |
| 11061 | /* XXX: this part need to be revamp so we don't dump the default settings */ |
| 11062 | if (conf->ssl_methods.min) { |
| 11063 | if (space) chunk_appendf(buf, " "); |
| 11064 | chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name); |
| 11065 | space++; |
| 11066 | } |
| 11067 | |
| 11068 | if (conf->ssl_methods.max) { |
| 11069 | if (space) chunk_appendf(buf, " "); |
| 11070 | chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name); |
| 11071 | space++; |
| 11072 | } |
| 11073 | |
William Lallemand | 58a5222 | 2020-04-02 18:11:47 +0200 | [diff] [blame] | 11074 | chunk_appendf(buf, "]"); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11075 | |
| 11076 | return; |
| 11077 | } |
| 11078 | |
| 11079 | /* dump a list of filters */ |
| 11080 | static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry) |
| 11081 | { |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11082 | int i; |
| 11083 | |
| 11084 | if (!entry->fcount) |
| 11085 | return; |
| 11086 | |
| 11087 | for (i = 0; i < entry->fcount; i++) { |
William Lallemand | 58a5222 | 2020-04-02 18:11:47 +0200 | [diff] [blame] | 11088 | chunk_appendf(buf, " %s", entry->filters[i]); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11089 | } |
| 11090 | return; |
| 11091 | } |
| 11092 | |
| 11093 | /* CLI IO handler for '(show|dump) ssl crt-list' */ |
| 11094 | static int cli_io_handler_dump_crtlist(struct appctx *appctx) |
| 11095 | { |
| 11096 | struct buffer *trash = alloc_trash_chunk(); |
| 11097 | struct stream_interface *si = appctx->owner; |
| 11098 | struct ebmb_node *lnode; |
| 11099 | |
| 11100 | if (trash == NULL) |
| 11101 | return 1; |
| 11102 | |
| 11103 | /* dump the list of crt-lists */ |
| 11104 | lnode = appctx->ctx.cli.p1; |
| 11105 | if (lnode == NULL) |
| 11106 | lnode = ebmb_first(&crtlists_tree); |
| 11107 | while (lnode) { |
| 11108 | chunk_appendf(trash, "%s\n", lnode->key); |
| 11109 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11110 | si_rx_room_blk(si); |
| 11111 | goto yield; |
| 11112 | } |
| 11113 | lnode = ebmb_next(lnode); |
| 11114 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11115 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11116 | return 1; |
| 11117 | yield: |
| 11118 | appctx->ctx.cli.p1 = lnode; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11119 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11120 | return 0; |
| 11121 | } |
| 11122 | |
| 11123 | /* CLI IO handler for '(show|dump) ssl crt-list <filename>' */ |
| 11124 | static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx) |
| 11125 | { |
| 11126 | struct buffer *trash = alloc_trash_chunk(); |
| 11127 | struct crtlist *crtlist; |
| 11128 | struct stream_interface *si = appctx->owner; |
| 11129 | struct crtlist_entry *entry; |
| 11130 | |
| 11131 | if (trash == NULL) |
| 11132 | return 1; |
| 11133 | |
| 11134 | crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node); |
| 11135 | |
| 11136 | entry = appctx->ctx.cli.p1; |
| 11137 | if (entry == NULL) { |
| 11138 | entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist); |
| 11139 | chunk_appendf(trash, "# %s\n", crtlist->node.key); |
| 11140 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11141 | si_rx_room_blk(si); |
| 11142 | goto yield; |
| 11143 | } |
| 11144 | } |
| 11145 | |
| 11146 | list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) { |
| 11147 | struct ckch_store *store; |
| 11148 | const char *filename; |
| 11149 | |
| 11150 | store = entry->node.key; |
| 11151 | filename = store->path; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11152 | chunk_appendf(trash, "%s", filename); |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11153 | if (appctx->ctx.cli.i0 == 's') /* show */ |
| 11154 | chunk_appendf(trash, ":%d", entry->linenum); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11155 | dump_crtlist_sslconf(trash, entry->ssl_conf); |
| 11156 | dump_crtlist_filters(trash, entry); |
| 11157 | chunk_appendf(trash, "\n"); |
| 11158 | |
| 11159 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11160 | si_rx_room_blk(si); |
| 11161 | goto yield; |
| 11162 | } |
| 11163 | } |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11164 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11165 | return 1; |
| 11166 | yield: |
| 11167 | appctx->ctx.cli.p1 = entry; |
William Lallemand | 2ea1b49 | 2020-03-17 15:13:11 +0100 | [diff] [blame] | 11168 | free_trash_chunk(trash); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11169 | return 0; |
| 11170 | } |
| 11171 | |
| 11172 | /* CLI argument parser for '(show|dump) ssl crt-list' */ |
| 11173 | static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11174 | { |
| 11175 | struct ebmb_node *lnode; |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11176 | char *filename = NULL; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11177 | int mode; |
| 11178 | |
| 11179 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11180 | return 1; |
| 11181 | |
| 11182 | appctx->ctx.cli.p0 = NULL; |
| 11183 | appctx->ctx.cli.p1 = NULL; |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11184 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11185 | if (*args[3] && !strcmp(args[3], "-n")) { |
| 11186 | mode = 's'; |
| 11187 | filename = args[4]; |
| 11188 | } else { |
| 11189 | mode = 'd'; |
| 11190 | filename = args[3]; |
| 11191 | } |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11192 | |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11193 | if (mode == 's' && !*args[4]) |
| 11194 | return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n"); |
| 11195 | |
| 11196 | if (filename && *filename) { |
| 11197 | lnode = ebst_lookup(&crtlists_tree, filename); |
William Lallemand | a6ffd5b | 2020-03-09 13:35:19 +0100 | [diff] [blame] | 11198 | if (lnode == NULL) |
| 11199 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 11200 | |
| 11201 | appctx->ctx.cli.p0 = lnode; |
| 11202 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 11203 | } |
| 11204 | appctx->ctx.cli.i0 = mode; |
| 11205 | |
| 11206 | return 0; |
| 11207 | } |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11208 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11209 | /* release function of the "add ssl crt-list' command, free things and unlock |
| 11210 | the spinlock */ |
| 11211 | static void cli_release_add_crtlist(struct appctx *appctx) |
| 11212 | { |
| 11213 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 11214 | |
| 11215 | if (appctx->st2 != SETCERT_ST_FIN) { |
| 11216 | struct ckch_inst *inst, *inst_s; |
| 11217 | /* upon error free the ckch_inst and everything inside */ |
| 11218 | ebpt_delete(&entry->node); |
| 11219 | LIST_DEL(&entry->by_crtlist); |
| 11220 | LIST_DEL(&entry->by_ckch_store); |
| 11221 | |
| 11222 | 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] | 11223 | ckch_inst_free(inst); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11224 | } |
| 11225 | crtlist_free_filters(entry->filters); |
| 11226 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 11227 | free(entry->ssl_conf); |
| 11228 | free(entry); |
| 11229 | } |
| 11230 | |
| 11231 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11232 | } |
| 11233 | |
| 11234 | |
| 11235 | /* IO Handler for the "add ssl crt-list" command It adds a new entry in the |
| 11236 | * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list |
| 11237 | * |
| 11238 | * The logic is the same as the "commit ssl cert" command but without the |
| 11239 | * freeing of the old structures, because there are none. |
| 11240 | */ |
| 11241 | static int cli_io_handler_add_crtlist(struct appctx *appctx) |
| 11242 | { |
| 11243 | struct bind_conf_list *bind_conf_node; |
| 11244 | struct stream_interface *si = appctx->owner; |
| 11245 | struct crtlist *crtlist = appctx->ctx.cli.p0; |
| 11246 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 11247 | struct ckch_store *store = entry->node.key; |
| 11248 | struct buffer *trash = alloc_trash_chunk(); |
| 11249 | struct ckch_inst *new_inst; |
| 11250 | char *err = NULL; |
| 11251 | int i = 0; |
| 11252 | int errcode = 0; |
| 11253 | |
| 11254 | if (trash == NULL) |
| 11255 | goto error; |
| 11256 | |
| 11257 | /* for each bind_conf which use the crt-list, a new ckch_inst must be |
| 11258 | * created. |
| 11259 | */ |
| 11260 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 11261 | goto error; |
| 11262 | |
| 11263 | while (1) { |
| 11264 | switch (appctx->st2) { |
| 11265 | case SETCERT_ST_INIT: |
| 11266 | /* This state just print the update message */ |
| 11267 | chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key); |
| 11268 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11269 | si_rx_room_blk(si); |
| 11270 | goto yield; |
| 11271 | } |
| 11272 | appctx->st2 = SETCERT_ST_GEN; |
| 11273 | /* fallthrough */ |
| 11274 | case SETCERT_ST_GEN: |
| 11275 | bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */ |
| 11276 | if (bind_conf_node == NULL) |
| 11277 | bind_conf_node = crtlist->bind_conf; |
| 11278 | for (; bind_conf_node; bind_conf_node = bind_conf_node->next) { |
| 11279 | struct bind_conf *bind_conf = bind_conf_node->bind_conf; |
| 11280 | struct sni_ctx *sni; |
| 11281 | |
| 11282 | /* yield every 10 generations */ |
| 11283 | if (i > 10) { |
| 11284 | appctx->ctx.cli.p2 = bind_conf_node; |
| 11285 | goto yield; |
| 11286 | } |
| 11287 | |
| 11288 | /* we don't support multi-cert bundles, only simple ones */ |
| 11289 | errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err); |
| 11290 | if (errcode & ERR_CODE) |
| 11291 | goto error; |
| 11292 | |
| 11293 | /* we need to initialize the SSL_CTX generated */ |
| 11294 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 11295 | list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) { |
| 11296 | if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 11297 | errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err); |
| 11298 | if (errcode & ERR_CODE) |
| 11299 | goto error; |
| 11300 | } |
| 11301 | } |
| 11302 | /* display one dot for each new instance */ |
| 11303 | chunk_appendf(trash, "."); |
| 11304 | i++; |
| 11305 | LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs); |
| 11306 | } |
| 11307 | appctx->st2 = SETCERT_ST_INSERT; |
| 11308 | /* fallthrough */ |
| 11309 | case SETCERT_ST_INSERT: |
| 11310 | /* insert SNIs in bind_conf */ |
| 11311 | list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) { |
| 11312 | HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 11313 | ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf); |
| 11314 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 11315 | } |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 11316 | entry->linenum = ++crtlist->linecount; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11317 | appctx->st2 = SETCERT_ST_FIN; |
| 11318 | goto end; |
| 11319 | } |
| 11320 | } |
| 11321 | |
| 11322 | end: |
| 11323 | chunk_appendf(trash, "\n"); |
| 11324 | if (errcode & ERR_WARN) |
| 11325 | chunk_appendf(trash, "%s", err); |
| 11326 | chunk_appendf(trash, "Success!\n"); |
| 11327 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11328 | si_rx_room_blk(si); |
| 11329 | free_trash_chunk(trash); |
| 11330 | /* success: call the release function and don't come back */ |
| 11331 | return 1; |
| 11332 | yield: |
| 11333 | /* store the state */ |
| 11334 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11335 | si_rx_room_blk(si); |
| 11336 | free_trash_chunk(trash); |
| 11337 | si_rx_endp_more(si); /* let's come back later */ |
| 11338 | return 0; /* should come back */ |
| 11339 | |
| 11340 | error: |
| 11341 | /* spin unlock and free are done in the release function */ |
| 11342 | if (trash) { |
| 11343 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 11344 | if (ci_putchk(si_ic(si), trash) == -1) |
| 11345 | si_rx_room_blk(si); |
| 11346 | free_trash_chunk(trash); |
| 11347 | } |
| 11348 | /* error: call the release function and don't come back */ |
| 11349 | return 1; |
| 11350 | } |
| 11351 | |
| 11352 | |
| 11353 | /* |
| 11354 | * Parse a "add ssl crt-list <crt-list> <certfile>" line. |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11355 | * Filters and option must be passed through payload: |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11356 | */ |
| 11357 | static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11358 | { |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11359 | int cfgerr = 0; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11360 | struct ckch_store *store; |
| 11361 | char *err = NULL; |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11362 | char path[MAXPATHLEN+1]; |
| 11363 | char *crtlist_path; |
| 11364 | char *cert_path = NULL; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11365 | struct ebmb_node *eb; |
| 11366 | struct ebpt_node *inserted; |
| 11367 | struct crtlist *crtlist; |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11368 | struct crtlist_entry *entry = NULL; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11369 | |
| 11370 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11371 | return 1; |
| 11372 | |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11373 | if (!*args[3] || (!payload && !*args[4])) |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11374 | return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n"); |
| 11375 | |
| 11376 | crtlist_path = args[3]; |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11377 | |
| 11378 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11379 | return cli_err(appctx, "Operations on certificates are currently locked!\n"); |
| 11380 | |
| 11381 | eb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 11382 | if (!eb) { |
| 11383 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 11384 | goto error; |
| 11385 | } |
| 11386 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 11387 | |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 11388 | entry = crtlist_entry_new(); |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11389 | if (entry == NULL) { |
| 11390 | memprintf(&err, "Not enough memory!"); |
| 11391 | goto error; |
| 11392 | } |
William Lallemand | c7c7a6b | 2020-04-01 17:32:46 +0200 | [diff] [blame] | 11393 | |
| 11394 | if (payload) { |
| 11395 | char *lf; |
| 11396 | |
| 11397 | lf = strrchr(payload, '\n'); |
| 11398 | if (lf) { |
| 11399 | memprintf(&err, "only one line of payload is supported!"); |
| 11400 | goto error; |
| 11401 | } |
| 11402 | /* cert_path is filled here */ |
| 11403 | cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err); |
| 11404 | if (cfgerr & ERR_CODE) |
| 11405 | goto error; |
| 11406 | } else { |
| 11407 | cert_path = args[4]; |
| 11408 | } |
| 11409 | |
| 11410 | if (!cert_path) { |
| 11411 | memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload"); |
| 11412 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 11413 | goto error; |
| 11414 | } |
| 11415 | |
| 11416 | if (*cert_path != '/' && global_ssl.crt_base) { |
| 11417 | if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) { |
| 11418 | memprintf(&err, "'%s' : path too long", cert_path); |
| 11419 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 11420 | goto error; |
| 11421 | } |
| 11422 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path); |
| 11423 | cert_path = path; |
| 11424 | } |
| 11425 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11426 | store = ckchs_lookup(cert_path); |
| 11427 | if (store == NULL) { |
| 11428 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 11429 | goto error; |
| 11430 | } |
William Lallemand | 36ccc39 | 2020-04-08 10:57:24 +0200 | [diff] [blame] | 11431 | if (store->multi) { |
| 11432 | 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); |
| 11433 | goto error; |
| 11434 | } |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11435 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 11436 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 11437 | goto error; |
| 11438 | } |
| 11439 | |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11440 | /* check if it's possible to insert this new crtlist_entry */ |
| 11441 | entry->node.key = store; |
| 11442 | inserted = ebpt_insert(&crtlist->entries, &entry->node); |
| 11443 | if (inserted != &entry->node) { |
| 11444 | memprintf(&err, "file already exists in this directory!"); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11445 | goto error; |
| 11446 | } |
| 11447 | |
| 11448 | LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11449 | entry->crtlist = crtlist; |
| 11450 | LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store); |
| 11451 | |
| 11452 | appctx->st2 = SETCERT_ST_INIT; |
| 11453 | appctx->ctx.cli.p0 = crtlist; |
| 11454 | appctx->ctx.cli.p1 = entry; |
| 11455 | |
| 11456 | /* unlock is done in the release handler */ |
| 11457 | return 0; |
| 11458 | |
| 11459 | error: |
William Lallemand | e718dfb | 2020-04-10 11:09:25 +0200 | [diff] [blame] | 11460 | crtlist_entry_free(entry); |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11461 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11462 | err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : ""); |
| 11463 | return cli_dynerr(appctx, err); |
| 11464 | } |
| 11465 | |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11466 | /* Parse a "del ssl crt-list <crt-list> <certfile>" line. */ |
| 11467 | static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 11468 | { |
| 11469 | struct ckch_store *store; |
| 11470 | char *err = NULL; |
| 11471 | char *crtlist_path, *cert_path; |
| 11472 | struct ebmb_node *ebmb; |
| 11473 | struct ebpt_node *ebpt; |
| 11474 | struct crtlist *crtlist; |
| 11475 | struct crtlist_entry *entry = NULL; |
| 11476 | struct ckch_inst *inst, *inst_s; |
| 11477 | int linenum = 0; |
| 11478 | char *colons; |
| 11479 | |
| 11480 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11481 | return 1; |
| 11482 | |
| 11483 | if (!*args[3] || !*args[4]) |
| 11484 | return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n"); |
| 11485 | |
William Lallemand | 463b524 | 2020-04-08 10:30:44 +0200 | [diff] [blame] | 11486 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11487 | return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n"); |
| 11488 | |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11489 | crtlist_path = args[3]; |
| 11490 | cert_path = args[4]; |
| 11491 | |
| 11492 | colons = strchr(cert_path, ':'); |
| 11493 | if (colons) { |
| 11494 | char *endptr; |
| 11495 | |
| 11496 | linenum = strtol(colons + 1, &endptr, 10); |
| 11497 | if (colons + 1 == endptr || *endptr != '\0') { |
| 11498 | memprintf(&err, "wrong line number after colons in '%s'!", cert_path); |
| 11499 | goto error; |
| 11500 | } |
| 11501 | *colons = '\0'; |
| 11502 | } |
| 11503 | /* look for crtlist */ |
| 11504 | ebmb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 11505 | if (!ebmb) { |
| 11506 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 11507 | goto error; |
| 11508 | } |
| 11509 | crtlist = ebmb_entry(ebmb, struct crtlist, node); |
| 11510 | |
| 11511 | /* look for store */ |
| 11512 | store = ckchs_lookup(cert_path); |
| 11513 | if (store == NULL) { |
| 11514 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 11515 | goto error; |
| 11516 | } |
William Lallemand | 36ccc39 | 2020-04-08 10:57:24 +0200 | [diff] [blame] | 11517 | if (store->multi) { |
| 11518 | 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); |
| 11519 | goto error; |
| 11520 | } |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11521 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 11522 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 11523 | goto error; |
| 11524 | } |
| 11525 | |
| 11526 | ebpt = ebpt_lookup(&crtlist->entries, store); |
| 11527 | if (!ebpt) { |
| 11528 | memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path); |
| 11529 | goto error; |
| 11530 | } |
| 11531 | |
| 11532 | /* list the line number of entries for errors in err, and select the right ebpt */ |
| 11533 | for (; ebpt; ebpt = ebpt_next_dup(ebpt)) { |
| 11534 | struct crtlist_entry *tmp; |
| 11535 | |
| 11536 | tmp = ebpt_entry(ebpt, struct crtlist_entry, node); |
| 11537 | memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum); |
| 11538 | |
| 11539 | /* select the entry we wanted */ |
| 11540 | if (linenum == 0 || tmp->linenum == linenum) { |
| 11541 | if (!entry) |
| 11542 | entry = tmp; |
| 11543 | } |
| 11544 | } |
| 11545 | |
| 11546 | /* we didn't found the specified entry */ |
| 11547 | if (!entry) { |
| 11548 | 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); |
| 11549 | goto error; |
| 11550 | } |
| 11551 | |
| 11552 | /* we didn't specified a line number but there were several entries */ |
| 11553 | if (linenum == 0 && ebpt_next_dup(&entry->node)) { |
| 11554 | memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 11555 | goto error; |
| 11556 | } |
| 11557 | |
| 11558 | /* upon error free the ckch_inst and everything inside */ |
| 11559 | |
| 11560 | ebpt_delete(&entry->node); |
| 11561 | LIST_DEL(&entry->by_crtlist); |
| 11562 | LIST_DEL(&entry->by_ckch_store); |
| 11563 | |
| 11564 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 11565 | struct sni_ctx *sni, *sni_s; |
| 11566 | |
| 11567 | HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 11568 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 11569 | ebmb_delete(&sni->name); |
| 11570 | LIST_DEL(&sni->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 11571 | SSL_CTX_free(sni->ctx); |
William Lallemand | 0a9b941 | 2020-04-06 17:43:05 +0200 | [diff] [blame] | 11572 | free(sni); |
| 11573 | } |
| 11574 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 11575 | LIST_DEL(&inst->by_ckchs); |
| 11576 | free(inst); |
| 11577 | } |
| 11578 | |
| 11579 | crtlist_free_filters(entry->filters); |
| 11580 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 11581 | free(entry->ssl_conf); |
| 11582 | free(entry); |
| 11583 | |
| 11584 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11585 | err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path); |
| 11586 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 11587 | |
| 11588 | error: |
| 11589 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11590 | err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : ""); |
| 11591 | return cli_dynerr(appctx, err); |
| 11592 | } |
William Lallemand | e67c80b | 2020-03-25 14:42:37 +0100 | [diff] [blame] | 11593 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11594 | /* Type of SSL payloads that can be updated over the CLI */ |
| 11595 | |
| 11596 | enum { |
| 11597 | CERT_TYPE_PEM = 0, |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11598 | CERT_TYPE_KEY, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11599 | #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] | 11600 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11601 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11602 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11603 | #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] | 11604 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 11605 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11606 | CERT_TYPE_MAX, |
| 11607 | }; |
| 11608 | |
| 11609 | struct { |
| 11610 | const char *ext; |
| 11611 | int type; |
| 11612 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 11613 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11614 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11615 | [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] | 11616 | [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] | 11617 | #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] | 11618 | [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] | 11619 | #endif |
| 11620 | #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] | 11621 | [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] | 11622 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11623 | [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] | 11624 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11625 | }; |
| 11626 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11627 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11628 | /* release function of the `show ssl cert' command */ |
| 11629 | static void cli_release_show_cert(struct appctx *appctx) |
| 11630 | { |
| 11631 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11632 | } |
| 11633 | |
| 11634 | /* IO handler of "show ssl cert <filename>" */ |
| 11635 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 11636 | { |
| 11637 | struct buffer *trash = alloc_trash_chunk(); |
| 11638 | struct ebmb_node *node; |
| 11639 | struct stream_interface *si = appctx->owner; |
| 11640 | struct ckch_store *ckchs; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11641 | |
| 11642 | if (trash == NULL) |
| 11643 | return 1; |
| 11644 | |
| 11645 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11646 | if (ckchs_transaction.old_ckchs) { |
| 11647 | ckchs = ckchs_transaction.old_ckchs; |
| 11648 | chunk_appendf(trash, "# transaction\n"); |
| 11649 | if (!ckchs->multi) { |
| 11650 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11651 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11652 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11653 | int n; |
| 11654 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11655 | chunk_appendf(trash, "*%s:", ckchs->path); |
| 11656 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11657 | if (ckchs->ckch[n].cert) |
| 11658 | chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11659 | } |
| 11660 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11661 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11662 | } |
| 11663 | } |
| 11664 | } |
| 11665 | |
| 11666 | if (!appctx->ctx.cli.p0) { |
| 11667 | chunk_appendf(trash, "# filename\n"); |
| 11668 | node = ebmb_first(&ckchs_tree); |
| 11669 | } else { |
| 11670 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 11671 | } |
| 11672 | while (node) { |
| 11673 | ckchs = ebmb_entry(node, struct ckch_store, node); |
| 11674 | if (!ckchs->multi) { |
| 11675 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11676 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11677 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 11678 | int n; |
| 11679 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11680 | chunk_appendf(trash, "%s:", ckchs->path); |
| 11681 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11682 | if (ckchs->ckch[n].cert) |
| 11683 | chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 11684 | } |
| 11685 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 11686 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11687 | } |
| 11688 | |
| 11689 | node = ebmb_next(node); |
| 11690 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11691 | si_rx_room_blk(si); |
| 11692 | goto yield; |
| 11693 | } |
| 11694 | } |
| 11695 | |
| 11696 | appctx->ctx.cli.p0 = NULL; |
| 11697 | free_trash_chunk(trash); |
| 11698 | return 1; |
| 11699 | yield: |
| 11700 | |
| 11701 | free_trash_chunk(trash); |
| 11702 | appctx->ctx.cli.p0 = ckchs; |
| 11703 | return 0; /* should come back */ |
| 11704 | } |
| 11705 | |
| 11706 | /* IO handler of the details "show ssl cert <filename>" */ |
| 11707 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 11708 | { |
| 11709 | struct stream_interface *si = appctx->owner; |
| 11710 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 11711 | struct buffer *out = alloc_trash_chunk(); |
| 11712 | struct buffer *tmp = alloc_trash_chunk(); |
| 11713 | X509_NAME *name = NULL; |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11714 | STACK_OF(X509) *chain; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11715 | unsigned int len = 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11716 | int write = -1; |
| 11717 | BIO *bio = NULL; |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11718 | int i; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11719 | |
| 11720 | if (!tmp || !out) |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11721 | goto end_no_putchk; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11722 | |
| 11723 | if (!ckchs->multi) { |
| 11724 | chunk_appendf(out, "Filename: "); |
| 11725 | if (ckchs == ckchs_transaction.new_ckchs) |
| 11726 | chunk_appendf(out, "*"); |
| 11727 | chunk_appendf(out, "%s\n", ckchs->path); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11728 | |
William Lallemand | 59c16fc | 2020-03-19 20:26:02 +0100 | [diff] [blame] | 11729 | chunk_appendf(out, "Status: "); |
| 11730 | if (ckchs->ckch->cert == NULL) |
| 11731 | chunk_appendf(out, "Empty\n"); |
| 11732 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 11733 | chunk_appendf(out, "Unused\n"); |
| 11734 | else |
| 11735 | chunk_appendf(out, "Used\n"); |
| 11736 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11737 | if (ckchs->ckch->cert == NULL) |
| 11738 | goto end; |
| 11739 | |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11740 | chain = ckchs->ckch->chain; |
| 11741 | if (chain == NULL) { |
| 11742 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 11743 | issuer = ssl_get0_issuer_chain(ckchs->ckch->cert); |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11744 | if (issuer) { |
| 11745 | chain = issuer->chain; |
| 11746 | chunk_appendf(out, "Chain Filename: "); |
| 11747 | chunk_appendf(out, "%s\n", issuer->path); |
| 11748 | } |
| 11749 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11750 | chunk_appendf(out, "Serial: "); |
| 11751 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 11752 | goto end; |
| 11753 | dump_binary(out, tmp->area, tmp->data); |
| 11754 | chunk_appendf(out, "\n"); |
| 11755 | |
| 11756 | chunk_appendf(out, "notBefore: "); |
| 11757 | chunk_reset(tmp); |
| 11758 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11759 | goto end; |
| 11760 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 11761 | goto end; |
| 11762 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 11763 | tmp->area[write] = '\0'; |
| 11764 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11765 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11766 | chunk_appendf(out, "%s\n", tmp->area); |
| 11767 | |
| 11768 | chunk_appendf(out, "notAfter: "); |
| 11769 | chunk_reset(tmp); |
| 11770 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 11771 | goto end; |
| 11772 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 11773 | goto end; |
| 11774 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 11775 | goto end; |
| 11776 | tmp->area[write] = '\0'; |
| 11777 | BIO_free(bio); |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11778 | bio = NULL; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11779 | chunk_appendf(out, "%s\n", tmp->area); |
| 11780 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11781 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11782 | chunk_appendf(out, "Subject Alternative Name: "); |
| 11783 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 11784 | goto end; |
| 11785 | *(out->area + out->data) = '\0'; |
| 11786 | chunk_appendf(out, "\n"); |
| 11787 | #endif |
| 11788 | chunk_reset(tmp); |
| 11789 | chunk_appendf(out, "Algorithm: "); |
| 11790 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 11791 | goto end; |
| 11792 | chunk_appendf(out, "%s\n", tmp->area); |
| 11793 | |
| 11794 | chunk_reset(tmp); |
| 11795 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11796 | 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] | 11797 | goto end; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame] | 11798 | tmp->data = len; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11799 | dump_binary(out, tmp->area, tmp->data); |
| 11800 | chunk_appendf(out, "\n"); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11801 | |
William Lallemand | a90e593 | 2020-02-25 14:07:58 +0100 | [diff] [blame] | 11802 | chunk_appendf(out, "Subject: "); |
| 11803 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 11804 | goto end; |
| 11805 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11806 | goto end; |
| 11807 | *(tmp->area + tmp->data) = '\0'; |
| 11808 | chunk_appendf(out, "%s\n", tmp->area); |
| 11809 | |
| 11810 | chunk_appendf(out, "Issuer: "); |
| 11811 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 11812 | goto end; |
| 11813 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11814 | goto end; |
| 11815 | *(tmp->area + tmp->data) = '\0'; |
| 11816 | chunk_appendf(out, "%s\n", tmp->area); |
| 11817 | |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11818 | /* Displays subject of each certificate in the chain */ |
Emmanuel Hocdet | cf8cf6c | 2020-02-18 16:06:14 +0100 | [diff] [blame] | 11819 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 11820 | X509 *ca = sk_X509_value(chain, i); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11821 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11822 | chunk_appendf(out, "Chain Subject: "); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11823 | if ((name = X509_get_subject_name(ca)) == NULL) |
| 11824 | goto end; |
| 11825 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11826 | goto end; |
| 11827 | *(tmp->area + tmp->data) = '\0'; |
| 11828 | chunk_appendf(out, "%s\n", tmp->area); |
| 11829 | |
William Lallemand | bb7288a | 2020-02-25 14:04:33 +0100 | [diff] [blame] | 11830 | chunk_appendf(out, "Chain Issuer: "); |
| 11831 | if ((name = X509_get_issuer_name(ca)) == NULL) |
| 11832 | goto end; |
| 11833 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 11834 | goto end; |
| 11835 | *(tmp->area + tmp->data) = '\0'; |
| 11836 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | 35f4a9d | 2020-02-25 11:56:32 +0100 | [diff] [blame] | 11837 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11838 | } |
| 11839 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 11840 | end: |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11841 | if (ci_putchk(si_ic(si), out) == -1) { |
| 11842 | si_rx_room_blk(si); |
| 11843 | goto yield; |
| 11844 | } |
| 11845 | |
William Lallemand | 18eeb8e | 2020-03-20 14:42:36 +0100 | [diff] [blame] | 11846 | end_no_putchk: |
William Lallemand | 67b991d | 2020-03-20 14:10:17 +0100 | [diff] [blame] | 11847 | if (bio) |
| 11848 | BIO_free(bio); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11849 | free_trash_chunk(tmp); |
| 11850 | free_trash_chunk(out); |
| 11851 | return 1; |
| 11852 | yield: |
| 11853 | free_trash_chunk(tmp); |
| 11854 | free_trash_chunk(out); |
| 11855 | return 0; /* should come back */ |
| 11856 | } |
| 11857 | |
| 11858 | /* parsing function for 'show ssl cert [certfile]' */ |
| 11859 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11860 | { |
| 11861 | struct ckch_store *ckchs; |
| 11862 | |
| 11863 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 11864 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 11865 | |
| 11866 | /* The operations on the CKCH architecture are locked so we can |
| 11867 | * manipulate ckch_store and ckch_inst */ |
| 11868 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11869 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 11870 | |
| 11871 | /* check if there is a certificate to lookup */ |
| 11872 | if (*args[3]) { |
| 11873 | if (*args[3] == '*') { |
| 11874 | if (!ckchs_transaction.new_ckchs) |
| 11875 | goto error; |
| 11876 | |
| 11877 | ckchs = ckchs_transaction.new_ckchs; |
| 11878 | |
| 11879 | if (strcmp(args[3] + 1, ckchs->path)) |
| 11880 | goto error; |
| 11881 | |
| 11882 | } else { |
| 11883 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 11884 | goto error; |
| 11885 | |
| 11886 | } |
| 11887 | |
| 11888 | if (ckchs->multi) |
| 11889 | goto error; |
| 11890 | |
| 11891 | appctx->ctx.cli.p0 = ckchs; |
| 11892 | /* use the IO handler that shows details */ |
| 11893 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 11894 | } |
| 11895 | |
| 11896 | return 0; |
| 11897 | |
| 11898 | error: |
| 11899 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11900 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 11901 | } |
| 11902 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11903 | /* 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] | 11904 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11905 | { |
| 11906 | struct ckch_store *new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11907 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11908 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11909 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11910 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11911 | /* 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] | 11912 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11913 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11914 | /* 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] | 11915 | ckch_store_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11916 | } |
| 11917 | } |
| 11918 | |
| 11919 | |
| 11920 | /* |
| 11921 | * This function tries to create the new ckch_inst and their SNIs |
| 11922 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11923 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11924 | { |
| 11925 | struct stream_interface *si = appctx->owner; |
| 11926 | int y = 0; |
| 11927 | char *err = NULL; |
| 11928 | int errcode = 0; |
| 11929 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 11930 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11931 | struct buffer *trash = alloc_trash_chunk(); |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 11932 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 90afe90 | 2020-03-30 19:29:45 +0200 | [diff] [blame] | 11933 | struct crtlist_entry *entry; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11934 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 11935 | if (trash == NULL) |
| 11936 | goto error; |
| 11937 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11938 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 11939 | goto error; |
| 11940 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11941 | while (1) { |
| 11942 | switch (appctx->st2) { |
| 11943 | case SETCERT_ST_INIT: |
| 11944 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11945 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11946 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 11947 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11948 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11949 | } |
| 11950 | appctx->st2 = SETCERT_ST_GEN; |
| 11951 | /* fallthrough */ |
| 11952 | case SETCERT_ST_GEN: |
| 11953 | /* |
| 11954 | * This state generates the ckch instances with their |
| 11955 | * sni_ctxs and SSL_CTX. |
| 11956 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11957 | * Since the SSL_CTX generation can be CPU consumer, we |
| 11958 | * yield every 10 instances. |
| 11959 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11960 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11961 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11962 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11963 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11964 | if (!new_ckchs) |
| 11965 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11966 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11967 | /* get the next ckchi to regenerate */ |
| 11968 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 11969 | /* we didn't start yet, set it to the first elem */ |
| 11970 | if (ckchi == NULL) |
| 11971 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11972 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11973 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11974 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11975 | struct ckch_inst *new_inst; |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11976 | char **sni_filter = NULL; |
| 11977 | int fcount = 0; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11978 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11979 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11980 | if (y >= 10) { |
| 11981 | /* save the next ckchi to compute */ |
| 11982 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 11983 | goto yield; |
| 11984 | } |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 11985 | |
| 11986 | if (ckchi->crtlist_entry) { |
| 11987 | sni_filter = ckchi->crtlist_entry->filters; |
| 11988 | fcount = ckchi->crtlist_entry->fcount; |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame] | 11989 | } |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11990 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11991 | if (new_ckchs->multi) |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11992 | 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] | 11993 | else |
William Lallemand | 38df1c8 | 2019-12-04 15:39:35 +0100 | [diff] [blame] | 11994 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
| 11995 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 11996 | if (errcode & ERR_CODE) |
| 11997 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11998 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 11999 | /* if the previous ckchi was used as the default */ |
| 12000 | if (ckchi->is_default) |
| 12001 | new_inst->is_default = 1; |
| 12002 | |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 12003 | /* we need to initialize the SSL_CTX generated */ |
William Lallemand | 696f317 | 2020-02-07 20:45:24 +0100 | [diff] [blame] | 12004 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 12005 | 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] | 12006 | 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] | 12007 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 12008 | if (errcode & ERR_CODE) |
| 12009 | goto error; |
| 12010 | } |
| 12011 | } |
| 12012 | |
| 12013 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12014 | /* display one dot per new instance */ |
| 12015 | chunk_appendf(trash, "."); |
| 12016 | /* link the new ckch_inst to the duplicate */ |
| 12017 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12018 | y++; |
| 12019 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12020 | appctx->st2 = SETCERT_ST_INSERT; |
| 12021 | /* fallthrough */ |
| 12022 | case SETCERT_ST_INSERT: |
| 12023 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12024 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12025 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12026 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12027 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12028 | if (!new_ckchs) |
| 12029 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12030 | |
William Lallemand | 90afe90 | 2020-03-30 19:29:45 +0200 | [diff] [blame] | 12031 | /* get the list of crtlist_entry in the old store, and update the pointers to the store */ |
| 12032 | LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry); |
| 12033 | list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) { |
| 12034 | ebpt_delete(&entry->node); |
| 12035 | /* change the ptr and reinsert the node */ |
| 12036 | entry->node.key = new_ckchs; |
| 12037 | ebpt_insert(&entry->crtlist->entries, &entry->node); |
| 12038 | } |
| 12039 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 12040 | /* 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] | 12041 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 12042 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 12043 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 12044 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 12045 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12046 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12047 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 12048 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
William Lallemand | d5e9377 | 2020-04-09 17:12:16 +0200 | [diff] [blame] | 12049 | struct bind_conf *bind_conf = ckchi->bind_conf; |
| 12050 | |
| 12051 | HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock); |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 12052 | ckch_inst_free(ckchi); |
William Lallemand | d5e9377 | 2020-04-09 17:12:16 +0200 | [diff] [blame] | 12053 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock); |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12054 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12055 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12056 | /* Replace the old ckchs by the new one */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12057 | ckch_store_free(old_ckchs); |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 12058 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12059 | appctx->st2 = SETCERT_ST_FIN; |
| 12060 | /* fallthrough */ |
| 12061 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12062 | /* we achieved the transaction, we can set everything to NULL */ |
| 12063 | free(ckchs_transaction.path); |
| 12064 | ckchs_transaction.path = NULL; |
| 12065 | ckchs_transaction.new_ckchs = NULL; |
| 12066 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12067 | goto end; |
| 12068 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12069 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12070 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12071 | |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 12072 | chunk_appendf(trash, "\n"); |
| 12073 | if (errcode & ERR_WARN) |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 12074 | chunk_appendf(trash, "%s", err); |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 12075 | chunk_appendf(trash, "Success!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12076 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12077 | si_rx_room_blk(si); |
| 12078 | free_trash_chunk(trash); |
| 12079 | /* success: call the release function and don't come back */ |
| 12080 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12081 | yield: |
| 12082 | /* store the state */ |
| 12083 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12084 | si_rx_room_blk(si); |
| 12085 | free_trash_chunk(trash); |
| 12086 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12087 | return 0; /* should come back */ |
| 12088 | |
| 12089 | error: |
| 12090 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 12091 | if (trash) { |
| 12092 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 12093 | if (ci_putchk(si_ic(si), trash) == -1) |
| 12094 | si_rx_room_blk(si); |
| 12095 | free_trash_chunk(trash); |
| 12096 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12097 | /* error: call the release function and don't come back */ |
| 12098 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12099 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12100 | |
| 12101 | /* |
| 12102 | * Parsing function of 'commit ssl cert' |
| 12103 | */ |
| 12104 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12105 | { |
| 12106 | char *err = NULL; |
| 12107 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12108 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12109 | return 1; |
| 12110 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12111 | if (!*args[3]) |
| 12112 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 12113 | |
| 12114 | /* The operations on the CKCH architecture are locked so we can |
| 12115 | * manipulate ckch_store and ckch_inst */ |
| 12116 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12117 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 12118 | |
| 12119 | if (!ckchs_transaction.path) { |
| 12120 | memprintf(&err, "No ongoing transaction! !\n"); |
| 12121 | goto error; |
| 12122 | } |
| 12123 | |
| 12124 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 12125 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 12126 | goto error; |
| 12127 | } |
| 12128 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 12129 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 12130 | if (ckchs_transaction.new_ckchs->multi) { |
| 12131 | int n; |
| 12132 | |
| 12133 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 12134 | 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)) { |
| 12135 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 12136 | goto error; |
| 12137 | } |
| 12138 | } |
| 12139 | } else |
| 12140 | #endif |
| 12141 | { |
| 12142 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 12143 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 12144 | goto error; |
| 12145 | } |
| 12146 | } |
| 12147 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12148 | /* init the appctx structure */ |
| 12149 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12150 | appctx->ctx.ssl.next_ckchi = NULL; |
| 12151 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 12152 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 12153 | |
| 12154 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 12155 | return 0; |
| 12156 | |
| 12157 | error: |
| 12158 | |
| 12159 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12160 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 12161 | |
| 12162 | return cli_dynerr(appctx, err); |
| 12163 | } |
| 12164 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12165 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12166 | * 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] | 12167 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12168 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12169 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 12170 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12171 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12172 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12173 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 12174 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12175 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12176 | char *end; |
| 12177 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12178 | struct cert_key_and_chain *ckch; |
| 12179 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12180 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12181 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12182 | return 1; |
| 12183 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12184 | if ((buf = alloc_trash_chunk()) == NULL) |
| 12185 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12186 | |
| 12187 | if (!*args[3] || !payload) |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12188 | 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] | 12189 | |
| 12190 | /* The operations on the CKCH architecture are locked so we can |
| 12191 | * manipulate ckch_store and ckch_inst */ |
| 12192 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12193 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 12194 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12195 | if (!chunk_strcpy(buf, args[3])) { |
| 12196 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 12197 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12198 | goto end; |
| 12199 | } |
| 12200 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12201 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 12202 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12203 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12204 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 12205 | *end = '\0'; |
| 12206 | type = cert_exts[i].type; |
| 12207 | break; |
| 12208 | } |
| 12209 | } |
| 12210 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12211 | appctx->ctx.ssl.old_ckchs = NULL; |
| 12212 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 12213 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12214 | /* if there is an ongoing transaction */ |
| 12215 | if (ckchs_transaction.path) { |
| 12216 | /* 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] | 12217 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12218 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12219 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12220 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12221 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12222 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12223 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12224 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12225 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12226 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 12227 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 12228 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 12229 | break; |
| 12230 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12231 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12232 | if (bundle < 0) { |
| 12233 | 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); |
| 12234 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12235 | goto end; |
| 12236 | } |
| 12237 | } |
| 12238 | #endif |
| 12239 | |
| 12240 | /* if there is an ongoing transaction, check if this is the same file */ |
| 12241 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 12242 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 12243 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12244 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12245 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12246 | |
| 12247 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 12248 | |
| 12249 | } else { |
| 12250 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 12251 | |
| 12252 | /* lookup for the certificate in the tree: |
| 12253 | * check if this is used as a bundle AND as a unique certificate */ |
| 12254 | for (i = 0; i < 2; i++) { |
| 12255 | |
| 12256 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 12257 | /* only the bundle name is in the tree and you should |
| 12258 | * never update a bundle name, only a filename */ |
| 12259 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 12260 | /* we tried to look for a non-bundle and we found a bundle */ |
| 12261 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 12262 | err ? err : "", args[3], args[3]); |
| 12263 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12264 | goto end; |
| 12265 | } |
William Lallemand | 3246d94 | 2019-11-04 14:02:11 +0100 | [diff] [blame] | 12266 | /* If we want a bundle but this is not a bundle |
| 12267 | * example: When you try to update <file>.rsa, but |
| 12268 | * <file> is a regular file */ |
| 12269 | if (bundle >= 0 && find_ckchs[i]->multi == 0) { |
| 12270 | find_ckchs[i] = NULL; |
| 12271 | break; |
| 12272 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12273 | } |
| 12274 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 12275 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12276 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12277 | int j; |
| 12278 | |
| 12279 | /* check if it was used in a bundle by removing the |
| 12280 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 12281 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 12282 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12283 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 12284 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 12285 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 12286 | break; |
| 12287 | } |
| 12288 | } |
William Lallemand | 37031b8 | 2019-11-04 13:38:53 +0100 | [diff] [blame] | 12289 | if (bundle < 0) /* we didn't find a bundle extension */ |
| 12290 | break; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12291 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12292 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12293 | /* bundles are not supported here, so we don't need to lookup again */ |
| 12294 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 12295 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12296 | } |
| 12297 | |
| 12298 | if (find_ckchs[0] && find_ckchs[1]) { |
| 12299 | 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", |
| 12300 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 12301 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12302 | goto end; |
| 12303 | } |
| 12304 | |
| 12305 | 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] | 12306 | } |
| 12307 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12308 | if (!appctx->ctx.ssl.old_ckchs) { |
| 12309 | 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] | 12310 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12311 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12312 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12313 | } |
| 12314 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame] | 12315 | if (!appctx->ctx.ssl.path) { |
| 12316 | /* this is a new transaction, set the path of the transaction */ |
| 12317 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 12318 | if (!appctx->ctx.ssl.path) { |
| 12319 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 12320 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12321 | goto end; |
| 12322 | } |
| 12323 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12324 | |
| 12325 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12326 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12327 | /* duplicate the ckch store */ |
| 12328 | new_ckchs = ckchs_dup(old_ckchs); |
| 12329 | if (!new_ckchs) { |
| 12330 | memprintf(&err, "%sCannot allocate memory!\n", |
| 12331 | err ? err : ""); |
| 12332 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12333 | goto end; |
| 12334 | } |
| 12335 | |
| 12336 | if (!new_ckchs->multi) |
| 12337 | ckch = new_ckchs->ckch; |
| 12338 | else |
| 12339 | ckch = &new_ckchs->ckch[bundle]; |
| 12340 | |
| 12341 | /* appply the change on the duplicate */ |
| 12342 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 12343 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 12344 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12345 | goto end; |
| 12346 | } |
| 12347 | |
| 12348 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 12349 | |
| 12350 | /* we succeed, we can save the ckchs in the transaction */ |
| 12351 | |
| 12352 | /* if there wasn't a transaction, update the old ckchs */ |
William Dauchy | c8bb153 | 2019-11-24 15:04:20 +0100 | [diff] [blame] | 12353 | if (!ckchs_transaction.old_ckchs) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12354 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 12355 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 12356 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 12357 | } else { |
| 12358 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 12359 | |
| 12360 | } |
| 12361 | |
| 12362 | /* free the previous ckchs if there was a transaction */ |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12363 | ckch_store_free(ckchs_transaction.new_ckchs); |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12364 | |
| 12365 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 12366 | |
| 12367 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12368 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12369 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12370 | end: |
| 12371 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 12372 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 12373 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12374 | |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12375 | ckch_store_free(appctx->ctx.ssl.new_ckchs); |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12376 | appctx->ctx.ssl.new_ckchs = NULL; |
| 12377 | |
| 12378 | appctx->ctx.ssl.old_ckchs = NULL; |
| 12379 | |
| 12380 | free(appctx->ctx.ssl.path); |
| 12381 | appctx->ctx.ssl.path = NULL; |
| 12382 | |
| 12383 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 12384 | 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] | 12385 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 12386 | |
| 12387 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12388 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 12389 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 12390 | /* 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] | 12391 | } |
| 12392 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12393 | /* parsing function of 'abort ssl cert' */ |
| 12394 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12395 | { |
| 12396 | char *err = NULL; |
| 12397 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 12398 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12399 | return 1; |
| 12400 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12401 | if (!*args[3]) |
| 12402 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 12403 | |
| 12404 | /* The operations on the CKCH architecture are locked so we can |
| 12405 | * manipulate ckch_store and ckch_inst */ |
| 12406 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12407 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 12408 | |
| 12409 | if (!ckchs_transaction.path) { |
| 12410 | memprintf(&err, "No ongoing transaction!\n"); |
| 12411 | goto error; |
| 12412 | } |
| 12413 | |
| 12414 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 12415 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 12416 | goto error; |
| 12417 | } |
| 12418 | |
| 12419 | /* 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] | 12420 | ckch_store_free(ckchs_transaction.new_ckchs); |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12421 | ckchs_transaction.new_ckchs = NULL; |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12422 | ckch_store_free(ckchs_transaction.old_ckchs); |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 12423 | ckchs_transaction.old_ckchs = NULL; |
| 12424 | free(ckchs_transaction.path); |
| 12425 | ckchs_transaction.path = NULL; |
| 12426 | |
| 12427 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12428 | |
| 12429 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 12430 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12431 | |
| 12432 | error: |
| 12433 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12434 | |
| 12435 | return cli_dynerr(appctx, err); |
| 12436 | } |
| 12437 | |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12438 | /* parsing function of 'new ssl cert' */ |
| 12439 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12440 | { |
| 12441 | struct ckch_store *store; |
| 12442 | char *err = NULL; |
| 12443 | char *path; |
| 12444 | |
| 12445 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12446 | return 1; |
| 12447 | |
| 12448 | if (!*args[3]) |
| 12449 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 12450 | |
| 12451 | path = args[3]; |
| 12452 | |
| 12453 | /* The operations on the CKCH architecture are locked so we can |
| 12454 | * manipulate ckch_store and ckch_inst */ |
| 12455 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12456 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 12457 | |
| 12458 | store = ckchs_lookup(path); |
| 12459 | if (store != NULL) { |
| 12460 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 12461 | store = NULL; /* we don't want to free it */ |
| 12462 | goto error; |
| 12463 | } |
William Lallemand | 8a874e4 | 2020-04-09 10:32:53 +0200 | [diff] [blame] | 12464 | /* we won't support multi-certificate bundle here */ |
| 12465 | store = ckch_store_new(path, 1); |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12466 | if (!store) { |
| 12467 | memprintf(&err, "unable to allocate memory.\n"); |
| 12468 | goto error; |
| 12469 | } |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12470 | |
| 12471 | /* insert into the ckchs tree */ |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12472 | ebst_insert(&ckchs_tree, &store->node); |
| 12473 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 12474 | |
| 12475 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12476 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12477 | error: |
| 12478 | free(store); |
| 12479 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12480 | return cli_dynerr(appctx, err); |
| 12481 | } |
| 12482 | |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 12483 | /* parsing function of 'del ssl cert' */ |
| 12484 | static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 12485 | { |
| 12486 | struct ckch_store *store; |
| 12487 | char *err = NULL; |
| 12488 | char *filename; |
| 12489 | |
| 12490 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 12491 | return 1; |
| 12492 | |
| 12493 | if (!*args[3]) |
| 12494 | return cli_err(appctx, "'del ssl cert' expects a certificate name\n"); |
| 12495 | |
| 12496 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12497 | return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n"); |
| 12498 | |
| 12499 | filename = args[3]; |
| 12500 | |
| 12501 | store = ckchs_lookup(filename); |
| 12502 | if (store == NULL) { |
| 12503 | memprintf(&err, "certificate '%s' doesn't exist!\n", filename); |
| 12504 | goto error; |
| 12505 | } |
| 12506 | if (!LIST_ISEMPTY(&store->ckch_inst)) { |
| 12507 | memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename); |
| 12508 | goto error; |
| 12509 | } |
| 12510 | |
| 12511 | ebmb_delete(&store->node); |
William Lallemand | ba1c33f | 2020-04-08 17:55:45 +0200 | [diff] [blame] | 12512 | ckch_store_free(store); |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 12513 | |
| 12514 | memprintf(&err, "Certificate '%s' deleted!\n", filename); |
| 12515 | |
| 12516 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12517 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 12518 | |
| 12519 | error: |
| 12520 | memprintf(&err, "Can't remove the certificate: %s\n", err ? err : ""); |
| 12521 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12522 | return cli_dynerr(appctx, err); |
| 12523 | } |
| 12524 | |
| 12525 | |
| 12526 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 12527 | 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] | 12528 | { |
| 12529 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 12530 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12531 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 12532 | |
| 12533 | if (!payload) |
| 12534 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12535 | |
| 12536 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12537 | if (!*payload) |
| 12538 | 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] | 12539 | |
| 12540 | /* remove \r and \n from the payload */ |
| 12541 | for (i = 0, j = 0; payload[i]; i++) { |
| 12542 | if (payload[i] == '\r' || payload[i] == '\n') |
| 12543 | continue; |
| 12544 | payload[j++] = payload[i]; |
| 12545 | } |
| 12546 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12547 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12548 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12549 | if (ret < 0) |
| 12550 | 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] | 12551 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 12552 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12553 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12554 | if (err) |
| 12555 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 12556 | else |
| 12557 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12558 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12559 | |
| 12560 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12561 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 12562 | 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] | 12563 | #endif |
| 12564 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12565 | } |
| 12566 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12567 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12568 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 12569 | { |
| 12570 | switch (arg->type) { |
| 12571 | case ARGT_STR: |
| 12572 | smp->data.type = SMP_T_STR; |
| 12573 | smp->data.u.str = arg->data.str; |
| 12574 | return 1; |
| 12575 | case ARGT_VAR: |
| 12576 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 12577 | return 0; |
| 12578 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 12579 | return 0; |
| 12580 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 12581 | return 0; |
| 12582 | return 1; |
| 12583 | default: |
| 12584 | return 0; |
| 12585 | } |
| 12586 | } |
| 12587 | |
| 12588 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 12589 | const char *file, int line, char **err) |
| 12590 | { |
| 12591 | switch(args[0].data.sint) { |
| 12592 | case 128: |
| 12593 | case 192: |
| 12594 | case 256: |
| 12595 | break; |
| 12596 | default: |
| 12597 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 12598 | return 0; |
| 12599 | } |
| 12600 | /* Try to decode a variable. */ |
| 12601 | vars_check_arg(&args[1], NULL); |
| 12602 | vars_check_arg(&args[2], NULL); |
| 12603 | vars_check_arg(&args[3], NULL); |
| 12604 | return 1; |
| 12605 | } |
| 12606 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12607 | /* 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] | 12608 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 12609 | { |
| 12610 | struct sample nonce, key, aead_tag; |
| 12611 | struct buffer *smp_trash, *smp_trash_alloc; |
| 12612 | EVP_CIPHER_CTX *ctx; |
| 12613 | int dec_size, ret; |
| 12614 | |
| 12615 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 12616 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 12617 | return 0; |
| 12618 | |
| 12619 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 12620 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 12621 | return 0; |
| 12622 | |
| 12623 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 12624 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 12625 | return 0; |
| 12626 | |
| 12627 | smp_trash = get_trash_chunk(); |
| 12628 | smp_trash_alloc = alloc_trash_chunk(); |
| 12629 | if (!smp_trash_alloc) |
| 12630 | return 0; |
| 12631 | |
| 12632 | ctx = EVP_CIPHER_CTX_new(); |
| 12633 | |
| 12634 | if (!ctx) |
| 12635 | goto err; |
| 12636 | |
| 12637 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12638 | if (dec_size < 0) |
| 12639 | goto err; |
| 12640 | smp_trash->data = dec_size; |
| 12641 | |
| 12642 | /* Set cipher type and mode */ |
| 12643 | switch(arg_p[0].data.sint) { |
| 12644 | case 128: |
| 12645 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 12646 | break; |
| 12647 | case 192: |
| 12648 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 12649 | break; |
| 12650 | case 256: |
| 12651 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 12652 | break; |
| 12653 | } |
| 12654 | |
| 12655 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 12656 | |
| 12657 | /* Initialise IV */ |
| 12658 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 12659 | goto err; |
| 12660 | |
| 12661 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 12662 | if (dec_size < 0) |
| 12663 | goto err; |
| 12664 | smp_trash->data = dec_size; |
| 12665 | |
| 12666 | /* Initialise key */ |
| 12667 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 12668 | goto err; |
| 12669 | |
| 12670 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 12671 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 12672 | goto err; |
| 12673 | |
| 12674 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 12675 | if (dec_size < 0) |
| 12676 | goto err; |
| 12677 | smp_trash_alloc->data = dec_size; |
| 12678 | dec_size = smp_trash->data; |
| 12679 | |
| 12680 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 12681 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 12682 | |
| 12683 | if (ret <= 0) |
| 12684 | goto err; |
| 12685 | |
| 12686 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 12687 | smp->data.u.str.area = smp_trash->area; |
| 12688 | smp->data.type = SMP_T_BIN; |
| 12689 | smp->flags &= ~SMP_F_CONST; |
| 12690 | free_trash_chunk(smp_trash_alloc); |
| 12691 | return 1; |
| 12692 | |
| 12693 | err: |
| 12694 | free_trash_chunk(smp_trash_alloc); |
| 12695 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12696 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12697 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12698 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 12699 | /* Argument validation functions */ |
| 12700 | |
| 12701 | /* This function is used to validate the arguments passed to any "x_dn" ssl |
| 12702 | * keywords. These keywords support specifying a third parameter that must be |
| 12703 | * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK. |
| 12704 | */ |
| 12705 | int val_dnfmt(struct arg *arg, char **err_msg) |
| 12706 | { |
| 12707 | if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) { |
| 12708 | memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument."); |
| 12709 | return 0; |
| 12710 | } |
| 12711 | return 1; |
| 12712 | } |
| 12713 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12714 | /* register cli keywords */ |
| 12715 | static struct cli_kw_list cli_kws = {{ },{ |
| 12716 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 12717 | { { "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] | 12718 | { { "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] | 12719 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 12720 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | ea987ed | 2020-03-19 16:48:33 +0100 | [diff] [blame] | 12721 | { { "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] | 12722 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 12723 | { { "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] | 12724 | { { "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] | 12725 | { { "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] | 12726 | { { "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] | 12727 | { { "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] | 12728 | { { "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] | 12729 | { { "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] | 12730 | { { NULL }, NULL, NULL, NULL } |
| 12731 | }}; |
| 12732 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12733 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 12734 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12735 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12736 | * Please take care of keeping this list alphabetically sorted. |
| 12737 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12738 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12739 | { "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] | 12740 | { "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] | 12741 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 12742 | { "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] | 12743 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12744 | { "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] | 12745 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 12746 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 12747 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 12748 | { "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] | 12749 | { "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] | 12750 | { "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] | 12751 | { "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] | 12752 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 12753 | { "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] | 12754 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12755 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12756 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12757 | { "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] | 12758 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 12759 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12760 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12761 | { "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] | 12762 | { "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] | 12763 | { "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] | 12764 | { "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] | 12765 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12766 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12767 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12768 | { "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] | 12769 | { "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] | 12770 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12771 | { "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] | 12772 | { "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] | 12773 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 12774 | { "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] | 12775 | { "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] | 12776 | { "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] | 12777 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12778 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12779 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12780 | { "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] | 12781 | { "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] | 12782 | { "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] | 12783 | { "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] | 12784 | { "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] | 12785 | { "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] | 12786 | { "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] | 12787 | { "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] | 12788 | { "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] | 12789 | { "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] | 12790 | { "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] | 12791 | { "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] | 12792 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12793 | { "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] | 12794 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 12795 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12796 | { "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] | 12797 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12798 | { "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] | 12799 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 12800 | { "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] | 12801 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12802 | { "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] | 12803 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12804 | { "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] | 12805 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12806 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 12807 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12808 | { "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] | 12809 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12810 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 12811 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12812 | { "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] | 12813 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12814 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12815 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 12816 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 12817 | { "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] | 12818 | { NULL, NULL, 0, 0, 0 }, |
| 12819 | }}; |
| 12820 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12821 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 12822 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12823 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12824 | * Please take care of keeping this list alphabetically sorted. |
| 12825 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12826 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12827 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 12828 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12829 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 12830 | }}; |
| 12831 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12832 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 12833 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12834 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12835 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12836 | * all code contributors. |
| 12837 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12838 | * the config parser can report an appropriate error when a known keyword was |
| 12839 | * not enabled. |
| 12840 | */ |
William Lallemand | 557823f | 2020-04-01 17:42:47 +0200 | [diff] [blame] | 12841 | |
| 12842 | /* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe |
| 12843 | * 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] | 12844 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12845 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12846 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12847 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12848 | { "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] | 12849 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12850 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12851 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12852 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12853 | { "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] | 12854 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12855 | { "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] | 12856 | { "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] | 12857 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 12858 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 12859 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 12860 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12861 | { NULL, NULL, 0 }, |
| 12862 | }; |
| 12863 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12864 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 12865 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 12866 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 12867 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12868 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 12869 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */ |
| 12870 | { "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] | 12871 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 12872 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 12873 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 12874 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12875 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12876 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 12877 | #endif |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12878 | { "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] | 12879 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 12880 | { "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] | 12881 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 12882 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 12883 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 12884 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 12885 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 12886 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 12887 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12888 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12889 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 12890 | { "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] | 12891 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 12892 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 12893 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 12894 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 12895 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12896 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 12897 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12898 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 12899 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 12900 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 12901 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 12902 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 12903 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 12904 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 12905 | { NULL, NULL, 0 }, |
| 12906 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12907 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12908 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 12909 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 12910 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12911 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 12912 | * all code contributors. |
| 12913 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 12914 | * the config parser can report an appropriate error when a known keyword was |
| 12915 | * not enabled. |
| 12916 | */ |
| 12917 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 12918 | { "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] | 12919 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12920 | { "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] | 12921 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 12922 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12923 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 12924 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12925 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12926 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 12927 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12928 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 12929 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 12930 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 12931 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 12932 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 12933 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 12934 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 12935 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 12936 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 12937 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 12938 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 12939 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 12940 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 12941 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 12942 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 12943 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 12944 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 12945 | { "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] | 12946 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 12947 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 12948 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 12949 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 12950 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 12951 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 12952 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 12953 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 12954 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 12955 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 12956 | { "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] | 12957 | { NULL, NULL, 0, 0 }, |
| 12958 | }}; |
| 12959 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12960 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 12961 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12962 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 12963 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 12964 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 12965 | { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 12966 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12967 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 12968 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 12969 | #ifndef OPENSSL_NO_DH |
| 12970 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 12971 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 12972 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12973 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12974 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12975 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 12976 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 12977 | #ifndef OPENSSL_NO_DH |
| 12978 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 12979 | #endif |
| 12980 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 12981 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 12982 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 12983 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 12984 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 12985 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 12986 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 12987 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 12988 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12989 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 12990 | #endif |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 12991 | { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 12992 | { 0, NULL, NULL }, |
| 12993 | }}; |
| 12994 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12995 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12996 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 12997 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12998 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 12999 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 13000 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 13001 | #endif |
| 13002 | { NULL, NULL, 0, 0, 0 }, |
| 13003 | }}; |
| 13004 | |
| 13005 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 13006 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 13007 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 13008 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13009 | .snd_buf = ssl_sock_from_buf, |
| 13010 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 13011 | .subscribe = ssl_subscribe, |
| 13012 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 13013 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 13014 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13015 | .rcv_pipe = NULL, |
| 13016 | .snd_pipe = NULL, |
| 13017 | .shutr = NULL, |
| 13018 | .shutw = ssl_sock_shutw, |
| 13019 | .close = ssl_sock_close, |
| 13020 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 13021 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 13022 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 13023 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 13024 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 13025 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 13026 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13027 | }; |
| 13028 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13029 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 13030 | struct session *sess, struct stream *s, int flags) |
| 13031 | { |
| 13032 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13033 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13034 | |
| 13035 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13036 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13037 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 13038 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13039 | 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] | 13040 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 13041 | s->req.flags |= CF_READ_NULL; |
| 13042 | return ACT_RET_YIELD; |
| 13043 | } |
| 13044 | } |
| 13045 | return (ACT_RET_CONT); |
| 13046 | } |
| 13047 | |
| 13048 | 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) |
| 13049 | { |
| 13050 | rule->action_ptr = ssl_action_wait_for_hs; |
| 13051 | |
| 13052 | return ACT_RET_PRS_OK; |
| 13053 | } |
| 13054 | |
| 13055 | static struct action_kw_list http_req_actions = {ILH, { |
| 13056 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 13057 | { /* END */ } |
| 13058 | }}; |
| 13059 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 13060 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 13061 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13062 | #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] | 13063 | |
| 13064 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 13065 | { |
| 13066 | if (ptr) { |
| 13067 | chunk_destroy(ptr); |
| 13068 | free(ptr); |
| 13069 | } |
| 13070 | } |
| 13071 | |
| 13072 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 13073 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 13074 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 13075 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 13076 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 13077 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13078 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 13079 | static void __ssl_sock_init(void) |
| 13080 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13081 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13082 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13083 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13084 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13085 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 13086 | if (global_ssl.listen_default_ciphers) |
| 13087 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 13088 | if (global_ssl.connect_default_ciphers) |
| 13089 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 13090 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 13091 | if (global_ssl.listen_default_ciphersuites) |
| 13092 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 13093 | if (global_ssl.connect_default_ciphersuites) |
| 13094 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 13095 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 13096 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 13097 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 13098 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13099 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13100 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13101 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13102 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13103 | n = sk_SSL_COMP_num(cm); |
| 13104 | while (n--) { |
| 13105 | (void) sk_SSL_COMP_pop(cm); |
| 13106 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 13107 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 13108 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13109 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13110 | ssl_locking_init(); |
| 13111 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13112 | #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] | 13113 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 13114 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 13115 | 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] | 13116 | 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] | 13117 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13118 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 13119 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13120 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 13121 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 13122 | hap_register_post_check(tlskeys_finalize_config); |
| 13123 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13124 | |
| 13125 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 13126 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 13127 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 13128 | hap_register_post_deinit(ssl_free_global_issuers); |
| 13129 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13130 | #ifndef OPENSSL_NO_DH |
| 13131 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 13132 | hap_register_post_deinit(ssl_free_dh); |
| 13133 | #endif |
| 13134 | #ifndef OPENSSL_NO_ENGINE |
| 13135 | hap_register_post_deinit(ssl_free_engines); |
| 13136 | #endif |
| 13137 | /* Load SSL string for the verbose & debug mode. */ |
| 13138 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 13139 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 13140 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 13141 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 13142 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 13143 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 13144 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 13145 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 13146 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 13147 | |
| 13148 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13149 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 13150 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13151 | /* Compute and register the version string */ |
| 13152 | static void ssl_register_build_options() |
| 13153 | { |
| 13154 | char *ptr = NULL; |
| 13155 | int i; |
| 13156 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13157 | memprintf(&ptr, "Built with OpenSSL version : " |
| 13158 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 13159 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13160 | #else /* OPENSSL_IS_BORINGSSL */ |
| 13161 | OPENSSL_VERSION_TEXT |
| 13162 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13163 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 13164 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13165 | #endif |
| 13166 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 13167 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13168 | "no (library version too old)" |
| 13169 | #elif defined(OPENSSL_NO_TLSEXT) |
| 13170 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 13171 | #else |
| 13172 | "yes" |
| 13173 | #endif |
| 13174 | "", ptr); |
| 13175 | |
| 13176 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 13177 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 13178 | "yes" |
| 13179 | #else |
| 13180 | #ifdef OPENSSL_NO_TLSEXT |
| 13181 | "no (because of OPENSSL_NO_TLSEXT)" |
| 13182 | #else |
| 13183 | "no (version might be too old, 0.9.8f min needed)" |
| 13184 | #endif |
| 13185 | #endif |
| 13186 | "", ptr); |
| 13187 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 13188 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 13189 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 13190 | if (methodVersions[i].option) |
| 13191 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 13192 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13193 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13194 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 13195 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13196 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 13197 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13198 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13199 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13200 | void ssl_free_engines(void) { |
| 13201 | struct ssl_engine_list *wl, *wlb; |
| 13202 | /* free up engine list */ |
| 13203 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 13204 | ENGINE_finish(wl->e); |
| 13205 | ENGINE_free(wl->e); |
| 13206 | LIST_DEL(&wl->list); |
| 13207 | free(wl); |
| 13208 | } |
| 13209 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 13210 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 13211 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13212 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13213 | void ssl_free_dh(void) { |
| 13214 | if (local_dh_1024) { |
| 13215 | DH_free(local_dh_1024); |
| 13216 | local_dh_1024 = NULL; |
| 13217 | } |
| 13218 | if (local_dh_2048) { |
| 13219 | DH_free(local_dh_2048); |
| 13220 | local_dh_2048 = NULL; |
| 13221 | } |
| 13222 | if (local_dh_4096) { |
| 13223 | DH_free(local_dh_4096); |
| 13224 | local_dh_4096 = NULL; |
| 13225 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 13226 | if (global_dh) { |
| 13227 | DH_free(global_dh); |
| 13228 | global_dh = NULL; |
| 13229 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 13230 | } |
| 13231 | #endif |
| 13232 | |
| 13233 | __attribute__((destructor)) |
| 13234 | static void __ssl_sock_deinit(void) |
| 13235 | { |
| 13236 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13237 | if (ssl_ctx_lru_tree) { |
| 13238 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 13239 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 13240 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13241 | #endif |
| 13242 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13243 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13244 | ERR_remove_state(0); |
| 13245 | ERR_free_strings(); |
| 13246 | |
| 13247 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 13248 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13249 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 13250 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13251 | CRYPTO_cleanup_all_ex_data(); |
| 13252 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 13253 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 13254 | } |
| 13255 | |
| 13256 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 13257 | /* |
| 13258 | * Local variables: |
| 13259 | * c-indent-level: 8 |
| 13260 | * c-basic-offset: 8 |
| 13261 | * End: |
| 13262 | */ |