2013-01-21 00:00:49 +00:00
|
|
|
<?php
|
|
|
|
|
2020-06-18 12:03:56 +00:00
|
|
|
use Leenooks\OpenPGP;
|
2013-01-21 00:00:49 +00:00
|
|
|
|
2020-06-18 12:03:56 +00:00
|
|
|
class MessageVerification extends PHPUnit\Framework\TestCase {
|
2013-01-21 00:00:49 +00:00
|
|
|
public function oneMessageRSA($pkey, $path) {
|
2020-06-18 12:03:56 +00:00
|
|
|
$pkeyM = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $pkey));
|
|
|
|
$m = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path));
|
|
|
|
$verify = new OpenPGP\Crypt\RSA($pkeyM);
|
2013-01-21 02:44:33 +00:00
|
|
|
$this->assertSame($verify->verify($m), $m->signatures());
|
2013-01-21 00:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUncompressedOpsRSA() {
|
|
|
|
$this->oneMessageRSA('pubring.gpg', 'uncompressed-ops-rsa.gpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompressedSig() {
|
|
|
|
$this->oneMessageRSA('pubring.gpg', 'compressedsig.gpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompressedSigZLIB() {
|
|
|
|
$this->oneMessageRSA('pubring.gpg', 'compressedsig-zlib.gpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompressedSigBzip2() {
|
|
|
|
$this->oneMessageRSA('pubring.gpg', 'compressedsig-bzip2.gpg');
|
|
|
|
}
|
|
|
|
|
2013-01-21 03:15:49 +00:00
|
|
|
public function testSigningMessages() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$wkey = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
$data = new OpenPGP\LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
|
|
|
|
$sign = new OpenPGP\Crypt\RSA($wkey);
|
2013-01-21 03:15:49 +00:00
|
|
|
$m = $sign->sign($data)->to_bytes();
|
2020-06-18 12:03:56 +00:00
|
|
|
$reparsedM = OpenPGP\Message::parse($m);
|
2013-01-21 03:15:49 +00:00
|
|
|
$this->assertSame($sign->verify($reparsedM), $reparsedM->signatures());
|
|
|
|
}
|
|
|
|
|
2013-01-21 00:00:49 +00:00
|
|
|
/*
|
|
|
|
public function testUncompressedOpsDSA() {
|
|
|
|
$this->oneMessageDSA('pubring.gpg', 'uncompressed-ops-dsa.gpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUncompressedOpsDSAsha384() {
|
|
|
|
$this->oneMessageDSA('pubring.gpg', 'uncompressed-ops-dsa-sha384.gpg');
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
2013-01-21 02:44:33 +00:00
|
|
|
|
|
|
|
|
2020-06-18 12:03:56 +00:00
|
|
|
class KeyVerification extends PHPUnit\Framework\TestCase {
|
2013-01-21 02:44:33 +00:00
|
|
|
public function oneKeyRSA($path) {
|
2020-06-18 12:03:56 +00:00
|
|
|
$m = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path));
|
|
|
|
$verify = new OpenPGP\Crypt\RSA($m);
|
2013-01-21 02:44:33 +00:00
|
|
|
$this->assertSame($verify->verify($m), $m->signatures());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHelloKey() {
|
|
|
|
$this->oneKeyRSA("helloKey.gpg");
|
|
|
|
}
|
|
|
|
}
|
2013-01-21 23:18:13 +00:00
|
|
|
|
|
|
|
|
2020-06-18 12:03:56 +00:00
|
|
|
class Decryption extends PHPUnit\Framework\TestCase {
|
2013-01-21 23:18:13 +00:00
|
|
|
public function oneSymmetric($pass, $cnt, $path) {
|
2020-06-18 12:03:56 +00:00
|
|
|
$m = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path));
|
|
|
|
$m2 = OpenPGP\Crypt\Symmetric::decryptSymmetric($pass, $m);
|
|
|
|
while($m2[0] instanceof OpenPGP\CompressedDataPacket) $m2 = $m2[0]->data;
|
2013-01-21 23:18:13 +00:00
|
|
|
foreach($m2 as $p) {
|
2020-06-18 12:03:56 +00:00
|
|
|
if($p instanceof OpenPGP\LiteralDataPacket) {
|
2013-01-21 23:18:13 +00:00
|
|
|
$this->assertEquals($p->data, $cnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 16:08:18 +00:00
|
|
|
public function testDecrypt3DES() {
|
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-3des.gpg");
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:38:41 +00:00
|
|
|
public function testDecryptCAST5() { // Requires mcrypt or openssl
|
2013-09-14 18:17:30 +00:00
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-cast5.gpg");
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:06:49 +00:00
|
|
|
public function testDecryptBlowfish() {
|
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-blowfish.gpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDecryptAES() {
|
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-aes.gpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDecryptTwofish() {
|
2020-06-18 12:03:56 +00:00
|
|
|
if(OpenPGP\Crypt\Symmetric::getCipher(10)[0]) {
|
2018-07-25 16:06:49 +00:00
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-twofish.gpg");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 23:18:13 +00:00
|
|
|
public function testDecryptSessionKey() {
|
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-with-session-key.gpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDecryptNoMDC() {
|
|
|
|
$this->oneSymmetric("hello", "PGP\n", "symmetric-no-mdc.gpg");
|
|
|
|
}
|
2013-01-26 19:00:00 +00:00
|
|
|
|
|
|
|
public function testDecryptAsymmetric() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$m = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/hello.gpg'));
|
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
$decryptor = new OpenPGP\Crypt\RSA($key);
|
2013-01-26 19:15:09 +00:00
|
|
|
$m2 = $decryptor->decrypt($m);
|
2020-06-18 12:03:56 +00:00
|
|
|
while($m2[0] instanceof OpenPGP\CompressedDataPacket) $m2 = $m2[0]->data;
|
2013-01-26 19:00:00 +00:00
|
|
|
foreach($m2 as $p) {
|
2020-06-18 12:03:56 +00:00
|
|
|
if($p instanceof OpenPGP\LiteralDataPacket) {
|
2013-01-26 19:00:00 +00:00
|
|
|
$this->assertEquals($p->data, "hello\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-26 19:55:51 +00:00
|
|
|
|
2018-07-25 14:57:33 +00:00
|
|
|
public function testDecryptRoundtrip() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$m = new OpenPGP\Message(array(new OpenPGP\LiteralDataPacket("hello\n")));
|
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
$em = OpenPGP\Crypt\Symmetric::encrypt($key, $m);
|
2018-07-25 14:57:33 +00:00
|
|
|
|
|
|
|
foreach($key as $packet) {
|
2020-06-18 12:03:56 +00:00
|
|
|
if(!($packet instanceof OpenPGP\SecretKeyPacket)) continue;
|
|
|
|
$decryptor = new OpenPGP\Crypt\RSA($packet);
|
2018-07-25 14:57:33 +00:00
|
|
|
$m2 = $decryptor->decrypt($em);
|
|
|
|
|
|
|
|
foreach($m2 as $p) {
|
2020-06-18 12:03:56 +00:00
|
|
|
if($p instanceof OpenPGP\LiteralDataPacket) {
|
2018-07-25 14:57:33 +00:00
|
|
|
$this->assertEquals($p->data, "hello\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 19:55:51 +00:00
|
|
|
public function testDecryptSecretKey() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/encryptedSecretKey.gpg'));
|
|
|
|
$skey = OpenPGP\Crypt\Symmetric::decryptSecretKey("hello", $key[0]);
|
2013-01-26 19:55:51 +00:00
|
|
|
$this->assertSame(!!$skey, true);
|
|
|
|
}
|
2018-07-25 14:56:57 +00:00
|
|
|
|
2019-09-11 02:31:06 +00:00
|
|
|
public function testEncryptSecretKeyRoundtrip() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
$enkey = OpenPGP\Crypt\Symmetric::encryptSecretKey("password", $key[0]);
|
|
|
|
$skey = OpenPGP\Crypt\Symmetric::decryptSecretKey("password", $enkey);
|
2019-09-11 02:31:06 +00:00
|
|
|
$this->assertEquals($key[0], $skey);
|
|
|
|
}
|
|
|
|
|
2018-07-25 14:56:57 +00:00
|
|
|
public function testAlreadyDecryptedSecretKey() {
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionMessage("Data is already unencrypted");
|
2020-06-18 12:03:56 +00:00
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
OpenPGP\Crypt\Symmetric::decryptSecretKey("hello", $key[0]);
|
2018-07-25 14:56:57 +00:00
|
|
|
}
|
2013-01-21 23:18:13 +00:00
|
|
|
}
|
2013-01-26 22:01:26 +00:00
|
|
|
|
2020-06-18 12:03:56 +00:00
|
|
|
class Encryption extends PHPUnit\Framework\TestCase {
|
2018-07-25 18:38:41 +00:00
|
|
|
public function oneSymmetric($algorithm) {
|
2020-06-18 12:03:56 +00:00
|
|
|
$data = new OpenPGP\LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
|
|
|
|
$encrypted = OpenPGP\Crypt\Symmetric::encrypt('secret', new OpenPGP\Message(array($data)), $algorithm);
|
|
|
|
$encrypted = OpenPGP\Message::parse($encrypted->to_bytes());
|
|
|
|
$decrypted = OpenPGP\Crypt\Symmetric::decryptSymmetric('secret', $encrypted);
|
2013-01-26 22:01:26 +00:00
|
|
|
$this->assertEquals($decrypted[0]->data, 'This is text.');
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:38:41 +00:00
|
|
|
public function testEncryptSymmetric3DES() {
|
|
|
|
$this->oneSymmetric(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricCAST5() {
|
|
|
|
$this->oneSymmetric(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricBlowfish() {
|
|
|
|
$this->oneSymmetric(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricAES128() {
|
|
|
|
$this->oneSymmetric(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricAES192() {
|
|
|
|
$this->oneSymmetric(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricAES256() {
|
|
|
|
$this->oneSymmetric(9);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptSymmetricTwofish() {
|
2020-06-18 12:03:56 +00:00
|
|
|
if(OpenPGP\Crypt\Symmetric::getCipher(10)[0]) {
|
2018-07-25 18:38:41 +00:00
|
|
|
$this->oneSymmetric(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 22:01:26 +00:00
|
|
|
public function testEncryptAsymmetric() {
|
2020-06-18 12:03:56 +00:00
|
|
|
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg'));
|
|
|
|
$data = new OpenPGP\LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
|
|
|
|
$encrypted = OpenPGP\Crypt\Symmetric::encrypt($key, new OpenPGP\Message(array($data)));
|
|
|
|
$encrypted = OpenPGP\Message::parse($encrypted->to_bytes());
|
|
|
|
$decryptor = new OpenPGP\Crypt\RSA($key);
|
2013-01-26 22:01:26 +00:00
|
|
|
$decrypted = $decryptor->decrypt($encrypted);
|
|
|
|
$this->assertEquals($decrypted[0]->data, 'This is text.');
|
|
|
|
}
|
|
|
|
}
|