2010-04-01 17:53:39 +00:00
|
|
|
<?php
|
|
|
|
// This is free and unencumbered software released into the public domain.
|
|
|
|
/**
|
|
|
|
* OpenPGP_Crypt_RSA.php is a wrapper for using the classes from OpenPGP.php with Crypt_RSA
|
|
|
|
*
|
|
|
|
* @package OpenPGP
|
|
|
|
* @version 0.0.1
|
|
|
|
* @author Stephen Paul Weber <singpolyma@singpolyma.net>
|
|
|
|
* @link http://github.com/singpolyma/openpgp-php
|
|
|
|
*/
|
|
|
|
|
|
|
|
// From http://phpseclib.sourceforge.net/
|
|
|
|
require 'Crypt/RSA.php';
|
|
|
|
|
2013-01-26 19:00:00 +00:00
|
|
|
require_once dirname(__FILE__).'/openpgp.php';
|
2013-09-14 16:36:58 +00:00
|
|
|
@include_once dirname(__FILE__).'/openpgp_crypt_symmetric.php'; /* For encrypt/decrypt */
|
2013-01-26 19:00:00 +00:00
|
|
|
|
2010-04-01 17:53:39 +00:00
|
|
|
class OpenPGP_Crypt_RSA {
|
|
|
|
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);
|
2010-04-01 23:03:42 +00:00
|
|
|
if($packet instanceof OpenPGP_PublicKeyPacket || $packet[0] instanceof OpenPGP_PublicKeyPacket) { // If it's a key (other keys are subclasses of this one)
|
2010-04-01 17:53:39 +00:00
|
|
|
$this->key = $packet;
|
|
|
|
} else {
|
|
|
|
$this->message = $packet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-01 23:03:42 +00:00
|
|
|
function key($keyid=NULL) {
|
2010-04-01 17:53:39 +00:00
|
|
|
if(!$this->key) return NULL; // No key
|
2010-04-01 23:03:42 +00:00
|
|
|
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));
|
2010-04-01 17:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get Crypt_RSA for the private key
|
2010-04-01 23:03:42 +00:00
|
|
|
function private_key($keyid=NULL) {
|
|
|
|
return self::convert_private_key($this->key($keyid));
|
2010-04-01 17:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2013-02-24 23:11:22 +00:00
|
|
|
function verify($packet) {
|
2013-01-21 02:44:33 +00:00
|
|
|
$self = $this; // For old PHP
|
2010-04-01 17:53:39 +00:00
|
|
|
if(!is_object($packet)) $packet = OpenPGP_Message::parse($packet);
|
2013-01-21 02:44:33 +00:00
|
|
|
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));
|
|
|
|
};
|
2010-04-01 17:53:39 +00:00
|
|
|
} else {
|
2010-04-01 23:03:42 +00:00
|
|
|
if(!($packet instanceof Crypt_RSA)) {
|
|
|
|
$packet = new self($packet);
|
|
|
|
}
|
2013-01-21 02:44:33 +00:00
|
|
|
|
|
|
|
$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));
|
|
|
|
};
|
2010-04-01 17:53:39 +00:00
|
|
|
}
|
2013-01-21 02:44:33 +00:00
|
|
|
|
|
|
|
return $m->verified_signatures(array('RSA' => array(
|
|
|
|
'MD5' => $verifier,
|
|
|
|
'SHA1' => $verifier,
|
|
|
|
'SHA224' => $verifier,
|
|
|
|
'SHA256' => $verifier,
|
|
|
|
'SHA384' => $verifier,
|
|
|
|
'SHA512' => $verifier
|
|
|
|
)));
|
2010-04-01 17:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2010-04-01 23:03:42 +00:00
|
|
|
function sign($packet, $hash='SHA256', $keyid=NULL) {
|
2010-04-01 17:53:39 +00:00
|
|
|
if(!is_object($packet)) {
|
|
|
|
if($this->key) {
|
|
|
|
$packet = new OpenPGP_LiteralDataPacket($packet);
|
|
|
|
} else {
|
|
|
|
$packet = OpenPGP_Message::parse($packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-01 23:03:42 +00:00
|
|
|
if($packet instanceof OpenPGP_SecretKeyPacket || $packet instanceof Crypt_RSA
|
|
|
|
|| ($packet instanceof ArrayAccess && $packet[0] instanceof OpenPGP_SecretKeyPacket)) {
|
2010-04-01 17:53:39 +00:00
|
|
|
$key = $packet;
|
|
|
|
$message = $this->message;
|
|
|
|
} else {
|
2010-04-01 23:03:42 +00:00
|
|
|
$key = $this->key;
|
2010-04-01 17:53:39 +00:00
|
|
|
$message = $packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$key || !$message) return NULL; // Missing some data
|
|
|
|
|
|
|
|
if($message instanceof OpenPGP_Message) {
|
2013-01-21 03:15:49 +00:00
|
|
|
$sign = $message->signatures();
|
|
|
|
$message = $sign[0][0];
|
2010-04-01 17:53:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 23:03:42 +00:00
|
|
|
if(!($key instanceof Crypt_RSA)) {
|
|
|
|
$key = new self($key);
|
|
|
|
if(!$keyid) $keyid = substr($key->key()->fingerprint, -16, 16);
|
|
|
|
$key = $key->private_key($keyid);
|
|
|
|
}
|
2010-04-01 17:53:39 +00:00
|
|
|
$key->setHash(strtolower($hash));
|
|
|
|
|
|
|
|
$sig = new OpenPGP_SignaturePacket($message, 'RSA', strtoupper($hash));
|
2010-04-01 23:03:42 +00:00
|
|
|
$sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket($keyid);
|
2013-01-21 03:15:49 +00:00
|
|
|
$sig->sign_data(array('RSA' => array($hash => function($data) use($key) {return array($key->sign($data));})));
|
2010-04-01 17:53:39 +00:00
|
|
|
|
|
|
|
return new OpenPGP_Message(array($sig, $message));
|
|
|
|
}
|
|
|
|
|
2011-07-25 20:15:17 +00:00
|
|
|
// Pass a message with a key and userid packet to sign
|
|
|
|
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);
|
|
|
|
if(!$key || !$packet) return NULL; // Missing some data
|
|
|
|
|
|
|
|
if(!$keyid) $keyid = substr($this->key->fingerprint, -16);
|
|
|
|
$key->setHash(strtolower($hash));
|
|
|
|
|
|
|
|
$sig = $packet->signature_and_data();
|
|
|
|
$sig = $sig[1];
|
|
|
|
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(array('RSA' => array($hash => array($key, 'sign'))));
|
|
|
|
|
|
|
|
return $packet;
|
|
|
|
}
|
|
|
|
|
2013-01-26 19:00:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$keys || !$message) return NULL; // Missing some data
|
|
|
|
|
|
|
|
if(!($keys instanceof Crypt_RSA)) {
|
|
|
|
$keys = new self($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($message as $p) {
|
|
|
|
if($p instanceof OpenPGP_AsymmetricSessionKeyPacket) {
|
|
|
|
if($keys instanceof Crypt_RSA) {
|
2013-06-24 16:21:26 +00:00
|
|
|
$sk = self::try_decrypt_session($keys, substr($p->encyrpted_data, 2));
|
2013-01-26 19:00:00 +00:00
|
|
|
} else if(strlen(str_replace('0', '', $p->keyid)) < 1) {
|
|
|
|
foreach($keys->key as $k) {
|
2013-06-24 16:21:26 +00:00
|
|
|
$sk = self::try_decrypt_session(self::convert_private_key($k), substr($p->encyrpted_data, 2));
|
2013-01-26 19:00:00 +00:00
|
|
|
if($sk) break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$key = $keys->private_key($p->keyid);
|
|
|
|
$sk = self::try_decrypt_session($key, substr($p->encrypted_data, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$sk) continue;
|
|
|
|
|
2013-09-14 16:36:58 +00:00
|
|
|
$r = OpenPGP_Crypt_Symmetric::decryptPacket(OpenPGP_Crypt_Symmetric::getEncryptedData($message), $sk[0], $sk[1]);
|
2013-01-26 19:00:00 +00:00
|
|
|
if($r) return $r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL; /* Failed */
|
|
|
|
}
|
|
|
|
|
|
|
|
static function try_decrypt_session($key, $edata) {
|
|
|
|
$key->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
|
|
|
|
$data = $key->decrypt($edata);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2010-04-01 17:53:39 +00:00
|
|
|
static function crypt_rsa_key($mod, $exp, $hash='SHA256') {
|
|
|
|
$rsa = new Crypt_RSA();
|
2013-01-21 00:00:49 +00:00
|
|
|
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
|
2010-04-01 17:53:39 +00:00
|
|
|
$rsa->setHash(strtolower($hash));
|
|
|
|
$rsa->modulus = new Math_BigInteger($mod, 256);
|
|
|
|
$rsa->k = strlen($rsa->modulus->toBytes());
|
|
|
|
$rsa->exponent = new Math_BigInteger($exp, 256);
|
2013-01-21 00:00:49 +00:00
|
|
|
$rsa->setPublicKey();
|
2010-04-01 17:53:39 +00:00
|
|
|
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'];
|
|
|
|
if(!$exp) return NULL; // Packet doesn't have needed data
|
|
|
|
|
|
|
|
$rsa = self::crypt_rsa_key($mod, $exp);
|
|
|
|
|
|
|
|
if($private) {
|
|
|
|
if($packet->key['p'] && $packet->key['q']) $rsa->primes = array($packet->key['p'], $packet->key['q']);
|
|
|
|
if($packet->key['u']) $rsa->coefficients = array($packet->key['u']);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|