Move to more PSR-4 standards.

This commit is contained in:
Deon George
2020-06-07 16:25:59 +10:00
parent d458fecff6
commit 7d259b251f
56 changed files with 3247 additions and 4292 deletions

356
lib/OpenPgP/Crypt/RSA.php Normal file
View File

@@ -0,0 +1,356 @@
<?php
/**
* OpenPGP_Crypt_RSA.php is a wrapper for using the classes from OpenPGP.php with Crypt_RSA
*
* @package OpenPGP
*/
namespace Leenooks\OpenPGP\Crypt;
use Leenooks\OpenPGP;
// From http://phpseclib.sourceforge.net/
use phpseclib\Crypt\RSA as Crypt_RSA;
use phpseclib\Math\BigInteger as Math_BigInteger;
define('CRYPT_RSA_ENCRYPTION_PKCS1', Crypt_RSA::ENCRYPTION_PKCS1);
define('CRYPT_RSA_SIGNATURE_PKCS1', Crypt_RSA::SIGNATURE_PKCS1);
class RSA
{
protected static $DEBUG = FALSE;
protected $key,$message;
// Construct a wrapper object from a key or a message packet
function __construct($packet)
{
if (!is_object($packet))
$packet = OpenPGP\Message::parse($packet);
// If it's a key (other keys are subclasses of this one)
if ($packet instanceof OpenPGP\PublicKeyPacket || $packet[0] instanceof OpenPGP\PublicKeyPacket) {
$this->key = $packet;
} else {
$this->message = $packet;
}
}
function key($keyid=NULL)
{
// No key
if (! $this->key)
return NULL;
if ($this->key instanceof OpenPGP\Message) {
foreach ($this->key as $p) {
if ($p instanceof OpenPGP\PublicKeyPacket) {
if (!$keyid || strtoupper(substr($p->fingerprint,strlen($keyid)*-1)) == strtoupper($keyid))
return $p;
}
}
}
return $this->key;
}
// Get Crypt_RSA for the public key
function public_key($keyid=NULL)
{
return self::convert_public_key($this->key($keyid));
}
// Get Crypt_RSA for the private key
function private_key($keyid=NULL)
{
return self::convert_private_key($this->key($keyid));
}
// Pass a message to verify with this key, or a key (OpenPGP or Crypt_RSA) to check this message with
// Second optional parameter to specify which signature to verify (if there is more than one)
function verify($packet)
{
if (static::$DEBUG)
dump(['In METHOD: '=>__METHOD__,'packet'=>$packet]);
$self = $this; // For old PHP
if (! is_object($packet))
$packet = OpenPGP\Message::parse($packet);
if (! $this->message) {
$m = $packet;
$verifier = function($m, $s) use($self) {
$key = $self->public_key($s->issuer());
if(!$key) return false;
$key->setHash(strtolower($s->hash_algorithm_name()));
return $key->verify($m, reset($s->data));
};
} else {
if(!($packet instanceof Crypt_RSA)) {
$packet = new self($packet);
}
$m = $this->message;
$verifier = function($m, $s) use($self, $packet) {
if(!($packet instanceof Crypt_RSA)) {
$key = $packet->public_key($s->issuer());
}
if(!$key) return false;
$key->setHash(strtolower($s->hash_algorithm_name()));
return $key->verify($m, reset($s->data));
};
}
return $m->verified_signatures(array('RSA' => array(
'MD5' => $verifier,
'SHA1' => $verifier,
'SHA224' => $verifier,
'SHA256' => $verifier,
'SHA384' => $verifier,
'SHA512' => $verifier
)));
}
// Pass a message to sign with this key, or a secret key to sign this message with
// Second parameter is hash algorithm to use (default SHA256)
// Third parameter is the 16-digit key ID to use... defaults to the key id in the key packet
function sign($packet,$hash='SHA256',$keyid=NULL)
{
if (! is_object($packet)) {
if($this->key) {
$packet = new OpenPGP\LiteralDataPacket($packet);
} else {
$packet = OpenPGP\Message::parse($packet);
}
}
if ($packet instanceof OpenPGP\SecretKeyPacket
|| $packet instanceof Crypt_RSA
|| ($packet instanceof ArrayAccess && $packet[0] instanceof OpenPGP\SecretKeyPacket))
{
$key = $packet;
$message = $this->message;
} else {
$key = $this->key;
$message = $packet;
}
// Missing some data
if (! $key || !$message)
return NULL;
if ($message instanceof OpenPGP\Message) {
$sign = $message->signatures();
$message = $sign[0][0];
}
if (!($key instanceof Crypt_RSA)) {
$key = new self($key);
if (! $keyid)
$keyid = substr($key->key()->fingerprint,-16,16);
$key = $key->private_key($keyid);
}
$key->setHash(strtolower($hash));
$sig = new SignaturePacket($message,'RSA',strtoupper($hash));
$sig->hashed_subpackets[] = new OpenPGP\SignaturePacket\IssuerPacket($keyid);
$sig->sign_data(['RSA'=>[$hash => function($data) use($key) {return [$key->sign($data)];}]]);
return new OpenPGP\Message(array($sig, $message));
}
/** Pass a message with a key and userid packet to sign */
// TODO: merge this with the normal sign function
function sign_key_userid($packet,$hash='SHA256',$keyid=NULL)
{
if (is_array($packet)) {
$packet = new OpenPGP\Message($packet);
} else if(!is_object($packet)) {
$packet = OpenPGP\Message::parse($packet);
}
$key = $this->private_key($keyid);
// Missing some data
if (! $key || ! $packet)
return NULL;
if (! $keyid)
$keyid = substr($this->key->fingerprint,-16);
$key->setHash(strtolower($hash));
$sig = NULL;
foreach($packet as $p) {
if ($p instanceof OpenPGP\SignaturePacket)
$sig = $p;
}
if (! $sig) {
$sig = new OpenPGP\SignaturePacket($packet,'RSA',strtoupper($hash));
$sig->signature_type = 0x13;
$sig->hashed_subpackets[] = new OpenPGP\SignaturePacket\KeyFlagsPacket(array(0x01|0x02));
$sig->hashed_subpackets[] = new OpenPGP\SignaturePacket\IssuerPacket($keyid);
$packet[] = $sig;
}
$sig->sign_data(['RSA'=>[$hash => function($data) use($key) {return [$key->sign($data)];}]]);
return $packet;
}
function decrypt($packet)
{
if (! is_object($packet))
$packet = OpenPGP\Message::parse($packet);
if ($packet instanceof OpenPGP\SecretKeyPacket
|| $packet instanceof Crypt_RSA
|| ($packet instanceof \ArrayAccess && $packet[0] instanceof OpenPGP\SecretKeyPacket))
{
$keys = $packet;
$message = $this->message;
} else {
$keys = $this->key;
$message = $packet;
}
// Missing some data
if (! $keys || ! $message)
return NULL;
if (! ($keys instanceof Crypt_RSA)) {
$keys = new self($keys);
}
if (static::$DEBUG)
dump([__METHOD__=>['keys'=>$keys,'message'=>$message]]);
foreach ($message as $p) {
if (static::$DEBUG)
dump(['p'=>$p,'test'=> ($p instanceof OpenPGP\AsymmetricSessionKeyPacket) ]);
if ($p instanceof OpenPGP\AsymmetricSessionKeyPacket) {
if (static::$DEBUG)
dump(['keys'=>$keys,'test'=>($keys instanceof Crypt_RSA)]);
if($keys instanceof Crypt_RSA) {
$sk = self::try_decrypt_session($keys,substr($p->encrypted_data,2));
} elseif (strlen(str_replace('0','',$p->keyid)) < 1) {
foreach($keys->key as $k) {
$sk = self::try_decrypt_session(self::convert_private_key($k), substr($p->encrypted_data, 2));
if ($sk)
break;
}
} else {
$key = $keys->private_key($p->keyid);
$sk = self::try_decrypt_session($key,substr($p->encrypted_data,2));
if (static::$DEBUG)
dump(['c'=>$p->keyid,'key'=>$key,'sk'=>$sk]);
}
if (! $sk)
continue;
$r = Symmetric::decryptPacket(Symmetric::getEncryptedData($message),$sk[0],$sk[1]);
if ($r)
return $r;
}
}
return NULL; /* Failed */
}
static function try_decrypt_session($key,$edata)
{
$key->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$data = @$key->decrypt($edata);
if (! $data)
return NULL;
$sk = substr($data,1,strlen($data)-3);
$chk = unpack('n',substr($data,-2));
$chk = reset($chk);
$sk_chk = 0;
for ($i=0;$i<strlen($sk);$i++) {
$sk_chk = ($sk_chk+ord($sk{$i}))%65536;
}
if ($sk_chk != $chk)
return NULL;
return array(ord($data{0}),$sk);
}
static function crypt_rsa_key($mod,$exp,$hash='SHA256')
{
$rsa = new Crypt_RSA();
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$rsa->setHash(strtolower($hash));
$rsa->modulus = new Math_BigInteger($mod,256);
$rsa->k = strlen($rsa->modulus->toBytes());
$rsa->exponent = new Math_BigInteger($exp,256);
$rsa->setPublicKey();
return $rsa;
}
static function convert_key($packet,$private=false)
{
if (! is_object($packet))
$packet = OpenPGP\Message::parse($packet);
if ($packet instanceof OpenPGP\Message)
$packet = $packet[0];
$mod = $packet->key['n'];
$exp = $packet->key['e'];
if ($private)
$exp = $packet->key['d'];
// Packet doesn't have needed data
if (! $exp)
return NULL;
$rsa = self::crypt_rsa_key($mod,$exp);
if ($private) {
/**
* @see https://github.com/phpseclib/phpseclib/issues/1113
* Primes and coefficients now use BigIntegers.
**/
//set the primes
if ($packet->key['p'] && $packet->key['q'])
$rsa->primes = [
1 => new Math_BigInteger($packet->key['p'], 256),
2 => new Math_BigInteger($packet->key['q'], 256)
];
// set the coefficients
if ($packet->key['u'])
$rsa->coefficients = [2=>new Math_BigInteger($packet->key['u'],256)];
}
return $rsa;
}
static function convert_public_key($packet)
{
return self::convert_key($packet, false);
}
static function convert_private_key($packet)
{
return self::convert_key($packet, true);
}
}

