1ceb81
Backported for 8.0 from
1ceb81
1ceb81
1ceb81
From 8bb0c74e24359a11216824117ac3adf3d5ef7b71 Mon Sep 17 00:00:00 2001
1ceb81
From: Remi Collet <remi@remirepo.net>
1ceb81
Date: Thu, 5 Aug 2021 11:10:15 +0200
1ceb81
Subject: [PATCH] switch phar to use sha256 signature by default
1ceb81
1ceb81
---
1ceb81
 ext/phar/phar/pharcommand.inc                  | 2 +-
1ceb81
 ext/phar/tests/create_new_and_modify.phpt      | 4 ++--
1ceb81
 ext/phar/tests/create_new_phar_c.phpt          | 4 ++--
1ceb81
 ext/phar/tests/phar_setsignaturealgo2.phpt     | 2 +-
1ceb81
 ext/phar/tests/tar/phar_setsignaturealgo2.phpt | 2 +-
1ceb81
 ext/phar/tests/zip/phar_setsignaturealgo2.phpt | 2 +-
1ceb81
 ext/phar/util.c                                | 6 +++---
1ceb81
 ext/phar/zip.c                                 | 2 +-
1ceb81
 8 files changed, 12 insertions(+), 12 deletions(-)
1ceb81
1ceb81
diff --git a/ext/phar/phar/pharcommand.inc b/ext/phar/phar/pharcommand.inc
1ceb81
index a31290eee75fe..5f698b4bec26b 100644
1ceb81
--- a/ext/phar/phar/pharcommand.inc
1ceb81
+++ b/ext/phar/phar/pharcommand.inc
1ceb81
@@ -92,7 +92,7 @@ class PharCommand extends CLICommand
1ceb81
                 'typ' => 'select',
1ceb81
                 'val' => NULL,
1ceb81
                 'inf' => '<method> Selects the hash algorithm.',
1ceb81
-                'select' => array('md5' => 'MD5','sha1' => 'SHA1')
1ceb81
+                'select' => array('md5' => 'MD5','sha1' => 'SHA1', 'sha256' => 'SHA256', 'sha512' => 'SHA512', 'openssl' => 'OPENSSL')
1ceb81
             ),
