Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 11 | * Acknowledgement: |
| 12 | * We'd like to specially thank the Stud project authors for a very clean |
| 13 | * and well documented code which helped us understand how the OpenSSL API |
| 14 | * ought to be used in non-blocking mode. This is one difficult part which |
| 15 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 16 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 17 | * the good works, guys ! |
| 18 | * |
| 19 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 20 | * particularly well with haproxy. For more info about this project, visit : |
| 21 | * https://github.com/bumptech/stud |
| 22 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 34 | |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | #include <netinet/tcp.h> |
| 40 | |
| 41 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 42 | #include <openssl/x509.h> |
| 43 | #include <openssl/x509v3.h> |
| 44 | #include <openssl/x509.h> |
| 45 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 46 | #include <openssl/rand.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 47 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 48 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 49 | #include <common/buffer.h> |
| 50 | #include <common/compat.h> |
| 51 | #include <common/config.h> |
| 52 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 53 | #include <common/errors.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> |
| 57 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 58 | #include <ebsttree.h> |
| 59 | |
| 60 | #include <types/global.h> |
| 61 | #include <types/ssl_sock.h> |
| 62 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 63 | #include <proto/acl.h> |
| 64 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 65 | #include <proto/connection.h> |
| 66 | #include <proto/fd.h> |
| 67 | #include <proto/freq_ctr.h> |
| 68 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 69 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 70 | #include <proto/pattern.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 71 | #include <proto/server.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 73 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 74 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 75 | #include <proto/ssl_sock.h> |
| 76 | #include <proto/task.h> |
| 77 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 78 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 79 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 80 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 81 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 82 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 83 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 84 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 85 | |
| 86 | /* Verify errors macros */ |
| 87 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 88 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 89 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 90 | |
| 91 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 92 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 93 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 94 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 95 | /* server and bind verify method, it uses a global value as default */ |
| 96 | enum { |
| 97 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 98 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 99 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 100 | SSL_SOCK_VERIFY_NONE = 3, |
| 101 | }; |
| 102 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 103 | int sslconns = 0; |
| 104 | int totalsslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 105 | |
| 106 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 107 | { |
| 108 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
| 109 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 110 | BIO *write_bio; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 111 | |
| 112 | if (where & SSL_CB_HANDSHAKE_START) { |
| 113 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 114 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 115 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 116 | conn->err_code = CO_ER_SSL_RENEG; |
| 117 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 118 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 119 | |
| 120 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 121 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 122 | /* Long certificate chains optimz |
| 123 | If write and read bios are differents, we |
| 124 | consider that the buffering was activated, |
| 125 | so we rise the output buffer size from 4k |
| 126 | to 16k */ |
| 127 | write_bio = SSL_get_wbio(ssl); |
| 128 | if (write_bio != SSL_get_rbio(ssl)) { |
| 129 | BIO_set_write_buffer_size(write_bio, 16384); |
| 130 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 131 | } |
| 132 | } |
| 133 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 136 | /* Callback is called for each certificate of the chain during a verify |
| 137 | ok is set to 1 if preverify detect no error on current certificate. |
| 138 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 139 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 140 | { |
| 141 | SSL *ssl; |
| 142 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 143 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 144 | |
| 145 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 146 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 147 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 148 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 149 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 150 | if (ok) /* no errors */ |
| 151 | return ok; |
| 152 | |
| 153 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 154 | err = X509_STORE_CTX_get_error(x_store); |
| 155 | |
| 156 | /* check if CA error needs to be ignored */ |
| 157 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 158 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 159 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 160 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 163 | if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
| 164 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 165 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 166 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 167 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 168 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 172 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 173 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 174 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 175 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 176 | if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
| 177 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 178 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 179 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 181 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 182 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 185 | /* Callback is called for ssl protocol analyse */ |
| 186 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 187 | { |
| 188 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
| 189 | |
| 190 | #ifdef TLS1_RT_HEARTBEAT |
| 191 | /* test heartbeat received (write_p is set to 0 |
| 192 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 193 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
| 194 | const unsigned char *p = buf; |
| 195 | unsigned int payload; |
| 196 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 197 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 198 | |
| 199 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 200 | if (*p != TLS1_HB_REQUEST) |
| 201 | return; |
| 202 | |
| 203 | if (len < 1 + 2 + 16) |
| 204 | goto kill_it; |
| 205 | |
| 206 | payload = (p[1] * 256) + p[2]; |
| 207 | if (1 + 2 + payload + 16 <= len) |
| 208 | return; /* OK no problem */ |
| 209 | kill_it: |
| 210 | /* we have a clear heartbleed attack (CVE-2014-0160), |
| 211 | * we can't know if the SSL stack is patched, so better |
| 212 | * kill the connection before OpenSSL tries to send the |
| 213 | * bytes back to the attacker. It will be reported above |
| 214 | * as SSL_ERROR_SSL while an other handshake failure with |
| 215 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 216 | */ |
| 217 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 218 | return; |
| 219 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 220 | #endif |
| 221 | } |
| 222 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 223 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 224 | /* This callback is used so that the server advertises the list of |
| 225 | * negociable protocols for NPN. |
| 226 | */ |
| 227 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 228 | unsigned int *len, void *arg) |
| 229 | { |
| 230 | struct bind_conf *conf = arg; |
| 231 | |
| 232 | *data = (const unsigned char *)conf->npn_str; |
| 233 | *len = conf->npn_len; |
| 234 | return SSL_TLSEXT_ERR_OK; |
| 235 | } |
| 236 | #endif |
| 237 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 238 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 239 | /* This callback is used so that the server advertises the list of |
| 240 | * negociable protocols for ALPN. |
| 241 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 242 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 243 | unsigned char *outlen, |
| 244 | const unsigned char *server, |
| 245 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 246 | { |
| 247 | struct bind_conf *conf = arg; |
| 248 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 249 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 250 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 251 | return SSL_TLSEXT_ERR_NOACK; |
| 252 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 253 | return SSL_TLSEXT_ERR_OK; |
| 254 | } |
| 255 | #endif |
| 256 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 257 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 258 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 259 | * warning when no match is found, which implies the default (first) cert |
| 260 | * will keep being used. |
| 261 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 262 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 263 | { |
| 264 | const char *servername; |
| 265 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 266 | struct ebmb_node *node, *n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 267 | int i; |
| 268 | (void)al; /* shut gcc stupid warning */ |
| 269 | |
| 270 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 271 | if (!servername) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 272 | return (s->strict_sni ? |
| 273 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 274 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 275 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 276 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 277 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 278 | if (!servername[i]) |
| 279 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 280 | trash.str[i] = tolower(servername[i]); |
| 281 | if (!wildp && (trash.str[i] == '.')) |
| 282 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 283 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 284 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 285 | |
| 286 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 287 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 288 | |
| 289 | /* lookup a not neg filter */ |
| 290 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 291 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 292 | node = n; |
| 293 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 294 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 295 | } |
| 296 | if (!node && wildp) { |
| 297 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 298 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 299 | } |
| 300 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
| 301 | return (s->strict_sni ? |
| 302 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 303 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* switch ctx */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 307 | SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 308 | return SSL_TLSEXT_ERR_OK; |
| 309 | } |
| 310 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 311 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 312 | #ifndef OPENSSL_NO_DH |
| 313 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 314 | if an error occured, and 0 if parameter not found. */ |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame^] | 315 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 316 | { |
| 317 | int ret = -1; |
| 318 | BIO *in; |
| 319 | DH *dh = NULL; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame^] | 320 | /* If not present, use parameters generated using 'openssl dhparam 1024 -C': |
| 321 | * -----BEGIN DH PARAMETERS----- |
| 322 | * MIGHAoGBAJJAJDXDoS5E03MNjnjK36eOL1tRqVa/9NuOVlI+lpXmPjJQbP65EvKn |
| 323 | * fSLnG7VMhoCJO4KtG88zf393ltP7loGB2bofcDSr+x+XsxBM8yA/Zj6BmQt+CQ9s |
| 324 | * TF7hoOV+wXTT6ErZ5y5qx9pq6hLfKXwTGFT78hrE6HnCO7xgtPdTAgEC |
| 325 | * -----END DH PARAMETERS----- |
| 326 | */ |
| 327 | static const unsigned char dh1024_p[] = { |
| 328 | 0x92, 0x40, 0x24, 0x35, 0xC3, 0xA1, 0x2E, 0x44, 0xD3, 0x73, 0x0D, 0x8E, |
| 329 | 0x78, 0xCA, 0xDF, 0xA7, 0x8E, 0x2F, 0x5B, 0x51, 0xA9, 0x56, 0xBF, 0xF4, |
| 330 | 0xDB, 0x8E, 0x56, 0x52, 0x3E, 0x96, 0x95, 0xE6, 0x3E, 0x32, 0x50, 0x6C, |
| 331 | 0xFE, 0xB9, 0x12, 0xF2, 0xA7, 0x7D, 0x22, 0xE7, 0x1B, 0xB5, 0x4C, 0x86, |
| 332 | 0x80, 0x89, 0x3B, 0x82, 0xAD, 0x1B, 0xCF, 0x33, 0x7F, 0x7F, 0x77, 0x96, |
| 333 | 0xD3, 0xFB, 0x96, 0x81, 0x81, 0xD9, 0xBA, 0x1F, 0x70, 0x34, 0xAB, 0xFB, |
| 334 | 0x1F, 0x97, 0xB3, 0x10, 0x4C, 0xF3, 0x20, 0x3F, 0x66, 0x3E, 0x81, 0x99, |
| 335 | 0x0B, 0x7E, 0x09, 0x0F, 0x6C, 0x4C, 0x5E, 0xE1, 0xA0, 0xE5, 0x7E, 0xC1, |
| 336 | 0x74, 0xD3, 0xE8, 0x4A, 0xD9, 0xE7, 0x2E, 0x6A, 0xC7, 0xDA, 0x6A, 0xEA, |
| 337 | 0x12, 0xDF, 0x29, 0x7C, 0x13, 0x18, 0x54, 0xFB, 0xF2, 0x1A, 0xC4, 0xE8, |
| 338 | 0x79, 0xC2, 0x3B, 0xBC, 0x60, 0xB4, 0xF7, 0x53, |
| 339 | }; |
| 340 | static const unsigned char dh1024_g[] = { |
| 341 | 0x02, |
| 342 | }; |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 343 | |
| 344 | in = BIO_new(BIO_s_file()); |
| 345 | if (in == NULL) |
| 346 | goto end; |
| 347 | |
| 348 | if (BIO_read_filename(in, file) <= 0) |
| 349 | goto end; |
| 350 | |
| 351 | dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame^] | 352 | if (!dh) { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 353 | /* Clear openssl global errors stack */ |
| 354 | ERR_clear_error(); |
| 355 | |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame^] | 356 | dh = DH_new(); |
| 357 | if (dh == NULL) |
| 358 | goto end; |
| 359 | |
| 360 | dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL); |
| 361 | if (dh->p == NULL) |
| 362 | goto end; |
| 363 | |
| 364 | dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL); |
| 365 | if (dh->g == NULL) |
| 366 | goto end; |
| 367 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 368 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 369 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame^] | 370 | else |
| 371 | ret = 1; |
| 372 | |
| 373 | SSL_CTX_set_tmp_dh(ctx, dh); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 374 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 375 | end: |
| 376 | if (dh) |
| 377 | DH_free(dh); |
| 378 | |
| 379 | if (in) |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 380 | BIO_free(in); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | #endif |
| 385 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 386 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 387 | { |
| 388 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 389 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 390 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 391 | if (*name == '!') { |
| 392 | neg = 1; |
| 393 | name++; |
| 394 | } |
| 395 | if (*name == '*') { |
| 396 | wild = 1; |
| 397 | name++; |
| 398 | } |
| 399 | /* !* filter is a nop */ |
| 400 | if (neg && wild) |
| 401 | return order; |
| 402 | if (*name) { |
| 403 | int j, len; |
| 404 | len = strlen(name); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 405 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 406 | for (j = 0; j < len; j++) |
| 407 | sc->name.key[j] = tolower(name[j]); |
| 408 | sc->name.key[len] = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 409 | sc->ctx = ctx; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 410 | sc->order = order++; |
| 411 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 412 | if (wild) |
| 413 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 414 | else |
| 415 | ebst_insert(&s->sni_ctx, &sc->name); |
| 416 | } |
| 417 | return order; |
| 418 | } |
| 419 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 420 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 421 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 422 | */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 423 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 424 | { |
| 425 | BIO *in; |
| 426 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 427 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 428 | int ret = -1; |
| 429 | int order = 0; |
| 430 | X509_NAME *xname; |
| 431 | char *str; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 432 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 433 | STACK_OF(GENERAL_NAME) *names; |
| 434 | #endif |
| 435 | |
| 436 | in = BIO_new(BIO_s_file()); |
| 437 | if (in == NULL) |
| 438 | goto end; |
| 439 | |
| 440 | if (BIO_read_filename(in, file) <= 0) |
| 441 | goto end; |
| 442 | |
| 443 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 444 | if (x == NULL) |
| 445 | goto end; |
| 446 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 447 | if (fcount) { |
| 448 | while (fcount--) |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 449 | order = ssl_sock_add_cert_sni(ctx, s, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 450 | } |
| 451 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 452 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 453 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 454 | if (names) { |
| 455 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 456 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 457 | if (name->type == GEN_DNS) { |
| 458 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 459 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 460 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 461 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 462 | } |
| 463 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 464 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 465 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 466 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 467 | xname = X509_get_subject_name(x); |
| 468 | i = -1; |
| 469 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 470 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 471 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 472 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 473 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 474 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 479 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 480 | goto end; |
| 481 | |
| 482 | if (ctx->extra_certs != NULL) { |
| 483 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 484 | ctx->extra_certs = NULL; |
| 485 | } |
| 486 | |
| 487 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 488 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 489 | X509_free(ca); |
| 490 | goto end; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | err = ERR_get_error(); |
| 495 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 496 | /* we successfully reached the last cert in the file */ |
| 497 | ret = 1; |
| 498 | } |
| 499 | ERR_clear_error(); |
| 500 | |
| 501 | end: |
| 502 | if (x) |
| 503 | X509_free(x); |
| 504 | |
| 505 | if (in) |
| 506 | BIO_free(in); |
| 507 | |
| 508 | return ret; |
| 509 | } |
| 510 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 511 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 512 | { |
| 513 | int ret; |
| 514 | SSL_CTX *ctx; |
| 515 | |
| 516 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 517 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 518 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 519 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 524 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 525 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 526 | SSL_CTX_free(ctx); |
| 527 | return 1; |
| 528 | } |
| 529 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 530 | ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, sni_filter, fcount); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 531 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 532 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 533 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 534 | if (ret < 0) /* serious error, must do that ourselves */ |
| 535 | SSL_CTX_free(ctx); |
| 536 | return 1; |
| 537 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 538 | |
| 539 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 540 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 541 | err && *err ? *err : "", path); |
| 542 | return 1; |
| 543 | } |
| 544 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 545 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 546 | * the tree, so it will be discovered and cleaned in time. |
| 547 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 548 | #ifndef OPENSSL_NO_DH |
| 549 | ret = ssl_sock_load_dh_params(ctx, path); |
| 550 | if (ret < 0) { |
| 551 | if (err) |
| 552 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 553 | *err ? *err : "", path); |
| 554 | return 1; |
| 555 | } |
| 556 | #endif |
| 557 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 558 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 559 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 560 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 561 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 562 | return 1; |
| 563 | } |
| 564 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 565 | if (!bind_conf->default_ctx) |
| 566 | bind_conf->default_ctx = ctx; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 571 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 572 | { |
| 573 | struct dirent *de; |
| 574 | DIR *dir; |
| 575 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 576 | char *end; |
| 577 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 578 | int cfgerr = 0; |
| 579 | |
| 580 | if (!(dir = opendir(path))) |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 581 | return ssl_sock_load_cert_file(path, bind_conf, curproxy, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 582 | |
| 583 | /* strip trailing slashes, including first one */ |
| 584 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 585 | *end = 0; |
| 586 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 587 | while ((de = readdir(dir))) { |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 588 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 589 | if (stat(fp, &buf) != 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 590 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 591 | err && *err ? *err : "", fp, strerror(errno)); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 592 | cfgerr++; |
| 593 | continue; |
| 594 | } |
| 595 | if (!S_ISREG(buf.st_mode)) |
| 596 | continue; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 597 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 598 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 599 | closedir(dir); |
| 600 | return cfgerr; |
| 601 | } |
| 602 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 603 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 604 | * done once. Zero is returned if the operation fails. No error is returned |
| 605 | * if the random is said as not implemented, because we expect that openssl |
| 606 | * will use another method once needed. |
| 607 | */ |
| 608 | static int ssl_initialize_random() |
| 609 | { |
| 610 | unsigned char random; |
| 611 | static int random_initialized = 0; |
| 612 | |
| 613 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 614 | random_initialized = 1; |
| 615 | |
| 616 | return random_initialized; |
| 617 | } |
| 618 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 619 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 620 | { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 621 | char thisline[LINESIZE]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 622 | FILE *f; |
| 623 | int linenum = 0; |
| 624 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 625 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 626 | if ((f = fopen(file, "r")) == NULL) { |
| 627 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 628 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 629 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 630 | |
| 631 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 632 | int arg; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 633 | int newarg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 634 | char *end; |
| 635 | char *args[MAX_LINE_ARGS + 1]; |
| 636 | char *line = thisline; |
| 637 | |
| 638 | linenum++; |
| 639 | end = line + strlen(line); |
| 640 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 641 | /* Check if we reached the limit and the last char is not \n. |
| 642 | * Watch out for the last line without the terminating '\n'! |
| 643 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 644 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 645 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 646 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 647 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 650 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 651 | newarg = 1; |
| 652 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 653 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 654 | /* end of string, end of loop */ |
| 655 | *line = 0; |
| 656 | break; |
| 657 | } |
| 658 | else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 659 | newarg = 1; |
| 660 | *line = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 661 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 662 | else if (newarg) { |
| 663 | if (arg == MAX_LINE_ARGS) { |
| 664 | memprintf(err, "too many args on line %d in file '%s'.", |
| 665 | linenum, file); |
| 666 | cfgerr = 1; |
| 667 | break; |
| 668 | } |
| 669 | newarg = 0; |
| 670 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 671 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 672 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 673 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 674 | if (cfgerr) |
| 675 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 676 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 677 | /* empty line */ |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 678 | if (!arg) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 679 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 680 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 681 | cfgerr = ssl_sock_load_cert_file(args[0], bind_conf, curproxy, &args[1], arg-1, err); |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 682 | if (cfgerr) { |
| 683 | 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] | 684 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 685 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 686 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 687 | fclose(f); |
| 688 | return cfgerr; |
| 689 | } |
| 690 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 691 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 692 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 693 | #endif |
| 694 | |
| 695 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 696 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 697 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 698 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 699 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 700 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 701 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 702 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 703 | #define SSL_OP_NO_TICKET 0 |
| 704 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 705 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 706 | #define SSL_OP_NO_COMPRESSION 0 |
| 707 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 708 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 709 | #define SSL_OP_NO_TLSv1_1 0 |
| 710 | #endif |
| 711 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 712 | #define SSL_OP_NO_TLSv1_2 0 |
| 713 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 714 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 715 | #define SSL_OP_SINGLE_DH_USE 0 |
| 716 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 717 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 718 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 719 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 720 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 721 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 722 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 723 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 724 | { |
| 725 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 726 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 727 | int ssloptions = |
| 728 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 729 | SSL_OP_NO_SSLv2 | |
| 730 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 731 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 732 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 733 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 734 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 735 | int sslmode = |
| 736 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 737 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 738 | SSL_MODE_RELEASE_BUFFERS; |
| 739 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 740 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 741 | if (!ssl_initialize_random()) { |
| 742 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 743 | cfgerr++; |
| 744 | } |
| 745 | |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 746 | if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 747 | ssloptions |= SSL_OP_NO_SSLv3; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 748 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 749 | ssloptions |= SSL_OP_NO_TLSv1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 750 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 751 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 752 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 753 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 754 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 755 | ssloptions |= SSL_OP_NO_TICKET; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 756 | if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3) |
| 757 | SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()); |
| 758 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10) |
| 759 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 760 | #if SSL_OP_NO_TLSv1_1 |
| 761 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11) |
| 762 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 763 | #endif |
| 764 | #if SSL_OP_NO_TLSv1_2 |
| 765 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12) |
| 766 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 767 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 768 | |
| 769 | SSL_CTX_set_options(ctx, ssloptions); |
| 770 | SSL_CTX_set_mode(ctx, sslmode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 771 | switch (bind_conf->verify) { |
| 772 | case SSL_SOCK_VERIFY_NONE: |
| 773 | verify = SSL_VERIFY_NONE; |
| 774 | break; |
| 775 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 776 | verify = SSL_VERIFY_PEER; |
| 777 | break; |
| 778 | case SSL_SOCK_VERIFY_REQUIRED: |
| 779 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 780 | break; |
| 781 | } |
| 782 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 783 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 784 | if (bind_conf->ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 785 | /* load CAfile to verify */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 786 | if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 787 | Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 788 | curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 789 | cfgerr++; |
| 790 | } |
| 791 | /* set CA names fo client cert request, function returns void */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 792 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file)); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 793 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 794 | else { |
| 795 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 796 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 797 | cfgerr++; |
| 798 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 799 | #ifdef X509_V_FLAG_CRL_CHECK |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 800 | if (bind_conf->crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 801 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 802 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 803 | if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 804 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 805 | curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 806 | cfgerr++; |
| 807 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 808 | else { |
| 809 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 810 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 811 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 812 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 813 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 814 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 815 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 816 | if (global.tune.ssllifetime) |
| 817 | SSL_CTX_set_timeout(ctx, global.tune.ssllifetime); |
| 818 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 819 | shared_context_set_cache(ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 820 | if (bind_conf->ciphers && |
| 821 | !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 822 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 823 | curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 824 | cfgerr++; |
| 825 | } |
| 826 | |
| 827 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 828 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
| 829 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 830 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 831 | if (bind_conf->npn_str) |
| 832 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf); |
| 833 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 834 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 835 | if (bind_conf->alpn_str) |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 836 | SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, bind_conf); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 837 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 838 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 839 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 840 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 841 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 842 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 843 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 844 | { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 845 | int i; |
| 846 | EC_KEY *ecdh; |
| 847 | |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 848 | i = OBJ_sn2nid(bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 849 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 850 | Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 851 | curproxy->id, bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE, |
| 852 | bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 853 | cfgerr++; |
| 854 | } |
| 855 | else { |
| 856 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 857 | EC_KEY_free(ecdh); |
| 858 | } |
| 859 | } |
| 860 | #endif |
| 861 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 862 | return cfgerr; |
| 863 | } |
| 864 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 865 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 866 | { |
| 867 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 868 | size_t prefixlen, suffixlen; |
| 869 | |
| 870 | /* Trivial case */ |
| 871 | if (strcmp(pattern, hostname) == 0) |
| 872 | return 1; |
| 873 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 874 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 875 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 876 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 877 | pattern_wildcard = NULL; |
| 878 | pattern_left_label_end = pattern; |
| 879 | while (*pattern_left_label_end != '.') { |
| 880 | switch (*pattern_left_label_end) { |
| 881 | case 0: |
| 882 | /* End of label not found */ |
| 883 | return 0; |
| 884 | case '*': |
| 885 | /* If there is more than one wildcards */ |
| 886 | if (pattern_wildcard) |
| 887 | return 0; |
| 888 | pattern_wildcard = pattern_left_label_end; |
| 889 | break; |
| 890 | } |
| 891 | pattern_left_label_end++; |
| 892 | } |
| 893 | |
| 894 | /* If it's not trivial and there is no wildcard, it can't |
| 895 | * match */ |
| 896 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 897 | return 0; |
| 898 | |
| 899 | /* Make sure all labels match except the leftmost */ |
| 900 | hostname_left_label_end = strchr(hostname, '.'); |
| 901 | if (!hostname_left_label_end |
| 902 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 903 | return 0; |
| 904 | |
| 905 | /* Make sure the leftmost label of the hostname is long enough |
| 906 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 907 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 908 | return 0; |
| 909 | |
| 910 | /* Finally compare the string on either side of the |
| 911 | * wildcard */ |
| 912 | prefixlen = pattern_wildcard - pattern; |
| 913 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 914 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 915 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 916 | return 0; |
| 917 | |
| 918 | return 1; |
| 919 | } |
| 920 | |
| 921 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 922 | { |
| 923 | SSL *ssl; |
| 924 | struct connection *conn; |
| 925 | char *servername; |
| 926 | |
| 927 | int depth; |
| 928 | X509 *cert; |
| 929 | STACK_OF(GENERAL_NAME) *alt_names; |
| 930 | int i; |
| 931 | X509_NAME *cert_subject; |
| 932 | char *str; |
| 933 | |
| 934 | if (ok == 0) |
| 935 | return ok; |
| 936 | |
| 937 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 938 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 939 | |
| 940 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 941 | |
| 942 | /* We only need to verify the CN on the actual server cert, |
| 943 | * not the indirect CAs */ |
| 944 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 945 | if (depth != 0) |
| 946 | return ok; |
| 947 | |
| 948 | /* At this point, the cert is *not* OK unless we can find a |
| 949 | * hostname match */ |
| 950 | ok = 0; |
| 951 | |
| 952 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 953 | /* It seems like this might happen if verify peer isn't set */ |
| 954 | if (!cert) |
| 955 | return ok; |
| 956 | |
| 957 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 958 | if (alt_names) { |
| 959 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 960 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 961 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 962 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 963 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 964 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 965 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 966 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 967 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 968 | OPENSSL_free(str); |
| 969 | } |
| 970 | } |
| 971 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 972 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | cert_subject = X509_get_subject_name(cert); |
| 976 | i = -1; |
| 977 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 978 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
| 979 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 980 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 981 | OPENSSL_free(str); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return ok; |
| 986 | } |
| 987 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 988 | /* prepare ssl context from servers options. Returns an error count */ |
| 989 | int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy) |
| 990 | { |
| 991 | int cfgerr = 0; |
| 992 | int options = |
| 993 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 994 | SSL_OP_NO_SSLv2 | |
| 995 | SSL_OP_NO_COMPRESSION; |
| 996 | int mode = |
| 997 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 998 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 999 | SSL_MODE_RELEASE_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1000 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1001 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1002 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 1003 | if (!ssl_initialize_random()) { |
| 1004 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 1005 | cfgerr++; |
| 1006 | } |
| 1007 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1008 | /* Initiate SSL context for current server */ |
| 1009 | srv->ssl_ctx.reused_sess = NULL; |
| 1010 | if (srv->use_ssl) |
| 1011 | srv->xprt = &ssl_sock; |
| 1012 | if (srv->check.use_ssl) |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1013 | srv->check_common.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1014 | |
| 1015 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 1016 | if (!srv->ssl_ctx.ctx) { |
| 1017 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 1018 | proxy_type_str(curproxy), curproxy->id, |
| 1019 | srv->id); |
| 1020 | cfgerr++; |
| 1021 | return cfgerr; |
| 1022 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 1023 | if (srv->ssl_ctx.client_crt) { |
| 1024 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 1025 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 1026 | proxy_type_str(curproxy), curproxy->id, |
| 1027 | srv->id, srv->ssl_ctx.client_crt); |
| 1028 | cfgerr++; |
| 1029 | } |
| 1030 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 1031 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 1032 | proxy_type_str(curproxy), curproxy->id, |
| 1033 | srv->id, srv->ssl_ctx.client_crt); |
| 1034 | cfgerr++; |
| 1035 | } |
| 1036 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 1037 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 1038 | proxy_type_str(curproxy), curproxy->id, |
| 1039 | srv->id, srv->ssl_ctx.client_crt); |
| 1040 | cfgerr++; |
| 1041 | } |
| 1042 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1043 | |
| 1044 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 1045 | options |= SSL_OP_NO_SSLv3; |
| 1046 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 1047 | options |= SSL_OP_NO_TLSv1; |
| 1048 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 1049 | options |= SSL_OP_NO_TLSv1_1; |
| 1050 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 1051 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 1052 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 1053 | options |= SSL_OP_NO_TICKET; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1054 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) |
| 1055 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method()); |
| 1056 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 1057 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 1058 | #if SSL_OP_NO_TLSv1_1 |
| 1059 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 1060 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 1061 | #endif |
| 1062 | #if SSL_OP_NO_TLSv1_2 |
| 1063 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 1064 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 1065 | #endif |
| 1066 | |
| 1067 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 1068 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1069 | |
| 1070 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 1071 | verify = SSL_VERIFY_PEER; |
| 1072 | |
| 1073 | switch (srv->ssl_ctx.verify) { |
| 1074 | case SSL_SOCK_VERIFY_NONE: |
| 1075 | verify = SSL_VERIFY_NONE; |
| 1076 | break; |
| 1077 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1078 | verify = SSL_VERIFY_PEER; |
| 1079 | break; |
| 1080 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1081 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1082 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1083 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1084 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1085 | if (srv->ssl_ctx.ca_file) { |
| 1086 | /* load CAfile to verify */ |
| 1087 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1088 | Alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1089 | curproxy->id, srv->id, |
| 1090 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 1091 | cfgerr++; |
| 1092 | } |
| 1093 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1094 | else { |
| 1095 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1096 | 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", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1097 | curproxy->id, srv->id, |
| 1098 | srv->conf.file, srv->conf.line); |
| 1099 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1100 | Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1101 | curproxy->id, srv->id, |
| 1102 | srv->conf.file, srv->conf.line); |
| 1103 | cfgerr++; |
| 1104 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1105 | #ifdef X509_V_FLAG_CRL_CHECK |
| 1106 | if (srv->ssl_ctx.crl_file) { |
| 1107 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 1108 | |
| 1109 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1110 | Alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1111 | curproxy->id, srv->id, |
| 1112 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 1113 | cfgerr++; |
| 1114 | } |
| 1115 | else { |
| 1116 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 1117 | } |
| 1118 | } |
| 1119 | #endif |
| 1120 | } |
| 1121 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 1122 | if (global.tune.ssllifetime) |
| 1123 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime); |
| 1124 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1125 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 1126 | if (srv->ssl_ctx.ciphers && |
| 1127 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 1128 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 1129 | curproxy->id, srv->id, |
| 1130 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 1131 | cfgerr++; |
| 1132 | } |
| 1133 | |
| 1134 | return cfgerr; |
| 1135 | } |
| 1136 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1137 | /* 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] | 1138 | * be NULL, in which case nothing is done. Returns the number of errors |
| 1139 | * encountered. |
| 1140 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1141 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1142 | { |
| 1143 | struct ebmb_node *node; |
| 1144 | struct sni_ctx *sni; |
| 1145 | int err = 0; |
| 1146 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1147 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1148 | return 0; |
| 1149 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1150 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1151 | while (node) { |
| 1152 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1153 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1154 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1155 | node = ebmb_next(node); |
| 1156 | } |
| 1157 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1158 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1159 | while (node) { |
| 1160 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1161 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1162 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1163 | node = ebmb_next(node); |
| 1164 | } |
| 1165 | return err; |
| 1166 | } |
| 1167 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1168 | /* 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] | 1169 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 1170 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1171 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1172 | { |
| 1173 | struct ebmb_node *node, *back; |
| 1174 | struct sni_ctx *sni; |
| 1175 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1176 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1177 | return; |
| 1178 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1179 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1180 | while (node) { |
| 1181 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1182 | back = ebmb_next(node); |
| 1183 | ebmb_delete(node); |
| 1184 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 1185 | SSL_CTX_free(sni->ctx); |
| 1186 | free(sni); |
| 1187 | node = back; |
| 1188 | } |
| 1189 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1190 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1191 | while (node) { |
| 1192 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1193 | back = ebmb_next(node); |
| 1194 | ebmb_delete(node); |
| 1195 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 1196 | SSL_CTX_free(sni->ctx); |
| 1197 | free(sni); |
| 1198 | node = back; |
| 1199 | } |
| 1200 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1201 | bind_conf->default_ctx = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1204 | /* |
| 1205 | * This function is called if SSL * context is not yet allocated. The function |
| 1206 | * is designed to be called before any other data-layer operation and sets the |
| 1207 | * handshake flag on the connection. It is safe to call it multiple times. |
| 1208 | * It returns 0 on success and -1 in error case. |
| 1209 | */ |
| 1210 | static int ssl_sock_init(struct connection *conn) |
| 1211 | { |
| 1212 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1213 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1214 | return 0; |
| 1215 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 1216 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1217 | return 0; |
| 1218 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1219 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 1220 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1221 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1222 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1223 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1224 | /* If it is in client mode initiate SSL session |
| 1225 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1226 | if (objt_server(conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1227 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1228 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1229 | if (!conn->xprt_ctx) { |
| 1230 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1231 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1232 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1233 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1234 | SSL_set_connect_state(conn->xprt_ctx); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1235 | if (objt_server(conn->target)->ssl_ctx.reused_sess) |
| 1236 | SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1237 | |
| 1238 | /* set fd on SSL session context */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1239 | SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1240 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1241 | /* set connection pointer */ |
| 1242 | SSL_set_app_data(conn->xprt_ctx, conn); |
| 1243 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1244 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 1245 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1246 | |
| 1247 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 1248 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1249 | return 0; |
| 1250 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1251 | else if (objt_listener(conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1252 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1253 | conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->default_ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1254 | if (!conn->xprt_ctx) { |
| 1255 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1256 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1257 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1258 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1259 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1260 | |
| 1261 | /* set fd on SSL session context */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1262 | SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1263 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1264 | /* set connection pointer */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1265 | SSL_set_app_data(conn->xprt_ctx, conn); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1266 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1267 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 1268 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1269 | |
| 1270 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 1271 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
| 1274 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1275 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1276 | return -1; |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | /* This is the callback which is used when an SSL handshake is pending. It |
| 1281 | * updates the FD status if it wants some polling before being called again. |
| 1282 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 1283 | * otherwise it returns non-zero and removes itself from the connection's |
| 1284 | * flags (the bit is provided in <flag> by the caller). |
| 1285 | */ |
| 1286 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 1287 | { |
| 1288 | int ret; |
| 1289 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 1290 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1291 | return 0; |
| 1292 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1293 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1294 | goto out_error; |
| 1295 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1296 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 1297 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 1298 | * Usually SSL_write and SSL_read are used and process implicitly |
| 1299 | * the reneg handshake. |
| 1300 | * Here we use SSL_peek as a workaround for reneg. |
| 1301 | */ |
| 1302 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1303 | char c; |
| 1304 | |
| 1305 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 1306 | if (ret <= 0) { |
| 1307 | /* handshake may have not been completed, let's find why */ |
| 1308 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 1309 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 1310 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 1311 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1312 | __conn_sock_want_send(conn); |
| 1313 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | else if (ret == SSL_ERROR_WANT_READ) { |
| 1317 | /* handshake may have been completed but we have |
| 1318 | * no more data to read. |
| 1319 | */ |
| 1320 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1321 | ret = 1; |
| 1322 | goto reneg_ok; |
| 1323 | } |
| 1324 | /* SSL handshake needs to read, L4 connection is ready */ |
| 1325 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 1326 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 1327 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1328 | __conn_sock_want_recv(conn); |
| 1329 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | else if (ret == SSL_ERROR_SYSCALL) { |
| 1333 | /* if errno is null, then connection was successfully established */ |
| 1334 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 1335 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1336 | if (!conn->err_code) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1337 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 1338 | if (!errno) { |
| 1339 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1340 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 1341 | else |
| 1342 | conn->err_code = CO_ER_SSL_EMPTY; |
| 1343 | } |
| 1344 | else { |
| 1345 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1346 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 1347 | else |
| 1348 | conn->err_code = CO_ER_SSL_ABORT; |
| 1349 | } |
| 1350 | } |
| 1351 | else { |
| 1352 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1353 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1354 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1355 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 1356 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1357 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1358 | goto out_error; |
| 1359 | } |
| 1360 | else { |
| 1361 | /* Fail on all other handshake errors */ |
| 1362 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 1363 | * buffer, causing an RST to be emitted upon close() on |
| 1364 | * TCP sockets. We first try to drain possibly pending |
| 1365 | * data to avoid this as much as possible. |
| 1366 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1367 | conn_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1368 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1369 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 1370 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1371 | goto out_error; |
| 1372 | } |
| 1373 | } |
| 1374 | /* read some data: consider handshake completed */ |
| 1375 | goto reneg_ok; |
| 1376 | } |
| 1377 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1378 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1379 | if (ret != 1) { |
| 1380 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1381 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1382 | |
| 1383 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 1384 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 1385 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1386 | __conn_sock_want_send(conn); |
| 1387 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1388 | return 0; |
| 1389 | } |
| 1390 | else if (ret == SSL_ERROR_WANT_READ) { |
| 1391 | /* SSL handshake needs to read, L4 connection is ready */ |
| 1392 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 1393 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 1394 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1395 | __conn_sock_want_recv(conn); |
| 1396 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 1399 | else if (ret == SSL_ERROR_SYSCALL) { |
| 1400 | /* if errno is null, then connection was successfully established */ |
| 1401 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 1402 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1403 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1404 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 1405 | if (!errno) { |
| 1406 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1407 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 1408 | else |
| 1409 | conn->err_code = CO_ER_SSL_EMPTY; |
| 1410 | } |
| 1411 | else { |
| 1412 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1413 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 1414 | else |
| 1415 | conn->err_code = CO_ER_SSL_ABORT; |
| 1416 | } |
| 1417 | } |
| 1418 | else { |
| 1419 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 1420 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1421 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1422 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 1423 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 1424 | goto out_error; |
| 1425 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1426 | else { |
| 1427 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 1428 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 1429 | * buffer, causing an RST to be emitted upon close() on |
| 1430 | * TCP sockets. We first try to drain possibly pending |
| 1431 | * data to avoid this as much as possible. |
| 1432 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1433 | conn_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1434 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1435 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 1436 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1437 | goto out_error; |
| 1438 | } |
| 1439 | } |
| 1440 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 1441 | reneg_ok: |
| 1442 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1443 | /* Handshake succeeded */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1444 | if (objt_server(conn->target)) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1445 | if (!SSL_session_reused(conn->xprt_ctx)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1446 | /* check if session was reused, if not store current session on server for reuse */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1447 | if (objt_server(conn->target)->ssl_ctx.reused_sess) |
| 1448 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1449 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1450 | objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | /* The connection is now established at both layers, it's time to leave */ |
| 1455 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 1456 | return 1; |
| 1457 | |
| 1458 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1459 | /* Clear openssl global errors stack */ |
| 1460 | ERR_clear_error(); |
| 1461 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 1462 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1463 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 1464 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 1465 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1468 | /* Fail on all other handshake errors */ |
| 1469 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1470 | if (!conn->err_code) |
| 1471 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | /* 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] | 1476 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1477 | * buffer wraps, in which case a second call may be performed. The connection's |
| 1478 | * flags are updated with whatever special event is detected (error, read0, |
| 1479 | * empty). The caller is responsible for taking care of those events and |
| 1480 | * avoiding the call if inappropriate. The function does not call the |
| 1481 | * connection's polling update function, so the caller is responsible for this. |
| 1482 | */ |
| 1483 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 1484 | { |
| 1485 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1486 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1487 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1488 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1489 | goto out_error; |
| 1490 | |
| 1491 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1492 | /* a handshake was requested */ |
| 1493 | return 0; |
| 1494 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1495 | /* let's realign the buffer to optimize I/O */ |
| 1496 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1497 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1498 | |
| 1499 | /* read the largest possible block. For this, we perform only one call |
| 1500 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 1501 | * in which case we accept to do it once again. A new attempt is made on |
| 1502 | * EINTR too. |
| 1503 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 1504 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1505 | /* first check if we have some room after p+i */ |
| 1506 | try = buf->data + buf->size - (buf->p + buf->i); |
| 1507 | /* otherwise continue between data and p-o */ |
| 1508 | if (try <= 0) { |
| 1509 | try = buf->p - (buf->data + buf->o); |
| 1510 | if (try <= 0) |
| 1511 | break; |
| 1512 | } |
| 1513 | if (try > count) |
| 1514 | try = count; |
| 1515 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1516 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1517 | if (conn->flags & CO_FL_ERROR) { |
| 1518 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1519 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1520 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1521 | if (ret > 0) { |
| 1522 | buf->i += ret; |
| 1523 | done += ret; |
| 1524 | if (ret < try) |
| 1525 | break; |
| 1526 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1527 | } |
| 1528 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1529 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 1530 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 1531 | /* error on protocol or underlying transport */ |
| 1532 | if ((ret != SSL_ERROR_SYSCALL) |
| 1533 | || (errno && (errno != EAGAIN))) |
| 1534 | conn->flags |= CO_FL_ERROR; |
| 1535 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1536 | /* Clear openssl global errors stack */ |
| 1537 | ERR_clear_error(); |
| 1538 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1539 | goto read0; |
| 1540 | } |
| 1541 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1542 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1543 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1544 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1545 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1546 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1547 | break; |
| 1548 | } |
| 1549 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 1550 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1551 | /* handshake is running, and it may need to re-enable read */ |
| 1552 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 1553 | __conn_sock_want_recv(conn); |
| 1554 | break; |
| 1555 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1556 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1557 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1558 | break; |
| 1559 | } |
| 1560 | /* otherwise it's a real error */ |
| 1561 | goto out_error; |
| 1562 | } |
| 1563 | } |
| 1564 | return done; |
| 1565 | |
| 1566 | read0: |
| 1567 | conn_sock_read0(conn); |
| 1568 | return done; |
| 1569 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1570 | /* Clear openssl global errors stack */ |
| 1571 | ERR_clear_error(); |
| 1572 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1573 | conn->flags |= CO_FL_ERROR; |
| 1574 | return done; |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 1579 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 1580 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1581 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 1582 | * a second call may be performed. The connection's flags are updated with |
| 1583 | * whatever special event is detected (error, empty). The caller is responsible |
| 1584 | * for taking care of those events and avoiding the call if inappropriate. The |
| 1585 | * function does not call the connection's polling update function, so the caller |
| 1586 | * is responsible for this. |
| 1587 | */ |
| 1588 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 1589 | { |
| 1590 | int ret, try, done; |
| 1591 | |
| 1592 | done = 0; |
| 1593 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1594 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1595 | goto out_error; |
| 1596 | |
| 1597 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1598 | /* a handshake was requested */ |
| 1599 | return 0; |
| 1600 | |
| 1601 | /* send the largest possible block. For this we perform only one call |
| 1602 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 1603 | * in which case we accept to do it once again. |
| 1604 | */ |
| 1605 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 1606 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 1608 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1609 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
| 1610 | global.tune.ssl_max_record && try > global.tune.ssl_max_record) { |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1611 | try = global.tune.ssl_max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1612 | } |
| 1613 | else { |
| 1614 | /* we need to keep the information about the fact that |
| 1615 | * we're not limiting the upcoming send(), because if it |
| 1616 | * fails, we'll have to retry with at least as many data. |
| 1617 | */ |
| 1618 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 1619 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1620 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1621 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1622 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1623 | if (conn->flags & CO_FL_ERROR) { |
| 1624 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1625 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1626 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1627 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1628 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 1629 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1630 | buf->o -= ret; |
| 1631 | done += ret; |
| 1632 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 1633 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1634 | /* optimize data alignment in the buffer */ |
| 1635 | buf->p = buf->data; |
| 1636 | |
| 1637 | /* if the system buffer is full, don't insist */ |
| 1638 | if (ret < try) |
| 1639 | break; |
| 1640 | } |
| 1641 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1642 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1643 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 1644 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1645 | /* handshake is running, and it may need to re-enable write */ |
| 1646 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 1647 | __conn_sock_want_send(conn); |
| 1648 | break; |
| 1649 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1650 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1651 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1652 | break; |
| 1653 | } |
| 1654 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1655 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1656 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1657 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1658 | break; |
| 1659 | } |
| 1660 | goto out_error; |
| 1661 | } |
| 1662 | } |
| 1663 | return done; |
| 1664 | |
| 1665 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1666 | /* Clear openssl global errors stack */ |
| 1667 | ERR_clear_error(); |
| 1668 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1669 | conn->flags |= CO_FL_ERROR; |
| 1670 | return done; |
| 1671 | } |
| 1672 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1673 | static void ssl_sock_close(struct connection *conn) { |
| 1674 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1675 | if (conn->xprt_ctx) { |
| 1676 | SSL_free(conn->xprt_ctx); |
| 1677 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1678 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1679 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 1683 | * any case, flags the connection as reusable if no handshake was in progress. |
| 1684 | */ |
| 1685 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 1686 | { |
| 1687 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1688 | return; |
| 1689 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1690 | if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) { |
| 1691 | /* Clear openssl global errors stack */ |
| 1692 | ERR_clear_error(); |
| 1693 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1694 | |
| 1695 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1696 | SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1699 | /* used for logging, may be changed for a sample fetch later */ |
| 1700 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 1701 | { |
| 1702 | if (!conn->xprt && !conn->xprt_ctx) |
| 1703 | return NULL; |
| 1704 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 1705 | } |
| 1706 | |
| 1707 | /* used for logging, may be changed for a sample fetch later */ |
| 1708 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 1709 | { |
| 1710 | if (!conn->xprt && !conn->xprt_ctx) |
| 1711 | return NULL; |
| 1712 | return SSL_get_version(conn->xprt_ctx); |
| 1713 | } |
| 1714 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1715 | /* Extract a serial from a cert, and copy it to a chunk. |
| 1716 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 1717 | * -1 if output is not large enough. |
| 1718 | */ |
| 1719 | static int |
| 1720 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 1721 | { |
| 1722 | ASN1_INTEGER *serial; |
| 1723 | |
| 1724 | serial = X509_get_serialNumber(crt); |
| 1725 | if (!serial) |
| 1726 | return 0; |
| 1727 | |
| 1728 | if (out->size < serial->length) |
| 1729 | return -1; |
| 1730 | |
| 1731 | memcpy(out->str, serial->data, serial->length); |
| 1732 | out->len = serial->length; |
| 1733 | return 1; |
| 1734 | } |
| 1735 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1736 | |
| 1737 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 1738 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 1739 | * and -1 if output is not large enough. |
| 1740 | */ |
| 1741 | static int |
| 1742 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 1743 | { |
| 1744 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 1745 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 1746 | |
| 1747 | if (gentm->length < 12) |
| 1748 | return 0; |
| 1749 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 1750 | return 0; |
| 1751 | if (out->size < gentm->length-2) |
| 1752 | return -1; |
| 1753 | |
| 1754 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 1755 | out->len = gentm->length-2; |
| 1756 | return 1; |
| 1757 | } |
| 1758 | else if (tm->type == V_ASN1_UTCTIME) { |
| 1759 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 1760 | |
| 1761 | if (utctm->length < 10) |
| 1762 | return 0; |
| 1763 | if (utctm->data[0] >= 0x35) |
| 1764 | return 0; |
| 1765 | if (out->size < utctm->length) |
| 1766 | return -1; |
| 1767 | |
| 1768 | memcpy(out->str, utctm->data, utctm->length); |
| 1769 | out->len = utctm->length; |
| 1770 | return 1; |
| 1771 | } |
| 1772 | |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 1776 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 1777 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 1778 | */ |
| 1779 | static int |
| 1780 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 1781 | { |
| 1782 | X509_NAME_ENTRY *ne; |
| 1783 | int i, j, n; |
| 1784 | int cur = 0; |
| 1785 | const char *s; |
| 1786 | char tmp[128]; |
| 1787 | |
| 1788 | out->len = 0; |
| 1789 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 1790 | if (pos < 0) |
| 1791 | j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i; |
| 1792 | else |
| 1793 | j = i; |
| 1794 | |
| 1795 | ne = sk_X509_NAME_ENTRY_value(a->entries, j); |
| 1796 | n = OBJ_obj2nid(ne->object); |
| 1797 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 1798 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 1799 | s = tmp; |
| 1800 | } |
| 1801 | |
| 1802 | if (chunk_strcasecmp(entry, s) != 0) |
| 1803 | continue; |
| 1804 | |
| 1805 | if (pos < 0) |
| 1806 | cur--; |
| 1807 | else |
| 1808 | cur++; |
| 1809 | |
| 1810 | if (cur != pos) |
| 1811 | continue; |
| 1812 | |
| 1813 | if (ne->value->length > out->size) |
| 1814 | return -1; |
| 1815 | |
| 1816 | memcpy(out->str, ne->value->data, ne->value->length); |
| 1817 | out->len = ne->value->length; |
| 1818 | return 1; |
| 1819 | } |
| 1820 | |
| 1821 | return 0; |
| 1822 | |
| 1823 | } |
| 1824 | |
| 1825 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 1826 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 1827 | */ |
| 1828 | static int |
| 1829 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 1830 | { |
| 1831 | X509_NAME_ENTRY *ne; |
| 1832 | int i, n, ln; |
| 1833 | int l = 0; |
| 1834 | const char *s; |
| 1835 | char *p; |
| 1836 | char tmp[128]; |
| 1837 | |
| 1838 | out->len = 0; |
| 1839 | p = out->str; |
| 1840 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 1841 | ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 1842 | n = OBJ_obj2nid(ne->object); |
| 1843 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 1844 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 1845 | s = tmp; |
| 1846 | } |
| 1847 | ln = strlen(s); |
| 1848 | |
| 1849 | l += 1 + ln + 1 + ne->value->length; |
| 1850 | if (l > out->size) |
| 1851 | return -1; |
| 1852 | out->len = l; |
| 1853 | |
| 1854 | *(p++)='/'; |
| 1855 | memcpy(p, s, ln); |
| 1856 | p += ln; |
| 1857 | *(p++)='='; |
| 1858 | memcpy(p, ne->value->data, ne->value->length); |
| 1859 | p += ne->value->length; |
| 1860 | } |
| 1861 | |
| 1862 | if (!out->len) |
| 1863 | return 0; |
| 1864 | |
| 1865 | return 1; |
| 1866 | } |
| 1867 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1868 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 1869 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1870 | /* boolean, returns true if client cert was present */ |
| 1871 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 1872 | smp_fetch_ssl_fc_has_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1873 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1874 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1875 | struct connection *conn; |
| 1876 | |
| 1877 | if (!l4) |
| 1878 | return 0; |
| 1879 | |
| 1880 | conn = objt_conn(l4->si[0].end); |
| 1881 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1882 | return 0; |
| 1883 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1884 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1885 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
| 1889 | smp->flags = 0; |
| 1890 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1891 | smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1892 | |
| 1893 | return 1; |
| 1894 | } |
| 1895 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1896 | /* bin, returns serial in a binary chunk */ |
| 1897 | static int |
| 1898 | smp_fetch_ssl_c_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1899 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1900 | { |
| 1901 | X509 *crt = NULL; |
| 1902 | int ret = 0; |
| 1903 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1904 | struct connection *conn; |
| 1905 | |
| 1906 | if (!l4) |
| 1907 | return 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1908 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1909 | conn = objt_conn(l4->si[0].end); |
| 1910 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1911 | return 0; |
| 1912 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1913 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1914 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
| 1918 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1919 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1920 | if (!crt) |
| 1921 | goto out; |
| 1922 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 1923 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1924 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 1925 | goto out; |
| 1926 | |
| 1927 | smp->data.str = *smp_trash; |
| 1928 | smp->type = SMP_T_BIN; |
| 1929 | ret = 1; |
| 1930 | out: |
| 1931 | if (crt) |
| 1932 | X509_free(crt); |
| 1933 | return ret; |
| 1934 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1935 | |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1936 | /* bin, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk */ |
| 1937 | static int |
| 1938 | smp_fetch_ssl_c_sha1(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1939 | const struct arg *args, struct sample *smp, const char *kw) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1940 | { |
| 1941 | X509 *crt = NULL; |
| 1942 | const EVP_MD *digest; |
| 1943 | int ret = 0; |
| 1944 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1945 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1946 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1947 | if (!l4) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1948 | return 0; |
| 1949 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1950 | conn = objt_conn(l4->si[0].end); |
| 1951 | if (!conn || conn->xprt != &ssl_sock) |
| 1952 | return 0; |
| 1953 | |
| 1954 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1955 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1960 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 1961 | if (!crt) |
| 1962 | goto out; |
| 1963 | |
| 1964 | smp_trash = get_trash_chunk(); |
| 1965 | digest = EVP_sha1(); |
| 1966 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 1967 | |
| 1968 | smp->data.str = *smp_trash; |
| 1969 | smp->type = SMP_T_BIN; |
| 1970 | ret = 1; |
| 1971 | out: |
| 1972 | if (crt) |
| 1973 | X509_free(crt); |
| 1974 | return ret; |
| 1975 | } |
| 1976 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1977 | /*str, returns notafter date in ASN1_UTCTIME format */ |
| 1978 | static int |
| 1979 | smp_fetch_ssl_c_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1980 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1981 | { |
| 1982 | X509 *crt = NULL; |
| 1983 | int ret = 0; |
| 1984 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1985 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1986 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1987 | if (!l4) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1988 | return 0; |
| 1989 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1990 | conn = objt_conn(l4->si[0].end); |
| 1991 | if (!conn || conn->xprt != &ssl_sock) |
| 1992 | return 0; |
| 1993 | |
| 1994 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1995 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2000 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2001 | if (!crt) |
| 2002 | goto out; |
| 2003 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2004 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2005 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 2006 | goto out; |
| 2007 | |
| 2008 | smp->data.str = *smp_trash; |
| 2009 | smp->type = SMP_T_STR; |
| 2010 | ret = 1; |
| 2011 | out: |
| 2012 | if (crt) |
| 2013 | X509_free(crt); |
| 2014 | return ret; |
| 2015 | } |
| 2016 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2017 | /* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */ |
| 2018 | static int |
| 2019 | smp_fetch_ssl_c_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2020 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2021 | { |
| 2022 | X509 *crt = NULL; |
| 2023 | X509_NAME *name; |
| 2024 | int ret = 0; |
| 2025 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2026 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2027 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2028 | if (!l4) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2029 | return 0; |
| 2030 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2031 | conn = objt_conn(l4->si[0].end); |
| 2032 | if (!conn || conn->xprt != &ssl_sock) |
| 2033 | return 0; |
| 2034 | |
| 2035 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2036 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2037 | return 0; |
| 2038 | } |
| 2039 | |
| 2040 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2041 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2042 | if (!crt) |
| 2043 | goto out; |
| 2044 | |
| 2045 | name = X509_get_issuer_name(crt); |
| 2046 | if (!name) |
| 2047 | goto out; |
| 2048 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2049 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2050 | if (args && args[0].type == ARGT_STR) { |
| 2051 | int pos = 1; |
| 2052 | |
| 2053 | if (args[1].type == ARGT_SINT) |
| 2054 | pos = args[1].data.sint; |
| 2055 | else if (args[1].type == ARGT_UINT) |
| 2056 | pos =(int)args[1].data.uint; |
| 2057 | |
| 2058 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2059 | goto out; |
| 2060 | } |
| 2061 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2062 | goto out; |
| 2063 | |
| 2064 | smp->type = SMP_T_STR; |
| 2065 | smp->data.str = *smp_trash; |
| 2066 | ret = 1; |
| 2067 | out: |
| 2068 | if (crt) |
| 2069 | X509_free(crt); |
| 2070 | return ret; |
| 2071 | } |
| 2072 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2073 | /*str, returns notbefore date in ASN1_UTCTIME format */ |
| 2074 | static int |
| 2075 | smp_fetch_ssl_c_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2076 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2077 | { |
| 2078 | X509 *crt = NULL; |
| 2079 | int ret = 0; |
| 2080 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2081 | struct connection *conn; |
| 2082 | |
| 2083 | if (!l4) |
| 2084 | return 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2085 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2086 | conn = objt_conn(l4->si[0].end); |
| 2087 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2088 | return 0; |
| 2089 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2090 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2091 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2096 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2097 | if (!crt) |
| 2098 | goto out; |
| 2099 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2100 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2101 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 2102 | goto out; |
| 2103 | |
| 2104 | smp->data.str = *smp_trash; |
| 2105 | smp->type = SMP_T_STR; |
| 2106 | ret = 1; |
| 2107 | out: |
| 2108 | if (crt) |
| 2109 | X509_free(crt); |
| 2110 | return ret; |
| 2111 | } |
| 2112 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2113 | /* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */ |
| 2114 | static int |
| 2115 | smp_fetch_ssl_c_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2116 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2117 | { |
| 2118 | X509 *crt = NULL; |
| 2119 | X509_NAME *name; |
| 2120 | int ret = 0; |
| 2121 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2122 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2123 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2124 | if (!l4) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2125 | return 0; |
| 2126 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2127 | conn = objt_conn(l4->si[0].end); |
| 2128 | if (!conn || conn->xprt != &ssl_sock) |
| 2129 | return 0; |
| 2130 | |
| 2131 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2132 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
| 2136 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2137 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2138 | if (!crt) |
| 2139 | goto out; |
| 2140 | |
| 2141 | name = X509_get_subject_name(crt); |
| 2142 | if (!name) |
| 2143 | goto out; |
| 2144 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2145 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2146 | if (args && args[0].type == ARGT_STR) { |
| 2147 | int pos = 1; |
| 2148 | |
| 2149 | if (args[1].type == ARGT_SINT) |
| 2150 | pos = args[1].data.sint; |
| 2151 | else if (args[1].type == ARGT_UINT) |
| 2152 | pos =(int)args[1].data.uint; |
| 2153 | |
| 2154 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2155 | goto out; |
| 2156 | } |
| 2157 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2158 | goto out; |
| 2159 | |
| 2160 | smp->type = SMP_T_STR; |
| 2161 | smp->data.str = *smp_trash; |
| 2162 | ret = 1; |
| 2163 | out: |
| 2164 | if (crt) |
| 2165 | X509_free(crt); |
| 2166 | return ret; |
| 2167 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2168 | |
| 2169 | /* integer, returns true if current session use a client certificate */ |
| 2170 | static int |
| 2171 | smp_fetch_ssl_c_used(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2172 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2173 | { |
| 2174 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2175 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2176 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2177 | if (!l4) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2178 | return 0; |
| 2179 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2180 | conn = objt_conn(l4->si[0].end); |
| 2181 | if (!conn || conn->xprt != &ssl_sock) |
| 2182 | return 0; |
| 2183 | |
| 2184 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2185 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2190 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2191 | if (crt) { |
| 2192 | X509_free(crt); |
| 2193 | } |
| 2194 | |
| 2195 | smp->type = SMP_T_BOOL; |
| 2196 | smp->data.uint = (crt != NULL); |
| 2197 | return 1; |
| 2198 | } |
| 2199 | |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2200 | /* integer, returns the client certificate version */ |
| 2201 | static int |
| 2202 | smp_fetch_ssl_c_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2203 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2204 | { |
| 2205 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2206 | struct connection *conn; |
| 2207 | |
| 2208 | if (!l4) |
| 2209 | return 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2210 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2211 | conn = objt_conn(l4->si[0].end); |
| 2212 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2213 | return 0; |
| 2214 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2215 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2216 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2221 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2222 | if (!crt) |
| 2223 | return 0; |
| 2224 | |
| 2225 | smp->data.uint = (unsigned int)(1 + X509_get_version(crt)); |
| 2226 | X509_free(crt); |
| 2227 | smp->type = SMP_T_UINT; |
| 2228 | |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2232 | /* str, returns the client certificate sig alg */ |
| 2233 | static int |
| 2234 | smp_fetch_ssl_c_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2235 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2236 | { |
| 2237 | X509 *crt; |
| 2238 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2239 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2240 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2241 | if (!l4) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2242 | return 0; |
| 2243 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2244 | conn = objt_conn(l4->si[0].end); |
| 2245 | if (!conn || conn->xprt != &ssl_sock) |
| 2246 | return 0; |
| 2247 | |
| 2248 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2249 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | /* SSL_get_peer_certificate increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2254 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2255 | if (!crt) |
| 2256 | return 0; |
| 2257 | |
| 2258 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm)); |
| 2259 | |
| 2260 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2261 | if (!smp->data.str.str) { |
| 2262 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2263 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2264 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2265 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2266 | smp->type = SMP_T_STR; |
| 2267 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2268 | smp->data.str.len = strlen(smp->data.str.str); |
| 2269 | X509_free(crt); |
| 2270 | |
| 2271 | return 1; |
| 2272 | } |
| 2273 | |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2274 | /* str, returns the client certificate key alg */ |
| 2275 | static int |
| 2276 | smp_fetch_ssl_c_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2277 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2278 | { |
| 2279 | X509 *crt; |
| 2280 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2281 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2282 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2283 | if (!l4) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2284 | return 0; |
| 2285 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2286 | conn = objt_conn(l4->si[0].end); |
| 2287 | if (!conn || conn->xprt != &ssl_sock) |
| 2288 | return 0; |
| 2289 | |
| 2290 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2291 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | /* SSL_get_peer_certificate increase X509 * ref count */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2296 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2297 | if (!crt) |
| 2298 | return 0; |
| 2299 | |
| 2300 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm)); |
| 2301 | |
| 2302 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2303 | if (!smp->data.str.str) { |
| 2304 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2305 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2306 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2307 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2308 | smp->type = SMP_T_STR; |
| 2309 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2310 | smp->data.str.len = strlen(smp->data.str.str); |
| 2311 | X509_free(crt); |
| 2312 | |
| 2313 | return 1; |
| 2314 | } |
| 2315 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2316 | /* boolean, returns true if front conn. transport layer is SSL */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2317 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2318 | smp_fetch_ssl_fc(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2319 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2320 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2321 | struct connection *conn = objt_conn(l4->si[0].end); |
| 2322 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2323 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2324 | smp->data.uint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2325 | return 1; |
| 2326 | } |
| 2327 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2328 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2329 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2330 | smp_fetch_ssl_fc_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2331 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2332 | { |
| 2333 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2334 | struct connection *conn = objt_conn(l4->si[0].end); |
| 2335 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2336 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2337 | smp->data.uint = (conn && conn->xprt == &ssl_sock) && |
| 2338 | conn->xprt_ctx && |
| 2339 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2340 | return 1; |
| 2341 | #else |
| 2342 | return 0; |
| 2343 | #endif |
| 2344 | } |
| 2345 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2346 | /* bin, returns serial in a binary chunk */ |
| 2347 | static int |
| 2348 | smp_fetch_ssl_f_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2349 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2350 | { |
| 2351 | X509 *crt = NULL; |
| 2352 | int ret = 0; |
| 2353 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2354 | struct connection *conn; |
| 2355 | |
| 2356 | if (!l4) |
| 2357 | return 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2358 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2359 | conn = objt_conn(l4->si[0].end); |
| 2360 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2361 | return 0; |
| 2362 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2363 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2364 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2368 | crt = SSL_get_certificate(conn->xprt_ctx); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2369 | if (!crt) |
| 2370 | goto out; |
| 2371 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2372 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2373 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 2374 | goto out; |
| 2375 | |
| 2376 | smp->data.str = *smp_trash; |
| 2377 | smp->type = SMP_T_BIN; |
| 2378 | ret = 1; |
| 2379 | out: |
| 2380 | return ret; |
| 2381 | } |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2382 | /*str, returns notafter date in ASN1_UTCTIME format */ |
| 2383 | static int |
| 2384 | smp_fetch_ssl_f_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2385 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2386 | { |
| 2387 | X509 *crt = NULL; |
| 2388 | int ret = 0; |
| 2389 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2390 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2391 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2392 | if (!l4) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2393 | return 0; |
| 2394 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2395 | conn = objt_conn(l4->si[0].end); |
| 2396 | if (!conn || conn->xprt != &ssl_sock) |
| 2397 | return 0; |
| 2398 | |
| 2399 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2400 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2404 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2405 | if (!crt) |
| 2406 | goto out; |
| 2407 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2408 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2409 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 2410 | goto out; |
| 2411 | |
| 2412 | smp->data.str = *smp_trash; |
| 2413 | smp->type = SMP_T_STR; |
| 2414 | ret = 1; |
| 2415 | out: |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
| 2419 | /*str, returns notbefore date in ASN1_UTCTIME format */ |
| 2420 | static int |
| 2421 | smp_fetch_ssl_f_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2422 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2423 | { |
| 2424 | X509 *crt = NULL; |
| 2425 | int ret = 0; |
| 2426 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2427 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2428 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2429 | if (!l4) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2430 | return 0; |
| 2431 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2432 | conn = objt_conn(l4->si[0].end); |
| 2433 | if (!conn || conn->xprt != &ssl_sock) |
| 2434 | return 0; |
| 2435 | |
| 2436 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2437 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2441 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2442 | if (!crt) |
| 2443 | goto out; |
| 2444 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2445 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2446 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 2447 | goto out; |
| 2448 | |
| 2449 | smp->data.str = *smp_trash; |
| 2450 | smp->type = SMP_T_STR; |
| 2451 | ret = 1; |
| 2452 | out: |
| 2453 | return ret; |
| 2454 | } |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2455 | |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2456 | /* integer, returns the frontend certificate version */ |
| 2457 | static int |
| 2458 | smp_fetch_ssl_f_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2459 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2460 | { |
| 2461 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2462 | struct connection *conn; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2463 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2464 | if (!l4) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2465 | return 0; |
| 2466 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2467 | conn = objt_conn(l4->si[0].end); |
| 2468 | if (!conn || conn->xprt != &ssl_sock) |
| 2469 | return 0; |
| 2470 | |
| 2471 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2472 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2473 | return 0; |
| 2474 | } |
| 2475 | |
| 2476 | /* SSL_get_certificate returns a ptr on an SSL * internal sub struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2477 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2478 | if (!crt) |
| 2479 | return 0; |
| 2480 | |
| 2481 | smp->data.uint = (unsigned int)(1 + X509_get_version(crt)); |
| 2482 | smp->type = SMP_T_UINT; |
| 2483 | |
| 2484 | return 1; |
| 2485 | } |
| 2486 | |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2487 | /* str, returns the client certificate sig alg */ |
| 2488 | static int |
| 2489 | smp_fetch_ssl_f_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2490 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2491 | { |
| 2492 | X509 *crt; |
| 2493 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2494 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2495 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2496 | if (!l4) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2497 | return 0; |
| 2498 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2499 | conn = objt_conn(l4->si[0].end); |
| 2500 | if (!conn || conn->xprt != &ssl_sock) |
| 2501 | return 0; |
| 2502 | |
| 2503 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2504 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2508 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2509 | if (!crt) |
| 2510 | return 0; |
| 2511 | |
| 2512 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm)); |
| 2513 | |
| 2514 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
| 2515 | if (!smp->data.str.str) |
| 2516 | return 0; |
| 2517 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2518 | smp->type = SMP_T_STR; |
| 2519 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2520 | smp->data.str.len = strlen(smp->data.str.str); |
| 2521 | |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2525 | /* str, returns the client certificate key alg */ |
| 2526 | static int |
| 2527 | smp_fetch_ssl_f_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2528 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2529 | { |
| 2530 | X509 *crt; |
| 2531 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2532 | struct connection *conn; |
| 2533 | |
| 2534 | if (!l4) |
| 2535 | return 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2536 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2537 | conn = objt_conn(l4->si[0].end); |
| 2538 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2539 | return 0; |
| 2540 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2541 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2542 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2546 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2547 | if (!crt) |
| 2548 | return 0; |
| 2549 | |
| 2550 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm)); |
| 2551 | |
| 2552 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
| 2553 | if (!smp->data.str.str) |
| 2554 | return 0; |
| 2555 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2556 | smp->type = SMP_T_STR; |
| 2557 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2558 | smp->data.str.len = strlen(smp->data.str.str); |
| 2559 | |
| 2560 | return 1; |
| 2561 | } |
| 2562 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2563 | /* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */ |
| 2564 | static int |
| 2565 | smp_fetch_ssl_f_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2566 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2567 | { |
| 2568 | X509 *crt = NULL; |
| 2569 | X509_NAME *name; |
| 2570 | int ret = 0; |
| 2571 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2572 | struct connection *conn; |
| 2573 | |
| 2574 | if (!l4) |
| 2575 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2576 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2577 | conn = objt_conn(l4->si[0].end); |
| 2578 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2579 | return 0; |
| 2580 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2581 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2582 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2586 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2587 | if (!crt) |
| 2588 | goto out; |
| 2589 | |
| 2590 | name = X509_get_issuer_name(crt); |
| 2591 | if (!name) |
| 2592 | goto out; |
| 2593 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2594 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2595 | if (args && args[0].type == ARGT_STR) { |
| 2596 | int pos = 1; |
| 2597 | |
| 2598 | if (args[1].type == ARGT_SINT) |
| 2599 | pos = args[1].data.sint; |
| 2600 | else if (args[1].type == ARGT_UINT) |
| 2601 | pos =(int)args[1].data.uint; |
| 2602 | |
| 2603 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2604 | goto out; |
| 2605 | } |
| 2606 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2607 | goto out; |
| 2608 | |
| 2609 | smp->type = SMP_T_STR; |
| 2610 | smp->data.str = *smp_trash; |
| 2611 | ret = 1; |
| 2612 | out: |
| 2613 | return ret; |
| 2614 | } |
| 2615 | |
| 2616 | /* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */ |
| 2617 | static int |
| 2618 | smp_fetch_ssl_f_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2619 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2620 | { |
| 2621 | X509 *crt = NULL; |
| 2622 | X509_NAME *name; |
| 2623 | int ret = 0; |
| 2624 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2625 | struct connection *conn; |
| 2626 | |
| 2627 | if (!l4) |
| 2628 | return 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2630 | conn = objt_conn(l4->si[0].end); |
| 2631 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2632 | return 0; |
| 2633 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2634 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2635 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2639 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2640 | if (!crt) |
| 2641 | goto out; |
| 2642 | |
| 2643 | name = X509_get_subject_name(crt); |
| 2644 | if (!name) |
| 2645 | goto out; |
| 2646 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2647 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2648 | if (args && args[0].type == ARGT_STR) { |
| 2649 | int pos = 1; |
| 2650 | |
| 2651 | if (args[1].type == ARGT_SINT) |
| 2652 | pos = args[1].data.sint; |
| 2653 | else if (args[1].type == ARGT_UINT) |
| 2654 | pos =(int)args[1].data.uint; |
| 2655 | |
| 2656 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2657 | goto out; |
| 2658 | } |
| 2659 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2660 | goto out; |
| 2661 | |
| 2662 | smp->type = SMP_T_STR; |
| 2663 | smp->data.str = *smp_trash; |
| 2664 | ret = 1; |
| 2665 | out: |
| 2666 | return ret; |
| 2667 | } |
| 2668 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2669 | static int |
| 2670 | smp_fetch_ssl_fc_cipher(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2671 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2672 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2673 | struct connection *conn; |
| 2674 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2675 | smp->flags = 0; |
| 2676 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2677 | if (!l4) |
| 2678 | return 0; |
| 2679 | |
| 2680 | conn = objt_conn(l4->si[0].end); |
| 2681 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2682 | return 0; |
| 2683 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2684 | smp->data.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2685 | if (!smp->data.str.str) |
| 2686 | return 0; |
| 2687 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2688 | smp->type = SMP_T_STR; |
| 2689 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2690 | smp->data.str.len = strlen(smp->data.str.str); |
| 2691 | |
| 2692 | return 1; |
| 2693 | } |
| 2694 | |
| 2695 | static int |
| 2696 | smp_fetch_ssl_fc_alg_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2697 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2698 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2699 | struct connection *conn; |
| 2700 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2701 | smp->flags = 0; |
| 2702 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2703 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2704 | return 0; |
| 2705 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2706 | conn = objt_conn(l4->si[0].end); |
| 2707 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2708 | return 0; |
| 2709 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2710 | if (!SSL_get_cipher_bits(conn->xprt_ctx, (int *)&smp->data.uint)) |
| 2711 | return 0; |
| 2712 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2713 | smp->type = SMP_T_UINT; |
| 2714 | |
| 2715 | return 1; |
| 2716 | } |
| 2717 | |
| 2718 | static int |
| 2719 | smp_fetch_ssl_fc_use_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2720 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2721 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2722 | struct connection *conn; |
| 2723 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2724 | smp->flags = 0; |
| 2725 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2726 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2727 | return 0; |
| 2728 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2729 | conn = objt_conn(l4->si[0].end); |
| 2730 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2731 | return 0; |
| 2732 | |
| 2733 | smp->data.uint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2734 | if (!smp->data.uint) |
| 2735 | return 0; |
| 2736 | |
| 2737 | smp->type = SMP_T_UINT; |
| 2738 | |
| 2739 | return 1; |
| 2740 | } |
| 2741 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2742 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2743 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2744 | smp_fetch_ssl_fc_npn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2745 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2746 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2747 | struct connection *conn; |
| 2748 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2749 | smp->flags = SMP_F_CONST; |
| 2750 | smp->type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2751 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2752 | if (!l4) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2753 | return 0; |
| 2754 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2755 | conn = objt_conn(l4->si[0].end); |
| 2756 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2757 | return 0; |
| 2758 | |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2759 | smp->data.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2760 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2761 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 2762 | |
| 2763 | if (!smp->data.str.str) |
| 2764 | return 0; |
| 2765 | |
| 2766 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2767 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2768 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2769 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2770 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2771 | static int |
| 2772 | smp_fetch_ssl_fc_alpn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2773 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2774 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2775 | struct connection *conn; |
| 2776 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2777 | smp->flags = SMP_F_CONST; |
| 2778 | smp->type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2780 | if (!l4) |
| 2781 | return 0; |
| 2782 | |
| 2783 | conn = objt_conn(l4->si[0].end); |
| 2784 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2785 | return 0; |
| 2786 | |
| 2787 | smp->data.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2788 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2789 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 2790 | |
| 2791 | if (!smp->data.str.str) |
| 2792 | return 0; |
| 2793 | |
| 2794 | return 1; |
| 2795 | } |
| 2796 | #endif |
| 2797 | |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2798 | static int |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2799 | smp_fetch_ssl_fc_protocol(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2800 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2801 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2802 | struct connection *conn; |
| 2803 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2804 | smp->flags = 0; |
| 2805 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2806 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2807 | return 0; |
| 2808 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2809 | conn = objt_conn(l4->si[0].end); |
| 2810 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2811 | return 0; |
| 2812 | |
| 2813 | smp->data.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2814 | if (!smp->data.str.str) |
| 2815 | return 0; |
| 2816 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2817 | smp->type = SMP_T_STR; |
| 2818 | smp->flags = SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2819 | smp->data.str.len = strlen(smp->data.str.str); |
| 2820 | |
| 2821 | return 1; |
| 2822 | } |
| 2823 | |
| 2824 | static int |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2825 | smp_fetch_ssl_fc_session_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2826 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2827 | { |
| 2828 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
| 2829 | SSL_SESSION *sess; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2830 | struct connection *conn; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2831 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2832 | smp->flags = SMP_F_CONST; |
| 2833 | smp->type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2834 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2835 | if (!l4) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2836 | return 0; |
| 2837 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2838 | conn = objt_conn(l4->si[0].end); |
| 2839 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2840 | return 0; |
| 2841 | |
| 2842 | sess = SSL_get_session(conn->xprt_ctx); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2843 | if (!sess) |
| 2844 | return 0; |
| 2845 | |
| 2846 | smp->data.str.str = (char *)SSL_SESSION_get_id(sess, (unsigned int *)&smp->data.str.len); |
| 2847 | if (!smp->data.str.str || !&smp->data.str.len) |
| 2848 | return 0; |
| 2849 | |
| 2850 | return 1; |
| 2851 | #else |
| 2852 | return 0; |
| 2853 | #endif |
| 2854 | } |
| 2855 | |
| 2856 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2857 | smp_fetch_ssl_fc_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2858 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2859 | { |
| 2860 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2861 | struct connection *conn; |
| 2862 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2863 | smp->flags = SMP_F_CONST; |
| 2864 | smp->type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2865 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2866 | if (!l4) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2867 | return 0; |
| 2868 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2869 | conn = objt_conn(l4->si[0].end); |
| 2870 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2871 | return 0; |
| 2872 | |
| 2873 | smp->data.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 2874 | if (!smp->data.str.str) |
| 2875 | return 0; |
| 2876 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2877 | smp->data.str.len = strlen(smp->data.str.str); |
| 2878 | return 1; |
| 2879 | #else |
| 2880 | return 0; |
| 2881 | #endif |
| 2882 | } |
| 2883 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2884 | static int |
| 2885 | smp_fetch_ssl_fc_unique_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 2886 | const struct arg *args, struct sample *smp, const char *kw) |
| 2887 | { |
| 2888 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
| 2889 | struct connection *conn; |
| 2890 | int finished_len; |
| 2891 | int b64_len; |
| 2892 | struct chunk *finished_trash; |
| 2893 | struct chunk *smp_trash; |
| 2894 | |
| 2895 | smp->flags = 0; |
| 2896 | |
| 2897 | if (!l4) |
| 2898 | return 0; |
| 2899 | |
| 2900 | conn = objt_conn(l4->si[0].end); |
| 2901 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2902 | return 0; |
| 2903 | |
| 2904 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2905 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2906 | return 0; |
| 2907 | } |
| 2908 | |
| 2909 | finished_trash = get_trash_chunk(); |
| 2910 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 2911 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 2912 | else |
| 2913 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 2914 | |
| 2915 | if (!finished_len) |
| 2916 | return 0; |
| 2917 | |
| 2918 | smp_trash = get_trash_chunk(); |
| 2919 | b64_len = a2base64(finished_trash->str, finished_len, smp_trash->str, smp_trash->size); |
| 2920 | if (b64_len < 0) |
| 2921 | return 0; |
| 2922 | |
| 2923 | smp->data.str.str = smp_trash->str; |
Willy Tarreau | 073edf3 | 2014-04-09 15:40:42 +0200 | [diff] [blame] | 2924 | smp->type = SMP_T_STR; |
| 2925 | smp->flags |= SMP_F_CONST; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2926 | smp->data.str.len = b64_len; |
| 2927 | |
| 2928 | return 1; |
| 2929 | #else |
| 2930 | return 0; |
| 2931 | #endif |
| 2932 | } |
| 2933 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2934 | /* 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] | 2935 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2936 | smp_fetch_ssl_c_ca_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2937 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2938 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2939 | struct connection *conn; |
| 2940 | |
| 2941 | if (!l4) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2942 | return 0; |
| 2943 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2944 | conn = objt_conn(l4->si[0].end); |
| 2945 | if (!conn || conn->xprt != &ssl_sock) |
| 2946 | return 0; |
| 2947 | |
| 2948 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2949 | smp->flags = SMP_F_MAY_CHANGE; |
| 2950 | return 0; |
| 2951 | } |
| 2952 | |
| 2953 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2954 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2955 | smp->flags = 0; |
| 2956 | |
| 2957 | return 1; |
| 2958 | } |
| 2959 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2960 | /* 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] | 2961 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2962 | smp_fetch_ssl_c_ca_err_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2963 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2964 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2965 | struct connection *conn; |
| 2966 | |
| 2967 | if (!l4) |
| 2968 | return 0; |
| 2969 | |
| 2970 | conn = objt_conn(l4->si[0].end); |
| 2971 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2972 | return 0; |
| 2973 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2974 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2975 | smp->flags = SMP_F_MAY_CHANGE; |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2980 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2981 | smp->flags = 0; |
| 2982 | |
| 2983 | return 1; |
| 2984 | } |
| 2985 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2986 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2987 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2988 | smp_fetch_ssl_c_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 2989 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2990 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2991 | struct connection *conn; |
| 2992 | |
| 2993 | if (!l4) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2994 | return 0; |
| 2995 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2996 | conn = objt_conn(l4->si[0].end); |
| 2997 | if (!conn || conn->xprt != &ssl_sock) |
| 2998 | return 0; |
| 2999 | |
| 3000 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 3001 | smp->flags = SMP_F_MAY_CHANGE; |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3006 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 3007 | smp->flags = 0; |
| 3008 | |
| 3009 | return 1; |
| 3010 | } |
| 3011 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 3012 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3013 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 3014 | smp_fetch_ssl_c_verify(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 3015 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3016 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3017 | struct connection *conn; |
| 3018 | |
| 3019 | if (!l4) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3020 | return 0; |
| 3021 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3022 | conn = objt_conn(l4->si[0].end); |
| 3023 | if (!conn || conn->xprt != &ssl_sock) |
| 3024 | return 0; |
| 3025 | |
| 3026 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3027 | smp->flags = SMP_F_MAY_CHANGE; |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3031 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3032 | return 0; |
| 3033 | |
| 3034 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3035 | smp->data.uint = (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 3036 | smp->flags = 0; |
| 3037 | |
| 3038 | return 1; |
| 3039 | } |
| 3040 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3041 | /* parse the "ca-file" bind keyword */ |
| 3042 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3043 | { |
| 3044 | if (!*args[cur_arg + 1]) { |
| 3045 | if (err) |
| 3046 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 3047 | return ERR_ALERT | ERR_FATAL; |
| 3048 | } |
| 3049 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3050 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 3051 | memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 3052 | else |
| 3053 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 3054 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3055 | return 0; |
| 3056 | } |
| 3057 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3058 | /* parse the "ciphers" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3059 | static int bind_parse_ciphers(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] | 3060 | { |
| 3061 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3062 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3063 | return ERR_ALERT | ERR_FATAL; |
| 3064 | } |
| 3065 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 3066 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3067 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3068 | return 0; |
| 3069 | } |
| 3070 | |
| 3071 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3072 | 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] | 3073 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 3074 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 3075 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3076 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3077 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3078 | return ERR_ALERT | ERR_FATAL; |
| 3079 | } |
| 3080 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 3081 | if ((*args[cur_arg + 1] != '/' ) && global.crt_base) { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 3082 | if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) { |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 3083 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 3084 | return ERR_ALERT | ERR_FATAL; |
| 3085 | } |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 3086 | snprintf(path, sizeof(path), "%s/%s", global.crt_base, args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 3087 | if (ssl_sock_load_cert(path, conf, px, err) > 0) |
| 3088 | return ERR_ALERT | ERR_FATAL; |
| 3089 | |
| 3090 | return 0; |
| 3091 | } |
| 3092 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3093 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3094 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3095 | |
| 3096 | return 0; |
| 3097 | } |
| 3098 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3099 | /* parse the "crt-list" bind keyword */ |
| 3100 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3101 | { |
| 3102 | if (!*args[cur_arg + 1]) { |
| 3103 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 3104 | return ERR_ALERT | ERR_FATAL; |
| 3105 | } |
| 3106 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3107 | if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) { |
| 3108 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3109 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3110 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3111 | |
| 3112 | return 0; |
| 3113 | } |
| 3114 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3115 | /* parse the "crl-file" bind keyword */ |
| 3116 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3117 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3118 | #ifndef X509_V_FLAG_CRL_CHECK |
| 3119 | if (err) |
| 3120 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 3121 | return ERR_ALERT | ERR_FATAL; |
| 3122 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3123 | if (!*args[cur_arg + 1]) { |
| 3124 | if (err) |
| 3125 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 3126 | return ERR_ALERT | ERR_FATAL; |
| 3127 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3128 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3129 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 3130 | memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 3131 | else |
| 3132 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 3133 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3134 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3135 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | /* parse the "ecdhe" bind keyword keywords */ |
| 3139 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3140 | { |
| 3141 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 3142 | if (err) |
| 3143 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 3144 | return ERR_ALERT | ERR_FATAL; |
| 3145 | #elif defined(OPENSSL_NO_ECDH) |
| 3146 | if (err) |
| 3147 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 3148 | return ERR_ALERT | ERR_FATAL; |
| 3149 | #else |
| 3150 | if (!*args[cur_arg + 1]) { |
| 3151 | if (err) |
| 3152 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 3153 | return ERR_ALERT | ERR_FATAL; |
| 3154 | } |
| 3155 | |
| 3156 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3157 | |
| 3158 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3159 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 3162 | /* parse the "crt_ignerr" and "ca_ignerr" bind keywords */ |
| 3163 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3164 | { |
| 3165 | int code; |
| 3166 | char *p = args[cur_arg + 1]; |
| 3167 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 3168 | |
| 3169 | if (!*p) { |
| 3170 | if (err) |
| 3171 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 3172 | return ERR_ALERT | ERR_FATAL; |
| 3173 | } |
| 3174 | |
| 3175 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 3176 | ignerr = &conf->ca_ignerr; |
| 3177 | |
| 3178 | if (strcmp(p, "all") == 0) { |
| 3179 | *ignerr = ~0ULL; |
| 3180 | return 0; |
| 3181 | } |
| 3182 | |
| 3183 | while (p) { |
| 3184 | code = atoi(p); |
| 3185 | if ((code <= 0) || (code > 63)) { |
| 3186 | if (err) |
| 3187 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 3188 | args[cur_arg], code, args[cur_arg + 1]); |
| 3189 | return ERR_ALERT | ERR_FATAL; |
| 3190 | } |
| 3191 | *ignerr |= 1ULL << code; |
| 3192 | p = strchr(p, ','); |
| 3193 | if (p) |
| 3194 | p++; |
| 3195 | } |
| 3196 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3197 | return 0; |
| 3198 | } |
| 3199 | |
| 3200 | /* parse the "force-sslv3" bind keyword */ |
| 3201 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3202 | { |
| 3203 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 3204 | return 0; |
| 3205 | } |
| 3206 | |
| 3207 | /* parse the "force-tlsv10" bind keyword */ |
| 3208 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3209 | { |
| 3210 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3211 | return 0; |
| 3212 | } |
| 3213 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3214 | /* parse the "force-tlsv11" bind keyword */ |
| 3215 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3216 | { |
| 3217 | #if SSL_OP_NO_TLSv1_1 |
| 3218 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 3219 | return 0; |
| 3220 | #else |
| 3221 | if (err) |
| 3222 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 3223 | return ERR_ALERT | ERR_FATAL; |
| 3224 | #endif |
| 3225 | } |
| 3226 | |
| 3227 | /* parse the "force-tlsv12" bind keyword */ |
| 3228 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3229 | { |
| 3230 | #if SSL_OP_NO_TLSv1_2 |
| 3231 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 3232 | return 0; |
| 3233 | #else |
| 3234 | if (err) |
| 3235 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 3236 | return ERR_ALERT | ERR_FATAL; |
| 3237 | #endif |
| 3238 | } |
| 3239 | |
| 3240 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3241 | /* parse the "no-tls-tickets" bind keyword */ |
| 3242 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3243 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3244 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3248 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3249 | /* parse the "no-sslv3" bind keyword */ |
| 3250 | static int bind_parse_no_sslv3(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] | 3251 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3252 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3253 | return 0; |
| 3254 | } |
| 3255 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3256 | /* parse the "no-tlsv10" bind keyword */ |
| 3257 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3258 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3259 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3260 | return 0; |
| 3261 | } |
| 3262 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3263 | /* parse the "no-tlsv11" bind keyword */ |
| 3264 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3265 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3266 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3267 | return 0; |
| 3268 | } |
| 3269 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3270 | /* parse the "no-tlsv12" bind keyword */ |
| 3271 | static int bind_parse_no_tlsv12(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] | 3272 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3273 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3274 | return 0; |
| 3275 | } |
| 3276 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3277 | /* parse the "npn" bind keyword */ |
| 3278 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3279 | { |
| 3280 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 3281 | char *p1, *p2; |
| 3282 | |
| 3283 | if (!*args[cur_arg + 1]) { |
| 3284 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 3285 | return ERR_ALERT | ERR_FATAL; |
| 3286 | } |
| 3287 | |
| 3288 | free(conf->npn_str); |
| 3289 | |
| 3290 | /* the NPN string is built as a suite of (<len> <name>)* */ |
| 3291 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
| 3292 | conf->npn_str = calloc(1, conf->npn_len); |
| 3293 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 3294 | |
| 3295 | /* replace commas with the name length */ |
| 3296 | p1 = conf->npn_str; |
| 3297 | p2 = p1 + 1; |
| 3298 | while (1) { |
| 3299 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 3300 | if (!p2) |
| 3301 | p2 = p1 + 1 + strlen(p1 + 1); |
| 3302 | |
| 3303 | if (p2 - (p1 + 1) > 255) { |
| 3304 | *p2 = '\0'; |
| 3305 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 3306 | return ERR_ALERT | ERR_FATAL; |
| 3307 | } |
| 3308 | |
| 3309 | *p1 = p2 - (p1 + 1); |
| 3310 | p1 = p2; |
| 3311 | |
| 3312 | if (!*p2) |
| 3313 | break; |
| 3314 | |
| 3315 | *(p2++) = '\0'; |
| 3316 | } |
| 3317 | return 0; |
| 3318 | #else |
| 3319 | if (err) |
| 3320 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 3321 | return ERR_ALERT | ERR_FATAL; |
| 3322 | #endif |
| 3323 | } |
| 3324 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3325 | /* parse the "alpn" bind keyword */ |
| 3326 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3327 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3328 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3329 | char *p1, *p2; |
| 3330 | |
| 3331 | if (!*args[cur_arg + 1]) { |
| 3332 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 3333 | return ERR_ALERT | ERR_FATAL; |
| 3334 | } |
| 3335 | |
| 3336 | free(conf->alpn_str); |
| 3337 | |
| 3338 | /* the ALPN string is built as a suite of (<len> <name>)* */ |
| 3339 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
| 3340 | conf->alpn_str = calloc(1, conf->alpn_len); |
| 3341 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 3342 | |
| 3343 | /* replace commas with the name length */ |
| 3344 | p1 = conf->alpn_str; |
| 3345 | p2 = p1 + 1; |
| 3346 | while (1) { |
| 3347 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 3348 | if (!p2) |
| 3349 | p2 = p1 + 1 + strlen(p1 + 1); |
| 3350 | |
| 3351 | if (p2 - (p1 + 1) > 255) { |
| 3352 | *p2 = '\0'; |
| 3353 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 3354 | return ERR_ALERT | ERR_FATAL; |
| 3355 | } |
| 3356 | |
| 3357 | *p1 = p2 - (p1 + 1); |
| 3358 | p1 = p2; |
| 3359 | |
| 3360 | if (!*p2) |
| 3361 | break; |
| 3362 | |
| 3363 | *(p2++) = '\0'; |
| 3364 | } |
| 3365 | return 0; |
| 3366 | #else |
| 3367 | if (err) |
| 3368 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 3369 | return ERR_ALERT | ERR_FATAL; |
| 3370 | #endif |
| 3371 | } |
| 3372 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3373 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3374 | 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] | 3375 | { |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3376 | struct listener *l; |
| 3377 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3378 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 3379 | |
| 3380 | if (global.listen_default_ciphers && !conf->ciphers) |
| 3381 | conf->ciphers = strdup(global.listen_default_ciphers); |
| 3382 | |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3383 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3384 | l->xprt = &ssl_sock; |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3385 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3386 | return 0; |
| 3387 | } |
| 3388 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3389 | /* parse the "strict-sni" bind keyword */ |
| 3390 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3391 | { |
| 3392 | conf->strict_sni = 1; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3396 | /* parse the "verify" bind keyword */ |
| 3397 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3398 | { |
| 3399 | if (!*args[cur_arg + 1]) { |
| 3400 | if (err) |
| 3401 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 3402 | return ERR_ALERT | ERR_FATAL; |
| 3403 | } |
| 3404 | |
| 3405 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3406 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3407 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3408 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3409 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3410 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3411 | else { |
| 3412 | if (err) |
| 3413 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 3414 | args[cur_arg], args[cur_arg + 1]); |
| 3415 | return ERR_ALERT | ERR_FATAL; |
| 3416 | } |
| 3417 | |
| 3418 | return 0; |
| 3419 | } |
| 3420 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3421 | /************** "server" keywords ****************/ |
| 3422 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3423 | /* parse the "ca-file" server keyword */ |
| 3424 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3425 | { |
| 3426 | if (!*args[*cur_arg + 1]) { |
| 3427 | if (err) |
| 3428 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 3429 | return ERR_ALERT | ERR_FATAL; |
| 3430 | } |
| 3431 | |
| 3432 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 3433 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3434 | else |
| 3435 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 3436 | |
| 3437 | return 0; |
| 3438 | } |
| 3439 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3440 | /* parse the "check-ssl" server keyword */ |
| 3441 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3442 | { |
| 3443 | newsrv->check.use_ssl = 1; |
| 3444 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 3445 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
| 3449 | /* parse the "ciphers" server keyword */ |
| 3450 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3451 | { |
| 3452 | if (!*args[*cur_arg + 1]) { |
| 3453 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 3454 | return ERR_ALERT | ERR_FATAL; |
| 3455 | } |
| 3456 | |
| 3457 | free(newsrv->ssl_ctx.ciphers); |
| 3458 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3462 | /* parse the "crl-file" server keyword */ |
| 3463 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3464 | { |
| 3465 | #ifndef X509_V_FLAG_CRL_CHECK |
| 3466 | if (err) |
| 3467 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 3468 | return ERR_ALERT | ERR_FATAL; |
| 3469 | #else |
| 3470 | if (!*args[*cur_arg + 1]) { |
| 3471 | if (err) |
| 3472 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 3473 | return ERR_ALERT | ERR_FATAL; |
| 3474 | } |
| 3475 | |
| 3476 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 3477 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3478 | else |
| 3479 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 3480 | |
| 3481 | return 0; |
| 3482 | #endif |
| 3483 | } |
| 3484 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3485 | /* parse the "crt" server keyword */ |
| 3486 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3487 | { |
| 3488 | if (!*args[*cur_arg + 1]) { |
| 3489 | if (err) |
| 3490 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 3491 | return ERR_ALERT | ERR_FATAL; |
| 3492 | } |
| 3493 | |
| 3494 | if ((*args[*cur_arg + 1] != '/') && global.crt_base) |
| 3495 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3496 | else |
| 3497 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 3498 | |
| 3499 | return 0; |
| 3500 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3501 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3502 | /* parse the "force-sslv3" server keyword */ |
| 3503 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3504 | { |
| 3505 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 3506 | return 0; |
| 3507 | } |
| 3508 | |
| 3509 | /* parse the "force-tlsv10" server keyword */ |
| 3510 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3511 | { |
| 3512 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 3513 | return 0; |
| 3514 | } |
| 3515 | |
| 3516 | /* parse the "force-tlsv11" server keyword */ |
| 3517 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3518 | { |
| 3519 | #if SSL_OP_NO_TLSv1_1 |
| 3520 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 3521 | return 0; |
| 3522 | #else |
| 3523 | if (err) |
| 3524 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 3525 | return ERR_ALERT | ERR_FATAL; |
| 3526 | #endif |
| 3527 | } |
| 3528 | |
| 3529 | /* parse the "force-tlsv12" server keyword */ |
| 3530 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3531 | { |
| 3532 | #if SSL_OP_NO_TLSv1_2 |
| 3533 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 3534 | return 0; |
| 3535 | #else |
| 3536 | if (err) |
| 3537 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 3538 | return ERR_ALERT | ERR_FATAL; |
| 3539 | #endif |
| 3540 | } |
| 3541 | |
| 3542 | /* parse the "no-sslv3" server keyword */ |
| 3543 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3544 | { |
| 3545 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 3546 | return 0; |
| 3547 | } |
| 3548 | |
| 3549 | /* parse the "no-tlsv10" server keyword */ |
| 3550 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3551 | { |
| 3552 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | /* parse the "no-tlsv11" server keyword */ |
| 3557 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3558 | { |
| 3559 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 3560 | return 0; |
| 3561 | } |
| 3562 | |
| 3563 | /* parse the "no-tlsv12" server keyword */ |
| 3564 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3565 | { |
| 3566 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3570 | /* parse the "no-tls-tickets" server keyword */ |
| 3571 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3572 | { |
| 3573 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3577 | /* parse the "ssl" server keyword */ |
| 3578 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3579 | { |
| 3580 | newsrv->use_ssl = 1; |
| 3581 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 3582 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 3583 | return 0; |
| 3584 | } |
| 3585 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3586 | /* parse the "verify" server keyword */ |
| 3587 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3588 | { |
| 3589 | if (!*args[*cur_arg + 1]) { |
| 3590 | if (err) |
| 3591 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 3592 | return ERR_ALERT | ERR_FATAL; |
| 3593 | } |
| 3594 | |
| 3595 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3596 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3597 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3598 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3599 | else { |
| 3600 | if (err) |
| 3601 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 3602 | args[*cur_arg], args[*cur_arg + 1]); |
| 3603 | return ERR_ALERT | ERR_FATAL; |
| 3604 | } |
| 3605 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3606 | return 0; |
| 3607 | } |
| 3608 | |
| 3609 | /* parse the "verifyhost" server keyword */ |
| 3610 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3611 | { |
| 3612 | if (!*args[*cur_arg + 1]) { |
| 3613 | if (err) |
| 3614 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 3615 | return ERR_ALERT | ERR_FATAL; |
| 3616 | } |
| 3617 | |
| 3618 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 3619 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3620 | return 0; |
| 3621 | } |
| 3622 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3623 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3624 | * Please take care of keeping this list alphabetically sorted. |
| 3625 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3626 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 3627 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3628 | { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3629 | { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3630 | { "ssl_c_i_dn", smp_fetch_ssl_c_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3631 | { "ssl_c_key_alg", smp_fetch_ssl_c_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3632 | { "ssl_c_notafter", smp_fetch_ssl_c_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3633 | { "ssl_c_notbefore", smp_fetch_ssl_c_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3634 | { "ssl_c_sig_alg", smp_fetch_ssl_c_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3635 | { "ssl_c_s_dn", smp_fetch_ssl_c_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3636 | { "ssl_c_serial", smp_fetch_ssl_c_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3637 | { "ssl_c_sha1", smp_fetch_ssl_c_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 3638 | { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3639 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3640 | { "ssl_c_version", smp_fetch_ssl_c_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3641 | { "ssl_f_i_dn", smp_fetch_ssl_f_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3642 | { "ssl_f_key_alg", smp_fetch_ssl_f_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3643 | { "ssl_f_notafter", smp_fetch_ssl_f_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3644 | { "ssl_f_notbefore", smp_fetch_ssl_f_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3645 | { "ssl_f_sig_alg", smp_fetch_ssl_f_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3646 | { "ssl_f_s_dn", smp_fetch_ssl_f_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3647 | { "ssl_f_serial", smp_fetch_ssl_f_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 3648 | { "ssl_f_version", smp_fetch_ssl_f_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3649 | { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3650 | { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3651 | { "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] | 3652 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3653 | { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3654 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3655 | { "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] | 3656 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3657 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3658 | { "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] | 3659 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3660 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 073edf3 | 2014-04-09 15:40:42 +0200 | [diff] [blame] | 3661 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 3662 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3663 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 3664 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3665 | { NULL, NULL, 0, 0, 0 }, |
| 3666 | }}; |
| 3667 | |
| 3668 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3669 | * Please take care of keeping this list alphabetically sorted. |
| 3670 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3671 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 3672 | { "ssl_c_i_dn", NULL, PAT_MATCH_STR }, |
| 3673 | { "ssl_c_key_alg", NULL, PAT_MATCH_STR }, |
| 3674 | { "ssl_c_notafter", NULL, PAT_MATCH_STR }, |
| 3675 | { "ssl_c_notbefore", NULL, PAT_MATCH_STR }, |
| 3676 | { "ssl_c_sig_alg", NULL, PAT_MATCH_STR }, |
| 3677 | { "ssl_c_s_dn", NULL, PAT_MATCH_STR }, |
| 3678 | { "ssl_c_serial", NULL, PAT_MATCH_BIN }, |
| 3679 | { "ssl_f_i_dn", NULL, PAT_MATCH_STR }, |
| 3680 | { "ssl_f_key_alg", NULL, PAT_MATCH_STR }, |
| 3681 | { "ssl_f_notafter", NULL, PAT_MATCH_STR }, |
| 3682 | { "ssl_f_notbefore", NULL, PAT_MATCH_STR }, |
| 3683 | { "ssl_f_sig_alg", NULL, PAT_MATCH_STR }, |
| 3684 | { "ssl_f_s_dn", NULL, PAT_MATCH_STR }, |
| 3685 | { "ssl_f_serial", NULL, PAT_MATCH_BIN }, |
| 3686 | { "ssl_fc_cipher", NULL, PAT_MATCH_STR }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3687 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 3688 | { "ssl_fc_npn", NULL, PAT_MATCH_STR }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3689 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3690 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 3691 | { "ssl_fc_alpn", NULL, PAT_MATCH_STR }, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3692 | #endif |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 3693 | { "ssl_fc_protocol", NULL, PAT_MATCH_STR }, |
| 3694 | { "ssl_fc_sni", "ssl_fc_sni", PAT_MATCH_STR }, |
| 3695 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 3696 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 3697 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3698 | }}; |
| 3699 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3700 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3701 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 3702 | * all code contributors. |
| 3703 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 3704 | * the config parser can report an appropriate error when a known keyword was |
| 3705 | * not enabled. |
| 3706 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 3707 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3708 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3709 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3710 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 3711 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3712 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3713 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 3714 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3715 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3716 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3717 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 3718 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 3719 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 3720 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3721 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 3722 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 3723 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 3724 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3725 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3726 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3727 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3728 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3729 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3730 | { NULL, NULL, 0 }, |
| 3731 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3733 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3734 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 3735 | * all code contributors. |
| 3736 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 3737 | * the config parser can report an appropriate error when a known keyword was |
| 3738 | * not enabled. |
| 3739 | */ |
| 3740 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3741 | { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 3742 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 3743 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3744 | { "crl-file", srv_parse_crl_file, 1, 0 }, /* set certificate revocation list file use on server cert verify */ |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3745 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 3746 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 3747 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 3748 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 3749 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
| 3750 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 3751 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 3752 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 3753 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3754 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 3755 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3756 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3757 | { "verifyhost", srv_parse_verifyhost, 1, 0 }, /* require that SSL cert verifies for hostname */ |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3758 | { NULL, NULL, 0, 0 }, |
| 3759 | }}; |
| 3760 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3761 | /* transport-layer operations for SSL sockets */ |
| 3762 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3763 | .snd_buf = ssl_sock_from_buf, |
| 3764 | .rcv_buf = ssl_sock_to_buf, |
| 3765 | .rcv_pipe = NULL, |
| 3766 | .snd_pipe = NULL, |
| 3767 | .shutr = NULL, |
| 3768 | .shutw = ssl_sock_shutw, |
| 3769 | .close = ssl_sock_close, |
| 3770 | .init = ssl_sock_init, |
| 3771 | }; |
| 3772 | |
| 3773 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3774 | static void __ssl_sock_init(void) |
| 3775 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3776 | STACK_OF(SSL_COMP)* cm; |
| 3777 | |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 3778 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 3779 | global.listen_default_ciphers = LISTEN_DEFAULT_CIPHERS; |
| 3780 | #endif |
| 3781 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 3782 | global.connect_default_ciphers = CONNECT_DEFAULT_CIPHERS; |
| 3783 | #endif |
| 3784 | if (global.listen_default_ciphers) |
| 3785 | global.listen_default_ciphers = strdup(global.listen_default_ciphers); |
| 3786 | if (global.connect_default_ciphers) |
| 3787 | global.connect_default_ciphers = strdup(global.connect_default_ciphers); |
| 3788 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3789 | SSL_library_init(); |
| 3790 | cm = SSL_COMP_get_compression_methods(); |
| 3791 | sk_SSL_COMP_zero(cm); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3792 | sample_register_fetches(&sample_fetch_keywords); |
| 3793 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3794 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3795 | srv_register_keywords(&srv_kws); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | /* |
| 3799 | * Local variables: |
| 3800 | * c-indent-level: 8 |
| 3801 | * c-basic-offset: 8 |
| 3802 | * End: |
| 3803 | */ |