View File

@@ -0,0 +1,328 @@
<?php
namespace Leenooks\OpenPGP\Crypt;
use phpseclib\Crypt\AES as Crypt_AES;
use phpseclib\Crypt\Blowfish as Crypt_Blowfish;
use phpseclib\Crypt\TripleDES as Crypt_TripleDES;
use phpseclib\Crypt\Twofish as Crypt_Twofish;
use phpseclib\Crypt\Random;
use Leenooks\OpenPGP;
class Symmetric
{
protected static $DEBUG = FALSE;
public static function encrypt($passphrases_and_keys,$message,$symmetric_algorithm=9): OpenPGP\Message
{
if (static::$DEBUG)
dump(['In METHOD: '=>__METHOD__,'passphrases_and_keys'=>$passphrases_and_keys,'symmetric_algorithm'=>$symmetric_algorithm]);
list($cipher,$key_bytes,$key_block_bytes) = self::getCipher($symmetric_algorithm);
if (static::$DEBUG)
dump(['cipher'=>$cipher,'key_bytes'=>$key_bytes,'key_block_bytes'=>$key_block_bytes]);
if (! $cipher)
throw new Exception("Unsupported cipher");
$prefix = Random::string($key_block_bytes);
$prefix .= substr($prefix, -2);
$key = Random::string($key_bytes);
$cipher->setKey($key);
$to_encrypt = $prefix.$message->to_bytes();
$mdc = new OpenPGP\ModificationDetectionCodePacket(hash('sha1',$to_encrypt."\xD3\x14",true));
$to_encrypt .= $mdc->to_bytes();
if (static::$DEBUG)
dump(['to_encrypt'=>$to_encrypt]);
$encrypted = [new OpenPGP\IntegrityProtectedDataPacket($cipher->encrypt($to_encrypt))];
if (static::$DEBUG)
dump(['encrypted'=>$encrypted]);
if (! is_array($passphrases_and_keys) && ! ($passphrases_and_keys instanceof \IteratorAggregate)) {
$passphrases_and_keys = (array)$passphrases_and_keys;
}
if (static::$DEBUG)
dump(['pk'=>$passphrases_and_keys]);
foreach ($passphrases_and_keys as $pass) {
if ($pass instanceof OpenPGP\PublicKeyPacket) {
if (static::$DEBUG)
dump(['pass'=>$pass,'instanceof'=>'Leenooks\OpenPGP\PublicKeyPacket']);
if (! in_array($pass->algorithm,[1,2,3]))
throw new Exception("Only RSA keys are supported.");
$crypt_rsa = new RSA($pass);
$rsa = $crypt_rsa->public_key();
if (static::$DEBUG)
dump(['public_key'=>$rsa]);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$esk = $rsa->encrypt(chr($symmetric_algorithm).$key.pack('n', self::checksum($key)));
$esk = pack('n',OpenPGP::bitlength($esk)).$esk;
array_unshift($encrypted, new OpenPGP\AsymmetricSessionKeyPacket($pass->algorithm,$pass->fingerprint(),$esk));
} elseif (is_string($pass)) {
$s2k = new OpenPGP\S2K(Random::string(8));
$cipher->setKey($s2k->make_key($pass, $key_bytes));
$esk = $cipher->encrypt(chr($symmetric_algorithm) . $key);
array_unshift($encrypted, new OpenPGP\SymmetricSessionKeyPacket($s2k, $esk, $symmetric_algorithm));
}
}
if (static::$DEBUG)
dump(['Out METHOD: '=>__METHOD__,'encrypted'=>$encrypted,'message'=>(new OpenPGP\Message($encrypted))]);
return new OpenPGP\Message($encrypted);
}
public static function decryptSymmetric($pass,$m)
{
$epacket = self::getEncryptedData($m);
foreach ($m as $p) {
if ($p instanceof OpenPGP\SymmetricSessionKeyPacket) {
if (strlen($p->encrypted_data) > 0) {
list ($cipher,$key_bytes,$key_block_bytes) = self::getCipher($p->symmetric_algorithm);
if (! $cipher)
continue;
$cipher->setKey($p->s2k->make_key($pass, $key_bytes));
$padAmount = $key_block_bytes - (strlen($p->encrypted_data) % $key_block_bytes);
$data = substr($cipher->decrypt($p->encrypted_data . str_repeat("\0", $padAmount)), 0, strlen($p->encrypted_data));
$decrypted = self::decryptPacket($epacket, ord($data{0}), substr($data, 1));
} else {
list($cipher,$key_bytes,$key_block_bytes) = self::getCipher($p->symmetric_algorithm);
$decrypted = self::decryptPacket($epacket,$p->symmetric_algorithm,$p->s2k->make_key($pass,$key_bytes));
}
if ($decrypted)
return $decrypted;
}
}
return NULL; /* If we get here, we failed */
}
public static function encryptSecretKey($pass,$packet,$symmetric_algorithm=9)
{
$packet = clone $packet; // Do not mutate original
$packet->s2k_useage = 254;
$packet->symmetric_algorithm = $symmetric_algorithm;
list($cipher,$key_bytes,$key_block_bytes) = self::getCipher($packet->symmetric_algorithm);
if (! $cipher)
throw new Exception("Unsupported cipher");
$material = '';
foreach (OpenPGP\SecretKeyPacket::$secret_key_fields[$packet->algorithm] as $field) {
$f = $packet->key[$field];
$material .= pack('n',OpenPGP::bitlength($f)).$f;
unset($packet->key[$field]);
}
$material .= hash('sha1',$material,true);
$iv = Random::string($key_block_bytes);
if (! $packet->s2k)
$packet->s2k = new OpenPGP\S2K(Random::string(8));
$cipher->setKey($packet->s2k->make_key($pass, $key_bytes));
$cipher->setIV($iv);
$packet->encrypted_data = $iv.$cipher->encrypt($material);
return $packet;
}
public static function decryptSecretKey($pass,$packet)
{
$packet = clone $packet; // Do not mutate orinigal
list($cipher,$key_bytes,$key_block_bytes) = self::getCipher($packet->symmetric_algorithm);
if (! $cipher)
throw new Exception("Unsupported cipher");
$cipher->setKey($packet->s2k->make_key($pass, $key_bytes));
$cipher->setIV(substr($packet->encrypted_data, 0, $key_block_bytes));
$material = $cipher->decrypt(substr($packet->encrypted_data, $key_block_bytes));
if ($packet->s2k_useage == 254) {
$chk = substr($material, -20);
$material = substr($material, 0, -20);
if ($chk != hash('sha1', $material, true))
return NULL;
} else {
$chk = unpack('n', substr($material, -2));
$chk = reset($chk);
$material = substr($material, 0, -2);
$mkChk = self::checksum($material);
if ($chk != $mkChk)
return NULL;
}
$packet->s2k = NULL;
$packet->s2k_useage = 0;
$packet->symmetric_algorithm = 0;
$packet->encrypted_data = NULL;
$packet->input = $material;
$packet->key_from_input();
unset($packet->input);
return $packet;
}
public static function decryptPacket($epacket, $symmetric_algorithm, $key)
{
list($cipher,$key_bytes,$key_block_bytes) = self::getCipher($symmetric_algorithm);
if (! $cipher)
return NULL;
$cipher->setKey($key);
if ($epacket instanceof OpenPGP\IntegrityProtectedDataPacket) {
$padAmount = $key_block_bytes - (strlen($epacket->data) % $key_block_bytes);
$data = substr($cipher->decrypt($epacket->data . str_repeat("\0", $padAmount)), 0, strlen($epacket->data));
$prefix = substr($data, 0, $key_block_bytes + 2);
$mdc = substr(substr($data, -22, 22), 2);
$data = substr($data, $key_block_bytes + 2, -22);
$mkMDC = hash("sha1", $prefix . $data . "\xD3\x14", true);
if ($mkMDC !== $mdc)
return false;
try {
$msg = OpenPGP\Message::parse($data);
dump(['data'=>$data,'msg'=>$msg]);
} catch (Exception $ex) {
$msg = NULL;
}
if ($msg)
return $msg; /* Otherwise keep trying */
} else {
// No MDC mean decrypt with resync
$iv = substr($epacket->data, 2, $key_block_bytes);
$edata = substr($epacket->data, $key_block_bytes + 2);
$padAmount = $key_block_bytes - (strlen($edata) % $key_block_bytes);
$cipher->setIV($iv);
$data = substr($cipher->decrypt($edata . str_repeat("\0", $padAmount)), 0, strlen($edata));
try {
$msg = OpenPGP\Message::parse($data);
} catch (Exception $ex) {
$msg = NULL;
}
if ($msg)
return $msg; /* Otherwise keep trying */
}
return NULL; /* Failed */
}
public static function getCipher($algo) {
$cipher = NULL;
switch($algo) {
case NULL:
case 0:
throw new Exception("Data is already unencrypted");
case 2:
$cipher = new Crypt_TripleDES(Crypt_TripleDES::MODE_CFB);
$key_bytes = 24;
$key_block_bytes = 8;
break;
case 3:
if (class_exists('OpenSSLWrapper')) {
$cipher = new OpenSSLWrapper("CAST5-CFB");
} else if(defined('MCRYPT_CAST_128')) {
$cipher = new MCryptWrapper(MCRYPT_CAST_128);
}
break;
case 4:
$cipher = new Crypt_Blowfish(Crypt_Blowfish::MODE_CFB);
$key_bytes = 16;
$key_block_bytes = 8;
break;
case 7:
$cipher = new Crypt_AES(Crypt_AES::MODE_CFB);
$cipher->setKeyLength(128);
break;
case 8:
$cipher = new Crypt_AES(Crypt_AES::MODE_CFB);
$cipher->setKeyLength(192);
break;
case 9:
$cipher = new Crypt_AES(Crypt_AES::MODE_CFB);
$cipher->setKeyLength(256);
break;
case 10:
$cipher = new Crypt_Twofish(Crypt_Twofish::MODE_CFB);
if (method_exists($cipher, 'setKeyLength')) {
$cipher->setKeyLength(256);
} else {
$cipher = NULL;
}
break;
}
// Unsupported cipher
if (! $cipher)
return [NULL,NULL,NULL];
if (! isset($key_bytes))
$key_bytes = isset($cipher->key_size)?$cipher->key_size:$cipher->key_length;
if (! isset($key_block_bytes))
$key_block_bytes = $cipher->block_size;
return [$cipher,$key_bytes,$key_block_bytes];
}
public static function getEncryptedData($m)
{
foreach ($m as $p) {
if ($p instanceof OpenPGP\EncryptedDataPacket)
return $p;
}
throw new Exception("Can only decrypt EncryptedDataPacket");
}
public static function checksum($s) {
$mkChk = 0;
for($i = 0; $i < strlen($s); $i++) {
$mkChk = ($mkChk + ord($s{$i})) % 65536;
}
return $mkChk;
}
}