1ceb81
             'i' => array(
1ceb81
                 'typ' => 'regex',
1ceb81
diff --git a/ext/phar/tests/create_new_and_modify.phpt b/ext/phar/tests/create_new_and_modify.phpt
1ceb81
index 02e36c6cea2fe..32defcae8a639 100644
1ceb81
--- a/ext/phar/tests/create_new_and_modify.phpt
1ceb81
+++ b/ext/phar/tests/create_new_and_modify.phpt
1ceb81
@@ -49,8 +49,8 @@ include $pname . '/b.php';
1ceb81
 
1ceb81
 --EXPECTF--
1ceb81
 brand new!
1ceb81
-string(40) "%s"
1ceb81
-string(40) "%s"
1ceb81
+string(%d) "%s"
1ceb81
+string(%d) "%s"
1ceb81
 bool(true)
1ceb81
 modified!
1ceb81
 another!
1ceb81
diff --git a/ext/phar/tests/create_new_phar_c.phpt b/ext/phar/tests/create_new_phar_c.phpt
1ceb81
index 566d3c4d5f8ad..bf6d740fd1d10 100644
1ceb81
--- a/ext/phar/tests/create_new_phar_c.phpt
1ceb81
+++ b/ext/phar/tests/create_new_phar_c.phpt
1ceb81
@@ -20,7 +20,7 @@ var_dump($phar->getSignature());
1ceb81
 --EXPECTF--
1ceb81
 array(2) {
1ceb81
   ["hash"]=>
1ceb81
-  string(40) "%s"
1ceb81
+  string(64) "%s"
1ceb81
   ["hash_type"]=>
1ceb81
-  string(5) "SHA-1"
1ceb81
+  string(7) "SHA-256"
1ceb81
 }
1ceb81
diff --git a/ext/phar/tests/phar_setsignaturealgo2.phpt b/ext/phar/tests/phar_setsignaturealgo2.phpt
1ceb81
index 293d3196713d8..4f31836fbbbcc 100644
1ceb81
--- a/ext/phar/tests/phar_setsignaturealgo2.phpt
1ceb81
+++ b/ext/phar/tests/phar_setsignaturealgo2.phpt
1ceb81
@@ -52,7 +52,7 @@ array(2) {
1ceb81
   ["hash"]=>
1ceb81
   string(%d) "%s"
1ceb81
   ["hash_type"]=>
1ceb81
-  string(5) "SHA-1"
1ceb81
+  string(7) "SHA-256"
1ceb81
 }
1ceb81
 array(2) {
1ceb81
   ["hash"]=>
1ceb81
diff --git a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
index 9923ac5c88476..cc10a241d739b 100644
1ceb81
--- a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
+++ b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
@@ -51,7 +51,7 @@ array(2) {
1ceb81
   ["hash"]=>
1ceb81
   string(%d) "%s"
1ceb81
   ["hash_type"]=>
1ceb81
-  string(5) "SHA-1"
1ceb81
+  string(7) "SHA-256"
1ceb81
 }
1ceb81
 array(2) {
1ceb81
   ["hash"]=>
1ceb81
diff --git a/ext/phar/tests/zip/phar_setsignaturealgo2.phpt b/ext/phar/tests/zip/phar_setsignaturealgo2.phpt
1ceb81
index 8de77479d7825..60fec578ee894 100644
1ceb81
--- a/ext/phar/tests/zip/phar_setsignaturealgo2.phpt
1ceb81
+++ b/ext/phar/tests/zip/phar_setsignaturealgo2.phpt
1ceb81
@@ -78,7 +78,7 @@ array(2) {
1ceb81
   ["hash"]=>
1ceb81
   string(%d) "%s"
1ceb81
   ["hash_type"]=>
1ceb81
-  string(5) "SHA-1"
1ceb81
+  string(7) "SHA-256"
1ceb81
 }
1ceb81
 array(2) {
1ceb81
   ["hash"]=>
1ceb81
diff --git a/ext/phar/util.c b/ext/phar/util.c
1ceb81
index 314acfe81a788..8d2db03b69601 100644
1ceb81
--- a/ext/phar/util.c
1ceb81
+++ b/ext/phar/util.c
1ceb81
@@ -1798,6 +1798,8 @@ int phar_create_signature(phar_archive_d
1ceb81
 			*signature_length = 64;
1ceb81
 			break;
1ceb81
 		}
1ceb81
+		default:
1ceb81
+			phar->sig_flags = PHAR_SIG_SHA256;
1ceb81
 		case PHAR_SIG_SHA256: {
1ceb81
 			unsigned char digest[32];
1ceb81
 			PHP_SHA256_CTX  context;
1ceb81
@@ -1894,8 +1896,6 @@ int phar_create_signature(phar_archive_d
1ceb81
 			*signature_length = siglen;
1ceb81
 		}
1ceb81
 		break;
1ceb81
-		default:
1ceb81
-			phar->sig_flags = PHAR_SIG_SHA1;
1ceb81
 		case PHAR_SIG_SHA1: {
1ceb81
 			unsigned char digest[20];
1ceb81
 			PHP_SHA1_CTX  context;
1ceb81
diff --git a/ext/phar/zip.c b/ext/phar/zip.c
1ceb81
index 31d4bd2998215..c5e38cabf7b87 100644
1ceb81
--- a/ext/phar/zip.c
1ceb81
+++ b/ext/phar/zip.c
1ceb81
@@ -1423,7 +1423,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
1ceb81
 
1ceb81
 	memcpy(eocd.signature, "PK\5\6", 4);
1ceb81
 	if (!phar->is_data && !phar->sig_flags) {
1ceb81
-		phar->sig_flags = PHAR_SIG_SHA1;
1ceb81
+		phar->sig_flags = PHAR_SIG_SHA256;
1ceb81
 	}
1ceb81
 	if (phar->sig_flags) {
1ceb81
 		PHAR_SET_16(eocd.counthere, zend_hash_num_elements(&phar->manifest) + 1);
1ceb81
1ceb81
From c51af22fef988c1b2f92b7b9e3a9d745f7084815 Mon Sep 17 00:00:00 2001
1ceb81
From: Remi Collet <remi@remirepo.net>
1ceb81
Date: Thu, 5 Aug 2021 16:49:48 +0200
1ceb81
Subject: [PATCH] implement openssl_256 and openssl_512 for phar singatures
1ceb81
1ceb81
---
1ceb81
 ext/openssl/openssl.c                         |   1 +
1ceb81
 ext/phar/phar.1.in                            |  10 +++-
1ceb81
 ext/phar/phar.c                               |   8 +++-
1ceb81
 ext/phar/phar/pharcommand.inc                 |  14 +++++-
1ceb81
 ext/phar/phar_internal.h                      |   2 +
1ceb81
 ext/phar/phar_object.c                        |  24 ++++++++--
1ceb81
 ext/phar/tests/files/openssl256.phar          | Bin 0 -> 7129 bytes
1ceb81
 ext/phar/tests/files/openssl256.phar.pubkey   |   6 +++
1ceb81
 ext/phar/tests/files/openssl512.phar          | Bin 0 -> 7129 bytes
1ceb81
 ext/phar/tests/files/openssl512.phar.pubkey   |   6 +++
1ceb81
 .../phar_get_supported_signatures_002a.phpt   |   6 ++-
1ceb81
 .../tests/tar/phar_setsignaturealgo2.phpt     |  16 +++++++
1ceb81
 ext/phar/tests/test_signaturealgos.phpt       |   8 ++++
1ceb81
 ext/phar/util.c                               |  45 ++++++++++++++----
1ceb81
 14 files changed, 128 insertions(+), 18 deletions(-)
1ceb81
 create mode 100644 ext/phar/tests/files/openssl256.phar
1ceb81
 create mode 100644 ext/phar/tests/files/openssl256.phar.pubkey
1ceb81
 create mode 100644 ext/phar/tests/files/openssl512.phar
1ceb81
 create mode 100644 ext/phar/tests/files/openssl512.phar.pubkey
1ceb81
1ceb81
diff --git a/ext/phar/phar.1.in b/ext/phar/phar.1.in
1ceb81
index 77912b241dfd5..323e77b0e2a3b 100644
1ceb81
--- a/ext/phar/phar.1.in
1ceb81
+++ b/ext/phar/phar.1.in
1ceb81
@@ -475,7 +475,15 @@ SHA512
1ceb81
 .TP
1ceb81
 .PD
1ceb81
 .B openssl
1ceb81
-OpenSSL
1ceb81
+OpenSSL using SHA-1
1ceb81
+.TP
1ceb81
+.PD
1ceb81
+.B openssl_sha256
1ceb81
+OpenSSL using SHA-256
1ceb81
+.TP
1ceb81
+.PD
1ceb81
+.B openssl_sha512
1ceb81
+OpenSSL using SHA-512
1ceb81
 
1ceb81
 .SH SEE ALSO
1ceb81
 For a more or less complete description of PHAR look here:
1ceb81
diff --git a/ext/phar/phar.c b/ext/phar/phar.c
1ceb81
index 77f21cef9da53..bc08e4edde05d 100644
1ceb81
--- a/ext/phar/phar.c
1ceb81
+++ b/ext/phar/phar.c
1ceb81
@@ -869,6 +869,8 @@ static int phar_parse_pharfile(php_stream *fp, char *fname, size_t fname_len, ch
1ceb81
 		PHAR_GET_32(sig_ptr, sig_flags);
1ceb81
 
1ceb81
 		switch(sig_flags) {
1ceb81
+			case PHAR_SIG_OPENSSL_SHA512:
1ceb81
+			case PHAR_SIG_OPENSSL_SHA256:
1ceb81
 			case PHAR_SIG_OPENSSL: {
1ceb81
 				uint32_t signature_len;
1ceb81
 				char *sig;
1ceb81
@@ -903,7 +905,7 @@ static int phar_parse_pharfile(php_stream *fp, char *fname, size_t fname_len, ch
1ceb81
 					return FAILURE;
1ceb81
 				}
1ceb81
 
1ceb81
-				if (FAILURE == phar_verify_signature(fp, end_of_phar, PHAR_SIG_OPENSSL, sig, signature_len, fname, &signature, &sig_len, error)) {
1ceb81
+				if (FAILURE == phar_verify_signature(fp, end_of_phar, sig_flags, sig, signature_len, fname, &signature, &sig_len, error)) {
1ceb81
 					efree(savebuf);
1ceb81
 					efree(sig);
1ceb81
 					php_stream_close(fp);
1ceb81
@@ -3162,7 +3164,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
1ceb81
 
1ceb81
 				php_stream_write(newfile, digest, digest_len);
1ceb81
 				efree(digest);
1ceb81
-				if (phar->sig_flags == PHAR_SIG_OPENSSL) {
1ceb81
+				if (phar->sig_flags == PHAR_SIG_OPENSSL ||
1ceb81
+					phar->sig_flags == PHAR_SIG_OPENSSL_SHA256 ||
1ceb81
+					phar->sig_flags == PHAR_SIG_OPENSSL_SHA512) {
1ceb81
 					phar_set_32(sig_buf, digest_len);
1ceb81
 					php_stream_write(newfile, sig_buf, 4);
1ceb81
 				}
1ceb81
diff --git a/ext/phar/phar/pharcommand.inc b/ext/phar/phar/pharcommand.inc
1ceb81
index 5f698b4bec26b..1b1eeca59c560 100644
1ceb81
--- a/ext/phar/phar/pharcommand.inc
1ceb81
+++ b/ext/phar/phar/pharcommand.inc
1ceb81
@@ -92,7 +92,7 @@ class PharCommand extends CLICommand
1ceb81
                 'typ' => 'select',
1ceb81
                 'val' => NULL,
1ceb81
                 'inf' => '<method> Selects the hash algorithm.',
1ceb81
-                'select' => array('md5' => 'MD5','sha1' => 'SHA1', 'sha256' => 'SHA256', 'sha512' => 'SHA512', 'openssl' => 'OPENSSL')
1ceb81
+                'select' => ['md5' => 'MD5','sha1' => 'SHA1', 'sha256' => 'SHA256', 'sha512' => 'SHA512', 'openssl' => 'OPENSSL', 'openssl_sha256' => 'OPENSSL_SHA256', 'openssl_sha512' => 'OPENSSL_SHA512']
1ceb81
             ),
1ceb81
             'i' => array(
1ceb81
                 'typ' => 'regex',
1ceb81
@@ -156,6 +156,8 @@ class PharCommand extends CLICommand
1ceb81
         $hash_avail = Phar::getSupportedSignatures();
1ceb81
         $hash_optional = array('SHA-256' => 'SHA256',
1ceb81
                                'SHA-512' => 'SHA512',
1ceb81
+                               'OpenSSL_sha256' => 'OpenSSL_SHA256',
1ceb81
+                               'OpenSSL_sha512' => 'OpenSSL_SHA512',
1ceb81
                                'OpenSSL' => 'OpenSSL');
1ceb81
         if (!in_array('OpenSSL', $hash_avail)) {
1ceb81
             unset($phar_args['y']);
1ceb81
@@ -429,6 +431,16 @@ class PharCommand extends CLICommand
1ceb81
                     self::error("Cannot use OpenSSL signing without key.\n");
1ceb81
                 }
1ceb81
                 return Phar::OPENSSL;
1ceb81
+            case 'openssl_sha256':
1ceb81
+                if (!$privkey) {
1ceb81
+                    self::error("Cannot use OpenSSL signing without key.\n");
1ceb81
+                }
1ceb81
+                return Phar::OPENSSL_SHA256;
1ceb81
+            case 'openssl_sha512':
1ceb81
+                if (!$privkey) {
1ceb81
+                    self::error("Cannot use OpenSSL signing without key.\n");
1ceb81
+                }
1ceb81
+                return Phar::OPENSSL_SHA512;
1ceb81
         }
1ceb81
     }
1ceb81
     // }}}
1ceb81
diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h
1ceb81
index a9f81e2ab994a..30b408a8c4462 100644
1ceb81
--- a/ext/phar/phar_internal.h
1ceb81
+++ b/ext/phar/phar_internal.h
1ceb81
@@ -88,6 +88,8 @@
1ceb81
 #define PHAR_SIG_SHA256           0x0003
1ceb81
 #define PHAR_SIG_SHA512           0x0004
1ceb81
 #define PHAR_SIG_OPENSSL          0x0010
1ceb81
+#define PHAR_SIG_OPENSSL_SHA256   0x0011
1ceb81
+#define PHAR_SIG_OPENSSL_SHA512   0x0012
1ceb81
 
1ceb81
 /* flags byte for each file adheres to these bitmasks.
1ceb81
    All unused values are reserved */
1ceb81
diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c
1ceb81
index 9c1e5f2fa1eef..c05970e657f18 100644
1ceb81
--- a/ext/phar/phar_object.c
1ceb81
+++ b/ext/phar/phar_object.c
1ceb81
@@ -1246,9 +1246,13 @@ PHP_METHOD(Phar, getSupportedSignatures)
1ceb81
 	add_next_index_stringl(return_value, "SHA-512", 7);
1ceb81
 #ifdef PHAR_HAVE_OPENSSL
1ceb81
 	add_next_index_stringl(return_value, "OpenSSL", 7);
1ceb81
+	add_next_index_stringl(return_value, "OpenSSL_SHA256", 14);
1ceb81
+	add_next_index_stringl(return_value, "OpenSSL_SHA512", 14);
1ceb81
 #else
1ceb81
 	if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
1ceb81
 		add_next_index_stringl(return_value, "OpenSSL", 7);
1ceb81
+		add_next_index_stringl(return_value, "OpenSSL_SHA256", 14);
1ceb81
+		add_next_index_stringl(return_value, "OpenSSL_SHA512", 14);
1ceb81
 	}
1ceb81
 #endif
1ceb81
 }
1ceb81
@@ -3028,6 +3032,8 @@ PHP_METHOD(Phar, setSignatureAlgorithm)
1ceb81
 		case PHAR_SIG_MD5:
1ceb81
 		case PHAR_SIG_SHA1:
1ceb81
 		case PHAR_SIG_OPENSSL:
1ceb81
+		case PHAR_SIG_OPENSSL_SHA256:
1ceb81
+		case PHAR_SIG_OPENSSL_SHA512:
1ceb81
 			if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
1ceb81
 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
1ceb81
 				RETURN_THROWS();
1ceb81
@@ -3066,19 +3072,25 @@ PHP_METHOD(Phar, getSignature)
1ceb81
 		add_assoc_stringl(return_value, "hash", phar_obj->archive->signature, phar_obj->archive->sig_len);
1ceb81
 		switch(phar_obj->archive->sig_flags) {
1ceb81
 			case PHAR_SIG_MD5:
1ceb81
-				add_assoc_stringl(return_value, "hash_type", "MD5", 3);
1ceb81
+				add_assoc_string(return_value, "hash_type", "MD5");
1ceb81
 				break;
1ceb81
 			case PHAR_SIG_SHA1:
1ceb81
-				add_assoc_stringl(return_value, "hash_type", "SHA-1", 5);
1ceb81
+				add_assoc_string(return_value, "hash_type", "SHA-1");
1ceb81
 				break;
1ceb81
 			case PHAR_SIG_SHA256:
1ceb81
-				add_assoc_stringl(return_value, "hash_type", "SHA-256", 7);
1ceb81
+				add_assoc_string(return_value, "hash_type", "SHA-256");
1ceb81
 				break;
1ceb81
 			case PHAR_SIG_SHA512:
1ceb81
-				add_assoc_stringl(return_value, "hash_type", "SHA-512", 7);
1ceb81
+				add_assoc_string(return_value, "hash_type", "SHA-512");
1ceb81
 				break;
1ceb81
 			case PHAR_SIG_OPENSSL:
1ceb81
-				add_assoc_stringl(return_value, "hash_type", "OpenSSL", 7);
1ceb81
+				add_assoc_string(return_value, "hash_type", "OpenSSL");
1ceb81
+				break;
1ceb81
+			case PHAR_SIG_OPENSSL_SHA256:
1ceb81
+				add_assoc_string(return_value, "hash_type", "OpenSSL_SHA256");
1ceb81
+				break;
1ceb81
+			case PHAR_SIG_OPENSSL_SHA512:
1ceb81
+				add_assoc_string(return_value, "hash_type", "OpenSSL_SHA512");
1ceb81
 				break;
1ceb81
 			default:
1ceb81
 				unknown = strpprintf(0, "Unknown (%u)", phar_obj->archive->sig_flags);
1ceb81
@@ -5103,6 +5115,8 @@ void phar_object_init(void) /* {{{ */
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
1ceb81
+	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL_SHA256", PHAR_SIG_OPENSSL_SHA256)
1ceb81
+	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL_SHA512", PHAR_SIG_OPENSSL_SHA512)
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
1ceb81
 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
1ceb81
diff --git a/ext/phar/tests/phar_get_supported_signatures_002a.phpt b/ext/phar/tests/phar_get_supported_signatures_002a.phpt
1ceb81
index 06d811f2c35c2..639143b3d2c90 100644
1ceb81
--- a/ext/phar/tests/phar_get_supported_signatures_002a.phpt
1ceb81
+++ b/ext/phar/tests/phar_get_supported_signatures_002a.phpt
1ceb81
@@ -14,7 +14,7 @@ phar.readonly=0
1ceb81
 var_dump(Phar::getSupportedSignatures());
1ceb81
 ?>
1ceb81
 --EXPECT--
1ceb81
-array(5) {
1ceb81
+array(7) {
1ceb81
   [0]=>
1ceb81
   string(3) "MD5"
1ceb81
   [1]=>
1ceb81
@@ -25,4 +25,8 @@ array(5) {
1ceb81
   string(7) "SHA-512"
1ceb81
   [4]=>
1ceb81
   string(7) "OpenSSL"
1ceb81
+  [5]=>
1ceb81
+  string(14) "OpenSSL_SHA256"
1ceb81
+  [6]=>
1ceb81
+  string(14) "OpenSSL_SHA512"
1ceb81
 }
1ceb81
diff --git a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
index cc10a241d739b..c2eb5d77a5bf0 100644
1ceb81
--- a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
+++ b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
1ceb81
@@ -38,6 +38,10 @@ $pkey = '';
1ceb81
 openssl_pkey_export($private, $pkey, NULL, $config_arg);
1ceb81
 $p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
1ceb81
 var_dump($p->getSignature());
1ceb81
+$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey);
1ceb81
+var_dump($p->getSignature());
1ceb81
+$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey);
1ceb81
+var_dump($p->getSignature());
1ceb81
 } catch (Exception $e) {
1ceb81
 echo $e->getMessage();
1ceb81
 }
1ceb81
@@ -83,3 +87,15 @@ array(2) {
1ceb81
   ["hash_type"]=>
1ceb81
   string(7) "OpenSSL"
1ceb81
 }
1ceb81
+array(2) {
1ceb81
+  ["hash"]=>
1ceb81
+  string(%d) "%s"
1ceb81
+  ["hash_type"]=>
1ceb81
+  string(14) "OpenSSL_SHA512"
1ceb81
+}
1ceb81
+array(2) {
1ceb81
+  ["hash"]=>
1ceb81
+  string(%d) "%s"
1ceb81
+  ["hash_type"]=>
1ceb81
+  string(14) "OpenSSL_SHA256"
1ceb81
+}
1ceb81
diff --git a/ext/phar/util.c b/ext/phar/util.c
1ceb81
index 8d2db03b69601..515830bf2c70a 100644
1ceb81
--- a/ext/phar/util.c
1ceb81
+++ b/ext/phar/util.c
1ceb81
@@ -34,7 +34,7 @@
1ceb81
 #include <openssl/ssl.h>
1ceb81
 #include <openssl/pkcs12.h>
1ceb81
 #else
1ceb81
-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);
1ceb81
+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, php_uint32 sig_type);
1ceb81
 #endif
1ceb81
 
1ceb81
 /* for links to relative location, prepend cwd of the entry */
1ceb81
@@ -1381,11 +1381,11 @@ static int phar_hex_str(const char *digest, size_t digest_len, char **signature)
1ceb81
 /* }}} */
1ceb81
 
1ceb81
 #ifndef PHAR_HAVE_OPENSSL
1ceb81
-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) /* {{{ */
1ceb81
+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, php_uint32 sig_type) /* {{{ */
1ceb81
 {
1ceb81
 	zend_fcall_info fci;
1ceb81
 	zend_fcall_info_cache fcc;
1ceb81
-	zval retval, zp[3], openssl;
1ceb81
+	zval retval, zp[4], openssl;
1ceb81
 	zend_string *str;
1ceb81
 
1ceb81
 	ZVAL_STRINGL(&openssl, is_sign ? "openssl_sign" : "openssl_verify", is_sign ? sizeof("openssl_sign")-1 : sizeof("openssl_verify")-1);
1ceb81
@@ -1402,6 +1402,14 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
1ceb81
 	} else {
1ceb81
 		ZVAL_EMPTY_STRING(&zp[0]);
1ceb81
 	}
1ceb81
+	if (sig_type == PHAR_SIG_OPENSSL_SHA512) {
1ceb81
+		ZVAL_LONG(&zp[3], 9); /* value from openssl.c #define OPENSSL_ALGO_SHA512 9 */
1ceb81
+	} else if (sig_type == PHAR_SIG_OPENSSL_SHA256) {
1ceb81
+		ZVAL_LONG(&zp[3], 7); /* value from openssl.c #define OPENSSL_ALGO_SHA256 7 */
1ceb81
+	} else {
1ceb81
+		/* don't rely on default value which may change in the future */
1ceb81
+		ZVAL_LONG(&zp[3], 1); /* value from openssl.c #define OPENSSL_ALGO_SHA1   1 */
1ceb81
+	}
1ceb81
 
1ceb81
 	if ((size_t)end != Z_STRLEN(zp[0])) {
1ceb81
 		zval_ptr_dtor_str(&zp[0]);
1ceb81
@@ -1419,7 +1427,7 @@ static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t
1ceb81
 		return FAILURE;
1ceb81
 	}
1ceb81
 
1ceb81
-	fci.param_count = 3;
1ceb81
+	fci.param_count = 4;
1ceb81
 	fci.params = zp;
1ceb81
 	Z_ADDREF(zp[0]);
1ceb81
 	if (is_sign) {
1ceb81
@@ -1482,12 +1490,22 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type,
1ceb81
 	php_stream_rewind(fp);
1ceb81
 
1ceb81
 	switch (sig_type) {
1ceb81
+		case PHAR_SIG_OPENSSL_SHA512:
1ceb81
+		case PHAR_SIG_OPENSSL_SHA256:
1ceb81
 		case PHAR_SIG_OPENSSL: {
1ceb81
 #ifdef PHAR_HAVE_OPENSSL
1ceb81
 			BIO *in;
1ceb81
 			EVP_PKEY *key;
1ceb81
-			EVP_MD *mdtype = (EVP_MD *) EVP_sha1();
1ceb81
+			const EVP_MD *mdtype;
1ceb81
 			EVP_MD_CTX *md_ctx;
1ceb81
+
1ceb81
+			if (sig_type == PHAR_SIG_OPENSSL_SHA512) {
1ceb81
+				mdtype = EVP_sha512();
1ceb81
+			} else if (sig_type == PHAR_SIG_OPENSSL_SHA256) {
1ceb81
+				mdtype = EVP_sha256();
1ceb81
+			} else {
1ceb81
+				mdtype = EVP_sha1();
1ceb81
+			}
1ceb81
 #else
1ceb81
 			size_t tempsig;
1ceb81
 #endif
1ceb81
@@ -1521,7 +1539,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type,
1ceb81
 #ifndef PHAR_HAVE_OPENSSL
1ceb81
 			tempsig = sig_len;
1ceb81
 
1ceb81
-			if (FAILURE == phar_call_openssl_signverify(0, fp, end_of_phar, pubkey ? ZSTR_VAL(pubkey) : NULL, pubkey ? ZSTR_LEN(pubkey) : 0, &sig, &tempsig)) {
1ceb81
+			if (FAILURE == phar_call_openssl_signverify(0, fp, end_of_phar, pubkey ? ZSTR_VAL(pubkey) : NULL, pubkey ? ZSTR_LEN(pubkey) : 0, &sig, &tempsig, sig_type)) {
1ceb81
 				if (pubkey) {
1ceb81
 					zend_string_release_ex(pubkey, 0);
1ceb81
 				}
1ceb81
@@ -1815,6 +1833,8 @@ int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signat
1ceb81
 			*signature_length = 32;
1ceb81
 			break;
1ceb81
 		}
1ceb81
+		case PHAR_SIG_OPENSSL_SHA512:
1ceb81
+		case PHAR_SIG_OPENSSL_SHA256:
1ceb81
 		case PHAR_SIG_OPENSSL: {
1ceb81
 			unsigned char *sigbuf;
1ceb81
 #ifdef PHAR_HAVE_OPENSSL
1ceb81
@@ -1822,6 +1842,15 @@ int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signat
1ceb81
 			BIO *in;
1ceb81
 			EVP_PKEY *key;
1ceb81
 			EVP_MD_CTX *md_ctx;
1ceb81
+			const EVP_MD *mdtype;
1ceb81
+
1ceb81
+			if (phar->sig_flags == PHAR_SIG_OPENSSL_SHA512) {
1ceb81
+				mdtype = EVP_sha512();
1ceb81
+			} else if (phar->sig_flags == PHAR_SIG_OPENSSL_SHA256) {
1ceb81
+				mdtype = EVP_sha256();
1ceb81
+			} else {
1ceb81
+				mdtype = EVP_sha1();
1ceb81
+			}
1ceb81
 
1ceb81
 			in = BIO_new_mem_buf(PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len));
1ceb81
 
1ceb81
@@ -1847,7 +1876,7 @@ int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signat
1ceb81
 			siglen = EVP_PKEY_size(key);
1ceb81
 			sigbuf = emalloc(siglen + 1);
1ceb81
 
1ceb81
-			if (!EVP_SignInit(md_ctx, EVP_sha1())) {
1ceb81
+			if (!EVP_SignInit(md_ctx, mdtype)) {
1ceb81
 				EVP_PKEY_free(key);
1ceb81
 				efree(sigbuf);
1ceb81
 				if (error) {
1ceb81
@@ -1885,7 +1914,7 @@ int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signat
1ceb81
 			siglen = 0;
1ceb81
 			php_stream_seek(fp, 0, SEEK_END);
1ceb81
 
1ceb81
-			if (FAILURE == phar_call_openssl_signverify(1, fp, php_stream_tell(fp), PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len), (char **)&sigbuf, &siglen)) {
1ceb81
+			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)) {
1ceb81
 				if (error) {
1ceb81
 					spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname);
1ceb81
 				}