mirror of
https://git.friendi.ca/friendica/friendica-addons.git
synced 2025-07-08 01:18:48 +00:00
[securemail] Upgrade Composer dependencies to fix PHP8 issue
- Upgrading phpseclib/phpseclib (2.0.4 => 2.0.34) - Upgrading singpolyma/openpgp-php (0.3.0 => 0.5.0)
This commit is contained in:
parent
fb77e3c5ea
commit
0c7fd9a34d
65 changed files with 5407 additions and 3230 deletions
|
@ -5,7 +5,7 @@
|
|||
* (RFC 4880).
|
||||
*
|
||||
* @package OpenPGP
|
||||
* @version 0.3.0
|
||||
* @version 0.5.0
|
||||
* @author Arto Bendiken <arto.bendiken@gmail.com>
|
||||
* @author Stephen Paul Weber <singpolyma@singpolyma.net>
|
||||
* @see http://github.com/bendiken/openpgp-php
|
||||
|
@ -18,6 +18,8 @@
|
|||
* @see http://tools.ietf.org/html/rfc4880
|
||||
*/
|
||||
class OpenPGP {
|
||||
const VERSION = array(0, 5, 0);
|
||||
|
||||
/**
|
||||
* @see http://tools.ietf.org/html/rfc4880#section-6
|
||||
* @see http://tools.ietf.org/html/rfc4880#section-6.2
|
||||
|
@ -28,7 +30,7 @@ class OpenPGP {
|
|||
foreach ($headers as $key => $value) {
|
||||
$text .= $key . ': ' . (string)$value . "\n";
|
||||
}
|
||||
$text .= "\n" . base64_encode($data);
|
||||
$text .= "\n" . wordwrap(base64_encode($data), 76, "\n", true);
|
||||
$text .= "\n".'=' . base64_encode(substr(pack('N', self::crc24($data)), 1)) . "\n";
|
||||
$text .= self::footer($marker) . "\n";
|
||||
return $text;
|
||||
|
@ -42,8 +44,13 @@ class OpenPGP {
|
|||
$header = self::header($header);
|
||||
$text = str_replace(array("\r\n", "\r"), array("\n", ''), $text);
|
||||
if (($pos1 = strpos($text, $header)) !== FALSE &&
|
||||
($pos1 = strpos($text, "\n\n", $pos1 += strlen($header))) !== FALSE &&
|
||||
($pos2 = strpos($text, "\n=", $pos1 += 2)) !== FALSE) {
|
||||
($pos1 = strpos($text, "\n\n", $pos1 += strlen($header))) !== FALSE) {
|
||||
$pos2 = strpos($text, "\n=", $pos1 += 2);
|
||||
if ($pos2 === FALSE) {
|
||||
trigger_error("Invalid ASCII armor, missing CRC");
|
||||
$pos2 = strpos($text, "-----END");
|
||||
if ($pos2 === FALSE) return NULL;
|
||||
}
|
||||
return base64_decode($text = substr($text, $pos1, $pos2 - $pos1));
|
||||
}
|
||||
}
|
||||
|
@ -122,20 +129,20 @@ class OpenPGP_S2K {
|
|||
|
||||
static function parse(&$input) {
|
||||
$s2k = new OpenPGP_S2k();
|
||||
switch($s2k->type = ord($input{0})) {
|
||||
switch($s2k->type = ord($input[0])) {
|
||||
case 0:
|
||||
$s2k->hash_algorithm = ord($input{1});
|
||||
$s2k->hash_algorithm = ord($input[1]);
|
||||
$input = substr($input, 2);
|
||||
break;
|
||||
case 1:
|
||||
$s2k->hash_algorithm = ord($input{1});
|
||||
$s2k->hash_algorithm = ord($input[1]);
|
||||
$s2k->salt = substr($input, 2, 8);
|
||||
$input = substr($input, 10);
|
||||
break;
|
||||
case 3:
|
||||
$s2k->hash_algorithm = ord($input{1});
|
||||
$s2k->hash_algorithm = ord($input[1]);
|
||||
$s2k->salt = substr($input, 2, 8);
|
||||
$s2k->count = OpenPGP::decode_s2k_count(ord($input{10}));
|
||||
$s2k->count = OpenPGP::decode_s2k_count(ord($input[10]));
|
||||
$input = substr($input, 11);
|
||||
break;
|
||||
}
|
||||
|
@ -150,10 +157,12 @@ class OpenPGP_S2K {
|
|||
$bytes .= chr($this->hash_algorithm);
|
||||
break;
|
||||
case 1:
|
||||
if(strlen($this->salt) != 8) throw new Exception("Invalid salt length");
|
||||
$bytes .= chr($this->hash_algorithm);
|
||||
$bytes .= $this->salt;
|
||||
break;
|
||||
case 3:
|
||||
if(strlen($this->salt) != 8) throw new Exception("Invalid salt length");
|
||||
$bytes .= chr($this->hash_algorithm);
|
||||
$bytes .= $this->salt;
|
||||
$bytes .= chr(OpenPGP::encode_s2k_count($this->count));
|
||||
|
@ -553,7 +562,7 @@ class OpenPGP_Packet {
|
|||
}
|
||||
|
||||
function read_byte() {
|
||||
return ($bytes = $this->read_bytes()) ? $bytes[0] : NULL;
|
||||
return !is_null($bytes = $this->read_bytes()) ? $bytes[0] : NULL;
|
||||
}
|
||||
|
||||
function read_bytes($count = 1) {
|
||||
|
@ -609,7 +618,7 @@ class OpenPGP_AsymmetricSessionKeyPacket extends OpenPGP_Packet {
|
|||
$rawkeyid = $this->read_bytes(8);
|
||||
$this->keyid = '';
|
||||
for($i = 0; $i < strlen($rawkeyid); $i++) { // Store KeyID in Hex
|
||||
$this->keyid .= sprintf('%02X',ord($rawkeyid{$i}));
|
||||
$this->keyid .= sprintf('%02X',ord($rawkeyid[$i]));
|
||||
}
|
||||
|
||||
$this->key_algorithm = ord($this->read_byte());
|
||||
|
@ -625,7 +634,7 @@ class OpenPGP_AsymmetricSessionKeyPacket extends OpenPGP_Packet {
|
|||
$bytes = chr($this->version);
|
||||
|
||||
for($i = 0; $i < strlen($this->keyid); $i += 2) {
|
||||
$bytes .= chr(hexdec($this->keyid{$i}.$this->keyid{$i+1}));
|
||||
$bytes .= chr(hexdec($this->keyid[$i].$this->keyid[$i+1]));
|
||||
}
|
||||
|
||||
$bytes .= chr($this->key_algorithm);
|
||||
|
@ -685,13 +694,15 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet {
|
|||
switch($this->version = ord($this->read_byte())) {
|
||||
case 2:
|
||||
case 3:
|
||||
assert(ord($this->read_byte()) == 5);
|
||||
if(ord($this->read_byte()) != 5) {
|
||||
throw new Exception("Invalid version 2 or 3 SignaturePacket");
|
||||
}
|
||||
$this->signature_type = ord($this->read_byte());
|
||||
$creation_time = $this->read_timestamp();
|
||||
$keyid = $this->read_bytes(8);
|
||||
$keyidHex = '';
|
||||
for($i = 0; $i < strlen($keyid); $i++) { // Store KeyID in Hex
|
||||
$keyidHex .= sprintf('%02X',ord($keyid{$i}));
|
||||
$keyidHex .= sprintf('%02X',ord($keyid[$i]));
|
||||
}
|
||||
|
||||
$this->hashed_subpackets = array();
|
||||
|
@ -768,7 +779,7 @@ class OpenPGP_SignaturePacket extends OpenPGP_Packet {
|
|||
foreach((array)$this->unhashed_subpackets as $p) {
|
||||
if($p instanceof OpenPGP_SignaturePacket_IssuerPacket) {
|
||||
for($i = 0; $i < strlen($p->data); $i += 2) {
|
||||
$body .= chr(hexdec($p->data{$i}.$p->data{$i+1}));
|
||||
$body .= chr(hexdec($p->data[$i].$p->data[$i+1]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -975,8 +986,8 @@ class OpenPGP_SignaturePacket_ExportableCertificationPacket extends OpenPGP_Sign
|
|||
|
||||
class OpenPGP_SignaturePacket_TrustSignaturePacket extends OpenPGP_SignaturePacket_Subpacket {
|
||||
function read() {
|
||||
$this->depth = ord($this->input{0});
|
||||
$this->trust = ord($this->input{1});
|
||||
$this->depth = ord($this->input[0]);
|
||||
$this->trust = ord($this->input[1]);
|
||||
}
|
||||
|
||||
function body() {
|
||||
|
@ -1052,7 +1063,7 @@ class OpenPGP_SignaturePacket_RevocationKeyPacket extends OpenPGP_SignaturePacke
|
|||
$bytes .= chr($this->key_algorithm);
|
||||
|
||||
for($i = 0; $i < strlen($this->fingerprint); $i += 2) {
|
||||
$bytes .= chr(hexdec($this->fingerprint{$i}.$this->fingerprint{$i+1}));
|
||||
$bytes .= chr(hexdec($this->fingerprint[$i].$this->fingerprint[$i+1]));
|
||||
}
|
||||
|
||||
return $bytes;
|
||||
|
@ -1072,7 +1083,7 @@ class OpenPGP_SignaturePacket_IssuerPacket extends OpenPGP_SignaturePacket_Subpa
|
|||
function body() {
|
||||
$bytes = '';
|
||||
for($i = 0; $i < strlen($this->data); $i += 2) {
|
||||
$bytes .= chr(hexdec($this->data{$i}.$this->data{$i+1}));
|
||||
$bytes .= chr(hexdec($this->data[$i].$this->data[$i+1]));
|
||||
}
|
||||
return $bytes;
|
||||
}
|
||||
|
@ -1305,7 +1316,7 @@ class OpenPGP_OnePassSignaturePacket extends OpenPGP_Packet {
|
|||
function body() {
|
||||
$body = chr($this->version).chr($this->signature_type).chr($this->hash_algorithm).chr($this->key_algorithm);
|
||||
for($i = 0; $i < strlen($this->key_id); $i += 2) {
|
||||
$body .= chr(hexdec($this->key_id{$i}.$this->key_id{$i+1}));
|
||||
$body .= chr(hexdec($this->key_id[$i].$this->key_id[$i+1]));
|
||||
}
|
||||
$body .= chr((int)$this->nested);
|
||||
return $body;
|
||||
|
@ -1594,6 +1605,13 @@ class OpenPGP_CompressedDataPacket extends OpenPGP_Packet implements IteratorAgg
|
|||
public $algorithm;
|
||||
/* see http://tools.ietf.org/html/rfc4880#section-9.3 */
|
||||
static $algorithms = array(0 => 'Uncompressed', 1 => 'ZIP', 2 => 'ZLIB', 3 => 'BZip2');
|
||||
|
||||
function __construct($m=NULL, $algorithm=1) {
|
||||
parent::__construct();
|
||||
$this->algorithm = $algorithm;
|
||||
$this->data = $m ? $m : new OpenPGP_Message();
|
||||
}
|
||||
|
||||
function read() {
|
||||
$this->algorithm = ord($this->read_byte());
|
||||
$this->data = $this->read_bytes($this->length);
|
||||
|
|
|
@ -182,8 +182,10 @@ class OpenPGP_Crypt_RSA {
|
|||
$keys = new self($keys);
|
||||
}
|
||||
|
||||
$session_key = NULL;
|
||||
foreach($message as $p) {
|
||||
if($p instanceof OpenPGP_AsymmetricSessionKeyPacket) {
|
||||
$session_key = $p;
|
||||
if($keys instanceof Crypt_RSA) {
|
||||
$sk = self::try_decrypt_session($keys, substr($p->encrypted_data, 2));
|
||||
} else if(strlen(str_replace('0', '', $p->keyid)) < 1) {
|
||||
|
@ -203,23 +205,26 @@ class OpenPGP_Crypt_RSA {
|
|||
}
|
||||
}
|
||||
|
||||
if (!$session_key) throw new Exception("Not an asymmetrically encrypted message");
|
||||
|
||||
return NULL; /* Failed */
|
||||
}
|
||||
|
||||
static function try_decrypt_session($key, $edata) {
|
||||
$key->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
|
||||
$data = $key->decrypt($edata);
|
||||
$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;
|
||||
$sk_chk = ($sk_chk + ord($sk[$i])) % 65536;
|
||||
}
|
||||
|
||||
if($sk_chk != $chk) return NULL;
|
||||
return array(ord($data{0}), $sk);
|
||||
return array(ord($data[0]), $sk);
|
||||
}
|
||||
|
||||
static function crypt_rsa_key($mod, $exp, $hash='SHA256') {
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
<?php
|
||||
|
||||
use phpseclib\Crypt\TripleDES as Crypt_TripleDES;
|
||||
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;
|
||||
|
||||
define('CRYPT_DES_MODE_CFB', Crypt_TripleDES::MODE_CFB);
|
||||
define('CRYPT_AES_MODE_CFB', Crypt_AES::MODE_CFB);
|
||||
|
||||
require_once dirname(__FILE__).'/openpgp.php';
|
||||
@include_once dirname(__FILE__).'/openpgp_crypt_rsa.php';
|
||||
@include_once dirname(__FILE__).'/openpgp_mcrypt_wrapper.php';
|
||||
@include_once dirname(__FILE__).'/openpgp_openssl_wrapper.php';
|
||||
|
||||
class OpenPGP_Crypt_Symmetric {
|
||||
public static function encrypt($passphrases_and_keys, $message, $symmetric_algorithm=9) {
|
||||
|
@ -40,7 +40,7 @@ class OpenPGP_Crypt_Symmetric {
|
|||
$esk = pack('n', OpenPGP::bitlength($esk)) . $esk;
|
||||
array_unshift($encrypted, new OpenPGP_AsymmetricSessionKeyPacket($pass->algorithm, $pass->fingerprint(), $esk));
|
||||
} else if(is_string($pass)) {
|
||||
$s2k = new OpenPGP_S2K(Random::string(10));
|
||||
$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));
|
||||
|
@ -62,7 +62,7 @@ class OpenPGP_Crypt_Symmetric {
|
|||
|
||||
$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));
|
||||
$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));
|
||||
|
@ -75,6 +75,31 @@ class OpenPGP_Crypt_Symmetric {
|
|||
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
|
||||
|
||||
|
@ -97,6 +122,7 @@ class OpenPGP_Crypt_Symmetric {
|
|||
if($chk != $mkChk) return NULL;
|
||||
}
|
||||
|
||||
$packet->s2k = NULL;
|
||||
$packet->s2k_useage = 0;
|
||||
$packet->symmetric_algorithm = 0;
|
||||
$packet->encrypted_data = NULL;
|
||||
|
@ -146,29 +172,45 @@ class OpenPGP_Crypt_Symmetric {
|
|||
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_DES_MODE_CFB);
|
||||
$key_bytes = 24;
|
||||
$key_block_bytes = 8;
|
||||
$cipher = new Crypt_TripleDES(Crypt_TripleDES::MODE_CFB);
|
||||
$key_bytes = 24;
|
||||
$key_block_bytes = 8;
|
||||
break;
|
||||
case 3:
|
||||
if(defined('MCRYPT_CAST_128')) {
|
||||
if(class_exists('OpenSSLWrapper')) {
|
||||
$cipher = new OpenSSLWrapper("CAST5-CFB");
|
||||
} else if(defined('MCRYPT_CAST_128')) {
|
||||
$cipher = new MCryptWrapper(MCRYPT_CAST_128);
|
||||
} else {
|
||||
throw new Exception("Unsupported cipher: you must have mcrypt installed to use CAST5");
|
||||
}
|
||||
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);
|
||||
$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);
|
||||
$cipher = new Crypt_AES(Crypt_AES::MODE_CFB);
|
||||
$cipher->setKeyLength(192);
|
||||
break;
|
||||
case 9:
|
||||
$cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
|
||||
$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;
|
||||
}
|
||||
if(!$cipher) return array(NULL, NULL, NULL); // Unsupported cipher
|
||||
|
@ -187,7 +229,7 @@ class OpenPGP_Crypt_Symmetric {
|
|||
public static function checksum($s) {
|
||||
$mkChk = 0;
|
||||
for($i = 0; $i < strlen($s); $i++) {
|
||||
$mkChk = ($mkChk + ord($s{$i})) % 65536;
|
||||
$mkChk = ($mkChk + ord($s[$i])) % 65536;
|
||||
}
|
||||
return $mkChk;
|
||||
}
|
||||
|
|
33
securemail/vendor/singpolyma/openpgp-php/lib/openpgp_openssl_wrapper.php
vendored
Normal file
33
securemail/vendor/singpolyma/openpgp-php/lib/openpgp_openssl_wrapper.php
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
if(function_exists('openssl_encrypt')) {
|
||||
class OpenSSLWrapper {
|
||||
public $cipher, $key, $iv, $key_size, $block_size;
|
||||
|
||||
|
||||
function __construct($cipher) {
|
||||
if($cipher != "CAST5-CFB") throw Exception("OpenSSLWrapper is only used for CAST5 right now");
|
||||
|
||||
$this->cipher = $cipher;
|
||||
$this->key_size = 16;
|
||||
$this->block_size = 8;
|
||||
$this->iv = str_repeat("\0", 8);
|
||||
}
|
||||
|
||||
function setKey($key) {
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
function setIV($iv) {
|
||||
$this->iv = $iv;
|
||||
}
|
||||
|
||||
function encrypt($data) {
|
||||
return openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $this->iv);
|
||||
}
|
||||
|
||||
function decrypt($data) {
|
||||
return openssl_decrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $this->iv);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue