Skip to content

Commit 234219d

Browse files
committed
ext/phar: Refactor phar_call_openssl_signverify()
1 parent 2684a5e commit 234219d

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

ext/phar/util.c

+19-30
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <openssl/ssl.h>
3737
#include <openssl/pkcs12.h>
3838
#else
39-
static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, uint32_t sig_type);
39+
static zend_result phar_call_openssl_signverify(bool is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, uint32_t sig_type);
4040
#endif
4141

4242
/* for links to relative location, prepend cwd of the entry */
@@ -1427,14 +1427,23 @@ static int phar_hex_str(const char *digest, size_t digest_len, char **signature)
14271427
/* }}} */
14281428

14291429
#ifndef PHAR_HAVE_OPENSSL
1430-
static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, uint32_t sig_type) /* {{{ */
1430+
static zend_result phar_call_openssl_signverify(bool is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, uint32_t sig_type) /* {{{ */
14311431
{
1432-
zend_fcall_info fci;
1433-
zend_fcall_info_cache fcc;
1434-
zval retval, zp[4], openssl;
1432+
zval retval, zp[4];
14351433
zend_string *str;
14361434

1437-
ZVAL_STRINGL(&openssl, is_sign ? "openssl_sign" : "openssl_verify", is_sign ? sizeof("openssl_sign")-1 : sizeof("openssl_verify")-1);
1435+
zend_function *fn = NULL;
1436+
if (is_sign) {
1437+
fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("openssl_sign"));
1438+
} else {
1439+
fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("openssl_verify"));
1440+
}
1441+
1442+
/* OpenSSL is not available, even as a shared module */
1443+
if (fn == NULL) {
1444+
return FAILURE;
1445+
}
1446+
14381447
if (*signature_len) {
14391448
ZVAL_STRINGL(&zp[1], *signature, *signature_len);
14401449
} else {
@@ -1461,20 +1470,9 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
14611470
zval_ptr_dtor_str(&zp[0]);
14621471
zval_ptr_dtor_str(&zp[1]);
14631472
zval_ptr_dtor_str(&zp[2]);
1464-
zval_ptr_dtor_str(&openssl);
1465-
return FAILURE;
1466-
}
1467-
1468-
if (FAILURE == zend_fcall_info_init(&openssl, 0, &fci, &fcc, NULL, NULL)) {
1469-
zval_ptr_dtor_str(&zp[0]);
1470-
zval_ptr_dtor_str(&zp[1]);
1471-
zval_ptr_dtor_str(&zp[2]);
1472-
zval_ptr_dtor_str(&openssl);
14731473
return FAILURE;
14741474
}
14751475

1476-
fci.param_count = 4;
1477-
fci.params = zp;
14781476
Z_ADDREF(zp[0]);
14791477
if (is_sign) {
14801478
ZVAL_NEW_REF(&zp[1], &zp[1]);
@@ -1483,17 +1481,8 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
14831481
}
14841482
Z_ADDREF(zp[2]);
14851483

1486-
fci.retval = &retval;
1487-
1488-
if (FAILURE == zend_call_function(&fci, &fcc)) {
1489-
zval_ptr_dtor_str(&zp[0]);
1490-
zval_ptr_dtor(&zp[1]);
1491-
zval_ptr_dtor_str(&zp[2]);
1492-
zval_ptr_dtor_str(&openssl);
1493-
return FAILURE;
1494-
}
1484+
zend_call_known_function(fn, NULL, NULL, &retval, /* param_count */ 4, zp, NULL);
14951485

1496-
zval_ptr_dtor_str(&openssl);
14971486
Z_DELREF(zp[0]);
14981487

14991488
if (is_sign) {
@@ -1507,7 +1496,6 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
15071496
zval_ptr_dtor_str(&zp[2]);
15081497

15091498
switch (Z_TYPE(retval)) {
1510-
default:
15111499
case IS_LONG:
15121500
zval_ptr_dtor(&zp[1]);
15131501
if (1 == Z_LVAL(retval)) {
@@ -1520,6 +1508,7 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
15201508
zval_ptr_dtor(&zp[1]);
15211509
return SUCCESS;
15221510
case IS_FALSE:
1511+
default:
15231512
zval_ptr_dtor(&zp[1]);
15241513
return FAILURE;
15251514
}
@@ -1585,7 +1574,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s
15851574
#ifndef PHAR_HAVE_OPENSSL
15861575
tempsig = sig_len;
15871576

1588-
if (FAILURE == phar_call_openssl_signverify(0, fp, end_of_phar, ZSTR_VAL(pubkey), ZSTR_LEN(pubkey), &sig, &tempsig, sig_type)) {
1577+
if (FAILURE == phar_call_openssl_signverify(false, fp, end_of_phar, ZSTR_VAL(pubkey), ZSTR_LEN(pubkey), &sig, &tempsig, sig_type)) {
15891578
zend_string_release_ex(pubkey, 0);
15901579

15911580
if (error) {
@@ -1975,7 +1964,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char
19751964
siglen = 0;
19761965
php_stream_seek(fp, 0, SEEK_END);
19771966

1978-
if (FAILURE == phar_call_openssl_signverify(1, fp, php_stream_tell(fp), PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len), (char **)&sigbuf, &siglen, phar->sig_flags)) {
1967+
if (FAILURE == phar_call_openssl_signverify(true, fp, php_stream_tell(fp), PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len), (char **)&sigbuf, &siglen, phar->sig_flags)) {
19791968
if (error) {
19801969
spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname);
19811970
}

0 commit comments

Comments
 (0)