2018-05-15 11:15:31 +02:00
/ * *
* @ author tlwr [ toby @ toby . codes ]
* @ copyright Crown Copyright 2017
* @ license Apache - 2.0
* /
import Operation from "../Operation" ;
2018-05-15 17:04:57 +02:00
import kbpgp from "kbpgp" ;
2018-05-15 11:15:31 +02:00
import { ASP , importPrivateKey , importPublicKey } from "../lib/PGP" ;
2018-05-15 19:01:04 +02:00
import OperationError from "../errors/OperationError" ;
2018-05-15 17:04:57 +02:00
import promisifyDefault from "es6-promisify" ;
const promisify = promisifyDefault . promisify ;
2018-05-15 11:15:31 +02:00
/ * *
* PGP Decrypt and Verify operation
* /
class PGPDecryptAndVerify extends Operation {
/ * *
* PGPDecryptAndVerify constructor
* /
constructor ( ) {
super ( ) ;
this . name = "PGP Decrypt and Verify" ;
this . module = "PGP" ;
this . description = "Input: the ASCII-armoured encrypted PGP message you want to verify.\n<br><br>\nArguments: the ASCII-armoured PGP public key of the signer, \nthe ASCII-armoured private key of the recipient (and the private key password if necessary).\n<br><br>\nThis operation uses PGP to decrypt and verify an encrypted digital signature.\n<br><br>\nPretty Good Privacy is an encryption standard (OpenPGP) used for encrypting, decrypting, and signing messages.\n<br><br>\nThis function uses the Keybase implementation of PGP." ;
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Public key of signer" ,
"type" : "text" ,
"value" : ""
} ,
{
"name" : "Private key of recipient" ,
"type" : "text" ,
"value" : ""
} ,
{
"name" : "Private key password" ,
"type" : "string" ,
"value" : ""
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
async run ( input , args ) {
const signedMessage = input ,
publicKey = args [ 0 ] ,
privateKey = args [ 1 ] ,
passphrase = args [ 2 ] ,
keyring = new kbpgp . keyring . KeyRing ( ) ;
let unboxedLiterals ;
2018-05-15 19:01:04 +02:00
if ( ! publicKey ) throw new OperationError ( "Enter the public key of the signer." ) ;
if ( ! privateKey ) throw new OperationError ( "Enter the private key of the recipient." ) ;
2018-05-15 11:15:31 +02:00
const privKey = await importPrivateKey ( privateKey , passphrase ) ;
const pubKey = await importPublicKey ( publicKey ) ;
keyring . add _key _manager ( privKey ) ;
keyring . add _key _manager ( pubKey ) ;
try {
unboxedLiterals = await promisify ( kbpgp . unbox ) ( {
armored : signedMessage ,
keyfetch : keyring ,
asp : ASP
} ) ;
const ds = unboxedLiterals [ 0 ] . get _data _signer ( ) ;
if ( ds ) {
const km = ds . get _key _manager ( ) ;
if ( km ) {
const signer = km . get _userids _mark _primary ( ) [ 0 ] . components ;
let text = "Signed by " ;
if ( signer . email || signer . username || signer . comment ) {
if ( signer . username ) {
text += ` ${ signer . username } ` ;
}
if ( signer . comment ) {
text += ` ${ signer . comment } ` ;
}
if ( signer . email ) {
text += ` < ${ signer . email } > ` ;
}
text += "\n" ;
}
text += [
` PGP fingerprint: ${ km . get _pgp _fingerprint ( ) . toString ( "hex" ) } ` ,
` Signed on ${ new Date ( ds . sig . hashed _subpackets [ 0 ] . time * 1000 ) . toUTCString ( ) } ` ,
"----------------------------------\n"
] . join ( "\n" ) ;
text += unboxedLiterals . toString ( ) ;
return text . trim ( ) ;
} else {
2018-05-15 19:01:04 +02:00
throw new OperationError ( "Could not identify a key manager." ) ;
2018-05-15 11:15:31 +02:00
}
} else {
2018-05-15 19:01:04 +02:00
throw new OperationError ( "The data does not appear to be signed." ) ;
2018-05-15 11:15:31 +02:00
}
} catch ( err ) {
2018-05-15 19:01:04 +02:00
throw new OperationError ( ` Couldn't verify message: ${ err } ` ) ;
2018-05-15 11:15:31 +02:00
}
}
}
export default PGPDecryptAndVerify ;