diff options
author | David Hook <dgh@cryptoworkshop.com> | 2014-04-21 10:18:25 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2014-04-21 10:18:25 +0400 |
commit | fc9f9d428ec82f65ce88603298e3236ec75b5930 (patch) | |
tree | d326e79fa499daf53c72e2fd2f0cc05b59302c22 | |
parent | 65f345d7758d46deb82ccaeed7af38c5d498ece5 (diff) |
cleanup of deprecations. Added new exception to deal with irregular use of illegal state exception and others.
21 files changed, 229 insertions, 953 deletions
diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPKeyPair.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPKeyPair.java index 856468e4..81c03f08 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPKeyPair.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPKeyPair.java @@ -1,20 +1,5 @@ package org.bouncycastle.openpgp; -import java.security.KeyPair; -import java.security.NoSuchProviderException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.interfaces.DSAPrivateKey; -import java.security.interfaces.RSAPrivateCrtKey; -import java.util.Date; - -import org.bouncycastle.bcpg.BCPGKey; -import org.bouncycastle.bcpg.DSASecretBCPGKey; -import org.bouncycastle.bcpg.ElGamalSecretBCPGKey; -import org.bouncycastle.bcpg.RSASecretBCPGKey; -import org.bouncycastle.jce.interfaces.ElGamalPrivateKey; - - /** * General class to handle JCA key pairs and convert them into OpenPGP ones. * <p> @@ -30,85 +15,6 @@ public class PGPKeyPair protected PGPPrivateKey priv; /** - * @deprecated use BcPGPKeyPair or JcaPGPKeyPair as appropriate. - */ - public PGPKeyPair( - int algorithm, - KeyPair keyPair, - Date time, - String provider) - throws PGPException, NoSuchProviderException - { - this(algorithm, keyPair.getPublic(), keyPair.getPrivate(), time, provider); - } - - /** - * @deprecated use BcPGPKeyPair or JcaPGPKeyPair as appropriate. - */ - public PGPKeyPair( - int algorithm, - KeyPair keyPair, - Date time) - throws PGPException - { - this(algorithm, keyPair.getPublic(), keyPair.getPrivate(), time); - } - - /** - * @deprecated use BcPGPKeyPair or JcaPGPKeyPair as appropriate. - */ - public PGPKeyPair( - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time, - String provider) - throws PGPException, NoSuchProviderException - { - this(algorithm, pubKey, privKey, time); - } - - /** - * @deprecated use BcPGPKeyPair or JcaPGPKeyPair as appropriate. - */ - public PGPKeyPair( - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time) - throws PGPException - { - this.pub = new PGPPublicKey(algorithm, pubKey, time); - - BCPGKey privPk; - - switch (pub.getAlgorithm()) - { - case PGPPublicKey.RSA_ENCRYPT: - case PGPPublicKey.RSA_SIGN: - case PGPPublicKey.RSA_GENERAL: - RSAPrivateCrtKey rsK = (RSAPrivateCrtKey)privKey; - - privPk = new RSASecretBCPGKey(rsK.getPrivateExponent(), rsK.getPrimeP(), rsK.getPrimeQ()); - break; - case PGPPublicKey.DSA: - DSAPrivateKey dsK = (DSAPrivateKey)privKey; - - privPk = new DSASecretBCPGKey(dsK.getX()); - break; - case PGPPublicKey.ELGAMAL_ENCRYPT: - case PGPPublicKey.ELGAMAL_GENERAL: - ElGamalPrivateKey esK = (ElGamalPrivateKey)privKey; - - privPk = new ElGamalSecretBCPGKey(esK.getX()); - break; - default: - throw new PGPException("unknown key class"); - } - this.priv = new PGPPrivateKey(pub.getKeyID(), pub.getPublicKeyPacket(), privPk); - } - - /** * Create a key pair from a PGPPrivateKey and a PGPPublicKey. * * @param pub the public key diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPOnePassSignature.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPOnePassSignature.java index a6fb8099..beb631b6 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPOnePassSignature.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPOnePassSignature.java @@ -3,9 +3,6 @@ package org.bouncycastle.openpgp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SignatureException; import org.bouncycastle.bcpg.BCPGInputStream; import org.bouncycastle.bcpg.BCPGOutputStream; @@ -13,7 +10,6 @@ import org.bouncycastle.bcpg.OnePassSignaturePacket; import org.bouncycastle.openpgp.operator.PGPContentVerifier; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilder; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; /** * A one pass signature object. @@ -41,39 +37,6 @@ public class PGPOnePassSignature this.sigPack = sigPack; this.signatureType = sigPack.getSignatureType(); } - - /** - * Initialise the signature object for verification. - * - * @param pubKey - * @param provider - * @throws NoSuchProviderException - * @throws PGPException - * @deprecated use init() method. - */ - public void initVerify( - PGPPublicKey pubKey, - String provider) - throws NoSuchProviderException, PGPException - { - initVerify(pubKey, PGPUtil.getProvider(provider)); - } - - /** - * Initialise the signature object for verification. - * - * @param pubKey - * @param provider - * @throws PGPException - * @deprecated use init() method. - */ - public void initVerify( - PGPPublicKey pubKey, - Provider provider) - throws PGPException - { - init(new JcaPGPContentVerifierBuilderProvider().setProvider(provider), pubKey); - } /** * Initialise the signature object for verification. @@ -95,7 +58,6 @@ public class PGPOnePassSignature public void update( byte b) - throws SignatureException { if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -127,7 +89,6 @@ public class PGPOnePassSignature public void update( byte[] bytes) - throws SignatureException { if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -146,7 +107,6 @@ public class PGPOnePassSignature byte[] bytes, int off, int length) - throws SignatureException { if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -164,20 +124,18 @@ public class PGPOnePassSignature } private void byteUpdate(byte b) - throws SignatureException { try { sigOut.write(b); } catch (IOException e) - { // TODO: we really should get rid of signature exception next.... - throw new SignatureException(e.getMessage()); + { + throw new PGPRuntimeOperationException(e.getMessage(), e); } } private void blockUpdate(byte[] block, int off, int len) - throws SignatureException { try { @@ -185,7 +143,7 @@ public class PGPOnePassSignature } catch (IOException e) { - throw new IllegalStateException(e.getMessage()); + throw new PGPRuntimeOperationException(e.getMessage(), e); } } @@ -195,11 +153,10 @@ public class PGPOnePassSignature * @param pgpSig * @return boolean * @throws PGPException - * @throws SignatureException */ public boolean verify( PGPSignature pgpSig) - throws PGPException, SignatureException + throws PGPException { try { diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPPrivateKey.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPPrivateKey.java index 52d9d1a1..1cd1de78 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPPrivateKey.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPPrivateKey.java @@ -1,16 +1,7 @@ package org.bouncycastle.openpgp; -import java.security.PrivateKey; -import java.security.interfaces.DSAPrivateKey; -import java.security.interfaces.RSAPrivateCrtKey; - import org.bouncycastle.bcpg.BCPGKey; -import org.bouncycastle.bcpg.DSASecretBCPGKey; -import org.bouncycastle.bcpg.ElGamalSecretBCPGKey; import org.bouncycastle.bcpg.PublicKeyPacket; -import org.bouncycastle.bcpg.RSASecretBCPGKey; -import org.bouncycastle.jce.interfaces.ElGamalPrivateKey; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; /** * general class to contain a private key for use with other openPGP @@ -19,51 +10,10 @@ import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; public class PGPPrivateKey { private long keyID; - private PrivateKey privateKey; private PublicKeyPacket publicKeyPacket; private BCPGKey privateKeyDataPacket; /** - * Create a PGPPrivateKey from a regular private key and the keyID of its associated - * public key. - * - * @param privateKey private key tu use. - * @param keyID keyID of the corresponding public key. - * @deprecated use JcaPGPKeyConverter - */ - public PGPPrivateKey( - PrivateKey privateKey, - long keyID) - { - this.privateKey = privateKey; - this.keyID = keyID; - - if (privateKey instanceof RSAPrivateCrtKey) - { - RSAPrivateCrtKey rsK = (RSAPrivateCrtKey)privateKey; - - privateKeyDataPacket = new RSASecretBCPGKey(rsK.getPrivateExponent(), rsK.getPrimeP(), rsK.getPrimeQ()); - } - else if (privateKey instanceof DSAPrivateKey) - { - DSAPrivateKey dsK = (DSAPrivateKey)privateKey; - - privateKeyDataPacket = new DSASecretBCPGKey(dsK.getX()); - } - else if (privateKey instanceof ElGamalPrivateKey) - { - ElGamalPrivateKey esK = (ElGamalPrivateKey)privateKey; - - privateKeyDataPacket = new ElGamalSecretBCPGKey(esK.getX()); - } - else - { - throw new IllegalArgumentException("unknown key class"); - } - - } - - /** * Base constructor. * * Create a PGPPrivateKey from a keyID and the associated public/private data packets needed @@ -92,29 +42,6 @@ public class PGPPrivateKey { return keyID; } - - /** - * Return the contained private key. - * - * @return PrivateKey - * @deprecated use a JcaPGPKeyConverter - */ - public PrivateKey getKey() - { - if (privateKey != null) - { - return privateKey; - } - - try - { - return new JcaPGPKeyConverter().setProvider(PGPUtil.getDefaultProvider()).getPrivateKey(this); - } - catch (PGPException e) - { - throw new IllegalStateException("unable to convert key: " + e.toString()); - } - } /** * Return the public key packet associated with this private key, if available. diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPRuntimeOperationException.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPRuntimeOperationException.java new file mode 100644 index 00000000..c4067ec0 --- /dev/null +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPRuntimeOperationException.java @@ -0,0 +1,19 @@ +package org.bouncycastle.openpgp; + +public class PGPRuntimeOperationException + extends RuntimeException +{ + private final Throwable cause; + + public PGPRuntimeOperationException(String message, Throwable cause) + { + super(message); + + this.cause = cause; + } + + public Throwable getCause() + { + return cause; + } +} diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKey.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKey.java index 8ac0bd86..7d1a9e38 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKey.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKey.java @@ -4,13 +4,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.security.NoSuchProviderException; -import java.security.PrivateKey; -import java.security.Provider; -import java.security.PublicKey; -import java.security.SecureRandom; import java.util.ArrayList; -import java.util.Date; import java.util.Iterator; import java.util.List; @@ -33,10 +27,6 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.PGPDigestCalculator; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; -import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; -import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; /** * general class to handle a PGP secret key object. @@ -144,43 +134,6 @@ public class PGPSecretKey } } - /** - * @deprecated use method taking PBESecretKeyEncryptor - */ - public PGPSecretKey( - int certificationLevel, - PGPKeyPair keyPair, - String id, - int encAlgorithm, - char[] passPhrase, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - this(certificationLevel, keyPair, id, encAlgorithm, passPhrase, false, hashedPcks, unhashedPcks, rand, provider); - } - - /** - * @deprecated use method taking PBESecretKeyEncryptor - */ - public PGPSecretKey( - int certificationLevel, - PGPKeyPair keyPair, - String id, - int encAlgorithm, - char[] passPhrase, - boolean useSHA1, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - this(certificationLevel, keyPair, id, encAlgorithm, passPhrase, useSHA1, hashedPcks, unhashedPcks, rand, PGPUtil.getProvider(provider)); - } - public PGPSecretKey( int certificationLevel, PGPKeyPair keyPair, @@ -194,31 +147,6 @@ public class PGPSecretKey this(certificationLevel, keyPair, id, null, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); } - /** - * @deprecated use method taking PBESecretKeyEncryptor - */ - public PGPSecretKey( - int certificationLevel, - PGPKeyPair keyPair, - String id, - int encAlgorithm, - char[] passPhrase, - boolean useSHA1, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - SecureRandom rand, - Provider provider) - throws PGPException - { - this(keyPair.getPrivateKey(), certifiedPublicKey(certificationLevel, keyPair, id, hashedPcks, unhashedPcks, new JcaPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(provider)), convertSHA1Flag(useSHA1), true, new JcePBESecretKeyEncryptorBuilder(encAlgorithm, new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1)).setProvider(provider).setSecureRandom(rand).build(passPhrase)); - } - - private static PGPDigestCalculator convertSHA1Flag(boolean useSHA1) - throws PGPException - { - return useSHA1 ? new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1) : null; - } - public PGPSecretKey( int certificationLevel, PGPKeyPair keyPair, @@ -273,88 +201,6 @@ public class PGPSecretKey } } - /** - * @deprecated use method taking PBESecretKeyEncryptor - */ - public PGPSecretKey( - int certificationLevel, - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time, - String id, - int encAlgorithm, - char[] passPhrase, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - this(certificationLevel, new PGPKeyPair(algorithm,pubKey, privKey, time), id, encAlgorithm, passPhrase, hashedPcks, unhashedPcks, rand, provider); - } - - /** - * @deprecated use method taking PBESecretKeyEncryptor - */ - public PGPSecretKey( - int certificationLevel, - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time, - String id, - int encAlgorithm, - char[] passPhrase, - boolean useSHA1, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - this(certificationLevel, new PGPKeyPair(algorithm, pubKey, privKey, time), id, encAlgorithm, passPhrase, useSHA1, hashedPcks, unhashedPcks, rand, provider); - } - - /** - * @deprecated use method taking PGPKeyPair - */ - public PGPSecretKey( - int certificationLevel, - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time, - String id, - PGPDigestCalculator checksumCalculator, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - PGPContentSignerBuilder certificationSignerBuilder, - PBESecretKeyEncryptor keyEncryptor) - throws PGPException - { - this(certificationLevel, new PGPKeyPair(algorithm, pubKey, privKey, time), id, checksumCalculator, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); - } - - /** - * @deprecated use method taking PGPKeyPair - */ - public PGPSecretKey( - int certificationLevel, - int algorithm, - PublicKey pubKey, - PrivateKey privKey, - Date time, - String id, - PGPSignatureSubpacketVector hashedPcks, - PGPSignatureSubpacketVector unhashedPcks, - PGPContentSignerBuilder certificationSignerBuilder, - PBESecretKeyEncryptor keyEncryptor) - throws PGPException, NoSuchProviderException - { - this(certificationLevel, new PGPKeyPair(algorithm, pubKey, privKey, time), id, null, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); - } - /** * Return true if this key has an algorithm type that makes it suitable to use for signing. * <p> @@ -544,41 +390,6 @@ public class PGPSecretKey /** * Extract a PGPPrivate key from the SecretKey's encrypted contents. - * - * @param passPhrase - * @param provider - * @return PGPPrivateKey - * @throws PGPException - * @throws NoSuchProviderException - * @deprecated use method that takes a PBESecretKeyDecryptor - */ - public PGPPrivateKey extractPrivateKey( - char[] passPhrase, - String provider) - throws PGPException, NoSuchProviderException - { - return extractPrivateKey(passPhrase, PGPUtil.getProvider(provider)); - } - - /** - * Extract a PGPPrivate key from the SecretKey's encrypted contents. - * - * @param passPhrase - * @param provider - * @return PGPPrivateKey - * @throws PGPException - * @deprecated use method that takes a PBESecretKeyDecryptor - */ - public PGPPrivateKey extractPrivateKey( - char[] passPhrase, - Provider provider) - throws PGPException - { - return extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider(provider).build()).setProvider(provider).build(passPhrase)); - } - - /** - * Extract a PGPPrivate key from the SecretKey's encrypted contents. * * @param decryptorFactory factory to use to generate a decryptor for the passed in secretKey. * @return PGPPrivateKey the unencrypted private key. @@ -749,30 +560,6 @@ public class PGPSecretKey * password and the passed in algorithm. * * @param key the PGPSecretKey to be copied. - * @param oldPassPhrase the current password for key. - * @param newPassPhrase the new password for the key. - * @param newEncAlgorithm the algorithm to be used for the encryption. - * @param rand source of randomness. - * @param provider name of the provider to use - * @deprecated use method taking PBESecretKeyDecryptor and PBESecretKeyEncryptor - */ - public static PGPSecretKey copyWithNewPassword( - PGPSecretKey key, - char[] oldPassPhrase, - char[] newPassPhrase, - int newEncAlgorithm, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - return copyWithNewPassword(key, oldPassPhrase, newPassPhrase, newEncAlgorithm, rand, PGPUtil.getProvider(provider)); - } - - /** - * Return a copy of the passed in secret key, encrypted using a new - * password and the passed in algorithm. - * - * @param key the PGPSecretKey to be copied. * @param oldKeyDecryptor the current decryptor based on the current password for key. * @param newKeyEncryptor a new encryptor based on a new password for encrypting the secret key material. */ @@ -894,30 +681,6 @@ public class PGPSecretKey } /** - * Return a copy of the passed in secret key, encrypted using a new - * password and the passed in algorithm. - * - * @param key the PGPSecretKey to be copied. - * @param oldPassPhrase the current password for key. - * @param newPassPhrase the new password for the key. - * @param newEncAlgorithm the algorithm to be used for the encryption. - * @param rand source of randomness. - * @param provider the provider to use - * @deprecated use method taking PBESecretKeyDecryptor and PBESecretKeyEncryptor - */ - public static PGPSecretKey copyWithNewPassword( - PGPSecretKey key, - char[] oldPassPhrase, - char[] newPassPhrase, - int newEncAlgorithm, - SecureRandom rand, - Provider provider) - throws PGPException - { - return copyWithNewPassword(key, new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider(provider).build()).setProvider(provider).build(oldPassPhrase), new JcePBESecretKeyEncryptorBuilder(newEncAlgorithm).setProvider(provider).setSecureRandom(rand).build(newPassPhrase)); - } - - /** * Replace the passed the public key on the passed in secret key. * * @param secretKey secret key to change diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java index 2e30e737..538c4719 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java @@ -5,9 +5,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -22,7 +19,6 @@ import org.bouncycastle.bcpg.TrustPacket; import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; -import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; /** * Class to hold a single master secret key and its subkeys. @@ -47,16 +43,6 @@ public class PGPSecretKeyRing this.extraPubKeys = extraPubKeys; } - /** - * @deprecated use version that takes KeyFingerprintCalculator - */ - public PGPSecretKeyRing( - byte[] encoding) - throws IOException, PGPException - { - this(new ByteArrayInputStream(encoding)); - } - public PGPSecretKeyRing( byte[] encoding, KeyFingerPrintCalculator fingerPrintCalculator) @@ -65,16 +51,6 @@ public class PGPSecretKeyRing this(new ByteArrayInputStream(encoding), fingerPrintCalculator); } - /** - * @deprecated use version that takes KeyFingerprintCalculator - */ - public PGPSecretKeyRing( - InputStream in) - throws IOException, PGPException - { - this(in, new JcaKeyFingerprintCalculator()); - } - public PGPSecretKeyRing( InputStream in, KeyFingerPrintCalculator fingerPrintCalculator) @@ -305,61 +281,6 @@ public class PGPSecretKeyRing } /** - * Return a copy of the passed in secret key ring, with the master key and sub keys encrypted - * using a new password and the passed in algorithm. - * - * @param ring the PGPSecretKeyRing to be copied. - * @param oldPassPhrase the current password for key. - * @param newPassPhrase the new password for the key. - * @param newEncAlgorithm the algorithm to be used for the encryption. - * @param rand source of randomness. - * @param provider name of the provider to use - * @deprecated use version taking PBESecretKeyEncryptor/PBESecretKeyDecryptor - */ - public static PGPSecretKeyRing copyWithNewPassword( - PGPSecretKeyRing ring, - char[] oldPassPhrase, - char[] newPassPhrase, - int newEncAlgorithm, - SecureRandom rand, - String provider) - throws PGPException, NoSuchProviderException - { - return copyWithNewPassword(ring, oldPassPhrase, newPassPhrase, newEncAlgorithm, rand, PGPUtil.getProvider(provider)); - } - - /** - * Return a copy of the passed in secret key ring, with the master key and sub keys encrypted - * using a new password and the passed in algorithm. - * - * @param ring the PGPSecretKeyRing to be copied. - * @param oldPassPhrase the current password for key. - * @param newPassPhrase the new password for the key. - * @param newEncAlgorithm the algorithm to be used for the encryption. - * @param rand source of randomness. - * @param provider provider to use - * @deprecated use version taking PBESecretKeyEncryptor/PBESecretKeyDecryptor - */ - public static PGPSecretKeyRing copyWithNewPassword( - PGPSecretKeyRing ring, - char[] oldPassPhrase, - char[] newPassPhrase, - int newEncAlgorithm, - SecureRandom rand, - Provider provider) - throws PGPException - { - List newKeys = new ArrayList(ring.keys.size()); - - for (Iterator keys = ring.getSecretKeys(); keys.hasNext();) - { - newKeys.add(PGPSecretKey.copyWithNewPassword((PGPSecretKey)keys.next(), oldPassPhrase, newPassPhrase, newEncAlgorithm, rand, provider)); - } - - return new PGPSecretKeyRing(newKeys, ring.extraPubKeys); - } - - /** * Return a copy of the passed in secret key ring, with the private keys (where present) associated with the master key and sub keys * are encrypted using a new password and the passed in algorithm. * diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java index fe124e64..4bd7ea0a 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java @@ -3,9 +3,6 @@ package org.bouncycastle.openpgp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SignatureException; import java.util.Date; import org.bouncycastle.asn1.ASN1EncodableVector; @@ -21,7 +18,6 @@ import org.bouncycastle.bcpg.UserAttributeSubpacket; import org.bouncycastle.openpgp.operator.PGPContentVerifier; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilder; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.Strings; @@ -108,28 +104,6 @@ public class PGPSignature return sigPck.getHashAlgorithm(); } - /** - * @deprecated use init(PGPContentVerifierBuilderProvider, PGPPublicKey) - */ - public void initVerify( - PGPPublicKey pubKey, - String provider) - throws NoSuchProviderException, PGPException - { - initVerify(pubKey, PGPUtil.getProvider(provider)); - } - - /** - * @deprecated use init(PGPContentVerifierBuilderProvider, PGPPublicKey) - */ - public void initVerify( - PGPPublicKey pubKey, - Provider provider) - throws PGPException - { - init(new JcaPGPContentVerifierBuilderProvider().setProvider(provider), pubKey); - } - public void init(PGPContentVerifierBuilderProvider verifierBuilderProvider, PGPPublicKey pubKey) throws PGPException { @@ -143,7 +117,6 @@ public class PGPSignature public void update( byte b) - throws SignatureException { if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -175,7 +148,6 @@ public class PGPSignature public void update( byte[] bytes) - throws SignatureException { this.update(bytes, 0, bytes.length); } @@ -184,7 +156,6 @@ public class PGPSignature byte[] bytes, int off, int length) - throws SignatureException { if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -202,20 +173,18 @@ public class PGPSignature } private void byteUpdate(byte b) - throws SignatureException { try { sigOut.write(b); } catch (IOException e) - { // TODO: we really should get rid of signature exception next.... - throw new SignatureException(e.getMessage()); + { + throw new PGPRuntimeOperationException(e.getMessage(), e); } } private void blockUpdate(byte[] block, int off, int len) - throws SignatureException { try { @@ -223,12 +192,12 @@ public class PGPSignature } catch (IOException e) { - throw new IllegalStateException(e.getMessage()); + throw new PGPRuntimeOperationException(e.getMessage(), e); } } public boolean verify() - throws PGPException, SignatureException + throws PGPException { try { @@ -238,7 +207,7 @@ public class PGPSignature } catch (IOException e) { - throw new SignatureException(e.getMessage()); + throw new PGPException(e.getMessage(), e); } return verifier.verify(this.getSignature()); @@ -246,7 +215,6 @@ public class PGPSignature private void updateWithIdData(int header, byte[] idBytes) - throws SignatureException { this.update((byte)header); this.update((byte)(idBytes.length >> 24)); @@ -257,7 +225,7 @@ public class PGPSignature } private void updateWithPublicKey(PGPPublicKey key) - throws PGPException, SignatureException + throws PGPException { byte[] keyBytes = getEncodedPublicKey(key); @@ -275,12 +243,11 @@ public class PGPSignature * @param key the key to be verified. * @return true if the signature matches, false otherwise. * @throws PGPException - * @throws SignatureException */ public boolean verifyCertification( PGPUserAttributeSubpacketVector userAttributes, PGPPublicKey key) - throws PGPException, SignatureException + throws PGPException { if (verifier == null) { @@ -320,12 +287,11 @@ public class PGPSignature * @param key the key to be verified. * @return true if the signature matches, false otherwise. * @throws PGPException - * @throws SignatureException */ public boolean verifyCertification( String id, PGPPublicKey key) - throws PGPException, SignatureException + throws PGPException { if (verifier == null) { @@ -351,13 +317,12 @@ public class PGPSignature * @param masterKey the key we are verifying against. * @param pubKey the key we are verifying. * @return true if the certification is valid, false otherwise. - * @throws SignatureException * @throws PGPException */ public boolean verifyCertification( PGPPublicKey masterKey, PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { if (verifier == null) { @@ -373,7 +338,6 @@ public class PGPSignature } private void addTrailer() - throws SignatureException { try { @@ -383,7 +347,7 @@ public class PGPSignature } catch (IOException e) { - throw new SignatureException(e.getMessage()); + throw new PGPRuntimeOperationException(e.getMessage(), e); } } @@ -392,12 +356,11 @@ public class PGPSignature * * @param pubKey the key we are checking. * @return true if the certification is valid, false otherwise. - * @throws SignatureException * @throws PGPException */ public boolean verifyCertification( PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { if (verifier == null) { diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java index 853e64b9..2450e1ee 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java @@ -4,11 +4,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigInteger; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SecureRandom; -import java.security.SignatureException; import java.util.Date; import org.bouncycastle.bcpg.MPInteger; @@ -22,7 +17,6 @@ import org.bouncycastle.bcpg.sig.IssuerKeyID; import org.bouncycastle.bcpg.sig.SignatureCreationTime; import org.bouncycastle.openpgp.operator.PGPContentSigner; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.util.Strings; /** @@ -40,83 +34,6 @@ public class PGPSignatureGenerator private int providedKeyAlgorithm = -1; /** - * Create a generator for the passed in keyAlgorithm and hashAlgorithm codes. - * - * @param keyAlgorithm keyAlgorithm to use for signing - * @param hashAlgorithm algorithm to use for digest - * @param provider provider to use for digest algorithm - * @throws NoSuchAlgorithmException - * @throws NoSuchProviderException - * @throws PGPException - * @deprecated use method taking a PGPContentSignerBuilder - */ - public PGPSignatureGenerator( - int keyAlgorithm, - int hashAlgorithm, - String provider) - throws NoSuchAlgorithmException, NoSuchProviderException, PGPException - { - this(keyAlgorithm, provider, hashAlgorithm, provider); - } - - /** - * Create a generator for the passed in keyAlgorithm and hashAlgorithm codes. - * - * @deprecated use method taking a PGPContentSignerBuilder - */ - public PGPSignatureGenerator( - int keyAlgorithm, - int hashAlgorithm, - Provider provider) - throws NoSuchAlgorithmException, PGPException - { - this(keyAlgorithm, provider, hashAlgorithm, provider); - } - - /** - * Create a generator for the passed in keyAlgorithm and hashAlgorithm codes. - * - * @param keyAlgorithm keyAlgorithm to use for signing - * @param sigProvider provider to use for signature generation - * @param hashAlgorithm algorithm to use for digest - * @param digProvider provider to use for digest algorithm - * @throws NoSuchAlgorithmException - * @throws NoSuchProviderException - * @throws PGPException - * @deprecated use method taking a PGPContentSignerBuilder - */ - public PGPSignatureGenerator( - int keyAlgorithm, - String sigProvider, - int hashAlgorithm, - String digProvider) - throws NoSuchAlgorithmException, NoSuchProviderException, PGPException - { - this(keyAlgorithm, PGPUtil.getProvider(sigProvider), hashAlgorithm, PGPUtil.getProvider(digProvider)); - } - - /** - * - * @param keyAlgorithm - * @param sigProvider - * @param hashAlgorithm - * @param digProvider - * @throws NoSuchAlgorithmException - * @throws PGPException - * @deprecated use constructor taking PGPContentSignerBuilder. - */ - public PGPSignatureGenerator( - int keyAlgorithm, - Provider sigProvider, - int hashAlgorithm, - Provider digProvider) - throws NoSuchAlgorithmException, PGPException - { - this.providedKeyAlgorithm = keyAlgorithm; - this.contentSignerBuilder = new JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm).setProvider(sigProvider).setDigestProvider(digProvider); - } - - /** * Create a signature generator built on the passed in contentSignerBuilder. * * @param contentSignerBuilder builder to produce PGPContentSigner objects for generating signatures. @@ -129,30 +46,6 @@ public class PGPSignatureGenerator /** * Initialise the generator for signing. - * - * @param signatureType - * @param key - * @throws PGPException - * @deprecated use init() method - */ - public void initSign( - int signatureType, - PGPPrivateKey key) - throws PGPException - { - contentSigner = contentSignerBuilder.build(signatureType, key); - sigOut = contentSigner.getOutputStream(); - sigType = contentSigner.getType(); - lastb = 0; - - if (providedKeyAlgorithm >= 0 && providedKeyAlgorithm != contentSigner.getKeyAlgorithm()) - { - throw new PGPException("key algorithm mismatch"); - } - } - - /** - * Initialise the generator for signing. * * @param signatureType * @param key @@ -173,28 +66,9 @@ public class PGPSignatureGenerator throw new PGPException("key algorithm mismatch"); } } - - /** - * Initialise the generator for signing. - * - * @param signatureType - * @param key - * @param random - * @throws PGPException - * @deprecated random parameter now ignored. - */ - public void initSign( - int signatureType, - PGPPrivateKey key, - SecureRandom random) - throws PGPException - { - initSign(signatureType, key); - } public void update( - byte b) - throws SignatureException + byte b) { if (sigType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -225,8 +99,7 @@ public class PGPSignatureGenerator } public void update( - byte[] b) - throws SignatureException + byte[] b) { this.update(b, 0, b.length); } @@ -234,8 +107,7 @@ public class PGPSignatureGenerator public void update( byte[] b, int off, - int len) - throws SignatureException + int len) { if (sigType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -253,20 +125,18 @@ public class PGPSignatureGenerator } private void byteUpdate(byte b) - throws SignatureException { try { sigOut.write(b); } catch (IOException e) - { // TODO: we really should get rid of signature exception next.... - throw new SignatureException(e.getMessage()); + { + throw new PGPRuntimeOperationException(e.getMessage(), e); } } private void blockUpdate(byte[] block, int off, int len) - throws SignatureException { try { @@ -274,7 +144,7 @@ public class PGPSignatureGenerator } catch (IOException e) { - throw new IllegalStateException(e.getMessage()); + throw new PGPRuntimeOperationException(e.getMessage(), e); } } @@ -321,10 +191,9 @@ public class PGPSignatureGenerator * * @return PGPSignature * @throws PGPException - * @throws SignatureException */ public PGPSignature generate() - throws PGPException, SignatureException + throws PGPException { MPInteger[] sigValues; int version = 4; @@ -413,13 +282,12 @@ public class PGPSignatureGenerator * @param id the id we are certifying against the public key. * @param pubKey the key we are certifying against the id. * @return the certification. - * @throws SignatureException * @throws PGPException */ public PGPSignature generateCertification( String id, PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { updateWithPublicKey(pubKey); @@ -436,13 +304,12 @@ public class PGPSignatureGenerator * @param userAttributes the id we are certifying against the public key. * @param pubKey the key we are certifying against the id. * @return the certification. - * @throws SignatureException * @throws PGPException */ public PGPSignature generateCertification( PGPUserAttributeSubpacketVector userAttributes, PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { updateWithPublicKey(pubKey); @@ -474,13 +341,12 @@ public class PGPSignatureGenerator * @param masterKey the key we are certifying against. * @param pubKey the key we are certifying. * @return the certification. - * @throws SignatureException * @throws PGPException */ public PGPSignature generateCertification( PGPPublicKey masterKey, PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { updateWithPublicKey(masterKey); updateWithPublicKey(pubKey); @@ -493,12 +359,11 @@ public class PGPSignatureGenerator * * @param pubKey the key we are certifying. * @return the certification. - * @throws SignatureException * @throws PGPException */ public PGPSignature generateCertification( PGPPublicKey pubKey) - throws SignatureException, PGPException + throws PGPException { if ((sigType == PGPSignature.SUBKEY_REVOCATION || sigType == PGPSignature.SUBKEY_BINDING) && !pubKey.isMasterKey()) { @@ -556,7 +421,6 @@ public class PGPSignatureGenerator } private void updateWithIdData(int header, byte[] idBytes) - throws SignatureException { this.update((byte)header); this.update((byte)(idBytes.length >> 24)); @@ -567,7 +431,7 @@ public class PGPSignatureGenerator } private void updateWithPublicKey(PGPPublicKey key) - throws PGPException, SignatureException + throws PGPException { byte[] keyBytes = getEncodedPublicKey(key); diff --git a/pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java b/pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java index b57236e5..605e6608 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java @@ -4,11 +4,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigInteger; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SecureRandom; -import java.security.SignatureException; import java.util.Date; import org.bouncycastle.bcpg.MPInteger; @@ -17,7 +12,6 @@ import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; import org.bouncycastle.bcpg.SignaturePacket; import org.bouncycastle.openpgp.operator.PGPContentSigner; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; -import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; /** * Generator for old style PGP V3 Signatures. @@ -32,45 +26,6 @@ public class PGPV3SignatureGenerator private int providedKeyAlgorithm = -1; /** - * Create a generator for the passed in keyAlgorithm and hashAlgorithm codes. - * - * @param keyAlgorithm - * @param hashAlgorithm - * @param provider - * @throws NoSuchAlgorithmException - * @throws NoSuchProviderException - * @throws PGPException - * @deprecated use constructor taking PGPContentSignerBuilder. - */ - public PGPV3SignatureGenerator( - int keyAlgorithm, - int hashAlgorithm, - String provider) - throws NoSuchAlgorithmException, NoSuchProviderException, PGPException - { - this(keyAlgorithm, hashAlgorithm, PGPUtil.getProvider(provider)); - } - - /** - * - * @param keyAlgorithm - * @param hashAlgorithm - * @param provider - * @throws NoSuchAlgorithmException - * @throws PGPException - * @deprecated use constructor taking PGPContentSignerBuilder. - */ - public PGPV3SignatureGenerator( - int keyAlgorithm, - int hashAlgorithm, - Provider provider) - throws NoSuchAlgorithmException, PGPException - { - this.providedKeyAlgorithm = keyAlgorithm; - this.contentSignerBuilder = new JcaPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm).setProvider(provider); - } - - /** * Create a signature generator built on the passed in contentSignerBuilder. * * @param contentSignerBuilder builder to produce PGPContentSigner objects for generating signatures. @@ -104,43 +59,8 @@ public class PGPV3SignatureGenerator } } - /** - * Initialise the generator for signing. - * - * @param signatureType - * @param key - * @param random - * @throws PGPException - * @deprecated random now ignored - set random in PGPContentSignerBuilder - */ - public void initSign( - int signatureType, - PGPPrivateKey key, - SecureRandom random) - throws PGPException - { - init(signatureType, key); - } - - /** - * Initialise the generator for signing. - * - * @param signatureType - * @param key - * @throws PGPException - * @deprecated use init() - */ - public void initSign( - int signatureType, - PGPPrivateKey key) - throws PGPException - { - init(signatureType, key); - } - public void update( - byte b) - throws SignatureException + byte b) { if (sigType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -171,8 +91,7 @@ public class PGPV3SignatureGenerator } public void update( - byte[] b) - throws SignatureException + byte[] b) { this.update(b, 0, b.length); } @@ -180,8 +99,7 @@ public class PGPV3SignatureGenerator public void update( byte[] b, int off, - int len) - throws SignatureException + int len) { if (sigType == PGPSignature.CANONICAL_TEXT_DOCUMENT) { @@ -199,7 +117,6 @@ public class PGPV3SignatureGenerator } private void byteUpdate(byte b) - throws SignatureException { try { @@ -207,12 +124,11 @@ public class PGPV3SignatureGenerator } catch (IOException e) { - throw new IllegalStateException("unable to update signature"); + throw new PGPRuntimeOperationException("unable to update signature: " + e.getMessage(), e); } } private void blockUpdate(byte[] block, int off, int len) - throws SignatureException { try { @@ -220,7 +136,7 @@ public class PGPV3SignatureGenerator } catch (IOException e) { - throw new IllegalStateException("unable to update signature"); + throw new PGPRuntimeOperationException("unable to update signature: " + e.getMessage(), e); } } @@ -243,10 +159,9 @@ public class PGPV3SignatureGenerator * * @return PGPSignature * @throws PGPException - * @throws SignatureException */ public PGPSignature generate() - throws PGPException, SignatureException + throws PGPException { long creationTime = new Date().getTime() / 1000; diff --git a/pg/src/main/java/org/bouncycastle/openpgp/examples/RSAKeyPairGenerator.java b/pg/src/main/java/org/bouncycastle/openpgp/examples/RSAKeyPairGenerator.java index 673258c1..b8e9a652 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/examples/RSAKeyPairGenerator.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/examples/RSAKeyPairGenerator.java @@ -7,8 +7,6 @@ import java.security.InvalidKeyException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchProviderException; -import java.security.PrivateKey; -import java.security.PublicKey; import java.security.Security; import java.security.SignatureException; import java.util.Date; @@ -25,6 +23,7 @@ import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.operator.PGPDigestCalculator; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; /** @@ -40,8 +39,7 @@ public class RSAKeyPairGenerator private static void exportKeyPair( OutputStream secretOut, OutputStream publicOut, - PublicKey publicKey, - PrivateKey privateKey, + KeyPair pair, String identity, char[] passPhrase, boolean armor) @@ -53,7 +51,7 @@ public class RSAKeyPairGenerator } PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1); - PGPKeyPair keyPair = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, publicKey, privateKey, new Date()); + PGPKeyPair keyPair = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, pair, new Date()); PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, keyPair, identity, sha1Calc, null, null, new JcaPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc).setProvider("BC").build(passPhrase)); secretKey.encode(secretOut); @@ -101,14 +99,14 @@ public class RSAKeyPairGenerator FileOutputStream out1 = new FileOutputStream("secret.asc"); FileOutputStream out2 = new FileOutputStream("pub.asc"); - exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(), args[1], args[2].toCharArray(), true); + exportKeyPair(out1, out2, kp, args[1], args[2].toCharArray(), true); } else { FileOutputStream out1 = new FileOutputStream("secret.bpg"); FileOutputStream out2 = new FileOutputStream("pub.bpg"); - exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(), args[0], args[1].toCharArray(), false); + exportKeyPair(out1, out2, kp, args[0], args[1].toCharArray(), false); } } } diff --git a/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPContentVerifierBuilderProvider.java b/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPContentVerifierBuilderProvider.java index 3954e41b..1fda5a20 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPContentVerifierBuilderProvider.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPContentVerifierBuilderProvider.java @@ -11,6 +11,7 @@ import org.bouncycastle.jcajce.NamedJcaJceHelper; import org.bouncycastle.jcajce.ProviderJcaJceHelper; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPRuntimeOperationException; import org.bouncycastle.openpgp.operator.PGPContentVerifier; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilder; import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider; @@ -97,8 +98,8 @@ public class JcaPGPContentVerifierBuilderProvider return signature.verify(expected); } catch (SignatureException e) - { // TODO: need a specific runtime exception for PGP operators. - throw new IllegalStateException("unable to verify signature"); + { + throw new PGPRuntimeOperationException("unable to verify signature: " + e.getMessage(), e); } } diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSAElGamalTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSAElGamalTest.java index 545f4b30..8a1ec447 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSAElGamalTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSAElGamalTest.java @@ -54,6 +54,7 @@ import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; import org.bouncycastle.openpgp.operator.bc.BcPGPKeyConverter; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.test.SimpleTest; import org.bouncycastle.util.test.UncloseableOutputStream; @@ -443,7 +444,7 @@ public class BcPGPDSAElGamalTest KeyPair kp = kpg.generateKeyPair(); - PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp.getPublic(), kp.getPrivate(), new Date()); + PGPKeyPair pgpKp = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp, new Date()); PGPPublicKey k1 = pgpKp.getPublicKey(); @@ -469,7 +470,7 @@ public class BcPGPDSAElGamalTest // Run a short encrypt/decrypt test with random key for the given parameters kp = keyGen.generateKeyPair(); - PGPKeyPair elGamalKeyPair = new PGPKeyPair( + PGPKeyPair elGamalKeyPair = new JcaPGPKeyPair( PublicKeyAlgorithmTags.ELGAMAL_GENERAL, kp, new Date()); cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(random)); diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSATest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSATest.java index fca957ac..891718c7 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSATest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPDSATest.java @@ -39,6 +39,10 @@ import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.test.SimpleTest; import org.bouncycastle.util.test.UncloseableOutputStream; @@ -582,9 +586,9 @@ public class BcPGPDSATest char [] passPhrase = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; sKey = new PGPSecretKeyRing(testPrivKey2, new BcKeyFingerprintCalculator()); - pgpPrivKey = sKey.getSecretKey().extractPrivateKey(passPhrase, "BC"); + pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase)); - byte[] bytes = pgpPrivKey.getKey().getEncoded(); + byte[] bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // reading test - aes256 encrypted passphrase. @@ -592,7 +596,7 @@ public class BcPGPDSATest sKey = new PGPSecretKeyRing(aesSecretKey, new BcKeyFingerprintCalculator()); pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass)); - bytes = pgpPrivKey.getKey().getEncoded(); + bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // reading test - twofish encrypted passphrase. @@ -600,7 +604,7 @@ public class BcPGPDSATest sKey = new PGPSecretKeyRing(twofishSecretKey, new BcKeyFingerprintCalculator()); pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass)); - bytes = pgpPrivKey.getKey().getEncoded(); + bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // use of PGPKeyPair @@ -611,7 +615,7 @@ public class BcPGPDSATest KeyPair kp = kpg.generateKeyPair(); - PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.DSA , kp.getPublic(), kp.getPrivate(), new Date()); + PGPKeyPair pgpKp = new JcaPGPKeyPair(PGPPublicKey.DSA, kp, new Date()); PGPPublicKey k1 = pgpKp.getPublicKey(); diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPKeyRingTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPKeyRingTest.java index 7a55ec49..f58cd4c4 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPKeyRingTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPKeyRingTest.java @@ -29,6 +29,7 @@ import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.util.test.SimpleTest; @@ -1219,7 +1220,7 @@ public class BcPGPKeyRingTest byte[] bytes = pgpSec.getEncoded(); - pgpSec = new PGPSecretKeyRing(bytes); + pgpSec = new PGPSecretKeyRing(bytes, new BcKeyFingerprintCalculator()); Iterator it = pgpSec.getSecretKeys(); while (it.hasNext()) @@ -1500,7 +1501,7 @@ public class BcPGPKeyRingTest byte[] bytes = pgpSec.getEncoded(); - pgpSec = new PGPSecretKeyRing(bytes); + pgpSec = new PGPSecretKeyRing(bytes, new BcKeyFingerprintCalculator()); Iterator it = pgpSec.getSecretKeys(); while (it.hasNext()) @@ -1933,8 +1934,8 @@ public class BcPGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair elgKp = elgKpg.generateKeyPair(); - PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); - PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); + PGPKeyPair dsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); + PGPKeyPair elgKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC"); @@ -1994,9 +1995,9 @@ public class BcPGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair1 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair1 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair2 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair2 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair1, "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC"); @@ -2060,8 +2061,8 @@ public class BcPGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair elgKp = elgKpg.generateKeyPair(); - PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); - PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); + PGPKeyPair dsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); + PGPKeyPair elgKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", PGPEncryptedData.AES_256, passPhrase, true, null, null, new SecureRandom(), "BC"); @@ -2207,9 +2208,9 @@ public class BcPGPKeyRingTest // KeyPairGenerator rsaKpg = KeyPairGenerator.getInstance("RSA", "BC"); KeyPair rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair1 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair1 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair2 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair2 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); char[] passPhrase = "passwd".toCharArray(); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair1, diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPRSATest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPRSATest.java index 713c5770..1b501c17 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPRSATest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/BcPGPRSATest.java @@ -67,6 +67,7 @@ import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; import org.bouncycastle.util.Arrays; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Hex; @@ -848,7 +849,7 @@ public class BcPGPRSATest byte[] out = c.doFinal(in); - c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey()); + c.init(Cipher.DECRYPT_MODE, new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey)); out = c.doFinal(out); diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/DSA2Test.java b/pg/src/test/java/org/bouncycastle/openpgp/test/DSA2Test.java index fd7fd2df..104a0c58 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/DSA2Test.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/DSA2Test.java @@ -1,5 +1,13 @@ package org.bouncycastle.openpgp.test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.Security; +import java.util.Date; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -17,16 +25,13 @@ import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignatureGenerator; import org.bouncycastle.openpgp.PGPSignatureList; import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.util.test.UncloseableOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.security.Security; -import java.util.Date; - /** * GPG compatability test vectors */ @@ -141,9 +146,9 @@ public class DSA2Test String data = "hello world!"; ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes()); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.DSA, digest, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.DSA, digest).setProvider("BC")); - sGen.initSign(PGPSignature.BINARY_DOCUMENT, secRing.getSecretKey().extractPrivateKey("test".toCharArray(), "BC")); + sGen.init(PGPSignature.BINARY_DOCUMENT, secRing.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build("test".toCharArray()))); BCPGOutputStream bcOut = new BCPGOutputStream(bOut); @@ -185,7 +190,7 @@ public class DSA2Test InputStream dIn = p2.getInputStream(); - ops.initVerify(pubRing.getPublicKey(), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubRing.getPublicKey()); while ((ch = dIn.read()) >= 0) { @@ -220,7 +225,7 @@ public class DSA2Test InputStream dIn = p2.getInputStream(); - ops.initVerify(publicKey.getPublicKey(), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), publicKey.getPublicKey()); int ch; while ((ch = dIn.read()) >= 0) @@ -248,7 +253,7 @@ public class DSA2Test { FileInputStream fIn = new FileInputStream(getDataHome() + "/keys/" + keyName); - return new PGPPublicKeyRing(fIn); + return new PGPPublicKeyRing(fIn, new JcaKeyFingerprintCalculator()); } private PGPSecretKeyRing loadSecretKey( @@ -257,7 +262,7 @@ public class DSA2Test { FileInputStream fIn = new FileInputStream(getDataHome() + "/keys/" + keyName); - return new PGPSecretKeyRing(fIn); + return new PGPSecretKeyRing(fIn, new JcaKeyFingerprintCalculator()); } private String getDataHome() diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPClearSignedSignatureTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPClearSignedSignatureTest.java index 7c8f6294..77b3697f 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPClearSignedSignatureTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPClearSignedSignatureTest.java @@ -1,5 +1,13 @@ package org.bouncycastle.openpgp.test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.SignatureException; +import java.util.Iterator; + import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.BCPGOutputStream; @@ -15,17 +23,13 @@ import org.bouncycastle.openpgp.PGPSignatureGenerator; import org.bouncycastle.openpgp.PGPSignatureList; import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.test.SimpleTest; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.security.SignatureException; -import java.util.Iterator; - public class PGPClearSignedSignatureTest extends SimpleTest { @@ -215,7 +219,7 @@ public class PGPClearSignedSignatureTest PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject(); PGPSignature sig = p3.get(0); - sig.initVerify(pgpRings.getPublicKey(sig.getKeyID()), "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pgpRings.getPublicKey(sig.getKeyID())); ByteArrayOutputStream lineOut = new ByteArrayOutputStream(); InputStream sigIn = new ByteArrayInputStream(bOut.toByteArray()); @@ -286,11 +290,11 @@ public class PGPClearSignedSignatureTest throws Exception { PGPSecretKey pgpSecKey = readSecretKey(new ByteArrayInputStream(secretKey)); - PGPPrivateKey pgpPrivKey = pgpSecKey.extractPrivateKey("".toCharArray(), "BC"); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSecKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256, "BC"); + PGPPrivateKey pgpPrivKey = pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build("".toCharArray())); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSecKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - sGen.initSign(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey); + sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey); Iterator it = pgpSecKey.getPublicKey().getUserIDs(); if (it.hasNext()) diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSAElGamalTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSAElGamalTest.java index 04474cff..23a84e30 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSAElGamalTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSAElGamalTest.java @@ -43,6 +43,13 @@ import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignatureGenerator; import org.bouncycastle.openpgp.PGPSignatureList; import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator; @@ -156,8 +163,8 @@ public class PGPDSAElGamalTest // // Read the private key // - PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKeyRing); - PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(pass, "BC"); + PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKeyRing, new JcaKeyFingerprintCalculator()); + PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); // // signature generation @@ -165,9 +172,9 @@ public class PGPDSAElGamalTest String data = "hello world!"; ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes()); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PGPPublicKey.DSA, PGPUtil.SHA1, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PGPPublicKey.DSA, PGPUtil.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); + sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZIP); @@ -221,7 +228,7 @@ public class PGPDSAElGamalTest InputStream dIn = p2.getInputStream(); - ops.initVerify(pubKey, "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); while ((ch = dIn.read()) >= 0) { @@ -275,9 +282,9 @@ public class PGPDSAElGamalTest byte[] out = c.doFinal(in); - pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC"); + pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); - c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey()); + c.init(Cipher.DECRYPT_MODE, new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey)); out = c.doFinal(out); @@ -361,7 +368,7 @@ public class PGPDSAElGamalTest // // note: we use the DSA public key here. // - ops.initVerify(pgpPub.getPublicKey(), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pgpPub.getPublicKey()); while ((ch = inLd.read()) >= 0) { @@ -402,7 +409,7 @@ public class PGPDSAElGamalTest encP = (PGPPublicKeyEncryptedData)encList.get(0); - pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC"); + pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); clear = encP.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(pgpPrivKey)); @@ -434,7 +441,7 @@ public class PGPDSAElGamalTest KeyPair kp = kpg.generateKeyPair(); - PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp.getPublic(), kp.getPrivate(), new Date()); + PGPKeyPair pgpKp = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL, kp, new Date()); PGPPublicKey k1 = pgpKp.getPublicKey(); @@ -460,7 +467,7 @@ public class PGPDSAElGamalTest // Run a short encrypt/decrypt test with random key for the given parameters kp = keyGen.generateKeyPair(); - PGPKeyPair elGamalKeyPair = new PGPKeyPair( + PGPKeyPair elGamalKeyPair = new JcaPGPKeyPair( PublicKeyAlgorithmTags.ELGAMAL_GENERAL, kp, new Date()); cPk = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(new SecureRandom()).setProvider("BC")); diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSATest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSATest.java index 2523d0e9..b0f4c944 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSATest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPDSATest.java @@ -34,6 +34,13 @@ import org.bouncycastle.openpgp.PGPSignatureList; import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector; import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.test.SimpleTest; import org.bouncycastle.util.test.UncloseableOutputStream; @@ -308,9 +315,9 @@ public class PGPDSATest String data = "hello world!"; ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes()); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.DSA, HashAlgorithmTags.SHA1, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.DSA, HashAlgorithmTags.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); + sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); @@ -369,7 +376,7 @@ public class PGPDSATest InputStream dIn = p2.getInputStream(); - ops.initVerify(pgpPubKey, "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pgpPubKey); while ((ch = dIn.read()) >= 0) { @@ -397,15 +404,15 @@ public class PGPDSATest // // Read the public key // - PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(testPubKey); + PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(testPubKey, new JcaKeyFingerprintCalculator()); pubKey = pgpPub.getPublicKey(); // // Read the private key // - PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKey); - PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(pass, "BC"); + PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKey, new JcaKeyFingerprintCalculator()); + PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); // // test signature message @@ -425,7 +432,7 @@ public class PGPDSATest InputStream dIn = p2.getInputStream(); int ch; - ops.initVerify(pubKey, "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); while ((ch = dIn.read()) >= 0) { @@ -450,9 +457,9 @@ public class PGPDSATest String data = "hello world!"; ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes()); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PGPPublicKey.DSA, PGPUtil.SHA1, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PGPPublicKey.DSA, PGPUtil.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey); + sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey); PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZIP); @@ -504,7 +511,7 @@ public class PGPDSATest dIn = p2.getInputStream(); - ops.initVerify(pubKey, "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); while ((ch = dIn.read()) >= 0) { @@ -521,7 +528,7 @@ public class PGPDSATest // // Read the public key with user attributes // - pgpPub = new PGPPublicKeyRing(testPubWithUserAttr); + pgpPub = new PGPPublicKeyRing(testPubWithUserAttr, new JcaKeyFingerprintCalculator()); pubKey = pgpPub.getPublicKey(); @@ -576,26 +583,26 @@ public class PGPDSATest // char [] passPhrase = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; - sKey = new PGPSecretKeyRing(testPrivKey2); - pgpPrivKey = sKey.getSecretKey().extractPrivateKey(passPhrase, "BC"); + sKey = new PGPSecretKeyRing(testPrivKey2, new JcaKeyFingerprintCalculator()); + pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase)); - byte[] bytes = pgpPrivKey.getKey().getEncoded(); + byte[] bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // reading test - aes256 encrypted passphrase. // - sKey = new PGPSecretKeyRing(aesSecretKey); - pgpPrivKey = sKey.getSecretKey().extractPrivateKey(pass, "BC"); + sKey = new PGPSecretKeyRing(aesSecretKey, new JcaKeyFingerprintCalculator()); + pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); - bytes = pgpPrivKey.getKey().getEncoded(); + bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // reading test - twofish encrypted passphrase. // - sKey = new PGPSecretKeyRing(twofishSecretKey); - pgpPrivKey = sKey.getSecretKey().extractPrivateKey(pass, "BC"); + sKey = new PGPSecretKeyRing(twofishSecretKey, new JcaKeyFingerprintCalculator()); + pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); - bytes = pgpPrivKey.getKey().getEncoded(); + bytes = new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey).getEncoded(); // // use of PGPKeyPair @@ -606,7 +613,7 @@ public class PGPDSATest KeyPair kp = kpg.generateKeyPair(); - PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.DSA , kp.getPublic(), kp.getPrivate(), new Date()); + PGPKeyPair pgpKp = new JcaPGPKeyPair(PGPPublicKey.DSA , kp, new Date()); PGPPublicKey k1 = pgpKp.getPublicKey(); diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPKeyRingTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPKeyRingTest.java index 0647d540..c0bef3c8 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPKeyRingTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPKeyRingTest.java @@ -31,6 +31,7 @@ import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider; import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.bouncycastle.util.encoders.Base64; @@ -1579,7 +1580,7 @@ public class PGPKeyRingTest PGPSecretKey k = (PGPSecretKey)it.next(); - k.extractPrivateKey(sec3pass1, "BC"); + k.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(sec3pass1)); } if (keyCount != 2) @@ -1625,7 +1626,7 @@ public class PGPKeyRingTest PGPSecretKey k = (PGPSecretKey)it.next(); - k.extractPrivateKey(sec3pass1, "BC"); + k.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(sec3pass1)); } if (keyCount != 2) @@ -2081,8 +2082,8 @@ public class PGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair elgKp = elgKpg.generateKeyPair(); - PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); - PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); + PGPKeyPair dsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); + PGPKeyPair elgKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC"); @@ -2120,7 +2121,7 @@ public class PGPKeyRingTest if (sig.getKeyID() == vKey.getKeyID() && sig.getSignatureType() == PGPSignature.SUBKEY_BINDING) { - sig.initVerify(vKey, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), vKey); if (!sig.verifyCertification(vKey, sKey)) { @@ -2142,9 +2143,9 @@ public class PGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair1 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair1 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair2 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair2 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair1, "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC"); @@ -2208,8 +2209,8 @@ public class PGPKeyRingTest // this is quicker because we are using pregenerated parameters. // KeyPair elgKp = elgKpg.generateKeyPair(); - PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); - PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); + PGPKeyPair dsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); + PGPKeyPair elgKeyPair = new JcaPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", PGPEncryptedData.AES_256, passPhrase, true, null, null, new SecureRandom(), "BC"); @@ -2263,7 +2264,7 @@ public class PGPKeyRingTest if (sig.getKeyID() == vKey.getKeyID() && sig.getSignatureType() == PGPSignature.SUBKEY_BINDING) { - sig.initVerify(vKey, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), vKey); if (!sig.verifyCertification(vKey, sKey)) { @@ -2517,7 +2518,7 @@ public class PGPKeyRingTest if (sig.getSignatureType() == PGPSignature.POSITIVE_CERTIFICATION) { - sig.initVerify(pub, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pub); if (!sig.verifyCertification(userID, pub)) { @@ -2531,9 +2532,9 @@ public class PGPKeyRingTest // KeyPairGenerator rsaKpg = KeyPairGenerator.getInstance("RSA", "BC"); KeyPair rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair1 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair1 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); rsaKp = rsaKpg.generateKeyPair(); - PGPKeyPair rsaKeyPair2 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); + PGPKeyPair rsaKeyPair2 = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date()); char[] passPhrase = "passwd".toCharArray(); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair1, @@ -2549,7 +2550,7 @@ public class PGPKeyRingTest if (sig.getSignatureType() == PGPSignature.POSITIVE_CERTIFICATION) { - sig.initVerify(pub, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pub); if (!sig.verifyCertification(userID, pub)) { diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPRSATest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPRSATest.java index 485a344a..cfb6c2f6 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPRSATest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPRSATest.java @@ -56,10 +56,13 @@ import org.bouncycastle.openpgp.operator.PGPDigestCalculator; import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory; import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator; +import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder; @@ -530,7 +533,7 @@ public class PGPRSATest { PGPSignature sig = (PGPSignature)sigs.next(); - sig.initVerify(pubKey, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); if (!sig.verifyCertification(attributes, pubKey)) { @@ -567,9 +570,9 @@ public class PGPRSATest PGPUserAttributeSubpacketVector uVec = vGen.generate(); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.POSITIVE_CERTIFICATION, pgpSec.getSecretKey().extractPrivateKey(pass, "BC")); + sGen.init(PGPSignature.POSITIVE_CERTIFICATION, pgpSec.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass))); PGPSignature sig = sGen.generateCertification(uVec, pubKey); @@ -587,7 +590,7 @@ public class PGPRSATest { PGPSignature s = (PGPSignature)sigs.next(); - s.initVerify(pubKey, "BC"); + s.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); if (!s.verifyCertification(attributes, pubKey)) { @@ -852,7 +855,7 @@ public class PGPRSATest PGPSignature sig = (PGPSignature)it.next(); - sig.initVerify(pgpPub.getPublicKey(), "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pgpPub.getPublicKey()); if (!sig.verifyCertification(uid, pgpPub.getPublicKey())) { @@ -894,7 +897,7 @@ public class PGPRSATest if (!noIDEA()) { PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(testPrivKeyV3, new JcaKeyFingerprintCalculator()); - PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(passP, "BC"); + PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passP)); // // write a v3 private key @@ -914,7 +917,7 @@ public class PGPRSATest // Read the private key // PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(testPrivKey, new JcaKeyFingerprintCalculator()); - PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(pass, "BC"); + PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); // // write a private key @@ -941,7 +944,7 @@ public class PGPRSATest byte[] out = c.doFinal(in); - c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey()); + c.init(Cipher.DECRYPT_MODE, new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pgpPrivKey)); out = c.doFinal(out); @@ -968,7 +971,7 @@ public class PGPRSATest InputStream dIn = p2.getInputStream(); int ch; - ops.initVerify(pgpPub.getPublicKey(ops.getKeyID()), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pgpPub.getPublicKey(ops.getKeyID())); while ((ch = dIn.read()) >= 0) { @@ -998,7 +1001,7 @@ public class PGPRSATest PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData)encList.get(0); - pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(pass, "BC"); + pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); InputStream clear = encP.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(pgpPrivKey)); @@ -1052,7 +1055,7 @@ public class PGPRSATest encP = (PGPPublicKeyEncryptedData)encList.get(0); - pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(pass, "BC"); + pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); PublicKeyDataDecryptorFactory dataDecryptorFactory = new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(pgpPrivKey); if (encP.getSymmetricAlgorithm(dataDecryptorFactory) != SymmetricKeyAlgorithmTags.CAST5) @@ -1097,7 +1100,7 @@ public class PGPRSATest encP = (PGPPublicKeyEncryptedData)encList.get(0); - pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(pass, "BC"); + pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass)); clear = encP.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(pgpPrivKey)); @@ -1137,7 +1140,11 @@ public class PGPRSATest KeyPair kp = kpg.generateKeyPair(); - PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp.getPrivate(), new Date(), "fred", SymmetricKeyAlgorithmTags.CAST5, passPhrase, null, null, new SecureRandom(), "BC"); + PGPSecretKey secretKey = new PGPSecretKey( + PGPSignature.DEFAULT_CERTIFICATION, + new JcaPGPKeyPair(PublicKeyAlgorithmTags.RSA_GENERAL, kp, new Date()), "fred", + null, null, new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_SIGN, HashAlgorithmTags.SHA1).setProvider("BC"), + new JcePBESecretKeyEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).build(passPhrase)); PGPPublicKey key = secretKey.getPublicKey(); @@ -1149,14 +1156,14 @@ public class PGPRSATest sig = (PGPSignature)it.next(); - sig.initVerify(key, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); if (!sig.verifyCertification(uid, key)) { fail("failed to verify certification"); } - pgpPrivKey = secretKey.extractPrivateKey(passPhrase, "BC"); + pgpPrivKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase)); key = PGPPublicKey.removeCertification(key, uid, sig); @@ -1171,9 +1178,9 @@ public class PGPRSATest keyEnc = key.getEncoded(); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC"); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.KEY_REVOCATION, secretKey.extractPrivateKey(passPhrase, "BC")); + sGen.init(PGPSignature.KEY_REVOCATION, secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase))); sig = sGen.generateCertification(key); @@ -1189,7 +1196,7 @@ public class PGPRSATest sig = (PGPSignature)sgIt.next(); - sig.initVerify(key, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); if (!sig.verifyCertification(key)) { @@ -1214,9 +1221,13 @@ public class PGPRSATest // kp = kpg.generateKeyPair(); - secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp.getPrivate(), new Date(), "fred", SymmetricKeyAlgorithmTags.AES_256, passPhrase, null, null, new SecureRandom(), "BC"); - - secretKey.extractPrivateKey(passPhrase, "BC"); + secretKey = new PGPSecretKey( + PGPSignature.DEFAULT_CERTIFICATION, + new JcaPGPKeyPair(PublicKeyAlgorithmTags.RSA_GENERAL, kp, new Date()), "fred", + null, null, new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_SIGN, HashAlgorithmTags.SHA1).setProvider("BC"), + new JcePBESecretKeyEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256).build(passPhrase)); + + secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase)); secretKey.encode(new ByteArrayOutputStream()); @@ -1225,9 +1236,9 @@ public class PGPRSATest // String newPass = "newPass"; - secretKey = PGPSecretKey.copyWithNewPassword(secretKey, passPhrase, newPass.toCharArray(), secretKey.getKeyEncryptionAlgorithm(), new SecureRandom(), "BC"); + secretKey = PGPSecretKey.copyWithNewPassword(secretKey, new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase), new JcePBESecretKeyEncryptorBuilder(secretKey.getKeyEncryptionAlgorithm()).setProvider("BC").setSecureRandom(new SecureRandom()).build(newPass.toCharArray())); - secretKey.extractPrivateKey(newPass.toCharArray(), "BC"); + secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(newPass.toCharArray())); secretKey.encode(new ByteArrayOutputStream()); @@ -1243,14 +1254,14 @@ public class PGPRSATest sig = (PGPSignature)it.next(); - sig.initVerify(key, "BC"); + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); if (!sig.verifyCertification(uid, key)) { fail("failed to verify certification"); } - pgpPrivKey = secretKey.extractPrivateKey(newPass.toCharArray(), "BC"); + pgpPrivKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(newPass.toCharArray())); // // signature generation @@ -1261,9 +1272,9 @@ public class PGPRSATest ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes()); - sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC"); + sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); + sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZIP); @@ -1316,7 +1327,7 @@ public class PGPRSATest dIn = p2.getInputStream(); - ops.initVerify(secretKey.getPublicKey(), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), secretKey.getPublicKey()); while ((ch = dIn.read()) >= 0) { @@ -1336,16 +1347,16 @@ public class PGPRSATest bOut = new ByteArrayOutputStream(); testIn = new ByteArrayInputStream(data.getBytes()); - PGPV3SignatureGenerator sGenV3 = new PGPV3SignatureGenerator(PGPPublicKey.RSA_GENERAL, PGPUtil.SHA1, "BC"); + PGPV3SignatureGenerator sGenV3 = new PGPV3SignatureGenerator(new JcaPGPContentSignerBuilder(PGPPublicKey.RSA_GENERAL, PGPUtil.SHA1).setProvider("BC")); - sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); + sGenV3.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZIP); bcOut = new BCPGOutputStream(cGen.open(bOut)); - sGen.generateOnePassVersion(false).encode(bcOut); + sGenV3.generateOnePassVersion(false).encode(bcOut); lGen = new PGPLiteralDataGenerator(); lOut = lGen.open( @@ -1358,12 +1369,12 @@ public class PGPRSATest while ((ch = testIn.read()) >= 0) { lOut.write(ch); - sGen.update((byte)ch); + sGenV3.update((byte)ch); } lOut.close(); - sGen.generate().encode(bcOut); + sGenV3.generate().encode(bcOut); bcOut.close(); @@ -1388,7 +1399,7 @@ public class PGPRSATest dIn = p2.getInputStream(); - ops.initVerify(secretKey.getPublicKey(), "BC"); + ops.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), secretKey.getPublicKey()); while ((ch = dIn.read()) >= 0) { @@ -1405,11 +1416,11 @@ public class PGPRSATest // // extract PGP 8 private key // - pgpPriv = new PGPSecretKeyRing(pgp8Key); + pgpPriv = new PGPSecretKeyRing(pgp8Key, new JcaKeyFingerprintCalculator()); secretKey = pgpPriv.getSecretKey(); - pgpPrivKey = secretKey.extractPrivateKey(pgp8Pass, "BC"); + pgpPrivKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pgp8Pass)); // // expiry @@ -1429,7 +1440,7 @@ public class PGPRSATest int subKeyDays) throws Exception { - PGPPublicKeyRing pubRing = new PGPPublicKeyRing(encodedRing); + PGPPublicKeyRing pubRing = new PGPPublicKeyRing(encodedRing, new JcaKeyFingerprintCalculator()); PGPPublicKey k = pubRing.getPublicKey(); if (k.getValidDays() != masterDays) |