summaryrefslogtreecommitdiff
path: root/ext/openssl
diff options
context:
space:
mode:
authorDaniel Lowrey <rdlowrey@php.net>2014-03-05 10:50:23 -0500
committerDaniel Lowrey <rdlowrey@php.net>2014-03-05 10:03:33 -0700
commitfad14e3180b17127496c95fe7f0a4b1ca4f36196 (patch)
tree563d06c3eb14ecece42b3fa0915a2288ae2ad5fa /ext/openssl
parent020e161966db1603f28df9baa33141ae59067243 (diff)
downloadphp-git-fad14e3180b17127496c95fe7f0a4b1ca4f36196.tar.gz
Add encrypted server SNI support
- New "SNI_server_certs" context option maps host names to appropriate certs should client handshakes advertise the SNI extension: $ctx = stream_context_create(["ssl" => [ "local_cert" => "/path/to/cert.pem", "SNI_server_certs" => [ "domain1.com" => "/path/to/domain1.pem", "*.domain2.com" => "/path/to/domain2.pem", "domain3.com" => "/path/to/domain3.pem" ] ]]); - Prefixing a "*." will utilize the matching cert if a client requests the primary host name or any subdomain thereof. So in the above example our "domain2.pem" will be used for both requests to "domain2.com" -and- "subdomain.domain2.com" - The "SNI_server_certs" ctx option has no effect for client streams. - SNI support is enabled by default as of 5.6 for both servers and clients. Servers must specify the "SNI_server_certs" array to actually use the SNI extension, though. - If the `"SNI_enabled" => false` ctx option is also passed then "SNI_server_certs" has no effect. - While supporting SNI by itself is enough to successfully negotiate the TLS handshake with many clients, servers MUST still specify a "local_cert" ctx option or run the risk of connection failures from clients that do not support the SNI extension.
Diffstat (limited to 'ext/openssl')
-rw-r--r--ext/openssl/tests/sni_server.phpt60
-rw-r--r--ext/openssl/tests/sni_server_ca.pem63
-rw-r--r--ext/openssl/tests/sni_server_domain1.pem82
-rw-r--r--ext/openssl/tests/sni_server_domain2.pem82
-rw-r--r--ext/openssl/tests/sni_server_domain3.pem82
-rw-r--r--ext/openssl/xp_ssl.c147
6 files changed, 516 insertions, 0 deletions
diff --git a/ext/openssl/tests/sni_server.phpt b/ext/openssl/tests/sni_server.phpt
new file mode 100644
index 0000000000..d44a69f549
--- /dev/null
+++ b/ext/openssl/tests/sni_server.phpt
@@ -0,0 +1,60 @@
+--TEST--
+sni_server
+--SKIPIF--
+<?php
+if (!extension_loaded("openssl")) die("skip openssl not loaded");
+--FILE--
+<?php
+$serverCode = <<<'CODE'
+ $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
+ $ctx = stream_context_create(['ssl' => [
+ 'local_cert' => __DIR__ . '/domain1.pem',
+ 'SNI_server_certs' => [
+ "domain1.com" => __DIR__ . "/sni_server_domain1.pem",
+ "domain2.com" => __DIR__ . "/sni_server_domain2.pem",
+ "domain3.com" => __DIR__ . "/sni_server_domain3.pem"
+ ]
+ ]]);
+
+ $server = stream_socket_server('tls://127.0.0.1:64321', $errno, $errstr, $flags, $ctx);
+ phpt_notify();
+
+ for ($i=0; $i < 3; $i++) {
+ @stream_socket_accept($server, 3);
+ }
+CODE;
+
+$clientCode = <<<'CODE'
+ $flags = STREAM_CLIENT_CONNECT;
+ $ctxArr = [
+ 'cafile' => __DIR__ . '/sni_server_ca.pem',
+ 'capture_peer_cert' => true
+ ];
+
+ phpt_wait();
+
+ $ctxArr['peer_name'] = 'domain1.com';
+ $ctx = stream_context_create(['ssl' => $ctxArr]);
+ $client = stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
+ $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
+ var_dump(openssl_x509_parse($cert)['subject']['CN']);
+
+ $ctxArr['peer_name'] = 'domain2.com';
+ $ctx = stream_context_create(['ssl' => $ctxArr]);
+ $client = @stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
+ $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
+ var_dump(openssl_x509_parse($cert)['subject']['CN']);
+
+ $ctxArr['peer_name'] = 'domain3.com';
+ $ctx = stream_context_create(['ssl' => $ctxArr]);
+ $client = @stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
+ $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
+ var_dump(openssl_x509_parse($cert)['subject']['CN']);
+CODE;
+
+include 'ServerClientTestCase.inc';
+ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
+--EXPECTF--
+string(%d) "domain1.com"
+string(%d) "domain2.com"
+string(%d) "domain3.com"
diff --git a/ext/openssl/tests/sni_server_ca.pem b/ext/openssl/tests/sni_server_ca.pem
new file mode 100644
index 0000000000..f840802c29
--- /dev/null
+++ b/ext/openssl/tests/sni_server_ca.pem
@@ -0,0 +1,63 @@
+-----BEGIN CERTIFICATE-----
+MIIFPjCCAyYCAQEwDQYJKoZIhvcNAQEFBQAwWTELMAkGA1UEBhMCVVMxCzAJBgNV
+BAgMAlNDMRUwEwYDVQQHDAxNeXJ0bGUgQmVhY2gxEjAQBgNVBAoMCXBocC50ZXN0
+czESMBAGA1UEAwwJcGhwLnRlc3RzMB4XDTE0MDMwNTE0MTg1M1oXDTI0MDMwMjE0
+MTg1M1owcTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlNDMRUwEwYDVQQHDAxNeXJ0
+bGUgQmVhY2gxHjAcBgNVBAoMFXBocC50ZXN0cyBzdWJvcmRpbmF0ZTEeMBwGA1UE
+AwwVcGhwLnRlc3RzLnN1Ym9yZGluYXRlMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A
+MIICCgKCAgEA4rmcz5M0B2Td+0g8+pmGT0IBCxZTVL14xKA1yLeY+6eshdNiRAIR
+8m5sI21AirtAh7mJziA2Q4C/1bRvY9VI/0yxXRP20aicNmhEvFVAvQCxmAkg8hIm
+WwDiNLw+4HpAkQInpd8exjExhvanUgAz6tq8w6CL2BqquiwQPVIf5HHMHjomz1gz
+iZEs6CRE5YMdUd7ZSx+MHl6ww3WYwW/XHDPzAUIe8uhOT+yA8lVLFAac3+lyjA6g
+zMPYG5SrqE6/+yZYDVx2WhVMjJq8d+N6hnIkhZeJyZo2T0G7/pSGIwaVsceIJ/8/
+6km8p4Dn2Pq75F8RZW5iz0MnSqKqSH/AN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FzpRqkUHbOhDZTEUlzYyvtn84t7rbC5g2u
+y+KFo3/S4qJheyRfl5FANFTUlxv4vMvMnuaHcJxvAgMBAAGjUDBOMB0GA1UdDgQW
+BBRQ95jPSVj20YO2Eq2+wnknOpCjqjAfBgNVHSMEGDAWgBRQ95jPSVj20YO2Eq2+
+wnknOpCjqjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4ICAQCaYxaA9H8O
+J55kLRzzgLV56yc+MhowAqOAOjD4J20aWbkRQIwUMIi6xnCwRRnwjuscPFOSF6P+
+Bn3kdVX+KhzFLNRhAk1qjEGjwSVLPKJSKJSdwOmrvCkSsIcbASxSXQkz714LkFAL
+hZ9cs4QD+/JVEPazgqjBwtlu3hwwuGAxwHNAKWeFaXpOJu9UerwfBmF090L0uBND
+QJAqDyCZRaQp4Re6qH3iQhxyERlAwCNIRSV2cWHJP0HmWye76Z2aehp96fKTo5Cd
+NXW3GKGSpmZeZgY8MZVyzUau1c83+nHJCYO0jhFyZjO4XMJ+cce0a2H8iIhox5fY
+ZBEuxYN6cPkdvDbFRQU+e+KO1jjyumNWentW57DDPihpgZ3f3Gf5xtUjnHTC5VKT
+chW9ujq18Hk4JWL5uyF+5am5Qm4YdjhhZ1TtGHNjoZGpHV0Tf4h5AcDG4Zr6TCmM
+3Rw952ytcMtZKZzBq2ZWC3lKpBPPzihAgwzgflVvi0BLP9Ek98oMxq22pymHEHY8
+ivfm+t4rRY6JMl7DxDcARvWoKujrJy2JUUm50vT5D2GG8xYwYr9XKgD8rP36qCjC
+B+1l+upz+37r9U12uWfjPsRjyphGbN8ZZAkjSSZQU2snogxaKOvWZb+2M/rajdek
+BSQ9sUZ6sNXnTkyYon6iH6WULvmSTgBGnQ==
+-----END CERTIFICATE-----
diff --git a/ext/openssl/tests/sni_server_domain1.pem b/ext/openssl/tests/sni_server_domain1.pem
new file mode 100644
index 0000000000..f00857bda8
--- /dev/null
+++ b/ext/openssl/tests/sni_server_domain1.pem
@@ -0,0 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BK3plPVZt7qIAMAQVpNmExLiLjW5vEwB6n049T5MV0ROmEy/dO3x
+WTHMP/t5zmBEakuBVeTrzaqDSC1sZdHQWGga7UcITBGGRQyuYw3NTraJaRdO7OOS
+cG2pAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAJ3pMaMcWaW5TJ1QV58npS/F7Ao7
+mciFMQ+KiQ6E5TJ/S37EfR/r5+mqQodM6Zvs07x4k2HdwcuI0cdpddLKfIH2znDW
+pJY1nEKI9W/pz9pEj3TBu9M3QAPzWVwYyBaJzVJGIll1yYVQVNGneHF8YpWQo+Ow
+yg97x1220oUSAlJ9zUwwls6QjJCo95PcClV9ZK6A+H17v+kwzBco7DwGhoxy3s+n
+D9+LjDa5Z6yk7Y1h+I4kIExenjuXT1wgQatruR8MBl8MX+KKeAi4neRdCBiDceHs
+zzO0f+IBNiF1J8THokV81i7DMbsHRGv28r3rcQpalDK9/rcBRVXUl0Kw8ZDQJLaS
+C1EuAcqw8TgvZOAhw431EpZW7Beek4xvrwTSzrF1XhlbD/1Jm8YAjGQyO138qzq2
+p40NRXgGL3rT6LEDcUVjL9D2z/7Tu7G9g17vleEzBC13ILnAOg/UFsZ4BuGNCnxa
+3PiJ7QaDBL0UA4uUuXjVgGTFzeDaTJbNCybcTKI7rD4zAa/4d05364gA3S1XJU66
+tH24Z3ncMBrh1GrsX4z6vLkNyb7m2wBqWBuyuVidm7rPcMGoggbrAsIP5vPjIMdg
+GR+ghPgzhJsbEg/WoSxv8+oljAJT2I1A53ulJaL4w6S/Ku1yEQ01Xk4vZEBJHAAw
+TJxrq625wwS0cKQd
+-----END CERTIFICATE-----
diff --git a/ext/openssl/tests/sni_server_domain2.pem b/ext/openssl/tests/sni_server_domain2.pem
new file mode 100644
index 0000000000..4acc355352
--- /dev/null
+++ b/ext/openssl/tests/sni_server_domain2.pem
@@ -0,0 +1,82 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIJKgIBAAKCAgEA52uBJ1tv4osh3GUqdiBS01Fj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iwljkwb
+29Itigeo7TMgVBeadN3biZGYSgIeYvf/MjOjxMKt4bCcoKNsieIsCNHj9+9lHbIr
+UrpDAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAAzy1BUPLBK3+230sILn9iJV+7DJ
+1pFOTBdChTn8UrCzP8NrhQ7TNyNuwETcjxOMZ0IYc9SBUZgQV2RZrGM4Ek2dV2so
+Z+HzQ9UsAl31t8bL1uSBH4lspAeSAIq8HyLK52JxZ4yBK8ID2e7oHzbqY58Xcfzn
+4WlA54XYNI1+gj6bBTP7lXLz40H9lcPTHDsed3usYYWtfH/ncIW0rw9/fK1P6aTO
+680lOaLFB26Z3ygGiJXbfmcnIjEmbpgWLfcPOoIBFrDmkiHqo31UT1WBxQ16c4yw
+DwO+DY5KK6adI1j5PWUmT/8vwlsO2CEtq4DOKfB51ggG9NyciCw+vJXP4Ec30IiX
+5TNWNwpg+ex44ICScSF6ew4hS2tO59VFBvDaa/2nFhEOG0iECN0537v10vOxgaVr
+kHlCgJtgCwUbKrfVTA2FogXRDIBJ1aOZiA7kIPPSqdTH49HwieH55Q5kfi7xak5y
+tSijrFw70vZOf3ORErmXEAZFqQgPuK4KlvTMom7z9QnNq4L579DeGUdBhLYGXJzP
+8WcMXRlQ7XUT6TQaSTDwDYlWcxcZ584seK4rVCLT2jyChEWSLR2K29hZ0eNVQt43
+3Xxz9hExGS/oELv53zPqp10w/0ZKlFQc5Wa4kEkjBz5R8VzzWuelxarqNAH6m9Du
+i3jyxtHS869PHMU3
+-----END CERTIFICATE-----
diff --git a/ext/openssl/tests/sni_server_domain3.pem b/ext/openssl/tests/sni_server_domain3.pem
new file mode 100644
index 0000000000..9f80b717a2
--- /dev/null
+++ b/ext/openssl/tests/sni_server_domain3.pem
@@ -0,0 +1,82 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIJKAIBAAKCAgEAruR9tBqYBDvB1DfMEhcFScRbKELR+ALF4a0EG/ferOfFyc4H
+gWS7pcTuzqtpqGNIZxQkR+yc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qU59FYfhW8jHI1v
+EY5BD8etxaT34QIDAQABMA0GCSqGSIb3DQEBBQUAA4ICAQDUel0pS4vCng3sQ2ab
+U4bSTDiiyR09TUlX54eaHrQFnnJAXxnHdu9S2xlizaGgkUHqQ7P6HXiMn5rl2GTw
+7uLcV2CoapceuX3HK+Iiy5r4phXtylmkUT737jR32Rib/jH7swkdPLDcnbI2J2Cs
+I1LIlCiuaJagu0q2liRnJOkdZQd1Rz3w9I/WHECxS9SAnaQsF4LbXabXObVeRrtq
+qiNoDbC0Q9c4RquVtbdjm4vP6eCjnqck/0Tq8ceq7Hg5hu3Q0scg3mEK+7cyp19X
+Y+/nCg8SZe+7LxewJ7GgqRgnDiHgfO5Nu4jhuZh72LA1mtIS+dU/cDVf8kxLiusG
+UlJKDwJ14Jh7LAKdRorqGFqNi8R5zEaCIcuSWvUfNl9TzOXlRj6c4lP8eixWG4FD
+CiHkv7jXsz8AYZcKy7IqsXVdnem+8PoKmBzhtngNX86W9JgQ65vLwsT4kdWHMfU0
+zEZrHZ3+qcmL5GUXI7r3uQQmp0RcNQV57X6bLRG4PaCMxuTv6JJ3Bi+qqX4vVWPL
+K1Au3W5UiJ3XPVllxSynA6lcUvVw/PoNLaV21sskxwJDtLwbSAH3DS0JQQSZzesj
+hGayf3SOVU1n84lTuc84KEgM+zaxGzqfShUadlLMKBS+w+8j8N+qBjUi08UOUKa7
+OAES8tCv7DK0Mi21rLXMadYwlw==
+-----END CERTIFICATE-----
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
index ad3d92d0dc..e251d2124a 100644
--- a/ext/openssl/xp_ssl.c
+++ b/ext/openssl/xp_ssl.c
@@ -77,6 +77,12 @@ extern int php_openssl_get_x509_list_id(void);
php_stream_ops php_openssl_socket_ops;
+/* Certificate contexts used for server-side SNI selection */
+typedef struct _php_openssl_sni_cert_t {
+ char *name;
+ SSL_CTX *ctx;
+} php_openssl_sni_cert_t;
+
/* Provides leaky bucket handhsake renegotiation rate-limiting */
typedef struct _php_openssl_handshake_bucket_t {
long prev_handshake;
@@ -1168,6 +1174,131 @@ static int set_server_specific_opts(php_stream *stream, SSL_CTX *ctx TSRMLS_DC)
/* }}} */
#ifdef HAVE_SNI
+static int server_sni_callback(SSL *ssl_handle, int *al, void *arg) /* {{{ */
+{
+ php_stream *stream;
+ php_openssl_netstream_data_t *sslsock;
+ unsigned i;
+ const char *server_name;
+
+ server_name = SSL_get_servername(ssl_handle, TLSEXT_NAMETYPE_host_name);
+
+ if (!server_name) {
+ return SSL_TLSEXT_ERR_NOACK;
+ }
+
+ stream = (php_stream*)SSL_get_ex_data(ssl_handle, php_openssl_get_ssl_stream_data_index());
+ sslsock = (php_openssl_netstream_data_t*)stream->abstract;
+
+ if (!(sslsock->sni_cert_count && sslsock->sni_certs)) {
+ return SSL_TLSEXT_ERR_NOACK;
+ }
+
+ for (i=0; i < sslsock->sni_cert_count; i++) {
+ if (matches_wildcard_name(server_name, sslsock->sni_certs[i].name)) {
+ SSL_set_SSL_CTX(ssl_handle, sslsock->sni_certs[i].ctx);
+ return SSL_TLSEXT_ERR_OK;
+ }
+ }
+
+ return SSL_TLSEXT_ERR_NOACK;
+}
+/* }}} */
+
+static int enable_server_sni(php_stream *stream, php_openssl_netstream_data_t *sslsock TSRMLS_DC)
+{
+ zval **val;
+ zval **current;
+ char *key;
+ uint key_len;
+ ulong key_index;
+ int key_type;
+ HashPosition pos;
+ int i = 0;
+ char resolved_path_buff[MAXPATHLEN];
+ SSL_CTX *ctx;
+
+ /* If the stream ctx disables SNI we're finished here */
+ if (GET_VER_OPT("SNI_enabled") && !zend_is_true(*val)) {
+ return SUCCESS;
+ }
+
+ /* If no SNI cert array is specified we're finished here */
+ if (!GET_VER_OPT("SNI_server_certs")) {
+ return SUCCESS;
+ }
+
+ if (Z_TYPE_PP(val) != IS_ARRAY) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "SNI_server_certs requires an array mapping host names to cert paths"
+ );
+ return FAILURE;
+ }
+
+ sslsock->sni_cert_count = zend_hash_num_elements(Z_ARRVAL_PP(val));
+ if (sslsock->sni_cert_count == 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "SNI_server_certs host cert array must not be empty"
+ );
+ return FAILURE;
+ }
+
+ sslsock->sni_certs = (php_openssl_sni_cert_t*)safe_pemalloc(sslsock->sni_cert_count,
+ sizeof(php_openssl_sni_cert_t), 0, php_stream_is_persistent(stream)
+ );
+
+ for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(val), &pos);
+ zend_hash_get_current_data_ex(Z_ARRVAL_PP(val), (void **)&current, &pos) == SUCCESS;
+ zend_hash_move_forward_ex(Z_ARRVAL_PP(val), &pos)
+ ) {
+ key_type = zend_hash_get_current_key_ex(Z_ARRVAL_PP(val), &key, &key_len, &key_index, 0, &pos);
+ if (key_type != HASH_KEY_IS_STRING) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "SNI_server_certs array requires string host name keys"
+ );
+ return FAILURE;
+ }
+
+ if (VCWD_REALPATH(Z_STRVAL_PP(current), resolved_path_buff)) {
+ /* The hello method is not inherited by SSL structs when assigning a new context
+ * inside the SNI callback, so the just use SSLv23 */
+ ctx = SSL_CTX_new(SSLv23_server_method());
+
+ if (SSL_CTX_use_certificate_chain_file(ctx, resolved_path_buff) != 1) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "failed setting local cert chain file `%s'; " \
+ "check that your cafile/capath settings include " \
+ "details of your certificate and its issuer",
+ resolved_path_buff
+ );
+ SSL_CTX_free(ctx);
+ return FAILURE;
+ } else if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "failed setting private key from file `%s'",
+ resolved_path_buff
+ );
+ SSL_CTX_free(ctx);
+ return FAILURE;
+ } else {
+ sslsock->sni_certs[i].name = pestrdup(key, php_stream_is_persistent(stream));
+ sslsock->sni_certs[i].ctx = ctx;
+ ++i;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "failed setting local cert chain file `%s'; file not found",
+ Z_STRVAL_PP(current)
+ );
+ return FAILURE;
+ }
+ }
+
+ SSL_CTX_set_tlsext_servername_callback(sslsock->ctx, server_sni_callback);
+
+ return SUCCESS;
+}
+
static void enable_client_sni(php_stream *stream, php_openssl_netstream_data_t *sslsock) /* {{{ */
{
zval **val;
@@ -1311,6 +1442,13 @@ int php_openssl_setup_crypto(php_stream *stream,
handle_ssl_error(stream, 0, 1 TSRMLS_CC);
}
+#ifdef HAVE_SNI
+ /* Enable server-side SNI */
+ if (sslsock->is_client == 0 && enable_server_sni(stream, sslsock TSRMLS_CC) == FAILURE) {
+ return FAILURE;
+ }
+#endif
+
/* Enable server-side handshake renegotiation rate-limiting */
if (sslsock->is_client == 0) {
init_server_reneg_limit(stream, sslsock);
@@ -1687,6 +1825,15 @@ static int php_openssl_sockop_close(php_stream *stream, int close_handle TSRMLS_
}
}
+ if (sslsock->sni_certs) {
+ for (i=0; i<sslsock->sni_cert_count; i++) {
+ SSL_CTX_free(sslsock->sni_certs[i].ctx);
+ pefree(sslsock->sni_certs[i].name, php_stream_is_persistent(stream));
+ }
+ pefree(sslsock->sni_certs, php_stream_is_persistent(stream));
+ sslsock->sni_certs = NULL;
+ }
+
if (sslsock->url_name) {
pefree(sslsock->url_name, php_stream_is_persistent(stream));
}