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; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 731 | int ssloptions = |
| 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; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 739 | int sslmode = |
| 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; |
| 998 | int options = |
| 999 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 1000 | SSL_OP_NO_SSLv2 | |
| 1001 | SSL_OP_NO_COMPRESSION; |
| 1002 | int mode = |
| 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 | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1450 | if (objt_server(conn->target)) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1451 | if (!SSL_session_reused(conn->xprt_ctx)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1452 | /* 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] | 1453 | if (objt_server(conn->target)->ssl_ctx.reused_sess) |
| 1454 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1455 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1456 | 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] | 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /* The connection is now established at both layers, it's time to leave */ |
| 1461 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 1462 | return 1; |
| 1463 | |
| 1464 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1465 | /* Clear openssl global errors stack */ |
| 1466 | ERR_clear_error(); |
| 1467 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 1468 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1469 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 1470 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 1471 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1474 | /* Fail on all other handshake errors */ |
| 1475 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1476 | if (!conn->err_code) |
| 1477 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* 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] | 1482 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1483 | * buffer wraps, in which case a second call may be performed. The connection's |
| 1484 | * flags are updated with whatever special event is detected (error, read0, |
| 1485 | * empty). The caller is responsible for taking care of those events and |
| 1486 | * avoiding the call if inappropriate. The function does not call the |
| 1487 | * connection's polling update function, so the caller is responsible for this. |
| 1488 | */ |
| 1489 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 1490 | { |
| 1491 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1492 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1493 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1494 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1495 | goto out_error; |
| 1496 | |
| 1497 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1498 | /* a handshake was requested */ |
| 1499 | return 0; |
| 1500 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1501 | /* let's realign the buffer to optimize I/O */ |
| 1502 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1503 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1504 | |
| 1505 | /* read the largest possible block. For this, we perform only one call |
| 1506 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 1507 | * in which case we accept to do it once again. A new attempt is made on |
| 1508 | * EINTR too. |
| 1509 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 1510 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 1511 | /* first check if we have some room after p+i */ |
| 1512 | try = buf->data + buf->size - (buf->p + buf->i); |
| 1513 | /* otherwise continue between data and p-o */ |
| 1514 | if (try <= 0) { |
| 1515 | try = buf->p - (buf->data + buf->o); |
| 1516 | if (try <= 0) |
| 1517 | break; |
| 1518 | } |
| 1519 | if (try > count) |
| 1520 | try = count; |
| 1521 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1522 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1523 | if (conn->flags & CO_FL_ERROR) { |
| 1524 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1525 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1526 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1527 | if (ret > 0) { |
| 1528 | buf->i += ret; |
| 1529 | done += ret; |
| 1530 | if (ret < try) |
| 1531 | break; |
| 1532 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1533 | } |
| 1534 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1535 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 1536 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 1537 | /* error on protocol or underlying transport */ |
| 1538 | if ((ret != SSL_ERROR_SYSCALL) |
| 1539 | || (errno && (errno != EAGAIN))) |
| 1540 | conn->flags |= CO_FL_ERROR; |
| 1541 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1542 | /* Clear openssl global errors stack */ |
| 1543 | ERR_clear_error(); |
| 1544 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1545 | goto read0; |
| 1546 | } |
| 1547 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1548 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1549 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1550 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1551 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1552 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1553 | break; |
| 1554 | } |
| 1555 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 1556 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1557 | /* handshake is running, and it may need to re-enable read */ |
| 1558 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 1559 | __conn_sock_want_recv(conn); |
| 1560 | break; |
| 1561 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1562 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1563 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1564 | break; |
| 1565 | } |
| 1566 | /* otherwise it's a real error */ |
| 1567 | goto out_error; |
| 1568 | } |
| 1569 | } |
| 1570 | return done; |
| 1571 | |
| 1572 | read0: |
| 1573 | conn_sock_read0(conn); |
| 1574 | return done; |
| 1575 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1576 | /* Clear openssl global errors stack */ |
| 1577 | ERR_clear_error(); |
| 1578 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1579 | conn->flags |= CO_FL_ERROR; |
| 1580 | return done; |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 1585 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 1586 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1587 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 1588 | * a second call may be performed. The connection's flags are updated with |
| 1589 | * whatever special event is detected (error, empty). The caller is responsible |
| 1590 | * for taking care of those events and avoiding the call if inappropriate. The |
| 1591 | * function does not call the connection's polling update function, so the caller |
| 1592 | * is responsible for this. |
| 1593 | */ |
| 1594 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 1595 | { |
| 1596 | int ret, try, done; |
| 1597 | |
| 1598 | done = 0; |
| 1599 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1600 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1601 | goto out_error; |
| 1602 | |
| 1603 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1604 | /* a handshake was requested */ |
| 1605 | return 0; |
| 1606 | |
| 1607 | /* send the largest possible block. For this we perform only one call |
| 1608 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 1609 | * in which case we accept to do it once again. |
| 1610 | */ |
| 1611 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 1612 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1613 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 1614 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1615 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
| 1616 | global.tune.ssl_max_record && try > global.tune.ssl_max_record) { |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1617 | try = global.tune.ssl_max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1618 | } |
| 1619 | else { |
| 1620 | /* we need to keep the information about the fact that |
| 1621 | * we're not limiting the upcoming send(), because if it |
| 1622 | * fails, we'll have to retry with at least as many data. |
| 1623 | */ |
| 1624 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 1625 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 1626 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1627 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1628 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1629 | if (conn->flags & CO_FL_ERROR) { |
| 1630 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1631 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1632 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1633 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 1634 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 1635 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1636 | buf->o -= ret; |
| 1637 | done += ret; |
| 1638 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 1639 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1640 | /* optimize data alignment in the buffer */ |
| 1641 | buf->p = buf->data; |
| 1642 | |
| 1643 | /* if the system buffer is full, don't insist */ |
| 1644 | if (ret < try) |
| 1645 | break; |
| 1646 | } |
| 1647 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1648 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1649 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 1650 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 1651 | /* handshake is running, and it may need to re-enable write */ |
| 1652 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 1653 | __conn_sock_want_send(conn); |
| 1654 | break; |
| 1655 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1656 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 1657 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1658 | break; |
| 1659 | } |
| 1660 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1661 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1662 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 1663 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | goto out_error; |
| 1667 | } |
| 1668 | } |
| 1669 | return done; |
| 1670 | |
| 1671 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1672 | /* Clear openssl global errors stack */ |
| 1673 | ERR_clear_error(); |
| 1674 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1675 | conn->flags |= CO_FL_ERROR; |
| 1676 | return done; |
| 1677 | } |
| 1678 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1679 | static void ssl_sock_close(struct connection *conn) { |
| 1680 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1681 | if (conn->xprt_ctx) { |
| 1682 | SSL_free(conn->xprt_ctx); |
| 1683 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1684 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1685 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 1689 | * any case, flags the connection as reusable if no handshake was in progress. |
| 1690 | */ |
| 1691 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 1692 | { |
| 1693 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1694 | return; |
| 1695 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1696 | if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) { |
| 1697 | /* Clear openssl global errors stack */ |
| 1698 | ERR_clear_error(); |
| 1699 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1700 | |
| 1701 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1702 | SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1705 | /* used for logging, may be changed for a sample fetch later */ |
| 1706 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 1707 | { |
| 1708 | if (!conn->xprt && !conn->xprt_ctx) |
| 1709 | return NULL; |
| 1710 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 1711 | } |
| 1712 | |
| 1713 | /* used for logging, may be changed for a sample fetch later */ |
| 1714 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 1715 | { |
| 1716 | if (!conn->xprt && !conn->xprt_ctx) |
| 1717 | return NULL; |
| 1718 | return SSL_get_version(conn->xprt_ctx); |
| 1719 | } |
| 1720 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1721 | /* Extract a serial from a cert, and copy it to a chunk. |
| 1722 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 1723 | * -1 if output is not large enough. |
| 1724 | */ |
| 1725 | static int |
| 1726 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 1727 | { |
| 1728 | ASN1_INTEGER *serial; |
| 1729 | |
| 1730 | serial = X509_get_serialNumber(crt); |
| 1731 | if (!serial) |
| 1732 | return 0; |
| 1733 | |
| 1734 | if (out->size < serial->length) |
| 1735 | return -1; |
| 1736 | |
| 1737 | memcpy(out->str, serial->data, serial->length); |
| 1738 | out->len = serial->length; |
| 1739 | return 1; |
| 1740 | } |
| 1741 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 1742 | |
| 1743 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 1744 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 1745 | * and -1 if output is not large enough. |
| 1746 | */ |
| 1747 | static int |
| 1748 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 1749 | { |
| 1750 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 1751 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 1752 | |
| 1753 | if (gentm->length < 12) |
| 1754 | return 0; |
| 1755 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 1756 | return 0; |
| 1757 | if (out->size < gentm->length-2) |
| 1758 | return -1; |
| 1759 | |
| 1760 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 1761 | out->len = gentm->length-2; |
| 1762 | return 1; |
| 1763 | } |
| 1764 | else if (tm->type == V_ASN1_UTCTIME) { |
| 1765 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 1766 | |
| 1767 | if (utctm->length < 10) |
| 1768 | return 0; |
| 1769 | if (utctm->data[0] >= 0x35) |
| 1770 | return 0; |
| 1771 | if (out->size < utctm->length) |
| 1772 | return -1; |
| 1773 | |
| 1774 | memcpy(out->str, utctm->data, utctm->length); |
| 1775 | out->len = utctm->length; |
| 1776 | return 1; |
| 1777 | } |
| 1778 | |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 1782 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 1783 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 1784 | */ |
| 1785 | static int |
| 1786 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 1787 | { |
| 1788 | X509_NAME_ENTRY *ne; |
| 1789 | int i, j, n; |
| 1790 | int cur = 0; |
| 1791 | const char *s; |
| 1792 | char tmp[128]; |
| 1793 | |
| 1794 | out->len = 0; |
| 1795 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 1796 | if (pos < 0) |
| 1797 | j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i; |
| 1798 | else |
| 1799 | j = i; |
| 1800 | |
| 1801 | ne = sk_X509_NAME_ENTRY_value(a->entries, j); |
| 1802 | n = OBJ_obj2nid(ne->object); |
| 1803 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 1804 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 1805 | s = tmp; |
| 1806 | } |
| 1807 | |
| 1808 | if (chunk_strcasecmp(entry, s) != 0) |
| 1809 | continue; |
| 1810 | |
| 1811 | if (pos < 0) |
| 1812 | cur--; |
| 1813 | else |
| 1814 | cur++; |
| 1815 | |
| 1816 | if (cur != pos) |
| 1817 | continue; |
| 1818 | |
| 1819 | if (ne->value->length > out->size) |
| 1820 | return -1; |
| 1821 | |
| 1822 | memcpy(out->str, ne->value->data, ne->value->length); |
| 1823 | out->len = ne->value->length; |
| 1824 | return 1; |
| 1825 | } |
| 1826 | |
| 1827 | return 0; |
| 1828 | |
| 1829 | } |
| 1830 | |
| 1831 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 1832 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 1833 | */ |
| 1834 | static int |
| 1835 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 1836 | { |
| 1837 | X509_NAME_ENTRY *ne; |
| 1838 | int i, n, ln; |
| 1839 | int l = 0; |
| 1840 | const char *s; |
| 1841 | char *p; |
| 1842 | char tmp[128]; |
| 1843 | |
| 1844 | out->len = 0; |
| 1845 | p = out->str; |
| 1846 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 1847 | ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 1848 | n = OBJ_obj2nid(ne->object); |
| 1849 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 1850 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 1851 | s = tmp; |
| 1852 | } |
| 1853 | ln = strlen(s); |
| 1854 | |
| 1855 | l += 1 + ln + 1 + ne->value->length; |
| 1856 | if (l > out->size) |
| 1857 | return -1; |
| 1858 | out->len = l; |
| 1859 | |
| 1860 | *(p++)='/'; |
| 1861 | memcpy(p, s, ln); |
| 1862 | p += ln; |
| 1863 | *(p++)='='; |
| 1864 | memcpy(p, ne->value->data, ne->value->length); |
| 1865 | p += ne->value->length; |
| 1866 | } |
| 1867 | |
| 1868 | if (!out->len) |
| 1869 | return 0; |
| 1870 | |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1874 | char *ssl_sock_get_version(struct connection *conn) |
| 1875 | { |
| 1876 | if (!ssl_sock_is_ssl(conn)) |
| 1877 | return NULL; |
| 1878 | |
| 1879 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 1880 | } |
| 1881 | |
| 1882 | /* returns common name, NULL terminated, from client certificate, or NULL if none */ |
| 1883 | char *ssl_sock_get_common_name(struct connection *conn) |
| 1884 | { |
| 1885 | X509 *crt = NULL; |
| 1886 | X509_NAME *name; |
| 1887 | struct chunk *cn_trash; |
| 1888 | const char find_cn[] = "CN"; |
| 1889 | const struct chunk find_cn_chunk = { |
| 1890 | .str = (char *)&find_cn, |
| 1891 | .len = sizeof(find_cn)-1 |
| 1892 | }; |
| 1893 | char *result = NULL; |
| 1894 | |
| 1895 | if (!ssl_sock_is_ssl(conn)) |
| 1896 | return NULL; |
| 1897 | |
| 1898 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 1899 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 1900 | if (!crt) |
| 1901 | goto out; |
| 1902 | |
| 1903 | name = X509_get_subject_name(crt); |
| 1904 | if (!name) |
| 1905 | goto out; |
| 1906 | |
| 1907 | cn_trash = get_trash_chunk(); |
| 1908 | if (ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, cn_trash) <= 0) |
| 1909 | goto out; |
| 1910 | cn_trash->str[cn_trash->len] = '\0'; |
| 1911 | result = cn_trash->str; |
| 1912 | |
| 1913 | out: |
| 1914 | if (crt) |
| 1915 | X509_free(crt); |
| 1916 | |
| 1917 | return result; |
| 1918 | } |
| 1919 | |
| 1920 | /* returns 1 if client passed a certificate, 0 if not */ |
| 1921 | int ssl_sock_get_cert_used(struct connection *conn) |
| 1922 | { |
| 1923 | if (!ssl_sock_is_ssl(conn)) |
| 1924 | return 0; |
| 1925 | |
| 1926 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 1927 | } |
| 1928 | |
| 1929 | /* returns result from SSL verify */ |
| 1930 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 1931 | { |
| 1932 | if (!ssl_sock_is_ssl(conn)) |
| 1933 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 1934 | |
| 1935 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 1936 | } |
| 1937 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1938 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 1939 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1940 | /* boolean, returns true if client cert was present */ |
| 1941 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 1942 | 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] | 1943 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1944 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1945 | struct connection *conn; |
| 1946 | |
| 1947 | if (!l4) |
| 1948 | return 0; |
| 1949 | |
| 1950 | conn = objt_conn(l4->si[0].end); |
| 1951 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1952 | return 0; |
| 1953 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1954 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1955 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | smp->flags = 0; |
| 1960 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1961 | 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] | 1962 | |
| 1963 | return 1; |
| 1964 | } |
| 1965 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 1966 | /* binary, returns serial of certificate in a binary chunk. |
| 1967 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 1968 | * should be use. |
| 1969 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1970 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 1971 | 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] | 1972 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1973 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 1974 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1975 | X509 *crt = NULL; |
| 1976 | int ret = 0; |
| 1977 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1978 | struct connection *conn; |
| 1979 | |
| 1980 | if (!l4) |
| 1981 | return 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1983 | conn = objt_conn(l4->si[0].end); |
| 1984 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1985 | return 0; |
| 1986 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1987 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1988 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 1992 | if (cert_peer) |
| 1993 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 1994 | else |
| 1995 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 1996 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 1997 | if (!crt) |
| 1998 | goto out; |
| 1999 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2000 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2001 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 2002 | goto out; |
| 2003 | |
| 2004 | smp->data.str = *smp_trash; |
| 2005 | smp->type = SMP_T_BIN; |
| 2006 | ret = 1; |
| 2007 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2008 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2009 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2010 | X509_free(crt); |
| 2011 | return ret; |
| 2012 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 2013 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2014 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 2015 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2016 | * should be use. |
| 2017 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2018 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2019 | 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] | 2020 | const struct arg *args, struct sample *smp, const char *kw) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2021 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2022 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2023 | X509 *crt = NULL; |
| 2024 | const EVP_MD *digest; |
| 2025 | int ret = 0; |
| 2026 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2027 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2028 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2029 | if (!l4) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2030 | return 0; |
| 2031 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2032 | conn = objt_conn(l4->si[0].end); |
| 2033 | if (!conn || conn->xprt != &ssl_sock) |
| 2034 | return 0; |
| 2035 | |
| 2036 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2037 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2038 | return 0; |
| 2039 | } |
| 2040 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2041 | if (cert_peer) |
| 2042 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2043 | else |
| 2044 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2045 | if (!crt) |
| 2046 | goto out; |
| 2047 | |
| 2048 | smp_trash = get_trash_chunk(); |
| 2049 | digest = EVP_sha1(); |
| 2050 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 2051 | |
| 2052 | smp->data.str = *smp_trash; |
| 2053 | smp->type = SMP_T_BIN; |
| 2054 | ret = 1; |
| 2055 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2056 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2057 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 2058 | X509_free(crt); |
| 2059 | return ret; |
| 2060 | } |
| 2061 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2062 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 2063 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2064 | * should be use. |
| 2065 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2066 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2067 | 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] | 2068 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2069 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2070 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2071 | X509 *crt = NULL; |
| 2072 | int ret = 0; |
| 2073 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2074 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2075 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2076 | if (!l4) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2077 | return 0; |
| 2078 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2079 | conn = objt_conn(l4->si[0].end); |
| 2080 | if (!conn || conn->xprt != &ssl_sock) |
| 2081 | return 0; |
| 2082 | |
| 2083 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2084 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2088 | if (cert_peer) |
| 2089 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2090 | else |
| 2091 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2092 | if (!crt) |
| 2093 | goto out; |
| 2094 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2095 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2096 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 2097 | goto out; |
| 2098 | |
| 2099 | smp->data.str = *smp_trash; |
| 2100 | smp->type = SMP_T_STR; |
| 2101 | ret = 1; |
| 2102 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2103 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2104 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2105 | X509_free(crt); |
| 2106 | return ret; |
| 2107 | } |
| 2108 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2109 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 2110 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2111 | * should be use. |
| 2112 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2113 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2114 | 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] | 2115 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2116 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2117 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2118 | X509 *crt = NULL; |
| 2119 | X509_NAME *name; |
| 2120 | int ret = 0; |
| 2121 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2122 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2123 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2124 | if (!l4) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2125 | return 0; |
| 2126 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2127 | conn = objt_conn(l4->si[0].end); |
| 2128 | if (!conn || conn->xprt != &ssl_sock) |
| 2129 | return 0; |
| 2130 | |
| 2131 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2132 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2136 | if (cert_peer) |
| 2137 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2138 | else |
| 2139 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2140 | if (!crt) |
| 2141 | goto out; |
| 2142 | |
| 2143 | name = X509_get_issuer_name(crt); |
| 2144 | if (!name) |
| 2145 | goto out; |
| 2146 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2147 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2148 | if (args && args[0].type == ARGT_STR) { |
| 2149 | int pos = 1; |
| 2150 | |
| 2151 | if (args[1].type == ARGT_SINT) |
| 2152 | pos = args[1].data.sint; |
| 2153 | else if (args[1].type == ARGT_UINT) |
| 2154 | pos =(int)args[1].data.uint; |
| 2155 | |
| 2156 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2157 | goto out; |
| 2158 | } |
| 2159 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2160 | goto out; |
| 2161 | |
| 2162 | smp->type = SMP_T_STR; |
| 2163 | smp->data.str = *smp_trash; |
| 2164 | ret = 1; |
| 2165 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2166 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2167 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2168 | X509_free(crt); |
| 2169 | return ret; |
| 2170 | } |
| 2171 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2172 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 2173 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2174 | * should be use. |
| 2175 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2176 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2177 | 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] | 2178 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2179 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2180 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2181 | X509 *crt = NULL; |
| 2182 | int ret = 0; |
| 2183 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2184 | struct connection *conn; |
| 2185 | |
| 2186 | if (!l4) |
| 2187 | return 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2188 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2189 | conn = objt_conn(l4->si[0].end); |
| 2190 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2191 | return 0; |
| 2192 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2193 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2194 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2198 | if (cert_peer) |
| 2199 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2200 | else |
| 2201 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2202 | if (!crt) |
| 2203 | goto out; |
| 2204 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2205 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2206 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 2207 | goto out; |
| 2208 | |
| 2209 | smp->data.str = *smp_trash; |
| 2210 | smp->type = SMP_T_STR; |
| 2211 | ret = 1; |
| 2212 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2213 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2214 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2215 | X509_free(crt); |
| 2216 | return ret; |
| 2217 | } |
| 2218 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2219 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 2220 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2221 | * should be use. |
| 2222 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2223 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2224 | 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] | 2225 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2226 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2227 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2228 | X509 *crt = NULL; |
| 2229 | X509_NAME *name; |
| 2230 | int ret = 0; |
| 2231 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2232 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2233 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2234 | if (!l4) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2235 | return 0; |
| 2236 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2237 | conn = objt_conn(l4->si[0].end); |
| 2238 | if (!conn || conn->xprt != &ssl_sock) |
| 2239 | return 0; |
| 2240 | |
| 2241 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2242 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2246 | if (cert_peer) |
| 2247 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2248 | else |
| 2249 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2250 | if (!crt) |
| 2251 | goto out; |
| 2252 | |
| 2253 | name = X509_get_subject_name(crt); |
| 2254 | if (!name) |
| 2255 | goto out; |
| 2256 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 2257 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2258 | if (args && args[0].type == ARGT_STR) { |
| 2259 | int pos = 1; |
| 2260 | |
| 2261 | if (args[1].type == ARGT_SINT) |
| 2262 | pos = args[1].data.sint; |
| 2263 | else if (args[1].type == ARGT_UINT) |
| 2264 | pos =(int)args[1].data.uint; |
| 2265 | |
| 2266 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 2267 | goto out; |
| 2268 | } |
| 2269 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 2270 | goto out; |
| 2271 | |
| 2272 | smp->type = SMP_T_STR; |
| 2273 | smp->data.str = *smp_trash; |
| 2274 | ret = 1; |
| 2275 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2276 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 2277 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2278 | X509_free(crt); |
| 2279 | return ret; |
| 2280 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2281 | |
| 2282 | /* integer, returns true if current session use a client certificate */ |
| 2283 | static int |
| 2284 | 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] | 2285 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2286 | { |
| 2287 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2288 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2289 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2290 | if (!l4) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2291 | return 0; |
| 2292 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2293 | conn = objt_conn(l4->si[0].end); |
| 2294 | if (!conn || conn->xprt != &ssl_sock) |
| 2295 | return 0; |
| 2296 | |
| 2297 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2298 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2299 | return 0; |
| 2300 | } |
| 2301 | |
| 2302 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2303 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 2304 | if (crt) { |
| 2305 | X509_free(crt); |
| 2306 | } |
| 2307 | |
| 2308 | smp->type = SMP_T_BOOL; |
| 2309 | smp->data.uint = (crt != NULL); |
| 2310 | return 1; |
| 2311 | } |
| 2312 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2313 | /* integer, returns the certificate version |
| 2314 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2315 | * should be use. |
| 2316 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2317 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2318 | 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] | 2319 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2320 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2321 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2322 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2323 | struct connection *conn; |
| 2324 | |
| 2325 | if (!l4) |
| 2326 | return 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2327 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2328 | conn = objt_conn(l4->si[0].end); |
| 2329 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2330 | return 0; |
| 2331 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2332 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2333 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2334 | return 0; |
| 2335 | } |
| 2336 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2337 | if (cert_peer) |
| 2338 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2339 | else |
| 2340 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2341 | if (!crt) |
| 2342 | return 0; |
| 2343 | |
| 2344 | smp->data.uint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2345 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 2346 | if (cert_peer) |
| 2347 | X509_free(crt); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 2348 | smp->type = SMP_T_UINT; |
| 2349 | |
| 2350 | return 1; |
| 2351 | } |
| 2352 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2353 | /* string, returns the certificate's signature algorithm. |
| 2354 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2355 | * should be use. |
| 2356 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2357 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2358 | 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] | 2359 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2360 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2361 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2362 | X509 *crt; |
| 2363 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2364 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2365 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2366 | if (!l4) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2367 | return 0; |
| 2368 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2369 | conn = objt_conn(l4->si[0].end); |
| 2370 | if (!conn || conn->xprt != &ssl_sock) |
| 2371 | return 0; |
| 2372 | |
| 2373 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2374 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2378 | if (cert_peer) |
| 2379 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2380 | else |
| 2381 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2382 | if (!crt) |
| 2383 | return 0; |
| 2384 | |
| 2385 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm)); |
| 2386 | |
| 2387 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2388 | if (!smp->data.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2389 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 2390 | if (cert_peer) |
| 2391 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2392 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2393 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2394 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2395 | smp->type = SMP_T_STR; |
| 2396 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 2397 | smp->data.str.len = strlen(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 | |
| 2402 | return 1; |
| 2403 | } |
| 2404 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2405 | /* string, returns the certificate's key algorithm. |
| 2406 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 2407 | * should be use. |
| 2408 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2409 | static int |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2410 | 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] | 2411 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2412 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2413 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2414 | X509 *crt; |
| 2415 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2416 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2417 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2418 | if (!l4) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2419 | return 0; |
| 2420 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2421 | conn = objt_conn(l4->si[0].end); |
| 2422 | if (!conn || conn->xprt != &ssl_sock) |
| 2423 | return 0; |
| 2424 | |
| 2425 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2426 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2430 | if (cert_peer) |
| 2431 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 2432 | else |
| 2433 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2434 | if (!crt) |
| 2435 | return 0; |
| 2436 | |
| 2437 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm)); |
| 2438 | |
| 2439 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2440 | if (!smp->data.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2441 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 2442 | if (cert_peer) |
| 2443 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2444 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 2445 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2446 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2447 | smp->type = SMP_T_STR; |
| 2448 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2449 | smp->data.str.len = strlen(smp->data.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 2450 | if (cert_peer) |
| 2451 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 2452 | |
| 2453 | return 1; |
| 2454 | } |
| 2455 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2456 | /* boolean, returns true if front conn. transport layer is SSL. |
| 2457 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2458 | * char is 'b'. |
| 2459 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2460 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2461 | 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] | 2462 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2463 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2464 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
| 2465 | struct connection *conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2466 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2467 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2468 | smp->data.uint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2469 | return 1; |
| 2470 | } |
| 2471 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2472 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2473 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2474 | 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] | 2475 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2476 | { |
| 2477 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2478 | struct connection *conn = objt_conn(l4->si[0].end); |
| 2479 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2480 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2481 | smp->data.uint = (conn && conn->xprt == &ssl_sock) && |
| 2482 | conn->xprt_ctx && |
| 2483 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2484 | return 1; |
| 2485 | #else |
| 2486 | return 0; |
| 2487 | #endif |
| 2488 | } |
| 2489 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2490 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 2491 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2492 | * char is 'b'. |
| 2493 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2494 | static int |
| 2495 | 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] | 2496 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2497 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2498 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2499 | struct connection *conn; |
| 2500 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2501 | smp->flags = 0; |
| 2502 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2503 | if (!l4) |
| 2504 | return 0; |
| 2505 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2506 | conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2507 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2508 | return 0; |
| 2509 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2510 | smp->data.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2511 | if (!smp->data.str.str) |
| 2512 | return 0; |
| 2513 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2514 | smp->type = SMP_T_STR; |
| 2515 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2516 | smp->data.str.len = strlen(smp->data.str.str); |
| 2517 | |
| 2518 | return 1; |
| 2519 | } |
| 2520 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2521 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 2522 | * is SSL. |
| 2523 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2524 | * char is 'b'. |
| 2525 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2526 | static int |
| 2527 | 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] | 2528 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2529 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2530 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2531 | struct connection *conn; |
| 2532 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2533 | smp->flags = 0; |
| 2534 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2535 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2536 | return 0; |
| 2537 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2538 | conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2539 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2540 | return 0; |
| 2541 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2542 | if (!SSL_get_cipher_bits(conn->xprt_ctx, (int *)&smp->data.uint)) |
| 2543 | return 0; |
| 2544 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2545 | smp->type = SMP_T_UINT; |
| 2546 | |
| 2547 | return 1; |
| 2548 | } |
| 2549 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2550 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 2551 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2552 | * char is 'b'. |
| 2553 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2554 | static int |
| 2555 | 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] | 2556 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2557 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2558 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2559 | struct connection *conn; |
| 2560 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2561 | smp->flags = 0; |
| 2562 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2563 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2564 | return 0; |
| 2565 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2566 | conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2567 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2568 | return 0; |
| 2569 | |
| 2570 | 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] | 2571 | if (!smp->data.uint) |
| 2572 | return 0; |
| 2573 | |
| 2574 | smp->type = SMP_T_UINT; |
| 2575 | |
| 2576 | return 1; |
| 2577 | } |
| 2578 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2579 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2580 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2581 | 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] | 2582 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2583 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2584 | struct connection *conn; |
| 2585 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2586 | smp->flags = SMP_F_CONST; |
| 2587 | smp->type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2588 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2589 | if (!l4) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2590 | return 0; |
| 2591 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2592 | conn = objt_conn(l4->si[0].end); |
| 2593 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2594 | return 0; |
| 2595 | |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2596 | smp->data.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2597 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2598 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 2599 | |
| 2600 | if (!smp->data.str.str) |
| 2601 | return 0; |
| 2602 | |
| 2603 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2604 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2605 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2606 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2607 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2608 | static int |
| 2609 | 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] | 2610 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2611 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2612 | struct connection *conn; |
| 2613 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2614 | smp->flags = SMP_F_CONST; |
| 2615 | smp->type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2616 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2617 | if (!l4) |
| 2618 | return 0; |
| 2619 | |
| 2620 | conn = objt_conn(l4->si[0].end); |
| 2621 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2622 | return 0; |
| 2623 | |
| 2624 | smp->data.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2625 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2626 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 2627 | |
| 2628 | if (!smp->data.str.str) |
| 2629 | return 0; |
| 2630 | |
| 2631 | return 1; |
| 2632 | } |
| 2633 | #endif |
| 2634 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2635 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 2636 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2637 | * char is 'b'. |
| 2638 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 2639 | static int |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2640 | 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] | 2641 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2642 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2643 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2644 | struct connection *conn; |
| 2645 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2646 | smp->flags = 0; |
| 2647 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2648 | if (!l4) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2649 | return 0; |
| 2650 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2651 | conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2652 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2653 | return 0; |
| 2654 | |
| 2655 | smp->data.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2656 | if (!smp->data.str.str) |
| 2657 | return 0; |
| 2658 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2659 | smp->type = SMP_T_STR; |
| 2660 | smp->flags = SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2661 | smp->data.str.len = strlen(smp->data.str.str); |
| 2662 | |
| 2663 | return 1; |
| 2664 | } |
| 2665 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2666 | /* binary, returns the SSL session id if front conn. transport layer is SSL. |
| 2667 | * This function is also usable on backend conn if the fetch keyword 5th |
| 2668 | * char is 'b'. |
| 2669 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 2670 | static int |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2671 | 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] | 2672 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2673 | { |
| 2674 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2675 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2676 | SSL_SESSION *sess; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2677 | struct connection *conn; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2678 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2679 | smp->flags = SMP_F_CONST; |
| 2680 | smp->type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2681 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2682 | if (!l4) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2683 | return 0; |
| 2684 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2685 | conn = objt_conn(l4->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2686 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2687 | return 0; |
| 2688 | |
| 2689 | sess = SSL_get_session(conn->xprt_ctx); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 2690 | if (!sess) |
| 2691 | return 0; |
| 2692 | |
| 2693 | smp->data.str.str = (char *)SSL_SESSION_get_id(sess, (unsigned int *)&smp->data.str.len); |
| 2694 | if (!smp->data.str.str || !&smp->data.str.len) |
| 2695 | return 0; |
| 2696 | |
| 2697 | return 1; |
| 2698 | #else |
| 2699 | return 0; |
| 2700 | #endif |
| 2701 | } |
| 2702 | |
| 2703 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2704 | 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] | 2705 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2706 | { |
| 2707 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2708 | struct connection *conn; |
| 2709 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2710 | smp->flags = SMP_F_CONST; |
| 2711 | smp->type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2712 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2713 | if (!l4) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2714 | return 0; |
| 2715 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2716 | conn = objt_conn(l4->si[0].end); |
| 2717 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2718 | return 0; |
| 2719 | |
| 2720 | 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] | 2721 | if (!smp->data.str.str) |
| 2722 | return 0; |
| 2723 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 2724 | smp->data.str.len = strlen(smp->data.str.str); |
| 2725 | return 1; |
| 2726 | #else |
| 2727 | return 0; |
| 2728 | #endif |
| 2729 | } |
| 2730 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2731 | static int |
| 2732 | smp_fetch_ssl_fc_unique_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 2733 | const struct arg *args, struct sample *smp, const char *kw) |
| 2734 | { |
| 2735 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2736 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2737 | struct connection *conn; |
| 2738 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2739 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2740 | |
| 2741 | smp->flags = 0; |
| 2742 | |
| 2743 | if (!l4) |
| 2744 | return 0; |
| 2745 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 2746 | conn = objt_conn(l4->si[back_conn].end); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2747 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 2748 | return 0; |
| 2749 | |
| 2750 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2751 | smp->flags |= SMP_F_MAY_CHANGE; |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | finished_trash = get_trash_chunk(); |
| 2756 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 2757 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 2758 | else |
| 2759 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 2760 | |
| 2761 | if (!finished_len) |
| 2762 | return 0; |
| 2763 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 2764 | finished_trash->len = finished_len; |
| 2765 | smp->data.str = *finished_trash; |
| 2766 | smp->type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 2767 | |
| 2768 | return 1; |
| 2769 | #else |
| 2770 | return 0; |
| 2771 | #endif |
| 2772 | } |
| 2773 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2774 | /* 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] | 2775 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2776 | 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] | 2777 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2778 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2779 | struct connection *conn; |
| 2780 | |
| 2781 | if (!l4) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2782 | return 0; |
| 2783 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2784 | conn = objt_conn(l4->si[0].end); |
| 2785 | if (!conn || conn->xprt != &ssl_sock) |
| 2786 | return 0; |
| 2787 | |
| 2788 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2789 | smp->flags = SMP_F_MAY_CHANGE; |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
| 2793 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2794 | 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] | 2795 | smp->flags = 0; |
| 2796 | |
| 2797 | return 1; |
| 2798 | } |
| 2799 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2800 | /* 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] | 2801 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2802 | 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] | 2803 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2804 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2805 | struct connection *conn; |
| 2806 | |
| 2807 | if (!l4) |
| 2808 | return 0; |
| 2809 | |
| 2810 | conn = objt_conn(l4->si[0].end); |
| 2811 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2812 | return 0; |
| 2813 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2814 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2815 | smp->flags = SMP_F_MAY_CHANGE; |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2820 | 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] | 2821 | smp->flags = 0; |
| 2822 | |
| 2823 | return 1; |
| 2824 | } |
| 2825 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2826 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2827 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2828 | 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] | 2829 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2830 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2831 | struct connection *conn; |
| 2832 | |
| 2833 | if (!l4) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2834 | return 0; |
| 2835 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2836 | conn = objt_conn(l4->si[0].end); |
| 2837 | if (!conn || conn->xprt != &ssl_sock) |
| 2838 | return 0; |
| 2839 | |
| 2840 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2841 | smp->flags = SMP_F_MAY_CHANGE; |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2846 | 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] | 2847 | smp->flags = 0; |
| 2848 | |
| 2849 | return 1; |
| 2850 | } |
| 2851 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2852 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2853 | static int |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 2854 | 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] | 2855 | const struct arg *args, struct sample *smp, const char *kw) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2856 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2857 | struct connection *conn; |
| 2858 | |
| 2859 | if (!l4) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2860 | return 0; |
| 2861 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2862 | conn = objt_conn(l4->si[0].end); |
| 2863 | if (!conn || conn->xprt != &ssl_sock) |
| 2864 | return 0; |
| 2865 | |
| 2866 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2867 | smp->flags = SMP_F_MAY_CHANGE; |
| 2868 | return 0; |
| 2869 | } |
| 2870 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2871 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2872 | return 0; |
| 2873 | |
| 2874 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2875 | smp->data.uint = (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 2876 | smp->flags = 0; |
| 2877 | |
| 2878 | return 1; |
| 2879 | } |
| 2880 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2881 | /* parse the "ca-file" bind keyword */ |
| 2882 | 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] | 2883 | { |
| 2884 | if (!*args[cur_arg + 1]) { |
| 2885 | if (err) |
| 2886 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 2887 | return ERR_ALERT | ERR_FATAL; |
| 2888 | } |
| 2889 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 2890 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 2891 | memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 2892 | else |
| 2893 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 2894 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2895 | return 0; |
| 2896 | } |
| 2897 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2898 | /* parse the "ciphers" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2899 | 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] | 2900 | { |
| 2901 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2902 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2903 | return ERR_ALERT | ERR_FATAL; |
| 2904 | } |
| 2905 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 2906 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2907 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2908 | return 0; |
| 2909 | } |
| 2910 | |
| 2911 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2912 | 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] | 2913 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 2914 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2916 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2917 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2918 | return ERR_ALERT | ERR_FATAL; |
| 2919 | } |
| 2920 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 2921 | if ((*args[cur_arg + 1] != '/' ) && global.crt_base) { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 2922 | 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] | 2923 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 2924 | return ERR_ALERT | ERR_FATAL; |
| 2925 | } |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 2926 | 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] | 2927 | if (ssl_sock_load_cert(path, conf, px, err) > 0) |
| 2928 | return ERR_ALERT | ERR_FATAL; |
| 2929 | |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2933 | 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] | 2934 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2935 | |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2939 | /* parse the "crt-list" bind keyword */ |
| 2940 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2941 | { |
| 2942 | if (!*args[cur_arg + 1]) { |
| 2943 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 2944 | return ERR_ALERT | ERR_FATAL; |
| 2945 | } |
| 2946 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2947 | if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) { |
| 2948 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2949 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2950 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2951 | |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2955 | /* parse the "crl-file" bind keyword */ |
| 2956 | 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] | 2957 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2958 | #ifndef X509_V_FLAG_CRL_CHECK |
| 2959 | if (err) |
| 2960 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 2961 | return ERR_ALERT | ERR_FATAL; |
| 2962 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2963 | if (!*args[cur_arg + 1]) { |
| 2964 | if (err) |
| 2965 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 2966 | return ERR_ALERT | ERR_FATAL; |
| 2967 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2968 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 2969 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 2970 | memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 2971 | else |
| 2972 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 2973 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2974 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2975 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | /* parse the "ecdhe" bind keyword keywords */ |
| 2979 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2980 | { |
| 2981 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 2982 | if (err) |
| 2983 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 2984 | return ERR_ALERT | ERR_FATAL; |
| 2985 | #elif defined(OPENSSL_NO_ECDH) |
| 2986 | if (err) |
| 2987 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 2988 | return ERR_ALERT | ERR_FATAL; |
| 2989 | #else |
| 2990 | if (!*args[cur_arg + 1]) { |
| 2991 | if (err) |
| 2992 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 2993 | return ERR_ALERT | ERR_FATAL; |
| 2994 | } |
| 2995 | |
| 2996 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2997 | |
| 2998 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2999 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 3002 | /* parse the "crt_ignerr" and "ca_ignerr" bind keywords */ |
| 3003 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3004 | { |
| 3005 | int code; |
| 3006 | char *p = args[cur_arg + 1]; |
| 3007 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 3008 | |
| 3009 | if (!*p) { |
| 3010 | if (err) |
| 3011 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 3012 | return ERR_ALERT | ERR_FATAL; |
| 3013 | } |
| 3014 | |
| 3015 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 3016 | ignerr = &conf->ca_ignerr; |
| 3017 | |
| 3018 | if (strcmp(p, "all") == 0) { |
| 3019 | *ignerr = ~0ULL; |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | while (p) { |
| 3024 | code = atoi(p); |
| 3025 | if ((code <= 0) || (code > 63)) { |
| 3026 | if (err) |
| 3027 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 3028 | args[cur_arg], code, args[cur_arg + 1]); |
| 3029 | return ERR_ALERT | ERR_FATAL; |
| 3030 | } |
| 3031 | *ignerr |= 1ULL << code; |
| 3032 | p = strchr(p, ','); |
| 3033 | if (p) |
| 3034 | p++; |
| 3035 | } |
| 3036 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | /* parse the "force-sslv3" bind keyword */ |
| 3041 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3042 | { |
| 3043 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
| 3047 | /* parse the "force-tlsv10" bind keyword */ |
| 3048 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3049 | { |
| 3050 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3051 | return 0; |
| 3052 | } |
| 3053 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3054 | /* parse the "force-tlsv11" bind keyword */ |
| 3055 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3056 | { |
| 3057 | #if SSL_OP_NO_TLSv1_1 |
| 3058 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 3059 | return 0; |
| 3060 | #else |
| 3061 | if (err) |
| 3062 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 3063 | return ERR_ALERT | ERR_FATAL; |
| 3064 | #endif |
| 3065 | } |
| 3066 | |
| 3067 | /* parse the "force-tlsv12" bind keyword */ |
| 3068 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3069 | { |
| 3070 | #if SSL_OP_NO_TLSv1_2 |
| 3071 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 3072 | return 0; |
| 3073 | #else |
| 3074 | if (err) |
| 3075 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 3076 | return ERR_ALERT | ERR_FATAL; |
| 3077 | #endif |
| 3078 | } |
| 3079 | |
| 3080 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3081 | /* parse the "no-tls-tickets" bind keyword */ |
| 3082 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3083 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3084 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 3085 | return 0; |
| 3086 | } |
| 3087 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3088 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3089 | /* parse the "no-sslv3" bind keyword */ |
| 3090 | 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] | 3091 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3092 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3093 | return 0; |
| 3094 | } |
| 3095 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3096 | /* parse the "no-tlsv10" bind keyword */ |
| 3097 | 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] | 3098 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3099 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3100 | return 0; |
| 3101 | } |
| 3102 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3103 | /* parse the "no-tlsv11" bind keyword */ |
| 3104 | 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] | 3105 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3106 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3107 | return 0; |
| 3108 | } |
| 3109 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3110 | /* parse the "no-tlsv12" bind keyword */ |
| 3111 | 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] | 3112 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 3113 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3114 | return 0; |
| 3115 | } |
| 3116 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3117 | /* parse the "npn" bind keyword */ |
| 3118 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3119 | { |
| 3120 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 3121 | char *p1, *p2; |
| 3122 | |
| 3123 | if (!*args[cur_arg + 1]) { |
| 3124 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 3125 | return ERR_ALERT | ERR_FATAL; |
| 3126 | } |
| 3127 | |
| 3128 | free(conf->npn_str); |
| 3129 | |
| 3130 | /* the NPN string is built as a suite of (<len> <name>)* */ |
| 3131 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
| 3132 | conf->npn_str = calloc(1, conf->npn_len); |
| 3133 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 3134 | |
| 3135 | /* replace commas with the name length */ |
| 3136 | p1 = conf->npn_str; |
| 3137 | p2 = p1 + 1; |
| 3138 | while (1) { |
| 3139 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 3140 | if (!p2) |
| 3141 | p2 = p1 + 1 + strlen(p1 + 1); |
| 3142 | |
| 3143 | if (p2 - (p1 + 1) > 255) { |
| 3144 | *p2 = '\0'; |
| 3145 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 3146 | return ERR_ALERT | ERR_FATAL; |
| 3147 | } |
| 3148 | |
| 3149 | *p1 = p2 - (p1 + 1); |
| 3150 | p1 = p2; |
| 3151 | |
| 3152 | if (!*p2) |
| 3153 | break; |
| 3154 | |
| 3155 | *(p2++) = '\0'; |
| 3156 | } |
| 3157 | return 0; |
| 3158 | #else |
| 3159 | if (err) |
| 3160 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 3161 | return ERR_ALERT | ERR_FATAL; |
| 3162 | #endif |
| 3163 | } |
| 3164 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3165 | /* parse the "alpn" bind keyword */ |
| 3166 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3167 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3168 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3169 | char *p1, *p2; |
| 3170 | |
| 3171 | if (!*args[cur_arg + 1]) { |
| 3172 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 3173 | return ERR_ALERT | ERR_FATAL; |
| 3174 | } |
| 3175 | |
| 3176 | free(conf->alpn_str); |
| 3177 | |
| 3178 | /* the ALPN string is built as a suite of (<len> <name>)* */ |
| 3179 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
| 3180 | conf->alpn_str = calloc(1, conf->alpn_len); |
| 3181 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 3182 | |
| 3183 | /* replace commas with the name length */ |
| 3184 | p1 = conf->alpn_str; |
| 3185 | p2 = p1 + 1; |
| 3186 | while (1) { |
| 3187 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 3188 | if (!p2) |
| 3189 | p2 = p1 + 1 + strlen(p1 + 1); |
| 3190 | |
| 3191 | if (p2 - (p1 + 1) > 255) { |
| 3192 | *p2 = '\0'; |
| 3193 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 3194 | return ERR_ALERT | ERR_FATAL; |
| 3195 | } |
| 3196 | |
| 3197 | *p1 = p2 - (p1 + 1); |
| 3198 | p1 = p2; |
| 3199 | |
| 3200 | if (!*p2) |
| 3201 | break; |
| 3202 | |
| 3203 | *(p2++) = '\0'; |
| 3204 | } |
| 3205 | return 0; |
| 3206 | #else |
| 3207 | if (err) |
| 3208 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 3209 | return ERR_ALERT | ERR_FATAL; |
| 3210 | #endif |
| 3211 | } |
| 3212 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3213 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3214 | 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] | 3215 | { |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3216 | struct listener *l; |
| 3217 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 3218 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 3219 | |
| 3220 | if (global.listen_default_ciphers && !conf->ciphers) |
| 3221 | conf->ciphers = strdup(global.listen_default_ciphers); |
| 3222 | |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3223 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3224 | l->xprt = &ssl_sock; |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 3225 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3226 | return 0; |
| 3227 | } |
| 3228 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3229 | /* parse the "strict-sni" bind keyword */ |
| 3230 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3231 | { |
| 3232 | conf->strict_sni = 1; |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3236 | /* parse the "verify" bind keyword */ |
| 3237 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 3238 | { |
| 3239 | if (!*args[cur_arg + 1]) { |
| 3240 | if (err) |
| 3241 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 3242 | return ERR_ALERT | ERR_FATAL; |
| 3243 | } |
| 3244 | |
| 3245 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3246 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3247 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3248 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3249 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3250 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3251 | else { |
| 3252 | if (err) |
| 3253 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 3254 | args[cur_arg], args[cur_arg + 1]); |
| 3255 | return ERR_ALERT | ERR_FATAL; |
| 3256 | } |
| 3257 | |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3261 | /************** "server" keywords ****************/ |
| 3262 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3263 | /* parse the "ca-file" server keyword */ |
| 3264 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3265 | { |
| 3266 | if (!*args[*cur_arg + 1]) { |
| 3267 | if (err) |
| 3268 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 3269 | return ERR_ALERT | ERR_FATAL; |
| 3270 | } |
| 3271 | |
| 3272 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 3273 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3274 | else |
| 3275 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 3276 | |
| 3277 | return 0; |
| 3278 | } |
| 3279 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3280 | /* parse the "check-ssl" server keyword */ |
| 3281 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3282 | { |
| 3283 | newsrv->check.use_ssl = 1; |
| 3284 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 3285 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
| 3289 | /* parse the "ciphers" server keyword */ |
| 3290 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3291 | { |
| 3292 | if (!*args[*cur_arg + 1]) { |
| 3293 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 3294 | return ERR_ALERT | ERR_FATAL; |
| 3295 | } |
| 3296 | |
| 3297 | free(newsrv->ssl_ctx.ciphers); |
| 3298 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3302 | /* parse the "crl-file" server keyword */ |
| 3303 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3304 | { |
| 3305 | #ifndef X509_V_FLAG_CRL_CHECK |
| 3306 | if (err) |
| 3307 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 3308 | return ERR_ALERT | ERR_FATAL; |
| 3309 | #else |
| 3310 | if (!*args[*cur_arg + 1]) { |
| 3311 | if (err) |
| 3312 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 3313 | return ERR_ALERT | ERR_FATAL; |
| 3314 | } |
| 3315 | |
| 3316 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 3317 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3318 | else |
| 3319 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 3320 | |
| 3321 | return 0; |
| 3322 | #endif |
| 3323 | } |
| 3324 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3325 | /* parse the "crt" server keyword */ |
| 3326 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3327 | { |
| 3328 | if (!*args[*cur_arg + 1]) { |
| 3329 | if (err) |
| 3330 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 3331 | return ERR_ALERT | ERR_FATAL; |
| 3332 | } |
| 3333 | |
| 3334 | if ((*args[*cur_arg + 1] != '/') && global.crt_base) |
| 3335 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 3336 | else |
| 3337 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 3338 | |
| 3339 | return 0; |
| 3340 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3341 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3342 | /* parse the "force-sslv3" server keyword */ |
| 3343 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3344 | { |
| 3345 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 3346 | return 0; |
| 3347 | } |
| 3348 | |
| 3349 | /* parse the "force-tlsv10" server keyword */ |
| 3350 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3351 | { |
| 3352 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 3353 | return 0; |
| 3354 | } |
| 3355 | |
| 3356 | /* parse the "force-tlsv11" server keyword */ |
| 3357 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3358 | { |
| 3359 | #if SSL_OP_NO_TLSv1_1 |
| 3360 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 3361 | return 0; |
| 3362 | #else |
| 3363 | if (err) |
| 3364 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 3365 | return ERR_ALERT | ERR_FATAL; |
| 3366 | #endif |
| 3367 | } |
| 3368 | |
| 3369 | /* parse the "force-tlsv12" server keyword */ |
| 3370 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3371 | { |
| 3372 | #if SSL_OP_NO_TLSv1_2 |
| 3373 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 3374 | return 0; |
| 3375 | #else |
| 3376 | if (err) |
| 3377 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 3378 | return ERR_ALERT | ERR_FATAL; |
| 3379 | #endif |
| 3380 | } |
| 3381 | |
| 3382 | /* parse the "no-sslv3" server keyword */ |
| 3383 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3384 | { |
| 3385 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 3386 | return 0; |
| 3387 | } |
| 3388 | |
| 3389 | /* parse the "no-tlsv10" server keyword */ |
| 3390 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3391 | { |
| 3392 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | /* parse the "no-tlsv11" server keyword */ |
| 3397 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3398 | { |
| 3399 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | /* parse the "no-tlsv12" server keyword */ |
| 3404 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3405 | { |
| 3406 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3410 | /* parse the "no-tls-tickets" server keyword */ |
| 3411 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3412 | { |
| 3413 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 3414 | return 0; |
| 3415 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3416 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 3417 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3418 | { |
| 3419 | newsrv->pp_opts |= SRV_PP_V2; |
| 3420 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 3421 | return 0; |
| 3422 | } |
| 3423 | |
| 3424 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 3425 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3426 | { |
| 3427 | newsrv->pp_opts |= SRV_PP_V2; |
| 3428 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 3429 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 3430 | return 0; |
| 3431 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3432 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3433 | /* parse the "ssl" server keyword */ |
| 3434 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3435 | { |
| 3436 | newsrv->use_ssl = 1; |
| 3437 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 3438 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 3439 | return 0; |
| 3440 | } |
| 3441 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3442 | /* parse the "verify" server keyword */ |
| 3443 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3444 | { |
| 3445 | if (!*args[*cur_arg + 1]) { |
| 3446 | if (err) |
| 3447 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 3448 | return ERR_ALERT | ERR_FATAL; |
| 3449 | } |
| 3450 | |
| 3451 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3452 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3453 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3454 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3455 | else { |
| 3456 | if (err) |
| 3457 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 3458 | args[*cur_arg], args[*cur_arg + 1]); |
| 3459 | return ERR_ALERT | ERR_FATAL; |
| 3460 | } |
| 3461 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3462 | return 0; |
| 3463 | } |
| 3464 | |
| 3465 | /* parse the "verifyhost" server keyword */ |
| 3466 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 3467 | { |
| 3468 | if (!*args[*cur_arg + 1]) { |
| 3469 | if (err) |
| 3470 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 3471 | return ERR_ALERT | ERR_FATAL; |
| 3472 | } |
| 3473 | |
| 3474 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 3475 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3476 | return 0; |
| 3477 | } |
| 3478 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3479 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3480 | * Please take care of keeping this list alphabetically sorted. |
| 3481 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3482 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3483 | { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
| 3484 | { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5SRV }, |
| 3485 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 3486 | { "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] | 3487 | { "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] | 3488 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5SRV }, |
| 3489 | { "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] | 3490 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3491 | { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3492 | { "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] | 3493 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3494 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3495 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3496 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3497 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3498 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3499 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 3500 | { "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] | 3501 | { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3502 | { "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] | 3503 | { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 3504 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3505 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3506 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3507 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3508 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3509 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 3510 | { "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] | 3511 | { "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] | 3512 | { "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] | 3513 | { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3514 | { "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] | 3515 | { "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] | 3516 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 3517 | { "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] | 3518 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3519 | { "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] | 3520 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3521 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3522 | { "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] | 3523 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3524 | { "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] | 3525 | { "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] | 3526 | { "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] | 3527 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 3528 | { "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] | 3529 | { NULL, NULL, 0, 0, 0 }, |
| 3530 | }}; |
| 3531 | |
| 3532 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3533 | * Please take care of keeping this list alphabetically sorted. |
| 3534 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3535 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 3536 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 3537 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 3538 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3539 | }}; |
| 3540 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3541 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3542 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 3543 | * all code contributors. |
| 3544 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 3545 | * the config parser can report an appropriate error when a known keyword was |
| 3546 | * not enabled. |
| 3547 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 3548 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3549 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3550 | { "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] | 3551 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 3552 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 3553 | { "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] | 3554 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 3555 | { "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] | 3556 | { "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] | 3557 | { "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] | 3558 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 3559 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 3560 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 3561 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 3562 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 3563 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 3564 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 3565 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3566 | { "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] | 3567 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3568 | { "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] | 3569 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3570 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3571 | { NULL, NULL, 0 }, |
| 3572 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3573 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3574 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3575 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 3576 | * all code contributors. |
| 3577 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 3578 | * the config parser can report an appropriate error when a known keyword was |
| 3579 | * not enabled. |
| 3580 | */ |
| 3581 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3582 | { "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] | 3583 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 3584 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3585 | { "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] | 3586 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 3587 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 3588 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 3589 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 3590 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
| 3591 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 3592 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 3593 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 3594 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3595 | { "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] | 3596 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 3597 | { "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] | 3598 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3599 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3600 | { "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] | 3601 | { NULL, NULL, 0, 0 }, |
| 3602 | }}; |
| 3603 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3604 | /* transport-layer operations for SSL sockets */ |
| 3605 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3606 | .snd_buf = ssl_sock_from_buf, |
| 3607 | .rcv_buf = ssl_sock_to_buf, |
| 3608 | .rcv_pipe = NULL, |
| 3609 | .snd_pipe = NULL, |
| 3610 | .shutr = NULL, |
| 3611 | .shutw = ssl_sock_shutw, |
| 3612 | .close = ssl_sock_close, |
| 3613 | .init = ssl_sock_init, |
| 3614 | }; |
| 3615 | |
| 3616 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3617 | static void __ssl_sock_init(void) |
| 3618 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3619 | STACK_OF(SSL_COMP)* cm; |
| 3620 | |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 3621 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 3622 | global.listen_default_ciphers = LISTEN_DEFAULT_CIPHERS; |
| 3623 | #endif |
| 3624 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 3625 | global.connect_default_ciphers = CONNECT_DEFAULT_CIPHERS; |
| 3626 | #endif |
| 3627 | if (global.listen_default_ciphers) |
| 3628 | global.listen_default_ciphers = strdup(global.listen_default_ciphers); |
| 3629 | if (global.connect_default_ciphers) |
| 3630 | global.connect_default_ciphers = strdup(global.connect_default_ciphers); |
| 3631 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3632 | SSL_library_init(); |
| 3633 | cm = SSL_COMP_get_compression_methods(); |
| 3634 | sk_SSL_COMP_zero(cm); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3635 | sample_register_fetches(&sample_fetch_keywords); |
| 3636 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 3637 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 3638 | srv_register_keywords(&srv_kws); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * Local variables: |
| 3643 | * c-indent-level: 8 |
| 3644 | * c-basic-offset: 8 |
| 3645 | * End: |
| 3646 | */ |