Skip to content

Commit

Permalink
attach public key when signing
Browse files Browse the repository at this point in the history
attach the sender's public key as signature.asc when sending a signed
and/or encrypted email
  • Loading branch information
bogde committed Oct 8, 2013
1 parent 81e8abb commit 5bedc55
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
40 changes: 36 additions & 4 deletions js/rc_openpgpjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,24 @@ if(window.rcmail) {
/**
* Get the user's public key
*/
function fetchSendersPubkey() {
function fetchSendersPubkey(armored) {

if (typeof(armored) == "undefined") {
armored = false;
}

var re = /[a-zA-Z0-9\._%+-]+@[a-zA-Z0-9\._%+-]+\.[a-zA-Z]{2,4}/g;
var address = $("#_from>option:selected").html().match(re);

if (address.length > 0) {
var pubkey = getPubkeyForAddress(address[0]);

if(typeof(pubkey[0]) != "undefined") {
return pubkey[0].obj;
if (armored)
return pubkey[0].armored;
else
return pubkey[0].obj;
}

}
return false;
}
Expand All @@ -308,6 +315,8 @@ if(window.rcmail) {
if(!$("#openpgpjs_encrypt").is(":checked") &&
!$("#openpgpjs_sign").is(":checked")) {
if(confirm(rcmail.gettext("continue_unencrypted", "rc_openpgpjs"))) {
// remove the public key attachment since we don't sign nor encrypt the message
removePublicKeyAttachment();
return true;
} else {
return false;
Expand All @@ -318,6 +327,14 @@ if(window.rcmail) {
return true;
}

// send the user's public key to the server so it can be sent as attachment
var pubkey_sender = fetchSendersPubkey(true);
if (pubkey_sender) {
var lock = rcmail.set_busy(true, 'loading');
rcmail.http_post('plugin.pubkey_save', { _pubkey: pubkey_sender }, lock);
}
// end send user's public key to the server

// Encrypt and sign
if($("#openpgpjs_encrypt").is(":checked") && $("#openpgpjs_sign").is(":checked")) {
// get the private key
Expand Down Expand Up @@ -433,6 +450,21 @@ if(window.rcmail) {
return false;
}

/**
* Removes the public key attachment
* Used if the user doesn't sign nor encrypt the message
*/
function removePublicKeyAttachment() {
$("#attachment-list").each(function() {
$(this).find('li').each(function() {
if ($(this).text().indexOf('signature.asc') >= 0) {
rcmail.command('remove-attachment', $(this).attr('id'));
return false;
}
});
});
}

function importFromSKS(id) {
rcmail.http_post("plugin.pks_search", "search=" + id + "&op=get");
return;
Expand Down
65 changes: 63 additions & 2 deletions rc_openpgpjs.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function init()
$this->add_hook('user_create', array($this, 'user_create'));
$this->register_action('plugin.pks_search', array($this, 'hkp_search'));
$this->register_action('plugin.hkp_add', array($this, 'hkp_add'));
$this->register_action('plugin.pubkey_save', array($this, 'pubkey_save'));

if ($this->rc->task == 'mail') {
$this->add_hook('render_page', array($this, 'render_page'));
Expand All @@ -52,6 +53,10 @@ function init()
// load css
$this->include_stylesheet($this->local_skin_path() . '/rc_openpgpjs.css');

// add public key attachment related hooks
$this->add_hook('message_compose', array($this, 'message_compose'));
$this->add_hook('message_sent', array($this, 'unlink_pubkey'));

if ($this->api->output->type == 'html') {
// add key manager item to message menu
$opts = array("command" => "open-key-manager",
Expand Down Expand Up @@ -229,7 +234,20 @@ function hkp_add() {
header("HTTP/1.1 501 Not Implemented");
die();
}


/**
* Saves the public key to a temporary file so we can send it as attachment
*/
function pubkey_save() {
$rcmail = rcmail::get_instance();
$temp_dir = unslashify($rcmail->config->get('temp_dir'));
$file = $temp_dir."/".md5($_SESSION['username']).".asc";
if(file_exists($file)) {
$pubkey = trim(get_input_value('_pubkey', RCUBE_INPUT_POST));
file_put_contents($file, $pubkey);
}
}

/**
* Handler for preferences_list hook.
* Adds options blocks into Compose settings sections in Preferences.
Expand Down Expand Up @@ -275,5 +293,48 @@ function preferences_save($p)
}

return $p;
}
}

/**
* Handler for message_compose hook
* Creates a dummy publick key attachment
*/
function message_compose($args) {
$dbg = print_r($args, true);

if ($f = $this->create_pubkey_dummy()) {
$args['attachments'][] = array('path' => $f, 'name' => "signature.asc", 'mimetype' => "text/plain");
}
return $args;
}

/**
* Handler for message_sent hook
* Deletes the public key from the server
*/
function unlink_pubkey($args) {
$rcmail = rcmail::get_instance();
$temp_dir = unslashify($rcmail->config->get('temp_dir'));
$file = $temp_dir."/".md5($_SESSION['username']).".asc";
if(file_exists($file)) {
@unlink($file);
}
}

/**
* Creates a dummy public key file
*/
function create_pubkey_dummy() {
$rcmail = rcmail::get_instance();
$temp_dir = unslashify($rcmail->config->get('temp_dir'));
if (!empty($temp_dir)) {
$file = $temp_dir."/".md5($_SESSION['username']).".asc";
if(file_exists($file))
@unlink($file);
if (file_put_contents($file, " ")) {
return $file;
}
}
return false;
}
}

0 comments on commit 5bedc55

Please sign in to comment.