yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 12 | * Acknowledgement: |
| 13 | * We'd like to specially thank the Stud project authors for a very clean |
| 14 | * and well documented code which helped us understand how the OpenSSL API |
| 15 | * ought to be used in non-blocking mode. This is one difficult part which |
| 16 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 17 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 18 | * the good works, guys ! |
| 19 | * |
| 20 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 21 | * particularly well with haproxy. For more info about this project, visit : |
| 22 | * https://github.com/bumptech/stud |
| 23 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Willy Tarreau | 8d164dc | 2019-05-10 09:35:00 +0200 | [diff] [blame] | 26 | /* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 27 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 36 | |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 40 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 41 | #include <netinet/tcp.h> |
| 42 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 43 | #include <import/lru.h> |
| 44 | #include <import/xxhash.h> |
| 45 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 46 | #include <common/buffer.h> |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 47 | #include <common/chunk.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 48 | #include <common/compat.h> |
| 49 | #include <common/config.h> |
| 50 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 51 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 52 | #include <common/initcall.h> |
Willy Tarreau | 5599456 | 2019-05-09 14:52:44 +0200 | [diff] [blame] | 53 | #include <common/openssl-compat.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 54 | #include <common/standard.h> |
| 55 | #include <common/ticks.h> |
| 56 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 57 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 58 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 60 | #include <ebsttree.h> |
| 61 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 62 | #include <types/applet.h> |
| 63 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 64 | #include <types/global.h> |
| 65 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 66 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 68 | #include <proto/acl.h> |
| 69 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 70 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 71 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 72 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 73 | #include <proto/fd.h> |
| 74 | #include <proto/freq_ctr.h> |
| 75 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 76 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 77 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 78 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 79 | #include <proto/proto_tcp.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 80 | #include <proto/http_ana.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 81 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 82 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 83 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 84 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 85 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 86 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 87 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/task.h> |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 89 | #include <proto/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 91 | /* ***** READ THIS before adding code here! ***** |
| 92 | * |
| 93 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 94 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 95 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 96 | * exclusively so that the whole code consistently uses the same macros. |
| 97 | * |
| 98 | * Whenever possible if a macro is missing in certain versions, it's better |
| 99 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 100 | */ |
| 101 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 102 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 103 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 104 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 105 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 106 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 107 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 108 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 109 | |
| 110 | /* Verify errors macros */ |
| 111 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 112 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 113 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 114 | |
| 115 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 116 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 117 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 118 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 119 | /* ssl_methods flags for ssl options */ |
| 120 | #define MC_SSL_O_ALL 0x0000 |
| 121 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 122 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 123 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 124 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 125 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 126 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 127 | /* file to guess during file loading */ |
| 128 | #define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */ |
| 129 | #define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */ |
| 130 | #define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */ |
| 131 | #define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */ |
| 132 | #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] | 133 | #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] | 134 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 135 | #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] | 136 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 137 | /* ssl_methods versions */ |
| 138 | enum { |
| 139 | CONF_TLSV_NONE = 0, |
| 140 | CONF_TLSV_MIN = 1, |
| 141 | CONF_SSLV3 = 1, |
| 142 | CONF_TLSV10 = 2, |
| 143 | CONF_TLSV11 = 3, |
| 144 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 145 | CONF_TLSV13 = 5, |
| 146 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 149 | /* server and bind verify method, it uses a global value as default */ |
| 150 | enum { |
| 151 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 152 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 153 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 154 | SSL_SOCK_VERIFY_NONE = 3, |
| 155 | }; |
| 156 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 157 | /* issuer chain store with hash of Subject Key Identifier |
| 158 | certificate/issuer matching is verify with X509_check_issued |
| 159 | */ |
| 160 | struct issuer_chain { |
| 161 | struct eb64_node node; |
| 162 | STACK_OF(X509) *chain; |
| 163 | char *path; |
| 164 | }; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 165 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 166 | int sslconns = 0; |
| 167 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 168 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 169 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 171 | static struct { |
| 172 | char *crt_base; /* base directory path for certificates */ |
| 173 | char *ca_base; /* base directory path for CAs and CRLs */ |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 174 | char *issuers_chain_path; /* from "issuers-chain-path" */ |
| 175 | struct eb_root cert_issuer_tree; /* issuers tree from "issuers-chain-path" */ |
| 176 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 177 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 178 | |
| 179 | char *listen_default_ciphers; |
| 180 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 181 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 182 | char *listen_default_ciphersuites; |
| 183 | char *connect_default_ciphersuites; |
| 184 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 185 | int listen_default_ssloptions; |
| 186 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 187 | struct tls_version_filter listen_default_sslmethods; |
| 188 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 189 | |
| 190 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 191 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 192 | unsigned int max_record; /* SSL max record size */ |
| 193 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 194 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 195 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 196 | 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] | 197 | } global_ssl = { |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 198 | .cert_issuer_tree = EB_ROOT, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 199 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 200 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 201 | #endif |
| 202 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 203 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 204 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 205 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 206 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 207 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 208 | #endif |
| 209 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 210 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 211 | #endif |
| 212 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 213 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 214 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 215 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 216 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 217 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 218 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 219 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 220 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 221 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 222 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 223 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 224 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 225 | #endif |
| 226 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 227 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 228 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 229 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 230 | }; |
| 231 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 232 | static BIO_METHOD *ha_meth; |
| 233 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 234 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 235 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 236 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 237 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 238 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 239 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 240 | struct wait_event wait_event; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 241 | struct wait_event *subs; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 242 | int xprt_st; /* transport layer state, initialized to zero */ |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 243 | struct buffer early_buf; /* buffer to store the early data received */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 244 | int sent_early_data; /* Amount of early data we sent so far */ |
| 245 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 249 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 250 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 251 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 252 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 253 | /* Methods to implement OpenSSL BIO */ |
| 254 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 255 | { |
| 256 | struct buffer tmpbuf; |
| 257 | struct ssl_sock_ctx *ctx; |
| 258 | int ret; |
| 259 | |
| 260 | ctx = BIO_get_data(h); |
| 261 | tmpbuf.size = num; |
| 262 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 263 | tmpbuf.data = num; |
| 264 | tmpbuf.head = 0; |
| 265 | 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] | 266 | 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] | 267 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 268 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 269 | } else if (ret == 0) |
| 270 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 275 | { |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int ha_ssl_puts(BIO *h, const char *str) |
| 281 | { |
| 282 | |
| 283 | return ha_ssl_write(h, str, strlen(str)); |
| 284 | } |
| 285 | |
| 286 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 287 | { |
| 288 | struct buffer tmpbuf; |
| 289 | struct ssl_sock_ctx *ctx; |
| 290 | int ret; |
| 291 | |
| 292 | ctx = BIO_get_data(h); |
| 293 | tmpbuf.size = size; |
| 294 | tmpbuf.area = buf; |
| 295 | tmpbuf.data = 0; |
| 296 | tmpbuf.head = 0; |
| 297 | 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] | 298 | 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] | 299 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 300 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 301 | } else if (ret == 0) |
| 302 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 308 | { |
| 309 | int ret = 0; |
| 310 | switch (cmd) { |
| 311 | case BIO_CTRL_DUP: |
| 312 | case BIO_CTRL_FLUSH: |
| 313 | ret = 1; |
| 314 | break; |
| 315 | } |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int ha_ssl_new(BIO *h) |
| 320 | { |
| 321 | BIO_set_init(h, 1); |
| 322 | BIO_set_data(h, NULL); |
| 323 | BIO_clear_flags(h, ~0); |
| 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | static int ha_ssl_free(BIO *data) |
| 328 | { |
| 329 | |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 334 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 335 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 336 | static HA_RWLOCK_T *ssl_rwlocks; |
| 337 | |
| 338 | |
| 339 | unsigned long ssl_id_function(void) |
| 340 | { |
| 341 | return (unsigned long)tid; |
| 342 | } |
| 343 | |
| 344 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 345 | { |
| 346 | if (mode & CRYPTO_LOCK) { |
| 347 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 348 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 349 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 350 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 351 | } |
| 352 | else { |
| 353 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 354 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 355 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 356 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | static int ssl_locking_init(void) |
| 361 | { |
| 362 | int i; |
| 363 | |
| 364 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 365 | if (!ssl_rwlocks) |
| 366 | return -1; |
| 367 | |
| 368 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 369 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 370 | |
| 371 | CRYPTO_set_id_callback(ssl_id_function); |
| 372 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 373 | |
| 374 | return 0; |
| 375 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 376 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 377 | #endif |
| 378 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 379 | __decl_hathreads(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 380 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 381 | /* Uncommitted CKCH transaction */ |
| 382 | |
| 383 | static struct { |
| 384 | struct ckch_store *new_ckchs; |
| 385 | struct ckch_store *old_ckchs; |
| 386 | char *path; |
| 387 | } ckchs_transaction; |
| 388 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 389 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 390 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 391 | */ |
| 392 | struct cafile_entry { |
| 393 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 394 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 395 | struct ebmb_node node; |
| 396 | char path[0]; |
| 397 | }; |
| 398 | |
| 399 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 400 | |
| 401 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 402 | { |
| 403 | struct ebmb_node *eb; |
| 404 | |
| 405 | eb = ebst_lookup(&cafile_tree, path); |
| 406 | if (eb) { |
| 407 | struct cafile_entry *ca_e; |
| 408 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 409 | return ca_e->ca_store; |
| 410 | } |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | static int ssl_store_load_locations_file(char *path) |
| 415 | { |
| 416 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 417 | struct cafile_entry *ca_e; |
| 418 | X509_STORE *store = X509_STORE_new(); |
| 419 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 420 | int pathlen; |
| 421 | pathlen = strlen(path); |
| 422 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 423 | if (ca_e) { |
| 424 | memcpy(ca_e->path, path, pathlen + 1); |
| 425 | ca_e->ca_store = store; |
| 426 | ebst_insert(&cafile_tree, &ca_e->node); |
| 427 | return 1; |
| 428 | } |
| 429 | } |
| 430 | X509_STORE_free(store); |
| 431 | return 0; |
| 432 | } |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 437 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 438 | { |
| 439 | X509_STORE *store; |
| 440 | store = ssl_store_get0_locations_file(path); |
| 441 | if (store_ctx && store) { |
| 442 | int i; |
| 443 | X509_OBJECT *obj; |
| 444 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 445 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 446 | obj = sk_X509_OBJECT_value(objs, i); |
| 447 | switch (X509_OBJECT_get_type(obj)) { |
| 448 | case X509_LU_X509: |
| 449 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 450 | break; |
| 451 | case X509_LU_CRL: |
| 452 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 453 | break; |
| 454 | default: |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | return 1; |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */ |
| 464 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 465 | { |
| 466 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 467 | return ssl_set_cert_crl_file(store_ctx, path); |
| 468 | } |
| 469 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 470 | /* |
| 471 | Extract CA_list from CA_file already in tree. |
| 472 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 473 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 474 | */ |
| 475 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 476 | { |
| 477 | struct ebmb_node *eb; |
| 478 | struct cafile_entry *ca_e; |
| 479 | |
| 480 | eb = ebst_lookup(&cafile_tree, path); |
| 481 | if (!eb) |
| 482 | return NULL; |
| 483 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 484 | |
| 485 | if (ca_e->ca_list == NULL) { |
| 486 | int i; |
| 487 | unsigned long key; |
| 488 | struct eb_root ca_name_tree = EB_ROOT; |
| 489 | struct eb64_node *node, *back; |
| 490 | struct { |
| 491 | struct eb64_node node; |
| 492 | X509_NAME *xname; |
| 493 | } *ca_name; |
| 494 | STACK_OF(X509_OBJECT) *objs; |
| 495 | STACK_OF(X509_NAME) *skn; |
| 496 | X509 *x; |
| 497 | X509_NAME *xn; |
| 498 | |
| 499 | skn = sk_X509_NAME_new_null(); |
| 500 | /* take x509 from cafile_tree */ |
| 501 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 502 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 503 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 504 | if (!x) |
| 505 | continue; |
| 506 | xn = X509_get_subject_name(x); |
| 507 | if (!xn) |
| 508 | continue; |
| 509 | /* Check for duplicates. */ |
| 510 | key = X509_NAME_hash(xn); |
| 511 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 512 | node && ca_name == NULL; |
| 513 | node = eb64_next(node)) { |
| 514 | ca_name = container_of(node, typeof(*ca_name), node); |
| 515 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 516 | ca_name = NULL; |
| 517 | } |
| 518 | /* find a duplicate */ |
| 519 | if (ca_name) |
| 520 | continue; |
| 521 | ca_name = calloc(1, sizeof *ca_name); |
| 522 | xn = X509_NAME_dup(xn); |
| 523 | if (!ca_name || |
| 524 | !xn || |
| 525 | !sk_X509_NAME_push(skn, xn)) { |
| 526 | free(ca_name); |
| 527 | X509_NAME_free(xn); |
| 528 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 529 | sk_X509_NAME_free(skn); |
| 530 | skn = NULL; |
| 531 | break; |
| 532 | } |
| 533 | ca_name->node.key = key; |
| 534 | ca_name->xname = xn; |
| 535 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 536 | } |
| 537 | ca_e->ca_list = skn; |
| 538 | /* remove temporary ca_name tree */ |
| 539 | node = eb64_first(&ca_name_tree); |
| 540 | while (node) { |
| 541 | ca_name = container_of(node, typeof(*ca_name), node); |
| 542 | back = eb64_next(node); |
| 543 | eb64_delete(node); |
| 544 | free(ca_name); |
| 545 | node = back; |
| 546 | } |
| 547 | } |
| 548 | return ca_e->ca_list; |
| 549 | } |
| 550 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 551 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 552 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 553 | unsigned long long int xxh64; |
| 554 | unsigned char ciphersuite_len; |
| 555 | char ciphersuite[0]; |
| 556 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 557 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 558 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 559 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 560 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 561 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 562 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 563 | #endif |
| 564 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 565 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 566 | static unsigned int openssl_engines_initialized; |
| 567 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 568 | struct ssl_engine_list { |
| 569 | struct list list; |
| 570 | ENGINE *e; |
| 571 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 572 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 573 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 574 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 575 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 576 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 577 | static DH *local_dh_1024 = NULL; |
| 578 | static DH *local_dh_2048 = NULL; |
| 579 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 580 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 581 | #endif /* OPENSSL_NO_DH */ |
| 582 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 583 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 584 | /* X509V3 Extensions that will be added on generated certificates */ |
| 585 | #define X509V3_EXT_SIZE 5 |
| 586 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 587 | "basicConstraints", |
| 588 | "nsComment", |
| 589 | "subjectKeyIdentifier", |
| 590 | "authorityKeyIdentifier", |
| 591 | "keyUsage", |
| 592 | }; |
| 593 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 594 | "CA:FALSE", |
| 595 | "\"OpenSSL Generated Certificate\"", |
| 596 | "hash", |
| 597 | "keyid,issuer:always", |
| 598 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 599 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 600 | /* LRU cache to store generated certificate */ |
| 601 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 602 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 603 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 604 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 605 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 606 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 607 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 608 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 609 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 610 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 611 | /* The order here matters for picking a default context, |
| 612 | * keep the most common keytype at the bottom of the list |
| 613 | */ |
| 614 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 615 | "dsa", |
| 616 | "ecdsa", |
| 617 | "rsa" |
| 618 | }; |
| 619 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 620 | #else |
| 621 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 622 | #endif |
| 623 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 624 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 625 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 626 | |
| 627 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 628 | |
| 629 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 630 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 631 | |
| 632 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 633 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 634 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 635 | /* |
| 636 | * This function gives the detail of the SSL error. It is used only |
| 637 | * if the debug mode and the verbose mode are activated. It dump all |
| 638 | * the SSL error until the stack was empty. |
| 639 | */ |
| 640 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 641 | { |
| 642 | unsigned long ret; |
| 643 | |
| 644 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 645 | while(1) { |
| 646 | ret = ERR_get_error(); |
| 647 | if (ret == 0) |
| 648 | return; |
| 649 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 650 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 651 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 656 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 657 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 658 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 659 | { |
| 660 | int err_code = ERR_ABORT; |
| 661 | ENGINE *engine; |
| 662 | struct ssl_engine_list *el; |
| 663 | |
| 664 | /* grab the structural reference to the engine */ |
| 665 | engine = ENGINE_by_id(engine_id); |
| 666 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 667 | 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] | 668 | goto fail_get; |
| 669 | } |
| 670 | |
| 671 | if (!ENGINE_init(engine)) { |
| 672 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 673 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 674 | goto fail_init; |
| 675 | } |
| 676 | |
| 677 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 678 | 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] | 679 | goto fail_set_method; |
| 680 | } |
| 681 | |
| 682 | el = calloc(1, sizeof(*el)); |
| 683 | el->e = engine; |
| 684 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 685 | nb_engines++; |
| 686 | if (global_ssl.async) |
| 687 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 688 | return 0; |
| 689 | |
| 690 | fail_set_method: |
| 691 | /* release the functional reference from ENGINE_init() */ |
| 692 | ENGINE_finish(engine); |
| 693 | |
| 694 | fail_init: |
| 695 | /* release the structural reference from ENGINE_by_id() */ |
| 696 | ENGINE_free(engine); |
| 697 | |
| 698 | fail_get: |
| 699 | return err_code; |
| 700 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 701 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 702 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 703 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 704 | /* |
| 705 | * openssl async fd handler |
| 706 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 707 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 708 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 709 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 710 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 711 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 712 | * to poll this fd until it is requested |
| 713 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 714 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 715 | fd_cant_recv(fd); |
| 716 | |
| 717 | /* crypto engine is available, let's notify the associated |
| 718 | * connection that it can pursue its processing. |
| 719 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 720 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 723 | /* |
| 724 | * openssl async delayed SSL_free handler |
| 725 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 726 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 727 | { |
| 728 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 729 | OSSL_ASYNC_FD all_fd[32]; |
| 730 | size_t num_all_fds = 0; |
| 731 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 732 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 733 | /* We suppose that the async job for a same SSL * |
| 734 | * are serialized. So if we are awake it is |
| 735 | * because the running job has just finished |
| 736 | * and we can remove all async fds safely |
| 737 | */ |
| 738 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 739 | if (num_all_fds > 32) { |
| 740 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 745 | for (i=0 ; i < num_all_fds ; i++) |
| 746 | fd_remove(all_fd[i]); |
| 747 | |
| 748 | /* 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] | 749 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 750 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 751 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 752 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 753 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 754 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 755 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 756 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 757 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 758 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 759 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 760 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 761 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 762 | size_t num_add_fds = 0; |
| 763 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 764 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 765 | |
| 766 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 767 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 768 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 769 | 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] | 770 | return; |
| 771 | } |
| 772 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 773 | 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] | 774 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 775 | /* We remove unused fds from the fdtab */ |
| 776 | for (i=0 ; i < num_del_fds ; i++) |
| 777 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 778 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 779 | /* We add new fds to the fdtab */ |
| 780 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 781 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 784 | num_add_fds = 0; |
| 785 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 786 | if (num_add_fds > 32) { |
| 787 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 788 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 789 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 790 | |
| 791 | /* We activate the polling for all known async fds */ |
| 792 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 793 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 794 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 795 | /* To ensure that the fd cache won't be used |
| 796 | * We'll prefer to catch a real RD event |
| 797 | * because handling an EAGAIN on this fd will |
| 798 | * result in a context switch and also |
| 799 | * some engines uses a fd in blocking mode. |
| 800 | */ |
| 801 | fd_cant_recv(add_fd[i]); |
| 802 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 803 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 804 | } |
| 805 | #endif |
| 806 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 807 | #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] | 808 | /* |
| 809 | * This function returns the number of seconds elapsed |
| 810 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 811 | * date presented un ASN1_GENERALIZEDTIME. |
| 812 | * |
| 813 | * In parsing error case, it returns -1. |
| 814 | */ |
| 815 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 816 | { |
| 817 | long epoch; |
| 818 | char *p, *end; |
| 819 | const unsigned short month_offset[12] = { |
| 820 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 821 | }; |
| 822 | int year, month; |
| 823 | |
| 824 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 825 | |
| 826 | p = (char *)d->data; |
| 827 | end = p + d->length; |
| 828 | |
| 829 | if (end - p < 4) return -1; |
| 830 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 831 | p += 4; |
| 832 | if (end - p < 2) return -1; |
| 833 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 834 | if (month < 1 || month > 12) return -1; |
| 835 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 836 | We consider leap years and the current month (<marsh or not) */ |
| 837 | epoch = ( ((year - 1970) * 365) |
| 838 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 839 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 840 | + month_offset[month-1] |
| 841 | ) * 24 * 60 * 60; |
| 842 | p += 2; |
| 843 | if (end - p < 2) return -1; |
| 844 | /* Add the number of seconds of completed days of current month */ |
| 845 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 846 | p += 2; |
| 847 | if (end - p < 2) return -1; |
| 848 | /* Add the completed hours of the current day */ |
| 849 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 850 | p += 2; |
| 851 | if (end - p < 2) return -1; |
| 852 | /* Add the completed minutes of the current hour */ |
| 853 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 854 | p += 2; |
| 855 | if (p == end) return -1; |
| 856 | /* Test if there is available seconds */ |
| 857 | if (p[0] < '0' || p[0] > '9') |
| 858 | goto nosec; |
| 859 | if (end - p < 2) return -1; |
| 860 | /* Add the seconds of the current minute */ |
| 861 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 862 | p += 2; |
| 863 | if (p == end) return -1; |
| 864 | /* Ignore seconds float part if present */ |
| 865 | if (p[0] == '.') { |
| 866 | do { |
| 867 | if (++p == end) return -1; |
| 868 | } while (p[0] >= '0' && p[0] <= '9'); |
| 869 | } |
| 870 | |
| 871 | nosec: |
| 872 | if (p[0] == 'Z') { |
| 873 | if (end - p != 1) return -1; |
| 874 | return epoch; |
| 875 | } |
| 876 | else if (p[0] == '+') { |
| 877 | if (end - p != 5) return -1; |
| 878 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 879 | 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] | 880 | } |
| 881 | else if (p[0] == '-') { |
| 882 | if (end - p != 5) return -1; |
| 883 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 884 | 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] | 885 | } |
| 886 | |
| 887 | return -1; |
| 888 | } |
| 889 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 890 | /* |
| 891 | * struct alignment works here such that the key.key is the same as key_data |
| 892 | * Do not change the placement of key_data |
| 893 | */ |
| 894 | struct certificate_ocsp { |
| 895 | struct ebmb_node key; |
| 896 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 897 | struct buffer response; |
| 898 | long expire; |
| 899 | }; |
| 900 | |
| 901 | struct ocsp_cbk_arg { |
| 902 | int is_single; |
| 903 | int single_kt; |
| 904 | union { |
| 905 | struct certificate_ocsp *s_ocsp; |
| 906 | /* |
| 907 | * m_ocsp will have multiple entries dependent on key type |
| 908 | * Entry 0 - DSA |
| 909 | * Entry 1 - ECDSA |
| 910 | * Entry 2 - RSA |
| 911 | */ |
| 912 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 913 | }; |
| 914 | }; |
| 915 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 916 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 917 | |
| 918 | /* This function starts to check if the OCSP response (in DER format) contained |
| 919 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 920 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 921 | * contained in the OCSP Response and exits on error if no match. |
| 922 | * If it's a valid OCSP Response: |
| 923 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 924 | * pointed by 'ocsp'. |
| 925 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 926 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 927 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 928 | * already present in the container, it will be overwritten. |
| 929 | * |
| 930 | * Note: OCSP response containing more than one OCSP Single response is not |
| 931 | * considered valid. |
| 932 | * |
| 933 | * Returns 0 on success, 1 in error case. |
| 934 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 935 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 936 | struct certificate_ocsp *ocsp, |
| 937 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 938 | { |
| 939 | OCSP_RESPONSE *resp; |
| 940 | OCSP_BASICRESP *bs = NULL; |
| 941 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 942 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 943 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 944 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 945 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 946 | int reason; |
| 947 | int ret = 1; |
| 948 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 949 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 950 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 951 | if (!resp) { |
| 952 | memprintf(err, "Unable to parse OCSP response"); |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | rc = OCSP_response_status(resp); |
| 957 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 958 | memprintf(err, "OCSP response status not successful"); |
| 959 | goto out; |
| 960 | } |
| 961 | |
| 962 | bs = OCSP_response_get1_basic(resp); |
| 963 | if (!bs) { |
| 964 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 965 | goto out; |
| 966 | } |
| 967 | |
| 968 | count_sr = OCSP_resp_count(bs); |
| 969 | if (count_sr > 1) { |
| 970 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | sr = OCSP_resp_get0(bs, 0); |
| 975 | if (!sr) { |
| 976 | memprintf(err, "Failed to get OCSP single response"); |
| 977 | goto out; |
| 978 | } |
| 979 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 980 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 981 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 982 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 983 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 984 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 985 | goto out; |
| 986 | } |
| 987 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 988 | if (!nextupd) { |
| 989 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 990 | goto out; |
| 991 | } |
| 992 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 993 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 994 | if (!rc) { |
| 995 | memprintf(err, "OCSP single response: no longer valid."); |
| 996 | goto out; |
| 997 | } |
| 998 | |
| 999 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1000 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1001 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 1002 | goto out; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (!ocsp) { |
| 1007 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 1008 | unsigned char *p; |
| 1009 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1010 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1011 | if (!rc) { |
| 1012 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
| 1016 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 1017 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 1018 | goto out; |
| 1019 | } |
| 1020 | |
| 1021 | p = key; |
| 1022 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1023 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1024 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1025 | if (!ocsp) { |
| 1026 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 1027 | goto out; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* According to comments on "chunk_dup", the |
| 1032 | previous chunk buffer will be freed */ |
| 1033 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1034 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1035 | goto out; |
| 1036 | } |
| 1037 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1038 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1039 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1040 | ret = 0; |
| 1041 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1042 | ERR_clear_error(); |
| 1043 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1044 | if (bs) |
| 1045 | OCSP_BASICRESP_free(bs); |
| 1046 | |
| 1047 | if (resp) |
| 1048 | OCSP_RESPONSE_free(resp); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | /* |
| 1053 | * External function use to update the OCSP response in the OCSP response's |
| 1054 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1055 | * to update in DER format. |
| 1056 | * |
| 1057 | * Returns 0 on success, 1 in error case. |
| 1058 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1059 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1060 | { |
| 1061 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1062 | } |
| 1063 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1064 | #endif |
| 1065 | |
| 1066 | #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] | 1067 | /* |
| 1068 | * 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] | 1069 | * path 'ocsp_path' or base64 in a buffer <buf> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1070 | * |
| 1071 | * Returns 0 on success, 1 in error case. |
| 1072 | */ |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1073 | 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] | 1074 | { |
| 1075 | int fd = -1; |
| 1076 | int r = 0; |
| 1077 | int ret = 1; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1078 | struct buffer *ocsp_response; |
| 1079 | struct buffer *src = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1080 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1081 | if (buf) { |
| 1082 | int i, j; |
| 1083 | /* if it's from a buffer it will be base64 */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1084 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1085 | /* remove \r and \n from the payload */ |
| 1086 | for (i = 0, j = 0; buf[i]; i++) { |
| 1087 | if (buf[i] == '\r' || buf[i] == '\n') |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1088 | continue; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1089 | buf[j++] = buf[i]; |
| 1090 | } |
| 1091 | buf[j] = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1092 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1093 | ret = base64dec(buf, j, trash.area, trash.size); |
| 1094 | if (ret < 0) { |
| 1095 | memprintf(err, "Error reading OCSP response in base64 format"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1096 | goto end; |
| 1097 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1098 | trash.data = ret; |
| 1099 | src = &trash; |
| 1100 | } else { |
| 1101 | fd = open(ocsp_path, O_RDONLY); |
| 1102 | if (fd == -1) { |
| 1103 | memprintf(err, "Error opening OCSP response file"); |
| 1104 | goto end; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1105 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1106 | |
| 1107 | trash.data = 0; |
| 1108 | while (trash.data < trash.size) { |
| 1109 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1110 | if (r < 0) { |
| 1111 | if (errno == EINTR) |
| 1112 | continue; |
| 1113 | |
| 1114 | memprintf(err, "Error reading OCSP response from file"); |
| 1115 | goto end; |
| 1116 | } |
| 1117 | else if (r == 0) { |
| 1118 | break; |
| 1119 | } |
| 1120 | trash.data += r; |
| 1121 | } |
| 1122 | close(fd); |
| 1123 | fd = -1; |
| 1124 | src = &trash; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1127 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 1128 | if (!chunk_dup(ocsp_response, src)) { |
| 1129 | free(ocsp_response); |
| 1130 | ocsp_response = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1131 | goto end; |
| 1132 | } |
Emmanuel Hocdet | 0667fae | 2020-01-16 14:41:36 +0100 | [diff] [blame] | 1133 | /* no error, fill ckch with new context, old context must be free */ |
| 1134 | if (ckch->ocsp_response) { |
| 1135 | free(ckch->ocsp_response->area); |
| 1136 | ckch->ocsp_response->area = NULL; |
| 1137 | free(ckch->ocsp_response); |
| 1138 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1139 | ckch->ocsp_response = ocsp_response; |
William Lallemand | e0f48ae | 2019-10-15 13:44:57 +0200 | [diff] [blame] | 1140 | ret = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1141 | end: |
| 1142 | if (fd != -1) |
| 1143 | close(fd); |
| 1144 | |
| 1145 | return ret; |
| 1146 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1147 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1148 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1149 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1150 | 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) |
| 1151 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1152 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1153 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1154 | struct connection *conn; |
| 1155 | int head; |
| 1156 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1157 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1158 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1159 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1160 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1161 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1162 | |
| 1163 | keys = ref->tlskeys; |
| 1164 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1165 | |
| 1166 | if (enc) { |
| 1167 | memcpy(key_name, keys[head].name, 16); |
| 1168 | |
| 1169 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1170 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1171 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1172 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1173 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1174 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1175 | goto end; |
| 1176 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1177 | 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] | 1178 | ret = 1; |
| 1179 | } |
| 1180 | else if (ref->key_size_bits == 256 ) { |
| 1181 | |
| 1182 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1183 | goto end; |
| 1184 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1185 | 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] | 1186 | ret = 1; |
| 1187 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1188 | } else { |
| 1189 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1190 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1191 | goto found; |
| 1192 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1193 | ret = 0; |
| 1194 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1195 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1196 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1197 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1198 | 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] | 1199 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1200 | goto end; |
| 1201 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1202 | ret = i ? 2 : 1; |
| 1203 | } |
| 1204 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1205 | 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] | 1206 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1207 | goto end; |
| 1208 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1209 | ret = i ? 2 : 1; |
| 1210 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1211 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1212 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1213 | end: |
| 1214 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1215 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1219 | { |
| 1220 | struct tls_keys_ref *ref; |
| 1221 | |
| 1222 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1223 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1224 | return ref; |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | |
| 1228 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1229 | { |
| 1230 | struct tls_keys_ref *ref; |
| 1231 | |
| 1232 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1233 | if (ref->unique_id == unique_id) |
| 1234 | return ref; |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1238 | /* Update the key into ref: if keysize doesnt |
| 1239 | * match existing ones, this function returns -1 |
| 1240 | * else it returns 0 on success. |
| 1241 | */ |
| 1242 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1243 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1244 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1245 | if (ref->key_size_bits == 128) { |
| 1246 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1247 | return -1; |
| 1248 | } |
| 1249 | else if (ref->key_size_bits == 256) { |
| 1250 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1251 | return -1; |
| 1252 | } |
| 1253 | else |
| 1254 | return -1; |
| 1255 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1256 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1257 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1258 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1259 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1260 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1261 | |
| 1262 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1265 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1266 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1267 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1268 | |
| 1269 | if(!ref) { |
| 1270 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1271 | return 1; |
| 1272 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1273 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1274 | memprintf(err, "Invalid key size"); |
| 1275 | return 1; |
| 1276 | } |
| 1277 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1278 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1279 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1280 | |
| 1281 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1282 | * automatic ids. It's called just after the basic checks. It returns |
| 1283 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1284 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1285 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1286 | { |
| 1287 | int i = 0; |
| 1288 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1289 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1290 | |
| 1291 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1292 | if (ref->unique_id == -1) { |
| 1293 | /* Look for the first free id. */ |
| 1294 | while (1) { |
| 1295 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1296 | if (ref2->unique_id == i) { |
| 1297 | i++; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | if (&ref2->list == &tlskeys_reference) |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | /* Uses the unique id and increment it for the next entry. */ |
| 1306 | ref->unique_id = i; |
| 1307 | i++; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* This sort the reference list by id. */ |
| 1312 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1313 | LIST_DEL(&ref->list); |
| 1314 | list_for_each_entry(ref3, &tkr, list) { |
| 1315 | if (ref->unique_id < ref3->unique_id) { |
| 1316 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1317 | break; |
| 1318 | } |
| 1319 | } |
| 1320 | if (&ref3->list == &tkr) |
| 1321 | LIST_ADDQ(&tkr, &ref->list); |
| 1322 | } |
| 1323 | |
| 1324 | /* swap root */ |
| 1325 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1326 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1327 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1328 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1329 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1330 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1331 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1332 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1333 | { |
| 1334 | switch (evp_keytype) { |
| 1335 | case EVP_PKEY_RSA: |
| 1336 | return 2; |
| 1337 | case EVP_PKEY_DSA: |
| 1338 | return 0; |
| 1339 | case EVP_PKEY_EC: |
| 1340 | return 1; |
| 1341 | } |
| 1342 | |
| 1343 | return -1; |
| 1344 | } |
| 1345 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1346 | /* |
| 1347 | * Callback used to set OCSP status extension content in server hello. |
| 1348 | */ |
| 1349 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1350 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1351 | struct certificate_ocsp *ocsp; |
| 1352 | struct ocsp_cbk_arg *ocsp_arg; |
| 1353 | char *ssl_buf; |
| 1354 | EVP_PKEY *ssl_pkey; |
| 1355 | int key_type; |
| 1356 | int index; |
| 1357 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1358 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1359 | |
| 1360 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1361 | if (!ssl_pkey) |
| 1362 | return SSL_TLSEXT_ERR_NOACK; |
| 1363 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1364 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1365 | |
| 1366 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1367 | ocsp = ocsp_arg->s_ocsp; |
| 1368 | else { |
| 1369 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1370 | * the certificate type |
| 1371 | */ |
| 1372 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1373 | |
| 1374 | if (index < 0) |
| 1375 | return SSL_TLSEXT_ERR_NOACK; |
| 1376 | |
| 1377 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1378 | |
| 1379 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1380 | |
| 1381 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1382 | !ocsp->response.area || |
| 1383 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1384 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1385 | return SSL_TLSEXT_ERR_NOACK; |
| 1386 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1387 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1388 | if (!ssl_buf) |
| 1389 | return SSL_TLSEXT_ERR_NOACK; |
| 1390 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1391 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1392 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1393 | |
| 1394 | return SSL_TLSEXT_ERR_OK; |
| 1395 | } |
| 1396 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1397 | #endif |
| 1398 | |
| 1399 | #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] | 1400 | /* |
| 1401 | * 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] | 1402 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1403 | * status extension, the issuer's certificate is mandatory. It should be |
| 1404 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1405 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1406 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1407 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1408 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1409 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1410 | * |
| 1411 | * 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] | 1412 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1413 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1414 | #ifndef OPENSSL_IS_BORINGSSL |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1415 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1416 | { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1417 | X509 *x = NULL, *issuer = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1418 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1419 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1420 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1421 | char *warn = NULL; |
| 1422 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1423 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1424 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1425 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1426 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1427 | if (!x) |
| 1428 | goto out; |
| 1429 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1430 | issuer = ckch->ocsp_issuer; |
| 1431 | if (!issuer) |
| 1432 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1433 | |
| 1434 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1435 | if (!cid) |
| 1436 | goto out; |
| 1437 | |
| 1438 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1439 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1440 | goto out; |
| 1441 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1442 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1443 | if (!ocsp) |
| 1444 | goto out; |
| 1445 | |
| 1446 | p = ocsp->key_data; |
| 1447 | i2d_OCSP_CERTID(cid, &p); |
| 1448 | |
| 1449 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1450 | if (iocsp == ocsp) |
| 1451 | ocsp = NULL; |
| 1452 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1453 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1454 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1455 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1456 | #endif |
| 1457 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1458 | |
| 1459 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1460 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1461 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1462 | |
| 1463 | cb_arg->is_single = 1; |
| 1464 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1465 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1466 | pkey = X509_get_pubkey(x); |
| 1467 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1468 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1469 | |
| 1470 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1471 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1472 | } else { |
| 1473 | /* |
| 1474 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1475 | * Update that cb_arg with the new cert's staple |
| 1476 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1477 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1478 | struct certificate_ocsp *tmp_ocsp; |
| 1479 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1480 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1481 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1482 | |
| 1483 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1484 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1485 | #else |
| 1486 | cb_arg = ctx->tlsext_status_arg; |
| 1487 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1488 | |
| 1489 | /* |
| 1490 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1491 | * the order of operations below matter, take care when changing it |
| 1492 | */ |
| 1493 | tmp_ocsp = cb_arg->s_ocsp; |
| 1494 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1495 | cb_arg->s_ocsp = NULL; |
| 1496 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1497 | cb_arg->is_single = 0; |
| 1498 | cb_arg->single_kt = 0; |
| 1499 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1500 | pkey = X509_get_pubkey(x); |
| 1501 | key_type = EVP_PKEY_base_id(pkey); |
| 1502 | EVP_PKEY_free(pkey); |
| 1503 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1504 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1505 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1506 | cb_arg->m_ocsp[index] = iocsp; |
| 1507 | |
| 1508 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1509 | |
| 1510 | ret = 0; |
| 1511 | |
| 1512 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1513 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1514 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1515 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1519 | if (cid) |
| 1520 | OCSP_CERTID_free(cid); |
| 1521 | |
| 1522 | if (ocsp) |
| 1523 | free(ocsp); |
| 1524 | |
| 1525 | if (warn) |
| 1526 | free(warn); |
| 1527 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1528 | return ret; |
| 1529 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1530 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1531 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1532 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1533 | 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] | 1534 | } |
| 1535 | #endif |
| 1536 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1537 | #endif |
| 1538 | |
| 1539 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1540 | #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] | 1541 | |
| 1542 | #define CT_EXTENSION_TYPE 18 |
| 1543 | |
| 1544 | static int sctl_ex_index = -1; |
| 1545 | |
| 1546 | /* |
| 1547 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1548 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1549 | * is performed. |
| 1550 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1551 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1552 | { |
| 1553 | int ret = 1; |
| 1554 | int len, pos, sct_len; |
| 1555 | unsigned char *data; |
| 1556 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1557 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1558 | goto out; |
| 1559 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1560 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1561 | len = (data[0] << 8) | data[1]; |
| 1562 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1563 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1564 | goto out; |
| 1565 | |
| 1566 | data = data + 2; |
| 1567 | pos = 0; |
| 1568 | while (pos < len) { |
| 1569 | if (len - pos < 2) |
| 1570 | goto out; |
| 1571 | |
| 1572 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1573 | if (pos + sct_len + 2 > len) |
| 1574 | goto out; |
| 1575 | |
| 1576 | pos += sct_len + 2; |
| 1577 | } |
| 1578 | |
| 1579 | ret = 0; |
| 1580 | |
| 1581 | out: |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1585 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 1586 | * It fills the ckch->sctl buffer |
| 1587 | * return 0 on success or != 0 on failure */ |
| 1588 | 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] | 1589 | { |
| 1590 | int fd = -1; |
| 1591 | int r = 0; |
| 1592 | int ret = 1; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1593 | struct buffer tmp; |
| 1594 | struct buffer *src; |
| 1595 | struct buffer *sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1596 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1597 | if (buf) { |
| 1598 | tmp.area = buf; |
| 1599 | tmp.data = strlen(buf); |
| 1600 | tmp.size = tmp.data + 1; |
| 1601 | src = &tmp; |
| 1602 | } else { |
| 1603 | fd = open(sctl_path, O_RDONLY); |
| 1604 | if (fd == -1) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1605 | goto end; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1606 | |
| 1607 | trash.data = 0; |
| 1608 | while (trash.data < trash.size) { |
| 1609 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1610 | if (r < 0) { |
| 1611 | if (errno == EINTR) |
| 1612 | continue; |
| 1613 | goto end; |
| 1614 | } |
| 1615 | else if (r == 0) { |
| 1616 | break; |
| 1617 | } |
| 1618 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1619 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1620 | src = &trash; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1621 | } |
| 1622 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1623 | ret = ssl_sock_parse_sctl(src); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1624 | if (ret) |
| 1625 | goto end; |
| 1626 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1627 | sctl = calloc(1, sizeof(*sctl)); |
| 1628 | if (!chunk_dup(sctl, src)) { |
| 1629 | free(sctl); |
| 1630 | sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1631 | goto end; |
| 1632 | } |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1633 | /* no error, fill ckch with new context, old context must be free */ |
| 1634 | if (ckch->sctl) { |
| 1635 | free(ckch->sctl->area); |
| 1636 | ckch->sctl->area = NULL; |
| 1637 | free(ckch->sctl); |
| 1638 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1639 | ckch->sctl = sctl; |
Emmanuel Hocdet | 224a087 | 2020-01-16 15:15:49 +0100 | [diff] [blame] | 1640 | ret = 0; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1641 | end: |
| 1642 | if (fd != -1) |
| 1643 | close(fd); |
| 1644 | |
| 1645 | return ret; |
| 1646 | } |
| 1647 | |
| 1648 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1649 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1650 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1652 | *out = (unsigned char *) sctl->area; |
| 1653 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1654 | |
| 1655 | return 1; |
| 1656 | } |
| 1657 | |
| 1658 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1659 | { |
| 1660 | return 1; |
| 1661 | } |
| 1662 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1663 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1664 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1665 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1666 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1667 | 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] | 1668 | goto out; |
| 1669 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1670 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1671 | |
| 1672 | ret = 0; |
| 1673 | |
| 1674 | out: |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | #endif |
| 1679 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1680 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1681 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1682 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1683 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1684 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1685 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1686 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1687 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1688 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1689 | * change this to #if and do not assign a default value to this macro! |
| 1690 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1691 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1692 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1693 | 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] | 1694 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1695 | conn->err_code = CO_ER_SSL_RENEG; |
| 1696 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1697 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1698 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1699 | |
| 1700 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1701 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1702 | /* Long certificate chains optimz |
| 1703 | If write and read bios are differents, we |
| 1704 | consider that the buffering was activated, |
| 1705 | so we rise the output buffer size from 4k |
| 1706 | to 16k */ |
| 1707 | write_bio = SSL_get_wbio(ssl); |
| 1708 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1709 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1710 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1716 | /* Callback is called for each certificate of the chain during a verify |
| 1717 | ok is set to 1 if preverify detect no error on current certificate. |
| 1718 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1719 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1720 | { |
| 1721 | SSL *ssl; |
| 1722 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1723 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1724 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1725 | |
| 1726 | 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] | 1727 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1728 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1729 | ctx = conn->xprt_ctx; |
| 1730 | |
| 1731 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1732 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1733 | if (ok) /* no errors */ |
| 1734 | return ok; |
| 1735 | |
| 1736 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1737 | err = X509_STORE_CTX_get_error(x_store); |
| 1738 | |
| 1739 | /* check if CA error needs to be ignored */ |
| 1740 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1741 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1742 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1743 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1744 | } |
| 1745 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1746 | 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] | 1747 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1748 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1749 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1750 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1751 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1752 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1753 | return 0; |
| 1754 | } |
| 1755 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1756 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1757 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1758 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1759 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1760 | 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] | 1761 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1762 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1763 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1764 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1766 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1767 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1768 | } |
| 1769 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1770 | static inline |
| 1771 | 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] | 1772 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1773 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1774 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1775 | unsigned char *msg; |
| 1776 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1777 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* This function is called for "from client" and "to server" |
| 1780 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1781 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1782 | */ |
| 1783 | |
| 1784 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1785 | * otherwise it is set to 1. |
| 1786 | */ |
| 1787 | if (write_p != 0) |
| 1788 | return; |
| 1789 | |
| 1790 | /* content_type contains the type of message received or sent |
| 1791 | * according with the SSL/TLS protocol spec. This message is |
| 1792 | * encoded with one byte. The value 256 (two bytes) is used |
| 1793 | * for designing the SSL/TLS record layer. According with the |
| 1794 | * rfc6101, the expected message (other than 256) are: |
| 1795 | * - change_cipher_spec(20) |
| 1796 | * - alert(21) |
| 1797 | * - handshake(22) |
| 1798 | * - application_data(23) |
| 1799 | * - (255) |
| 1800 | * We are interessed by the handshake and specially the client |
| 1801 | * hello. |
| 1802 | */ |
| 1803 | if (content_type != 22) |
| 1804 | return; |
| 1805 | |
| 1806 | /* The message length is at least 4 bytes, containing the |
| 1807 | * message type and the message length. |
| 1808 | */ |
| 1809 | if (len < 4) |
| 1810 | return; |
| 1811 | |
| 1812 | /* First byte of the handshake message id the type of |
| 1813 | * message. The konwn types are: |
| 1814 | * - hello_request(0) |
| 1815 | * - client_hello(1) |
| 1816 | * - server_hello(2) |
| 1817 | * - certificate(11) |
| 1818 | * - server_key_exchange (12) |
| 1819 | * - certificate_request(13) |
| 1820 | * - server_hello_done(14) |
| 1821 | * We are interested by the client hello. |
| 1822 | */ |
| 1823 | msg = (unsigned char *)buf; |
| 1824 | if (msg[0] != 1) |
| 1825 | return; |
| 1826 | |
| 1827 | /* Next three bytes are the length of the message. The total length |
| 1828 | * must be this decoded length + 4. If the length given as argument |
| 1829 | * is not the same, we abort the protocol dissector. |
| 1830 | */ |
| 1831 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1832 | if (len < rec_len + 4) |
| 1833 | return; |
| 1834 | msg += 4; |
| 1835 | end = msg + rec_len; |
| 1836 | if (end < msg) |
| 1837 | return; |
| 1838 | |
| 1839 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1840 | * 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] | 1841 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1842 | */ |
| 1843 | msg += 1 + 1 + 4 + 28; |
| 1844 | if (msg > end) |
| 1845 | return; |
| 1846 | |
| 1847 | /* Next, is session id: |
| 1848 | * if present, we have to jump by length + 1 for the size information |
| 1849 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1850 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1851 | if (msg[0] > 0) |
| 1852 | msg += msg[0]; |
| 1853 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1854 | if (msg > end) |
| 1855 | return; |
| 1856 | |
| 1857 | /* Next two bytes are the ciphersuite length. */ |
| 1858 | if (msg + 2 > end) |
| 1859 | return; |
| 1860 | rec_len = (msg[0] << 8) + msg[1]; |
| 1861 | msg += 2; |
| 1862 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1863 | return; |
| 1864 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1865 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1866 | if (!capture) |
| 1867 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1868 | /* Compute the xxh64 of the ciphersuite. */ |
| 1869 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1870 | |
| 1871 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1872 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1873 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1874 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1875 | |
| 1876 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1879 | /* Callback is called for ssl protocol analyse */ |
| 1880 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1881 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1882 | #ifdef TLS1_RT_HEARTBEAT |
| 1883 | /* test heartbeat received (write_p is set to 0 |
| 1884 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1885 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1886 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1887 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1888 | const unsigned char *p = buf; |
| 1889 | unsigned int payload; |
| 1890 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1891 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1892 | |
| 1893 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1894 | if (*p != TLS1_HB_REQUEST) |
| 1895 | return; |
| 1896 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1897 | 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] | 1898 | goto kill_it; |
| 1899 | |
| 1900 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1901 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1902 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1903 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1904 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1905 | * advertised payload is larger than the advertised packet |
| 1906 | * length, so we have garbage in the buffer between the |
| 1907 | * payload and the end of the buffer (p+len). We can't know |
| 1908 | * if the SSL stack is patched, and we don't know if we can |
| 1909 | * safely wipe out the area between p+3+len and payload. |
| 1910 | * So instead, we prevent the response from being sent by |
| 1911 | * setting the max_send_fragment to 0 and we report an SSL |
| 1912 | * error, which will kill this connection. It will be reported |
| 1913 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1914 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1915 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1916 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1917 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1918 | return; |
| 1919 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1920 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1921 | if (global_ssl.capture_cipherlist > 0) |
| 1922 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1925 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1926 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1927 | const unsigned char *in, unsigned int inlen, |
| 1928 | void *arg) |
| 1929 | { |
| 1930 | struct server *srv = arg; |
| 1931 | |
| 1932 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1933 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1934 | return SSL_TLSEXT_ERR_OK; |
| 1935 | return SSL_TLSEXT_ERR_NOACK; |
| 1936 | } |
| 1937 | #endif |
| 1938 | |
| 1939 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1940 | /* This callback is used so that the server advertises the list of |
| 1941 | * negociable protocols for NPN. |
| 1942 | */ |
| 1943 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1944 | unsigned int *len, void *arg) |
| 1945 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1946 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1947 | |
| 1948 | *data = (const unsigned char *)conf->npn_str; |
| 1949 | *len = conf->npn_len; |
| 1950 | return SSL_TLSEXT_ERR_OK; |
| 1951 | } |
| 1952 | #endif |
| 1953 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1954 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1955 | /* This callback is used so that the server advertises the list of |
| 1956 | * negociable protocols for ALPN. |
| 1957 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1958 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1959 | unsigned char *outlen, |
| 1960 | const unsigned char *server, |
| 1961 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1962 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1963 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1964 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1965 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1966 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1967 | return SSL_TLSEXT_ERR_NOACK; |
| 1968 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1969 | return SSL_TLSEXT_ERR_OK; |
| 1970 | } |
| 1971 | #endif |
| 1972 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1973 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1974 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1975 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1976 | /* Create a X509 certificate with the specified servername and serial. This |
| 1977 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1978 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1979 | 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] | 1980 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1981 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1982 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1983 | SSL_CTX *ssl_ctx = NULL; |
| 1984 | X509 *newcrt = NULL; |
| 1985 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1986 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1987 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1988 | X509_NAME *name; |
| 1989 | const EVP_MD *digest; |
| 1990 | X509V3_CTX ctx; |
| 1991 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1992 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1993 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1994 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1995 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1996 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1997 | #else |
| 1998 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1999 | if (tmp_ssl) |
| 2000 | pkey = SSL_get_privatekey(tmp_ssl); |
| 2001 | #endif |
| 2002 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2003 | goto mkcert_error; |
| 2004 | |
| 2005 | /* Create the certificate */ |
| 2006 | if (!(newcrt = X509_new())) |
| 2007 | goto mkcert_error; |
| 2008 | |
| 2009 | /* Set version number for the certificate (X509v3) and the serial |
| 2010 | * number */ |
| 2011 | if (X509_set_version(newcrt, 2L) != 1) |
| 2012 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 2013 | 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] | 2014 | |
| 2015 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 2016 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 2017 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2018 | goto mkcert_error; |
| 2019 | |
| 2020 | /* set public key in the certificate */ |
| 2021 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 2022 | goto mkcert_error; |
| 2023 | |
| 2024 | /* Set issuer name from the CA */ |
| 2025 | if (!(name = X509_get_subject_name(cacert))) |
| 2026 | goto mkcert_error; |
| 2027 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 2028 | goto mkcert_error; |
| 2029 | |
| 2030 | /* Set the subject name using the same, but the CN */ |
| 2031 | name = X509_NAME_dup(name); |
| 2032 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 2033 | (const unsigned char *)servername, |
| 2034 | -1, -1, 0) != 1) { |
| 2035 | X509_NAME_free(name); |
| 2036 | goto mkcert_error; |
| 2037 | } |
| 2038 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 2039 | X509_NAME_free(name); |
| 2040 | goto mkcert_error; |
| 2041 | } |
| 2042 | X509_NAME_free(name); |
| 2043 | |
| 2044 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2045 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2046 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 2047 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 2048 | X509_EXTENSION *ext; |
| 2049 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2050 | 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] | 2051 | goto mkcert_error; |
| 2052 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2053 | X509_EXTENSION_free(ext); |
| 2054 | goto mkcert_error; |
| 2055 | } |
| 2056 | X509_EXTENSION_free(ext); |
| 2057 | } |
| 2058 | |
| 2059 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2060 | |
| 2061 | key_type = EVP_PKEY_base_id(capkey); |
| 2062 | |
| 2063 | if (key_type == EVP_PKEY_DSA) |
| 2064 | digest = EVP_sha1(); |
| 2065 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2066 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2067 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2068 | digest = EVP_sha256(); |
| 2069 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2070 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2071 | int nid; |
| 2072 | |
| 2073 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2074 | goto mkcert_error; |
| 2075 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2076 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2077 | #else |
| 2078 | goto mkcert_error; |
| 2079 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2080 | } |
| 2081 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2082 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2083 | goto mkcert_error; |
| 2084 | |
| 2085 | /* Create and set the new SSL_CTX */ |
| 2086 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2087 | goto mkcert_error; |
| 2088 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2089 | goto mkcert_error; |
| 2090 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2091 | goto mkcert_error; |
| 2092 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2093 | goto mkcert_error; |
| 2094 | |
| 2095 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2096 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2097 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2098 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2099 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2100 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2101 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2102 | 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] | 2103 | EC_KEY *ecc; |
| 2104 | int nid; |
| 2105 | |
| 2106 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2107 | goto end; |
| 2108 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2109 | goto end; |
| 2110 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2111 | EC_KEY_free(ecc); |
| 2112 | } |
| 2113 | #endif |
| 2114 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2115 | return ssl_ctx; |
| 2116 | |
| 2117 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2118 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2119 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2120 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2121 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2122 | return NULL; |
| 2123 | } |
| 2124 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2125 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2126 | 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] | 2127 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2128 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2129 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2130 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2131 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2132 | } |
| 2133 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2134 | /* 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] | 2135 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2136 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2137 | 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] | 2138 | { |
| 2139 | struct lru64 *lru = NULL; |
| 2140 | |
| 2141 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2142 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2143 | 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] | 2144 | if (lru && lru->domain) { |
| 2145 | if (ssl) |
| 2146 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2147 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2148 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2149 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2150 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2151 | } |
| 2152 | return NULL; |
| 2153 | } |
| 2154 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2155 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2156 | * function is not thread-safe, it should only be used to check if a certificate |
| 2157 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2158 | * thread). It is kept for backward compatibility. */ |
| 2159 | SSL_CTX * |
| 2160 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2161 | { |
| 2162 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2163 | } |
| 2164 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2165 | /* Set a certificate int the LRU cache used to store generated |
| 2166 | * certificate. Return 0 on success, otherwise -1 */ |
| 2167 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2168 | 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] | 2169 | { |
| 2170 | struct lru64 *lru = NULL; |
| 2171 | |
| 2172 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2173 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2174 | 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] | 2175 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2176 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2177 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2178 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2179 | if (lru->domain && lru->data) |
| 2180 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2181 | 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] | 2182 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2183 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2184 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2185 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2188 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2189 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2190 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2191 | { |
| 2192 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2193 | } |
| 2194 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2195 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2196 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2197 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2198 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2199 | 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] | 2200 | { |
| 2201 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2202 | SSL_CTX *ssl_ctx = NULL; |
| 2203 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2204 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2205 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2206 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2207 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2208 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2209 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2210 | if (lru && lru->domain) |
| 2211 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2212 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2213 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2214 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2215 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2216 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2217 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2218 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2219 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2220 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2221 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2222 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2223 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2224 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2225 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2226 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2227 | return 0; |
| 2228 | } |
| 2229 | static int |
| 2230 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2231 | { |
| 2232 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2233 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2234 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2235 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2236 | 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] | 2237 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2238 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2239 | } |
| 2240 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2241 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2242 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2243 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2244 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2245 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2246 | |
| 2247 | 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] | 2248 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2249 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2250 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2251 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2252 | #endif |
| 2253 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2254 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2255 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2256 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2257 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2258 | 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] | 2259 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2260 | 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] | 2261 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2262 | #endif |
| 2263 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2264 | 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] | 2265 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2266 | 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] | 2267 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2268 | #endif |
| 2269 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2270 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2271 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2272 | /* Unusable in this context. */ |
| 2273 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2274 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2275 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2276 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2277 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2278 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2279 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2280 | |
| 2281 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2282 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2283 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2284 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2285 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2286 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2287 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2288 | } |
| 2289 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2290 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2291 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2292 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2293 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2294 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2295 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2296 | } |
| 2297 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2298 | 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] | 2299 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2300 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2301 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2302 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2303 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2304 | } |
| 2305 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2306 | 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] | 2307 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2308 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2309 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2310 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2311 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2312 | } |
| 2313 | 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] | 2314 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2315 | 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] | 2316 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2317 | #endif |
| 2318 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2319 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2320 | #if SSL_OP_NO_TLSv1_3 |
| 2321 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2322 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2323 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2324 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2325 | #endif |
| 2326 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2327 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2328 | |
| 2329 | static struct { |
| 2330 | int option; |
| 2331 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2332 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2333 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2334 | const char *name; |
| 2335 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2336 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2337 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2338 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2339 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2340 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2341 | {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] | 2342 | }; |
| 2343 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2344 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2345 | { |
| 2346 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2347 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2348 | SSL_set_SSL_CTX(ssl, ctx); |
| 2349 | } |
| 2350 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2351 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2352 | |
| 2353 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2354 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2355 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2357 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2358 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2359 | return SSL_TLSEXT_ERR_OK; |
| 2360 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2363 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2364 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2365 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2366 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2367 | #else |
| 2368 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2369 | { |
| 2370 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2371 | struct connection *conn; |
| 2372 | struct bind_conf *s; |
| 2373 | const uint8_t *extension_data; |
| 2374 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2375 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2376 | |
| 2377 | char *wildp = NULL; |
| 2378 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2379 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2380 | 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] | 2381 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2382 | int i; |
| 2383 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2384 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2385 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2386 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2387 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2388 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2389 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2391 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2392 | #else |
| 2393 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2394 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2395 | /* |
| 2396 | * The server_name extension was given too much extensibility when it |
| 2397 | * was written, so parsing the normal case is a bit complex. |
| 2398 | */ |
| 2399 | size_t len; |
| 2400 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2401 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2402 | /* Extract the length of the supplied list of names. */ |
| 2403 | len = (*extension_data++) << 8; |
| 2404 | len |= *extension_data++; |
| 2405 | if (len + 2 != extension_len) |
| 2406 | goto abort; |
| 2407 | /* |
| 2408 | * The list in practice only has a single element, so we only consider |
| 2409 | * the first one. |
| 2410 | */ |
| 2411 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2412 | goto abort; |
| 2413 | extension_len = len - 1; |
| 2414 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2415 | if (extension_len <= 2) |
| 2416 | goto abort; |
| 2417 | len = (*extension_data++) << 8; |
| 2418 | len |= *extension_data++; |
| 2419 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2420 | || memchr(extension_data, 0, len) != NULL) |
| 2421 | goto abort; |
| 2422 | servername = extension_data; |
| 2423 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2424 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2425 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2426 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2427 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2428 | } |
| 2429 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2430 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2431 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2432 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2433 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2434 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2435 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2436 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2437 | goto abort; |
| 2438 | } |
| 2439 | |
| 2440 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2441 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2442 | 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] | 2443 | #else |
| 2444 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2445 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2446 | uint8_t sign; |
| 2447 | size_t len; |
| 2448 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2449 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2450 | len = (*extension_data++) << 8; |
| 2451 | len |= *extension_data++; |
| 2452 | if (len + 2 != extension_len) |
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 | if (len % 2 != 0) |
| 2455 | goto abort; |
| 2456 | for (; len > 0; len -= 2) { |
| 2457 | extension_data++; /* hash */ |
| 2458 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2459 | switch (sign) { |
| 2460 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2461 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2462 | break; |
| 2463 | case TLSEXT_signature_ecdsa: |
| 2464 | has_ecdsa_sig = 1; |
| 2465 | break; |
| 2466 | default: |
| 2467 | continue; |
| 2468 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2469 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2470 | break; |
| 2471 | } |
| 2472 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2473 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2474 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2475 | } |
| 2476 | 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] | 2477 | const SSL_CIPHER *cipher; |
| 2478 | size_t len; |
| 2479 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2480 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2481 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2482 | len = ctx->cipher_suites_len; |
| 2483 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2484 | #else |
| 2485 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2486 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2487 | if (len % 2 != 0) |
| 2488 | goto abort; |
| 2489 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2490 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2491 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2492 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2493 | #else |
| 2494 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2495 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2496 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2497 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2498 | break; |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2503 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2504 | trash.area[i] = tolower(servername[i]); |
| 2505 | if (!wildp && (trash.area[i] == '.')) |
| 2506 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2507 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2508 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2509 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2510 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2511 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2512 | for (i = 0; i < 2; i++) { |
| 2513 | if (i == 0) /* lookup in full qualified names */ |
| 2514 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2515 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2516 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2517 | else |
| 2518 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2519 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2520 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2521 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2522 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2523 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2524 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2525 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2526 | break; |
| 2527 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2528 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2530 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2531 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2532 | if (!node_anonymous) |
| 2533 | node_anonymous = n; |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
| 2537 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2538 | /* select by key_signature priority order */ |
| 2539 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2540 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2541 | : (node_anonymous ? node_anonymous |
| 2542 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2543 | : node_rsa /* no rsa signature case (far far away) */ |
| 2544 | ))); |
| 2545 | if (node) { |
| 2546 | /* switch ctx */ |
| 2547 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2548 | 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] | 2549 | if (conf) { |
| 2550 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2551 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2552 | if (conf->early_data) |
| 2553 | allow_early = 1; |
| 2554 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2555 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2556 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2557 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2558 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2559 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2560 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2561 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2562 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2563 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2564 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2565 | } |
| 2566 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2567 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2568 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2569 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2570 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2571 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2572 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2573 | allow_early: |
| 2574 | #ifdef OPENSSL_IS_BORINGSSL |
| 2575 | if (allow_early) |
| 2576 | SSL_set_early_data_enabled(ssl, 1); |
| 2577 | #else |
| 2578 | if (!allow_early) |
| 2579 | SSL_set_max_early_data(ssl, 0); |
| 2580 | #endif |
| 2581 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2582 | abort: |
| 2583 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2584 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2585 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2586 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2587 | #else |
| 2588 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2589 | return 0; |
| 2590 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2594 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2595 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2596 | * warning when no match is found, which implies the default (first) cert |
| 2597 | * will keep being used. |
| 2598 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2599 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2600 | { |
| 2601 | const char *servername; |
| 2602 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2603 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2604 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2605 | int i; |
| 2606 | (void)al; /* shut gcc stupid warning */ |
| 2607 | |
| 2608 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2609 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2610 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2611 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2612 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2613 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2614 | if (s->strict_sni) |
| 2615 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2616 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2617 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2618 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2619 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2620 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2621 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2622 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2623 | if (!servername[i]) |
| 2624 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2625 | trash.area[i] = tolower(servername[i]); |
| 2626 | if (!wildp && (trash.area[i] == '.')) |
| 2627 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2628 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2629 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2630 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2631 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2632 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2633 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2634 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2635 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2636 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2637 | node = n; |
| 2638 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2639 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2640 | } |
| 2641 | if (!node && wildp) { |
| 2642 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2643 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2644 | /* lookup a not neg filter */ |
| 2645 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2646 | node = n; |
| 2647 | break; |
| 2648 | } |
| 2649 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2650 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2651 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2652 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2653 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2654 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2655 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2656 | return SSL_TLSEXT_ERR_OK; |
| 2657 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2658 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2659 | if (s->strict_sni) { |
| 2660 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2661 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2662 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2663 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 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_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2669 | 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] | 2670 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2671 | return SSL_TLSEXT_ERR_OK; |
| 2672 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2673 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2674 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2675 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2676 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2677 | |
| 2678 | static DH * ssl_get_dh_1024(void) |
| 2679 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2680 | static unsigned char dh1024_p[]={ |
| 2681 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2682 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2683 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2684 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2685 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2686 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2687 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2688 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2689 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2690 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2691 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2692 | }; |
| 2693 | static unsigned char dh1024_g[]={ |
| 2694 | 0x02, |
| 2695 | }; |
| 2696 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2697 | BIGNUM *p; |
| 2698 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2699 | DH *dh = DH_new(); |
| 2700 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2701 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2702 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2703 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2704 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2705 | DH_free(dh); |
| 2706 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2707 | } else { |
| 2708 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | return dh; |
| 2712 | } |
| 2713 | |
| 2714 | static DH *ssl_get_dh_2048(void) |
| 2715 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2716 | static unsigned char dh2048_p[]={ |
| 2717 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2718 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2719 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2720 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2721 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2722 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2723 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2724 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2725 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2726 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2727 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2728 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2729 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2730 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2731 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2732 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2733 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2734 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2735 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2736 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2737 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2738 | 0xB7,0x1F,0x77,0xF3, |
| 2739 | }; |
| 2740 | static unsigned char dh2048_g[]={ |
| 2741 | 0x02, |
| 2742 | }; |
| 2743 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2744 | BIGNUM *p; |
| 2745 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2746 | DH *dh = DH_new(); |
| 2747 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2748 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2749 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2750 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2751 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2752 | DH_free(dh); |
| 2753 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2754 | } else { |
| 2755 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | return dh; |
| 2759 | } |
| 2760 | |
| 2761 | static DH *ssl_get_dh_4096(void) |
| 2762 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2763 | static unsigned char dh4096_p[]={ |
| 2764 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2765 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2766 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2767 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2768 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2769 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2770 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2771 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2772 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2773 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2774 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2775 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2776 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2777 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2778 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2779 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2780 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2781 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2782 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2783 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2784 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2785 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2786 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2787 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2788 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2789 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2790 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2791 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2792 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2793 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2794 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2795 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2796 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2797 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2798 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2799 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2800 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2801 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2802 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2803 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2804 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2805 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2806 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2807 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2808 | static unsigned char dh4096_g[]={ |
| 2809 | 0x02, |
| 2810 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2811 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2812 | BIGNUM *p; |
| 2813 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2814 | DH *dh = DH_new(); |
| 2815 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2816 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2817 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2818 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2819 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2820 | DH_free(dh); |
| 2821 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2822 | } else { |
| 2823 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | return dh; |
| 2827 | } |
| 2828 | |
| 2829 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2830 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2831 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2832 | { |
| 2833 | DH *dh = NULL; |
| 2834 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2835 | int type; |
| 2836 | |
| 2837 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2838 | |
| 2839 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2840 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2841 | */ |
| 2842 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2843 | keylen = EVP_PKEY_bits(pkey); |
| 2844 | } |
| 2845 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2846 | if (keylen > global_ssl.default_dh_param) { |
| 2847 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2850 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2851 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2852 | } |
| 2853 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2854 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2855 | } |
| 2856 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2857 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | return dh; |
| 2861 | } |
| 2862 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2863 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2864 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2865 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2866 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2867 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2868 | if (in == NULL) |
| 2869 | goto end; |
| 2870 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2871 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2872 | goto end; |
| 2873 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2874 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2875 | |
| 2876 | end: |
| 2877 | if (in) |
| 2878 | BIO_free(in); |
| 2879 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2880 | ERR_clear_error(); |
| 2881 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2882 | return dh; |
| 2883 | } |
| 2884 | |
| 2885 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2886 | { |
| 2887 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2888 | |
| 2889 | if (global_dh) { |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | return -1; |
| 2894 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2895 | #endif |
| 2896 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2897 | /* Alloc and init a ckch_inst */ |
| 2898 | static struct ckch_inst *ckch_inst_new() |
| 2899 | { |
| 2900 | struct ckch_inst *ckch_inst; |
| 2901 | |
| 2902 | ckch_inst = calloc(1, sizeof *ckch_inst); |
| 2903 | if (ckch_inst) |
| 2904 | LIST_INIT(&ckch_inst->sni_ctx); |
| 2905 | |
| 2906 | return ckch_inst; |
| 2907 | } |
| 2908 | |
| 2909 | |
| 2910 | /* 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] | 2911 | 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] | 2912 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2913 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2914 | { |
| 2915 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2916 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2917 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2918 | if (*name == '!') { |
| 2919 | neg = 1; |
| 2920 | name++; |
| 2921 | } |
| 2922 | if (*name == '*') { |
| 2923 | wild = 1; |
| 2924 | name++; |
| 2925 | } |
| 2926 | /* !* filter is a nop */ |
| 2927 | if (neg && wild) |
| 2928 | return order; |
| 2929 | if (*name) { |
| 2930 | int j, len; |
| 2931 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2932 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2933 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2934 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2935 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2936 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2937 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2938 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2939 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2940 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2941 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2942 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2943 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2944 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2945 | sc->order = order++; |
| 2946 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2947 | sc->wild = wild; |
| 2948 | sc->name.node.leaf_p = NULL; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2949 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2950 | } |
| 2951 | return order; |
| 2952 | } |
| 2953 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2954 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2955 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2956 | * This function can't return an error. |
| 2957 | * |
| 2958 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2959 | */ |
| 2960 | static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
| 2961 | { |
| 2962 | |
| 2963 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2964 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2965 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2966 | |
| 2967 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2968 | |
| 2969 | /* ignore if sc0 was already inserted in a tree */ |
| 2970 | if (sc0->name.node.leaf_p) |
| 2971 | continue; |
| 2972 | |
| 2973 | /* Check for duplicates. */ |
| 2974 | if (sc0->wild) |
| 2975 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2976 | else |
| 2977 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2978 | |
| 2979 | for (; node; node = ebmb_next_dup(node)) { |
| 2980 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2981 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2982 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2983 | /* it's a duplicate, we should remove and free it */ |
| 2984 | LIST_DEL(&sc0->by_ckch_inst); |
| 2985 | free(sc0); |
| 2986 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2987 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | /* if duplicate, ignore the insertion */ |
| 2992 | if (!sc0) |
| 2993 | continue; |
| 2994 | |
| 2995 | if (sc0->wild) |
| 2996 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2997 | else |
| 2998 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2999 | |
| 3000 | /* replace the default_ctx if required with the first ctx */ |
| 3001 | if (ckch_inst->is_default && !def) { |
| 3002 | /* we don't need to free the default_ctx because the refcount was not incremented */ |
| 3003 | bind_conf->default_ctx = sc0->ctx; |
| 3004 | def = 1; |
| 3005 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3010 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3011 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3012 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3013 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3014 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3015 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
| 3016 | * If there is no DH paramater availaible in the ckchs, the global |
| 3017 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3018 | * DH parameter available in ckchs nor in global, the default |
| 3019 | * DH parameters are applied on the SSL_CTX. |
| 3020 | * Returns a bitfield containing the flags: |
| 3021 | * ERR_FATAL in any fatal error case |
| 3022 | * ERR_ALERT if a reason of the error is availabine in err |
| 3023 | * ERR_WARN if a warning is available into err |
| 3024 | * The value 0 means there is no error nor warning and |
| 3025 | * the operation succeed. |
| 3026 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3027 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3028 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 3029 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3030 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3031 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3032 | DH *dh = NULL; |
| 3033 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 3034 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3035 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3036 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 3037 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3038 | err && *err ? *err : "", path); |
| 3039 | #if defined(SSL_CTX_set_dh_auto) |
| 3040 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3041 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3042 | err && *err ? *err : ""); |
| 3043 | #else |
| 3044 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3045 | err && *err ? *err : ""); |
| 3046 | #endif |
| 3047 | ret |= ERR_WARN; |
| 3048 | goto end; |
| 3049 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3050 | |
| 3051 | if (ssl_dh_ptr_index >= 0) { |
| 3052 | /* store a pointer to the DH params to avoid complaining about |
| 3053 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3054 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3055 | } |
| 3056 | } |
| 3057 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3058 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 3059 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3060 | err && *err ? *err : "", path); |
| 3061 | #if defined(SSL_CTX_set_dh_auto) |
| 3062 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3063 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3064 | err && *err ? *err : ""); |
| 3065 | #else |
| 3066 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3067 | err && *err ? *err : ""); |
| 3068 | #endif |
| 3069 | ret |= ERR_WARN; |
| 3070 | goto end; |
| 3071 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3072 | } |
| 3073 | else { |
| 3074 | /* Clear openssl global errors stack */ |
| 3075 | ERR_clear_error(); |
| 3076 | |
| 3077 | if (global_ssl.default_dh_param <= 1024) { |
| 3078 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3079 | if (local_dh_1024 == NULL) |
| 3080 | local_dh_1024 = ssl_get_dh_1024(); |
| 3081 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3082 | if (local_dh_1024 == NULL) { |
| 3083 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3084 | err && *err ? *err : "", path); |
| 3085 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3086 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3087 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3088 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3089 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3090 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3091 | err && *err ? *err : "", path); |
| 3092 | #if defined(SSL_CTX_set_dh_auto) |
| 3093 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3094 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3095 | err && *err ? *err : ""); |
| 3096 | #else |
| 3097 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3098 | err && *err ? *err : ""); |
| 3099 | #endif |
| 3100 | ret |= ERR_WARN; |
| 3101 | goto end; |
| 3102 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3103 | } |
| 3104 | else { |
| 3105 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3106 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | end: |
William Lallemand | 4dd145a | 2020-02-05 11:46:33 +0100 | [diff] [blame] | 3110 | ERR_clear_error(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3111 | return ret; |
| 3112 | } |
| 3113 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3114 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3115 | /* Frees the contents of a cert_key_and_chain |
| 3116 | */ |
| 3117 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 3118 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3119 | if (!ckch) |
| 3120 | return; |
| 3121 | |
| 3122 | /* Free the certificate and set pointer to NULL */ |
| 3123 | if (ckch->cert) |
| 3124 | X509_free(ckch->cert); |
| 3125 | ckch->cert = NULL; |
| 3126 | |
| 3127 | /* Free the key and set pointer to NULL */ |
| 3128 | if (ckch->key) |
| 3129 | EVP_PKEY_free(ckch->key); |
| 3130 | ckch->key = NULL; |
| 3131 | |
| 3132 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3133 | if (ckch->chain) |
| 3134 | sk_X509_pop_free(ckch->chain, X509_free); |
| 3135 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3136 | |
William Lallemand | 455af50 | 2019-10-17 18:04:45 +0200 | [diff] [blame] | 3137 | if (ckch->dh) |
| 3138 | DH_free(ckch->dh); |
| 3139 | ckch->dh = NULL; |
| 3140 | |
| 3141 | if (ckch->sctl) { |
| 3142 | free(ckch->sctl->area); |
| 3143 | ckch->sctl->area = NULL; |
| 3144 | free(ckch->sctl); |
| 3145 | ckch->sctl = NULL; |
| 3146 | } |
| 3147 | |
| 3148 | if (ckch->ocsp_response) { |
| 3149 | free(ckch->ocsp_response->area); |
| 3150 | ckch->ocsp_response->area = NULL; |
| 3151 | free(ckch->ocsp_response); |
| 3152 | ckch->ocsp_response = NULL; |
| 3153 | } |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3154 | |
| 3155 | if (ckch->ocsp_issuer) |
William Lallemand | dad239d | 2020-01-23 11:59:02 +0100 | [diff] [blame] | 3156 | X509_free(ckch->ocsp_issuer); |
William Lallemand | 5c3c96f | 2020-01-23 11:53:13 +0100 | [diff] [blame] | 3157 | ckch->ocsp_issuer = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3158 | } |
| 3159 | |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3160 | /* |
| 3161 | * |
| 3162 | * This function copy a cert_key_and_chain in memory |
| 3163 | * |
| 3164 | * It's used to try to apply changes on a ckch before committing them, because |
| 3165 | * most of the time it's not possible to revert those changes |
| 3166 | * |
| 3167 | * Return a the dst or NULL |
| 3168 | */ |
| 3169 | static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 3170 | struct cert_key_and_chain *dst) |
| 3171 | { |
| 3172 | if (src->cert) { |
| 3173 | dst->cert = src->cert; |
| 3174 | X509_up_ref(src->cert); |
| 3175 | } |
| 3176 | |
| 3177 | if (src->key) { |
| 3178 | dst->key = src->key; |
| 3179 | EVP_PKEY_up_ref(src->key); |
| 3180 | } |
| 3181 | |
| 3182 | if (src->chain) { |
| 3183 | dst->chain = X509_chain_up_ref(src->chain); |
| 3184 | } |
| 3185 | |
| 3186 | if (src->dh) { |
| 3187 | DH_up_ref(src->dh); |
| 3188 | dst->dh = src->dh; |
| 3189 | } |
| 3190 | |
| 3191 | if (src->sctl) { |
| 3192 | struct buffer *sctl; |
| 3193 | |
| 3194 | sctl = calloc(1, sizeof(*sctl)); |
| 3195 | if (!chunk_dup(sctl, src->sctl)) { |
| 3196 | free(sctl); |
| 3197 | sctl = NULL; |
| 3198 | goto error; |
| 3199 | } |
| 3200 | dst->sctl = sctl; |
| 3201 | } |
| 3202 | |
| 3203 | if (src->ocsp_response) { |
| 3204 | struct buffer *ocsp_response; |
| 3205 | |
| 3206 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 3207 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 3208 | free(ocsp_response); |
| 3209 | ocsp_response = NULL; |
| 3210 | goto error; |
| 3211 | } |
| 3212 | dst->ocsp_response = ocsp_response; |
| 3213 | } |
| 3214 | |
| 3215 | if (src->ocsp_issuer) { |
| 3216 | X509_up_ref(src->ocsp_issuer); |
| 3217 | dst->ocsp_issuer = src->ocsp_issuer; |
| 3218 | } |
| 3219 | |
| 3220 | return dst; |
| 3221 | |
| 3222 | error: |
| 3223 | |
| 3224 | /* free everything */ |
| 3225 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 3226 | |
| 3227 | return NULL; |
| 3228 | } |
| 3229 | |
| 3230 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3231 | /* checks if a key and cert exists in the ckch |
| 3232 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3233 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3234 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 3235 | { |
| 3236 | return (ckch->cert != NULL && ckch->key != NULL); |
| 3237 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3238 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3239 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3240 | /* |
| 3241 | * return 0 on success or != 0 on failure |
| 3242 | */ |
| 3243 | static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 3244 | { |
| 3245 | int ret = 1; |
| 3246 | BIO *in = NULL; |
| 3247 | X509 *issuer; |
| 3248 | |
| 3249 | if (buf) { |
| 3250 | /* reading from a buffer */ |
| 3251 | in = BIO_new_mem_buf(buf, -1); |
| 3252 | if (in == NULL) { |
| 3253 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3254 | goto end; |
| 3255 | } |
| 3256 | |
| 3257 | } else { |
| 3258 | /* reading from a file */ |
| 3259 | in = BIO_new(BIO_s_file()); |
| 3260 | if (in == NULL) |
| 3261 | goto end; |
| 3262 | |
| 3263 | if (BIO_read_filename(in, path) <= 0) |
| 3264 | goto end; |
| 3265 | } |
| 3266 | |
| 3267 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3268 | if (!issuer) { |
| 3269 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3270 | err && *err ? *err : "", path); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3271 | goto end; |
| 3272 | } |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3273 | /* no error, fill ckch with new context, old context must be free */ |
| 3274 | if (ckch->ocsp_issuer) |
| 3275 | X509_free(ckch->ocsp_issuer); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3276 | ckch->ocsp_issuer = issuer; |
Emmanuel Hocdet | eb73dc3 | 2020-01-16 14:45:00 +0100 | [diff] [blame] | 3277 | ret = 0; |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3278 | |
| 3279 | end: |
| 3280 | |
| 3281 | ERR_clear_error(); |
| 3282 | if (in) |
| 3283 | BIO_free(in); |
| 3284 | |
| 3285 | return ret; |
| 3286 | } |
| 3287 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3288 | |
| 3289 | /* |
| 3290 | * 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] | 3291 | * The PEM must contain at least a Certificate, |
| 3292 | * It could contain a DH, a certificate chain and a PrivateKey. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3293 | * |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3294 | * If it failed you should not attempt to use the ckch but free it. |
| 3295 | * |
| 3296 | * Return 0 on success or != 0 on failure |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3297 | */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3298 | 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] | 3299 | { |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3300 | BIO *in = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3301 | int ret = 1; |
Emmanuel Hocdet | 078156d | 2020-01-22 17:02:53 +0100 | [diff] [blame] | 3302 | int i; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3303 | X509 *ca; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3304 | X509 *cert = NULL; |
| 3305 | EVP_PKEY *key = NULL; |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3306 | DH *dh = NULL; |
| 3307 | STACK_OF(X509) *chain = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3308 | |
| 3309 | if (buf) { |
| 3310 | /* reading from a buffer */ |
| 3311 | in = BIO_new_mem_buf(buf, -1); |
| 3312 | if (in == NULL) { |
| 3313 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3314 | goto end; |
| 3315 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3316 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3317 | } else { |
| 3318 | /* reading from a file */ |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3319 | in = BIO_new(BIO_s_file()); |
| 3320 | if (in == NULL) |
| 3321 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3322 | |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3323 | if (BIO_read_filename(in, path) <= 0) |
| 3324 | goto end; |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3325 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3326 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3327 | /* Read Private Key */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3328 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3329 | /* 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] | 3330 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3331 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3332 | /* Seek back to beginning of file */ |
| 3333 | if (BIO_reset(in) == -1) { |
| 3334 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3335 | err && *err ? *err : "", path); |
| 3336 | goto end; |
| 3337 | } |
| 3338 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3339 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 3340 | /* no need to return an error there, dh is not mandatory */ |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3341 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3342 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3343 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3344 | if (BIO_reset(in) == -1) { |
| 3345 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3346 | err && *err ? *err : "", path); |
| 3347 | goto end; |
| 3348 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3349 | |
| 3350 | /* Read Certificate */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3351 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3352 | if (cert == NULL) { |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3353 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3354 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3355 | goto end; |
| 3356 | } |
| 3357 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3358 | /* Look for a Certificate Chain */ |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3359 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3360 | if (chain == NULL) |
| 3361 | chain = sk_X509_new_null(); |
| 3362 | if (!sk_X509_push(chain, ca)) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3363 | X509_free(ca); |
| 3364 | goto end; |
| 3365 | } |
| 3366 | } |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 3367 | /* Find Certificate Chain in global */ |
| 3368 | if (chain == NULL) { |
| 3369 | AUTHORITY_KEYID *akid; |
| 3370 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 3371 | if (akid) { |
| 3372 | struct issuer_chain *issuer; |
| 3373 | struct eb64_node *node; |
| 3374 | u64 hk; |
| 3375 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 3376 | for (node = eb64_lookup(&global_ssl.cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 3377 | issuer = container_of(node, typeof(*issuer), node); |
| 3378 | if (X509_check_issued(sk_X509_value(issuer->chain, 0), cert) == X509_V_OK) { |
| 3379 | chain = X509_chain_up_ref(issuer->chain); |
| 3380 | break; |
| 3381 | } |
| 3382 | } |
| 3383 | AUTHORITY_KEYID_free(akid); |
| 3384 | } |
| 3385 | } |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3386 | /* no chain */ |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3387 | if (chain == NULL) { |
| 3388 | chain = sk_X509_new_null(); |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3391 | ret = ERR_get_error(); |
| 3392 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3393 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3394 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3395 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
William Lallemand | 75b15f7 | 2020-01-23 10:56:05 +0100 | [diff] [blame] | 3398 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 3399 | if (ckch->ocsp_response) { |
| 3400 | free(ckch->ocsp_response->area); |
| 3401 | ckch->ocsp_response->area = NULL; |
| 3402 | free(ckch->ocsp_response); |
| 3403 | ckch->ocsp_response = NULL; |
| 3404 | } |
| 3405 | |
| 3406 | if (ckch->sctl) { |
| 3407 | free(ckch->sctl->area); |
| 3408 | ckch->sctl->area = NULL; |
| 3409 | free(ckch->sctl); |
| 3410 | ckch->sctl = NULL; |
| 3411 | } |
| 3412 | |
| 3413 | if (ckch->ocsp_issuer) { |
| 3414 | X509_free(ckch->ocsp_issuer); |
| 3415 | ckch->ocsp_issuer = NULL; |
| 3416 | } |
| 3417 | |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3418 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 3419 | SWAP(ckch->key, key); |
| 3420 | SWAP(ckch->dh, dh); |
| 3421 | SWAP(ckch->cert, cert); |
| 3422 | SWAP(ckch->chain, chain); |
| 3423 | |
Emmanuel Hocdet | 078156d | 2020-01-22 17:02:53 +0100 | [diff] [blame] | 3424 | /* check if one of the certificate of the chain is the issuer */ |
| 3425 | for (i = 0; i < sk_X509_num(ckch->chain); i++) { |
| 3426 | X509 *issuer = sk_X509_value(ckch->chain, i); |
| 3427 | if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) { |
| 3428 | ckch->ocsp_issuer = issuer; |
| 3429 | X509_up_ref(issuer); |
| 3430 | break; |
| 3431 | } |
| 3432 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3433 | ret = 0; |
| 3434 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3435 | end: |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3436 | |
| 3437 | ERR_clear_error(); |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3438 | if (in) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3439 | BIO_free(in); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3440 | if (key) |
| 3441 | EVP_PKEY_free(key); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3442 | if (dh) |
| 3443 | DH_free(dh); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3444 | if (cert) |
| 3445 | X509_free(cert); |
Emmanuel Hocdet | 6b5b44e | 2019-12-20 17:47:12 +0100 | [diff] [blame] | 3446 | if (chain) |
| 3447 | sk_X509_pop_free(chain, X509_free); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3448 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3449 | return ret; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3453 | * Try to load a private key file from a <path> or a buffer <buf> |
| 3454 | * |
| 3455 | * If it failed you should not attempt to use the ckch but free it. |
| 3456 | * |
| 3457 | * Return 0 on success or != 0 on failure |
| 3458 | */ |
| 3459 | static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 3460 | { |
| 3461 | BIO *in = NULL; |
| 3462 | int ret = 1; |
| 3463 | EVP_PKEY *key = NULL; |
| 3464 | |
| 3465 | if (buf) { |
| 3466 | /* reading from a buffer */ |
| 3467 | in = BIO_new_mem_buf(buf, -1); |
| 3468 | if (in == NULL) { |
| 3469 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3470 | goto end; |
| 3471 | } |
| 3472 | |
| 3473 | } else { |
| 3474 | /* reading from a file */ |
| 3475 | in = BIO_new(BIO_s_file()); |
| 3476 | if (in == NULL) |
| 3477 | goto end; |
| 3478 | |
| 3479 | if (BIO_read_filename(in, path) <= 0) |
| 3480 | goto end; |
| 3481 | } |
| 3482 | |
| 3483 | /* Read Private Key */ |
| 3484 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3485 | if (key == NULL) { |
| 3486 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3487 | err && *err ? *err : "", path); |
| 3488 | goto end; |
| 3489 | } |
| 3490 | |
| 3491 | ret = 0; |
| 3492 | |
| 3493 | SWAP(ckch->key, key); |
| 3494 | |
| 3495 | end: |
| 3496 | |
| 3497 | ERR_clear_error(); |
| 3498 | if (in) |
| 3499 | BIO_free(in); |
| 3500 | if (key) |
| 3501 | EVP_PKEY_free(key); |
| 3502 | |
| 3503 | return ret; |
| 3504 | } |
| 3505 | |
| 3506 | /* |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3507 | * Try to load in a ckch every files related to a ckch. |
| 3508 | * (PEM, sctl, ocsp, issuer etc.) |
| 3509 | * |
| 3510 | * This function is only used to load files during the configuration parsing, |
| 3511 | * it is not used with the CLI. |
| 3512 | * |
| 3513 | * This allows us to carry the contents of the file without having to read the |
| 3514 | * file multiple times. The caller must call |
| 3515 | * ssl_sock_free_cert_key_and_chain_contents. |
| 3516 | * |
| 3517 | * returns: |
| 3518 | * 0 on Success |
| 3519 | * 1 on SSL Failure |
| 3520 | */ |
| 3521 | static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3522 | { |
| 3523 | int ret = 1; |
| 3524 | |
| 3525 | /* try to load the PEM */ |
| 3526 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 3527 | goto end; |
| 3528 | } |
| 3529 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 3530 | /* try to load an external private key if it wasn't in the PEM */ |
| 3531 | if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) { |
| 3532 | char fp[MAXPATHLEN+1]; |
| 3533 | struct stat st; |
| 3534 | |
| 3535 | snprintf(fp, MAXPATHLEN+1, "%s.key", path); |
| 3536 | if (stat(fp, &st) == 0) { |
| 3537 | if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) { |
| 3538 | memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n", |
| 3539 | err && *err ? *err : "", fp); |
| 3540 | goto end; |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | if (ckch->key == NULL) { |
| 3546 | memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path); |
| 3547 | goto end; |
| 3548 | } |
| 3549 | |
| 3550 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 3551 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 3552 | err && *err ? *err : "", path); |
| 3553 | goto end; |
| 3554 | } |
| 3555 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3556 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3557 | /* try to load the sctl file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3558 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3559 | char fp[MAXPATHLEN+1]; |
| 3560 | struct stat st; |
| 3561 | |
| 3562 | snprintf(fp, MAXPATHLEN+1, "%s.sctl", path); |
| 3563 | if (stat(fp, &st) == 0) { |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 3564 | if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3565 | 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] | 3566 | err && *err ? *err : "", fp); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3567 | ret = 1; |
| 3568 | goto end; |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3573 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3574 | /* try to load an ocsp response file */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3575 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3576 | char fp[MAXPATHLEN+1]; |
| 3577 | struct stat st; |
| 3578 | |
| 3579 | snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path); |
| 3580 | if (stat(fp, &st) == 0) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 3581 | if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3582 | ret = 1; |
| 3583 | goto end; |
| 3584 | } |
| 3585 | } |
| 3586 | } |
| 3587 | |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3588 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 3589 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3590 | /* 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] | 3591 | if (!ckch->ocsp_issuer) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3592 | struct stat st; |
| 3593 | char fp[MAXPATHLEN+1]; |
| 3594 | |
| 3595 | snprintf(fp, MAXPATHLEN+1, "%s.issuer", path); |
| 3596 | if (stat(fp, &st) == 0) { |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3597 | if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3598 | ret = 1; |
| 3599 | goto end; |
| 3600 | } |
| 3601 | |
| 3602 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
William Lallemand | 786188f | 2019-10-15 10:05:37 +0200 | [diff] [blame] | 3603 | memprintf(err, "%s '%s' is not an issuer'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3604 | err && *err ? *err : "", fp); |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3605 | ret = 1; |
| 3606 | goto end; |
| 3607 | } |
| 3608 | } else { |
| 3609 | memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3610 | err && *err ? *err : ""); |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3611 | ret = 1; |
| 3612 | goto end; |
| 3613 | } |
| 3614 | } |
| 3615 | } |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3616 | #endif |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3617 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3618 | ret = 0; |
| 3619 | |
| 3620 | end: |
| 3621 | |
| 3622 | ERR_clear_error(); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3623 | |
| 3624 | /* Something went wrong in one of the reads */ |
| 3625 | if (ret != 0) |
| 3626 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3627 | |
| 3628 | return ret; |
| 3629 | } |
| 3630 | |
| 3631 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3632 | * Returns a bitfield containing the flags: |
| 3633 | * ERR_FATAL in any fatal error case |
| 3634 | * ERR_ALERT if the reason of the error is available in err |
| 3635 | * ERR_WARN if a warning is available into err |
| 3636 | * The value 0 means there is no error nor warning and |
| 3637 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3638 | */ |
| 3639 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3640 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3641 | int errcode = 0; |
| 3642 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3643 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3644 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3645 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3646 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3647 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3651 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3652 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3653 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3654 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3655 | } |
| 3656 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3657 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3658 | #ifdef SSL_CTX_set1_chain |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3659 | if (!SSL_CTX_set1_chain(ctx, ckch->chain)) { |
| 3660 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3661 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3662 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3663 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3664 | } |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3665 | #else |
| 3666 | { /* legacy compat (< openssl 1.0.2) */ |
| 3667 | X509 *ca; |
Emmanuel Hocdet | 140b64f | 2019-10-24 18:33:10 +0200 | [diff] [blame] | 3668 | STACK_OF(X509) *chain; |
| 3669 | chain = X509_chain_up_ref(ckch->chain); |
| 3670 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3671 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3672 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3673 | err && *err ? *err : "", path); |
| 3674 | X509_free(ca); |
Emmanuel Hocdet | 140b64f | 2019-10-24 18:33:10 +0200 | [diff] [blame] | 3675 | sk_X509_pop_free(chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3676 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3677 | goto end; |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3681 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3682 | #ifndef OPENSSL_NO_DH |
| 3683 | /* store a NULL pointer to indicate we have not yet loaded |
| 3684 | a custom DH param file */ |
| 3685 | if (ssl_dh_ptr_index >= 0) { |
| 3686 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3687 | } |
| 3688 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3689 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3690 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3691 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3692 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3693 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3694 | } |
| 3695 | #endif |
| 3696 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3697 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3698 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3699 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3700 | 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] | 3701 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3702 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3703 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3704 | } |
| 3705 | } |
| 3706 | #endif |
| 3707 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 3708 | #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] | 3709 | /* Load OCSP Info into context */ |
| 3710 | if (ckch->ocsp_response) { |
| 3711 | if (ssl_sock_load_ocsp(ctx, ckch) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3712 | 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", |
| 3713 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3714 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3715 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3716 | } |
| 3717 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3718 | #endif |
| 3719 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3720 | end: |
| 3721 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3722 | } |
| 3723 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3724 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3725 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3726 | 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] | 3727 | { |
| 3728 | struct sni_keytype *s_kt = NULL; |
| 3729 | struct ebmb_node *node; |
| 3730 | int i; |
| 3731 | |
| 3732 | for (i = 0; i < trash.size; i++) { |
| 3733 | if (!str[i]) |
| 3734 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3735 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3736 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3737 | trash.area[i] = 0; |
| 3738 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3739 | if (!node) { |
| 3740 | /* CN not found in tree */ |
| 3741 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3742 | /* Using memcpy here instead of strncpy. |
| 3743 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3744 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3745 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3746 | if (!s_kt) |
| 3747 | return -1; |
| 3748 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3749 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3750 | s_kt->keytypes = 0; |
| 3751 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3752 | } else { |
| 3753 | /* CN found in tree */ |
| 3754 | s_kt = container_of(node, struct sni_keytype, name); |
| 3755 | } |
| 3756 | |
| 3757 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3758 | s_kt->keytypes |= 1<<key_index; |
| 3759 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3760 | return 0; |
| 3761 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3762 | } |
| 3763 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3764 | #endif |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3765 | /* |
| 3766 | * Free a ckch_store and its ckch(s) |
| 3767 | * The linked ckch_inst are not free'd |
| 3768 | */ |
| 3769 | void ckchs_free(struct ckch_store *ckchs) |
| 3770 | { |
| 3771 | if (!ckchs) |
| 3772 | return; |
| 3773 | |
| 3774 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3775 | if (ckchs->multi) { |
| 3776 | int n; |
| 3777 | |
| 3778 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3779 | ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]); |
| 3780 | } else |
| 3781 | #endif |
| 3782 | { |
| 3783 | ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch); |
| 3784 | ckchs->ckch = NULL; |
| 3785 | } |
| 3786 | |
| 3787 | free(ckchs); |
| 3788 | } |
| 3789 | |
| 3790 | /* allocate and duplicate a ckch_store |
| 3791 | * Return a new ckch_store or NULL */ |
| 3792 | static struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 3793 | { |
| 3794 | struct ckch_store *dst; |
| 3795 | int pathlen; |
| 3796 | |
| 3797 | pathlen = strlen(src->path); |
| 3798 | dst = calloc(1, sizeof(*dst) + pathlen + 1); |
| 3799 | if (!dst) |
| 3800 | return NULL; |
| 3801 | /* copy previous key */ |
| 3802 | memcpy(dst->path, src->path, pathlen + 1); |
| 3803 | dst->multi = src->multi; |
| 3804 | LIST_INIT(&dst->ckch_inst); |
| 3805 | |
| 3806 | dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch)); |
| 3807 | if (!dst->ckch) |
| 3808 | goto error; |
| 3809 | |
| 3810 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3811 | if (src->multi) { |
| 3812 | int n; |
| 3813 | |
| 3814 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3815 | if (&src->ckch[n]) { |
| 3816 | if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n])) |
| 3817 | goto error; |
| 3818 | } |
| 3819 | } |
| 3820 | } else |
| 3821 | #endif |
| 3822 | { |
| 3823 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 3824 | goto error; |
| 3825 | } |
| 3826 | |
| 3827 | return dst; |
| 3828 | |
| 3829 | error: |
| 3830 | ckchs_free(dst); |
| 3831 | |
| 3832 | return NULL; |
| 3833 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3834 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3835 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3836 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3837 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3838 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3839 | { |
| 3840 | struct ebmb_node *eb; |
| 3841 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3842 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3843 | if (!eb) |
| 3844 | return NULL; |
| 3845 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3846 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3850 | * 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] | 3851 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3852 | 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] | 3853 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3854 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3855 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3856 | ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1); |
| 3857 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3858 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3859 | goto end; |
| 3860 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3861 | ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1)); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3862 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3863 | if (!ckchs->ckch) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3864 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3865 | goto end; |
| 3866 | } |
| 3867 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3868 | LIST_INIT(&ckchs->ckch_inst); |
| 3869 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3870 | if (!multi) { |
| 3871 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3872 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3873 | goto end; |
| 3874 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3875 | /* insert into the ckchs tree */ |
| 3876 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3877 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3878 | } else { |
| 3879 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3880 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3881 | char fp[MAXPATHLEN+1] = {0}; |
| 3882 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3883 | |
| 3884 | /* Load all possible certs and keys */ |
| 3885 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3886 | struct stat buf; |
| 3887 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3888 | if (stat(fp, &buf) == 0) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3889 | 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] | 3890 | goto end; |
| 3891 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3892 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3893 | } |
| 3894 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3895 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3896 | |
| 3897 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 3898 | 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] | 3899 | goto end; |
| 3900 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3901 | /* insert into the ckchs tree */ |
| 3902 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3903 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3904 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3905 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3906 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3907 | end: |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3908 | if (ckchs) { |
| 3909 | free(ckchs->ckch); |
| 3910 | ebmb_delete(&ckchs->node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3913 | free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3914 | |
| 3915 | return NULL; |
| 3916 | } |
| 3917 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3918 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3919 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3920 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3921 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3922 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3923 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3924 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3925 | * 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] | 3926 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3927 | * Returns a bitfield containing the flags: |
| 3928 | * ERR_FATAL in any fatal error case |
| 3929 | * ERR_ALERT if the reason of the error is available in err |
| 3930 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3931 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3932 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3933 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3934 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3935 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3936 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3937 | int i = 0, n = 0; |
| 3938 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 3939 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3940 | struct ebmb_node *node; |
| 3941 | struct ebmb_node *next; |
| 3942 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3943 | * of keytypes |
| 3944 | */ |
| 3945 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3946 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3947 | X509_NAME *xname = NULL; |
| 3948 | char *str = NULL; |
| 3949 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3950 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3951 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3952 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3953 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3954 | *ckchi = NULL; |
| 3955 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3956 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3957 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3958 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3959 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | ckch_inst = ckch_inst_new(); |
| 3963 | if (!ckch_inst) { |
| 3964 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3965 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3966 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3967 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3968 | } |
| 3969 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3970 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3971 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3972 | /* at least one of the instances is using filters during the config |
| 3973 | * parsing, that's ok to inherit this during loading on CLI */ |
William Lallemand | 920b035 | 2019-12-04 15:33:01 +0100 | [diff] [blame] | 3974 | ckchs->filters |= !!fcount; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3975 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3976 | /* Process each ckch and update keytypes for each CN/SAN |
| 3977 | * for example, if CN/SAN www.a.com is associated with |
| 3978 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3979 | * www.a.com will have: |
| 3980 | * keyindex = 0 | 1 | 4 = 5 |
| 3981 | */ |
| 3982 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3983 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3984 | |
| 3985 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3986 | continue; |
| 3987 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3988 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3989 | for (i = 0; i < fcount; i++) { |
| 3990 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3991 | if (ret < 0) { |
| 3992 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3993 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3994 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3995 | goto end; |
| 3996 | } |
| 3997 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3998 | } else { |
| 3999 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 4000 | * so the line that contains logic is marked via comments |
| 4001 | */ |
| 4002 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 4003 | i = -1; |
| 4004 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4005 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4006 | ASN1_STRING *value; |
| 4007 | value = X509_NAME_ENTRY_get_data(entry); |
| 4008 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4009 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4010 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4011 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4012 | OPENSSL_free(str); |
| 4013 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4014 | if (ret < 0) { |
| 4015 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4016 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4017 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4018 | goto end; |
| 4019 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4020 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4021 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4022 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4023 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4024 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4025 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 4026 | if (names) { |
| 4027 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4028 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4029 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4030 | if (name->type == GEN_DNS) { |
| 4031 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 4032 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4033 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4034 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4035 | OPENSSL_free(str); |
| 4036 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4037 | if (ret < 0) { |
| 4038 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4039 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4040 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 4041 | goto end; |
| 4042 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 4043 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4044 | } |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 4049 | } |
| 4050 | |
| 4051 | /* If no files found, return error */ |
| 4052 | if (eb_is_empty(&sni_keytypes_map)) { |
| 4053 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 4054 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4055 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4056 | goto end; |
| 4057 | } |
| 4058 | |
| 4059 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 4060 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 4061 | * and add each CTX to the SNI tree |
| 4062 | * |
| 4063 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4064 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4065 | * combination is denoted by the key in the map. Each key |
| 4066 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 4067 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 4068 | * entry in the array to correspond to the unique combo (key) |
| 4069 | * associated with i. This unique key combo (i) will be associated |
| 4070 | * with combos[i-1] |
| 4071 | */ |
| 4072 | |
| 4073 | node = ebmb_first(&sni_keytypes_map); |
| 4074 | while (node) { |
| 4075 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4076 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4077 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4078 | |
| 4079 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 4080 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 4081 | cur_ctx = key_combos[i-1].ctx; |
| 4082 | |
| 4083 | if (cur_ctx == NULL) { |
| 4084 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4085 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4086 | if (cur_ctx == NULL) { |
| 4087 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 4088 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4089 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4090 | goto end; |
| 4091 | } |
| 4092 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 4093 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4094 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4095 | if (i & (1<<n)) { |
| 4096 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 4097 | 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] | 4098 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 4099 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4100 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4104 | /* Update key_combos */ |
| 4105 | key_combos[i-1].ctx = cur_ctx; |
| 4106 | } |
| 4107 | |
| 4108 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4109 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4110 | 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] | 4111 | kinfo, str, key_combos[i-1].order); |
| 4112 | if (key_combos[i-1].order < 0) { |
| 4113 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4114 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4115 | goto end; |
| 4116 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4117 | node = ebmb_next(node); |
| 4118 | } |
| 4119 | |
| 4120 | |
| 4121 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 4122 | if (!bind_conf->default_ctx) { |
| 4123 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 4124 | if (key_combos[i].ctx) { |
| 4125 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4126 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4127 | ckch_inst->is_default = 1; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4128 | break; |
| 4129 | } |
| 4130 | } |
| 4131 | } |
| 4132 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4133 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4134 | ckch_inst->ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4135 | end: |
| 4136 | |
| 4137 | if (names) |
| 4138 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 4139 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4140 | node = ebmb_first(&sni_keytypes_map); |
| 4141 | while (node) { |
| 4142 | next = ebmb_next(node); |
| 4143 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 4144 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4145 | node = next; |
| 4146 | } |
| 4147 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4148 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4149 | struct sni_ctx *sc0, *sc0b; |
| 4150 | |
| 4151 | /* free the SSL_CTX in case of error */ |
| 4152 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 4153 | if (key_combos[i].ctx) |
| 4154 | SSL_CTX_free(key_combos[i].ctx); |
| 4155 | } |
| 4156 | |
| 4157 | /* free the sni_ctx in case of error */ |
| 4158 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 4159 | |
| 4160 | ebmb_delete(&sc0->name); |
| 4161 | LIST_DEL(&sc0->by_ckch_inst); |
| 4162 | free(sc0); |
| 4163 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4164 | free(ckch_inst); |
| 4165 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 4166 | } |
| 4167 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4168 | *ckchi = ckch_inst; |
| 4169 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4170 | } |
| 4171 | #else |
| 4172 | /* This is a dummy, that just logs an error and returns error */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4173 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 4174 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4175 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4176 | { |
| 4177 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4178 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4179 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4180 | } |
| 4181 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4182 | #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] | 4183 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4184 | /* |
| 4185 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4186 | * |
| 4187 | * Returns a bitfield containing the flags: |
| 4188 | * ERR_FATAL in any fatal error case |
| 4189 | * ERR_ALERT if the reason of the error is available in err |
| 4190 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4191 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4192 | static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
| 4193 | 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] | 4194 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4195 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4196 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4197 | int order = 0; |
| 4198 | X509_NAME *xname; |
| 4199 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4200 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4201 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4202 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4203 | STACK_OF(GENERAL_NAME) *names; |
| 4204 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4205 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4206 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4207 | int errcode = 0; |
| 4208 | |
| 4209 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 4210 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4211 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4212 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4213 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4214 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4215 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4216 | /* at least one of the instances is using filters during the config |
| 4217 | * parsing, that's ok to inherit this during loading on CLI */ |
William Lallemand | 920b035 | 2019-12-04 15:33:01 +0100 | [diff] [blame] | 4218 | ckchs->filters |= !!fcount; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4219 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4220 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 4221 | if (!ctx) { |
| 4222 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4223 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4224 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4225 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4228 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 4229 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4230 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4231 | |
| 4232 | ckch_inst = ckch_inst_new(); |
| 4233 | if (!ckch_inst) { |
| 4234 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4235 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4236 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4237 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4238 | } |
| 4239 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4240 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4241 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4242 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4243 | switch(EVP_PKEY_base_id(pkey)) { |
| 4244 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4245 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4246 | break; |
| 4247 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4248 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 4249 | break; |
| 4250 | case EVP_PKEY_DSA: |
| 4251 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4252 | break; |
| 4253 | } |
| 4254 | EVP_PKEY_free(pkey); |
| 4255 | } |
| 4256 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4257 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4258 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4259 | 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] | 4260 | if (order < 0) { |
| 4261 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4262 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4263 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4264 | } |
| 4265 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4266 | } |
| 4267 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4268 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4269 | 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] | 4270 | if (names) { |
| 4271 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4272 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 4273 | if (name->type == GEN_DNS) { |
| 4274 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4275 | 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] | 4276 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4277 | if (order < 0) { |
| 4278 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4279 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4280 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4281 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4282 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4283 | } |
| 4284 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4285 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4286 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4287 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4288 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4289 | i = -1; |
| 4290 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4291 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4292 | ASN1_STRING *value; |
| 4293 | |
| 4294 | value = X509_NAME_ENTRY_get_data(entry); |
| 4295 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4296 | 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] | 4297 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4298 | if (order < 0) { |
| 4299 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4300 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4301 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4302 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4303 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4304 | } |
| 4305 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4306 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4307 | * the tree, so it will be discovered and cleaned in time. |
| 4308 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4309 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4310 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4311 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4312 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4313 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4314 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4315 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4316 | } |
| 4317 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4318 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4319 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4320 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4321 | ckch_inst->is_default = 1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4322 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4323 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4324 | /* everything succeed, the ckch instance can be used */ |
| 4325 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4326 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4327 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4328 | *ckchi = ckch_inst; |
| 4329 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4330 | |
| 4331 | error: |
| 4332 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4333 | if (ckch_inst) { |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4334 | struct sni_ctx *sc0, *sc0b; |
| 4335 | |
| 4336 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 4337 | |
| 4338 | ebmb_delete(&sc0->name); |
| 4339 | LIST_DEL(&sc0->by_ckch_inst); |
| 4340 | free(sc0); |
| 4341 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4342 | free(ckch_inst); |
| 4343 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4344 | } |
| 4345 | /* We only created 1 SSL_CTX so we can free it there */ |
| 4346 | SSL_CTX_free(ctx); |
| 4347 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4348 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4351 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4352 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4353 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4354 | char **sni_filter, int fcount, char **err) |
| 4355 | { |
| 4356 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4357 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4358 | |
| 4359 | /* we found the ckchs in the tree, we can use it directly */ |
| 4360 | if (ckchs->multi) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4361 | 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] | 4362 | else |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4363 | 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] | 4364 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4365 | if (errcode & ERR_CODE) |
| 4366 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4367 | |
| 4368 | ssl_sock_load_cert_sni(ckch_inst, bind_conf); |
| 4369 | |
| 4370 | /* succeed, add the instance to the ckch_store's list of instance */ |
| 4371 | LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4372 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4376 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4377 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4378 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4379 | struct dirent **de_list; |
| 4380 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4381 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 4382 | char *end; |
| 4383 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4384 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4385 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4386 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4387 | int is_bundle; |
| 4388 | int j; |
| 4389 | #endif |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4390 | if ((ckchs = ckchs_lookup(path))) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4391 | /* we found the ckchs in the tree, we can use it directly */ |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4392 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4395 | if (stat(path, &buf) == 0) { |
William Dauchy | 9a8ef7f | 2020-01-13 17:52:49 +0100 | [diff] [blame] | 4396 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4397 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 4398 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4399 | return ERR_ALERT | ERR_FATAL; |
| 4400 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4401 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4402 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4403 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4404 | /* strip trailing slashes, including first one */ |
| 4405 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 4406 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4407 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4408 | n = scandir(path, &de_list, 0, alphasort); |
| 4409 | if (n < 0) { |
| 4410 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 4411 | err && *err ? *err : "", path, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4412 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4413 | } |
| 4414 | else { |
| 4415 | for (i = 0; i < n; i++) { |
| 4416 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 4417 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4418 | end = strrchr(de->d_name, '.'); |
William Lallemand | 3f25ae3 | 2020-02-24 16:30:12 +0100 | [diff] [blame] | 4419 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key"))) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4420 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4421 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4422 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 4423 | if (stat(fp, &buf) != 0) { |
| 4424 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4425 | err && *err ? *err : "", fp, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4426 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4427 | goto ignore_entry; |
| 4428 | } |
| 4429 | if (!S_ISREG(buf.st_mode)) |
| 4430 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4431 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4432 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4433 | is_bundle = 0; |
| 4434 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 4435 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 4436 | if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4437 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 4438 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 4439 | is_bundle = 1; |
| 4440 | break; |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | if (is_bundle) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4445 | int dp_len; |
| 4446 | |
| 4447 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4448 | |
| 4449 | /* increment i and free de until we get to a non-bundle cert |
| 4450 | * Note here that we look at de_list[i + 1] before freeing de |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4451 | * this is important since ignore_entry will free de. This also |
| 4452 | * guarantees that de->d_name continues to hold the same prefix. |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4453 | */ |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4454 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4455 | free(de); |
| 4456 | i++; |
| 4457 | de = de_list[i]; |
| 4458 | } |
| 4459 | |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4460 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4461 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 4462 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 4463 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4464 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4465 | else |
| 4466 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4467 | /* Successfully processed the bundle */ |
| 4468 | goto ignore_entry; |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | #endif |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4473 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 4474 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 4475 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4476 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4477 | else |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4478 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4479 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4480 | ignore_entry: |
| 4481 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4482 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4483 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4484 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4485 | return cfgerr; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4486 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 4487 | } else { |
| 4488 | /* stat failed */ |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4489 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 4490 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 4491 | /* try to load a bundle if it is permitted */ |
| 4492 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 4493 | if (!ckchs) |
| 4494 | return ERR_ALERT | ERR_FATAL; |
| 4495 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
| 4496 | } else { |
| 4497 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4498 | err && *err ? *err : "", fp, strerror(errno)); |
| 4499 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4500 | } |
| 4501 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4502 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4503 | return cfgerr; |
| 4504 | } |
| 4505 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4506 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4507 | * done once. Zero is returned if the operation fails. No error is returned |
| 4508 | * if the random is said as not implemented, because we expect that openssl |
| 4509 | * will use another method once needed. |
| 4510 | */ |
| 4511 | static int ssl_initialize_random() |
| 4512 | { |
| 4513 | unsigned char random; |
| 4514 | static int random_initialized = 0; |
| 4515 | |
| 4516 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4517 | random_initialized = 1; |
| 4518 | |
| 4519 | return random_initialized; |
| 4520 | } |
| 4521 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4522 | /* release ssl bind conf */ |
| 4523 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4524 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4525 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4526 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4527 | free(conf->npn_str); |
| 4528 | conf->npn_str = NULL; |
| 4529 | #endif |
| 4530 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4531 | free(conf->alpn_str); |
| 4532 | conf->alpn_str = NULL; |
| 4533 | #endif |
| 4534 | free(conf->ca_file); |
| 4535 | conf->ca_file = NULL; |
| 4536 | free(conf->crl_file); |
| 4537 | conf->crl_file = NULL; |
| 4538 | free(conf->ciphers); |
| 4539 | conf->ciphers = NULL; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4540 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4541 | free(conf->ciphersuites); |
| 4542 | conf->ciphersuites = NULL; |
| 4543 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4544 | free(conf->curves); |
| 4545 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4546 | free(conf->ecdhe); |
| 4547 | conf->ecdhe = NULL; |
| 4548 | } |
| 4549 | } |
| 4550 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4551 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4552 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 4553 | { |
| 4554 | char thisline[CRT_LINESIZE]; |
| 4555 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4556 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4557 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4558 | int linenum = 0; |
| 4559 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4560 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4561 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4562 | if ((f = fopen(file, "r")) == NULL) { |
| 4563 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4564 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4565 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4566 | |
| 4567 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4568 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4569 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4570 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4571 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4572 | char *crt_path; |
| 4573 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4574 | |
| 4575 | linenum++; |
| 4576 | end = line + strlen(line); |
| 4577 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4578 | /* Check if we reached the limit and the last char is not \n. |
| 4579 | * Watch out for the last line without the terminating '\n'! |
| 4580 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4581 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4582 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4583 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4584 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4587 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4588 | newarg = 1; |
| 4589 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4590 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 4591 | /* end of string, end of loop */ |
| 4592 | *line = 0; |
| 4593 | break; |
Willy Tarreau | ded15b7 | 2020-02-25 07:51:59 +0100 | [diff] [blame] | 4594 | } else if (isspace((unsigned char)*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4595 | newarg = 1; |
| 4596 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4597 | } else if (*line == '[') { |
| 4598 | if (ssl_b) { |
| 4599 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4600 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4601 | break; |
| 4602 | } |
| 4603 | if (!arg) { |
| 4604 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4605 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4606 | break; |
| 4607 | } |
| 4608 | ssl_b = arg; |
| 4609 | newarg = 1; |
| 4610 | *line = 0; |
| 4611 | } else if (*line == ']') { |
| 4612 | if (ssl_e) { |
| 4613 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4614 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4615 | break; |
| 4616 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4617 | if (!ssl_b) { |
| 4618 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4619 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4620 | break; |
| 4621 | } |
| 4622 | ssl_e = arg; |
| 4623 | newarg = 1; |
| 4624 | *line = 0; |
| 4625 | } else if (newarg) { |
| 4626 | if (arg == MAX_CRT_ARGS) { |
| 4627 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4628 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4629 | break; |
| 4630 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4631 | newarg = 0; |
| 4632 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4633 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4634 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4635 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4636 | if (cfgerr) |
| 4637 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4638 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4639 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4640 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4641 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4642 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4643 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4644 | crt_path = args[0]; |
| 4645 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4646 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4647 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4648 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4649 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4650 | break; |
| 4651 | } |
| 4652 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4653 | crt_path = path; |
| 4654 | } |
| 4655 | |
| 4656 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4657 | cur_arg = ssl_b ? ssl_b : 1; |
| 4658 | while (cur_arg < ssl_e) { |
| 4659 | newarg = 0; |
| 4660 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4661 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4662 | newarg = 1; |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4663 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4664 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4665 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4666 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4667 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4668 | } |
| 4669 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4670 | break; |
| 4671 | } |
| 4672 | } |
| 4673 | if (!cfgerr && !newarg) { |
| 4674 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4675 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4676 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4677 | break; |
| 4678 | } |
| 4679 | } |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4680 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4681 | if (cfgerr) { |
| 4682 | ssl_sock_free_ssl_conf(ssl_conf); |
| 4683 | free(ssl_conf); |
| 4684 | ssl_conf = NULL; |
| 4685 | break; |
| 4686 | } |
| 4687 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4688 | if ((ckchs = ckchs_lookup(crt_path)) == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4689 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4690 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4691 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4692 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4693 | } |
| 4694 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4695 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4696 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4697 | else |
| 4698 | cfgerr |= ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, &args[cur_arg], arg - cur_arg - 1, err); |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4699 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4700 | if (cfgerr) { |
| 4701 | memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4702 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4703 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4704 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4705 | fclose(f); |
| 4706 | return cfgerr; |
| 4707 | } |
| 4708 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4709 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4710 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4711 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4712 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4713 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4714 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4715 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4716 | SSL_OP_NO_SSLv2 | |
| 4717 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4718 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4719 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4720 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4721 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4722 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4723 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4724 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4725 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4726 | SSL_MODE_RELEASE_BUFFERS | |
| 4727 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4728 | 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] | 4729 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4730 | int flags = MC_SSL_O_ALL; |
| 4731 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4732 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4733 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4734 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4735 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4736 | 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] | 4737 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4738 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4739 | 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] | 4740 | else |
| 4741 | flags = conf_ssl_methods->flags; |
| 4742 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4743 | min = conf_ssl_methods->min; |
| 4744 | max = conf_ssl_methods->max; |
| 4745 | /* start with TLSv10 to remove SSLv3 per default */ |
| 4746 | if (!min && (!max || max >= CONF_TLSV10)) |
| 4747 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4748 | /* 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] | 4749 | if (min) |
| 4750 | flags |= (methodVersions[min].flag - 1); |
| 4751 | if (max) |
| 4752 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4753 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4754 | min = max = CONF_TLSV_NONE; |
| 4755 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4756 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4757 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4758 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4759 | if (min) { |
| 4760 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4761 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4762 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4763 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4764 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4765 | hole = 0; |
| 4766 | } |
| 4767 | max = i; |
| 4768 | } |
| 4769 | else { |
| 4770 | min = max = i; |
| 4771 | } |
| 4772 | } |
| 4773 | else { |
| 4774 | if (min) |
| 4775 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4776 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4777 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4778 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4779 | 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] | 4780 | cfgerr += 1; |
| 4781 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4782 | /* save real min/max in bind_conf */ |
| 4783 | conf_ssl_methods->min = min; |
| 4784 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4785 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4786 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4787 | /* 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] | 4788 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4789 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4790 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4791 | else |
| 4792 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4793 | if (flags & methodVersions[i].flag) |
| 4794 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4795 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4796 | /* 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] | 4797 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4798 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4799 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4800 | |
| 4801 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4802 | options |= SSL_OP_NO_TICKET; |
| 4803 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4804 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4805 | |
| 4806 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4807 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4808 | #endif |
| 4809 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4810 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4811 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4812 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4813 | if (global_ssl.async) |
| 4814 | mode |= SSL_MODE_ASYNC; |
| 4815 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4816 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4817 | if (global_ssl.life_time) |
| 4818 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4819 | |
| 4820 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4821 | #ifdef OPENSSL_IS_BORINGSSL |
| 4822 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4823 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 4824 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4825 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4826 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4827 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4828 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4829 | #else |
| 4830 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4831 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4832 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4833 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4834 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4835 | } |
| 4836 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4837 | |
| 4838 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4839 | { |
| 4840 | if (first == block) { |
| 4841 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4842 | if (first->len > 0) |
| 4843 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | /* return first block from sh_ssl_sess */ |
| 4848 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4849 | { |
| 4850 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4851 | |
| 4852 | } |
| 4853 | |
| 4854 | /* store a session into the cache |
| 4855 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4856 | * data: asn1 encoded session |
| 4857 | * data_len: asn1 encoded session length |
| 4858 | * Returns 1 id session was stored (else 0) |
| 4859 | */ |
| 4860 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4861 | { |
| 4862 | struct shared_block *first; |
| 4863 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4864 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4865 | 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] | 4866 | if (!first) { |
| 4867 | /* Could not retrieve enough free blocks to store that session */ |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
| 4871 | /* STORE the key in the first elem */ |
| 4872 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4873 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4874 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4875 | |
| 4876 | /* it returns the already existing node |
| 4877 | or current node if none, never returns null */ |
| 4878 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4879 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4880 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4881 | /* release the reserved row */ |
| 4882 | shctx_row_dec_hot(ssl_shctx, first); |
| 4883 | /* replace the previous session already in the tree */ |
| 4884 | sh_ssl_sess = oldsh_ssl_sess; |
| 4885 | /* ignore the previous session data, only use the header */ |
| 4886 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4887 | shctx_row_inc_hot(ssl_shctx, first); |
| 4888 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4889 | } |
| 4890 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4891 | 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] | 4892 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4893 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4897 | |
| 4898 | return 1; |
| 4899 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4900 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4901 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4902 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4903 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4904 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4905 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4906 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4907 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4908 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4909 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4910 | int len; |
| 4911 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4912 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4913 | len = i2d_SSL_SESSION(sess, NULL); |
| 4914 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4915 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4916 | } else { |
| 4917 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4918 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4919 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4920 | } |
| 4921 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4922 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4923 | &ptr); |
| 4924 | } |
| 4925 | } else { |
| 4926 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4927 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4928 | } |
| 4929 | |
| 4930 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4931 | } |
| 4932 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4933 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4934 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4935 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4936 | { |
| 4937 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4938 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4939 | unsigned char *p; |
| 4940 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4941 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4942 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4943 | |
| 4944 | /* Session id is already stored in to key and session id is known |
| 4945 | * so we dont store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4946 | * note: SSL_SESSION_set1_id is using |
| 4947 | * a memcpy so we need to use a different pointer |
| 4948 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4949 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4950 | */ |
| 4951 | |
| 4952 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4953 | |
| 4954 | /* copy value in an other buffer */ |
| 4955 | memcpy(encid, sid_data, sid_length); |
| 4956 | |
| 4957 | /* pad with 0 */ |
| 4958 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4959 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4960 | |
| 4961 | /* force length to zero to avoid ASN1 encoding */ |
| 4962 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4963 | |
| 4964 | /* force length to zero to avoid ASN1 encoding */ |
| 4965 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4966 | |
| 4967 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4968 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4969 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4970 | goto err; |
| 4971 | |
| 4972 | p = encsess; |
| 4973 | |
| 4974 | /* process ASN1 session encoding before the lock */ |
| 4975 | i2d_SSL_SESSION(sess, &p); |
| 4976 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4977 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4978 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4979 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4980 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4981 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4982 | err: |
| 4983 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4984 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4985 | 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] | 4986 | |
| 4987 | return 0; /* do not increment session reference count */ |
| 4988 | } |
| 4989 | |
| 4990 | /* 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] | 4991 | 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] | 4992 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4993 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4994 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4995 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4996 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4997 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4998 | |
| 4999 | global.shctx_lookups++; |
| 5000 | |
| 5001 | /* allow the session to be freed automatically by openssl */ |
| 5002 | *do_copy = 0; |
| 5003 | |
| 5004 | /* tree key is zeros padded sessionid */ |
| 5005 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5006 | memcpy(tmpkey, key, key_len); |
| 5007 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 5008 | key = tmpkey; |
| 5009 | } |
| 5010 | |
| 5011 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5012 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5013 | |
| 5014 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5015 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 5016 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5017 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5018 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5019 | global.shctx_misses++; |
| 5020 | return NULL; |
| 5021 | } |
| 5022 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5023 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 5024 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5025 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5026 | 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] | 5027 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5028 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5029 | |
| 5030 | /* decode ASN1 session */ |
| 5031 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5032 | 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] | 5033 | /* Reset session id and session id contenxt */ |
| 5034 | if (sess) { |
| 5035 | SSL_SESSION_set1_id(sess, key, key_len); |
| 5036 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5037 | } |
| 5038 | |
| 5039 | return sess; |
| 5040 | } |
| 5041 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5042 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5043 | /* 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] | 5044 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5045 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5046 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5047 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 5048 | unsigned int sid_length; |
| 5049 | const unsigned char *sid_data; |
| 5050 | (void)ctx; |
| 5051 | |
| 5052 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 5053 | /* tree key is zeros padded sessionid */ |
| 5054 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 5055 | memcpy(tmpkey, sid_data, sid_length); |
| 5056 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 5057 | sid_data = tmpkey; |
| 5058 | } |
| 5059 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5060 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5061 | |
| 5062 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5063 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 5064 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5065 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5066 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 5070 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | /* Set session cache mode to server and disable openssl internal cache. |
| 5074 | * Set shared cache callbacks on an ssl context. |
| 5075 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5076 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5077 | { |
| 5078 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 5079 | |
| 5080 | if (!ssl_shctx) { |
| 5081 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 5082 | return; |
| 5083 | } |
| 5084 | |
| 5085 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 5086 | SSL_SESS_CACHE_NO_INTERNAL | |
| 5087 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 5088 | |
| 5089 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5090 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 5091 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 5092 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5095 | /* |
| 5096 | * This function applies the SSL configuration on a SSL_CTX |
| 5097 | * It returns an error code and fills the <err> buffer |
| 5098 | */ |
| 5099 | 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] | 5100 | { |
| 5101 | struct proxy *curproxy = bind_conf->frontend; |
| 5102 | int cfgerr = 0; |
| 5103 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5104 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5105 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5106 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5107 | const char *conf_ciphersuites; |
| 5108 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5109 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5110 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5111 | if (ssl_conf) { |
| 5112 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5113 | int i, min, max; |
| 5114 | int flags = MC_SSL_O_ALL; |
| 5115 | |
| 5116 | /* 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] | 5117 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5118 | 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] | 5119 | if (min) |
| 5120 | flags |= (methodVersions[min].flag - 1); |
| 5121 | if (max) |
| 5122 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5123 | min = max = CONF_TLSV_NONE; |
| 5124 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5125 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5126 | if (min) |
| 5127 | max = i; |
| 5128 | else |
| 5129 | min = max = i; |
| 5130 | } |
| 5131 | /* save real min/max */ |
| 5132 | conf_ssl_methods->min = min; |
| 5133 | conf_ssl_methods->max = max; |
| 5134 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5135 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5136 | 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] | 5137 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5138 | } |
| 5139 | } |
| 5140 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5141 | 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] | 5142 | case SSL_SOCK_VERIFY_NONE: |
| 5143 | verify = SSL_VERIFY_NONE; |
| 5144 | break; |
| 5145 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5146 | verify = SSL_VERIFY_PEER; |
| 5147 | break; |
| 5148 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5149 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5150 | break; |
| 5151 | } |
| 5152 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5153 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5154 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 5155 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 5156 | if (ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5157 | /* set CAfile to verify */ |
| 5158 | if (!ssl_set_verify_locations_file(ctx, ca_file)) { |
| 5159 | 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] | 5160 | 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] | 5161 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5162 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 5163 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 5164 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5165 | 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] | 5166 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5167 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5168 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5169 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5170 | 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] | 5171 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5172 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5173 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5174 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5175 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5176 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5177 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5178 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5179 | 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] | 5180 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5181 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5182 | else { |
| 5183 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5184 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5185 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5186 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5187 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5188 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5189 | #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] | 5190 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5191 | 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] | 5192 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5193 | 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] | 5194 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | #endif |
| 5198 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5199 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5200 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5201 | if (conf_ciphers && |
| 5202 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5203 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5204 | 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] | 5205 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5206 | } |
| 5207 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5208 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5209 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5210 | if (conf_ciphersuites && |
| 5211 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5212 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5213 | 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] | 5214 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5215 | } |
| 5216 | #endif |
| 5217 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5218 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5219 | /* If tune.ssl.default-dh-param has not been set, |
| 5220 | neither has ssl-default-dh-file and no static DH |
| 5221 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5222 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 5223 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 5224 | (ssl_dh_ptr_index == -1 || |
| 5225 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5226 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 5227 | const SSL_CIPHER * cipher = NULL; |
| 5228 | char cipher_description[128]; |
| 5229 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 5230 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 5231 | which is not ephemeral DH. */ |
| 5232 | const char dhe_description[] = " Kx=DH "; |
| 5233 | const char dhe_export_description[] = " Kx=DH("; |
| 5234 | int idx = 0; |
| 5235 | int dhe_found = 0; |
| 5236 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5237 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5238 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5239 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5240 | if (ssl) { |
| 5241 | ciphers = SSL_get_ciphers(ssl); |
| 5242 | |
| 5243 | if (ciphers) { |
| 5244 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 5245 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 5246 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 5247 | if (strstr(cipher_description, dhe_description) != NULL || |
| 5248 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 5249 | dhe_found = 1; |
| 5250 | break; |
| 5251 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 5252 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5253 | } |
| 5254 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 5255 | SSL_free(ssl); |
| 5256 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5257 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5258 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 5259 | if (dhe_found) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5260 | 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", |
| 5261 | err && *err ? *err : ""); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5262 | cfgerr |= ERR_WARN; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5263 | } |
| 5264 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5265 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5266 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5267 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5268 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5269 | if (local_dh_1024 == NULL) { |
| 5270 | local_dh_1024 = ssl_get_dh_1024(); |
| 5271 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5272 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5273 | if (local_dh_2048 == NULL) { |
| 5274 | local_dh_2048 = ssl_get_dh_2048(); |
| 5275 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5276 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5277 | if (local_dh_4096 == NULL) { |
| 5278 | local_dh_4096 = ssl_get_dh_4096(); |
| 5279 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5280 | } |
| 5281 | } |
| 5282 | } |
| 5283 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5284 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5285 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5286 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5287 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5288 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5289 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5290 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5291 | ssl_conf_cur = NULL; |
| 5292 | if (ssl_conf && ssl_conf->npn_str) |
| 5293 | ssl_conf_cur = ssl_conf; |
| 5294 | else if (bind_conf->ssl_conf.npn_str) |
| 5295 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5296 | if (ssl_conf_cur) |
| 5297 | 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] | 5298 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5299 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5300 | ssl_conf_cur = NULL; |
| 5301 | if (ssl_conf && ssl_conf->alpn_str) |
| 5302 | ssl_conf_cur = ssl_conf; |
| 5303 | else if (bind_conf->ssl_conf.alpn_str) |
| 5304 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5305 | if (ssl_conf_cur) |
| 5306 | 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] | 5307 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 5308 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5309 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5310 | if (conf_curves) { |
| 5311 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5312 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5313 | 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] | 5314 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5315 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5316 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5317 | } |
| 5318 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5319 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5320 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5321 | int i; |
| 5322 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5323 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5324 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5325 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5326 | NULL); |
| 5327 | |
| 5328 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 5329 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5330 | return cfgerr; |
| 5331 | } |
| 5332 | #else |
| 5333 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5334 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5335 | ECDHE_DEFAULT_CURVE); |
| 5336 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5337 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5338 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5339 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5340 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5341 | 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] | 5342 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5343 | } |
| 5344 | else { |
| 5345 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5346 | EC_KEY_free(ecdh); |
| 5347 | } |
| 5348 | } |
| 5349 | #endif |
| 5350 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5351 | return cfgerr; |
| 5352 | } |
| 5353 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5354 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5355 | { |
| 5356 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5357 | size_t prefixlen, suffixlen; |
| 5358 | |
| 5359 | /* Trivial case */ |
| 5360 | if (strcmp(pattern, hostname) == 0) |
| 5361 | return 1; |
| 5362 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5363 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5364 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5365 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5366 | pattern_wildcard = NULL; |
| 5367 | pattern_left_label_end = pattern; |
| 5368 | while (*pattern_left_label_end != '.') { |
| 5369 | switch (*pattern_left_label_end) { |
| 5370 | case 0: |
| 5371 | /* End of label not found */ |
| 5372 | return 0; |
| 5373 | case '*': |
| 5374 | /* If there is more than one wildcards */ |
| 5375 | if (pattern_wildcard) |
| 5376 | return 0; |
| 5377 | pattern_wildcard = pattern_left_label_end; |
| 5378 | break; |
| 5379 | } |
| 5380 | pattern_left_label_end++; |
| 5381 | } |
| 5382 | |
| 5383 | /* If it's not trivial and there is no wildcard, it can't |
| 5384 | * match */ |
| 5385 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5386 | return 0; |
| 5387 | |
| 5388 | /* Make sure all labels match except the leftmost */ |
| 5389 | hostname_left_label_end = strchr(hostname, '.'); |
| 5390 | if (!hostname_left_label_end |
| 5391 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5392 | return 0; |
| 5393 | |
| 5394 | /* Make sure the leftmost label of the hostname is long enough |
| 5395 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5396 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5397 | return 0; |
| 5398 | |
| 5399 | /* Finally compare the string on either side of the |
| 5400 | * wildcard */ |
| 5401 | prefixlen = pattern_wildcard - pattern; |
| 5402 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5403 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5404 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5405 | return 0; |
| 5406 | |
| 5407 | return 1; |
| 5408 | } |
| 5409 | |
| 5410 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5411 | { |
| 5412 | SSL *ssl; |
| 5413 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5414 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5415 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5416 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5417 | |
| 5418 | int depth; |
| 5419 | X509 *cert; |
| 5420 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5421 | int i; |
| 5422 | X509_NAME *cert_subject; |
| 5423 | char *str; |
| 5424 | |
| 5425 | if (ok == 0) |
| 5426 | return ok; |
| 5427 | |
| 5428 | 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] | 5429 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5430 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5431 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5432 | /* We're checking if the provided hostnames match the desired one. The |
| 5433 | * desired hostname comes from the SNI we presented if any, or if not |
| 5434 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5435 | * directive. If neither is set, we don't care about the name so the |
| 5436 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5437 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5438 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5439 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5440 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5441 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5442 | if (!servername) |
| 5443 | return ok; |
| 5444 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5445 | |
| 5446 | /* We only need to verify the CN on the actual server cert, |
| 5447 | * not the indirect CAs */ |
| 5448 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5449 | if (depth != 0) |
| 5450 | return ok; |
| 5451 | |
| 5452 | /* At this point, the cert is *not* OK unless we can find a |
| 5453 | * hostname match */ |
| 5454 | ok = 0; |
| 5455 | |
| 5456 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5457 | /* It seems like this might happen if verify peer isn't set */ |
| 5458 | if (!cert) |
| 5459 | return ok; |
| 5460 | |
| 5461 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5462 | if (alt_names) { |
| 5463 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5464 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5465 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5466 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5467 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5468 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5469 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5470 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5471 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5472 | OPENSSL_free(str); |
| 5473 | } |
| 5474 | } |
| 5475 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5476 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | cert_subject = X509_get_subject_name(cert); |
| 5480 | i = -1; |
| 5481 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5482 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5483 | ASN1_STRING *value; |
| 5484 | value = X509_NAME_ENTRY_get_data(entry); |
| 5485 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5486 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5487 | OPENSSL_free(str); |
| 5488 | } |
| 5489 | } |
| 5490 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5491 | /* report the mismatch and indicate if SNI was used or not */ |
| 5492 | if (!ok && !conn->err_code) |
| 5493 | 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] | 5494 | return ok; |
| 5495 | } |
| 5496 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5497 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5498 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5499 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5500 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5501 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5502 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5503 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5504 | SSL_OP_NO_SSLv2 | |
| 5505 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5506 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5507 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5508 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5509 | SSL_MODE_RELEASE_BUFFERS | |
| 5510 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5511 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5512 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5513 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5514 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5515 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5516 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5517 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5518 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5519 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5520 | cfgerr++; |
| 5521 | } |
| 5522 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5523 | /* Automatic memory computations need to know we use SSL there */ |
| 5524 | global.ssl_used_backend = 1; |
| 5525 | |
| 5526 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5527 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5528 | 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] | 5529 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5530 | curproxy->id, srv->id, |
| 5531 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5532 | cfgerr++; |
| 5533 | return cfgerr; |
| 5534 | } |
| 5535 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5536 | if (srv->use_ssl) |
| 5537 | srv->xprt = &ssl_sock; |
| 5538 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 5539 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5540 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5541 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5542 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5543 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5544 | proxy_type_str(curproxy), curproxy->id, |
| 5545 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5546 | cfgerr++; |
| 5547 | return cfgerr; |
| 5548 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5549 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5550 | 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] | 5551 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5552 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5553 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5554 | else |
| 5555 | flags = conf_ssl_methods->flags; |
| 5556 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5557 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5558 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5559 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5560 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5561 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5562 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5563 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5564 | min = max = CONF_TLSV_NONE; |
| 5565 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5566 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5567 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5568 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5569 | if (min) { |
| 5570 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5571 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5572 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5573 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5574 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5575 | hole = 0; |
| 5576 | } |
| 5577 | max = i; |
| 5578 | } |
| 5579 | else { |
| 5580 | min = max = i; |
| 5581 | } |
| 5582 | } |
| 5583 | else { |
| 5584 | if (min) |
| 5585 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5586 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5587 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5588 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5589 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5590 | cfgerr += 1; |
| 5591 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5592 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5593 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5594 | /* 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] | 5595 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5596 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5597 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5598 | else |
| 5599 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5600 | if (flags & methodVersions[i].flag) |
| 5601 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5602 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5603 | /* 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] | 5604 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5605 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5606 | #endif |
| 5607 | |
| 5608 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5609 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5610 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5611 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5612 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5613 | if (global_ssl.async) |
| 5614 | mode |= SSL_MODE_ASYNC; |
| 5615 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5616 | SSL_CTX_set_mode(ctx, mode); |
| 5617 | srv->ssl_ctx.ctx = ctx; |
| 5618 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5619 | if (srv->ssl_ctx.client_crt) { |
| 5620 | 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] | 5621 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 5622 | proxy_type_str(curproxy), curproxy->id, |
| 5623 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5624 | cfgerr++; |
| 5625 | } |
| 5626 | 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] | 5627 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 5628 | proxy_type_str(curproxy), curproxy->id, |
| 5629 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5630 | cfgerr++; |
| 5631 | } |
| 5632 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5633 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 5634 | proxy_type_str(curproxy), curproxy->id, |
| 5635 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5636 | cfgerr++; |
| 5637 | } |
| 5638 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5639 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5640 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5641 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5642 | switch (srv->ssl_ctx.verify) { |
| 5643 | case SSL_SOCK_VERIFY_NONE: |
| 5644 | verify = SSL_VERIFY_NONE; |
| 5645 | break; |
| 5646 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5647 | verify = SSL_VERIFY_PEER; |
| 5648 | break; |
| 5649 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5650 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5651 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5652 | (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] | 5653 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5654 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5655 | /* set CAfile to verify */ |
| 5656 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 5657 | 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] | 5658 | curproxy->id, srv->id, |
| 5659 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5660 | cfgerr++; |
| 5661 | } |
| 5662 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5663 | else { |
| 5664 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5665 | 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", |
| 5666 | curproxy->id, srv->id, |
| 5667 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5668 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5669 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 5670 | curproxy->id, srv->id, |
| 5671 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5672 | cfgerr++; |
| 5673 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5674 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5675 | if (srv->ssl_ctx.crl_file) { |
| 5676 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 5677 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5678 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5679 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 5680 | curproxy->id, srv->id, |
| 5681 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5682 | cfgerr++; |
| 5683 | } |
| 5684 | else { |
| 5685 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5686 | } |
| 5687 | } |
| 5688 | #endif |
| 5689 | } |
| 5690 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5691 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 5692 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5693 | 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] | 5694 | if (srv->ssl_ctx.ciphers && |
| 5695 | !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] | 5696 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 5697 | curproxy->id, srv->id, |
| 5698 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5699 | cfgerr++; |
| 5700 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5701 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5702 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5703 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 5704 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5705 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5706 | curproxy->id, srv->id, |
| 5707 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 5708 | cfgerr++; |
| 5709 | } |
| 5710 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5711 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5712 | if (srv->ssl_ctx.npn_str) |
| 5713 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 5714 | #endif |
| 5715 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5716 | if (srv->ssl_ctx.alpn_str) |
| 5717 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5718 | #endif |
| 5719 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5720 | |
| 5721 | return cfgerr; |
| 5722 | } |
| 5723 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5724 | /* 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] | 5725 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5726 | * encountered. |
| 5727 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5728 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5729 | { |
| 5730 | struct ebmb_node *node; |
| 5731 | struct sni_ctx *sni; |
| 5732 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5733 | int errcode = 0; |
| 5734 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5735 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5736 | /* Automatic memory computations need to know we use SSL there */ |
| 5737 | global.ssl_used_frontend = 1; |
| 5738 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5739 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5740 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5741 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5742 | err++; |
| 5743 | } |
| 5744 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5745 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5746 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5747 | /* It should not be necessary to call this function, but it's |
| 5748 | necessary first to check and move all initialisation related |
| 5749 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5750 | 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] | 5751 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5752 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5753 | 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] | 5754 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5755 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5756 | while (node) { |
| 5757 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5758 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 5759 | /* only initialize the CTX on its first occurrence and |
| 5760 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5761 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5762 | node = ebmb_next(node); |
| 5763 | } |
| 5764 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5765 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5766 | while (node) { |
| 5767 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5768 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5769 | /* only initialize the CTX on its first occurrence and |
| 5770 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5771 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 5772 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5773 | node = ebmb_next(node); |
| 5774 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5775 | |
| 5776 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5777 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5778 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5779 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5780 | err++; |
| 5781 | } |
| 5782 | |
| 5783 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5784 | return err; |
| 5785 | } |
| 5786 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5787 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5788 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5789 | * alerts are directly emitted since the rest of the stack does it below. |
| 5790 | */ |
| 5791 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5792 | { |
| 5793 | struct proxy *px = bind_conf->frontend; |
| 5794 | int alloc_ctx; |
| 5795 | int err; |
| 5796 | |
| 5797 | if (!bind_conf->is_ssl) { |
| 5798 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5799 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5800 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5801 | } |
| 5802 | return 0; |
| 5803 | } |
| 5804 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5805 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5806 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5807 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5808 | } |
| 5809 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5810 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5811 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5812 | return -1; |
| 5813 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5814 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5815 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5816 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5817 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5818 | sizeof(*sh_ssl_sess_tree), |
| 5819 | ((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] | 5820 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5821 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5822 | 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"); |
| 5823 | else |
| 5824 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5825 | return -1; |
| 5826 | } |
| 5827 | /* free block callback */ |
| 5828 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5829 | /* init the root tree within the extra space */ |
| 5830 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5831 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5832 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5833 | err = 0; |
| 5834 | /* initialize all certificate contexts */ |
| 5835 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5836 | |
| 5837 | /* initialize CA variables if the certificates generation is enabled */ |
| 5838 | err += ssl_sock_load_ca(bind_conf); |
| 5839 | |
| 5840 | return -err; |
| 5841 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5842 | |
| 5843 | /* release ssl context allocated for servers. */ |
| 5844 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5845 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5846 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5847 | if (srv->ssl_ctx.alpn_str) |
| 5848 | free(srv->ssl_ctx.alpn_str); |
| 5849 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5850 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5851 | if (srv->ssl_ctx.npn_str) |
| 5852 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5853 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5854 | if (srv->ssl_ctx.ctx) |
| 5855 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 5856 | } |
| 5857 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5858 | /* 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] | 5859 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5860 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5861 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5862 | { |
| 5863 | struct ebmb_node *node, *back; |
| 5864 | struct sni_ctx *sni; |
| 5865 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5866 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5867 | while (node) { |
| 5868 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5869 | back = ebmb_next(node); |
| 5870 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5871 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5872 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5873 | ssl_sock_free_ssl_conf(sni->conf); |
| 5874 | free(sni->conf); |
| 5875 | sni->conf = NULL; |
| 5876 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5877 | free(sni); |
| 5878 | node = back; |
| 5879 | } |
| 5880 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5881 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5882 | while (node) { |
| 5883 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5884 | back = ebmb_next(node); |
| 5885 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5886 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5887 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5888 | ssl_sock_free_ssl_conf(sni->conf); |
| 5889 | free(sni->conf); |
| 5890 | sni->conf = NULL; |
| 5891 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5892 | free(sni); |
| 5893 | node = back; |
| 5894 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5895 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5896 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5897 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5898 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5901 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5902 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5903 | { |
| 5904 | ssl_sock_free_ca(bind_conf); |
| 5905 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5906 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5907 | free(bind_conf->ca_sign_file); |
| 5908 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5909 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5910 | free(bind_conf->keys_ref->filename); |
| 5911 | free(bind_conf->keys_ref->tlskeys); |
| 5912 | LIST_DEL(&bind_conf->keys_ref->list); |
| 5913 | free(bind_conf->keys_ref); |
| 5914 | } |
| 5915 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5916 | bind_conf->ca_sign_pass = NULL; |
| 5917 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5918 | } |
| 5919 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5920 | /* Load CA cert file and private key used to generate certificates */ |
| 5921 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5922 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5923 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5924 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5925 | FILE *fp; |
| 5926 | X509 *cacert = NULL; |
| 5927 | EVP_PKEY *capkey = NULL; |
| 5928 | int err = 0; |
| 5929 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5930 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5931 | return err; |
| 5932 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5933 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5934 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5935 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5936 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5937 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5938 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5939 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5940 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5941 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5942 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5943 | "no CA certificate File configured at [%s:%d].\n", |
| 5944 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5945 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5946 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5947 | |
| 5948 | /* read in the CA certificate */ |
| 5949 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5950 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5951 | 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] | 5952 | goto load_error; |
| 5953 | } |
| 5954 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5955 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5956 | 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] | 5957 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5958 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5959 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5960 | 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] | 5961 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5962 | 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] | 5963 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5964 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5965 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5966 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5967 | bind_conf->ca_sign_cert = cacert; |
| 5968 | bind_conf->ca_sign_pkey = capkey; |
| 5969 | return err; |
| 5970 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5971 | read_error: |
| 5972 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5973 | if (capkey) EVP_PKEY_free(capkey); |
| 5974 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5975 | load_error: |
| 5976 | bind_conf->generate_certs = 0; |
| 5977 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5978 | return err; |
| 5979 | } |
| 5980 | |
| 5981 | /* Release CA cert and private key used to generate certificated */ |
| 5982 | void |
| 5983 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5984 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5985 | if (bind_conf->ca_sign_pkey) |
| 5986 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5987 | if (bind_conf->ca_sign_cert) |
| 5988 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5989 | bind_conf->ca_sign_pkey = NULL; |
| 5990 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5991 | } |
| 5992 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5993 | /* |
| 5994 | * This function is called if SSL * context is not yet allocated. The function |
| 5995 | * is designed to be called before any other data-layer operation and sets the |
| 5996 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5997 | * It returns 0 on success and -1 in error case. |
| 5998 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5999 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6000 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6001 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6002 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6003 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6004 | return 0; |
| 6005 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6006 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6007 | return 0; |
| 6008 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6009 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6010 | if (!ctx) { |
| 6011 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6012 | return -1; |
| 6013 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6014 | ctx->wait_event.tasklet = tasklet_new(); |
| 6015 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6016 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6017 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6018 | return -1; |
| 6019 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6020 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6021 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6022 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6023 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6024 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6025 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6026 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6027 | ctx->xprt_st = 0; |
| 6028 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6029 | |
| 6030 | /* Only work with sockets for now, this should be adapted when we'll |
| 6031 | * add QUIC support. |
| 6032 | */ |
| 6033 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6034 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6035 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6036 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6037 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6038 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6039 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 6040 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6041 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6042 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6043 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6044 | /* If it is in client mode initiate SSL session |
| 6045 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6046 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6047 | int may_retry = 1; |
| 6048 | |
| 6049 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6050 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6051 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 6052 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6053 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6054 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6055 | goto retry_connect; |
| 6056 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6057 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6058 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6059 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6060 | ctx->bio = BIO_new(ha_meth); |
| 6061 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6062 | SSL_free(ctx->ssl); |
| 6063 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6064 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6065 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6066 | goto retry_connect; |
| 6067 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6068 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6069 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6070 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6071 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6072 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6073 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6074 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6075 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6076 | SSL_free(ctx->ssl); |
| 6077 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6078 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6079 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6080 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6081 | goto retry_connect; |
| 6082 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6083 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6084 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6085 | } |
| 6086 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6087 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6088 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6089 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6090 | 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] | 6091 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6092 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6093 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6094 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6095 | } else if (sess) { |
| 6096 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6097 | } |
| 6098 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6099 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6100 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6101 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6102 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6103 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6104 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6105 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6106 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6107 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6108 | return 0; |
| 6109 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6110 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6111 | int may_retry = 1; |
| 6112 | |
| 6113 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6114 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6115 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 6116 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6117 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6118 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6119 | goto retry_accept; |
| 6120 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6121 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6122 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6123 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6124 | ctx->bio = BIO_new(ha_meth); |
| 6125 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 6126 | SSL_free(ctx->ssl); |
| 6127 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6128 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6129 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6130 | goto retry_accept; |
| 6131 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6132 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6133 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6134 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6135 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6136 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6137 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6138 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6139 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 6140 | SSL_free(ctx->ssl); |
| 6141 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6142 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6143 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6144 | goto retry_accept; |
| 6145 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6146 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6147 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6148 | } |
| 6149 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6150 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6151 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 6152 | b_alloc(&ctx->early_buf); |
| 6153 | SSL_set_max_early_data(ctx->ssl, |
| 6154 | /* Only allow early data if we managed to allocate |
| 6155 | * a buffer. |
| 6156 | */ |
| 6157 | (!b_is_null(&ctx->early_buf)) ? |
| 6158 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6159 | } |
| 6160 | #endif |
| 6161 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6162 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6163 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6164 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6165 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6166 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6167 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 6168 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6169 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6170 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 6171 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6172 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6173 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6174 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6175 | return 0; |
| 6176 | } |
| 6177 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6178 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6179 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6180 | if (ctx && ctx->wait_event.tasklet) |
| 6181 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6182 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6183 | return -1; |
| 6184 | } |
| 6185 | |
| 6186 | |
| 6187 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6188 | * updates the FD status if it wants some polling before being called again. |
| 6189 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6190 | * otherwise it returns non-zero and removes itself from the connection's |
| 6191 | * flags (the bit is provided in <flag> by the caller). |
| 6192 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6193 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6194 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6195 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6196 | int ret; |
| 6197 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6198 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6199 | return 0; |
| 6200 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6201 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6202 | goto out_error; |
| 6203 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6204 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6205 | /* |
| 6206 | * Check if we have early data. If we do, we have to read them |
| 6207 | * before SSL_do_handshake() is called, And there's no way to |
| 6208 | * detect early data, except to try to read them |
| 6209 | */ |
| 6210 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6211 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6212 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6213 | while (1) { |
| 6214 | ret = SSL_read_early_data(ctx->ssl, |
| 6215 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6216 | &read_data); |
| 6217 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6218 | goto check_error; |
| 6219 | if (read_data > 0) { |
| 6220 | conn->flags |= CO_FL_EARLY_DATA; |
| 6221 | b_add(&ctx->early_buf, read_data); |
| 6222 | } |
| 6223 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6224 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6225 | if (!b_data(&ctx->early_buf)) |
| 6226 | b_free(&ctx->early_buf); |
| 6227 | break; |
| 6228 | } |
| 6229 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6230 | } |
| 6231 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6232 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6233 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6234 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6235 | * the reneg handshake. |
| 6236 | * Here we use SSL_peek as a workaround for reneg. |
| 6237 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6238 | 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] | 6239 | char c; |
| 6240 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6241 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6242 | if (ret <= 0) { |
| 6243 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6244 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6245 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6246 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6247 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6248 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6249 | 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] | 6250 | return 0; |
| 6251 | } |
| 6252 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6253 | /* handshake may have been completed but we have |
| 6254 | * no more data to read. |
| 6255 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6256 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6257 | ret = 1; |
| 6258 | goto reneg_ok; |
| 6259 | } |
| 6260 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6261 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6262 | 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] | 6263 | return 0; |
| 6264 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6265 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6266 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6267 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6268 | return 0; |
| 6269 | } |
| 6270 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6271 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6272 | /* if errno is null, then connection was successfully established */ |
| 6273 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6274 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6275 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6276 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6277 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6278 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6279 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6280 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6281 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6282 | /* 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] | 6283 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6284 | empty_handshake = state == TLS_ST_BEFORE; |
| 6285 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6286 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6287 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6288 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6289 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6290 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6291 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6292 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6293 | else |
| 6294 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6295 | } |
| 6296 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6297 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6298 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6299 | else |
| 6300 | conn->err_code = CO_ER_SSL_ABORT; |
| 6301 | } |
| 6302 | } |
| 6303 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6304 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6305 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6306 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6307 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6308 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6309 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6310 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6311 | goto out_error; |
| 6312 | } |
| 6313 | else { |
| 6314 | /* Fail on all other handshake errors */ |
| 6315 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6316 | * buffer, causing an RST to be emitted upon close() on |
| 6317 | * TCP sockets. We first try to drain possibly pending |
| 6318 | * data to avoid this as much as possible. |
| 6319 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6320 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6321 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6322 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6323 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6324 | goto out_error; |
| 6325 | } |
| 6326 | } |
| 6327 | /* read some data: consider handshake completed */ |
| 6328 | goto reneg_ok; |
| 6329 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6330 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6331 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6332 | if (ret != 1) { |
| 6333 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6334 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6335 | |
| 6336 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6337 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6338 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6339 | 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] | 6340 | return 0; |
| 6341 | } |
| 6342 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6343 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6344 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6345 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6346 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6347 | return 0; |
| 6348 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6349 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6350 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6351 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6352 | return 0; |
| 6353 | } |
| 6354 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6355 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6356 | /* if errno is null, then connection was successfully established */ |
| 6357 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6358 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6359 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6360 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6361 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6362 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6363 | #else |
| 6364 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6365 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6366 | /* 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] | 6367 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6368 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6369 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6370 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6371 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6372 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6373 | if (empty_handshake) { |
| 6374 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6375 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6376 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6377 | else |
| 6378 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6379 | } |
| 6380 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6381 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6382 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6383 | else |
| 6384 | conn->err_code = CO_ER_SSL_ABORT; |
| 6385 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6386 | } |
| 6387 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6388 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6389 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6390 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6391 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6392 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6393 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6394 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6395 | goto out_error; |
| 6396 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6397 | else { |
| 6398 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6399 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6400 | * buffer, causing an RST to be emitted upon close() on |
| 6401 | * TCP sockets. We first try to drain possibly pending |
| 6402 | * data to avoid this as much as possible. |
| 6403 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6404 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6405 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6406 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6407 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6408 | goto out_error; |
| 6409 | } |
| 6410 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6411 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6412 | else { |
| 6413 | /* |
| 6414 | * If the server refused the early data, we have to send a |
| 6415 | * 425 to the client, as we no longer have the data to sent |
| 6416 | * them again. |
| 6417 | */ |
| 6418 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6419 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6420 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6421 | goto out_error; |
| 6422 | } |
| 6423 | } |
| 6424 | } |
| 6425 | #endif |
| 6426 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6427 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6428 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6429 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6430 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6431 | /* ASYNC engine API doesn't support moving read/write |
| 6432 | * buffers. So we disable ASYNC mode right after |
| 6433 | * the handshake to avoid buffer oveflows. |
| 6434 | */ |
| 6435 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6436 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6437 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6438 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6439 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6440 | if (objt_server(conn->target)) { |
| 6441 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6442 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6443 | 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] | 6444 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6445 | else { |
| 6446 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6447 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6448 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6449 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | /* The connection is now established at both layers, it's time to leave */ |
| 6453 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6454 | return 1; |
| 6455 | |
| 6456 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6457 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6458 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6459 | ERR_clear_error(); |
| 6460 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6461 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6462 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6463 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6464 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6465 | } |
| 6466 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6467 | /* Fail on all other handshake errors */ |
| 6468 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6469 | if (!conn->err_code) |
| 6470 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6471 | return 0; |
| 6472 | } |
| 6473 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6474 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6475 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6476 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6477 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6478 | * unless the transport layer was already released. |
| 6479 | */ |
| 6480 | 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] | 6481 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6482 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6483 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6484 | if (!ctx) |
| 6485 | return -1; |
| 6486 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6487 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 6488 | BUG_ON(ctx->subs && ctx->subs->events & event_type); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6489 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6490 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6491 | ctx->subs = es; |
| 6492 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6493 | |
| 6494 | /* we may have to subscribe to lower layers for new events */ |
| 6495 | event_type &= ~ctx->wait_event.events; |
| 6496 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6497 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6498 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6499 | } |
| 6500 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6501 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6502 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6503 | * subscribe() call. It always returns zero. |
| 6504 | */ |
| 6505 | 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] | 6506 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6507 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6508 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6509 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6510 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6511 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6512 | es->events &= ~event_type; |
| 6513 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6514 | ctx->subs = NULL; |
| 6515 | |
| 6516 | /* If we subscribed, and we're not doing the handshake, |
| 6517 | * then we subscribed because the upper layer asked for it, |
| 6518 | * as the upper layer is no longer interested, we can |
| 6519 | * unsubscribe too. |
| 6520 | */ |
| 6521 | event_type &= ctx->wait_event.events; |
| 6522 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6523 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6524 | |
| 6525 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6526 | } |
| 6527 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6528 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6529 | * Returns 0 on success, and non-zero on failure. |
| 6530 | */ |
| 6531 | 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) |
| 6532 | { |
| 6533 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6534 | |
| 6535 | if (oldxprt_ops != NULL) |
| 6536 | *oldxprt_ops = ctx->xprt; |
| 6537 | if (oldxprt_ctx != NULL) |
| 6538 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6539 | ctx->xprt = toadd_ops; |
| 6540 | ctx->xprt_ctx = toadd_ctx; |
| 6541 | return 0; |
| 6542 | } |
| 6543 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6544 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6545 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6546 | * XPRT. |
| 6547 | */ |
| 6548 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6549 | { |
| 6550 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6551 | |
| 6552 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6553 | ctx->xprt_ctx = newctx; |
| 6554 | ctx->xprt = newops; |
| 6555 | return 0; |
| 6556 | } |
| 6557 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6558 | } |
| 6559 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6560 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6561 | { |
| 6562 | struct ssl_sock_ctx *ctx = context; |
| 6563 | |
| 6564 | /* First if we're doing an handshake, try that */ |
| 6565 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6566 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6567 | /* If we had an error, or the handshake is done and I/O is available, |
| 6568 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6569 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6570 | * we can't be sure conn_fd_handler() will be called again. |
| 6571 | */ |
| 6572 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6573 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6574 | int ret = 0; |
| 6575 | int woke = 0; |
| 6576 | |
| 6577 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6578 | if (ctx->subs) { |
| 6579 | tasklet_wakeup(ctx->subs->tasklet); |
| 6580 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6581 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6582 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6583 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6584 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6585 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6586 | * upper layers know. If we have no mux, create it, |
| 6587 | * and once we have a mux, call its wake method if we didn't |
| 6588 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6589 | */ |
| 6590 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6591 | if (!ctx->conn->mux) |
| 6592 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6593 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6594 | ctx->conn->mux->wake(ctx->conn); |
| 6595 | return NULL; |
| 6596 | } |
| 6597 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6598 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6599 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6600 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6601 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6602 | tasklet_wakeup(ctx->subs->tasklet); |
| 6603 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6604 | if (!ctx->subs->events) |
| 6605 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6606 | } |
| 6607 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6608 | return NULL; |
| 6609 | } |
| 6610 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6611 | /* 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] | 6612 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6613 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6614 | * flags are updated with whatever special event is detected (error, read0, |
| 6615 | * empty). The caller is responsible for taking care of those events and |
| 6616 | * avoiding the call if inappropriate. The function does not call the |
| 6617 | * connection's polling update function, so the caller is responsible for this. |
| 6618 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6619 | 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] | 6620 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6621 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6622 | ssize_t ret; |
| 6623 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6624 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6625 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6626 | goto out_error; |
| 6627 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6628 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6629 | if (b_data(&ctx->early_buf)) { |
| 6630 | try = b_contig_space(buf); |
| 6631 | if (try > b_data(&ctx->early_buf)) |
| 6632 | try = b_data(&ctx->early_buf); |
| 6633 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6634 | b_add(buf, try); |
| 6635 | b_del(&ctx->early_buf, try); |
| 6636 | if (b_data(&ctx->early_buf) == 0) |
| 6637 | b_free(&ctx->early_buf); |
| 6638 | return try; |
| 6639 | } |
| 6640 | #endif |
| 6641 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6642 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6643 | /* a handshake was requested */ |
| 6644 | return 0; |
| 6645 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6646 | /* read the largest possible block. For this, we perform only one call |
| 6647 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6648 | * in which case we accept to do it once again. A new attempt is made on |
| 6649 | * EINTR too. |
| 6650 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6651 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6652 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6653 | try = b_contig_space(buf); |
| 6654 | if (!try) |
| 6655 | break; |
| 6656 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6657 | if (try > count) |
| 6658 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6659 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6660 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6661 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6662 | if (conn->flags & CO_FL_ERROR) { |
| 6663 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6664 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6665 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6666 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6667 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6668 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6669 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6670 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6671 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6672 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6673 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6674 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6675 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6676 | 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] | 6677 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6678 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6679 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6680 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6681 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6682 | break; |
| 6683 | } |
| 6684 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6685 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6686 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6687 | SUB_RETRY_RECV, |
| 6688 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6689 | /* handshake is running, and it may need to re-enable read */ |
| 6690 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6691 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6692 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6693 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6694 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6695 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6696 | break; |
| 6697 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6698 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6699 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6700 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6701 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6702 | * stack before shutting down the connection for |
| 6703 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6704 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 6705 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6706 | /* otherwise it's a real error */ |
| 6707 | goto out_error; |
| 6708 | } |
| 6709 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6710 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6711 | return done; |
| 6712 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6713 | clear_ssl_error: |
| 6714 | /* Clear openssl global errors stack */ |
| 6715 | ssl_sock_dump_errors(conn); |
| 6716 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6717 | read0: |
| 6718 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6719 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6720 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6721 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6722 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6723 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6724 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6725 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6726 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6727 | } |
| 6728 | |
| 6729 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6730 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6731 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6732 | * 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] | 6733 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6734 | * a second call may be performed. The connection's flags are updated with |
| 6735 | * whatever special event is detected (error, empty). The caller is responsible |
| 6736 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6737 | * 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] | 6738 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6739 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6740 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6741 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6742 | 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] | 6743 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6744 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6745 | ssize_t ret; |
| 6746 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6747 | |
| 6748 | done = 0; |
| 6749 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6750 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6751 | goto out_error; |
| 6752 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6753 | 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] | 6754 | /* a handshake was requested */ |
| 6755 | return 0; |
| 6756 | |
| 6757 | /* send the largest possible block. For this we perform only one call |
| 6758 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6759 | * in which case we accept to do it once again. |
| 6760 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6761 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6762 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6763 | size_t written_data; |
| 6764 | #endif |
| 6765 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6766 | try = b_contig_data(buf, done); |
| 6767 | if (try > count) |
| 6768 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6769 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6770 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6771 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6772 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6773 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6774 | } |
| 6775 | else { |
| 6776 | /* we need to keep the information about the fact that |
| 6777 | * we're not limiting the upcoming send(), because if it |
| 6778 | * fails, we'll have to retry with at least as many data. |
| 6779 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6780 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6781 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6782 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6783 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6784 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6785 | unsigned int max_early; |
| 6786 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6787 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6788 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6789 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6790 | if (SSL_get0_session(ctx->ssl)) |
| 6791 | 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] | 6792 | else |
| 6793 | max_early = 0; |
| 6794 | } |
| 6795 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6796 | if (try + ctx->sent_early_data > max_early) { |
| 6797 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6798 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6799 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6800 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6801 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6802 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6803 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6804 | 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] | 6805 | if (ret == 1) { |
| 6806 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6807 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6808 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6809 | 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] | 6810 | /* Initiate the handshake, now */ |
| 6811 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6812 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6813 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6814 | } |
| 6815 | |
| 6816 | } else |
| 6817 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6818 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6819 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6820 | if (conn->flags & CO_FL_ERROR) { |
| 6821 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6822 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6823 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6824 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6825 | /* A send succeeded, so we can consider ourself connected */ |
| 6826 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6827 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6828 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6829 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6830 | } |
| 6831 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6832 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6833 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6834 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6835 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6836 | /* handshake is running, and it may need to re-enable write */ |
| 6837 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6838 | 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] | 6839 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6840 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6841 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6842 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6843 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6844 | break; |
| 6845 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6846 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6847 | break; |
| 6848 | } |
| 6849 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6850 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6851 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6852 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6853 | SUB_RETRY_RECV, |
| 6854 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6855 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6856 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6857 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6858 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6859 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6860 | break; |
| 6861 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6862 | goto out_error; |
| 6863 | } |
| 6864 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6865 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6866 | return done; |
| 6867 | |
| 6868 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6869 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6870 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6871 | ERR_clear_error(); |
| 6872 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6873 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6874 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6875 | } |
| 6876 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6877 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6878 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6879 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6880 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6881 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6882 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6883 | if (ctx->wait_event.events != 0) |
| 6884 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6885 | ctx->wait_event.events, |
| 6886 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6887 | if (ctx->subs) { |
| 6888 | ctx->subs->events = 0; |
| 6889 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6890 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6891 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6892 | if (ctx->xprt->close) |
| 6893 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6894 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6895 | if (global_ssl.async) { |
| 6896 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6897 | size_t num_all_fds = 0; |
| 6898 | int i; |
| 6899 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6900 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6901 | if (num_all_fds > 32) { |
| 6902 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6903 | return; |
| 6904 | } |
| 6905 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6906 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6907 | |
| 6908 | /* If an async job is pending, we must try to |
| 6909 | to catch the end using polling before calling |
| 6910 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6911 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6912 | for (i=0 ; i < num_all_fds ; i++) { |
| 6913 | /* switch on an handler designed to |
| 6914 | * handle the SSL_free |
| 6915 | */ |
| 6916 | afd = all_fd[i]; |
| 6917 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6918 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6919 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6920 | /* To ensure that the fd cache won't be used |
| 6921 | * and we'll catch a real RD event. |
| 6922 | */ |
| 6923 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6924 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6925 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6926 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6927 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6928 | return; |
| 6929 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6930 | /* Else we can remove the fds from the fdtab |
| 6931 | * and call SSL_free. |
| 6932 | * note: we do a fd_remove and not a delete |
| 6933 | * because the fd is owned by the engine. |
| 6934 | * the engine is responsible to close |
| 6935 | */ |
| 6936 | for (i=0 ; i < num_all_fds ; i++) |
| 6937 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6938 | } |
| 6939 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6940 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6941 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6942 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6943 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6944 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6945 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6946 | } |
| 6947 | |
| 6948 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6949 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6950 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6951 | 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] | 6952 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6953 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6954 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6955 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6956 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6957 | if (!clean) |
| 6958 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6959 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6960 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6961 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6962 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6963 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6964 | ERR_clear_error(); |
| 6965 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6966 | } |
| 6967 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6968 | /* fill a buffer with the algorithm and size of a public key */ |
| 6969 | static int cert_get_pkey_algo(X509 *crt, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6970 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6971 | int bits = 0; |
| 6972 | int sig = TLSEXT_signature_anonymous; |
| 6973 | int len = -1; |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 6974 | EVP_PKEY *pkey; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6975 | |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 6976 | pkey = X509_get_pubkey(crt); |
| 6977 | if (pkey) { |
| 6978 | bits = EVP_PKEY_bits(pkey); |
| 6979 | switch(EVP_PKEY_base_id(pkey)) { |
| 6980 | case EVP_PKEY_RSA: |
| 6981 | sig = TLSEXT_signature_rsa; |
| 6982 | break; |
| 6983 | case EVP_PKEY_EC: |
| 6984 | sig = TLSEXT_signature_ecdsa; |
| 6985 | break; |
| 6986 | case EVP_PKEY_DSA: |
| 6987 | sig = TLSEXT_signature_dsa; |
| 6988 | break; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6989 | } |
Emmanuel Hocdet | c3775d2 | 2019-11-04 18:19:32 +0100 | [diff] [blame] | 6990 | EVP_PKEY_free(pkey); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | switch(sig) { |
| 6994 | case TLSEXT_signature_rsa: |
| 6995 | len = chunk_printf(out, "RSA%d", bits); |
| 6996 | break; |
| 6997 | case TLSEXT_signature_ecdsa: |
| 6998 | len = chunk_printf(out, "EC%d", bits); |
| 6999 | break; |
| 7000 | case TLSEXT_signature_dsa: |
| 7001 | len = chunk_printf(out, "DSA%d", bits); |
| 7002 | break; |
| 7003 | default: |
| 7004 | return 0; |
| 7005 | } |
| 7006 | if (len < 0) |
| 7007 | return 0; |
| 7008 | return 1; |
| 7009 | } |
| 7010 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7011 | /* used for ppv2 pkey alog (can be used for logging) */ |
| 7012 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7013 | { |
| 7014 | struct ssl_sock_ctx *ctx; |
| 7015 | X509 *crt; |
| 7016 | |
| 7017 | if (!ssl_sock_is_ssl(conn)) |
| 7018 | return 0; |
| 7019 | |
| 7020 | ctx = conn->xprt_ctx; |
| 7021 | |
| 7022 | crt = SSL_get_certificate(ctx->ssl); |
| 7023 | if (!crt) |
| 7024 | return 0; |
| 7025 | |
| 7026 | return cert_get_pkey_algo(crt, out); |
| 7027 | } |
| 7028 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7029 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7030 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7031 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7032 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7033 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7034 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7035 | X509 *crt; |
| 7036 | |
| 7037 | if (!ssl_sock_is_ssl(conn)) |
| 7038 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7039 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7040 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7041 | if (!crt) |
| 7042 | return NULL; |
| 7043 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7044 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7045 | } |
| 7046 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7047 | /* used for ppv2 authority */ |
| 7048 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7049 | { |
| 7050 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7051 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7052 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7053 | if (!ssl_sock_is_ssl(conn)) |
| 7054 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7055 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7056 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7057 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7058 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7059 | #endif |
| 7060 | } |
| 7061 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7062 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7063 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7064 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7065 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7066 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7067 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7068 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7069 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7070 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7071 | } |
| 7072 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7073 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7074 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7075 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7076 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7077 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7078 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7079 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7080 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7081 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7082 | } |
| 7083 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7084 | /* Extract a serial from a cert, and copy it to a chunk. |
| 7085 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 7086 | * -1 if output is not large enough. |
| 7087 | */ |
| 7088 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7089 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7090 | { |
| 7091 | ASN1_INTEGER *serial; |
| 7092 | |
| 7093 | serial = X509_get_serialNumber(crt); |
| 7094 | if (!serial) |
| 7095 | return 0; |
| 7096 | |
| 7097 | if (out->size < serial->length) |
| 7098 | return -1; |
| 7099 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7100 | memcpy(out->area, serial->data, serial->length); |
| 7101 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7102 | return 1; |
| 7103 | } |
| 7104 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7105 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7106 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 7107 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7108 | */ |
| 7109 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7110 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7111 | { |
| 7112 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7113 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7114 | |
| 7115 | len =i2d_X509(crt, NULL); |
| 7116 | if (len <= 0) |
| 7117 | return 1; |
| 7118 | |
| 7119 | if (out->size < len) |
| 7120 | return -1; |
| 7121 | |
| 7122 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7123 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7124 | return 1; |
| 7125 | } |
| 7126 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7127 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7128 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7129 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 7130 | * and -1 if output is not large enough. |
| 7131 | */ |
| 7132 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7133 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7134 | { |
| 7135 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 7136 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 7137 | |
| 7138 | if (gentm->length < 12) |
| 7139 | return 0; |
| 7140 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 7141 | return 0; |
| 7142 | if (out->size < gentm->length-2) |
| 7143 | return -1; |
| 7144 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7145 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 7146 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7147 | return 1; |
| 7148 | } |
| 7149 | else if (tm->type == V_ASN1_UTCTIME) { |
| 7150 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 7151 | |
| 7152 | if (utctm->length < 10) |
| 7153 | return 0; |
| 7154 | if (utctm->data[0] >= 0x35) |
| 7155 | return 0; |
| 7156 | if (out->size < utctm->length) |
| 7157 | return -1; |
| 7158 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7159 | memcpy(out->area, utctm->data, utctm->length); |
| 7160 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7161 | return 1; |
| 7162 | } |
| 7163 | |
| 7164 | return 0; |
| 7165 | } |
| 7166 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7167 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 7168 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 7169 | */ |
| 7170 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7171 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 7172 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7173 | { |
| 7174 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7175 | ASN1_OBJECT *obj; |
| 7176 | ASN1_STRING *data; |
| 7177 | const unsigned char *data_ptr; |
| 7178 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7179 | int i, j, n; |
| 7180 | int cur = 0; |
| 7181 | const char *s; |
| 7182 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7183 | int name_count; |
| 7184 | |
| 7185 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7186 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7187 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7188 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7189 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7190 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7191 | else |
| 7192 | j = i; |
| 7193 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7194 | ne = X509_NAME_get_entry(a, j); |
| 7195 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7196 | data = X509_NAME_ENTRY_get_data(ne); |
| 7197 | data_ptr = ASN1_STRING_get0_data(data); |
| 7198 | data_len = ASN1_STRING_length(data); |
| 7199 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7200 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7201 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7202 | s = tmp; |
| 7203 | } |
| 7204 | |
| 7205 | if (chunk_strcasecmp(entry, s) != 0) |
| 7206 | continue; |
| 7207 | |
| 7208 | if (pos < 0) |
| 7209 | cur--; |
| 7210 | else |
| 7211 | cur++; |
| 7212 | |
| 7213 | if (cur != pos) |
| 7214 | continue; |
| 7215 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7216 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7217 | return -1; |
| 7218 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7219 | memcpy(out->area, data_ptr, data_len); |
| 7220 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7221 | return 1; |
| 7222 | } |
| 7223 | |
| 7224 | return 0; |
| 7225 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7226 | } |
| 7227 | |
| 7228 | /* |
| 7229 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 7230 | * Return 0; |
| 7231 | */ |
| 7232 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7233 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 7234 | { |
| 7235 | int i; |
| 7236 | char *str; |
| 7237 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 7238 | |
| 7239 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 7240 | if (names) { |
| 7241 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 7242 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 7243 | if (i > 0) |
| 7244 | chunk_appendf(out, ", "); |
| 7245 | if (name->type == GEN_DNS) { |
| 7246 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 7247 | chunk_appendf(out, "DNS:%s", str); |
| 7248 | OPENSSL_free(str); |
| 7249 | } |
| 7250 | } |
| 7251 | } |
| 7252 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 7253 | } |
| 7254 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7255 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7256 | #endif |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7257 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7258 | /* |
| 7259 | * Extract the DN in the specified format from the X509_NAME and copy result to a chunk. |
| 7260 | * Currently supports rfc2253 for returning LDAP V3 DNs. |
| 7261 | * Returns 1 if dn entries exist, 0 if no dn entry was found. |
| 7262 | */ |
| 7263 | static int |
| 7264 | ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out) |
| 7265 | { |
| 7266 | BIO *bio = NULL; |
| 7267 | int ret = 0; |
| 7268 | int data_len = 0; |
| 7269 | |
| 7270 | if (chunk_strcmp(format, "rfc2253") == 0) { |
| 7271 | bio = BIO_new(BIO_s_mem()); |
| 7272 | if (bio == NULL) |
| 7273 | goto out; |
| 7274 | |
| 7275 | if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0) |
| 7276 | goto out; |
| 7277 | |
| 7278 | if ((data_len = BIO_read(bio, out->area, out->size)) <= 0) |
| 7279 | goto out; |
| 7280 | |
| 7281 | out->data = data_len; |
| 7282 | |
| 7283 | ret = 1; |
| 7284 | } |
| 7285 | out: |
| 7286 | if (bio) |
| 7287 | BIO_free(bio); |
| 7288 | return ret; |
| 7289 | } |
| 7290 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7291 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 7292 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 7293 | */ |
| 7294 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7295 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7296 | { |
| 7297 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7298 | ASN1_OBJECT *obj; |
| 7299 | ASN1_STRING *data; |
| 7300 | const unsigned char *data_ptr; |
| 7301 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7302 | int i, n, ln; |
| 7303 | int l = 0; |
| 7304 | const char *s; |
| 7305 | char *p; |
| 7306 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7307 | int name_count; |
| 7308 | |
| 7309 | |
| 7310 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7311 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7312 | out->data = 0; |
| 7313 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7314 | for (i = 0; i < name_count; i++) { |
| 7315 | ne = X509_NAME_get_entry(a, i); |
| 7316 | obj = X509_NAME_ENTRY_get_object(ne); |
| 7317 | data = X509_NAME_ENTRY_get_data(ne); |
| 7318 | data_ptr = ASN1_STRING_get0_data(data); |
| 7319 | data_len = ASN1_STRING_length(data); |
| 7320 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7321 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7322 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7323 | s = tmp; |
| 7324 | } |
| 7325 | ln = strlen(s); |
| 7326 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7327 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7328 | if (l > out->size) |
| 7329 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7330 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7331 | |
| 7332 | *(p++)='/'; |
| 7333 | memcpy(p, s, ln); |
| 7334 | p += ln; |
| 7335 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7336 | memcpy(p, data_ptr, data_len); |
| 7337 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7338 | } |
| 7339 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7340 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7341 | return 0; |
| 7342 | |
| 7343 | return 1; |
| 7344 | } |
| 7345 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7346 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7347 | { |
| 7348 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7349 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7350 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7351 | if (!ssl_sock_is_ssl(conn)) |
| 7352 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7353 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7354 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7355 | #endif |
| 7356 | } |
| 7357 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7358 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7359 | * to disable SNI. |
| 7360 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7361 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7362 | { |
| 7363 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7364 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7365 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7366 | char *prev_name; |
| 7367 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7368 | if (!ssl_sock_is_ssl(conn)) |
| 7369 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7370 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7371 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7372 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7373 | * new connection will present a new SNI. As an optimization we could |
| 7374 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7375 | * server. |
| 7376 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7377 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7378 | if ((!prev_name && hostname) || |
| 7379 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7380 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7381 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7382 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7383 | #endif |
| 7384 | } |
| 7385 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7386 | /* Extract peer certificate's common name into the chunk dest |
| 7387 | * Returns |
| 7388 | * the len of the extracted common name |
| 7389 | * or 0 if no CN found in DN |
| 7390 | * or -1 on error case (i.e. no peer certificate) |
| 7391 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7392 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7393 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7394 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7395 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7396 | X509 *crt = NULL; |
| 7397 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7398 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7399 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7400 | .area = (char *)&find_cn, |
| 7401 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7402 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7403 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7404 | |
| 7405 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7406 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7407 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7408 | |
| 7409 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7410 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7411 | if (!crt) |
| 7412 | goto out; |
| 7413 | |
| 7414 | name = X509_get_subject_name(crt); |
| 7415 | if (!name) |
| 7416 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7417 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7418 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7419 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7420 | if (crt) |
| 7421 | X509_free(crt); |
| 7422 | |
| 7423 | return result; |
| 7424 | } |
| 7425 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7426 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7427 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7428 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7429 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7430 | X509 *crt = NULL; |
| 7431 | |
| 7432 | if (!ssl_sock_is_ssl(conn)) |
| 7433 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7434 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7435 | |
| 7436 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7437 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7438 | if (!crt) |
| 7439 | return 0; |
| 7440 | |
| 7441 | X509_free(crt); |
| 7442 | return 1; |
| 7443 | } |
| 7444 | |
| 7445 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7446 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7447 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7448 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7449 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7450 | if (!ssl_sock_is_ssl(conn)) |
| 7451 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7452 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7453 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7454 | } |
| 7455 | |
| 7456 | /* returns result from SSL verify */ |
| 7457 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7458 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7459 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7460 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7461 | if (!ssl_sock_is_ssl(conn)) |
| 7462 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7463 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7464 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7465 | } |
| 7466 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7467 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7468 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7469 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7470 | * freed by the caller. NPN is also checked if available since older versions |
| 7471 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7472 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7473 | 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] | 7474 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7475 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7476 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7477 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7478 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7479 | return 0; |
| 7480 | |
| 7481 | *str = NULL; |
| 7482 | |
| 7483 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7484 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7485 | if (*str) |
| 7486 | return 1; |
| 7487 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7488 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7489 | 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] | 7490 | if (*str) |
| 7491 | return 1; |
| 7492 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7493 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7494 | return 0; |
| 7495 | } |
| 7496 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7497 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7498 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7499 | static int |
| 7500 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7501 | { |
| 7502 | struct connection *conn; |
| 7503 | |
| 7504 | conn = objt_conn(smp->sess->origin); |
| 7505 | if (!conn || conn->xprt != &ssl_sock) |
| 7506 | return 0; |
| 7507 | |
| 7508 | smp->flags = 0; |
| 7509 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7510 | #ifdef OPENSSL_IS_BORINGSSL |
| 7511 | { |
| 7512 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7513 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7514 | SSL_early_data_accepted(ctx->ssl)); |
| 7515 | } |
| 7516 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7517 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 7518 | (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] | 7519 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7520 | return 1; |
| 7521 | } |
| 7522 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7523 | /* boolean, returns true if client cert was present */ |
| 7524 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7525 | 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] | 7526 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7527 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7528 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7529 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7530 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7531 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7532 | return 0; |
| 7533 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7534 | ctx = conn->xprt_ctx; |
| 7535 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7536 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7537 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7538 | return 0; |
| 7539 | } |
| 7540 | |
| 7541 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7542 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7543 | 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] | 7544 | |
| 7545 | return 1; |
| 7546 | } |
| 7547 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7548 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7549 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7550 | * should be use. |
| 7551 | */ |
| 7552 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7553 | 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] | 7554 | { |
| 7555 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7556 | X509 *crt = NULL; |
| 7557 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7558 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7559 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7560 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7561 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7562 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7563 | if (!conn || conn->xprt != &ssl_sock) |
| 7564 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7565 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7566 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7567 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7568 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7569 | return 0; |
| 7570 | } |
| 7571 | |
| 7572 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7573 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7574 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7575 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7576 | |
| 7577 | if (!crt) |
| 7578 | goto out; |
| 7579 | |
| 7580 | smp_trash = get_trash_chunk(); |
| 7581 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7582 | goto out; |
| 7583 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7584 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7585 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7586 | ret = 1; |
| 7587 | out: |
| 7588 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7589 | if (cert_peer && crt) |
| 7590 | X509_free(crt); |
| 7591 | return ret; |
| 7592 | } |
| 7593 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7594 | /* binary, returns serial of certificate in a binary chunk. |
| 7595 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7596 | * should be use. |
| 7597 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7598 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7599 | 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] | 7600 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7601 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7602 | X509 *crt = NULL; |
| 7603 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7604 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7605 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7606 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7607 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7608 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7609 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7610 | return 0; |
| 7611 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7612 | ctx = conn->xprt_ctx; |
| 7613 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7614 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7615 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7616 | return 0; |
| 7617 | } |
| 7618 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7619 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7620 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7621 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7622 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7623 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7624 | if (!crt) |
| 7625 | goto out; |
| 7626 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7627 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7628 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 7629 | goto out; |
| 7630 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7631 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7632 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7633 | ret = 1; |
| 7634 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7635 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7636 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7637 | X509_free(crt); |
| 7638 | return ret; |
| 7639 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7640 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7641 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 7642 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7643 | * should be use. |
| 7644 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7645 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7646 | 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] | 7647 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7648 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7649 | X509 *crt = NULL; |
| 7650 | const EVP_MD *digest; |
| 7651 | int ret = 0; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 7652 | unsigned int len = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7653 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7654 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7655 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7656 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7657 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7658 | if (!conn || conn->xprt != &ssl_sock) |
| 7659 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7660 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7661 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7662 | if (conn->flags & CO_FL_WAIT_XPRT) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7663 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7664 | return 0; |
| 7665 | } |
| 7666 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7667 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7668 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7669 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7670 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7671 | if (!crt) |
| 7672 | goto out; |
| 7673 | |
| 7674 | smp_trash = get_trash_chunk(); |
| 7675 | digest = EVP_sha1(); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 7676 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len); |
| 7677 | smp_trash->data = len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7678 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7679 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7680 | ret = 1; |
| 7681 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7682 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7683 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7684 | X509_free(crt); |
| 7685 | return ret; |
| 7686 | } |
| 7687 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7688 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 7689 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7690 | * should be use. |
| 7691 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7692 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7693 | 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] | 7694 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7695 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7696 | X509 *crt = NULL; |
| 7697 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7698 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7699 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7700 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7701 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7702 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7703 | if (!conn || conn->xprt != &ssl_sock) |
| 7704 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7705 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7706 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7707 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7708 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7709 | return 0; |
| 7710 | } |
| 7711 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7712 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7713 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7714 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7715 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7716 | if (!crt) |
| 7717 | goto out; |
| 7718 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7719 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7720 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7721 | goto out; |
| 7722 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7723 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7724 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7725 | ret = 1; |
| 7726 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7727 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7728 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7729 | X509_free(crt); |
| 7730 | return ret; |
| 7731 | } |
| 7732 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7733 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 7734 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7735 | * should be use. |
| 7736 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7737 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7738 | 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] | 7739 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7740 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7741 | X509 *crt = NULL; |
| 7742 | X509_NAME *name; |
| 7743 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7744 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7745 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7746 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7747 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7748 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7749 | if (!conn || conn->xprt != &ssl_sock) |
| 7750 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7751 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7752 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7753 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7754 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7755 | return 0; |
| 7756 | } |
| 7757 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7758 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7759 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7760 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7761 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7762 | if (!crt) |
| 7763 | goto out; |
| 7764 | |
| 7765 | name = X509_get_issuer_name(crt); |
| 7766 | if (!name) |
| 7767 | goto out; |
| 7768 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7769 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7770 | 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] | 7771 | int pos = 1; |
| 7772 | |
| 7773 | if (args[1].type == ARGT_SINT) |
| 7774 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7775 | |
| 7776 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 7777 | goto out; |
| 7778 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7779 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 7780 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 7781 | goto out; |
| 7782 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7783 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 7784 | goto out; |
| 7785 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7786 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7787 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7788 | ret = 1; |
| 7789 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7790 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7791 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7792 | X509_free(crt); |
| 7793 | return ret; |
| 7794 | } |
| 7795 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7796 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 7797 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7798 | * should be use. |
| 7799 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7800 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7801 | 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] | 7802 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7803 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7804 | X509 *crt = NULL; |
| 7805 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7806 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7807 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7808 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7809 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7810 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7811 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7812 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7813 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7814 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7815 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7816 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7817 | return 0; |
| 7818 | } |
| 7819 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7820 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7821 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7822 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7823 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7824 | if (!crt) |
| 7825 | goto out; |
| 7826 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7827 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7828 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7829 | goto out; |
| 7830 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7831 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7832 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7833 | ret = 1; |
| 7834 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7835 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7836 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7837 | X509_free(crt); |
| 7838 | return ret; |
| 7839 | } |
| 7840 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7841 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 7842 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7843 | * should be use. |
| 7844 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7845 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7846 | 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] | 7847 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7848 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7849 | X509 *crt = NULL; |
| 7850 | X509_NAME *name; |
| 7851 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7852 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7853 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7854 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7855 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7856 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7857 | if (!conn || conn->xprt != &ssl_sock) |
| 7858 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7859 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7860 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7861 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7862 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7863 | return 0; |
| 7864 | } |
| 7865 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7866 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7867 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7868 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7869 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7870 | if (!crt) |
| 7871 | goto out; |
| 7872 | |
| 7873 | name = X509_get_subject_name(crt); |
| 7874 | if (!name) |
| 7875 | goto out; |
| 7876 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7877 | smp_trash = get_trash_chunk(); |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7878 | 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] | 7879 | int pos = 1; |
| 7880 | |
| 7881 | if (args[1].type == ARGT_SINT) |
| 7882 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7883 | |
| 7884 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 7885 | goto out; |
| 7886 | } |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7887 | else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) { |
| 7888 | if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0) |
| 7889 | goto out; |
| 7890 | } |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7891 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 7892 | goto out; |
| 7893 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7894 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7895 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7896 | ret = 1; |
| 7897 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7898 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7899 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7900 | X509_free(crt); |
| 7901 | return ret; |
| 7902 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7903 | |
| 7904 | /* integer, returns true if current session use a client certificate */ |
| 7905 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7906 | 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] | 7907 | { |
| 7908 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7909 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7910 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7911 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7912 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7913 | if (!conn || conn->xprt != &ssl_sock) |
| 7914 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7915 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7916 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7917 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7918 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7919 | return 0; |
| 7920 | } |
| 7921 | |
| 7922 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7923 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7924 | if (crt) { |
| 7925 | X509_free(crt); |
| 7926 | } |
| 7927 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7928 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7929 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7930 | return 1; |
| 7931 | } |
| 7932 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7933 | /* integer, returns the certificate version |
| 7934 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7935 | * should be use. |
| 7936 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7937 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7938 | 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] | 7939 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7940 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7941 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7942 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7943 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7944 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7945 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7946 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7947 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7948 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7949 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7950 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7951 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7952 | return 0; |
| 7953 | } |
| 7954 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7955 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7956 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7957 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7958 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7959 | if (!crt) |
| 7960 | return 0; |
| 7961 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7962 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7963 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7964 | if (cert_peer) |
| 7965 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7966 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7967 | |
| 7968 | return 1; |
| 7969 | } |
| 7970 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7971 | /* string, returns the certificate's signature algorithm. |
| 7972 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7973 | * should be use. |
| 7974 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7975 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7976 | 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] | 7977 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7978 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7979 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7980 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7981 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7982 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7983 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7984 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7985 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7986 | if (!conn || conn->xprt != &ssl_sock) |
| 7987 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7988 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7989 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7990 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7991 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7992 | return 0; |
| 7993 | } |
| 7994 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7995 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7996 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7997 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7998 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7999 | if (!crt) |
| 8000 | return 0; |
| 8001 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8002 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 8003 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8004 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8005 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8006 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8007 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8008 | if (cert_peer) |
| 8009 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8010 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8011 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8012 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8013 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8014 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8015 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8016 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8017 | if (cert_peer) |
| 8018 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 8019 | |
| 8020 | return 1; |
| 8021 | } |
| 8022 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8023 | /* string, returns the certificate's key algorithm. |
| 8024 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 8025 | * should be use. |
| 8026 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8027 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8028 | 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] | 8029 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8030 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8031 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8032 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8033 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8034 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8035 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8036 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8037 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8038 | if (!conn || conn->xprt != &ssl_sock) |
| 8039 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8040 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8041 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8042 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8043 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8044 | return 0; |
| 8045 | } |
| 8046 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8047 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8048 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8049 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8050 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8051 | if (!crt) |
| 8052 | return 0; |
| 8053 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 8054 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 8055 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8056 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8057 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 8058 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8059 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 8060 | if (cert_peer) |
| 8061 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8062 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 8063 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8064 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8065 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8066 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8067 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 8068 | if (cert_peer) |
| 8069 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 8070 | |
| 8071 | return 1; |
| 8072 | } |
| 8073 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8074 | /* boolean, returns true if front conn. transport layer is SSL. |
| 8075 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8076 | * char is 'b'. |
| 8077 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8078 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8079 | 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] | 8080 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8081 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8082 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8083 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8084 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8085 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8086 | return 1; |
| 8087 | } |
| 8088 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8089 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8090 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8091 | 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] | 8092 | { |
| 8093 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8094 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8095 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8096 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8097 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8098 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8099 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8100 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8101 | return 1; |
| 8102 | #else |
| 8103 | return 0; |
| 8104 | #endif |
| 8105 | } |
| 8106 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8107 | /* boolean, returns true if client session has been resumed. |
| 8108 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8109 | * char is 'b'. |
| 8110 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8111 | static int |
| 8112 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8113 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8114 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8115 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8116 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8117 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8118 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8119 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8120 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8121 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8122 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 8123 | return 1; |
| 8124 | } |
| 8125 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8126 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 8127 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8128 | * char is 'b'. |
| 8129 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8130 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8131 | 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] | 8132 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8133 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8134 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8135 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8136 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8137 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8138 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8139 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8140 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8141 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8142 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8143 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8144 | return 0; |
| 8145 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8146 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8147 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8148 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8149 | |
| 8150 | return 1; |
| 8151 | } |
| 8152 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8153 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 8154 | * is SSL. |
| 8155 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8156 | * char is 'b'. |
| 8157 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8158 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8159 | 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] | 8160 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8161 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8162 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8163 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8164 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8165 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8166 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8167 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8168 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8169 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8170 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8171 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8172 | return 0; |
| 8173 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8174 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8175 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8176 | |
| 8177 | return 1; |
| 8178 | } |
| 8179 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8180 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 8181 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8182 | * char is 'b'. |
| 8183 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8184 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8185 | 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] | 8186 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8187 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8188 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8189 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8190 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8191 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8192 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8193 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8194 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8195 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8196 | 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] | 8197 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8198 | return 0; |
| 8199 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8200 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8201 | |
| 8202 | return 1; |
| 8203 | } |
| 8204 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8205 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8206 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8207 | 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] | 8208 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8209 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8210 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8211 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8212 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8213 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8214 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8215 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8216 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8217 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8218 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8219 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8220 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8221 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8222 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8223 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8224 | (const unsigned char **)&smp->data.u.str.area, |
| 8225 | &len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8226 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8227 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8228 | return 0; |
| 8229 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8230 | smp->data.u.str.data = len; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8231 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8232 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8233 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8234 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8235 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8236 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8237 | 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] | 8238 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8239 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8240 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8241 | unsigned int len = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8242 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8243 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8244 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8245 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 8246 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 8247 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8248 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8249 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8250 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8251 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8252 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8253 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8254 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8255 | (const unsigned char **)&smp->data.u.str.area, |
| 8256 | &len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8257 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8258 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8259 | return 0; |
| 8260 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8261 | smp->data.u.str.data = len; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8262 | return 1; |
| 8263 | } |
| 8264 | #endif |
| 8265 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8266 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 8267 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8268 | * char is 'b'. |
| 8269 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 8270 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8271 | 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] | 8272 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8273 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8274 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8275 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8276 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8277 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8278 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8279 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8280 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8281 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8282 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8283 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8284 | return 0; |
| 8285 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8286 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8287 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8288 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8289 | |
| 8290 | return 1; |
| 8291 | } |
| 8292 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8293 | /* 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] | 8294 | * This function is also usable on backend conn if the fetch keyword 5th |
| 8295 | * char is 'b'. |
| 8296 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8297 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 8298 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8299 | 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] | 8300 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8301 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8302 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 8303 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8304 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8305 | unsigned int len = 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8306 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8307 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8308 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8309 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8310 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8311 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8312 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8313 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8314 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8315 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8316 | return 0; |
| 8317 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8318 | 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] | 8319 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8320 | return 0; |
| 8321 | |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 8322 | smp->data.u.str.data = len; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8323 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8324 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8325 | #endif |
| 8326 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8327 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8328 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8329 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 8330 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8331 | { |
| 8332 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8333 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8334 | struct buffer *data; |
| 8335 | struct ssl_sock_ctx *ctx; |
| 8336 | |
| 8337 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8338 | return 0; |
| 8339 | ctx = conn->xprt_ctx; |
| 8340 | |
| 8341 | data = get_trash_chunk(); |
| 8342 | if (kw[7] == 'c') |
| 8343 | data->data = SSL_get_client_random(ctx->ssl, |
| 8344 | (unsigned char *) data->area, |
| 8345 | data->size); |
| 8346 | else |
| 8347 | data->data = SSL_get_server_random(ctx->ssl, |
| 8348 | (unsigned char *) data->area, |
| 8349 | data->size); |
| 8350 | if (!data->data) |
| 8351 | return 0; |
| 8352 | |
| 8353 | smp->flags = 0; |
| 8354 | smp->data.type = SMP_T_BIN; |
| 8355 | smp->data.u.str = *data; |
| 8356 | |
| 8357 | return 1; |
| 8358 | } |
| 8359 | |
| 8360 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8361 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8362 | { |
| 8363 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8364 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 8365 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8366 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8367 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8368 | |
| 8369 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8370 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8371 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8372 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8373 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8374 | if (!ssl_sess) |
| 8375 | return 0; |
| 8376 | |
| 8377 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8378 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8379 | (unsigned char *) data->area, |
| 8380 | data->size); |
| 8381 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8382 | return 0; |
| 8383 | |
| 8384 | smp->flags = 0; |
| 8385 | smp->data.type = SMP_T_BIN; |
| 8386 | smp->data.u.str = *data; |
| 8387 | |
| 8388 | return 1; |
| 8389 | } |
| 8390 | #endif |
| 8391 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8392 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8393 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8394 | 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] | 8395 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8396 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8397 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8398 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8399 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8400 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8401 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8402 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8403 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8404 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8405 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8406 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8407 | 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] | 8408 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8409 | return 0; |
| 8410 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8411 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8412 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8413 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8414 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8415 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8416 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8417 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8418 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8419 | struct connection *conn; |
| 8420 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8421 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8422 | |
| 8423 | conn = objt_conn(smp->sess->origin); |
| 8424 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8425 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8426 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8427 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8428 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8429 | if (!capture) |
| 8430 | return 0; |
| 8431 | |
| 8432 | smp->flags = SMP_F_CONST; |
| 8433 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8434 | smp->data.u.str.area = capture->ciphersuite; |
| 8435 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8436 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | static int |
| 8440 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8441 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8442 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8443 | |
| 8444 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8445 | return 0; |
| 8446 | |
| 8447 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8448 | 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] | 8449 | smp->data.type = SMP_T_BIN; |
| 8450 | smp->data.u.str = *data; |
| 8451 | return 1; |
| 8452 | } |
| 8453 | |
| 8454 | static int |
| 8455 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8456 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8457 | struct connection *conn; |
| 8458 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8459 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8460 | |
| 8461 | conn = objt_conn(smp->sess->origin); |
| 8462 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8463 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8464 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8465 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8466 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8467 | if (!capture) |
| 8468 | return 0; |
| 8469 | |
| 8470 | smp->data.type = SMP_T_SINT; |
| 8471 | smp->data.u.sint = capture->xxh64; |
| 8472 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8473 | } |
| 8474 | |
| 8475 | static int |
| 8476 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8477 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8478 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8479 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8480 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8481 | |
| 8482 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8483 | return 0; |
| 8484 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8485 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8486 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8487 | const char *str; |
| 8488 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8489 | 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] | 8490 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8491 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8492 | cipher = SSL_get_cipher_by_value(id); |
| 8493 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8494 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8495 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8496 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8497 | #endif |
| 8498 | str = SSL_CIPHER_get_name(cipher); |
| 8499 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8500 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8501 | else |
| 8502 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8503 | } |
| 8504 | smp->data.type = SMP_T_STR; |
| 8505 | smp->data.u.str = *data; |
| 8506 | return 1; |
| 8507 | #else |
| 8508 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8509 | #endif |
| 8510 | } |
| 8511 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8512 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8513 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8514 | 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] | 8515 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8516 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8517 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8518 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8519 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8520 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8521 | |
| 8522 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8523 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8524 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8525 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8526 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8527 | if (conn->flags & CO_FL_WAIT_XPRT) { |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8528 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8529 | return 0; |
| 8530 | } |
| 8531 | |
| 8532 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8533 | if (!SSL_session_reused(ctx->ssl)) |
| 8534 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8535 | finished_trash->area, |
| 8536 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8537 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8538 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8539 | finished_trash->area, |
| 8540 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8541 | |
| 8542 | if (!finished_len) |
| 8543 | return 0; |
| 8544 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8545 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8546 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8547 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8548 | |
| 8549 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8550 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8551 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8552 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8553 | /* 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] | 8554 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8555 | 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] | 8556 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8557 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8558 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8559 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8560 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8561 | if (!conn || conn->xprt != &ssl_sock) |
| 8562 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8563 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8564 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8565 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8566 | smp->flags = SMP_F_MAY_CHANGE; |
| 8567 | return 0; |
| 8568 | } |
| 8569 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8570 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8571 | 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] | 8572 | smp->flags = 0; |
| 8573 | |
| 8574 | return 1; |
| 8575 | } |
| 8576 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8577 | /* 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] | 8578 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8579 | 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] | 8580 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8581 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8582 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8583 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8584 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8585 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8586 | return 0; |
| 8587 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8588 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8589 | smp->flags = SMP_F_MAY_CHANGE; |
| 8590 | return 0; |
| 8591 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8592 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8593 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8594 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8595 | 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] | 8596 | smp->flags = 0; |
| 8597 | |
| 8598 | return 1; |
| 8599 | } |
| 8600 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8601 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8602 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8603 | 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] | 8604 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8605 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8606 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8607 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8608 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8609 | if (!conn || conn->xprt != &ssl_sock) |
| 8610 | return 0; |
| 8611 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8612 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8613 | smp->flags = SMP_F_MAY_CHANGE; |
| 8614 | return 0; |
| 8615 | } |
| 8616 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8617 | ctx = conn->xprt_ctx; |
| 8618 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8619 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8620 | 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] | 8621 | smp->flags = 0; |
| 8622 | |
| 8623 | return 1; |
| 8624 | } |
| 8625 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8626 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8627 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8628 | 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] | 8629 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8630 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8631 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8632 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8633 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8634 | if (!conn || conn->xprt != &ssl_sock) |
| 8635 | return 0; |
| 8636 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 8637 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8638 | smp->flags = SMP_F_MAY_CHANGE; |
| 8639 | return 0; |
| 8640 | } |
| 8641 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8642 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8643 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8644 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8645 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8646 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8647 | 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] | 8648 | smp->flags = 0; |
| 8649 | |
| 8650 | return 1; |
| 8651 | } |
| 8652 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 8653 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8654 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8655 | { |
| 8656 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8657 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8658 | return ERR_ALERT | ERR_FATAL; |
| 8659 | } |
| 8660 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8661 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8662 | memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8663 | else |
| 8664 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8665 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 8666 | if (!ssl_store_load_locations_file(conf->ca_file)) { |
| 8667 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file); |
| 8668 | return ERR_ALERT | ERR_FATAL; |
| 8669 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8670 | return 0; |
| 8671 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8672 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8673 | { |
| 8674 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8675 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8676 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8677 | /* parse the "ca-sign-file" bind keyword */ |
| 8678 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8679 | { |
| 8680 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8681 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8682 | return ERR_ALERT | ERR_FATAL; |
| 8683 | } |
| 8684 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8685 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8686 | 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] | 8687 | else |
| 8688 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 8689 | |
| 8690 | return 0; |
| 8691 | } |
| 8692 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8693 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8694 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8695 | { |
| 8696 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8697 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8698 | return ERR_ALERT | ERR_FATAL; |
| 8699 | } |
| 8700 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 8701 | return 0; |
| 8702 | } |
| 8703 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8704 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8705 | 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] | 8706 | { |
| 8707 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8708 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8709 | return ERR_ALERT | ERR_FATAL; |
| 8710 | } |
| 8711 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8712 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8713 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8714 | return 0; |
| 8715 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8716 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8717 | { |
| 8718 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 8719 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8720 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8721 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8722 | /* parse the "ciphersuites" bind keyword */ |
| 8723 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8724 | { |
| 8725 | if (!*args[cur_arg + 1]) { |
| 8726 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 8727 | return ERR_ALERT | ERR_FATAL; |
| 8728 | } |
| 8729 | |
| 8730 | free(conf->ciphersuites); |
| 8731 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 8732 | return 0; |
| 8733 | } |
| 8734 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8735 | { |
| 8736 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 8737 | } |
| 8738 | #endif |
| 8739 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8740 | /* 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] | 8741 | 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] | 8742 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 8743 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 8744 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8745 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8746 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8747 | return ERR_ALERT | ERR_FATAL; |
| 8748 | } |
| 8749 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8750 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 8751 | 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] | 8752 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 8753 | return ERR_ALERT | ERR_FATAL; |
| 8754 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8755 | 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] | 8756 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8757 | } |
| 8758 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8759 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8760 | } |
| 8761 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8762 | /* 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] | 8763 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8764 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8765 | int err_code; |
| 8766 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8767 | if (!*args[cur_arg + 1]) { |
| 8768 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 8769 | return ERR_ALERT | ERR_FATAL; |
| 8770 | } |
| 8771 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8772 | err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err); |
| 8773 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 8774 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8775 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8776 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8777 | } |
| 8778 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 8779 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8780 | 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] | 8781 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 8782 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8783 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 8784 | return ERR_ALERT | ERR_FATAL; |
| 8785 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8786 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8787 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8788 | return ERR_ALERT | ERR_FATAL; |
| 8789 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8790 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8791 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8792 | 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] | 8793 | else |
| 8794 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8795 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 8796 | if (!ssl_store_load_locations_file(conf->crl_file)) { |
| 8797 | memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file); |
| 8798 | return ERR_ALERT | ERR_FATAL; |
| 8799 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8800 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 8801 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8802 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8803 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8804 | { |
| 8805 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8806 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8807 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8808 | /* parse the "curves" bind keyword keyword */ |
| 8809 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8810 | { |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 8811 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8812 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8813 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8814 | return ERR_ALERT | ERR_FATAL; |
| 8815 | } |
| 8816 | conf->curves = strdup(args[cur_arg + 1]); |
| 8817 | return 0; |
| 8818 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8819 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8820 | return ERR_ALERT | ERR_FATAL; |
| 8821 | #endif |
| 8822 | } |
| 8823 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8824 | { |
| 8825 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 8826 | } |
| 8827 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8828 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8829 | 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] | 8830 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8831 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8832 | 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] | 8833 | return ERR_ALERT | ERR_FATAL; |
| 8834 | #elif defined(OPENSSL_NO_ECDH) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8835 | 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] | 8836 | return ERR_ALERT | ERR_FATAL; |
| 8837 | #else |
| 8838 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8839 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8840 | return ERR_ALERT | ERR_FATAL; |
| 8841 | } |
| 8842 | |
| 8843 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8844 | |
| 8845 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8846 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8847 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8848 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8849 | { |
| 8850 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 8851 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8852 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8853 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8854 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8855 | { |
| 8856 | int code; |
| 8857 | char *p = args[cur_arg + 1]; |
| 8858 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 8859 | |
| 8860 | if (!*p) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8861 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8862 | return ERR_ALERT | ERR_FATAL; |
| 8863 | } |
| 8864 | |
| 8865 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 8866 | ignerr = &conf->ca_ignerr; |
| 8867 | |
| 8868 | if (strcmp(p, "all") == 0) { |
| 8869 | *ignerr = ~0ULL; |
| 8870 | return 0; |
| 8871 | } |
| 8872 | |
| 8873 | while (p) { |
| 8874 | code = atoi(p); |
| 8875 | if ((code <= 0) || (code > 63)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8876 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 8877 | args[cur_arg], code, args[cur_arg + 1]); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8878 | return ERR_ALERT | ERR_FATAL; |
| 8879 | } |
| 8880 | *ignerr |= 1ULL << code; |
| 8881 | p = strchr(p, ','); |
| 8882 | if (p) |
| 8883 | p++; |
| 8884 | } |
| 8885 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8886 | return 0; |
| 8887 | } |
| 8888 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8889 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 8890 | 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] | 8891 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8892 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8893 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8894 | p = strchr(arg, '-'); |
| 8895 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8896 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8897 | p++; |
| 8898 | if (!strcmp(p, "sslv3")) |
| 8899 | v = CONF_SSLV3; |
| 8900 | else if (!strcmp(p, "tlsv10")) |
| 8901 | v = CONF_TLSV10; |
| 8902 | else if (!strcmp(p, "tlsv11")) |
| 8903 | v = CONF_TLSV11; |
| 8904 | else if (!strcmp(p, "tlsv12")) |
| 8905 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8906 | else if (!strcmp(p, "tlsv13")) |
| 8907 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8908 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8909 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8910 | if (!strncmp(arg, "no-", 3)) |
| 8911 | methods->flags |= methodVersions[v].flag; |
| 8912 | else if (!strncmp(arg, "force-", 6)) |
| 8913 | methods->min = methods->max = v; |
| 8914 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8915 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8916 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8917 | fail: |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8918 | memprintf(err, "'%s' : option not implemented", arg); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8919 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8920 | } |
| 8921 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8922 | 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] | 8923 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8924 | 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] | 8925 | } |
| 8926 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8927 | 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] | 8928 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8929 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 8930 | } |
| 8931 | |
| 8932 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 8933 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 8934 | { |
| 8935 | uint16_t i, v = 0; |
| 8936 | char *argv = args[cur_arg + 1]; |
| 8937 | if (!*argv) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8938 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8939 | return ERR_ALERT | ERR_FATAL; |
| 8940 | } |
| 8941 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8942 | if (!strcmp(argv, methodVersions[i].name)) |
| 8943 | v = i; |
| 8944 | if (!v) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8945 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8946 | return ERR_ALERT | ERR_FATAL; |
| 8947 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8948 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 8949 | methods->min = v; |
| 8950 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 8951 | methods->max = v; |
| 8952 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 8953 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8954 | return ERR_ALERT | ERR_FATAL; |
| 8955 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8956 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8957 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8958 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8959 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8960 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8961 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8962 | 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] | 8963 | #endif |
| 8964 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 8965 | } |
| 8966 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8967 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8968 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8969 | 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] | 8970 | } |
| 8971 | |
| 8972 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8973 | { |
| 8974 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 8975 | } |
| 8976 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8977 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8978 | 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] | 8979 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 8980 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8981 | return 0; |
| 8982 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8983 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8984 | /* parse the "allow-0rtt" bind keyword */ |
| 8985 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8986 | { |
| 8987 | conf->early_data = 1; |
| 8988 | return 0; |
| 8989 | } |
| 8990 | |
| 8991 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8992 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 8993 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8994 | return 0; |
| 8995 | } |
| 8996 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8997 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8998 | 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] | 8999 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 9000 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9001 | char *p1, *p2; |
| 9002 | |
| 9003 | if (!*args[cur_arg + 1]) { |
| 9004 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 9005 | return ERR_ALERT | ERR_FATAL; |
| 9006 | } |
| 9007 | |
| 9008 | free(conf->npn_str); |
| 9009 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9010 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9011 | * so we reuse each comma to store the next <len> and need |
| 9012 | * one more for the end of the string. |
| 9013 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9014 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 9015 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 9016 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 9017 | |
| 9018 | /* replace commas with the name length */ |
| 9019 | p1 = conf->npn_str; |
| 9020 | p2 = p1 + 1; |
| 9021 | while (1) { |
| 9022 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 9023 | if (!p2) |
| 9024 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9025 | |
| 9026 | if (p2 - (p1 + 1) > 255) { |
| 9027 | *p2 = '\0'; |
| 9028 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9029 | return ERR_ALERT | ERR_FATAL; |
| 9030 | } |
| 9031 | |
| 9032 | *p1 = p2 - (p1 + 1); |
| 9033 | p1 = p2; |
| 9034 | |
| 9035 | if (!*p2) |
| 9036 | break; |
| 9037 | |
| 9038 | *(p2++) = '\0'; |
| 9039 | } |
| 9040 | return 0; |
| 9041 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9042 | 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] | 9043 | return ERR_ALERT | ERR_FATAL; |
| 9044 | #endif |
| 9045 | } |
| 9046 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9047 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9048 | { |
| 9049 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9050 | } |
| 9051 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9052 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9053 | 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] | 9054 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9055 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9056 | char *p1, *p2; |
| 9057 | |
| 9058 | if (!*args[cur_arg + 1]) { |
| 9059 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 9060 | return ERR_ALERT | ERR_FATAL; |
| 9061 | } |
| 9062 | |
| 9063 | free(conf->alpn_str); |
| 9064 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9065 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9066 | * so we reuse each comma to store the next <len> and need |
| 9067 | * one more for the end of the string. |
| 9068 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9069 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 9070 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 9071 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 9072 | |
| 9073 | /* replace commas with the name length */ |
| 9074 | p1 = conf->alpn_str; |
| 9075 | p2 = p1 + 1; |
| 9076 | while (1) { |
| 9077 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 9078 | if (!p2) |
| 9079 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9080 | |
| 9081 | if (p2 - (p1 + 1) > 255) { |
| 9082 | *p2 = '\0'; |
| 9083 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 9084 | return ERR_ALERT | ERR_FATAL; |
| 9085 | } |
| 9086 | |
| 9087 | *p1 = p2 - (p1 + 1); |
| 9088 | p1 = p2; |
| 9089 | |
| 9090 | if (!*p2) |
| 9091 | break; |
| 9092 | |
| 9093 | *(p2++) = '\0'; |
| 9094 | } |
| 9095 | return 0; |
| 9096 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9097 | 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] | 9098 | return ERR_ALERT | ERR_FATAL; |
| 9099 | #endif |
| 9100 | } |
| 9101 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9102 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9103 | { |
| 9104 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 9105 | } |
| 9106 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9107 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9108 | 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] | 9109 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 9110 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 9111 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9112 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9113 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 9114 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9115 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9116 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 9117 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9118 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 9119 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 9120 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 9121 | if (!conf->ssl_conf.ssl_methods.min) |
| 9122 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 9123 | if (!conf->ssl_conf.ssl_methods.max) |
| 9124 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 9125 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9126 | return 0; |
| 9127 | } |
| 9128 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9129 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 9130 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9131 | { |
| 9132 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 9133 | return 0; |
| 9134 | } |
| 9135 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9136 | /* parse the "generate-certificates" bind keyword */ |
| 9137 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9138 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9139 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9140 | conf->generate_certs = 1; |
| 9141 | #else |
| 9142 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 9143 | err && *err ? *err : ""); |
| 9144 | #endif |
| 9145 | return 0; |
| 9146 | } |
| 9147 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9148 | /* parse the "strict-sni" bind keyword */ |
| 9149 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9150 | { |
| 9151 | conf->strict_sni = 1; |
| 9152 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9153 | } |
| 9154 | |
| 9155 | /* parse the "tls-ticket-keys" bind keyword */ |
| 9156 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9157 | { |
| 9158 | #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] | 9159 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9160 | int i = 0; |
| 9161 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9162 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9163 | |
| 9164 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9165 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9166 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9167 | } |
| 9168 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9169 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9170 | if (keys_ref) { |
| 9171 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9172 | conf->keys_ref = keys_ref; |
| 9173 | return 0; |
| 9174 | } |
| 9175 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9176 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9177 | if (!keys_ref) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9178 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9179 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9180 | } |
| 9181 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9182 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9183 | if (!keys_ref->tlskeys) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9184 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9185 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9186 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9187 | |
| 9188 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9189 | 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] | 9190 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9191 | } |
| 9192 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9193 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9194 | if (!keys_ref->filename) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9195 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9196 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 9197 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9198 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9199 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9200 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 9201 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9202 | int dec_size; |
| 9203 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9204 | /* Strip newline characters from the end */ |
| 9205 | if(thisline[len - 1] == '\n') |
| 9206 | thisline[--len] = 0; |
| 9207 | |
| 9208 | if(thisline[len - 1] == '\r') |
| 9209 | thisline[--len] = 0; |
| 9210 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9211 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 9212 | if (dec_size < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9213 | 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] | 9214 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9215 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9216 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 9217 | keys_ref->key_size_bits = 128; |
| 9218 | } |
| 9219 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 9220 | keys_ref->key_size_bits = 256; |
| 9221 | } |
| 9222 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 9223 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 9224 | || ((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] | 9225 | 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] | 9226 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9227 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9228 | i++; |
| 9229 | } |
| 9230 | |
| 9231 | if (i < TLS_TICKETS_NO) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9232 | 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] | 9233 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | fclose(f); |
| 9237 | |
| 9238 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 9239 | i -= 2; |
| 9240 | 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] | 9241 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 9242 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9243 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 9244 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9245 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 9246 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 9247 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9248 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 9249 | |
| 9250 | fail: |
| 9251 | if (f) |
| 9252 | fclose(f); |
| 9253 | if (keys_ref) { |
| 9254 | free(keys_ref->filename); |
| 9255 | free(keys_ref->tlskeys); |
| 9256 | free(keys_ref); |
| 9257 | } |
| 9258 | return ERR_ALERT | ERR_FATAL; |
| 9259 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9260 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9261 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 9262 | return ERR_ALERT | ERR_FATAL; |
| 9263 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 9264 | } |
| 9265 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9266 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9267 | 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] | 9268 | { |
| 9269 | if (!*args[cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9270 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9271 | return ERR_ALERT | ERR_FATAL; |
| 9272 | } |
| 9273 | |
| 9274 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9275 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9276 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9277 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9278 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9279 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9280 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9281 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 9282 | args[cur_arg], args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9283 | return ERR_ALERT | ERR_FATAL; |
| 9284 | } |
| 9285 | |
| 9286 | return 0; |
| 9287 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9288 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9289 | { |
| 9290 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 9291 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 9292 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9293 | /* parse the "no-ca-names" bind keyword */ |
| 9294 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 9295 | { |
| 9296 | conf->no_ca_names = 1; |
| 9297 | return 0; |
| 9298 | } |
| 9299 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 9300 | { |
| 9301 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 9302 | } |
| 9303 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9304 | /************** "server" keywords ****************/ |
| 9305 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9306 | /* parse the "npn" bind keyword */ |
| 9307 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9308 | { |
| 9309 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9310 | char *p1, *p2; |
| 9311 | |
| 9312 | if (!*args[*cur_arg + 1]) { |
| 9313 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 9314 | return ERR_ALERT | ERR_FATAL; |
| 9315 | } |
| 9316 | |
| 9317 | free(newsrv->ssl_ctx.npn_str); |
| 9318 | |
| 9319 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 9320 | * so we reuse each comma to store the next <len> and need |
| 9321 | * one more for the end of the string. |
| 9322 | */ |
| 9323 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9324 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 9325 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 9326 | newsrv->ssl_ctx.npn_len); |
| 9327 | |
| 9328 | /* replace commas with the name length */ |
| 9329 | p1 = newsrv->ssl_ctx.npn_str; |
| 9330 | p2 = p1 + 1; |
| 9331 | while (1) { |
| 9332 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 9333 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 9334 | if (!p2) |
| 9335 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9336 | |
| 9337 | if (p2 - (p1 + 1) > 255) { |
| 9338 | *p2 = '\0'; |
| 9339 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9340 | return ERR_ALERT | ERR_FATAL; |
| 9341 | } |
| 9342 | |
| 9343 | *p1 = p2 - (p1 + 1); |
| 9344 | p1 = p2; |
| 9345 | |
| 9346 | if (!*p2) |
| 9347 | break; |
| 9348 | |
| 9349 | *(p2++) = '\0'; |
| 9350 | } |
| 9351 | return 0; |
| 9352 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9353 | 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] | 9354 | return ERR_ALERT | ERR_FATAL; |
| 9355 | #endif |
| 9356 | } |
| 9357 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9358 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9359 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9360 | { |
| 9361 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9362 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9363 | char **alpn_str; |
| 9364 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9365 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9366 | if (*args[*cur_arg] == 'c') { |
| 9367 | alpn_str = &newsrv->check.alpn_str; |
| 9368 | alpn_len = &newsrv->check.alpn_len; |
| 9369 | } else { |
| 9370 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9371 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9372 | |
| 9373 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9374 | if (!*args[*cur_arg + 1]) { |
| 9375 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 9376 | return ERR_ALERT | ERR_FATAL; |
| 9377 | } |
| 9378 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9379 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9380 | |
| 9381 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9382 | * so we reuse each comma to store the next <len> and need |
| 9383 | * one more for the end of the string. |
| 9384 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9385 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9386 | *alpn_str = calloc(1, *alpn_len + 1); |
| 9387 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9388 | |
| 9389 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9390 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9391 | p2 = p1 + 1; |
| 9392 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9393 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9394 | if (!p2) |
| 9395 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9396 | |
| 9397 | if (p2 - (p1 + 1) > 255) { |
| 9398 | *p2 = '\0'; |
| 9399 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9400 | return ERR_ALERT | ERR_FATAL; |
| 9401 | } |
| 9402 | |
| 9403 | *p1 = p2 - (p1 + 1); |
| 9404 | p1 = p2; |
| 9405 | |
| 9406 | if (!*p2) |
| 9407 | break; |
| 9408 | |
| 9409 | *(p2++) = '\0'; |
| 9410 | } |
| 9411 | return 0; |
| 9412 | #else |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9413 | 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] | 9414 | return ERR_ALERT | ERR_FATAL; |
| 9415 | #endif |
| 9416 | } |
| 9417 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9418 | /* parse the "ca-file" server keyword */ |
| 9419 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9420 | { |
| 9421 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9422 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9423 | return ERR_ALERT | ERR_FATAL; |
| 9424 | } |
| 9425 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9426 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9427 | 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] | 9428 | else |
| 9429 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9430 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 9431 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) { |
| 9432 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file); |
| 9433 | return ERR_ALERT | ERR_FATAL; |
| 9434 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9435 | return 0; |
| 9436 | } |
| 9437 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9438 | /* parse the "check-sni" server keyword */ |
| 9439 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9440 | { |
| 9441 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9442 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9443 | return ERR_ALERT | ERR_FATAL; |
| 9444 | } |
| 9445 | |
| 9446 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9447 | if (!newsrv->check.sni) { |
| 9448 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9449 | return ERR_ALERT | ERR_FATAL; |
| 9450 | } |
| 9451 | return 0; |
| 9452 | |
| 9453 | } |
| 9454 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9455 | /* parse the "check-ssl" server keyword */ |
| 9456 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9457 | { |
| 9458 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9459 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9460 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9461 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9462 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9463 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9464 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9465 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9466 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9467 | if (!newsrv->ssl_ctx.methods.min) |
| 9468 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9469 | if (!newsrv->ssl_ctx.methods.max) |
| 9470 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9471 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9472 | return 0; |
| 9473 | } |
| 9474 | |
| 9475 | /* parse the "ciphers" server keyword */ |
| 9476 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9477 | { |
| 9478 | if (!*args[*cur_arg + 1]) { |
| 9479 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9480 | return ERR_ALERT | ERR_FATAL; |
| 9481 | } |
| 9482 | |
| 9483 | free(newsrv->ssl_ctx.ciphers); |
| 9484 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9485 | return 0; |
| 9486 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9487 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9488 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9489 | /* parse the "ciphersuites" server keyword */ |
| 9490 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9491 | { |
| 9492 | if (!*args[*cur_arg + 1]) { |
| 9493 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9494 | return ERR_ALERT | ERR_FATAL; |
| 9495 | } |
| 9496 | |
| 9497 | free(newsrv->ssl_ctx.ciphersuites); |
| 9498 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9499 | return 0; |
| 9500 | } |
| 9501 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9502 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9503 | /* parse the "crl-file" server keyword */ |
| 9504 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9505 | { |
| 9506 | #ifndef X509_V_FLAG_CRL_CHECK |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9507 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9508 | return ERR_ALERT | ERR_FATAL; |
| 9509 | #else |
| 9510 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9511 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9512 | return ERR_ALERT | ERR_FATAL; |
| 9513 | } |
| 9514 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9515 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9516 | 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] | 9517 | else |
| 9518 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9519 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 9520 | if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) { |
| 9521 | memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file); |
| 9522 | return ERR_ALERT | ERR_FATAL; |
| 9523 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9524 | return 0; |
| 9525 | #endif |
| 9526 | } |
| 9527 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9528 | /* parse the "crt" server keyword */ |
| 9529 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9530 | { |
| 9531 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9532 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9533 | return ERR_ALERT | ERR_FATAL; |
| 9534 | } |
| 9535 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9536 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9537 | 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] | 9538 | else |
| 9539 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9540 | |
| 9541 | return 0; |
| 9542 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9543 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9544 | /* parse the "no-check-ssl" server keyword */ |
| 9545 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9546 | { |
| 9547 | newsrv->check.use_ssl = 0; |
| 9548 | free(newsrv->ssl_ctx.ciphers); |
| 9549 | newsrv->ssl_ctx.ciphers = NULL; |
| 9550 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9551 | return 0; |
| 9552 | } |
| 9553 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9554 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9555 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9556 | { |
| 9557 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9558 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9559 | return 0; |
| 9560 | } |
| 9561 | |
| 9562 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9563 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9564 | { |
| 9565 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9566 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9567 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9568 | return 0; |
| 9569 | } |
| 9570 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9571 | /* parse the "no-ssl" server keyword */ |
| 9572 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9573 | { |
| 9574 | newsrv->use_ssl = 0; |
| 9575 | free(newsrv->ssl_ctx.ciphers); |
| 9576 | newsrv->ssl_ctx.ciphers = NULL; |
| 9577 | return 0; |
| 9578 | } |
| 9579 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9580 | /* parse the "allow-0rtt" server keyword */ |
| 9581 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9582 | { |
| 9583 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9584 | return 0; |
| 9585 | } |
| 9586 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9587 | /* parse the "no-ssl-reuse" server keyword */ |
| 9588 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9589 | { |
| 9590 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9591 | return 0; |
| 9592 | } |
| 9593 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9594 | /* parse the "no-tls-tickets" server keyword */ |
| 9595 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9596 | { |
| 9597 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 9598 | return 0; |
| 9599 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 9600 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 9601 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9602 | { |
| 9603 | newsrv->pp_opts |= SRV_PP_V2; |
| 9604 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9605 | return 0; |
| 9606 | } |
| 9607 | |
| 9608 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 9609 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9610 | { |
| 9611 | newsrv->pp_opts |= SRV_PP_V2; |
| 9612 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9613 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 9614 | return 0; |
| 9615 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9616 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9617 | /* parse the "sni" server keyword */ |
| 9618 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9619 | { |
| 9620 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9621 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 9622 | return ERR_ALERT | ERR_FATAL; |
| 9623 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9624 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9625 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9626 | arg = args[*cur_arg + 1]; |
| 9627 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9628 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 9629 | return ERR_ALERT | ERR_FATAL; |
| 9630 | } |
| 9631 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9632 | free(newsrv->sni_expr); |
| 9633 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9634 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9635 | return 0; |
| 9636 | #endif |
| 9637 | } |
| 9638 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9639 | /* parse the "ssl" server keyword */ |
| 9640 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9641 | { |
| 9642 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9643 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9644 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9645 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9646 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9647 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9648 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9649 | return 0; |
| 9650 | } |
| 9651 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9652 | /* parse the "ssl-reuse" server keyword */ |
| 9653 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9654 | { |
| 9655 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 9656 | return 0; |
| 9657 | } |
| 9658 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9659 | /* parse the "tls-tickets" server keyword */ |
| 9660 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9661 | { |
| 9662 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 9663 | return 0; |
| 9664 | } |
| 9665 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9666 | /* parse the "verify" server keyword */ |
| 9667 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9668 | { |
| 9669 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9670 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9671 | return ERR_ALERT | ERR_FATAL; |
| 9672 | } |
| 9673 | |
| 9674 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9675 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9676 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9677 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9678 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9679 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 9680 | args[*cur_arg], args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9681 | return ERR_ALERT | ERR_FATAL; |
| 9682 | } |
| 9683 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9684 | return 0; |
| 9685 | } |
| 9686 | |
| 9687 | /* parse the "verifyhost" server keyword */ |
| 9688 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9689 | { |
| 9690 | if (!*args[*cur_arg + 1]) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 9691 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9692 | return ERR_ALERT | ERR_FATAL; |
| 9693 | } |
| 9694 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 9695 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9696 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 9697 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9698 | return 0; |
| 9699 | } |
| 9700 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9701 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 9702 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 9703 | struct proxy *defpx, const char *file, int line, |
| 9704 | char **err) { |
| 9705 | int i = 1; |
| 9706 | |
| 9707 | if (*(args[i]) == 0) { |
| 9708 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 9709 | return -1; |
| 9710 | } |
| 9711 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9712 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9713 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9714 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 9715 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9716 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 9717 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 9718 | i++; |
| 9719 | else { |
| 9720 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 9721 | return -1; |
| 9722 | } |
| 9723 | } |
| 9724 | 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] | 9725 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 9726 | return -1; |
| 9727 | } |
| 9728 | i++; |
| 9729 | } |
| 9730 | return 0; |
| 9731 | } |
| 9732 | |
| 9733 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 9734 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 9735 | struct proxy *defpx, const char *file, int line, |
| 9736 | char **err) { |
| 9737 | int i = 1; |
| 9738 | |
| 9739 | if (*(args[i]) == 0) { |
| 9740 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 9741 | return -1; |
| 9742 | } |
| 9743 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9744 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9745 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9746 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 9747 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 9748 | i++; |
| 9749 | else { |
| 9750 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 9751 | return -1; |
| 9752 | } |
| 9753 | } |
| 9754 | 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] | 9755 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 9756 | return -1; |
| 9757 | } |
| 9758 | i++; |
| 9759 | } |
| 9760 | return 0; |
| 9761 | } |
| 9762 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9763 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 9764 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9765 | */ |
| 9766 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 9767 | struct proxy *defpx, const char *file, int line, |
| 9768 | char **err) |
| 9769 | { |
| 9770 | char **target; |
| 9771 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9772 | 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] | 9773 | |
| 9774 | if (too_many_args(1, args, err, NULL)) |
| 9775 | return -1; |
| 9776 | |
| 9777 | if (*target) { |
| 9778 | memprintf(err, "'%s' already specified.", args[0]); |
| 9779 | return -1; |
| 9780 | } |
| 9781 | |
| 9782 | if (*(args[1]) == 0) { |
| 9783 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 9784 | return -1; |
| 9785 | } |
| 9786 | *target = strdup(args[1]); |
| 9787 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9788 | } |
| 9789 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 9790 | /* "issuers-chain-path" load chain certificate in global */ |
| 9791 | static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
| 9792 | { |
| 9793 | X509 *ca; |
| 9794 | X509_NAME *name = NULL; |
| 9795 | ASN1_OCTET_STRING *skid = NULL; |
| 9796 | STACK_OF(X509) *chain = NULL; |
| 9797 | struct issuer_chain *issuer; |
| 9798 | struct eb64_node *node; |
| 9799 | char *path; |
| 9800 | u64 key; |
| 9801 | int ret = 0; |
| 9802 | |
| 9803 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 9804 | if (chain == NULL) { |
| 9805 | chain = sk_X509_new_null(); |
| 9806 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 9807 | name = X509_get_subject_name(ca); |
| 9808 | } |
| 9809 | if (!sk_X509_push(chain, ca)) { |
| 9810 | X509_free(ca); |
| 9811 | goto end; |
| 9812 | } |
| 9813 | } |
| 9814 | if (!chain) { |
| 9815 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 9816 | goto end; |
| 9817 | } |
| 9818 | if (!skid) { |
| 9819 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 9820 | goto end; |
| 9821 | } |
| 9822 | if (!name) { |
| 9823 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 9824 | goto end; |
| 9825 | } |
| 9826 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
| 9827 | for (node = eb64_lookup(&global_ssl.cert_issuer_tree, key); node; node = eb64_next(node)) { |
| 9828 | issuer = container_of(node, typeof(*issuer), node); |
| 9829 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 9830 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 9831 | goto end; |
| 9832 | } |
| 9833 | } |
| 9834 | issuer = calloc(1, sizeof *issuer); |
| 9835 | path = strdup(fp); |
| 9836 | if (!issuer || !path) { |
| 9837 | free(issuer); |
| 9838 | free(path); |
| 9839 | goto end; |
| 9840 | } |
| 9841 | issuer->node.key = key; |
| 9842 | issuer->path = path; |
| 9843 | issuer->chain = chain; |
| 9844 | chain = NULL; |
| 9845 | eb64_insert(&global_ssl.cert_issuer_tree, &issuer->node); |
| 9846 | ret = 1; |
| 9847 | end: |
| 9848 | if (skid) |
| 9849 | ASN1_OCTET_STRING_free(skid); |
| 9850 | if (chain) |
| 9851 | sk_X509_pop_free(chain, X509_free); |
| 9852 | return ret; |
| 9853 | } |
| 9854 | |
| 9855 | static void ssl_free_global_issuers(void) |
| 9856 | { |
| 9857 | struct eb64_node *node, *back; |
| 9858 | struct issuer_chain *issuer; |
| 9859 | |
| 9860 | node = eb64_first(&global_ssl.cert_issuer_tree); |
| 9861 | while (node) { |
| 9862 | issuer = container_of(node, typeof(*issuer), node); |
| 9863 | back = eb64_next(node); |
| 9864 | eb64_delete(node); |
| 9865 | free(issuer->path); |
| 9866 | sk_X509_pop_free(issuer->chain, X509_free); |
| 9867 | free(issuer); |
| 9868 | node = back; |
| 9869 | } |
| 9870 | } |
| 9871 | |
| 9872 | static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx, |
| 9873 | struct proxy *defpx, const char *file, int line, |
| 9874 | char **err) |
| 9875 | { |
| 9876 | char *path; |
| 9877 | struct dirent **de_list; |
| 9878 | int i, n; |
| 9879 | struct stat buf; |
| 9880 | char *end; |
| 9881 | char fp[MAXPATHLEN+1]; |
| 9882 | |
| 9883 | if (too_many_args(1, args, err, NULL)) |
| 9884 | return -1; |
| 9885 | |
| 9886 | path = args[1]; |
| 9887 | if (*path == 0 || stat(path, &buf)) { |
| 9888 | memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n", |
| 9889 | err && *err ? *err : "", args[0]); |
| 9890 | return -1; |
| 9891 | } |
| 9892 | if (S_ISDIR(buf.st_mode) == 0) { |
| 9893 | memprintf(err, "%sglobal statement '%s': %s is not a directory.\n", |
| 9894 | err && *err ? *err : "", args[0], path); |
| 9895 | return -1; |
| 9896 | } |
| 9897 | |
| 9898 | /* strip trailing slashes, including first one */ |
| 9899 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 9900 | *end = 0; |
| 9901 | /* path already parsed? */ |
| 9902 | if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0) |
| 9903 | return 0; |
| 9904 | /* overwrite old issuers_chain_path */ |
| 9905 | free(global_ssl.issuers_chain_path); |
| 9906 | global_ssl.issuers_chain_path = strdup(path); |
| 9907 | ssl_free_global_issuers(); |
| 9908 | |
| 9909 | n = scandir(path, &de_list, 0, alphasort); |
| 9910 | if (n < 0) { |
| 9911 | memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n", |
| 9912 | err && *err ? *err : "", args[0], path, strerror(errno)); |
| 9913 | return -1; |
| 9914 | } |
| 9915 | for (i = 0; i < n; i++) { |
| 9916 | struct dirent *de = de_list[i]; |
| 9917 | BIO *in = NULL; |
| 9918 | char *warn = NULL; |
| 9919 | |
| 9920 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 9921 | free(de); |
| 9922 | if (stat(fp, &buf) != 0) { |
| 9923 | ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno)); |
| 9924 | goto next; |
| 9925 | } |
| 9926 | if (!S_ISREG(buf.st_mode)) |
| 9927 | goto next; |
| 9928 | |
| 9929 | in = BIO_new(BIO_s_file()); |
| 9930 | if (in == NULL) |
| 9931 | goto next; |
| 9932 | if (BIO_read_filename(in, fp) <= 0) |
| 9933 | goto next; |
| 9934 | ssl_load_global_issuer_from_BIO(in, fp, &warn); |
| 9935 | if (warn) { |
Tim Duesterhus | e8aa5f2 | 2020-02-19 11:41:13 +0100 | [diff] [blame] | 9936 | ha_warning("%s", warn); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 9937 | free(warn); |
| 9938 | warn = NULL; |
| 9939 | } |
| 9940 | next: |
| 9941 | if (in) |
| 9942 | BIO_free(in); |
| 9943 | } |
| 9944 | free(de_list); |
| 9945 | |
| 9946 | return 0; |
| 9947 | } |
| 9948 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9949 | /* parse the "ssl-mode-async" keyword in global section. |
| 9950 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9951 | */ |
| 9952 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 9953 | struct proxy *defpx, const char *file, int line, |
| 9954 | char **err) |
| 9955 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9956 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9957 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 9958 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9959 | return 0; |
| 9960 | #else |
| 9961 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 9962 | return -1; |
| 9963 | #endif |
| 9964 | } |
| 9965 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9966 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9967 | static int ssl_check_async_engine_count(void) { |
| 9968 | int err_code = 0; |
| 9969 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 9970 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9971 | 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] | 9972 | err_code = ERR_ABORT; |
| 9973 | } |
| 9974 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9975 | } |
| 9976 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9977 | /* parse the "ssl-engine" keyword in global section. |
| 9978 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9979 | */ |
| 9980 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 9981 | struct proxy *defpx, const char *file, int line, |
| 9982 | char **err) |
| 9983 | { |
| 9984 | char *algo; |
| 9985 | int ret = -1; |
| 9986 | |
| 9987 | if (*(args[1]) == 0) { |
| 9988 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 9989 | return ret; |
| 9990 | } |
| 9991 | |
| 9992 | if (*(args[2]) == 0) { |
| 9993 | /* if no list of algorithms is given, it defaults to ALL */ |
| 9994 | algo = strdup("ALL"); |
| 9995 | goto add_engine; |
| 9996 | } |
| 9997 | |
| 9998 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 9999 | if (strcmp(args[2], "algo") != 0) { |
| 10000 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 10001 | return ret; |
| 10002 | } |
| 10003 | |
| 10004 | if (*(args[3]) == 0) { |
| 10005 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 10006 | return ret; |
| 10007 | } |
| 10008 | algo = strdup(args[3]); |
| 10009 | |
| 10010 | add_engine: |
| 10011 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 10012 | openssl_engines_initialized++; |
| 10013 | ret = 0; |
| 10014 | } |
| 10015 | free(algo); |
| 10016 | return ret; |
| 10017 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10018 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10019 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10020 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 10021 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10022 | */ |
| 10023 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 10024 | struct proxy *defpx, const char *file, int line, |
| 10025 | char **err) |
| 10026 | { |
| 10027 | char **target; |
| 10028 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10029 | 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] | 10030 | |
| 10031 | if (too_many_args(1, args, err, NULL)) |
| 10032 | return -1; |
| 10033 | |
| 10034 | if (*(args[1]) == 0) { |
| 10035 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10036 | return -1; |
| 10037 | } |
| 10038 | |
| 10039 | free(*target); |
| 10040 | *target = strdup(args[1]); |
| 10041 | return 0; |
| 10042 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10043 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10044 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10045 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 10046 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 10047 | */ |
| 10048 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 10049 | struct proxy *defpx, const char *file, int line, |
| 10050 | char **err) |
| 10051 | { |
| 10052 | char **target; |
| 10053 | |
| 10054 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 10055 | |
| 10056 | if (too_many_args(1, args, err, NULL)) |
| 10057 | return -1; |
| 10058 | |
| 10059 | if (*(args[1]) == 0) { |
| 10060 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 10061 | return -1; |
| 10062 | } |
| 10063 | |
| 10064 | free(*target); |
| 10065 | *target = strdup(args[1]); |
| 10066 | return 0; |
| 10067 | } |
| 10068 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10069 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10070 | /* parse various global tune.ssl settings consisting in positive integers. |
| 10071 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10072 | */ |
| 10073 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 10074 | struct proxy *defpx, const char *file, int line, |
| 10075 | char **err) |
| 10076 | { |
| 10077 | int *target; |
| 10078 | |
| 10079 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 10080 | target = &global.tune.sslcachesize; |
| 10081 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10082 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10083 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10084 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10085 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 10086 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10087 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 10088 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10089 | else { |
| 10090 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 10091 | return -1; |
| 10092 | } |
| 10093 | |
| 10094 | if (too_many_args(1, args, err, NULL)) |
| 10095 | return -1; |
| 10096 | |
| 10097 | if (*(args[1]) == 0) { |
| 10098 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10099 | return -1; |
| 10100 | } |
| 10101 | |
| 10102 | *target = atoi(args[1]); |
| 10103 | if (*target < 0) { |
| 10104 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 10105 | return -1; |
| 10106 | } |
| 10107 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10108 | } |
| 10109 | |
| 10110 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 10111 | struct proxy *defpx, const char *file, int line, |
| 10112 | char **err) |
| 10113 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10114 | int ret; |
| 10115 | |
| 10116 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 10117 | if (ret != 0) |
| 10118 | return ret; |
| 10119 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10120 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10121 | memprintf(err, "'%s' is already configured.", args[0]); |
| 10122 | return -1; |
| 10123 | } |
| 10124 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10125 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 10126 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10127 | memprintf(err, "Out of memory error."); |
| 10128 | return -1; |
| 10129 | } |
| 10130 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10131 | } |
| 10132 | |
| 10133 | /* parse "ssl.force-private-cache". |
| 10134 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10135 | */ |
| 10136 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 10137 | struct proxy *defpx, const char *file, int line, |
| 10138 | char **err) |
| 10139 | { |
| 10140 | if (too_many_args(0, args, err, NULL)) |
| 10141 | return -1; |
| 10142 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10143 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10144 | return 0; |
| 10145 | } |
| 10146 | |
| 10147 | /* parse "ssl.lifetime". |
| 10148 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10149 | */ |
| 10150 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 10151 | struct proxy *defpx, const char *file, int line, |
| 10152 | char **err) |
| 10153 | { |
| 10154 | const char *res; |
| 10155 | |
| 10156 | if (too_many_args(1, args, err, NULL)) |
| 10157 | return -1; |
| 10158 | |
| 10159 | if (*(args[1]) == 0) { |
| 10160 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 10161 | return -1; |
| 10162 | } |
| 10163 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10164 | 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] | 10165 | if (res == PARSE_TIME_OVER) { |
| 10166 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 10167 | args[1], args[0]); |
| 10168 | return -1; |
| 10169 | } |
| 10170 | else if (res == PARSE_TIME_UNDER) { |
| 10171 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 10172 | args[1], args[0]); |
| 10173 | return -1; |
| 10174 | } |
| 10175 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10176 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 10177 | return -1; |
| 10178 | } |
| 10179 | return 0; |
| 10180 | } |
| 10181 | |
| 10182 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10183 | /* parse "ssl-dh-param-file". |
| 10184 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10185 | */ |
| 10186 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 10187 | struct proxy *defpx, const char *file, int line, |
| 10188 | char **err) |
| 10189 | { |
| 10190 | if (too_many_args(1, args, err, NULL)) |
| 10191 | return -1; |
| 10192 | |
| 10193 | if (*(args[1]) == 0) { |
| 10194 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 10195 | return -1; |
| 10196 | } |
| 10197 | |
| 10198 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 10199 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 10200 | return -1; |
| 10201 | } |
| 10202 | return 0; |
| 10203 | } |
| 10204 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10205 | /* parse "ssl.default-dh-param". |
| 10206 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 10207 | */ |
| 10208 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 10209 | struct proxy *defpx, const char *file, int line, |
| 10210 | char **err) |
| 10211 | { |
| 10212 | if (too_many_args(1, args, err, NULL)) |
| 10213 | return -1; |
| 10214 | |
| 10215 | if (*(args[1]) == 0) { |
| 10216 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 10217 | return -1; |
| 10218 | } |
| 10219 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10220 | global_ssl.default_dh_param = atoi(args[1]); |
| 10221 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10222 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 10223 | return -1; |
| 10224 | } |
| 10225 | return 0; |
| 10226 | } |
| 10227 | #endif |
| 10228 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10229 | |
| 10230 | /* |
| 10231 | * parse "ssl-load-extra-files". |
| 10232 | * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none" |
| 10233 | */ |
| 10234 | static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx, |
| 10235 | struct proxy *defpx, const char *file, int line, |
| 10236 | char **err) |
| 10237 | { |
| 10238 | int i; |
| 10239 | int gf = SSL_GF_NONE; |
| 10240 | |
| 10241 | if (*(args[1]) == 0) |
| 10242 | goto err_arg; |
| 10243 | |
| 10244 | for (i = 1; *args[i]; i++) { |
| 10245 | |
| 10246 | if (!strcmp("bundle", args[i])) { |
| 10247 | gf |= SSL_GF_BUNDLE; |
| 10248 | |
| 10249 | } else if (!strcmp("sctl", args[i])) { |
| 10250 | gf |= SSL_GF_SCTL; |
| 10251 | |
| 10252 | } else if (!strcmp("ocsp", args[i])){ |
| 10253 | gf |= SSL_GF_OCSP; |
| 10254 | |
| 10255 | } else if (!strcmp("issuer", args[i])){ |
| 10256 | gf |= SSL_GF_OCSP_ISSUER; |
| 10257 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10258 | } else if (!strcmp("key", args[i])) { |
| 10259 | gf |= SSL_GF_KEY; |
| 10260 | |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 10261 | } else if (!strcmp("none", args[i])) { |
| 10262 | if (gf != SSL_GF_NONE) |
| 10263 | goto err_alone; |
| 10264 | gf = SSL_GF_NONE; |
| 10265 | i++; |
| 10266 | break; |
| 10267 | |
| 10268 | } else if (!strcmp("all", args[i])) { |
| 10269 | if (gf != SSL_GF_NONE) |
| 10270 | goto err_alone; |
| 10271 | gf = SSL_GF_ALL; |
| 10272 | i++; |
| 10273 | break; |
| 10274 | } else { |
| 10275 | goto err_arg; |
| 10276 | } |
| 10277 | } |
| 10278 | /* break from loop but there are still arguments */ |
| 10279 | if (*args[i]) |
| 10280 | goto err_alone; |
| 10281 | |
| 10282 | global_ssl.extra_files = gf; |
| 10283 | |
| 10284 | return 0; |
| 10285 | |
| 10286 | err_alone: |
| 10287 | memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]); |
| 10288 | return -1; |
| 10289 | |
| 10290 | err_arg: |
| 10291 | memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]); |
| 10292 | return -1; |
| 10293 | } |
| 10294 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10295 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10296 | /* This function is used with TLS ticket keys management. It permits to browse |
| 10297 | * each reference. The variable <getnext> must contain the current node, |
| 10298 | * <end> point to the root node. |
| 10299 | */ |
| 10300 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10301 | static inline |
| 10302 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 10303 | { |
| 10304 | struct tls_keys_ref *ref = getnext; |
| 10305 | |
| 10306 | while (1) { |
| 10307 | |
| 10308 | /* Get next list entry. */ |
| 10309 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 10310 | |
| 10311 | /* If the entry is the last of the list, return NULL. */ |
| 10312 | if (&ref->list == end) |
| 10313 | return NULL; |
| 10314 | |
| 10315 | return ref; |
| 10316 | } |
| 10317 | } |
| 10318 | |
| 10319 | static inline |
| 10320 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 10321 | { |
| 10322 | int id; |
| 10323 | char *error; |
| 10324 | |
| 10325 | /* If the reference starts by a '#', this is numeric id. */ |
| 10326 | if (reference[0] == '#') { |
| 10327 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 10328 | id = strtol(reference + 1, &error, 10); |
| 10329 | if (*error != '\0') |
| 10330 | return NULL; |
| 10331 | |
| 10332 | /* Perform the unique id lookup. */ |
| 10333 | return tlskeys_ref_lookupid(id); |
| 10334 | } |
| 10335 | |
| 10336 | /* Perform the string lookup. */ |
| 10337 | return tlskeys_ref_lookup(reference); |
| 10338 | } |
| 10339 | #endif |
| 10340 | |
| 10341 | |
| 10342 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10343 | |
| 10344 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 10345 | |
| 10346 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 10347 | return cli_io_handler_tlskeys_files(appctx); |
| 10348 | } |
| 10349 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10350 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 10351 | * (next index to be dumped), and cli.p0 (next key reference). |
| 10352 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10353 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 10354 | |
| 10355 | struct stream_interface *si = appctx->owner; |
| 10356 | |
| 10357 | switch (appctx->st2) { |
| 10358 | case STAT_ST_INIT: |
| 10359 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 10360 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10361 | * later and restart at the state "STAT_ST_INIT". |
| 10362 | */ |
| 10363 | chunk_reset(&trash); |
| 10364 | |
| 10365 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 10366 | chunk_appendf(&trash, "# id secret\n"); |
| 10367 | else |
| 10368 | chunk_appendf(&trash, "# id (file)\n"); |
| 10369 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10370 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10371 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10372 | return 0; |
| 10373 | } |
| 10374 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10375 | /* Now, we start the browsing of the references lists. |
| 10376 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 10377 | * available field of this pointer is <list>. It is used with the function |
| 10378 | * tlskeys_list_get_next() for retruning the first available entry |
| 10379 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10380 | if (appctx->ctx.cli.p0 == NULL) { |
| 10381 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 10382 | 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] | 10383 | } |
| 10384 | |
| 10385 | appctx->st2 = STAT_ST_LIST; |
| 10386 | /* fall through */ |
| 10387 | |
| 10388 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10389 | while (appctx->ctx.cli.p0) { |
| 10390 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10391 | |
| 10392 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10393 | 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] | 10394 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10395 | |
| 10396 | if (appctx->ctx.cli.i1 == 0) |
| 10397 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 10398 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10399 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10400 | int head; |
| 10401 | |
| 10402 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 10403 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10404 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10405 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10406 | |
| 10407 | chunk_reset(t2); |
| 10408 | /* 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] | 10409 | if (ref->key_size_bits == 128) { |
| 10410 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10411 | sizeof(struct tls_sess_key_128), |
| 10412 | t2->area, t2->size); |
| 10413 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10414 | t2->area); |
| 10415 | } |
| 10416 | else if (ref->key_size_bits == 256) { |
| 10417 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 10418 | sizeof(struct tls_sess_key_256), |
| 10419 | t2->area, t2->size); |
| 10420 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 10421 | t2->area); |
| 10422 | } |
| 10423 | else { |
| 10424 | /* This case should never happen */ |
| 10425 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 10426 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10427 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10428 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10429 | /* let's try again later from this stream. We add ourselves into |
| 10430 | * this stream's users so that it can remove us upon termination. |
| 10431 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10432 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10433 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10434 | return 0; |
| 10435 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10436 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10437 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 10438 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10439 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10440 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 10441 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10442 | /* let's try again later from this stream. We add ourselves into |
| 10443 | * this stream's users so that it can remove us upon termination. |
| 10444 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10445 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10446 | return 0; |
| 10447 | } |
| 10448 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10449 | 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] | 10450 | break; |
| 10451 | |
| 10452 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10453 | 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] | 10454 | } |
| 10455 | |
| 10456 | appctx->st2 = STAT_ST_FIN; |
| 10457 | /* fall through */ |
| 10458 | |
| 10459 | default: |
| 10460 | appctx->st2 = STAT_ST_FIN; |
| 10461 | return 1; |
| 10462 | } |
| 10463 | return 0; |
| 10464 | } |
| 10465 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10466 | /* 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] | 10467 | 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] | 10468 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10469 | /* no parameter, shows only file list */ |
| 10470 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10471 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10472 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10473 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10474 | } |
| 10475 | |
| 10476 | if (args[2][0] == '*') { |
| 10477 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10478 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10479 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10480 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10481 | if (!appctx->ctx.cli.p0) |
| 10482 | 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] | 10483 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10484 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 10485 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10486 | } |
| 10487 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10488 | 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] | 10489 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10490 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10491 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10492 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10493 | /* 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] | 10494 | if (!*args[3] || !*args[4]) |
| 10495 | 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] | 10496 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 10497 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10498 | if (!ref) |
| 10499 | 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] | 10500 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10501 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10502 | if (ret < 0) |
| 10503 | 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] | 10504 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10505 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10506 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 10507 | 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] | 10508 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10509 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10510 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10511 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10512 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10513 | |
| 10514 | /* Type of SSL payloads that can be updated over the CLI */ |
| 10515 | |
| 10516 | enum { |
| 10517 | CERT_TYPE_PEM = 0, |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 10518 | CERT_TYPE_KEY, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 10519 | #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] | 10520 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 10521 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10522 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 10523 | #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] | 10524 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 10525 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10526 | CERT_TYPE_MAX, |
| 10527 | }; |
| 10528 | |
| 10529 | struct { |
| 10530 | const char *ext; |
| 10531 | int type; |
| 10532 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 10533 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 10534 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10535 | [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] | 10536 | [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] | 10537 | #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] | 10538 | [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] | 10539 | #endif |
| 10540 | #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] | 10541 | [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] | 10542 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10543 | [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] | 10544 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10545 | }; |
| 10546 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10547 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 10548 | enum { |
| 10549 | SETCERT_ST_INIT = 0, |
| 10550 | SETCERT_ST_GEN, |
| 10551 | SETCERT_ST_INSERT, |
| 10552 | SETCERT_ST_FIN, |
| 10553 | }; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10554 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10555 | /* release function of the `show ssl cert' command */ |
| 10556 | static void cli_release_show_cert(struct appctx *appctx) |
| 10557 | { |
| 10558 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 10559 | } |
| 10560 | |
| 10561 | /* IO handler of "show ssl cert <filename>" */ |
| 10562 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 10563 | { |
| 10564 | struct buffer *trash = alloc_trash_chunk(); |
| 10565 | struct ebmb_node *node; |
| 10566 | struct stream_interface *si = appctx->owner; |
| 10567 | struct ckch_store *ckchs; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10568 | |
| 10569 | if (trash == NULL) |
| 10570 | return 1; |
| 10571 | |
| 10572 | if (!appctx->ctx.ssl.old_ckchs) { |
| 10573 | if (ckchs_transaction.old_ckchs) { |
| 10574 | ckchs = ckchs_transaction.old_ckchs; |
| 10575 | chunk_appendf(trash, "# transaction\n"); |
| 10576 | if (!ckchs->multi) { |
| 10577 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 10578 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10579 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 10580 | int n; |
| 10581 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10582 | chunk_appendf(trash, "*%s:", ckchs->path); |
| 10583 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 10584 | if (ckchs->ckch[n].cert) |
| 10585 | chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 10586 | } |
| 10587 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 10588 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10589 | } |
| 10590 | } |
| 10591 | } |
| 10592 | |
| 10593 | if (!appctx->ctx.cli.p0) { |
| 10594 | chunk_appendf(trash, "# filename\n"); |
| 10595 | node = ebmb_first(&ckchs_tree); |
| 10596 | } else { |
| 10597 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 10598 | } |
| 10599 | while (node) { |
| 10600 | ckchs = ebmb_entry(node, struct ckch_store, node); |
| 10601 | if (!ckchs->multi) { |
| 10602 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 10603 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10604 | } else { |
William Lallemand | a25a19f | 2020-01-29 00:04:24 +0100 | [diff] [blame] | 10605 | int n; |
| 10606 | |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10607 | chunk_appendf(trash, "%s:", ckchs->path); |
| 10608 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 10609 | if (ckchs->ckch[n].cert) |
| 10610 | chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 10611 | } |
| 10612 | chunk_appendf(trash, "\n"); |
William Lallemand | ba22e90 | 2019-12-18 20:36:01 +0100 | [diff] [blame] | 10613 | #endif |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10614 | } |
| 10615 | |
| 10616 | node = ebmb_next(node); |
| 10617 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 10618 | si_rx_room_blk(si); |
| 10619 | goto yield; |
| 10620 | } |
| 10621 | } |
| 10622 | |
| 10623 | appctx->ctx.cli.p0 = NULL; |
| 10624 | free_trash_chunk(trash); |
| 10625 | return 1; |
| 10626 | yield: |
| 10627 | |
| 10628 | free_trash_chunk(trash); |
| 10629 | appctx->ctx.cli.p0 = ckchs; |
| 10630 | return 0; /* should come back */ |
| 10631 | } |
| 10632 | |
| 10633 | /* IO handler of the details "show ssl cert <filename>" */ |
| 10634 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 10635 | { |
| 10636 | struct stream_interface *si = appctx->owner; |
| 10637 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 10638 | struct buffer *out = alloc_trash_chunk(); |
| 10639 | struct buffer *tmp = alloc_trash_chunk(); |
| 10640 | X509_NAME *name = NULL; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 10641 | unsigned int len = 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10642 | int write = -1; |
| 10643 | BIO *bio = NULL; |
| 10644 | |
| 10645 | if (!tmp || !out) |
| 10646 | goto end; |
| 10647 | |
| 10648 | if (!ckchs->multi) { |
| 10649 | chunk_appendf(out, "Filename: "); |
| 10650 | if (ckchs == ckchs_transaction.new_ckchs) |
| 10651 | chunk_appendf(out, "*"); |
| 10652 | chunk_appendf(out, "%s\n", ckchs->path); |
| 10653 | chunk_appendf(out, "Serial: "); |
| 10654 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 10655 | goto end; |
| 10656 | dump_binary(out, tmp->area, tmp->data); |
| 10657 | chunk_appendf(out, "\n"); |
| 10658 | |
| 10659 | chunk_appendf(out, "notBefore: "); |
| 10660 | chunk_reset(tmp); |
| 10661 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 10662 | goto end; |
| 10663 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 10664 | goto end; |
| 10665 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 10666 | tmp->area[write] = '\0'; |
| 10667 | BIO_free(bio); |
| 10668 | chunk_appendf(out, "%s\n", tmp->area); |
| 10669 | |
| 10670 | chunk_appendf(out, "notAfter: "); |
| 10671 | chunk_reset(tmp); |
| 10672 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 10673 | goto end; |
| 10674 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 10675 | goto end; |
| 10676 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 10677 | goto end; |
| 10678 | tmp->area[write] = '\0'; |
| 10679 | BIO_free(bio); |
| 10680 | chunk_appendf(out, "%s\n", tmp->area); |
| 10681 | |
| 10682 | |
| 10683 | chunk_appendf(out, "Issuer: "); |
| 10684 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 10685 | goto end; |
| 10686 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 10687 | goto end; |
| 10688 | *(tmp->area + tmp->data) = '\0'; |
| 10689 | chunk_appendf(out, "%s\n", tmp->area); |
| 10690 | |
| 10691 | chunk_appendf(out, "Subject: "); |
| 10692 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 10693 | goto end; |
| 10694 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 10695 | goto end; |
| 10696 | *(tmp->area + tmp->data) = '\0'; |
| 10697 | chunk_appendf(out, "%s\n", tmp->area); |
| 10698 | |
| 10699 | |
| 10700 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 10701 | chunk_appendf(out, "Subject Alternative Name: "); |
| 10702 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 10703 | goto end; |
| 10704 | *(out->area + out->data) = '\0'; |
| 10705 | chunk_appendf(out, "\n"); |
| 10706 | #endif |
| 10707 | chunk_reset(tmp); |
| 10708 | chunk_appendf(out, "Algorithm: "); |
| 10709 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 10710 | goto end; |
| 10711 | chunk_appendf(out, "%s\n", tmp->area); |
| 10712 | |
| 10713 | chunk_reset(tmp); |
| 10714 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 10715 | 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] | 10716 | goto end; |
Willy Tarreau | 105599c | 2020-02-25 08:59:23 +0100 | [diff] [blame^] | 10717 | |
| 10718 | tmp->data = len; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 10719 | dump_binary(out, tmp->area, tmp->data); |
| 10720 | chunk_appendf(out, "\n"); |
| 10721 | } |
| 10722 | |
| 10723 | if (ci_putchk(si_ic(si), out) == -1) { |
| 10724 | si_rx_room_blk(si); |
| 10725 | goto yield; |
| 10726 | } |
| 10727 | |
| 10728 | end: |
| 10729 | free_trash_chunk(tmp); |
| 10730 | free_trash_chunk(out); |
| 10731 | return 1; |
| 10732 | yield: |
| 10733 | free_trash_chunk(tmp); |
| 10734 | free_trash_chunk(out); |
| 10735 | return 0; /* should come back */ |
| 10736 | } |
| 10737 | |
| 10738 | /* parsing function for 'show ssl cert [certfile]' */ |
| 10739 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 10740 | { |
| 10741 | struct ckch_store *ckchs; |
| 10742 | |
| 10743 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 10744 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 10745 | |
| 10746 | /* The operations on the CKCH architecture are locked so we can |
| 10747 | * manipulate ckch_store and ckch_inst */ |
| 10748 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 10749 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 10750 | |
| 10751 | /* check if there is a certificate to lookup */ |
| 10752 | if (*args[3]) { |
| 10753 | if (*args[3] == '*') { |
| 10754 | if (!ckchs_transaction.new_ckchs) |
| 10755 | goto error; |
| 10756 | |
| 10757 | ckchs = ckchs_transaction.new_ckchs; |
| 10758 | |
| 10759 | if (strcmp(args[3] + 1, ckchs->path)) |
| 10760 | goto error; |
| 10761 | |
| 10762 | } else { |
| 10763 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 10764 | goto error; |
| 10765 | |
| 10766 | } |
| 10767 | |
| 10768 | if (ckchs->multi) |
| 10769 | goto error; |
| 10770 | |
| 10771 | appctx->ctx.cli.p0 = ckchs; |
| 10772 | /* use the IO handler that shows details */ |
| 10773 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 10774 | } |
| 10775 | |
| 10776 | return 0; |
| 10777 | |
| 10778 | error: |
| 10779 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 10780 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 10781 | } |
| 10782 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10783 | /* 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] | 10784 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10785 | { |
| 10786 | struct ckch_store *new_ckchs; |
| 10787 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10788 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10789 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10790 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10791 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10792 | /* 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] | 10793 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10794 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10795 | if (!new_ckchs) |
| 10796 | return; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10797 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10798 | /* if the allocation failed, we need to free everything from the temporary list */ |
| 10799 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 10800 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10801 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10802 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 10803 | if (sc0->order == 0) /* we only free if it's the first inserted */ |
| 10804 | SSL_CTX_free(sc0->ctx); |
| 10805 | LIST_DEL(&sc0->by_ckch_inst); |
| 10806 | free(sc0); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10807 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10808 | LIST_DEL(&ckchi->by_ckchs); |
| 10809 | free(ckchi); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10810 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10811 | ckchs_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10812 | } |
| 10813 | } |
| 10814 | |
| 10815 | |
| 10816 | /* |
| 10817 | * This function tries to create the new ckch_inst and their SNIs |
| 10818 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10819 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10820 | { |
| 10821 | struct stream_interface *si = appctx->owner; |
| 10822 | int y = 0; |
| 10823 | char *err = NULL; |
| 10824 | int errcode = 0; |
| 10825 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 10826 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10827 | struct buffer *trash = alloc_trash_chunk(); |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 10828 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10829 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 10830 | if (trash == NULL) |
| 10831 | goto error; |
| 10832 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10833 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 10834 | goto error; |
| 10835 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10836 | while (1) { |
| 10837 | switch (appctx->st2) { |
| 10838 | case SETCERT_ST_INIT: |
| 10839 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10840 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10841 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 10842 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10843 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10844 | } |
| 10845 | appctx->st2 = SETCERT_ST_GEN; |
| 10846 | /* fallthrough */ |
| 10847 | case SETCERT_ST_GEN: |
| 10848 | /* |
| 10849 | * This state generates the ckch instances with their |
| 10850 | * sni_ctxs and SSL_CTX. |
| 10851 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10852 | * Since the SSL_CTX generation can be CPU consumer, we |
| 10853 | * yield every 10 instances. |
| 10854 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10855 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10856 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10857 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10858 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10859 | if (!new_ckchs) |
| 10860 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10861 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10862 | /* get the next ckchi to regenerate */ |
| 10863 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 10864 | /* we didn't start yet, set it to the first elem */ |
| 10865 | if (ckchi == NULL) |
| 10866 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10867 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10868 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 10869 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 10870 | struct ckch_inst *new_inst; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10871 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10872 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 10873 | if (y >= 10) { |
| 10874 | /* save the next ckchi to compute */ |
| 10875 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 10876 | goto yield; |
| 10877 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10878 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10879 | if (new_ckchs->multi) |
| 10880 | errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err); |
| 10881 | else |
| 10882 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10883 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10884 | if (errcode & ERR_CODE) |
| 10885 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10886 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 10887 | /* if the previous ckchi was used as the default */ |
| 10888 | if (ckchi->is_default) |
| 10889 | new_inst->is_default = 1; |
| 10890 | |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 10891 | /* we need to initialize the SSL_CTX generated */ |
William Lallemand | 696f317 | 2020-02-07 20:45:24 +0100 | [diff] [blame] | 10892 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 10893 | list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) { |
William Lallemand | 8ef0c2a | 2019-11-21 16:30:34 +0100 | [diff] [blame] | 10894 | if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 10895 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 10896 | if (errcode & ERR_CODE) |
| 10897 | goto error; |
| 10898 | } |
| 10899 | } |
| 10900 | |
| 10901 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10902 | /* display one dot per new instance */ |
| 10903 | chunk_appendf(trash, "."); |
| 10904 | /* link the new ckch_inst to the duplicate */ |
| 10905 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 10906 | y++; |
| 10907 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10908 | appctx->st2 = SETCERT_ST_INSERT; |
| 10909 | /* fallthrough */ |
| 10910 | case SETCERT_ST_INSERT: |
| 10911 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10912 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10913 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10914 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10915 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10916 | if (!new_ckchs) |
| 10917 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10918 | |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 10919 | /* 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] | 10920 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 10921 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10922 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 10923 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10924 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10925 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10926 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 10927 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10928 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10929 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10930 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 10931 | ebmb_delete(&sc0->name); |
| 10932 | LIST_DEL(&sc0->by_ckch_inst); |
| 10933 | free(sc0); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10934 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10935 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10936 | LIST_DEL(&ckchi->by_ckchs); |
| 10937 | free(ckchi); |
| 10938 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10939 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10940 | /* Replace the old ckchs by the new one */ |
| 10941 | ebmb_delete(&old_ckchs->node); |
| 10942 | ckchs_free(old_ckchs); |
| 10943 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10944 | appctx->st2 = SETCERT_ST_FIN; |
| 10945 | /* fallthrough */ |
| 10946 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10947 | /* we achieved the transaction, we can set everything to NULL */ |
| 10948 | free(ckchs_transaction.path); |
| 10949 | ckchs_transaction.path = NULL; |
| 10950 | ckchs_transaction.new_ckchs = NULL; |
| 10951 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10952 | goto end; |
| 10953 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10954 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10955 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10956 | |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 10957 | chunk_appendf(trash, "\n"); |
| 10958 | if (errcode & ERR_WARN) |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 10959 | chunk_appendf(trash, "%s", err); |
William Lallemand | ed44243 | 2019-11-21 16:41:07 +0100 | [diff] [blame] | 10960 | chunk_appendf(trash, "Success!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10961 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10962 | si_rx_room_blk(si); |
| 10963 | free_trash_chunk(trash); |
| 10964 | /* success: call the release function and don't come back */ |
| 10965 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10966 | yield: |
| 10967 | /* store the state */ |
| 10968 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10969 | si_rx_room_blk(si); |
| 10970 | free_trash_chunk(trash); |
| 10971 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10972 | return 0; /* should come back */ |
| 10973 | |
| 10974 | error: |
| 10975 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 10976 | if (trash) { |
| 10977 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 10978 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10979 | si_rx_room_blk(si); |
| 10980 | free_trash_chunk(trash); |
| 10981 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10982 | /* error: call the release function and don't come back */ |
| 10983 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10984 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10985 | |
| 10986 | /* |
| 10987 | * Parsing function of 'commit ssl cert' |
| 10988 | */ |
| 10989 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 10990 | { |
| 10991 | char *err = NULL; |
| 10992 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 10993 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 10994 | return 1; |
| 10995 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10996 | if (!*args[3]) |
| 10997 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 10998 | |
| 10999 | /* The operations on the CKCH architecture are locked so we can |
| 11000 | * manipulate ckch_store and ckch_inst */ |
| 11001 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11002 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 11003 | |
| 11004 | if (!ckchs_transaction.path) { |
| 11005 | memprintf(&err, "No ongoing transaction! !\n"); |
| 11006 | goto error; |
| 11007 | } |
| 11008 | |
| 11009 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11010 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 11011 | goto error; |
| 11012 | } |
| 11013 | |
William Lallemand | 4c5adbf | 2020-02-24 14:23:22 +0100 | [diff] [blame] | 11014 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11015 | if (ckchs_transaction.new_ckchs->multi) { |
| 11016 | int n; |
| 11017 | |
| 11018 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 11019 | 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)) { |
| 11020 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11021 | goto error; |
| 11022 | } |
| 11023 | } |
| 11024 | } else |
| 11025 | #endif |
| 11026 | { |
| 11027 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 11028 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 11029 | goto error; |
| 11030 | } |
| 11031 | } |
| 11032 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11033 | /* init the appctx structure */ |
| 11034 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11035 | appctx->ctx.ssl.next_ckchi = NULL; |
| 11036 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 11037 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 11038 | |
| 11039 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 11040 | return 0; |
| 11041 | |
| 11042 | error: |
| 11043 | |
| 11044 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11045 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 11046 | |
| 11047 | return cli_dynerr(appctx, err); |
| 11048 | } |
| 11049 | |
| 11050 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11051 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11052 | * 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] | 11053 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11054 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11055 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 11056 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11057 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11058 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11059 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11060 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11061 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11062 | char *end; |
| 11063 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11064 | struct cert_key_and_chain *ckch; |
| 11065 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11066 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11067 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11068 | return 1; |
| 11069 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11070 | if ((buf = alloc_trash_chunk()) == NULL) |
| 11071 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11072 | |
| 11073 | if (!*args[3] || !payload) |
| 11074 | return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n"); |
| 11075 | |
| 11076 | /* The operations on the CKCH architecture are locked so we can |
| 11077 | * manipulate ckch_store and ckch_inst */ |
| 11078 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11079 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 11080 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11081 | if (!chunk_strcpy(buf, args[3])) { |
| 11082 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11083 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11084 | goto end; |
| 11085 | } |
| 11086 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11087 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 11088 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11089 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11090 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 11091 | *end = '\0'; |
| 11092 | type = cert_exts[i].type; |
| 11093 | break; |
| 11094 | } |
| 11095 | } |
| 11096 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11097 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11098 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 11099 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11100 | /* if there is an ongoing transaction */ |
| 11101 | if (ckchs_transaction.path) { |
| 11102 | /* 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] | 11103 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11104 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11105 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11106 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11107 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11108 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11109 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11110 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11111 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11112 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11113 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11114 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11115 | break; |
| 11116 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11117 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11118 | if (bundle < 0) { |
| 11119 | 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); |
| 11120 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11121 | goto end; |
| 11122 | } |
| 11123 | } |
| 11124 | #endif |
| 11125 | |
| 11126 | /* if there is an ongoing transaction, check if this is the same file */ |
| 11127 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 11128 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 11129 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11130 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11131 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11132 | |
| 11133 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 11134 | |
| 11135 | } else { |
| 11136 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 11137 | |
| 11138 | /* lookup for the certificate in the tree: |
| 11139 | * check if this is used as a bundle AND as a unique certificate */ |
| 11140 | for (i = 0; i < 2; i++) { |
| 11141 | |
| 11142 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 11143 | /* only the bundle name is in the tree and you should |
| 11144 | * never update a bundle name, only a filename */ |
| 11145 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 11146 | /* we tried to look for a non-bundle and we found a bundle */ |
| 11147 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 11148 | err ? err : "", args[3], args[3]); |
| 11149 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11150 | goto end; |
| 11151 | } |
William Lallemand | 3246d94 | 2019-11-04 14:02:11 +0100 | [diff] [blame] | 11152 | /* If we want a bundle but this is not a bundle |
| 11153 | * example: When you try to update <file>.rsa, but |
| 11154 | * <file> is a regular file */ |
| 11155 | if (bundle >= 0 && find_ckchs[i]->multi == 0) { |
| 11156 | find_ckchs[i] = NULL; |
| 11157 | break; |
| 11158 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11159 | } |
| 11160 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 11161 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11162 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11163 | int j; |
| 11164 | |
| 11165 | /* check if it was used in a bundle by removing the |
| 11166 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 11167 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 11168 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11169 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 11170 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 11171 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 11172 | break; |
| 11173 | } |
| 11174 | } |
William Lallemand | 37031b8 | 2019-11-04 13:38:53 +0100 | [diff] [blame] | 11175 | if (bundle < 0) /* we didn't find a bundle extension */ |
| 11176 | break; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11177 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11178 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11179 | /* bundles are not supported here, so we don't need to lookup again */ |
| 11180 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 11181 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11182 | } |
| 11183 | |
| 11184 | if (find_ckchs[0] && find_ckchs[1]) { |
| 11185 | 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", |
| 11186 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 11187 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11188 | goto end; |
| 11189 | } |
| 11190 | |
| 11191 | 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] | 11192 | } |
| 11193 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11194 | if (!appctx->ctx.ssl.old_ckchs) { |
| 11195 | 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] | 11196 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11197 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11198 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11199 | } |
| 11200 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame] | 11201 | if (!appctx->ctx.ssl.path) { |
| 11202 | /* this is a new transaction, set the path of the transaction */ |
| 11203 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 11204 | if (!appctx->ctx.ssl.path) { |
| 11205 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 11206 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11207 | goto end; |
| 11208 | } |
| 11209 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11210 | |
| 11211 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11212 | |
| 11213 | /* TODO: handle filters */ |
| 11214 | if (old_ckchs->filters) { |
| 11215 | memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n", |
| 11216 | err ? err : ""); |
| 11217 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11218 | goto end; |
| 11219 | } |
| 11220 | |
| 11221 | /* duplicate the ckch store */ |
| 11222 | new_ckchs = ckchs_dup(old_ckchs); |
| 11223 | if (!new_ckchs) { |
| 11224 | memprintf(&err, "%sCannot allocate memory!\n", |
| 11225 | err ? err : ""); |
| 11226 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11227 | goto end; |
| 11228 | } |
| 11229 | |
| 11230 | if (!new_ckchs->multi) |
| 11231 | ckch = new_ckchs->ckch; |
| 11232 | else |
| 11233 | ckch = &new_ckchs->ckch[bundle]; |
| 11234 | |
| 11235 | /* appply the change on the duplicate */ |
| 11236 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 11237 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11238 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11239 | goto end; |
| 11240 | } |
| 11241 | |
| 11242 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 11243 | |
| 11244 | /* we succeed, we can save the ckchs in the transaction */ |
| 11245 | |
| 11246 | /* if there wasn't a transaction, update the old ckchs */ |
William Dauchy | c8bb153 | 2019-11-24 15:04:20 +0100 | [diff] [blame] | 11247 | if (!ckchs_transaction.old_ckchs) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11248 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 11249 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 11250 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 11251 | } else { |
| 11252 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 11253 | |
| 11254 | } |
| 11255 | |
| 11256 | /* free the previous ckchs if there was a transaction */ |
| 11257 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11258 | |
| 11259 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 11260 | |
| 11261 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11262 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11263 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11264 | end: |
| 11265 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11266 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 11267 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11268 | |
| 11269 | ckchs_free(appctx->ctx.ssl.new_ckchs); |
| 11270 | appctx->ctx.ssl.new_ckchs = NULL; |
| 11271 | |
| 11272 | appctx->ctx.ssl.old_ckchs = NULL; |
| 11273 | |
| 11274 | free(appctx->ctx.ssl.path); |
| 11275 | appctx->ctx.ssl.path = NULL; |
| 11276 | |
| 11277 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 11278 | 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] | 11279 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11280 | |
| 11281 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11282 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 11283 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 11284 | /* 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] | 11285 | } |
| 11286 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11287 | /* parsing function of 'abort ssl cert' */ |
| 11288 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 11289 | { |
| 11290 | char *err = NULL; |
| 11291 | |
William Lallemand | 230662a | 2019-12-03 13:32:54 +0100 | [diff] [blame] | 11292 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 11293 | return 1; |
| 11294 | |
William Lallemand | 0bc9c8a | 2019-11-19 15:51:51 +0100 | [diff] [blame] | 11295 | if (!*args[3]) |
| 11296 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 11297 | |
| 11298 | /* The operations on the CKCH architecture are locked so we can |
| 11299 | * manipulate ckch_store and ckch_inst */ |
| 11300 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11301 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 11302 | |
| 11303 | if (!ckchs_transaction.path) { |
| 11304 | memprintf(&err, "No ongoing transaction!\n"); |
| 11305 | goto error; |
| 11306 | } |
| 11307 | |
| 11308 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 11309 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 11310 | goto error; |
| 11311 | } |
| 11312 | |
| 11313 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 11314 | ckchs_free(ckchs_transaction.new_ckchs); |
| 11315 | ckchs_transaction.new_ckchs = NULL; |
| 11316 | ckchs_free(ckchs_transaction.old_ckchs); |
| 11317 | ckchs_transaction.old_ckchs = NULL; |
| 11318 | free(ckchs_transaction.path); |
| 11319 | ckchs_transaction.path = NULL; |
| 11320 | |
| 11321 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11322 | |
| 11323 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 11324 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 11325 | |
| 11326 | error: |
| 11327 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11328 | |
| 11329 | return cli_dynerr(appctx, err); |
| 11330 | } |
| 11331 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 11332 | 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] | 11333 | { |
| 11334 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 11335 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11336 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 11337 | |
| 11338 | if (!payload) |
| 11339 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11340 | |
| 11341 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11342 | if (!*payload) |
| 11343 | 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] | 11344 | |
| 11345 | /* remove \r and \n from the payload */ |
| 11346 | for (i = 0, j = 0; payload[i]; i++) { |
| 11347 | if (payload[i] == '\r' || payload[i] == '\n') |
| 11348 | continue; |
| 11349 | payload[j++] = payload[i]; |
| 11350 | } |
| 11351 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11352 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11353 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11354 | if (ret < 0) |
| 11355 | 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] | 11356 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 11357 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11358 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11359 | if (err) |
| 11360 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 11361 | else |
| 11362 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11363 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11364 | |
| 11365 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11366 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 11367 | 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] | 11368 | #endif |
| 11369 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 11370 | } |
| 11371 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 11372 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 11373 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 11374 | { |
| 11375 | switch (arg->type) { |
| 11376 | case ARGT_STR: |
| 11377 | smp->data.type = SMP_T_STR; |
| 11378 | smp->data.u.str = arg->data.str; |
| 11379 | return 1; |
| 11380 | case ARGT_VAR: |
| 11381 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 11382 | return 0; |
| 11383 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 11384 | return 0; |
| 11385 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 11386 | return 0; |
| 11387 | return 1; |
| 11388 | default: |
| 11389 | return 0; |
| 11390 | } |
| 11391 | } |
| 11392 | |
| 11393 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 11394 | const char *file, int line, char **err) |
| 11395 | { |
| 11396 | switch(args[0].data.sint) { |
| 11397 | case 128: |
| 11398 | case 192: |
| 11399 | case 256: |
| 11400 | break; |
| 11401 | default: |
| 11402 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 11403 | return 0; |
| 11404 | } |
| 11405 | /* Try to decode a variable. */ |
| 11406 | vars_check_arg(&args[1], NULL); |
| 11407 | vars_check_arg(&args[2], NULL); |
| 11408 | vars_check_arg(&args[3], NULL); |
| 11409 | return 1; |
| 11410 | } |
| 11411 | |
| 11412 | /* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 11413 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 11414 | { |
| 11415 | struct sample nonce, key, aead_tag; |
| 11416 | struct buffer *smp_trash, *smp_trash_alloc; |
| 11417 | EVP_CIPHER_CTX *ctx; |
| 11418 | int dec_size, ret; |
| 11419 | |
| 11420 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 11421 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 11422 | return 0; |
| 11423 | |
| 11424 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 11425 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 11426 | return 0; |
| 11427 | |
| 11428 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 11429 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 11430 | return 0; |
| 11431 | |
| 11432 | smp_trash = get_trash_chunk(); |
| 11433 | smp_trash_alloc = alloc_trash_chunk(); |
| 11434 | if (!smp_trash_alloc) |
| 11435 | return 0; |
| 11436 | |
| 11437 | ctx = EVP_CIPHER_CTX_new(); |
| 11438 | |
| 11439 | if (!ctx) |
| 11440 | goto err; |
| 11441 | |
| 11442 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 11443 | if (dec_size < 0) |
| 11444 | goto err; |
| 11445 | smp_trash->data = dec_size; |
| 11446 | |
| 11447 | /* Set cipher type and mode */ |
| 11448 | switch(arg_p[0].data.sint) { |
| 11449 | case 128: |
| 11450 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 11451 | break; |
| 11452 | case 192: |
| 11453 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 11454 | break; |
| 11455 | case 256: |
| 11456 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 11457 | break; |
| 11458 | } |
| 11459 | |
| 11460 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 11461 | |
| 11462 | /* Initialise IV */ |
| 11463 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 11464 | goto err; |
| 11465 | |
| 11466 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 11467 | if (dec_size < 0) |
| 11468 | goto err; |
| 11469 | smp_trash->data = dec_size; |
| 11470 | |
| 11471 | /* Initialise key */ |
| 11472 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 11473 | goto err; |
| 11474 | |
| 11475 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 11476 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 11477 | goto err; |
| 11478 | |
| 11479 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 11480 | if (dec_size < 0) |
| 11481 | goto err; |
| 11482 | smp_trash_alloc->data = dec_size; |
| 11483 | dec_size = smp_trash->data; |
| 11484 | |
| 11485 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 11486 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 11487 | |
| 11488 | if (ret <= 0) |
| 11489 | goto err; |
| 11490 | |
| 11491 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 11492 | smp->data.u.str.area = smp_trash->area; |
| 11493 | smp->data.type = SMP_T_BIN; |
| 11494 | smp->flags &= ~SMP_F_CONST; |
| 11495 | free_trash_chunk(smp_trash_alloc); |
| 11496 | return 1; |
| 11497 | |
| 11498 | err: |
| 11499 | free_trash_chunk(smp_trash_alloc); |
| 11500 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11501 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 11502 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11503 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 11504 | /* Argument validation functions */ |
| 11505 | |
| 11506 | /* This function is used to validate the arguments passed to any "x_dn" ssl |
| 11507 | * keywords. These keywords support specifying a third parameter that must be |
| 11508 | * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK. |
| 11509 | */ |
| 11510 | int val_dnfmt(struct arg *arg, char **err_msg) |
| 11511 | { |
| 11512 | if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) { |
| 11513 | memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument."); |
| 11514 | return 0; |
| 11515 | } |
| 11516 | return 1; |
| 11517 | } |
| 11518 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11519 | /* register cli keywords */ |
| 11520 | static struct cli_kw_list cli_kws = {{ },{ |
| 11521 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 11522 | { { "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] | 11523 | { { "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] | 11524 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 11525 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 11526 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 11527 | { { "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] | 11528 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 11529 | { { "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 | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11530 | { { NULL }, NULL, NULL, NULL } |
| 11531 | }}; |
| 11532 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11533 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 11534 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 11535 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11536 | * Please take care of keeping this list alphabetically sorted. |
| 11537 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11538 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 11539 | { "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] | 11540 | { "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] | 11541 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 11542 | { "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] | 11543 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 11544 | { "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] | 11545 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 11546 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 11547 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 11548 | { "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] | 11549 | { "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] | 11550 | { "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] | 11551 | { "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] | 11552 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 11553 | { "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] | 11554 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11555 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 11556 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 11557 | { "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] | 11558 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 11559 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11560 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 11561 | { "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] | 11562 | { "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] | 11563 | { "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] | 11564 | { "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] | 11565 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11566 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11567 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11568 | { "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] | 11569 | { "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] | 11570 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 11571 | { "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] | 11572 | { "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] | 11573 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 11574 | { "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] | 11575 | { "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] | 11576 | { "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] | 11577 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11578 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11579 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11580 | { "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] | 11581 | { "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] | 11582 | { "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] | 11583 | { "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] | 11584 | { "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] | 11585 | { "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] | 11586 | { "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] | 11587 | { "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] | 11588 | { "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] | 11589 | { "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] | 11590 | { "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] | 11591 | { "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] | 11592 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11593 | { "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] | 11594 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 11595 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11596 | { "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] | 11597 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11598 | { "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] | 11599 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 11600 | { "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] | 11601 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11602 | { "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] | 11603 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11604 | { "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] | 11605 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11606 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 11607 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 11608 | { "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] | 11609 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 11610 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 11611 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11612 | { "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] | 11613 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 11614 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11615 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 11616 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 11617 | { "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] | 11618 | { NULL, NULL, 0, 0, 0 }, |
| 11619 | }}; |
| 11620 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11621 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 11622 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 11623 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11624 | * Please take care of keeping this list alphabetically sorted. |
| 11625 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11626 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11627 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 11628 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11629 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 11630 | }}; |
| 11631 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11632 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 11633 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 11634 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11635 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 11636 | * all code contributors. |
| 11637 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 11638 | * the config parser can report an appropriate error when a known keyword was |
| 11639 | * not enabled. |
| 11640 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 11641 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 11642 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 11643 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 11644 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 11645 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11646 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 11647 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 11648 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 11649 | { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 11650 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 11651 | { "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] | 11652 | { "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] | 11653 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 11654 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 11655 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 11656 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 11657 | { NULL, NULL, 0 }, |
| 11658 | }; |
| 11659 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11660 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 11661 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 11662 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 11663 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 11664 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 11665 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 11666 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 11667 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 11668 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 11669 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11670 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 11671 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 11672 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 11673 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 11674 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 11675 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 11676 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 11677 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 11678 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 11679 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 11680 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 11681 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 11682 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 11683 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 11684 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 11685 | { "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] | 11686 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 11687 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 11688 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 11689 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 11690 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 11691 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 11692 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 11693 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 11694 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 11695 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 11696 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 11697 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 11698 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 11699 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 11700 | { NULL, NULL, 0 }, |
| 11701 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11702 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11703 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 11704 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 11705 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11706 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 11707 | * all code contributors. |
| 11708 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 11709 | * the config parser can report an appropriate error when a known keyword was |
| 11710 | * not enabled. |
| 11711 | */ |
| 11712 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 11713 | { "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] | 11714 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 11715 | { "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] | 11716 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 11717 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 11718 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 11719 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11720 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 11721 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 11722 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 11723 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 11724 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 11725 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 11726 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 11727 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 11728 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 11729 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 11730 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 11731 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 11732 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 11733 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 11734 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 11735 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 11736 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 11737 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 11738 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 11739 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 11740 | { "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] | 11741 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 11742 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 11743 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 11744 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 11745 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 11746 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 11747 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 11748 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 11749 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 11750 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 11751 | { "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] | 11752 | { NULL, NULL, 0, 0 }, |
| 11753 | }}; |
| 11754 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11755 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 11756 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 11757 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 11758 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 11759 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 11760 | { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 11761 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 11762 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 11763 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 11764 | #ifndef OPENSSL_NO_DH |
| 11765 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 11766 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 11767 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11768 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11769 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11770 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 11771 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 11772 | #ifndef OPENSSL_NO_DH |
| 11773 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 11774 | #endif |
| 11775 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 11776 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 11777 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 11778 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 11779 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 11780 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 11781 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11782 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 11783 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 11784 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 11785 | #endif |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 11786 | { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 11787 | { 0, NULL, NULL }, |
| 11788 | }}; |
| 11789 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11790 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11791 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 11792 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11793 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 11794 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 11795 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 11796 | #endif |
| 11797 | { NULL, NULL, 0, 0, 0 }, |
| 11798 | }}; |
| 11799 | |
| 11800 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 11801 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 11802 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 11803 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11804 | .snd_buf = ssl_sock_from_buf, |
| 11805 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 11806 | .subscribe = ssl_subscribe, |
| 11807 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 11808 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 11809 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11810 | .rcv_pipe = NULL, |
| 11811 | .snd_pipe = NULL, |
| 11812 | .shutr = NULL, |
| 11813 | .shutw = ssl_sock_shutw, |
| 11814 | .close = ssl_sock_close, |
| 11815 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 11816 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 11817 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 11818 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 11819 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 11820 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 11821 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11822 | }; |
| 11823 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 11824 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 11825 | struct session *sess, struct stream *s, int flags) |
| 11826 | { |
| 11827 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 11828 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 11829 | |
| 11830 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 11831 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 11832 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 11833 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 11834 | 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] | 11835 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 11836 | s->req.flags |= CF_READ_NULL; |
| 11837 | return ACT_RET_YIELD; |
| 11838 | } |
| 11839 | } |
| 11840 | return (ACT_RET_CONT); |
| 11841 | } |
| 11842 | |
| 11843 | 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) |
| 11844 | { |
| 11845 | rule->action_ptr = ssl_action_wait_for_hs; |
| 11846 | |
| 11847 | return ACT_RET_PRS_OK; |
| 11848 | } |
| 11849 | |
| 11850 | static struct action_kw_list http_req_actions = {ILH, { |
| 11851 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 11852 | { /* END */ } |
| 11853 | }}; |
| 11854 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11855 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 11856 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 11857 | #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] | 11858 | |
| 11859 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 11860 | { |
| 11861 | if (ptr) { |
| 11862 | chunk_destroy(ptr); |
| 11863 | free(ptr); |
| 11864 | } |
| 11865 | } |
| 11866 | |
| 11867 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 11868 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 11869 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11870 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 11871 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 11872 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11873 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 11874 | static void __ssl_sock_init(void) |
| 11875 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 11876 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11877 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 11878 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 11879 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11880 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 11881 | if (global_ssl.listen_default_ciphers) |
| 11882 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 11883 | if (global_ssl.connect_default_ciphers) |
| 11884 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 11885 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 11886 | if (global_ssl.listen_default_ciphersuites) |
| 11887 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 11888 | if (global_ssl.connect_default_ciphersuites) |
| 11889 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 11890 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 11891 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 11892 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 11893 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11894 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 11895 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 11896 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11897 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 11898 | n = sk_SSL_COMP_num(cm); |
| 11899 | while (n--) { |
| 11900 | (void) sk_SSL_COMP_pop(cm); |
| 11901 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 11902 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 11903 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 11904 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 11905 | ssl_locking_init(); |
| 11906 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 11907 | #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] | 11908 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 11909 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 11910 | 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] | 11911 | 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] | 11912 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11913 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 11914 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11915 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 11916 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 11917 | hap_register_post_check(tlskeys_finalize_config); |
| 11918 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11919 | |
| 11920 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 11921 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 11922 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 11923 | hap_register_post_deinit(ssl_free_global_issuers); |
| 11924 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11925 | #ifndef OPENSSL_NO_DH |
| 11926 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 11927 | hap_register_post_deinit(ssl_free_dh); |
| 11928 | #endif |
| 11929 | #ifndef OPENSSL_NO_ENGINE |
| 11930 | hap_register_post_deinit(ssl_free_engines); |
| 11931 | #endif |
| 11932 | /* Load SSL string for the verbose & debug mode. */ |
| 11933 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 11934 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 11935 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 11936 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 11937 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 11938 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 11939 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 11940 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 11941 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11942 | |
| 11943 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11944 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 11945 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11946 | /* Compute and register the version string */ |
| 11947 | static void ssl_register_build_options() |
| 11948 | { |
| 11949 | char *ptr = NULL; |
| 11950 | int i; |
| 11951 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11952 | memprintf(&ptr, "Built with OpenSSL version : " |
| 11953 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 11954 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11955 | #else /* OPENSSL_IS_BORINGSSL */ |
| 11956 | OPENSSL_VERSION_TEXT |
| 11957 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 11958 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 11959 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11960 | #endif |
| 11961 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 11962 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11963 | "no (library version too old)" |
| 11964 | #elif defined(OPENSSL_NO_TLSEXT) |
| 11965 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 11966 | #else |
| 11967 | "yes" |
| 11968 | #endif |
| 11969 | "", ptr); |
| 11970 | |
| 11971 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 11972 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11973 | "yes" |
| 11974 | #else |
| 11975 | #ifdef OPENSSL_NO_TLSEXT |
| 11976 | "no (because of OPENSSL_NO_TLSEXT)" |
| 11977 | #else |
| 11978 | "no (version might be too old, 0.9.8f min needed)" |
| 11979 | #endif |
| 11980 | #endif |
| 11981 | "", ptr); |
| 11982 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 11983 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 11984 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 11985 | if (methodVersions[i].option) |
| 11986 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 11987 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11988 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11989 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11990 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11991 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 11992 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11993 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11994 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11995 | void ssl_free_engines(void) { |
| 11996 | struct ssl_engine_list *wl, *wlb; |
| 11997 | /* free up engine list */ |
| 11998 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 11999 | ENGINE_finish(wl->e); |
| 12000 | ENGINE_free(wl->e); |
| 12001 | LIST_DEL(&wl->list); |
| 12002 | free(wl); |
| 12003 | } |
| 12004 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 12005 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 12006 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12007 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12008 | void ssl_free_dh(void) { |
| 12009 | if (local_dh_1024) { |
| 12010 | DH_free(local_dh_1024); |
| 12011 | local_dh_1024 = NULL; |
| 12012 | } |
| 12013 | if (local_dh_2048) { |
| 12014 | DH_free(local_dh_2048); |
| 12015 | local_dh_2048 = NULL; |
| 12016 | } |
| 12017 | if (local_dh_4096) { |
| 12018 | DH_free(local_dh_4096); |
| 12019 | local_dh_4096 = NULL; |
| 12020 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 12021 | if (global_dh) { |
| 12022 | DH_free(global_dh); |
| 12023 | global_dh = NULL; |
| 12024 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 12025 | } |
| 12026 | #endif |
| 12027 | |
| 12028 | __attribute__((destructor)) |
| 12029 | static void __ssl_sock_deinit(void) |
| 12030 | { |
| 12031 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12032 | if (ssl_ctx_lru_tree) { |
| 12033 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 12034 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 12035 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12036 | #endif |
| 12037 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12038 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12039 | ERR_remove_state(0); |
| 12040 | ERR_free_strings(); |
| 12041 | |
| 12042 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 12043 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12044 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 12045 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12046 | CRYPTO_cleanup_all_ex_data(); |
| 12047 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 12048 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 12049 | } |
| 12050 | |
| 12051 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 12052 | /* |
| 12053 | * Local variables: |
| 12054 | * c-indent-level: 8 |
| 12055 | * c-basic-offset: 8 |
| 12056 | * End: |
| 12057 | */ |