Skip to content

Commit 5de64a2

Browse files
committed
Merge branch 'PHP-5.6'
* PHP-5.6: Fixed bug #68920 (use strict peer_fingerprint input checks) Conflicts: ext/openssl/xp_ssl.c
2 parents 01ccfb7 + 241f3c3 commit 5de64a2

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
. Fixed bug #68912 (Segmentation fault at openssl_spki_new). (Laruence)
3939
. Fixed bug #61285, #68329, #68046, #41631 (encrypted streams don't observe
4040
socket timeouts). (Brad Broerman)
41+
. Fixed bug #68920 (use strict peer_fingerprint input checks)
42+
(Daniel Lowrey)
4143

4244
- pgsql:
4345
. Fixed bug #68638 (pg_update() fails to store infinite values).

ext/openssl/tests/bug68920.phpt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
Bug #68920: peer_fingerprint input checks should be strict
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("openssl")) die("skip openssl not loaded");
6+
--FILE--
7+
<?php
8+
error_reporting(E_ALL);
9+
10+
$ctx = stream_context_create(['ssl' => ['verify_peer'=> false, 'peer_fingerprint' => true]]);
11+
$sock = stream_socket_client("ssl://php.net:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
12+
var_dump($sock);
13+
14+
$ctx = stream_context_create(['ssl' => ['verify_peer'=> false, 'peer_fingerprint' => null]]);
15+
$sock = stream_socket_client("ssl://php.net:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
16+
var_dump($sock);
17+
18+
$ctx = stream_context_create(['ssl' => ['verify_peer'=> false, 'peer_fingerprint' => []]]);
19+
$sock = stream_socket_client("ssl://php.net:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
20+
var_dump($sock);
21+
22+
$ctx = stream_context_create(['ssl' => ['verify_peer'=> false, 'peer_fingerprint' => ['foo']]]);
23+
$sock = stream_socket_client("ssl://php.net:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
24+
var_dump($sock);
25+
--EXPECTF--
26+
27+
Warning: stream_socket_client(): Expected peer fingerprint must be a string or an array in %s on line %d
28+
29+
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
30+
31+
Warning: stream_socket_client(): unable to connect to %s (Unknown error) in %s on line %d
32+
bool(false)
33+
34+
Warning: stream_socket_client(): Expected peer fingerprint must be a string or an array in %s on line %d
35+
36+
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
37+
38+
Warning: stream_socket_client(): unable to connect to %s (Unknown error) in %s on line %d
39+
bool(false)
40+
41+
Warning: stream_socket_client(): Invalid peer_fingerprint array; [algo => fingerprint] form required in %s on line %d
42+
43+
Warning: stream_socket_client(): peer_fingerprint match failure in %s on line %d
44+
45+
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
46+
47+
Warning: stream_socket_client(): unable to connect to %s (Unknown error) in %s on line %d
48+
bool(false)
49+
50+
Warning: stream_socket_client(): Invalid peer_fingerprint array; [algo => fingerprint] form required in %s on line %d
51+
52+
Warning: stream_socket_client(): peer_fingerprint match failure in %s on line %d
53+
54+
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
55+
56+
Warning: stream_socket_client(): unable to connect to %s (Unknown error) in %s on line %d
57+
bool(false)

ext/openssl/tests/openssl_peer_fingerprint.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CODE;
4545
include 'ServerClientTestCase.inc';
4646
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
4747
--EXPECTF--
48-
Warning: stream_socket_client(): Peer fingerprint doesn't match in %s on line %d
48+
Warning: stream_socket_client(): peer_fingerprint match failure in %s on line %d
4949

5050
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
5151

ext/openssl/xp_ssl.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,26 @@ static zend_bool php_x509_fingerprint_match(X509 *peer, zval *val)
299299
zval *current;
300300
zend_string *key;
301301

302+
if (!zend_hash_num_elements(Z_ARRVAL_P(val))) {
303+
php_error_docref(NULL, E_WARNING, "Invalid peer_fingerprint array; [algo => fingerprint] form required");
304+
return 0;
305+
}
306+
302307
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(val), key, current) {
303-
if (key && Z_TYPE_P(current) == IS_STRING
304-
&& php_x509_fingerprint_cmp(peer, key->val, Z_STRVAL_P(current)) != 0
305-
) {
308+
if (key == NULL || Z_TYPE_P(current) != IS_STRING) {
309+
php_error_docref(NULL, E_WARNING, "Invalid peer_fingerprint array; [algo => fingerprint] form required");
310+
return 0;
311+
}
312+
if (php_x509_fingerprint_cmp(peer, key->val, Z_STRVAL_P(current)) != 0) {
306313
return 0;
307314
}
308315
} ZEND_HASH_FOREACH_END();
316+
309317
return 1;
318+
} else {
319+
php_error_docref(NULL, E_WARNING, "Invalid peer_fingerprint value; fingerprint string or array of the form [algo => fingerprint] required");
310320
}
321+
311322
return 0;
312323
}
313324

@@ -428,7 +439,7 @@ static int apply_peer_verification_policy(SSL *ssl, X509 *peer, php_stream *stre
428439
? zend_is_true(val)
429440
: sslsock->is_client;
430441

431-
must_verify_fingerprint = (GET_VER_OPT("peer_fingerprint") && zend_is_true(val));
442+
must_verify_fingerprint = GET_VER_OPT("peer_fingerprint");
432443

433444
if ((must_verify_peer || must_verify_peer_name || must_verify_fingerprint) && peer == NULL) {
434445
php_error_docref(NULL, E_WARNING, "Could not get peer certificate");
@@ -463,14 +474,15 @@ static int apply_peer_verification_policy(SSL *ssl, X509 *peer, php_stream *stre
463474
if (Z_TYPE_P(val) == IS_STRING || Z_TYPE_P(val) == IS_ARRAY) {
464475
if (!php_x509_fingerprint_match(peer, val)) {
465476
php_error_docref(NULL, E_WARNING,
466-
"Peer fingerprint doesn't match"
477+
"peer_fingerprint match failure"
467478
);
468479
return FAILURE;
469480
}
470481
} else {
471482
php_error_docref(NULL, E_WARNING,
472483
"Expected peer fingerprint must be a string or an array"
473484
);
485+
return FAILURE;
474486
}
475487
}
476488

0 commit comments

Comments
 (0)