2018-05-17 19:15:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__ . "/vars.inc";
|
|
|
|
|
|
|
|
class gnupgt {
|
2018-06-14 19:14:27 +00:00
|
|
|
/**
|
|
|
|
* Import all keys
|
|
|
|
*/
|
|
|
|
static public function import_key()
|
2018-05-17 19:15:14 +00:00
|
|
|
{
|
|
|
|
global $testkey;
|
|
|
|
|
2021-02-14 18:29:37 +00:00
|
|
|
self::reset_key();
|
2018-05-17 19:15:14 +00:00
|
|
|
|
|
|
|
$gpg = new gnupg();
|
|
|
|
$gpg->import($testkey);
|
|
|
|
}
|
|
|
|
|
2018-06-14 19:14:27 +00:00
|
|
|
/**
|
|
|
|
* Delete all keys.
|
2021-02-14 18:49:40 +00:00
|
|
|
* @param null|string $homeDir
|
2018-06-14 19:14:27 +00:00
|
|
|
*/
|
2021-02-14 18:49:40 +00:00
|
|
|
static public function delete_key($homeDir = null)
|
2018-05-17 19:15:14 +00:00
|
|
|
{
|
2021-02-14 18:49:40 +00:00
|
|
|
if (is_null($homeDir)) {
|
|
|
|
$homeDir = self::get_home_dir();
|
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
if (!is_dir($homeDir)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-14 18:49:40 +00:00
|
|
|
foreach (glob($homeDir . '/*') as $filename) {
|
|
|
|
if (is_file($filename)) {
|
|
|
|
unlink($filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$privKeyDir = self::get_priv_key_dir($homeDir);
|
2018-05-22 16:43:05 +00:00
|
|
|
if (is_dir($privKeyDir)) {
|
2021-02-14 18:49:40 +00:00
|
|
|
foreach (glob($privKeyDir . '/*') as $key) {
|
2018-05-22 16:43:05 +00:00
|
|
|
unlink($key);
|
|
|
|
}
|
|
|
|
rmdir($privKeyDir);
|
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
rmdir($homeDir);
|
2018-05-17 19:15:14 +00:00
|
|
|
}
|
2018-06-14 19:14:27 +00:00
|
|
|
|
2021-02-14 18:49:40 +00:00
|
|
|
/**
|
|
|
|
* Initialize key directory.
|
|
|
|
*/
|
|
|
|
static public function init_key_dir()
|
|
|
|
{
|
2021-02-14 18:29:37 +00:00
|
|
|
mkdir(self::get_home_dir());
|
|
|
|
mkdir(self::get_priv_key_dir(), 0700);
|
2021-02-14 18:49:40 +00:00
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
|
2021-02-14 18:49:40 +00:00
|
|
|
/**
|
|
|
|
* Reset all keys.
|
|
|
|
*/
|
|
|
|
static public function reset_key()
|
|
|
|
{
|
|
|
|
self::delete_key();
|
|
|
|
self::init_key_dir();
|
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
|
2021-02-14 18:49:40 +00:00
|
|
|
/**
|
|
|
|
* Get home directory.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-02-14 18:29:37 +00:00
|
|
|
static private function get_home_dir()
|
2021-02-14 18:49:40 +00:00
|
|
|
{
|
|
|
|
return __DIR__ . '/home';
|
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
|
2021-02-14 18:49:40 +00:00
|
|
|
/**
|
|
|
|
* Get private key directory (for GPG2).
|
|
|
|
* @param null|string $homeDir
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static private function get_priv_key_dir($homeDir = null)
|
|
|
|
{
|
|
|
|
if (is_null($homeDir)) {
|
|
|
|
$homeDir = self::get_home_dir();
|
|
|
|
}
|
|
|
|
return $homeDir . '/private-keys-v1.d';
|
|
|
|
}
|
2021-02-14 18:29:37 +00:00
|
|
|
|
2018-06-14 19:14:27 +00:00
|
|
|
/**
|
|
|
|
* Print error message and return false.
|
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static private function error($msg)
|
|
|
|
{
|
|
|
|
echo "ERROR: " . $msg;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check single array value.
|
|
|
|
*
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param array $a
|
|
|
|
* @param string $key1
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static public function check_array($expected, $a, $key1)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
$keys = array_splice($args, 2);
|
|
|
|
$value = $a;
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (!isset($value[$key])) {
|
|
|
|
return self::error("key $key not found in the array");
|
|
|
|
}
|
|
|
|
$value = $value[$key];
|
|
|
|
}
|
|
|
|
if ($value !== $expected) {
|
|
|
|
|
|
|
|
return self::error(
|
|
|
|
sprintf(
|
2018-06-18 14:36:48 +00:00
|
|
|
"key %s value %s does not match expected %s\n",
|
2018-06-14 19:14:27 +00:00
|
|
|
$key,
|
|
|
|
var_export($value, true),
|
2018-06-18 14:36:48 +00:00
|
|
|
var_export($expected, true)
|
2018-06-14 19:14:27 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check single array value but only for GpgME version higher than supplied.
|
|
|
|
*
|
|
|
|
* @param mixed $expected
|
|
|
|
* @param array $a
|
|
|
|
* @param string $key1
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static public function check_array_from_version($version, $expected, $a, $key1)
|
|
|
|
{
|
2021-02-14 18:49:40 +00:00
|
|
|
if (version_compare(GNUPG_GPGME_VERSION, $version) < 0) {
|
2018-06-14 19:14:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = func_get_args();
|
|
|
|
return call_user_func_array('gnupgt::check_array', array_splice($args, 1));
|
|
|
|
}
|
2018-08-27 17:49:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check keyinfo for var key
|
|
|
|
*
|
|
|
|
* @param $ret
|
|
|
|
* @param $secret_only
|
|
|
|
*/
|
|
|
|
static public function check_keyinfo($ret, $secret_only) {
|
|
|
|
self::check_array(false, $ret, 0, 'disabled');
|
|
|
|
self::check_array(false, $ret, 0, 'expired');
|
|
|
|
self::check_array(false, $ret, 0, 'revoked');
|
|
|
|
self::check_array($secret_only, $ret, 0, 'is_secret');
|
|
|
|
self::check_array(true, $ret, 0, 'can_sign');
|
|
|
|
self::check_array(true, $ret, 0, 'can_encrypt');
|
|
|
|
// uid
|
2019-09-29 16:14:01 +00:00
|
|
|
self::check_array('PHP GnuPG', $ret, 0, 'uids', 0, 'name');
|
|
|
|
self::check_array('', $ret, 0, 'uids', 0, 'comment');
|
|
|
|
self::check_array('gnupg@php.net', $ret, 0, 'uids', 0, 'email');
|
|
|
|
self::check_array('PHP GnuPG <gnupg@php.net>', $ret, 0, 'uids', 0, 'uid');
|
2018-08-27 17:49:33 +00:00
|
|
|
self::check_array(false, $ret, 0, 'uids', 0, 'revoked');
|
|
|
|
self::check_array(false, $ret, 0, 'uids', 0, 'invalid');
|
|
|
|
self::check_array(false, $ret, 0, 'uids', 0, 'invalid');
|
|
|
|
// subkey 1
|
2019-09-29 16:14:01 +00:00
|
|
|
self::check_array("2DF0DD02DC9B70B7F64F572E669E775E0A6284B3", $ret, 0, 'subkeys', 0, 'fingerprint');
|
|
|
|
self::check_array("669E775E0A6284B3", $ret, 0, 'subkeys', 0, 'keyid');
|
|
|
|
self::check_array(1567958444, $ret, 0, 'subkeys', 0, 'timestamp');
|
2018-08-27 17:49:33 +00:00
|
|
|
self::check_array(0, $ret, 0, 'subkeys', 0, 'expires');
|
|
|
|
self::check_array($secret_only, $ret, 0, 'subkeys', 0, 'is_secret');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'can_encrypt');
|
|
|
|
self::check_array(true, $ret, 0, 'subkeys', 0, 'can_sign');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'disabled');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'expired');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'revoked');
|
|
|
|
self::check_array(true, $ret, 0, 'subkeys', 0, 'can_certify');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'can_authenticate');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 0, 'is_qualified');
|
2021-02-14 19:18:00 +00:00
|
|
|
// TODO: The is_de_vs seems to differ between gpg2 (true) and gpg1 (false) - differenatiate the test
|
|
|
|
//self::check_array_from_version('1.9.0', true, $ret, 0, 'subkeys', 0, 'is_de_vs');
|
2019-09-29 16:14:01 +00:00
|
|
|
self::check_array(GNUPG_PK_RSA, $ret, 0, 'subkeys', 0, 'pubkey_algo');
|
|
|
|
self::check_array(2048, $ret, 0, 'subkeys', 0, 'length');
|
2018-08-27 17:49:33 +00:00
|
|
|
self::check_array_from_version('1.7.0', false, $ret, 0, 'subkeys', 0, 'is_cardkey');
|
|
|
|
// subkey 2
|
2019-09-29 16:14:01 +00:00
|
|
|
self::check_array("9E84AE800874DFF647B6062B46DCA9B3662C7DFC", $ret, 0, 'subkeys', 1, 'fingerprint');
|
|
|
|
self::check_array("46DCA9B3662C7DFC", $ret, 0, 'subkeys', 1, 'keyid');
|
|
|
|
self::check_array(1567958444, $ret, 0, 'subkeys', 1, 'timestamp');
|
2018-08-27 17:49:33 +00:00
|
|
|
self::check_array(0, $ret, 0, 'subkeys', 1, 'expires');
|
|
|
|
self::check_array($secret_only, $ret, 0, 'subkeys', 1, 'is_secret');
|
|
|
|
self::check_array(true, $ret, 0, 'subkeys', 1, 'can_encrypt');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'can_sign');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'disabled');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'expired');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'revoked');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'can_certify');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'can_authenticate');
|
|
|
|
self::check_array(false, $ret, 0, 'subkeys', 1, 'is_qualified');
|
2021-02-14 19:18:00 +00:00
|
|
|
// TODO: The is_de_vs seems to differ between gpg2 (true) and gpg1 (false) - differenatiate the test
|
|
|
|
// self::check_array_from_version('1.9.0', true, $ret, 0, 'subkeys', 1, 'is_de_vs');
|
2019-09-29 16:14:01 +00:00
|
|
|
self::check_array(GNUPG_PK_RSA, $ret, 0, 'subkeys', 1, 'pubkey_algo');
|
|
|
|
self::check_array(2048, $ret, 0, 'subkeys', 1, 'length');
|
2018-08-27 17:49:33 +00:00
|
|
|
self::check_array_from_version('1.7.0', false, $ret, 0, 'subkeys', 1, 'is_cardkey');
|
|
|
|
}
|
2018-05-17 19:15:14 +00:00
|
|
|
}
|