diff options
author | bouncy <bouncy> | 2012-03-21 03:04:01 +0000 |
---|---|---|
committer | bouncy <bouncy> | 2012-03-21 03:04:01 +0000 |
commit | 5a73e4f9e7be9db84d59727be57b3c5198e7244f (patch) | |
tree | ff2b2efa9839649540cd511fcfab792ba65a3525 | |
parent | 536771fddae61a3952f1deb19a3c809712748d29 (diff) | |
download | android_external_spongycastle-5a73e4f9e7be9db84d59727be57b3c5198e7244f.tar.gz android_external_spongycastle-5a73e4f9e7be9db84d59727be57b3c5198e7244f.tar.bz2 android_external_spongycastle-5a73e4f9e7be9db84d59727be57b3c5198e7244f.zip |
J2ME updates
4 files changed, 124 insertions, 10 deletions
diff --git a/crypto/src/org/bouncycastle/openpgp/PGPKeyPair.java b/crypto/src/org/bouncycastle/openpgp/PGPKeyPair.java index 01583d066..8b2bd5b7f 100644 --- a/crypto/src/org/bouncycastle/openpgp/PGPKeyPair.java +++ b/crypto/src/org/bouncycastle/openpgp/PGPKeyPair.java @@ -26,8 +26,8 @@ import org.bouncycastle.jce.interfaces.ElGamalPrivateKey; */ public class PGPKeyPair { - PGPPublicKey pub; - PGPPrivateKey priv; + protected PGPPublicKey pub; + protected PGPPrivateKey priv; /** * @deprecated use version without provider. @@ -115,7 +115,11 @@ public class PGPKeyPair this.pub = pub; this.priv = priv; } - + + protected PGPKeyPair() + { + } + /** * Return the keyID associated with this key pair. * diff --git a/crypto/src/org/bouncycastle/openpgp/PGPPrivateKey.java b/crypto/src/org/bouncycastle/openpgp/PGPPrivateKey.java index 527e65115..afea38794 100644 --- a/crypto/src/org/bouncycastle/openpgp/PGPPrivateKey.java +++ b/crypto/src/org/bouncycastle/openpgp/PGPPrivateKey.java @@ -62,10 +62,10 @@ public class PGPPrivateKey } - PGPPrivateKey( - long keyID, - PublicKeyPacket publicKeyPacket, - BCPGKey privateKeyDataPacket) + public PGPPrivateKey( + long keyID, + PublicKeyPacket publicKeyPacket, + BCPGKey privateKeyDataPacket) { this.keyID = keyID; this.publicKeyPacket = publicKeyPacket; diff --git a/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyConverter.java b/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyConverter.java index d42b17998..2531477f2 100644 --- a/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyConverter.java +++ b/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyConverter.java @@ -1,5 +1,7 @@ package org.bouncycastle.openpgp.operator.bc; +import java.util.Date; + import org.bouncycastle.bcpg.BCPGKey; import org.bouncycastle.bcpg.DSAPublicBCPGKey; import org.bouncycastle.bcpg.DSASecretBCPGKey; @@ -24,6 +26,81 @@ import org.bouncycastle.openpgp.PGPPublicKey; public class BcPGPKeyConverter { + /** + * Create a PGPPublicKey from the passed in JCA one. + * <p/> + * Note: the time passed in affects the value of the key's keyID, so you probably only want + * to do this once for a JCA key, or make sure you keep track of the time you used. + * + * @param algorithm asymmetric algorithm type representing the public key. + * @param pubKey actual public key to associate. + * @param time date of creation. + * @throws PGPException on key creation problem. + */ + public PGPPublicKey getPGPPublicKey(int algorithm, AsymmetricKeyParameter pubKey, Date time) + throws PGPException + { + BCPGKey bcpgKey; + + if (pubKey instanceof RSAKeyParameters) + { + RSAKeyParameters rK = (RSAKeyParameters)pubKey; + + bcpgKey = new RSAPublicBCPGKey(rK.getModulus(), rK.getExponent()); + } + else if (pubKey instanceof DSAPublicKeyParameters) + { + DSAPublicKeyParameters dK = (DSAPublicKeyParameters)pubKey; + DSAParameters dP = dK.getParameters(); + + bcpgKey = new DSAPublicBCPGKey(dP.getP(), dP.getQ(), dP.getG(), dK.getY()); + } + else if (pubKey instanceof ElGamalPublicKeyParameters) + { + ElGamalPublicKeyParameters eK = (ElGamalPublicKeyParameters)pubKey; + ElGamalParameters eS = eK.getParameters(); + + bcpgKey = new ElGamalPublicBCPGKey(eS.getP(), eS.getG(), eK.getY()); + } + else + { + throw new PGPException("unknown key class"); + } + + return new PGPPublicKey(new PublicKeyPacket(algorithm, time, bcpgKey), new BcKeyFingerprintCalculator()); + } + + public PGPPrivateKey getPGPPrivateKey(PGPPublicKey pubKey, AsymmetricKeyParameter privKey) + throws PGPException + { + BCPGKey privPk; + + switch (pubKey.getAlgorithm()) + { + case PGPPublicKey.RSA_ENCRYPT: + case PGPPublicKey.RSA_SIGN: + case PGPPublicKey.RSA_GENERAL: + RSAPrivateCrtKeyParameters rsK = (RSAPrivateCrtKeyParameters)privKey; + + privPk = new RSASecretBCPGKey(rsK.getExponent(), rsK.getP(), rsK.getQ()); + break; + case PGPPublicKey.DSA: + DSAPrivateKeyParameters dsK = (DSAPrivateKeyParameters)privKey; + + privPk = new DSASecretBCPGKey(dsK.getX()); + break; + case PGPPublicKey.ELGAMAL_ENCRYPT: + case PGPPublicKey.ELGAMAL_GENERAL: + ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters)privKey; + + privPk = new ElGamalSecretBCPGKey(esK.getX()); + break; + default: + throw new PGPException("unknown key class"); + } + return new PGPPrivateKey(pubKey.getKeyID(), pubKey.getPublicKeyPacket(), privPk); + } + public AsymmetricKeyParameter getPublicKey(PGPPublicKey publicKey) throws PGPException { @@ -80,13 +157,13 @@ public class BcPGPKeyConverter return new RSAPrivateCrtKeyParameters(rsaPriv.getModulus(), rsaPub.getPublicExponent(), rsaPriv.getPrivateExponent(), rsaPriv.getPrimeP(), rsaPriv.getPrimeQ(), rsaPriv.getPrimeExponentP(), rsaPriv.getPrimeExponentQ(), rsaPriv.getCrtCoefficient()); case PGPPublicKey.DSA: - DSAPublicBCPGKey dsaPub = (DSAPublicBCPGKey)pubPk.getKey(); + DSAPublicBCPGKey dsaPub = (DSAPublicBCPGKey)pubPk.getKey(); DSASecretBCPGKey dsaPriv = (DSASecretBCPGKey)privPk; - return new DSAPrivateKeyParameters(dsaPriv.getX(), new DSAParameters(dsaPub.getP(), dsaPub.getQ(), dsaPub.getG())); + return new DSAPrivateKeyParameters(dsaPriv.getX(), new DSAParameters(dsaPub.getP(), dsaPub.getQ(), dsaPub.getG())); case PGPPublicKey.ELGAMAL_ENCRYPT: case PGPPublicKey.ELGAMAL_GENERAL: - ElGamalPublicBCPGKey elPub = (ElGamalPublicBCPGKey)pubPk.getKey(); + ElGamalPublicBCPGKey elPub = (ElGamalPublicBCPGKey)pubPk.getKey(); ElGamalSecretBCPGKey elPriv = (ElGamalSecretBCPGKey)privPk; return new ElGamalPrivateKeyParameters(elPriv.getX(), new ElGamalParameters(elPub.getP(), elPub.getG())); diff --git a/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyPair.java b/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyPair.java new file mode 100644 index 000000000..ebf0fa0b6 --- /dev/null +++ b/crypto/src/org/bouncycastle/openpgp/operator/bc/BcPGPKeyPair.java @@ -0,0 +1,33 @@ +package org.bouncycastle.openpgp.operator.bc; + +import java.util.Date; + +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPKeyPair; +import org.bouncycastle.openpgp.PGPPrivateKey; +import org.bouncycastle.openpgp.PGPPublicKey; + +public class BcPGPKeyPair + extends PGPKeyPair +{ + private static PGPPublicKey getPublicKey(int algorithm, AsymmetricKeyParameter pubKey, Date date) + throws PGPException + { + return new BcPGPKeyConverter().getPGPPublicKey(algorithm, pubKey, date); + } + + private static PGPPrivateKey getPrivateKey(PGPPublicKey pub, AsymmetricKeyParameter privKey) + throws PGPException + { + return new BcPGPKeyConverter().getPGPPrivateKey(pub, privKey); + } + + public BcPGPKeyPair(int algorithm, AsymmetricCipherKeyPair keyPair, Date date) + throws PGPException + { + this.pub = getPublicKey(algorithm, (AsymmetricKeyParameter)keyPair.getPublic(), date); + this.priv = getPrivateKey(this.pub, (AsymmetricKeyParameter)keyPair.getPrivate()); + } +} |