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