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