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