Added --b64 command to output the B64 Destination of a public OpenPGP keyring

This commit is contained in:
str4d
2013-04-19 09:50:23 +00:00
parent 5bf4e000b3
commit e0eea6512e
2 changed files with 73 additions and 12 deletions

View File

@ -67,6 +67,7 @@ public class OpenPGPDest extends OpenPGPFile {
System.err.println("Syntax: OpenPGPDest [options] <command>");
System.err.println("");
System.err.println("Commands:");
System.err.println(" -b, --b64 <pubFile>");
System.err.println(" -e, --export <eepPriv.dat> <pgpFile>");
System.err.println(" -i, --import <pgpFile> <eepPriv.dat>");
System.err.println("");
@ -97,7 +98,16 @@ public class OpenPGPDest extends OpenPGPFile {
// Parse commands
OpenPGPDest opf = null;
try {
if (args[numOpts].equals("-e") || args[numOpts].equals("--export")) {
if (args[numOpts].equals("-b") || args[numOpts].equals("--b64")) {
if (args.length-numOpts < 2) {
System.err.println("Usage: OpenPGPDest "+args[numOpts]+" <pubFile>");
return;
}
opf = new OpenPGPDest();
opf.readOpenPGPPublicFile(new File(args[numOpts+1]));
opf.importKeys();
System.out.println(opf.getDestination().toBase64());
} else if (args[numOpts].equals("-e") || args[numOpts].equals("--export")) {
if (args.length-numOpts < 3) {
System.err.println("Usage: OpenPGPDest "+args[numOpts]+" <eepPriv.dat> <pgpFile> [pubFile]");
return;
@ -120,7 +130,7 @@ public class OpenPGPDest extends OpenPGPFile {
System.out.print("GPG passphrase to decrypt with: ");
char[] passPhrase = br.readLine().toCharArray();
opf = new OpenPGPDest();
opf.readOpenPGPFile(new File(args[numOpts+1]), passPhrase);
opf.readOpenPGPSecretFile(new File(args[numOpts+1]), passPhrase);
opf.importKeys();
opf.writePrivateKeyFile(new File(args[numOpts+2]), forceWrite);
}
@ -213,9 +223,7 @@ public class OpenPGPDest extends OpenPGPFile {
*/
public void importKeys() throws IllegalArgumentException {
DSAPublicBCPGKey sigPubKey = (DSAPublicBCPGKey)this.pgpTopKeyPair.getPublicKey().getPublicKeyPacket().getKey();
DSASecretBCPGKey sigPrivKey = (DSASecretBCPGKey)this.pgpTopKeyPair.getPrivateKey().getPrivateKeyDataPacket();
ElGamalPublicBCPGKey encPubKey = (ElGamalPublicBCPGKey)this.pgpSubKeyPairs.get(0).getPublicKey().getPublicKeyPacket().getKey();
ElGamalSecretBCPGKey encPrivKey = (ElGamalSecretBCPGKey)this.pgpSubKeyPairs.get(0).getPrivateKey().getPrivateKeyDataPacket();
// Verify the cryptographic constants
if (!CryptoConstants.dsap.equals(sigPubKey.getP()) ||
@ -228,9 +236,7 @@ public class OpenPGPDest extends OpenPGPFile {
// BigInteger.toByteArray returns SIGNED integers, but since they're
// positive, signed two's complement is the same as unsigned
byte[] pubKeyData = encPubKey.getY().toByteArray();
byte[] privKeyData = encPrivKey.getX().toByteArray();
byte[] signingPubKeyData = sigPubKey.getY().toByteArray();
byte[] signingPrivKeyData = sigPrivKey.getX().toByteArray();
PublicKey pubKey = new PublicKey();
pubKey.setData(padBuffer(pubKeyData, PublicKey.KEYSIZE_BYTES));
@ -253,15 +259,30 @@ public class OpenPGPDest extends OpenPGPFile {
this.dest.setSigningPublicKey(signingPubKey);
this.dest.setCertificate(cert);
this.privKey = new PrivateKey();
this.privKey.setData(padBuffer(privKeyData, PrivateKey.KEYSIZE_BYTES));
if (this.pgpTopKeyPair.getPrivateKey() != null && this.pgpSubKeyPairs.get(0).getPrivateKey() != null) {
DSASecretBCPGKey sigPrivKey = (DSASecretBCPGKey)this.pgpTopKeyPair.getPrivateKey().getPrivateKeyDataPacket();
ElGamalSecretBCPGKey encPrivKey = (ElGamalSecretBCPGKey)this.pgpSubKeyPairs.get(0).getPrivateKey().getPrivateKeyDataPacket();
this.signingPrivKey = new SigningPrivateKey();
this.signingPrivKey.setData(padBuffer(signingPrivKeyData, SigningPrivateKey.KEYSIZE_BYTES));
byte[] privKeyData = encPrivKey.getX().toByteArray();
byte[] signingPrivKeyData = sigPrivKey.getX().toByteArray();
this.privKey = new PrivateKey();
this.privKey.setData(padBuffer(privKeyData, PrivateKey.KEYSIZE_BYTES));
this.signingPrivKey = new SigningPrivateKey();
this.signingPrivKey.setData(padBuffer(signingPrivKeyData, SigningPrivateKey.KEYSIZE_BYTES));
} else {
this.privKey = null;
this.signingPrivKey = null;
}
this.lastMod = pgpTopKeyPair.getPublicKey().getCreationTime();
}
public Destination getDestination() {
return this.dest;
}
// I2P key representations
private Destination dest;

View File

@ -20,6 +20,7 @@ import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
@ -58,9 +59,48 @@ public abstract class OpenPGPFile {
}
/**
* Read in OpenPGP keys from a file.
* Read in OpenPGP keys from a public keyring file.
*/
public void readOpenPGPFile(File pgpFile, char[] passPhrase) throws FileNotFoundException, IOException, PGPException {
public void readOpenPGPPublicFile(File pubFile) throws FileNotFoundException, IOException, PGPException {
PGPPublicKeyRing pubKeys = new PGPPublicKeyRing(
PGPUtil.getDecoderStream(new FileInputStream(pubFile)),
new JcaKeyFingerprintCalculator());
Iterator it = pubKeys.getPublicKeys();
// Set the top key pair
PGPPublicKey pgpTopPublic = (PGPPublicKey)it.next();
this.pgpTopKeyPair = new PGPKeyPair(pgpTopPublic, null);
// Set any subkey pairs
this.pgpSubKeyPairs.clear();
while (it.hasNext()) {
PGPPublicKey pgpSubPublic = (PGPPublicKey)it.next();
this.pgpSubKeyPairs.add(new PGPKeyPair(pgpSubPublic, null));
}
// Set any included I2P DataStructures (other user attributes are ignored)
this.dataStructures = null;
Iterator itUA = pgpTopPublic.getUserAttributes();
if (itUA.hasNext()) {
this.dataStructures = new PGPI2PDataStructureAttributeVectorGenerator();
while (itUA.hasNext()) {
PGPUserAttributeSubpacketVector userAttrs = (PGPUserAttributeSubpacketVector)itUA.next();
// Check the subpacket vector for I2P DataStructures
this.dataStructures.setFrom(userAttrs);
}
}
// Set any included identities
this.identities.clear();
Iterator uids = pgpTopPublic.getUserIDs();
while(uids.hasNext())
this.identities.add((String)uids.next());
}
/**
* Read in OpenPGP keys from a secret keyring file.
*/
public void readOpenPGPSecretFile(File pgpFile, char[] passPhrase) throws FileNotFoundException, IOException, PGPException {
PGPSecretKeyRing pgpKeys = new PGPSecretKeyRing(
PGPUtil.getDecoderStream(new FileInputStream(pgpFile)),
new JcaKeyFingerprintCalculator());