24
24
#include "ext/hash/php_hash_sha.h"
25
25
#include "ext/standard/md5.h"
26
26
27
- #ifdef PHAR_HAVE_OPENSSL
28
- /* OpenSSL includes */
29
- #include <openssl/evp.h>
30
- #include <openssl/x509.h>
31
- #include <openssl/x509v3.h>
32
- #include <openssl/crypto.h>
33
- #include <openssl/pem.h>
34
- #include <openssl/err.h>
35
- #include <openssl/conf.h>
36
- #include <openssl/rand.h>
37
- #include <openssl/ssl.h>
38
- #include <openssl/pkcs12.h>
39
- #else
40
27
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 );
41
- #endif
42
28
43
29
/* for links to relative location, prepend cwd of the entry */
44
30
static char * phar_get_link_location (phar_entry_info * entry ) /* {{{ */
@@ -1439,7 +1425,6 @@ static int phar_hex_str(const char *digest, size_t digest_len, char **signature)
1439
1425
}
1440
1426
/* }}} */
1441
1427
1442
- #ifndef PHAR_HAVE_OPENSSL
1443
1428
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 ) /* {{{ */
1444
1429
{
1445
1430
zend_fcall_info fci ;
@@ -1538,7 +1523,6 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
1538
1523
}
1539
1524
}
1540
1525
/* }}} */
1541
- #endif /* #ifndef PHAR_HAVE_OPENSSL */
1542
1526
1543
1527
zend_result phar_verify_signature (php_stream * fp , size_t end_of_phar , uint32_t sig_type , char * sig , size_t sig_len , char * fname , char * * signature , size_t * signature_len , char * * error ) /* {{{ */
1544
1528
{
@@ -1552,33 +1536,18 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s
1552
1536
case PHAR_SIG_OPENSSL_SHA512 :
1553
1537
case PHAR_SIG_OPENSSL_SHA256 :
1554
1538
case PHAR_SIG_OPENSSL : {
1555
- #ifdef PHAR_HAVE_OPENSSL
1556
- BIO * in ;
1557
- EVP_PKEY * key ;
1558
- const EVP_MD * mdtype ;
1559
- EVP_MD_CTX * md_ctx ;
1560
-
1561
- if (sig_type == PHAR_SIG_OPENSSL_SHA512 ) {
1562
- mdtype = EVP_sha512 ();
1563
- } else if (sig_type == PHAR_SIG_OPENSSL_SHA256 ) {
1564
- mdtype = EVP_sha256 ();
1565
- } else {
1566
- mdtype = EVP_sha1 ();
1567
- }
1568
- #else
1569
1539
size_t tempsig ;
1570
- #endif
1571
1540
zend_string * pubkey = NULL ;
1572
1541
char * pfile ;
1573
1542
php_stream * pfp ;
1574
- #ifndef PHAR_HAVE_OPENSSL
1543
+
1575
1544
if (!zend_hash_str_exists (& module_registry , "openssl" , sizeof ("openssl" )- 1 )) {
1576
1545
if (error ) {
1577
1546
spprintf (error , 0 , "openssl not loaded" );
1578
1547
}
1579
1548
return FAILURE ;
1580
1549
}
1581
- #endif
1550
+
1582
1551
/* use __FILE__ . '.pubkey' for public key file */
1583
1552
spprintf (& pfile , 0 , "%s.pubkey" , fname );
1584
1553
pfp = php_stream_open_wrapper (pfile , "rb" , 0 , NULL );
@@ -1595,7 +1564,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s
1595
1564
}
1596
1565
1597
1566
php_stream_close (pfp );
1598
- #ifndef PHAR_HAVE_OPENSSL
1567
+
1599
1568
tempsig = sig_len ;
1600
1569
1601
1570
if (FAILURE == phar_call_openssl_signverify (0 , fp , end_of_phar , ZSTR_VAL (pubkey ), ZSTR_LEN (pubkey ), & sig , & tempsig , sig_type )) {
@@ -1611,76 +1580,6 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s
1611
1580
zend_string_release_ex (pubkey , 0 );
1612
1581
1613
1582
sig_len = tempsig ;
1614
- #else
1615
- in = BIO_new_mem_buf (ZSTR_VAL (pubkey ), ZSTR_LEN (pubkey ));
1616
-
1617
- if (NULL == in ) {
1618
- zend_string_release_ex (pubkey , 0 );
1619
- if (error ) {
1620
- spprintf (error , 0 , "openssl signature could not be processed" );
1621
- }
1622
- return FAILURE ;
1623
- }
1624
-
1625
- key = PEM_read_bio_PUBKEY (in , NULL , NULL , NULL );
1626
- BIO_free (in );
1627
- zend_string_release_ex (pubkey , 0 );
1628
-
1629
- if (NULL == key ) {
1630
- if (error ) {
1631
- spprintf (error , 0 , "openssl signature could not be processed" );
1632
- }
1633
- return FAILURE ;
1634
- }
1635
-
1636
- md_ctx = EVP_MD_CTX_create ();
1637
- if (!md_ctx || !EVP_VerifyInit (md_ctx , mdtype )) {
1638
- if (md_ctx ) {
1639
- EVP_MD_CTX_destroy (md_ctx );
1640
- }
1641
- if (error ) {
1642
- spprintf (error , 0 , "openssl signature could not be verified" );
1643
- }
1644
- return FAILURE ;
1645
- }
1646
- read_len = end_of_phar ;
1647
-
1648
- if ((size_t )read_len > sizeof (buf )) {
1649
- read_size = sizeof (buf );
1650
- } else {
1651
- read_size = (size_t )read_len ;
1652
- }
1653
-
1654
- php_stream_seek (fp , 0 , SEEK_SET );
1655
-
1656
- while (read_size && (len = php_stream_read (fp , (char * )buf , read_size )) > 0 ) {
1657
- if (UNEXPECTED (EVP_VerifyUpdate (md_ctx , buf , len ) == 0 )) {
1658
- goto failure ;
1659
- }
1660
- read_len -= (zend_off_t )len ;
1661
-
1662
- if (read_len < read_size ) {
1663
- read_size = (size_t )read_len ;
1664
- }
1665
- }
1666
-
1667
- if (EVP_VerifyFinal (md_ctx , (unsigned char * )sig , sig_len , key ) != 1 ) {
1668
- failure :
1669
- /* 1: signature verified, 0: signature does not match, -1: failed signature operation */
1670
- EVP_PKEY_free (key );
1671
- EVP_MD_CTX_destroy (md_ctx );
1672
-
1673
- if (error ) {
1674
- spprintf (error , 0 , "broken openssl signature" );
1675
- }
1676
-
1677
- return FAILURE ;
1678
- }
1679
-
1680
- EVP_PKEY_free (key );
1681
- EVP_MD_CTX_destroy (md_ctx );
1682
- #endif
1683
-
1684
1583
* signature_len = phar_hex_str ((const char * )sig , sig_len , signature );
1685
1584
}
1686
1585
break ;
@@ -1904,85 +1803,6 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char
1904
1803
case PHAR_SIG_OPENSSL_SHA256 :
1905
1804
case PHAR_SIG_OPENSSL : {
1906
1805
unsigned char * sigbuf ;
1907
- #ifdef PHAR_HAVE_OPENSSL
1908
- unsigned int siglen ;
1909
- BIO * in ;
1910
- EVP_PKEY * key ;
1911
- EVP_MD_CTX * md_ctx ;
1912
- const EVP_MD * mdtype ;
1913
-
1914
- if (phar -> sig_flags == PHAR_SIG_OPENSSL_SHA512 ) {
1915
- mdtype = EVP_sha512 ();
1916
- } else if (phar -> sig_flags == PHAR_SIG_OPENSSL_SHA256 ) {
1917
- mdtype = EVP_sha256 ();
1918
- } else {
1919
- mdtype = EVP_sha1 ();
1920
- }
1921
-
1922
- in = BIO_new_mem_buf (PHAR_G (openssl_privatekey ), PHAR_G (openssl_privatekey_len ));
1923
-
1924
- if (in == NULL ) {
1925
- if (error ) {
1926
- spprintf (error , 0 , "unable to write to phar \"%s\" with requested openssl signature" , phar -> fname );
1927
- }
1928
- return FAILURE ;
1929
- }
1930
-
1931
- key = PEM_read_bio_PrivateKey (in , NULL ,NULL , "" );
1932
- BIO_free (in );
1933
-
1934
- if (!key ) {
1935
- if (error ) {
1936
- spprintf (error , 0 , "unable to process private key" );
1937
- }
1938
- return FAILURE ;
1939
- }
1940
-
1941
- md_ctx = EVP_MD_CTX_create ();
1942
- if (md_ctx == NULL ) {
1943
- EVP_PKEY_free (key );
1944
- if (error ) {
1945
- spprintf (error , 0 , "unable to initialize openssl signature for phar \"%s\"" , phar -> fname );
1946
- }
1947
- return FAILURE ;
1948
- }
1949
-
1950
- siglen = EVP_PKEY_size (key );
1951
- sigbuf = emalloc (siglen + 1 );
1952
-
1953
- if (!EVP_SignInit (md_ctx , mdtype )) {
1954
- EVP_PKEY_free (key );
1955
- efree (sigbuf );
1956
- if (error ) {
1957
- spprintf (error , 0 , "unable to initialize openssl signature for phar \"%s\"" , phar -> fname );
1958
- }
1959
- return FAILURE ;
1960
- }
1961
-
1962
- while ((sig_len = php_stream_read (fp , (char * )buf , sizeof (buf ))) > 0 ) {
1963
- if (!EVP_SignUpdate (md_ctx , buf , sig_len )) {
1964
- EVP_PKEY_free (key );
1965
- efree (sigbuf );
1966
- if (error ) {
1967
- spprintf (error , 0 , "unable to update the openssl signature for phar \"%s\"" , phar -> fname );
1968
- }
1969
- return FAILURE ;
1970
- }
1971
- }
1972
-
1973
- if (!EVP_SignFinal (md_ctx , sigbuf , & siglen , key )) {
1974
- EVP_PKEY_free (key );
1975
- efree (sigbuf );
1976
- if (error ) {
1977
- spprintf (error , 0 , "unable to write phar \"%s\" with requested openssl signature" , phar -> fname );
1978
- }
1979
- return FAILURE ;
1980
- }
1981
-
1982
- sigbuf [siglen ] = '\0' ;
1983
- EVP_PKEY_free (key );
1984
- EVP_MD_CTX_destroy (md_ctx );
1985
- #else
1986
1806
size_t siglen ;
1987
1807
sigbuf = NULL ;
1988
1808
siglen = 0 ;
@@ -1994,7 +1814,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char
1994
1814
}
1995
1815
return FAILURE ;
1996
1816
}
1997
- #endif
1817
+
1998
1818
* signature = (char * ) sigbuf ;
1999
1819
* signature_length = siglen ;
2000
1820
}
0 commit comments