2021-07-01 23:18:13 -06:00
/ *
* Copyright 2021 Netsyms Technologies .
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
* /
var keymgr ;
/ * *
* Load and unlock the private key in localstorage , prompting user as needed . If there is no key , generates , saves , and loads a new one .
* @ param { function } callback Passed two arguments : message for user , and boolean true if OK false if error .
* @ returns { undefined }
* /
function loadKeyFromLocalStorage ( callback ) {
2021-07-02 00:12:47 -06:00
if ( typeof keymgr != "undefined" ) {
callback ( "Key already loaded." , true ) ;
return ;
}
2021-07-01 23:18:13 -06:00
if ( ! inStorage ( "signingkey" ) || getStorage ( "signingkey" ) == "undefined" ) {
2021-07-02 00:12:47 -06:00
var pass = prompt ( "Generating a new signing key (might take a while, be patient). Enter a password to protect it. You'll need to save this password somewhere safe; it cannot be recovered." ) ;
2021-07-01 23:18:13 -06:00
generatePrivateKey ( getStorage ( "notary_name" ) + " <null@null.com>" , pass , function ( key ) {
if ( typeof key == "undefined" ) {
callback ( "Could not generate key." , false ) ;
return ;
}
keymgr = key ;
setStorage ( "signingkey" , keymgr . armored _pgp _private ) ;
callback ( "Signing key generated." , true ) ;
} ) ;
} else {
2021-07-02 00:12:47 -06:00
var pass = prompt ( "Enter password to unlock signing key:" ) ;
2021-07-01 23:18:13 -06:00
loadPrivateKey ( getStorage ( "signingkey" ) , pass , function ( key ) {
if ( typeof key == "undefined" ) {
callback ( "Could not unlock key. Password is probably incorrect." , false ) ;
return ;
}
keymgr = key ;
callback ( "Signing key unlocked." , true ) ;
} ) ;
}
}
2021-07-02 00:12:47 -06:00
function loadKeyFromLocalStorageWithUserFeedback ( ) {
loadKeyFromLocalStorage ( function ( msg , ok ) {
2021-07-02 01:31:20 -06:00
if ( ok ) {
alert ( msg ) ;
} else {
alert ( "Error: " + msg ) ;
}
2021-07-02 00:12:47 -06:00
} ) ;
}
2021-07-01 23:18:13 -06:00
/ * *
* Load a private key .
* @ param { string } armoredkey PGP private key
* @ param { string } pass key password
* @ param { function } callback Passed a new keymanager for the key .
* @ returns { undefined }
* /
function loadPrivateKey ( armoredkey , pass , callback ) {
kbpgp . KeyManager . import _from _armored _pgp ( {
armored : armoredkey
} , function ( err , key ) {
if ( ! err ) {
if ( key . is _pgp _locked ( ) ) {
key . unlock _pgp ( {
passphrase : pass
} , function ( err ) {
if ( ! err ) {
console . log ( "Loaded private key with passphrase" ) ;
callback ( key ) ;
} else {
console . error ( err ) ;
callback ( undefined ) ;
}
} ) ;
} else {
console . log ( "Loaded private key w/o passphrase" ) ;
callback ( key ) ;
}
} else {
console . error ( err ) ;
callback ( undefined ) ;
}
} ) ;
}
/ * *
* Sign a message with a key and return the signed message in the callback .
* @ param { type } text
* @ param { type } key
* @ param { type } callback
* @ returns { undefined }
* /
function signMessage ( text , key , callback ) {
var params = {
msg : text ,
sign _with : key
} ;
kbpgp . box ( params , function ( err , result _string , result _buffer ) {
//console.log(err, result_string, result_buffer);
callback ( result _string ) ;
} ) ;
}
/ * *
* Generate a new private key .
* @ param { string } userid Something like "Test User <test@netsyms.com>"
* @ param { string } passphrase protects the key
* @ param { function } callback Passed the keymanager for the new key
* @ returns { undefined }
* /
function generatePrivateKey ( userid , passphrase , callback ) {
var F = kbpgp [ "const" ] . openpgp ;
var opts = {
userid : userid ,
primary : {
nbits : 2048 ,
flags : F . certify _keys | F . sign _data | F . auth | F . encrypt _comm | F . encrypt _storage ,
expire _in : 0 // never expire
} ,
subkeys : [ ]
} ;
kbpgp . KeyManager . generate ( opts , function ( err , alice ) {
if ( ! err ) {
alice . sign ( { } , function ( err ) {
alice . export _pgp _private ( {
passphrase : passphrase
} , function ( err , pgp _private ) {
callback ( alice ) ;
} ) ;
} ) ;
}
} ) ;
2021-07-02 00:12:47 -06:00
}
function exportPublicKey ( ) {
loadKeyFromLocalStorage ( function ( message , ok ) {
if ( ok ) {
openSaveFileDialog ( function ( path ) {
keymgr . export _pgp _public ( { } , function ( err , pgp _public ) {
if ( err ) {
alert ( "Something went wrong." ) ;
} else {
writeToFile ( path , pgp _public ) ;
}
} ) ;
} , "public-key.asc" , ".asc" ) ;
} else {
alert ( "Error: " + message ) ;
}
} ) ;
}
function exportPrivateKey ( ) {
2021-07-02 01:31:20 -06:00
var pass = prompt ( "Enter password for private key:" ) ;
const savepriv = function ( key ) {
var pass2 = prompt ( "Enter a password to protect the key backup:" ) ;
openSaveFileDialog ( function ( path ) {
key . export _pgp _private ( {
passphrase : pass2
} , function ( err , pgp _private ) {
if ( err ) {
alert ( "Something went wrong." ) ;
} else {
writeToFile ( path , pgp _private ) ;
}
} ) ;
} , "private-key.asc" , ".asc" ) ;
}
kbpgp . KeyManager . import _from _armored _pgp ( {
armored : getStorage ( "signingkey" )
} , function ( err , key ) {
if ( ! err ) {
if ( key . is _pgp _locked ( ) ) {
key . unlock _pgp ( {
2021-07-02 00:12:47 -06:00
passphrase : pass
2021-07-02 01:31:20 -06:00
} , function ( err ) {
if ( ! err ) {
savepriv ( key ) ;
2021-07-02 00:12:47 -06:00
} else {
2021-07-02 01:31:20 -06:00
alert ( "Could not unlock key. Password is probably incorrect." ) ;
2021-07-02 00:12:47 -06:00
}
} ) ;
2021-07-02 01:31:20 -06:00
} else {
console . log ( "Loaded private key w/o passphrase" ) ;
savepriv ( key ) ;
}
2021-07-02 00:12:47 -06:00
} else {
2021-07-02 01:31:20 -06:00
alert ( "Could not unlock key: " + err ) ;
2021-07-02 00:12:47 -06:00
}
} ) ;
}
function importPrivateKey ( ) {
if ( inStorage ( "signingkey" ) && getStorage ( "signingkey" ) != "undefined" ) {
if ( ! confirm ( "The restored key will replace the current key, which will be unrecoverable unless you made a backup. Continue?" ) ) {
return ;
}
}
2021-07-02 01:31:20 -06:00
keymgr = null ;
2021-07-02 00:12:47 -06:00
openFileDialog ( function ( path ) {
var keyfile = getFileAsString ( path ) ;
var pass = prompt ( "Enter password for imported key (password was set when exported):" ) ;
loadPrivateKey ( keyfile , pass , function ( key ) {
if ( typeof key == "undefined" ) {
alert ( "Could not import key. Password is probably incorrect." ) ;
return ;
}
keymgr = key ;
setStorage ( "signingkey" , keymgr . armored _pgp _private ) ;
alert ( "Private key imported." ) ;
} ) ;
} , ".asc" ) ;
2021-07-01 23:18:13 -06:00
}