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