Use PHP's gnupg extension for encrypting mail
This commit is contained in:
parent
cdadfb5127
commit
c75eb5eb03
@ -12,7 +12,6 @@ use PHPMailer\PHPMailer\Exception;
|
||||
* Class containing static functions for an easier handling of mails.
|
||||
*/
|
||||
final class MDMailerHelper {
|
||||
|
||||
/**
|
||||
* PGP-encrypts a message to a given email address.
|
||||
*
|
||||
@ -23,9 +22,40 @@ final class MDMailerHelper {
|
||||
*/
|
||||
public static function pgp_encrypt(string $to, string $msg):string {
|
||||
|
||||
$gpg = new gnupg();
|
||||
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
|
||||
|
||||
if (empty($recipients = $gpg->keyinfo($to))) {
|
||||
throw new \Exception("Cannot retrieve public key of recipient for GPG encryption");
|
||||
}
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
$recipient_fingerprint = $recipient["subkeys"][0]["fingerprint"];
|
||||
|
||||
if (empty($recipient = $gpg->addencryptkey($recipient_fingerprint))) {
|
||||
throw new \Exception("Cannot set public key of recipient for GPG encryption");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (($gpg->addsignkey(MD_CONF_EMAIL::PGP_ENC_KEY)) === false) {
|
||||
throw new \Exception("Cannot set private key for GPG encryption");
|
||||
}
|
||||
|
||||
if (($msg = $gpg->encrypt($msg)) === false) {
|
||||
throw new \Exception("Failed to sign and encrypt the message");
|
||||
}
|
||||
|
||||
return $msg;
|
||||
|
||||
/*
|
||||
echo shell_exec("gpg --list-keys 2>&1");
|
||||
echo shell_exec("echo " . escapeshellarg($msg) . " | gpg --always-trust --homedir " . escapeshellarg(MD_CONF_EMAIL::PGP_HOMEDIR) . " --recipient " . escapeshellarg($to) . " --encrypt --armor --local-user " . escapeshellarg(MD_CONF_EMAIL::PGP_ENC_KEY) . " --sign 2>&1");
|
||||
exit;
|
||||
$msg = shell_exec("echo " . escapeshellarg($msg) . " | gpg --always-trust --homedir " . escapeshellarg(MD_CONF_EMAIL::PGP_HOMEDIR) . " --recipient " . escapeshellarg($to) . " --encrypt --armor --local-user " . escapeshellarg(MD_CONF_EMAIL::PGP_ENC_KEY) . " --sign");
|
||||
if ($msg === null) return "Error encrypting message";
|
||||
return $msg;
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@ -55,5 +85,4 @@ final class MDMailerHelper {
|
||||
return $mail;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user