Compare commits

..

No commits in common. "wip" and "master" have entirely different histories.
wip ... master

19 changed files with 97 additions and 152 deletions

View File

@ -14,8 +14,8 @@
namespace Leenooks; namespace Leenooks;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use phpseclib\Crypt\RSA as Crypt_RSA; use phpseclib\Crypt\RSA as Crypt_RSA;
use Leenooks\OpenPGP\Exceptions\PacketTagException; use Leenooks\OpenPGP\Exceptions\PacketTagException;
/** /**
@ -26,8 +26,6 @@ class OpenPGP
const VERSION = [0,5,0]; const VERSION = [0,5,0];
private $key = NULL; private $key = NULL;
// Functions
/** /**
* @see http://tools.ietf.org/html/rfc4880#section-12.2 * @see http://tools.ietf.org/html/rfc4880#section-12.2
*/ */
@ -99,18 +97,12 @@ class OpenPGP
return ((int)16 + ($c & 15)) << (($c >> 4) + 6); return ((int)16 + ($c & 15)) << (($c >> 4) + 6);
} }
public function decrypt($data)
{
$decryptor = new OpenPGP\Crypt\RSA($this->key);
return $decryptor->decrypt($data);
}
/** /**
* @see http://tools.ietf.org/html/rfc4880#section-6 * @see http://tools.ietf.org/html/rfc4880#section-6
* @see http://tools.ietf.org/html/rfc4880#section-6.2 * @see http://tools.ietf.org/html/rfc4880#section-6.2
* @see http://tools.ietf.org/html/rfc2045 * @see http://tools.ietf.org/html/rfc2045
*/ */
static function enarmor1($data,$marker='MESSAGE',array $headers=[]) static function enarmor($data,$marker='MESSAGE',array $headers=[])
{ {
$text = self::header($marker)."\n"; $text = self::header($marker)."\n";
@ -125,16 +117,6 @@ class OpenPGP
return $text; return $text;
} }
protected function enarmor(string $data,$marker='MESSAGE',array $headers=[]): string
{
return static::enarmor1($data,$marker,$headers);
}
public function encrypt(OpenPGP\LiteralDataPacket $data)
{
return OpenPGP\Crypt\Symmetric::encrypt($this->key,new OpenPGP\Message([$data]));
}
static function encode_s2k_count($iterations) static function encode_s2k_count($iterations)
{ {
if($iterations >= 65011712) return 255; if($iterations >= 65011712) return 255;
@ -171,42 +153,6 @@ class OpenPGP
return '-----BEGIN '.strtoupper((string)$marker).'-----'; return '-----BEGIN '.strtoupper((string)$marker).'-----';
} }
public function key()
{
return $this->key;
}
static function load(string $data,$marker='MESSAGE',bool $binary=TRUE): self
{
$result = new self;
$result->key = OpenPGP\Message::parse(
$binary ? $data : OpenPGP::unarmor($data,$marker)
);
return $result;
}
public function private(): string
{
return $this->enarmor((string)$this->key,'PRIVATE KEY');
}
protected function publicKey(): OpenPGP\PublicKeyPacket
{
return $this->key[0];
}
public function public(): string
{
return $this->enarmor((string)$this->publicKey(),'PUBLIC KEY');
}
public function signatures()
{
return $this->key->signatures();
}
/** /**
* @see http://tools.ietf.org/html/rfc4880#section-6 * @see http://tools.ietf.org/html/rfc4880#section-6
* @see http://tools.ietf.org/html/rfc2045 * @see http://tools.ietf.org/html/rfc2045
@ -224,10 +170,4 @@ class OpenPGP
return base64_decode($text=substr($text,$pos1,$pos2-$pos1)); return base64_decode($text=substr($text,$pos1,$pos2-$pos1));
} }
} }
public function verify(OpenPGP\Message $data)
{
$verify = new OpenPGP\Crypt\RSA($this->key);
return $verify->verify($data);
}
} }

View File

@ -51,19 +51,19 @@ class CompressedDataPacket extends Packet implements \IteratorAggregate, \ArrayA
switch($this->algorithm) { switch($this->algorithm) {
case 0: case 0:
$body .= (string)$this->data; $body .= $this->data->to_bytes();
break; break;
case 1: case 1:
$body .= gzdeflate((string)$this->data); $body .= gzdeflate($this->data->to_bytes());
break; break;
case 2: case 2:
$body .= gzcompress((string)$this->data); $body .= gzcompress($this->data->to_bytes());
break; break;
case 3: case 3:
$body .= bzcompress((string)$this->data); $body .= bzcompress($this->data->to_bytes());
break; break;
default: default:

View File

@ -32,10 +32,10 @@ class Symmetric
$key = Random::string($key_bytes); $key = Random::string($key_bytes);
$cipher->setKey($key); $cipher->setKey($key);
$to_encrypt = $prefix.$message; $to_encrypt = $prefix.$message->to_bytes();
$mdc = new OpenPGP\ModificationDetectionCodePacket(hash('sha1',$to_encrypt."\xD3\x14",true)); $mdc = new OpenPGP\ModificationDetectionCodePacket(hash('sha1',$to_encrypt."\xD3\x14",true));
$to_encrypt .= (string)$mdc; $to_encrypt .= $mdc->to_bytes();
if (static::$DEBUG) if (static::$DEBUG)
dump(['to_encrypt'=>$to_encrypt]); dump(['to_encrypt'=>$to_encrypt]);

View File

@ -48,17 +48,6 @@ class Message implements \IteratorAggregate,\ArrayAccess
$this->packets = $packets; $this->packets = $packets;
} }
public function __toString()
{
$result = '';
foreach ($this as $p) {
$result .= (string)$p;
}
return $result;
}
/** /**
* @see http://tools.ietf.org/html/rfc4880#section-4.1 * @see http://tools.ietf.org/html/rfc4880#section-4.1
* @see http://tools.ietf.org/html/rfc4880#section-4.2 * @see http://tools.ietf.org/html/rfc4880#section-4.2
@ -198,6 +187,17 @@ class Message implements \IteratorAggregate,\ArrayAccess
return $final_sigs; return $final_sigs;
} }
public function to_bytes(): string
{
$bytes = '';
foreach ($this as $p) {
$bytes .= $p->to_bytes();
}
return $bytes;
}
/** /**
* Function to extract verified signatures * Function to extract verified signatures
* *

View File

@ -11,7 +11,7 @@ class ModificationDetectionCodePacket extends Packet
{ {
protected $tag = 19; protected $tag = 19;
protected function header_and_body(): array function header_and_body(): array
{ {
// Get body first, we will need it's length // Get body first, we will need it's length
$body = $this->body(); $body = $this->body();

View File

@ -40,13 +40,6 @@ abstract class Packet
63 => 'Experimental', // Private or Experimental Values 63 => 'Experimental', // Private or Experimental Values
]; ];
public function __toString()
{
$data = $this->header_and_body();
return $data['header'].$data['body'];
}
static function class_for($tag) static function class_for($tag)
{ {
return (isset(self::$tags[$tag]) AND class_exists($class='Leenooks\OpenPGP\\'.self::$tags[$tag].'Packet')) return (isset(self::$tags[$tag]) AND class_exists($class='Leenooks\OpenPGP\\'.self::$tags[$tag].'Packet'))
@ -221,17 +214,13 @@ abstract class Packet
{ {
} }
protected function header_and_body(): array function header_and_body(): array
{ {
$body = $this->body(); // Get body first, we will need it's length
$size = chr(255).pack('N',strlen($body)); // Use 5-octet lengths
$tag = chr($this->tag|0xC0); // First two bits are 1 for new packet format $tag = chr($this->tag|0xC0); // First two bits are 1 for new packet format
return ['header'=>$tag.$this->size(),'body'=>$this->body()]; return ['header'=>$tag.$size,'body'=>$body];
}
protected function size(): string
{
// Use 5-octet lengths
return chr(255).pack('N',strlen($this->body()));
} }
public function tag(): int public function tag(): int
@ -239,6 +228,13 @@ abstract class Packet
return $this->tag; return $this->tag;
} }
function to_bytes()
{
$data = $this->header_and_body();
return $data['header'].$data['body'];
}
/** /**
* @see http://tools.ietf.org/html/rfc4880#section-3.5 * @see http://tools.ietf.org/html/rfc4880#section-3.5
*/ */

