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