From 40d004d7422bf6c4900c1107b3a19f462d859d22 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 25 Mar 2015 00:27:11 -0600 Subject: [PATCH] Remove spaces in full key fingerprint --- .../lib/Horde/Crypt/Pgp/Backend/Binary.php | 2 +- framework/Crypt/test/Horde/Crypt/PgpTest.php | 125 ++++++++++++------ 2 files changed, 88 insertions(+), 39 deletions(-) diff --git a/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php b/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php index bba6117c33e..dc9ba0ebda8 100644 --- a/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php +++ b/framework/Crypt/lib/Horde/Crypt/Pgp/Backend/Binary.php @@ -338,7 +338,7 @@ public function getFingerprintsFromKey($pgpdata) if (preg_match('/pub\s+\w+\/(\w{8})/', $line, $matches)) { $keyid = '0x' . $matches[1]; } elseif ($keyid && preg_match('/^\s+[\s\w]+=\s*([\w\s]+)$/m', $line, $matches)) { - $fingerprints[$keyid] = trim($matches[1]); + $fingerprints[$keyid] = str_replace(' ', '', $matches[1]); $keyid = null; } } diff --git a/framework/Crypt/test/Horde/Crypt/PgpTest.php b/framework/Crypt/test/Horde/Crypt/PgpTest.php index f33992a3a2e..b949e285e03 100644 --- a/framework/Crypt/test/Horde/Crypt/PgpTest.php +++ b/framework/Crypt/test/Horde/Crypt/PgpTest.php @@ -11,9 +11,8 @@ class Horde_Crypt_PgpTest extends PHPUnit_Framework_TestCase { + protected $_language; protected $_pgp; - protected $_privkey; - protected $_pubkey; protected function setUp() { @@ -28,9 +27,6 @@ protected function setUp() 'program' => '/usr/bin/gpg', 'temp' => sys_get_temp_dir() )); - - $this->_privkey = file_get_contents(__DIR__ . '/fixtures/pgp_private.asc'); - $this->_pubkey = file_get_contents(__DIR__ . '/fixtures/pgp_public.asc'); } protected function tearDown() @@ -53,7 +49,7 @@ public function testBug6601() E-Mail: rselsky@bu.edu Hash-Algorithm: pgp-sha1 Key ID: 0xF3C01D42 -Key Fingerprint: 5912 D91D 4C79 C670 1FFF 1486 04A6 7B37 F3C0 1D42 +Key Fingerprint: 5912D91D4C79C6701FFF148604A67B37F3C01D42 ', $this->_pgp->pgpPrettyKey($data) @@ -68,8 +64,8 @@ public function testPgpDecrypt() $decrypt = $this->_pgp->decrypt($crypt, array( 'passphrase' => 'Secret', - 'privkey' => $this->_privkey, - 'pubkey' => $this->_pubkey, + 'privkey' => $this->getPrivateKey(), + 'pubkey' => $this->getPublicKey(), 'type' => 'message' )); @@ -129,7 +125,7 @@ public function testPgpEncrypt() $clear = file_get_contents(__DIR__ . '/fixtures/clear.txt'); $out = $this->_pgp->encrypt($clear, array( - 'recips' => array('me@example.com' => $this->_pubkey), + 'recips' => array('me@example.com' => $this->getPublicKey()), 'type' => 'message' )); @@ -236,7 +232,7 @@ protected function _testParsePGPData($data) public function testPgpPacketInformation() { - $out = $this->_pgp->pgpPacketInformation($this->_pubkey); + $out = $this->_pgp->pgpPacketInformation($this->getPublicKey()); $this->assertArrayHasKey( 'public_key', @@ -259,7 +255,7 @@ public function testPgpPacketInformation() $out['keyid'] ); - $out = $this->_pgp->pgpPacketInformation($this->_privkey); + $out = $this->_pgp->pgpPacketInformation($this->getPrivateKey()); $this->assertArrayHasKey( 'secret_key', @@ -285,21 +281,30 @@ public function testPgpPacketInformation() public function testPgpPacketSignature() { - $out = $this->_pgp->pgpPacketSignature($this->_pubkey, 'me@example.com'); + $out = $this->_pgp->pgpPacketSignature( + $this->getPublicKey(), + 'me@example.com' + ); $this->assertEquals( '7CA74426BADEABD7', $out['keyid'] ); - $out = $this->_pgp->pgpPacketSignature($this->_privkey, 'me@example.com'); + $out = $this->_pgp->pgpPacketSignature( + $this->getPrivateKey(), + 'me@example.com' + ); $this->assertEquals( '7CA74426BADEABD7', $out['keyid'] ); - $out = $this->_pgp->pgpPacketSignature($this->_privkey, 'foo@example.com'); + $out = $this->_pgp->pgpPacketSignature( + $this->getPrivateKey(), + 'foo@example.com' + ); $this->assertArrayNotHasKey( 'keyid', @@ -309,21 +314,30 @@ public function testPgpPacketSignature() public function testPgpPacketSignatureByUidIndex() { - $out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_pubkey, 'id1'); + $out = $this->_pgp->pgpPacketSignatureByUidIndex( + $this->getPublicKey(), + 'id1' + ); $this->assertEquals( '7CA74426BADEABD7', $out['keyid'] ); - $out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_privkey, 'id1'); + $out = $this->_pgp->pgpPacketSignatureByUidIndex( + $this->getPrivateKey(), + 'id1' + ); $this->assertEquals( '7CA74426BADEABD7', $out['keyid'] ); - $out = $this->_pgp->pgpPacketSignatureByUidIndex($this->_privkey, 'id2'); + $out = $this->_pgp->pgpPacketSignatureByUidIndex( + $this->getPrivateKey(), + 'id2' + ); $this->assertArrayNotHasKey( 'keyid', @@ -345,10 +359,10 @@ public function testPgpPrettyKey() E-Mail: me@example.com Hash-Algorithm: pgp-sha1 Key ID: 0xBADEABD7 -Key Fingerprint: 966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7 +Key Fingerprint: 966F4BA9569DE6F65E8253977CA74426BADEABD7 ', - $this->_pgp->pgpPrettyKey($this->_pubkey) + $this->_pgp->pgpPrettyKey($this->getPublicKey()) ); $this->assertEquals( @@ -361,28 +375,45 @@ public function testPgpPrettyKey() E-Mail: me@example.com Hash-Algorithm: pgp-sha1 Key ID: 0xBADEABD7 -Key Fingerprint: 966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7 +Key Fingerprint: 966F4BA9569DE6F65E8253977CA74426BADEABD7 ', - $this->_pgp->pgpPrettyKey($this->_privkey) + $this->_pgp->pgpPrettyKey($this->getPrivateKey()) ); } - public function testPgpGetFingerprintsFromKey() + /** + * @dataProvider pgpGetFingerprintsFromKeyProvider + */ + public function testPgpGetFingerprintsFromKey($expected, $key) { $this->assertEquals( - array('0xBADEABD7' => '966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7'), - $this->_pgp->getFingerprintsFromKey($this->_pubkey) + $expected, + $this->_pgp->getFingerprintsFromKey($key) ); - $this->assertEquals( - array('0xBADEABD7' => '966F 4BA9 569D E6F6 5E82 5397 7CA7 4426 BADE ABD7'), - $this->_pgp->getFingerprintsFromKey($this->_privkey) + } + + public function pgpGetFingerprintsFromKeyProvider() + { + return array( + array( + array( + '0xBADEABD7' => '966F4BA9569DE6F65E8253977CA74426BADEABD7' + ), + $this->getPublicKey() + ), + array( + array( + '0xBADEABD7' => '966F4BA9569DE6F65E8253977CA74426BADEABD7' + ), + $this->getPrivateKey() + ) ); } public function pgpPublicKeyMIMEPart() { - $mime_part = $this->_pgp->publicKeyMIMEPart($this->_pubkey); + $mime_part = $this->_pgp->publicKeyMIMEPart($this->getPublicKey()); $this->assertEquals( 'application/pgp-keys', @@ -424,8 +455,8 @@ public function testPgpSign() $out = $this->_pgp->encrypt($clear, array( 'passphrase' => 'Secret', - 'privkey' => $this->_privkey, - 'pubkey' => $this->_pubkey, + 'privkey' => $this->getPrivateKey(), + 'pubkey' => $this->getPublicKey(), 'type' => 'signature' )); @@ -442,8 +473,8 @@ public function testPgpSign() $out = $this->_pgp->encrypt($clear, array( 'passphrase' => 'Secret', - 'privkey' => $this->_privkey, - 'pubkey' => $this->_pubkey, + 'privkey' => $this->getPrivateKey(), + 'pubkey' => $this->getPublicKey(), 'sigtype' => 'cleartext', 'type' => 'signature' )); @@ -484,7 +515,7 @@ public function testDecryptSignature() $out = $this->_pgp->decrypt( file_get_contents(__DIR__ . '/fixtures/clear.txt'), array( - 'pubkey' => $this->_pubkey, + 'pubkey' => $this->getPublicKey(), 'signature' => file_get_contents(__DIR__ . '/fixtures/pgp_signature.txt'), 'type' => 'detached-signature' ) @@ -495,7 +526,7 @@ public function testDecryptSignature() $out = $this->_pgp->decrypt( file_get_contents(__DIR__ . '/fixtures/pgp_signed.txt'), array( - 'pubkey' => $this->_pubkey, + 'pubkey' => $this->getPublicKey(), 'type' => 'signature' ) ); @@ -505,7 +536,7 @@ public function testDecryptSignature() $out = $this->_pgp->decrypt( file_get_contents(__DIR__ . '/fixtures/pgp_signed2.txt'), array( - 'pubkey' => $this->_pubkey, + 'pubkey' => $this->getPublicKey(), 'type' => 'signature' ) ); @@ -516,19 +547,37 @@ public function testDecryptSignature() public function testVerifyPassphrase() { $this->assertTrue( - $this->_pgp->verifyPassphrase($this->_pubkey, $this->_privkey, 'Secret') + $this->_pgp->verifyPassphrase( + $this->getPublicKey(), + $this->getPrivateKey(), + 'Secret' + ) ); $this->assertFalse( - $this->_pgp->verifyPassphrase($this->_pubkey, $this->_privkey, 'Wrong') + $this->_pgp->verifyPassphrase( + $this->getPublicKey(), + $this->getPrivateKey(), + 'Wrong' + ) ); } public function testGetPublicKeyFromPrivateKey() { $this->assertNotNull( - $this->_pgp->getPublicKeyFromPrivateKey($this->_privkey) + $this->_pgp->getPublicKeyFromPrivateKey($this->getPrivateKey()) ); } + protected function getPrivateKey() + { + return file_get_contents(__DIR__ . '/fixtures/pgp_private.asc'); + } + + protected function getPublicKey() + { + return file_get_contents(__DIR__ . '/fixtures/pgp_public.asc'); + } + }