View File

@ -38,15 +38,9 @@ class PublicKeyPacket extends Packet
function __construct($key=[],$algorithm='RSA',$timestamp=NULL,$version=4) function __construct($key=[],$algorithm='RSA',$timestamp=NULL,$version=4)
{ {
if (self::$DEBUG)
dump(['CREATE'=>__METHOD__,'key'=>$key,'alg'=>$algorithm,'ts'=>$timestamp,'version'=>$version]);
parent::__construct(); parent::__construct();
if ($key instanceof PublicKeyPacket) { if ($key instanceof PublicKeyPacket) {
if (self::$DEBUG)
dump('key is PublicKeyPacket');
$this->algorithm = $key->algorithm; $this->algorithm = $key->algorithm;
$this->key = array(); $this->key = array();
@ -62,9 +56,6 @@ class PublicKeyPacket extends Packet
$this->v3_days_of_validity = $key->v3_days_of_validity; $this->v3_days_of_validity = $key->v3_days_of_validity;
} else { } else {
if (self::$DEBUG)
dump(['key'=>$key]);
$this->key = $key; $this->key = $key;
if (is_string($this->algorithm = $algorithm)) { if (is_string($this->algorithm = $algorithm)) {
$this->algorithm = array_search($this->algorithm,self::$algorithms); $this->algorithm = array_search($this->algorithm,self::$algorithms);

View File

@ -43,7 +43,7 @@ class S2K
return $s2k; return $s2k;
} }
function __toString() function to_bytes()
{ {
$bytes = chr($this->type); $bytes = chr($this->type);

View File

@ -29,7 +29,7 @@ class SecretKeyPacket extends PublicKeyPacket
$secret_material = NULL; $secret_material = NULL;
if($this->s2k_useage == 255 || $this->s2k_useage == 254) { if($this->s2k_useage == 255 || $this->s2k_useage == 254) {
$bytes .= chr($this->symmetric_algorithm); $bytes .= chr($this->symmetric_algorithm);
$bytes .= (string)$this->s2k; $bytes .= $this->s2k->to_bytes();
} }
if($this->s2k_useage > 0) { if($this->s2k_useage > 0) {
$bytes .= $this->encrypted_data; $bytes .= $this->encrypted_data;

View File

@ -143,7 +143,7 @@ class SignaturePacket extends Packet
$unhashed_subpackets = ''; $unhashed_subpackets = '';
foreach((array)$this->unhashed_subpackets as $p) { foreach((array)$this->unhashed_subpackets as $p) {
$unhashed_subpackets .= (string)$p; $unhashed_subpackets .= $p->to_bytes();
} }
$body .= pack('n',strlen($unhashed_subpackets)).$unhashed_subpackets; $body .= pack('n',strlen($unhashed_subpackets)).$unhashed_subpackets;
@ -164,7 +164,7 @@ class SignaturePacket extends Packet
$hashed_subpackets = ''; $hashed_subpackets = '';
foreach((array)$this->hashed_subpackets as $p) { foreach((array)$this->hashed_subpackets as $p) {
$hashed_subpackets .= (string)$p; $hashed_subpackets .= $p->to_bytes();
} }
$body .= pack('n',strlen($hashed_subpackets)).$hashed_subpackets; $body .= pack('n',strlen($hashed_subpackets)).$hashed_subpackets;

View File

@ -11,14 +11,12 @@ class EmbeddedSignaturePacket extends SignaturePacket
{ {
protected $tag = 32; protected $tag = 32;
protected function header_and_body(): array function header_and_body(): array
{ {
return ['header'=>$this->size().chr($this->tag),'body'=>$this->body()]; $body = $this->body(); // Get body first, we will need it's length
} $size = chr(255).pack('N',strlen($body)+1); // Use 5-octet lengths + 1 for tag as first packet body octet
$tag = chr($this->tag);
protected function size(): string return ['header'=>$size.$tag,'body'=>$body];
{
// Use 5-octet lengths + 1 for tag as first packet body octet
return chr(255).pack('N',strlen($this->body())+1);
} }
} }

View File

@ -9,6 +9,11 @@ class PolicyURIPacket extends Subpacket
{ {
protected $tag = 26; protected $tag = 26;
function body()
{
return $this->data;
}
function read() function read()
{ {
$this->data = $this->input; $this->data = $this->input;

View File

@ -9,6 +9,11 @@ class PreferredKeyServerPacket extends Subpacket
{ {
protected $tag = 24; protected $tag = 24;
function body()
{
return $this->data;
}
function read() function read()
{ {
$this->data = $this->input; $this->data = $this->input;

View File

@ -9,6 +9,11 @@ class SignersUserIDPacket extends Subpacket
{ {
protected $tag = 28; protected $tag = 28;
function body()
{
return $this->data;
}
function read() function read()
{ {
$this->data = $this->input; $this->data = $this->input;

View File

@ -9,9 +9,18 @@ class Subpacket extends Packet
{ {
protected $tag = NULL; protected $tag = NULL;
protected function header_and_body(): array function body()
{ {
return ['header'=>$this->size().chr($this->tag),'body'=>$this->body()]; return $this->data;
}
function header_and_body(): array
{
$body = $this->body(); // Get body first, we will need it's length
$size = chr(255).pack('N',strlen($body)+1); // Use 5-octet lengths + 1 for tag as first packet body octet
$tag = chr($this->tag);
return ['header'=>$size.$tag,'body'=>$body];
} }
/* Defaults for unsupported packets */ /* Defaults for unsupported packets */
@ -27,10 +36,4 @@ class Subpacket extends Packet
$this->tag = $tag; $this->tag = $tag;
} }
protected function size(): string
{
// Use 5-octet lengths + 1 for tag as first packet body octet
return chr(255).pack('N',strlen($this->body())+1);
}
} }

View File

@ -23,7 +23,8 @@ class SymmetricSessionKeyPacket extends Packet
function body() function body()
{ {
return chr($this->version).chr($this->symmetric_algorithm).$this->s2k.$this->encrypted_data; return chr($this->version) . chr($this->symmetric_algorithm) .
$this->s2k->to_bytes() . $this->encrypted_data;
} }
function read() function read()

View File

@ -28,7 +28,7 @@ class UserIDPacket extends Packet
} }
} }
public function display() function __toString()
{ {
$text = []; $text = [];
@ -41,7 +41,7 @@ class UserIDPacket extends Packet
function body() function body()
{ {
return ''.$this->display(); // Convert to string is the body return ''.$this; // Convert to string is the body
} }
function read() function read()

View File

@ -4,9 +4,10 @@ use Leenooks\OpenPGP;
class MessageVerification extends PHPUnit\Framework\TestCase { class MessageVerification extends PHPUnit\Framework\TestCase {
public function oneMessageRSA($pkey, $path) { public function oneMessageRSA($pkey, $path) {
$pkeyM = OpenPGP::load(file_get_contents(dirname(__FILE__).'/data/'.$pkey)); $pkeyM = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $pkey));
$m = OpenPGP::load(file_get_contents(dirname(__FILE__).'/data/'.$path)); $m = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path));
$this->assertSame($pkeyM->verify($m->key()), $m->signatures()); $verify = new OpenPGP\Crypt\RSA($pkeyM);
$this->assertSame($verify->verify($m), $m->signatures());
} }
public function testUncompressedOpsRSA() { public function testUncompressedOpsRSA() {
@ -29,7 +30,7 @@ class MessageVerification extends PHPUnit\Framework\TestCase {
$wkey = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); $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')); $data = new OpenPGP\LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));
$sign = new OpenPGP\Crypt\RSA($wkey); $sign = new OpenPGP\Crypt\RSA($wkey);
$m = (string)$sign->sign($data); $m = $sign->sign($data)->to_bytes();
$reparsedM = OpenPGP\Message::parse($m); $reparsedM = OpenPGP\Message::parse($m);
$this->assertSame($sign->verify($reparsedM), $reparsedM->signatures()); $this->assertSame($sign->verify($reparsedM), $reparsedM->signatures());
} }
@ -157,7 +158,7 @@ class Encryption extends PHPUnit\Framework\TestCase {
public function oneSymmetric($algorithm) { public function oneSymmetric($algorithm) {
$data = new OpenPGP\LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $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\Crypt\Symmetric::encrypt('secret', new OpenPGP\Message(array($data)), $algorithm);
$encrypted = OpenPGP\Message::parse((string)$encrypted); $encrypted = OpenPGP\Message::parse($encrypted->to_bytes());
$decrypted = OpenPGP\Crypt\Symmetric::decryptSymmetric('secret', $encrypted); $decrypted = OpenPGP\Crypt\Symmetric::decryptSymmetric('secret', $encrypted);
$this->assertEquals($decrypted[0]->data, 'This is text.'); $this->assertEquals($decrypted[0]->data, 'This is text.');
} }
@ -196,7 +197,7 @@ class Encryption extends PHPUnit\Framework\TestCase {
$key = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); $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')); $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\Crypt\Symmetric::encrypt($key, new OpenPGP\Message(array($data)));
$encrypted = OpenPGP\Message::parse((string)$encrypted); $encrypted = OpenPGP\Message::parse($encrypted->to_bytes());
$decryptor = new OpenPGP\Crypt\RSA($key); $decryptor = new OpenPGP\Crypt\RSA($key);
$decrypted = $decryptor->decrypt($encrypted); $decrypted = $decryptor->decrypt($encrypted);
$this->assertEquals($decrypted[0]->data, 'This is text.'); $this->assertEquals($decrypted[0]->data, 'This is text.');

View File

@ -5,7 +5,7 @@ use Leenooks\OpenPGP;
class Serialization extends PHPUnit\Framework\TestCase { class Serialization extends PHPUnit\Framework\TestCase {
public function oneSerialization($path) { public function oneSerialization($path) {
$in = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path)); $in = OpenPGP\Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path));
$mid = (string)$in; $mid = $in->to_bytes();
$out = OpenPGP\Message::parse($mid); $out = OpenPGP\Message::parse($mid);
$this->assertEquals($in, $out); $this->assertEquals($in, $out);
} }