10 require
'Crypt/RSA.php';
12 require_once dirname(__FILE__).
'/openpgp.php';
13 @include_once dirname(__FILE__).
'/openpgp_crypt_symmetric.php';
24 $this->message = $packet;
28 function key($keyid=NULL) {
29 if(!$this->
key)
return NULL;
31 foreach($this->
key as $p) {
33 if(!$keyid || strtoupper(substr($p->fingerprint, strlen($keyid)*-1)) == strtoupper($keyid))
return $p;
42 return self::convert_public_key($this->
key($keyid));
47 return self::convert_private_key($this->
key($keyid));
57 $verifier =
function($m, $s) use($self) {
58 $key = $self->public_key($s->issuer());
59 if(!
$key)
return false;
60 $key->setHash(strtolower($s->hash_algorithm_name()));
61 return $key->verify($m, reset($s->data));
64 if(!($packet instanceof Crypt_RSA)) {
65 $packet =
new self($packet);
69 $verifier =
function($m, $s) use($self, $packet) {
70 if(!($packet instanceof Crypt_RSA)) {
71 $key = $packet->public_key($s->issuer());
73 if(!
$key)
return false;
74 $key->setHash(strtolower($s->hash_algorithm_name()));
75 return $key->verify($m, reset($s->data));
79 return $m->verified_signatures(array(
'RSA' => array(
82 'SHA224' => $verifier,
83 'SHA256' => $verifier,
84 'SHA384' => $verifier,
92 function sign($packet, $hash=
'SHA256', $keyid=NULL) {
93 if(!is_object($packet)) {
117 if(!(
$key instanceof Crypt_RSA)) {
119 if(!$keyid) $keyid = substr(
$key->key()->fingerprint, -16, 16);
120 $key = $key->private_key($keyid);
122 $key->setHash(strtolower($hash));
126 $sig->sign_data(array(
'RSA' => array($hash =>
function($data) use(
$key) {
return array(
$key->sign($data));})));
128 return new OpenPGP_Message(array($sig,
$message));
134 if(is_array($packet)) {
136 }
else if(!is_object($packet)) {
141 if(!
$key || !$packet)
return NULL;
143 if(!$keyid) $keyid = substr($this->
key->fingerprint, -16);
144 $key->setHash(strtolower($hash));
147 foreach($packet as $p) {
152 $sig->signature_type = 0x13;
158 $sig->sign_data(array(
'RSA' => array($hash =>
function($data) use(
$key) {
return array(
$key->sign($data));})));
175 if(!$keys || !
$message)
return NULL;
177 if(!($keys instanceof Crypt_RSA)) {
178 $keys =
new self($keys);
183 if($keys instanceof Crypt_RSA) {
184 $sk = self::try_decrypt_session($keys, substr($p->encyrpted_data, 2));
185 }
else if(strlen(str_replace(
'0',
'', $p->keyid)) < 1) {
186 foreach($keys->key as $k) {
187 $sk = self::try_decrypt_session(self::convert_private_key($k), substr($p->encyrpted_data, 2));
191 $key = $keys->private_key($p->keyid);
192 $sk = self::try_decrypt_session(
$key, substr($p->encrypted_data, 2));
206 $key->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
207 $data =
$key->decrypt($edata);
208 $sk = substr($data, 1, strlen($data)-3);
209 $chk = unpack(
'n', substr($data, -2));
213 for($i = 0; $i < strlen($sk); $i++) {
214 $sk_chk = ($sk_chk + ord($sk{$i})) % 65536;
217 if($sk_chk != $chk)
return NULL;
218 return array(ord($data{0}), $sk);
222 $rsa =
new Crypt_RSA();
223 $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
224 $rsa->setHash(strtolower($hash));
225 $rsa->modulus =
new Math_BigInteger($mod, 256);
226 $rsa->k = strlen($rsa->modulus->toBytes());
227 $rsa->exponent =
new Math_BigInteger($exp, 256);
228 $rsa->setPublicKey();
236 $mod = $packet->key[
'n'];
237 $exp = $packet->key[
'e'];
238 if($private) $exp = $packet->key[
'd'];
239 if(!$exp)
return NULL;
241 $rsa = self::crypt_rsa_key($mod, $exp);
244 if($packet->key[
'p'] && $packet->key[
'q']) $rsa->primes = array($packet->key[
'p'], $packet->key[
'q']);
245 if($packet->key[
'u']) $rsa->coefficients = array($packet->key[
'u']);
252 return self::convert_key($packet,
false);
256 return self::convert_key($packet,
true);