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