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