Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'prov/src/main/jdk1.4/org/bouncycastle/jcajce')
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPrivateKey.java385
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPublicKey.java376
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java317
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyFactorySpi.java200
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyPairGeneratorSpi.java259
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java355
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PrivateKey.java359
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PublicKey.java454
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyFactorySpi.java128
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyPairGeneratorSpi.java144
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/SignatureSpi.java219
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/elgamal/CipherSpi.java299
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/AlgorithmParametersSpi.java217
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/CipherSpi.java509
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/PSSSignatureSpi.java405
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/DSABase.java128
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/ECUtil.java220
-rw-r--r--prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/keystore/pkcs12/PKCS12KeyStoreSpi.java1637
18 files changed, 0 insertions, 6611 deletions
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPrivateKey.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPrivateKey.java
deleted file mode 100644
index 91b79dc4..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPrivateKey.java
+++ /dev/null
@@ -1,385 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.math.BigInteger;
-import java.util.Enumeration;
-
-import org.bouncycastle.asn1.ASN1Encodable;
-import org.bouncycastle.asn1.ASN1Integer;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.ASN1Sequence;
-import org.bouncycastle.asn1.DERBitString;
-import org.bouncycastle.asn1.DERNull;
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
-import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
-import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
-import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x9.X962Parameters;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
-import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
-import org.bouncycastle.jce.interfaces.ECPointEncoder;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPrivateKeySpec;
-import org.bouncycastle.math.ec.ECCurve;
-import org.bouncycastle.math.ec.ECPoint;
-
-public class BCECPrivateKey
- implements ECPrivateKey, PKCS12BagAttributeCarrier, ECPointEncoder
-{
- private String algorithm = "EC";
- private boolean withCompression;
-
- private transient BigInteger d;
- private transient ECParameterSpec ecSpec;
- private transient ProviderConfiguration configuration;
- private transient DERBitString publicKey;
-
- private transient PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
-
- protected BCECPrivateKey()
- {
- }
-
- BCECPrivateKey(
- ECPrivateKey key,
- ProviderConfiguration configuration)
- {
- this.d = key.getD();
- this.algorithm = key.getAlgorithm();
- this.ecSpec = key.getParameters();
- this.configuration = configuration;
- }
-
- public BCECPrivateKey(
- String algorithm,
- ECPrivateKeySpec spec,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.d = spec.getD();
- this.ecSpec = spec.getParams();
- this.configuration = configuration;
- }
-
- public BCECPrivateKey(
- String algorithm,
- ECPrivateKeyParameters params,
- BCECPublicKey pubKey,
- ECParameterSpec spec,
- ProviderConfiguration configuration)
- {
- ECDomainParameters dp = params.getParameters();
-
- this.algorithm = algorithm;
- this.d = params.getD();
- this.configuration = configuration;
-
- if (spec == null)
- {
- this.ecSpec = new ECParameterSpec(
- dp.getCurve(),
- dp.getG(),
- dp.getN(),
- dp.getH(),
- dp.getSeed());
- }
- else
- {
- this.ecSpec = spec;
- }
-
- publicKey = getPublicKeyDetails(pubKey);
- }
-
- public BCECPrivateKey(
- String algorithm,
- ECPrivateKeyParameters params,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.d = params.getD();
- this.ecSpec = null;
- this.configuration = configuration;
- }
-
- public BCECPrivateKey(
- String algorithm,
- BCECPrivateKey key)
- {
- this.algorithm = algorithm;
- this.d = key.d;
- this.ecSpec = key.ecSpec;
- this.withCompression = key.withCompression;
- this.publicKey = key.publicKey;
- this.attrCarrier = key.attrCarrier;
- this.configuration = key.configuration;
- }
-
- BCECPrivateKey(
- PrivateKeyInfo info,
- ProviderConfiguration configuration)
- {
- this.configuration = configuration;
-
- populateFromPrivKeyInfo(info);
- }
-
- BCECPrivateKey(
- String algorithm,
- PrivateKeyInfo info,
- ProviderConfiguration configuration)
- {
- this.configuration = configuration;
- populateFromPrivKeyInfo(info);
- this.algorithm = algorithm;
- }
-
- private void populateFromPrivKeyInfo(PrivateKeyInfo info)
- {
- X962Parameters params = X962Parameters.getInstance(info.getAlgorithmId().getParameters());
-
- if (params.isNamedCurve())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)params.getParameters();
- X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
-
- ecSpec = new ECNamedCurveParameterSpec(
- ECUtil.getCurveName(oid),
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- }
- else if (params.isImplicitlyCA())
- {
- ecSpec = null;
- }
- else
- {
- X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
- ecSpec = new ECParameterSpec(ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- }
-
- if (info.getPrivateKey() instanceof ASN1Integer)
- {
- ASN1Integer derD = ASN1Integer.getInstance(info.getPrivateKey());
-
- this.d = derD.getValue();
- }
- else
- {
- ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence)info.getPrivateKey());
-
- this.d = ec.getKey();
- this.publicKey = ec.getPublicKey();
- }
- }
-
- public String getAlgorithm()
- {
- return algorithm;
- }
-
- /**
- * return the encoding format we produce in getEncoded().
- *
- * @return the string "PKCS#8"
- */
- public String getFormat()
- {
- return "PKCS#8";
- }
-
- /**
- * Return a PKCS8 representation of the key. The sequence returned
- * represents a full PrivateKeyInfo object.
- *
- * @return a PKCS8 representation of the key.
- */
- public byte[] getEncoded()
- {
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- DEROutputStream dOut = new DEROutputStream(bOut);
- X962Parameters params = null;
-
- if (ecSpec instanceof ECNamedCurveParameterSpec)
- {
- ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveParameterSpec)ecSpec).getName());
-
- params = new X962Parameters(curveOid);
- }
- else if (ecSpec == null)
- {
- params = new X962Parameters(DERNull.INSTANCE);
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecSpec;
-
- ECPoint pG = p.getG().normalize();
- ECPoint g = pG.getCurve().createPoint(pG.getAffineXCoord().toBigInteger(), pG.getAffineYCoord().toBigInteger());
-
- X9ECParameters ecP = new X9ECParameters(
- p.getCurve(),
- g,
- p.getN(),
- p.getH(),
- p.getSeed());
-
- params = new X962Parameters(ecP);
- }
-
- PrivateKeyInfo info;
- ECPrivateKeyStructure keyStructure;
-
- if (publicKey != null)
- {
- keyStructure = new ECPrivateKeyStructure(this.getD(), publicKey, params);
- }
- else
- {
- keyStructure = new ECPrivateKeyStructure(this.getD(), params);
- }
-
- try
- {
- if (algorithm.equals("ECGOST3410"))
- {
- info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), keyStructure);
- }
- else
- {
- info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), keyStructure);
- }
-
- return KeyUtil.getEncodedPrivateKeyInfo(info);
- }
- catch (IOException e)
- {
- return null;
- }
- }
-
- public ECParameterSpec getParams()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public ECParameterSpec getParameters()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public BigInteger getD()
- {
- return d;
- }
-
- public void setBagAttribute(
- ASN1ObjectIdentifier oid,
- ASN1Encodable attribute)
- {
- attrCarrier.setBagAttribute(oid, attribute);
- }
-
- public ASN1Encodable getBagAttribute(
- ASN1ObjectIdentifier oid)
- {
- return attrCarrier.getBagAttribute(oid);
- }
-
- public Enumeration getBagAttributeKeys()
- {
- return attrCarrier.getBagAttributeKeys();
- }
-
- public void setPointFormat(String style)
- {
- withCompression = !("UNCOMPRESSED".equalsIgnoreCase(style));
- }
-
- ECParameterSpec engineGetSpec()
- {
- if (ecSpec != null)
- {
- return ecSpec;
- }
-
- return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- }
-
- public boolean equals(Object o)
- {
- if (!(o instanceof BCECPrivateKey))
- {
- return false;
- }
-
- BCECPrivateKey other = (BCECPrivateKey)o;
-
- return getD().equals(other.getD()) && (engineGetSpec().equals(other.engineGetSpec()));
- }
-
- public int hashCode()
- {
- return getD().hashCode() ^ engineGetSpec().hashCode();
- }
-
- private DERBitString getPublicKeyDetails(BCECPublicKey pub)
- {
- try
- {
- SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(pub.getEncoded()));
-
- return info.getPublicKeyData();
- }
- catch (IOException e)
- { // should never happen
- return null;
- }
- }
-
- private void readObject(
- ObjectInputStream in)
- throws IOException, ClassNotFoundException
- {
- in.defaultReadObject();
-
- byte[] enc = (byte[])in.readObject();
-
- populateFromPrivKeyInfo(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));
-
- this.configuration = BouncyCastleProvider.CONFIGURATION;
- this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
- }
-
- private void writeObject(
- ObjectOutputStream out)
- throws IOException
- {
- out.defaultWriteObject();
-
- out.writeObject(this.getEncoded());
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPublicKey.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPublicKey.java
deleted file mode 100644
index b03b23dc..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPublicKey.java
+++ /dev/null
@@ -1,376 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.ASN1OctetString;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.DERBitString;
-import org.bouncycastle.asn1.DERNull;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.DEROctetString;
-import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x9.X962Parameters;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.asn1.x9.X9ECPoint;
-import org.bouncycastle.asn1.x9.X9IntegerConverter;
-import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
-import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
-import org.bouncycastle.jce.interfaces.ECPointEncoder;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPublicKeySpec;
-import org.bouncycastle.math.ec.ECCurve;
-import org.bouncycastle.math.ec.ECPoint;
-
-public class BCECPublicKey
- implements ECPublicKey, ECPointEncoder
-{
- private String algorithm = "EC";
- private boolean withCompression;
-
- private transient org.bouncycastle.math.ec.ECPoint q;
- private transient ECParameterSpec ecSpec;
- private transient ProviderConfiguration configuration;
-
- public BCECPublicKey(
- String algorithm,
- BCECPublicKey key
- )
- {
- this.algorithm = algorithm;
- this.q = key.q;
- this.ecSpec = key.ecSpec;
- this.withCompression = key.withCompression;
- this.configuration = key.configuration;
- }
-
- public BCECPublicKey(
- String algorithm,
- ECPublicKeySpec spec,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.q = spec.getQ();
- this.configuration = configuration;
-
- if (spec.getParams() != null)
- {
- this.ecSpec = spec.getParams();
- }
- else
- {
- if (q.getCurve() == null)
- {
- org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
- }
- this.ecSpec = null;
- }
- }
-
- public BCECPublicKey(
- String algorithm,
- ECPublicKeyParameters params,
- ECParameterSpec spec,
- ProviderConfiguration configuration)
- {
- ECDomainParameters dp = params.getParameters();
-
- this.algorithm = algorithm;
- this.q = params.getQ();
- this.configuration = configuration;
-
- if (spec == null)
- {
- this.ecSpec = new ECParameterSpec(
- dp.getCurve(),
- dp.getG(),
- dp.getN(),
- dp.getH(),
- dp.getSeed());
- }
- else
- {
- this.ecSpec = spec;
- }
- }
-
- public BCECPublicKey(
- String algorithm,
- ECPublicKeyParameters params,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.q = params.getQ();
- this.ecSpec = null;
- this.configuration = configuration;
- }
-
- BCECPublicKey(
- ECPublicKey key,
- ProviderConfiguration configuration)
- {
- this.q = key.getQ();
- this.algorithm = key.getAlgorithm();
- this.ecSpec = key.getParameters();
- this.configuration = configuration;
- }
-
- BCECPublicKey(
- String algorithm,
- ECPoint q,
- ECParameterSpec ecSpec,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.q = q;
- this.ecSpec = ecSpec;
- this.configuration = configuration;
- }
-
- BCECPublicKey(
- SubjectPublicKeyInfo info,
- ProviderConfiguration configuration)
- {
- this.configuration = configuration;
-
- populateFromPubKeyInfo(info);
- }
-
- BCECPublicKey(
- String algorithm,
- SubjectPublicKeyInfo info,
- ProviderConfiguration configuration)
- {
- this.configuration = configuration;
- populateFromPubKeyInfo(info);
- this.algorithm = algorithm;
- }
-
- private void populateFromPubKeyInfo(SubjectPublicKeyInfo info)
- {
- X962Parameters params = X962Parameters.getInstance(info.getAlgorithmId().getParameters());
- ECCurve curve;
-
- if (params.isNamedCurve())
- {
- ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
- X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
-
- ecSpec = new ECNamedCurveParameterSpec(
- ECUtil.getCurveName(oid),
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- curve = ((ECParameterSpec)ecSpec).getCurve();
- }
- else if (params.isImplicitlyCA())
- {
- ecSpec = null;
- curve = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa().getCurve();
- }
- else
- {
- X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
- ecSpec = new ECParameterSpec(
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- curve = ((ECParameterSpec)ecSpec).getCurve();
- }
-
- DERBitString bits = info.getPublicKeyData();
- byte[] data = bits.getBytes();
- ASN1OctetString key = new DEROctetString(data);
-
- //
- // extra octet string - one of our old certs...
- //
- if (data[0] == 0x04 && data[1] == data.length - 2
- && (data[2] == 0x02 || data[2] == 0x03))
- {
- int qLength = new X9IntegerConverter().getByteLength(curve);
-
- if (qLength >= data.length - 3)
- {
- try
- {
- key = (ASN1OctetString)ASN1Primitive.fromByteArray(data);
- }
- catch (IOException ex)
- {
- throw new IllegalArgumentException("error recovering public key");
- }
- }
- }
-
- X9ECPoint derQ = new X9ECPoint(curve, key);
-
- this.q = derQ.getPoint();
- }
-
- public String getAlgorithm()
- {
- return algorithm;
- }
-
- public String getFormat()
- {
- return "X.509";
- }
-
- public byte[] getEncoded()
- {
- SubjectPublicKeyInfo info;
-
- X962Parameters params = null;
- if (ecSpec instanceof ECNamedCurveParameterSpec)
- {
- ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveParameterSpec)ecSpec).getName());
-
- if (curveOid == null)
- {
- curveOid = new ASN1ObjectIdentifier(((ECNamedCurveParameterSpec)ecSpec).getName());
- }
- params = new X962Parameters(curveOid);
- }
- else if (ecSpec == null)
- {
- params = new X962Parameters(DERNull.INSTANCE);
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecSpec;
-
- ECCurve curve = p.getG().getCurve();
- ECPoint generator = curve.createPoint(p.getG().getX().toBigInteger(), p.getG().getY().toBigInteger(), withCompression);
-
- X9ECParameters ecP = new X9ECParameters(
- p.getCurve(), generator, p.getN(), p.getH(), p.getSeed());
-
- params = new X962Parameters(ecP);
- }
-
- ECCurve curve = this.engineGetQ().getCurve();
- ECPoint point = curve.createPoint(this.getQ().getX().toBigInteger(), this.getQ().getY().toBigInteger(), withCompression);
- ASN1OctetString p = ASN1OctetString.getInstance(new X9ECPoint(point));
-
- info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
-
- return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
- }
-
- public ECParameterSpec getParams()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public ECParameterSpec getParameters()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public org.bouncycastle.math.ec.ECPoint getQ()
- {
- if (ecSpec == null)
- {
- if (q instanceof org.bouncycastle.math.ec.ECPoint.Fp)
- {
- return new org.bouncycastle.math.ec.ECPoint.Fp(null, q.getX(), q.getY());
- }
- else
- {
- return new org.bouncycastle.math.ec.ECPoint.F2m(null, q.getX(), q.getY());
- }
- }
-
- return q;
- }
-
- public org.bouncycastle.math.ec.ECPoint engineGetQ()
- {
- return q;
- }
-
- public String toString()
- {
- StringBuffer buf = new StringBuffer();
- String nl = System.getProperty("line.separator");
-
- buf.append("EC Public Key").append(nl);
- buf.append(" X: ").append(this.getQ().getX().toBigInteger().toString(16)).append(nl);
- buf.append(" Y: ").append(this.getQ().getY().toBigInteger().toString(16)).append(nl);
-
- return buf.toString();
-
- }
-
- public void setPointFormat(String style)
- {
- withCompression = !("UNCOMPRESSED".equalsIgnoreCase(style));
- }
-
- ECParameterSpec engineGetSpec()
- {
- if (ecSpec != null)
- {
- return (ECParameterSpec)ecSpec;
- }
-
- return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- }
-
- public boolean equals(Object o)
- {
- if (!(o instanceof BCECPublicKey))
- {
- return false;
- }
-
- BCECPublicKey other = (BCECPublicKey)o;
-
- return getQ().equals(other.getQ()) && (engineGetSpec().equals(other.engineGetSpec()));
- }
-
- public int hashCode()
- {
- return getQ().hashCode() ^ engineGetSpec().hashCode();
- }
-
- private void readObject(
- ObjectInputStream in)
- throws IOException, ClassNotFoundException
- {
- in.defaultReadObject();
-
- byte[] enc = (byte[])in.readObject();
-
- populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));
-
- this.configuration = BouncyCastleProvider.CONFIGURATION;
- }
-
- private void writeObject(
- ObjectOutputStream out)
- throws IOException
- {
- out.defaultWriteObject();
-
- out.writeObject(this.getEncoded());
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java
deleted file mode 100644
index 200ff350..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyAgreementSpi.java
+++ /dev/null
@@ -1,317 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.math.BigInteger;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.Key;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.SecureRandom;
-import java.security.spec.AlgorithmParameterSpec;
-import java.util.Hashtable;
-
-import javax.crypto.SecretKey;
-import javax.crypto.ShortBufferException;
-import javax.crypto.spec.SecretKeySpec;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
-import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
-import org.bouncycastle.asn1.x9.X9IntegerConverter;
-import org.bouncycastle.crypto.BasicAgreement;
-import org.bouncycastle.crypto.CipherParameters;
-import org.bouncycastle.crypto.DerivationFunction;
-import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
-import org.bouncycastle.crypto.agreement.ECDHCBasicAgreement;
-import org.bouncycastle.crypto.agreement.ECMQVBasicAgreement;
-import org.bouncycastle.crypto.agreement.kdf.DHKDFParameters;
-import org.bouncycastle.crypto.agreement.kdf.ECDHKEKGenerator;
-import org.bouncycastle.crypto.digests.SHA1Digest;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.crypto.params.MQVPrivateParameters;
-import org.bouncycastle.crypto.params.MQVPublicParameters;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.interfaces.MQVPrivateKey;
-import org.bouncycastle.jce.interfaces.MQVPublicKey;
-import org.bouncycastle.util.Integers;
-
-/**
- * Diffie-Hellman key agreement using elliptic curve keys, ala IEEE P1363
- * both the simple one, and the simple one with cofactors are supported.
- *
- * Also, MQV key agreement per SEC-1
- */
-public class KeyAgreementSpi
- extends javax.crypto.KeyAgreementSpi
-{
- private static final X9IntegerConverter converter = new X9IntegerConverter();
- private static final Hashtable algorithms = new Hashtable();
-
- static
- {
- Integer i128 = Integers.valueOf(128);
- Integer i192 = Integers.valueOf(192);
- Integer i256 = Integers.valueOf(256);
-
- algorithms.put(NISTObjectIdentifiers.id_aes128_CBC.getId(), i128);
- algorithms.put(NISTObjectIdentifiers.id_aes192_CBC.getId(), i192);
- algorithms.put(NISTObjectIdentifiers.id_aes256_CBC.getId(), i256);
- algorithms.put(NISTObjectIdentifiers.id_aes128_wrap.getId(), i128);
- algorithms.put(NISTObjectIdentifiers.id_aes192_wrap.getId(), i192);
- algorithms.put(NISTObjectIdentifiers.id_aes256_wrap.getId(), i256);
- algorithms.put(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId(), i192);
- }
-
- private String kaAlgorithm;
- private BigInteger result;
- private ECDomainParameters parameters;
- private BasicAgreement agreement;
- private DerivationFunction kdf;
-
- private byte[] bigIntToBytes(
- BigInteger r)
- {
- return converter.integerToBytes(r, converter.getByteLength(parameters.getG().getX()));
- }
-
- protected KeyAgreementSpi(
- String kaAlgorithm,
- BasicAgreement agreement,
- DerivationFunction kdf)
- {
- this.kaAlgorithm = kaAlgorithm;
- this.agreement = agreement;
- this.kdf = kdf;
- }
-
- protected Key engineDoPhase(
- Key key,
- boolean lastPhase)
- throws InvalidKeyException, IllegalStateException
- {
- if (parameters == null)
- {
- throw new IllegalStateException(kaAlgorithm + " not initialised.");
- }
-
- if (!lastPhase)
- {
- throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
- }
-
- CipherParameters pubKey;
- if (agreement instanceof ECMQVBasicAgreement)
- {
- if (!(key instanceof MQVPublicKey))
- {
- throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
- + getSimpleName(MQVPublicKey.class) + " for doPhase");
- }
-
- MQVPublicKey mqvPubKey = (MQVPublicKey)key;
- ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
- ECUtil.generatePublicKeyParameter(mqvPubKey.getStaticKey());
- ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
- ECUtil.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
-
- pubKey = new MQVPublicParameters(staticKey, ephemKey);
-
- // TODO Validate that all the keys are using the same parameters?
- }
- else
- {
- if (!(key instanceof ECPublicKey))
- {
- throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
- + getSimpleName(ECPublicKey.class) + " for doPhase");
- }
-
- pubKey = ECUtil.generatePublicKeyParameter((PublicKey)key);
-
- // TODO Validate that all the keys are using the same parameters?
- }
-
- result = agreement.calculateAgreement(pubKey);
-
- return null;
- }
-
- protected byte[] engineGenerateSecret()
- throws IllegalStateException
- {
- if (kdf != null)
- {
- throw new UnsupportedOperationException(
- "KDF can only be used when algorithm is known");
- }
-
- return bigIntToBytes(result);
- }
-
- protected int engineGenerateSecret(
- byte[] sharedSecret,
- int offset)
- throws IllegalStateException, ShortBufferException
- {
- byte[] secret = engineGenerateSecret();
-
- if (sharedSecret.length - offset < secret.length)
- {
- throw new ShortBufferException(kaAlgorithm + " key agreement: need " + secret.length + " bytes");
- }
-
- System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
-
- return secret.length;
- }
-
- protected SecretKey engineGenerateSecret(
- String algorithm)
- throws NoSuchAlgorithmException
- {
- byte[] secret = bigIntToBytes(result);
-
- if (kdf != null)
- {
- if (!algorithms.containsKey(algorithm))
- {
- throw new NoSuchAlgorithmException("unknown algorithm encountered: " + algorithm);
- }
-
- int keySize = ((Integer)algorithms.get(algorithm)).intValue();
-
- DHKDFParameters params = new DHKDFParameters(new ASN1ObjectIdentifier(algorithm), keySize, secret);
-
- byte[] keyBytes = new byte[keySize / 8];
- kdf.init(params);
- kdf.generateBytes(keyBytes, 0, keyBytes.length);
- secret = keyBytes;
- }
- else
- {
- // TODO Should we be ensuring the key is the right length?
- }
-
- return new SecretKeySpec(secret, algorithm);
- }
-
- protected void engineInit(
- Key key,
- AlgorithmParameterSpec params,
- SecureRandom random)
- throws InvalidKeyException, InvalidAlgorithmParameterException
- {
- initFromKey(key);
- }
-
- protected void engineInit(
- Key key,
- SecureRandom random)
- throws InvalidKeyException
- {
- initFromKey(key);
- }
-
- private void initFromKey(Key key)
- throws InvalidKeyException
- {
- if (agreement instanceof ECMQVBasicAgreement)
- {
- if (!(key instanceof MQVPrivateKey))
- {
- throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
- + getSimpleName(MQVPrivateKey.class) + " for initialisation");
- }
-
- MQVPrivateKey mqvPrivKey = (MQVPrivateKey)key;
- ECPrivateKeyParameters staticPrivKey = (ECPrivateKeyParameters)
- ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
- ECPrivateKeyParameters ephemPrivKey = (ECPrivateKeyParameters)
- ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
-
- ECPublicKeyParameters ephemPubKey = null;
- if (mqvPrivKey.getEphemeralPublicKey() != null)
- {
- ephemPubKey = (ECPublicKeyParameters)
- ECUtil.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
- }
-
- MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
- this.parameters = staticPrivKey.getParameters();
-
- // TODO Validate that all the keys are using the same parameters?
-
- agreement.init(localParams);
- }
- else
- {
- if (!(key instanceof ECPrivateKey))
- {
- throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
- + getSimpleName(ECPrivateKey.class) + " for initialisation");
- }
-
- ECPrivateKeyParameters privKey = (ECPrivateKeyParameters)ECUtil.generatePrivateKeyParameter((PrivateKey)key);
- this.parameters = privKey.getParameters();
-
- agreement.init(privKey);
- }
- }
-
- private static String getSimpleName(Class clazz)
- {
- String fullName = clazz.getName();
-
- return fullName.substring(fullName.lastIndexOf('.') + 1);
- }
-
- public static class DH
- extends KeyAgreementSpi
- {
- public DH()
- {
- super("ECDH", new ECDHBasicAgreement(), null);
- }
- }
-
- public static class DHC
- extends KeyAgreementSpi
- {
- public DHC()
- {
- super("ECDHC", new ECDHCBasicAgreement(), null);
- }
- }
-
- public static class MQV
- extends KeyAgreementSpi
- {
- public MQV()
- {
- super("ECMQV", new ECMQVBasicAgreement(), null);
- }
- }
-
- public static class DHwithSHA1KDF
- extends KeyAgreementSpi
- {
- public DHwithSHA1KDF()
- {
- super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new ECDHKEKGenerator(new SHA1Digest()));
- }
- }
-
- public static class MQVwithSHA1KDF
- extends KeyAgreementSpi
- {
- public MQVwithSHA1KDF()
- {
- super("ECMQVwithSHA1KDF", new ECMQVBasicAgreement(), new ECDHKEKGenerator(new SHA1Digest()));
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyFactorySpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyFactorySpi.java
deleted file mode 100644
index 762f703e..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyFactorySpi.java
+++ /dev/null
@@ -1,200 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.io.IOException;
-import java.security.InvalidKeyException;
-import java.security.Key;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.spec.InvalidKeySpecException;
-import java.security.spec.KeySpec;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
-import org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi;
-import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
-import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPrivateKeySpec;
-import org.bouncycastle.jce.spec.ECPublicKeySpec;
-
-public class KeyFactorySpi
- extends BaseKeyFactorySpi
- implements AsymmetricKeyInfoConverter
-{
- String algorithm;
- ProviderConfiguration configuration;
-
- KeyFactorySpi(
- String algorithm,
- ProviderConfiguration configuration)
- {
- this.algorithm = algorithm;
- this.configuration = configuration;
- }
-
- protected Key engineTranslateKey(
- Key key)
- throws InvalidKeyException
- {
- if (key instanceof ECPublicKey)
- {
- return new BCECPublicKey((ECPublicKey)key, configuration);
- }
- else if (key instanceof ECPrivateKey)
- {
- return new BCECPrivateKey((ECPrivateKey)key, configuration);
- }
-
- throw new InvalidKeyException("key type unknown");
- }
-
- protected KeySpec engineGetKeySpec(
- Key key,
- Class spec)
- throws InvalidKeySpecException
- {
- if (spec.isAssignableFrom(org.bouncycastle.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey)
- {
- ECPublicKey k = (ECPublicKey)key;
- if (k.getParams() != null)
- {
- return new org.bouncycastle.jce.spec.ECPublicKeySpec(k.getQ(), k.getParameters());
- }
- else
- {
- ECParameterSpec implicitSpec = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- return new org.bouncycastle.jce.spec.ECPublicKeySpec(k.getQ(), implicitSpec);
- }
- }
- else if (spec.isAssignableFrom(org.bouncycastle.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey)
- {
- ECPrivateKey k = (ECPrivateKey)key;
-
- if (k.getParams() != null)
- {
- return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getD(), k.getParameters());
- }
- else
- {
- ECParameterSpec implicitSpec = configuration.getEcImplicitlyCa();
-
- return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getD(), implicitSpec);
- }
- }
- return super.engineGetKeySpec(key, spec);
- }
-
- protected PrivateKey engineGeneratePrivate(
- KeySpec keySpec)
- throws InvalidKeySpecException
- {
- if (keySpec instanceof ECPrivateKeySpec)
- {
- return new BCECPrivateKey(algorithm, (ECPrivateKeySpec)keySpec, configuration);
- }
-
- return super.engineGeneratePrivate(keySpec);
- }
-
- protected PublicKey engineGeneratePublic(
- KeySpec keySpec)
- throws InvalidKeySpecException
- {
- if (keySpec instanceof ECPublicKeySpec)
- {
- return new BCECPublicKey(algorithm, (ECPublicKeySpec)keySpec, configuration);
- }
-
- return super.engineGeneratePublic(keySpec);
- }
-
- public PrivateKey generatePrivate(PrivateKeyInfo keyInfo)
- throws IOException
- {
- ASN1ObjectIdentifier algOid = keyInfo.getPrivateKeyAlgorithm().getAlgorithm();
-
- if (algOid.equals(X9ObjectIdentifiers.id_ecPublicKey))
- {
- return new BCECPrivateKey(algorithm, keyInfo, configuration);
- }
- else
- {
- throw new IOException("algorithm identifier " + algOid + " in key not recognised");
- }
- }
-
- public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo)
- throws IOException
- {
- ASN1ObjectIdentifier algOid = keyInfo.getAlgorithm().getAlgorithm();
-
- if (algOid.equals(X9ObjectIdentifiers.id_ecPublicKey))
- {
- return new BCECPublicKey(algorithm, keyInfo, configuration);
- }
- else
- {
- throw new IOException("algorithm identifier " + algOid + " in key not recognised");
- }
- }
-
- public static class EC
- extends KeyFactorySpi
- {
- public EC()
- {
- super("EC", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECDSA
- extends KeyFactorySpi
- {
- public ECDSA()
- {
- super("ECDSA", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECGOST3410
- extends KeyFactorySpi
- {
- public ECGOST3410()
- {
- super("ECGOST3410", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECDH
- extends KeyFactorySpi
- {
- public ECDH()
- {
- super("ECDH", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECDHC
- extends KeyFactorySpi
- {
- public ECDHC()
- {
- super("ECDHC", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECMQV
- extends KeyFactorySpi
- {
- public ECMQV()
- {
- super("ECMQV", BouncyCastleProvider.CONFIGURATION);
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyPairGeneratorSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyPairGeneratorSpi.java
deleted file mode 100644
index 6cdfd1e4..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/KeyPairGeneratorSpi.java
+++ /dev/null
@@ -1,259 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidParameterException;
-import java.security.KeyPair;
-import java.security.SecureRandom;
-import java.security.spec.AlgorithmParameterSpec;
-import java.util.Hashtable;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.nist.NISTNamedCurves;
-import org.bouncycastle.asn1.sec.SECNamedCurves;
-import org.bouncycastle.asn1.teletrust.TeleTrusTNamedCurves;
-import org.bouncycastle.asn1.x9.X962NamedCurves;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
-import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
-import org.bouncycastle.jce.ECNamedCurveTable;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.util.Integers;
-
-public abstract class KeyPairGeneratorSpi
- extends java.security.KeyPairGenerator
-{
- public KeyPairGeneratorSpi(String algorithmName)
- {
- super(algorithmName);
- }
-
- public static class EC
- extends KeyPairGeneratorSpi
- {
- ECKeyGenerationParameters param;
- ECKeyPairGenerator engine = new ECKeyPairGenerator();
- ECParameterSpec ecParams = null;
- int strength = 239;
- int certainty = 50;
- SecureRandom random = new SecureRandom();
- boolean initialised = false;
- String algorithm;
- ProviderConfiguration configuration;
-
- static private Hashtable ecParameters;
-
- static {
- ecParameters = new Hashtable();
-
- ecParameters.put(Integers.valueOf(192),
- ECNamedCurveTable.getParameterSpec("prime192v1"));
- ecParameters.put(Integers.valueOf(239),
- ECNamedCurveTable.getParameterSpec("prime239v1"));
- ecParameters.put(Integers.valueOf(256),
- ECNamedCurveTable.getParameterSpec("prime256v1"));
- }
-
- public EC()
- {
- super("EC");
- this.algorithm = "EC";
- this.configuration = BouncyCastleProvider.CONFIGURATION;
- }
-
- public EC(
- String algorithm,
- ProviderConfiguration configuration)
- {
- super(algorithm);
- this.algorithm = algorithm;
- this.configuration = configuration;
- }
-
- public void initialize(
- int strength,
- SecureRandom random)
- {
- this.strength = strength;
- this.random = random;
- this.ecParams = (ECParameterSpec)ecParameters.get(Integers.valueOf(strength));
-
- if (ecParams != null)
- {
- param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else
- {
- throw new InvalidParameterException("unknown key size.");
- }
- }
-
- public void initialize(
- AlgorithmParameterSpec params,
- SecureRandom random)
- throws InvalidAlgorithmParameterException
- {
- if (params instanceof ECParameterSpec)
- {
- ECParameterSpec p = (ECParameterSpec)params;
- this.ecParams = (ECParameterSpec)params;
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params instanceof ECNamedCurveGenParameterSpec)
- {
- String curveName;
-
- curveName = ((ECNamedCurveGenParameterSpec)params).getName();
-
- X9ECParameters ecP = X962NamedCurves.getByName(curveName);
- if (ecP == null)
- {
- ecP = SECNamedCurves.getByName(curveName);
- if (ecP == null)
- {
- ecP = NISTNamedCurves.getByName(curveName);
- }
- if (ecP == null)
- {
- ecP = TeleTrusTNamedCurves.getByName(curveName);
- }
- if (ecP == null)
- {
- // See if it's actually an OID string (SunJSSE ServerHandshaker setupEphemeralECDHKeys bug)
- try
- {
- ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier(curveName);
- ecP = X962NamedCurves.getByOID(oid);
- if (ecP == null)
- {
- ecP = SECNamedCurves.getByOID(oid);
- }
- if (ecP == null)
- {
- ecP = NISTNamedCurves.getByOID(oid);
- }
- if (ecP == null)
- {
- ecP = TeleTrusTNamedCurves.getByOID(oid);
- }
- if (ecP == null)
- {
- throw new InvalidAlgorithmParameterException("unknown curve OID: " + curveName);
- }
- }
- catch (IllegalArgumentException ex)
- {
- throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName);
- }
- }
- }
-
- this.ecParams = new ECNamedCurveParameterSpec(
- curveName,
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- null); // ecP.getSeed()); Work-around JDK bug -- it won't look up named curves properly if seed is present
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params == null && configuration.getEcImplicitlyCa() != null)
- {
- ECParameterSpec p = configuration.getEcImplicitlyCa();
- this.ecParams = (ECParameterSpec)params;
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params == null && configuration.getEcImplicitlyCa() == null)
- {
- throw new InvalidAlgorithmParameterException("null parameter passed but no implicitCA set");
- }
- else
- {
- throw new InvalidAlgorithmParameterException("parameter object not a ECParameterSpec");
- }
- }
-
- public KeyPair generateKeyPair()
- {
- if (!initialised)
- {
- throw new IllegalStateException("EC Key Pair Generator not initialised");
- }
-
- AsymmetricCipherKeyPair pair = engine.generateKeyPair();
- ECPublicKeyParameters pub = (ECPublicKeyParameters)pair.getPublic();
- ECPrivateKeyParameters priv = (ECPrivateKeyParameters)pair.getPrivate();
-
- if (ecParams == null)
- {
- return new KeyPair(new BCECPublicKey(algorithm, pub, configuration),
- new BCECPrivateKey(algorithm, priv, configuration));
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecParams;
- BCECPublicKey pubKey = new BCECPublicKey(algorithm, pub, p, configuration);
-
- return new KeyPair(pubKey, new BCECPrivateKey(algorithm, priv, pubKey, p, configuration));
- }
- }
- }
-
- public static class ECDSA
- extends EC
- {
- public ECDSA()
- {
- super("ECDSA", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECDH
- extends EC
- {
- public ECDH()
- {
- super("ECDH", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECDHC
- extends EC
- {
- public ECDHC()
- {
- super("ECDHC", BouncyCastleProvider.CONFIGURATION);
- }
- }
-
- public static class ECMQV
- extends EC
- {
- public ECMQV()
- {
- super("ECMQV", BouncyCastleProvider.CONFIGURATION);
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java
deleted file mode 100644
index 1bb7d3b4..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ec/SignatureSpi.java
+++ /dev/null
@@ -1,355 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ec;
-
-import java.io.IOException;
-import java.math.BigInteger;
-import java.security.InvalidKeyException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.SecureRandom;
-
-import org.bouncycastle.asn1.ASN1EncodableVector;
-import org.bouncycastle.asn1.ASN1Encoding;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.ASN1Sequence;
-import org.bouncycastle.asn1.ASN1Integer;
-import org.bouncycastle.asn1.DERSequence;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.crypto.CipherParameters;
-import org.bouncycastle.crypto.DSA;
-import org.bouncycastle.crypto.Digest;
-import org.bouncycastle.crypto.digests.NullDigest;
-import org.bouncycastle.crypto.digests.RIPEMD160Digest;
-import org.bouncycastle.crypto.digests.SHA1Digest;
-import org.bouncycastle.crypto.digests.SHA224Digest;
-import org.bouncycastle.crypto.digests.SHA256Digest;
-import org.bouncycastle.crypto.digests.SHA384Digest;
-import org.bouncycastle.crypto.digests.SHA512Digest;
-import org.bouncycastle.crypto.params.ParametersWithRandom;
-import org.bouncycastle.crypto.signers.ECDSASigner;
-import org.bouncycastle.crypto.signers.ECNRSigner;
-import org.bouncycastle.jcajce.provider.asymmetric.util.DSABase;
-import org.bouncycastle.jcajce.provider.asymmetric.util.DSAEncoder;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jce.interfaces.ECKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-
-public class SignatureSpi
- extends DSABase
-{
- SignatureSpi(Digest digest, DSA signer, DSAEncoder encoder)
- {
- super("ECDSA", digest, signer, encoder);
- }
-
- protected void engineInitVerify(PublicKey publicKey)
- throws InvalidKeyException
- {
- CipherParameters param;
-
- if (publicKey instanceof ECPublicKey)
- {
- param = ECUtil.generatePublicKeyParameter(publicKey);
- }
- else
- {
- try
- {
- byte[] bytes = publicKey.getEncoded();
-
- publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
-
- if (publicKey instanceof ECPublicKey)
- {
- param = ECUtil.generatePublicKeyParameter(publicKey);
- }
- else
- {
- throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
- }
- }
- catch (Exception e)
- {
- throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
- }
- }
-
- digest.reset();
-
- signer.init(false, param);
- }
-
- protected void doEngineInitSign(
- PrivateKey privateKey,
- SecureRandom random)
- throws InvalidKeyException
- {
- CipherParameters param;
-
- if (privateKey instanceof ECKey)
- {
- param = ECUtil.generatePrivateKeyParameter(privateKey);
- }
- else
- {
- throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
- }
-
- digest.reset();
-
- if (random != null)
- {
- signer.init(true, new ParametersWithRandom(param, random));
- }
- else
- {
- signer.init(true, param);
- }
- }
-
- static public class ecDSA
- extends SignatureSpi
- {
- public ecDSA()
- {
- super(new SHA1Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSAnone
- extends SignatureSpi
- {
- public ecDSAnone()
- {
- super(new NullDigest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSA224
- extends SignatureSpi
- {
- public ecDSA224()
- {
- super(new SHA224Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSA256
- extends SignatureSpi
- {
- public ecDSA256()
- {
- super(new SHA256Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSA384
- extends SignatureSpi
- {
- public ecDSA384()
- {
- super(new SHA384Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSA512
- extends SignatureSpi
- {
- public ecDSA512()
- {
- super(new SHA512Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecDSARipeMD160
- extends SignatureSpi
- {
- public ecDSARipeMD160()
- {
- super(new RIPEMD160Digest(), new ECDSASigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecNR
- extends SignatureSpi
- {
- public ecNR()
- {
- super(new SHA1Digest(), new ECNRSigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecNR224
- extends SignatureSpi
- {
- public ecNR224()
- {
- super(new SHA224Digest(), new ECNRSigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecNR256
- extends SignatureSpi
- {
- public ecNR256()
- {
- super(new SHA256Digest(), new ECNRSigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecNR384
- extends SignatureSpi
- {
- public ecNR384()
- {
- super(new SHA384Digest(), new ECNRSigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecNR512
- extends SignatureSpi
- {
- public ecNR512()
- {
- super(new SHA512Digest(), new ECNRSigner(), new StdDSAEncoder());
- }
- }
-
- static public class ecCVCDSA
- extends SignatureSpi
- {
- public ecCVCDSA()
- {
- super(new SHA1Digest(), new ECDSASigner(), new CVCDSAEncoder());
- }
- }
-
- static public class ecCVCDSA224
- extends SignatureSpi
- {
- public ecCVCDSA224()
- {
- super(new SHA224Digest(), new ECDSASigner(), new CVCDSAEncoder());
- }
- }
-
- static public class ecCVCDSA256
- extends SignatureSpi
- {
- public ecCVCDSA256()
- {
- super(new SHA256Digest(), new ECDSASigner(), new CVCDSAEncoder());
- }
- }
-
- static public class ecCVCDSA384
- extends SignatureSpi
- {
- public ecCVCDSA384()
- {
- super(new SHA384Digest(), new ECDSASigner(), new CVCDSAEncoder());
- }
- }
-
- static public class ecCVCDSA512
- extends SignatureSpi
- {
- public ecCVCDSA512()
- {
- super(new SHA512Digest(), new ECDSASigner(), new CVCDSAEncoder());
- }
- }
-
- private static class StdDSAEncoder
- implements DSAEncoder
- {
- public byte[] encode(
- BigInteger r,
- BigInteger s)
- throws IOException
- {
- ASN1EncodableVector v = new ASN1EncodableVector();
-
- v.add(new ASN1Integer(r));
- v.add(new ASN1Integer(s));
-
- return new DERSequence(v).getEncoded(ASN1Encoding.DER);
- }
-
- public BigInteger[] decode(
- byte[] encoding)
- throws IOException
- {
- ASN1Sequence s = (ASN1Sequence)ASN1Primitive.fromByteArray(encoding);
- BigInteger[] sig = new BigInteger[2];
-
- sig[0] = ((ASN1Integer)s.getObjectAt(0)).getValue();
- sig[1] = ((ASN1Integer)s.getObjectAt(1)).getValue();
-
- return sig;
- }
- }
-
- private static class CVCDSAEncoder
- implements DSAEncoder
- {
- public byte[] encode(
- BigInteger r,
- BigInteger s)
- throws IOException
- {
- byte[] first = makeUnsigned(r);
- byte[] second = makeUnsigned(s);
- byte[] res;
-
- if (first.length > second.length)
- {
- res = new byte[first.length * 2];
- }
- else
- {
- res = new byte[second.length * 2];
- }
-
- System.arraycopy(first, 0, res, res.length / 2 - first.length, first.length);
- System.arraycopy(second, 0, res, res.length - second.length, second.length);
-
- return res;
- }
-
-
- private byte[] makeUnsigned(BigInteger val)
- {
- byte[] res = val.toByteArray();
-
- if (res[0] == 0)
- {
- byte[] tmp = new byte[res.length - 1];
-
- System.arraycopy(res, 1, tmp, 0, tmp.length);
-
- return tmp;
- }
-
- return res;
- }
-
- public BigInteger[] decode(
- byte[] encoding)
- throws IOException
- {
- BigInteger[] sig = new BigInteger[2];
-
- byte[] first = new byte[encoding.length / 2];
- byte[] second = new byte[encoding.length / 2];
-
- System.arraycopy(encoding, 0, first, 0, first.length);
- System.arraycopy(encoding, first.length, second, 0, second.length);
-
- sig[0] = new BigInteger(1, first);
- sig[1] = new BigInteger(1, second);
-
- return sig;
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PrivateKey.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PrivateKey.java
deleted file mode 100644
index c2e1d9b7..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PrivateKey.java
+++ /dev/null
@@ -1,359 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.math.BigInteger;
-import java.util.Enumeration;
-
-import org.bouncycastle.asn1.ASN1Encodable;
-import org.bouncycastle.asn1.ASN1Integer;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.ASN1Sequence;
-import org.bouncycastle.asn1.DERBitString;
-import org.bouncycastle.asn1.DERNull;
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
-import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
-import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
-import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
-import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x9.X962Parameters;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
-import org.bouncycastle.jce.interfaces.ECPointEncoder;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPrivateKeySpec;
-import org.bouncycastle.math.ec.ECCurve;
-import org.bouncycastle.math.ec.ECPoint;
-
-public class BCECGOST3410PrivateKey
- implements ECPrivateKey, PKCS12BagAttributeCarrier, ECPointEncoder
-{
- private String algorithm = "ECGOST3410";
- private boolean withCompression;
-
- private transient BigInteger d;
- private transient ECParameterSpec ecSpec;
- private transient DERBitString publicKey;
- private transient PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
-
- protected BCECGOST3410PrivateKey()
- {
- }
-
- BCECGOST3410PrivateKey(
- ECPrivateKey key)
- {
- this.d = key.getD();
- this.algorithm = key.getAlgorithm();
- this.ecSpec = key.getParameters();
- }
-
- public BCECGOST3410PrivateKey(
- ECPrivateKeySpec spec)
- {
- this.d = spec.getD();
- this.ecSpec = spec.getParams();
- }
-
- public BCECGOST3410PrivateKey(
- String algorithm,
- ECPrivateKeyParameters params,
- BCECGOST3410PublicKey pubKey,
- ECParameterSpec spec)
- {
- ECDomainParameters dp = params.getParameters();
-
- this.algorithm = algorithm;
- this.d = params.getD();
-
- if (spec == null)
- {
- this.ecSpec = new ECParameterSpec(
- dp.getCurve(),
- dp.getG(),
- dp.getN(),
- dp.getH(),
- dp.getSeed());
- }
- else
- {
- this.ecSpec = spec;
- }
-
- publicKey = getPublicKeyDetails(pubKey);
- }
-
- public BCECGOST3410PrivateKey(
- String algorithm,
- ECPrivateKeyParameters params)
- {
- this.algorithm = algorithm;
- this.d = params.getD();
- this.ecSpec = null;
- }
-
- public BCECGOST3410PrivateKey(
- String algorithm,
- BCECGOST3410PrivateKey key)
- {
- this.algorithm = algorithm;
- this.d = key.d;
- this.ecSpec = key.ecSpec;
- this.withCompression = key.withCompression;
- this.publicKey = key.publicKey;
- this.attrCarrier = key.attrCarrier;
- }
-
- BCECGOST3410PrivateKey(
- PrivateKeyInfo info)
- {
- populateFromPrivKeyInfo(info);
- }
-
- private void populateFromPrivKeyInfo(PrivateKeyInfo info)
- {
- X962Parameters params = X962Parameters.getInstance(info.getAlgorithmId().getParameters());
-
- if (params.isNamedCurve())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)params.getParameters();
- ECDomainParameters ecP = ECGOST3410NamedCurves.getByOID(oid);
-
- ecSpec = new ECNamedCurveParameterSpec(
- ECUtil.getCurveName(oid),
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- }
- else if (params.isImplicitlyCA())
- {
- ecSpec = null;
- }
- else
- {
- X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
- ecSpec = new ECParameterSpec(ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- }
-
- if (info.getPrivateKey() instanceof ASN1Integer)
- {
- ASN1Integer derD = ASN1Integer.getInstance(info.getPrivateKey());
-
- this.d = derD.getValue();
- }
- else
- {
- ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence)info.getPrivateKey());
-
- this.d = ec.getKey();
- this.publicKey = ec.getPublicKey();
- }
- }
-
- public String getAlgorithm()
- {
- return algorithm;
- }
-
- /**
- * return the encoding format we produce in getEncoded().
- *
- * @return the string "PKCS#8"
- */
- public String getFormat()
- {
- return "PKCS#8";
- }
-
- /**
- * Return a PKCS8 representation of the key. The sequence returned
- * represents a full PrivateKeyInfo object.
- *
- * @return a PKCS8 representation of the key.
- */
- public byte[] getEncoded()
- {
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- DEROutputStream dOut = new DEROutputStream(bOut);
- X962Parameters params = null;
-
- if (ecSpec instanceof ECNamedCurveParameterSpec)
- {
- ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveParameterSpec)ecSpec).getName());
-
- params = new X962Parameters(curveOid);
- }
- else if (ecSpec == null)
- {
- params = new X962Parameters(DERNull.INSTANCE);
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecSpec;
-
- ECPoint pG = p.getG().normalize();
- ECPoint g = pG.getCurve().createPoint(pG.getAffineXCoord().toBigInteger(), pG.getAffineYCoord().toBigInteger());
-
- X9ECParameters ecP = new X9ECParameters(
- p.getCurve(),
- g,
- p.getN(),
- p.getH(),
- p.getSeed());
-
- params = new X962Parameters(ecP);
- }
-
- PrivateKeyInfo info;
- ECPrivateKeyStructure keyStructure;
-
- if (publicKey != null)
- {
- keyStructure = new ECPrivateKeyStructure(this.getD(), publicKey, params);
- }
- else
- {
- keyStructure = new ECPrivateKeyStructure(this.getD(), params);
- }
-
- try
- {
- if (algorithm.equals("ECGOST3410"))
- {
- info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), keyStructure);
- }
- else
- {
- info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), keyStructure);
- }
-
- return KeyUtil.getEncodedPrivateKeyInfo(info);
- }
- catch (IOException e)
- {
- return null;
- }
- }
-
- public ECParameterSpec getParams()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public ECParameterSpec getParameters()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public BigInteger getD()
- {
- return d;
- }
-
- public void setBagAttribute(
- ASN1ObjectIdentifier oid,
- ASN1Encodable attribute)
- {
- attrCarrier.setBagAttribute(oid, attribute);
- }
-
- public ASN1Encodable getBagAttribute(
- ASN1ObjectIdentifier oid)
- {
- return attrCarrier.getBagAttribute(oid);
- }
-
- public Enumeration getBagAttributeKeys()
- {
- return attrCarrier.getBagAttributeKeys();
- }
-
- public void setPointFormat(String style)
- {
- withCompression = !("UNCOMPRESSED".equalsIgnoreCase(style));
- }
-
- ECParameterSpec engineGetSpec()
- {
- if (ecSpec != null)
- {
- return ecSpec;
- }
-
- return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- }
-
- public boolean equals(Object o)
- {
- if (!(o instanceof BCECGOST3410PrivateKey))
- {
- return false;
- }
-
- BCECGOST3410PrivateKey other = (BCECGOST3410PrivateKey)o;
-
- return getD().equals(other.getD()) && (engineGetSpec().equals(other.engineGetSpec()));
- }
-
- public int hashCode()
- {
- return getD().hashCode() ^ engineGetSpec().hashCode();
- }
-
- private DERBitString getPublicKeyDetails(BCECGOST3410PublicKey pub)
- {
- try
- {
- SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(pub.getEncoded()));
-
- return info.getPublicKeyData();
- }
- catch (IOException e)
- { // should never happen
- return null;
- }
- }
-
-
- private void readObject(
- ObjectInputStream in)
- throws IOException, ClassNotFoundException
- {
- in.defaultReadObject();
-
- byte[] enc = (byte[])in.readObject();
-
- populateFromPrivKeyInfo(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));
-
- this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
- }
-
- private void writeObject(
- ObjectOutputStream out)
- throws IOException
- {
- out.defaultWriteObject();
-
- out.writeObject(this.getEncoded());
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PublicKey.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PublicKey.java
deleted file mode 100644
index 8d33fd90..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PublicKey.java
+++ /dev/null
@@ -1,454 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.math.BigInteger;
-
-import org.bouncycastle.asn1.ASN1Encodable;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.ASN1OctetString;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.ASN1Sequence;
-import org.bouncycastle.asn1.DERBitString;
-import org.bouncycastle.asn1.DERNull;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.DEROctetString;
-import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
-import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
-import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters;
-import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x9.X962Parameters;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.asn1.x9.X9ECPoint;
-import org.bouncycastle.asn1.x9.X9IntegerConverter;
-import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
-import org.bouncycastle.jce.ECGOST3410NamedCurveTable;
-import org.bouncycastle.jce.interfaces.ECPointEncoder;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPublicKeySpec;
-import org.bouncycastle.math.ec.ECCurve;
-import org.bouncycastle.math.ec.ECPoint;
-
-public class BCECGOST3410PublicKey
- implements ECPublicKey, ECPointEncoder
-{
- private String algorithm = "ECGOST3410";
- private boolean withCompression;
-
- private transient org.bouncycastle.math.ec.ECPoint q;
- private transient ECParameterSpec ecSpec;
- private transient GOST3410PublicKeyAlgParameters gostParams;
-
- public BCECGOST3410PublicKey(
- String algorithm,
- BCECGOST3410PublicKey key)
- {
- this.algorithm = algorithm;
- this.q = key.q;
- this.ecSpec = key.ecSpec;
- this.withCompression = key.withCompression;
- this.gostParams = key.gostParams;
- }
-
- public BCECGOST3410PublicKey(
- ECPublicKeySpec spec)
- {
- this.q = spec.getQ();
-
- if (spec.getParams() != null)
- {
- this.ecSpec = spec.getParams();
- }
- else
- {
- if (q.getCurve() == null)
- {
- org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
- }
- this.ecSpec = null;
- }
- }
-
- public BCECGOST3410PublicKey(
- String algorithm,
- ECPublicKeyParameters params,
- ECParameterSpec spec)
- {
- ECDomainParameters dp = params.getParameters();
-
- this.algorithm = algorithm;
- this.q = params.getQ();
-
- if (spec == null)
- {
- this.ecSpec = new ECParameterSpec(
- dp.getCurve(),
- dp.getG(),
- dp.getN(),
- dp.getH(),
- dp.getSeed());
- }
- else
- {
- this.ecSpec = spec;
- }
- }
-
- public BCECGOST3410PublicKey(
- String algorithm,
- ECPublicKeyParameters params)
- {
- this.algorithm = algorithm;
- this.q = params.getQ();
- this.ecSpec = null;
- }
-
- BCECGOST3410PublicKey(
- ECPublicKey key)
- {
- this.q = key.getQ();
- this.algorithm = key.getAlgorithm();
- this.ecSpec = key.getParameters();
- }
-
- BCECGOST3410PublicKey(
- String algorithm,
- ECPoint q,
- ECParameterSpec ecSpec)
- {
- this.algorithm = algorithm;
- this.q = q;
- this.ecSpec = ecSpec;
- }
-
- BCECGOST3410PublicKey(
- SubjectPublicKeyInfo info)
- {
- populateFromPubKeyInfo(info);
- }
-
- private void populateFromPubKeyInfo(SubjectPublicKeyInfo info)
- {
- if (info.getAlgorithmId().getObjectId().equals(CryptoProObjectIdentifiers.gostR3410_2001))
- {
- DERBitString bits = info.getPublicKeyData();
- ASN1OctetString key;
- this.algorithm = "ECGOST3410";
-
- try
- {
- key = (ASN1OctetString)ASN1Primitive.fromByteArray(bits.getBytes());
- }
- catch (IOException ex)
- {
- throw new IllegalArgumentException("error recovering public key");
- }
-
- byte[] keyEnc = key.getOctets();
- byte[] x = new byte[32];
- byte[] y = new byte[32];
-
- for (int i = 0; i != x.length; i++)
- {
- x[i] = keyEnc[32 - 1 - i];
- }
-
- for (int i = 0; i != y.length; i++)
- {
- y[i] = keyEnc[64 - 1 - i];
- }
-
- gostParams = new GOST3410PublicKeyAlgParameters((ASN1Sequence)info.getAlgorithmId().getParameters());
-
- ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
-
- ecSpec = spec;
-
- this.q = spec.getCurve().createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
- }
- else
- {
- X962Parameters params = X962Parameters.getInstance(info.getAlgorithmId().getParameters());
- ECCurve curve;
-
- if (params.isNamedCurve())
- {
- ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
- X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
-
- ecSpec = new ECNamedCurveParameterSpec(
- ECUtil.getCurveName(oid),
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- curve = ((ECParameterSpec)ecSpec).getCurve();
- }
- else if (params.isImplicitlyCA())
- {
- ecSpec = null;
- curve = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa().getCurve();
- }
- else
- {
- X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
- ecSpec = new ECParameterSpec(
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
- curve = ((ECParameterSpec)ecSpec).getCurve();
- }
-
- DERBitString bits = info.getPublicKeyData();
- byte[] data = bits.getBytes();
- ASN1OctetString key = new DEROctetString(data);
-
- //
- // extra octet string - one of our old certs...
- //
- if (data[0] == 0x04 && data[1] == data.length - 2
- && (data[2] == 0x02 || data[2] == 0x03))
- {
- int qLength = new X9IntegerConverter().getByteLength(curve);
-
- if (qLength >= data.length - 3)
- {
- try
- {
- key = (ASN1OctetString)ASN1Primitive.fromByteArray(data);
- }
- catch (IOException ex)
- {
- throw new IllegalArgumentException("error recovering public key");
- }
- }
- }
-
- X9ECPoint derQ = new X9ECPoint(curve, key);
-
- this.q = derQ.getPoint();
- }
- }
-
- public String getAlgorithm()
- {
- return algorithm;
- }
-
- public String getFormat()
- {
- return "X.509";
- }
-
- public byte[] getEncoded()
- {
- SubjectPublicKeyInfo info;
-
- if (algorithm.equals("ECGOST3410"))
- {
- ASN1Encodable params = null;
- if (gostParams != null)
- {
- params = gostParams;
- }
- else if (ecSpec instanceof ECNamedCurveParameterSpec)
- {
- params = new GOST3410PublicKeyAlgParameters(
- ECGOST3410NamedCurves.getOID(((ECNamedCurveParameterSpec)ecSpec).getName()),
- CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecSpec;
-
- ECCurve curve = p.getG().getCurve();
- ECPoint generator = curve.createPoint(p.getG().getX().toBigInteger(), p.getG().getY().toBigInteger(), withCompression);
-
- X9ECParameters ecP = new X9ECParameters(
- p.getCurve(), generator, p.getN(), p.getH(), p.getSeed());
-
- params = new X962Parameters(ecP);
- }
-
- ECPoint qq = this.getQ();
- ECPoint point = qq.getCurve().createPoint(qq.getX().toBigInteger(), qq.getY().toBigInteger(), false);
- ASN1OctetString p = ASN1OctetString.getInstance(new X9ECPoint(point));
-
- BigInteger bX = this.q.getX().toBigInteger();
- BigInteger bY = this.q.getY().toBigInteger();
- byte[] encKey = new byte[64];
-
- byte[] val = bX.toByteArray();
-
- for (int i = 0; i != 32; i++)
- {
- encKey[i] = val[val.length - 1 - i];
- }
-
- val = bY.toByteArray();
-
- for (int i = 0; i != 32; i++)
- {
- encKey[32 + i] = val[val.length - 1 - i];
- }
-
- try
- {
- info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
- }
- catch (IOException e)
- {
- return null;
- }
- }
- else
- {
- X962Parameters params = null;
- if (ecSpec instanceof ECNamedCurveParameterSpec)
- {
- ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveParameterSpec)ecSpec).getName());
-
- if (curveOid == null)
- {
- curveOid = new ASN1ObjectIdentifier(((ECNamedCurveParameterSpec)ecSpec).getName());
- }
- params = new X962Parameters(curveOid);
- }
- else if (ecSpec == null)
- {
- params = new X962Parameters(DERNull.INSTANCE);
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecSpec;
-
- ECCurve curve = p.getG().getCurve();
- ECPoint generator = curve.createPoint(p.getG().getX().toBigInteger(), p.getG().getY().toBigInteger(), withCompression);
-
- X9ECParameters ecP = new X9ECParameters(
- p.getCurve(), generator, p.getN(), p.getH(), p.getSeed());
-
- params = new X962Parameters(ecP);
- }
-
- ECCurve curve = this.engineGetQ().getCurve();
- ECPoint point = curve.createPoint(this.getQ().getX().toBigInteger(), this.getQ().getY().toBigInteger(), withCompression);
- ASN1OctetString p = ASN1OctetString.getInstance(new X9ECPoint(point));
-
- info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
- }
-
- return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
- }
-
- public ECParameterSpec getParams()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public ECParameterSpec getParameters()
- {
- return (ECParameterSpec)ecSpec;
- }
-
- public org.bouncycastle.math.ec.ECPoint getQ()
- {
- if (ecSpec == null)
- {
- if (q instanceof org.bouncycastle.math.ec.ECPoint.Fp)
- {
- return new org.bouncycastle.math.ec.ECPoint.Fp(null, q.getX(), q.getY());
- }
- else
- {
- return new org.bouncycastle.math.ec.ECPoint.F2m(null, q.getX(), q.getY());
- }
- }
-
- return q;
- }
-
- public org.bouncycastle.math.ec.ECPoint engineGetQ()
- {
- return q;
- }
-
- public String toString()
- {
- StringBuffer buf = new StringBuffer();
- String nl = System.getProperty("line.separator");
-
- buf.append("EC Public Key").append(nl);
- buf.append(" X: ").append(this.getQ().getX().toBigInteger().toString(16)).append(nl);
- buf.append(" Y: ").append(this.getQ().getY().toBigInteger().toString(16)).append(nl);
-
- return buf.toString();
-
- }
-
- public void setPointFormat(String style)
- {
- withCompression = !("UNCOMPRESSED".equalsIgnoreCase(style));
- }
-
- ECParameterSpec engineGetSpec()
- {
- if (ecSpec != null)
- {
- return (ECParameterSpec)ecSpec;
- }
-
- return BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- }
-
- public boolean equals(Object o)
- {
- if (!(o instanceof BCECGOST3410PublicKey))
- {
- return false;
- }
-
- BCECGOST3410PublicKey other = (BCECGOST3410PublicKey)o;
-
- return getQ().equals(other.getQ()) && (engineGetSpec().equals(other.engineGetSpec()));
- }
-
- public int hashCode()
- {
- return getQ().hashCode() ^ engineGetSpec().hashCode();
- }
-
- private void readObject(
- ObjectInputStream in)
- throws IOException, ClassNotFoundException
- {
- in.defaultReadObject();
-
- byte[] enc = (byte[])in.readObject();
-
- populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));
- }
-
- private void writeObject(
- ObjectOutputStream out)
- throws IOException
- {
- out.defaultWriteObject();
-
- out.writeObject(this.getEncoded());
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyFactorySpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyFactorySpi.java
deleted file mode 100644
index f0caf5ce..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyFactorySpi.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
-
-import java.io.IOException;
-import java.security.InvalidKeyException;
-import java.security.Key;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.spec.InvalidKeySpecException;
-import java.security.spec.KeySpec;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
-import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-import org.bouncycastle.jce.spec.ECPrivateKeySpec;
-import org.bouncycastle.jce.spec.ECPublicKeySpec;
-
-public class KeyFactorySpi
- extends BaseKeyFactorySpi
-{
- public KeyFactorySpi()
- {
- }
-
- protected KeySpec engineGetKeySpec(
- Key key,
- Class spec)
- throws InvalidKeySpecException
- {
- if (spec.isAssignableFrom(org.bouncycastle.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey)
- {
- ECPublicKey k = (ECPublicKey)key;
- if (k.getParams() != null)
- {
- return new org.bouncycastle.jce.spec.ECPublicKeySpec(k.getQ(), k.getParameters());
- }
- else
- {
- ECParameterSpec implicitSpec = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- return new org.bouncycastle.jce.spec.ECPublicKeySpec(k.getQ(), implicitSpec);
- }
- }
- else if (spec.isAssignableFrom(org.bouncycastle.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey)
- {
- ECPrivateKey k = (ECPrivateKey)key;
-
- if (k.getParams() != null)
- {
- return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getD(), k.getParameters());
- }
- else
- {
- ECParameterSpec implicitSpec = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getD(), implicitSpec);
- }
- }
-
- return super.engineGetKeySpec(key, spec);
- }
-
- protected Key engineTranslateKey(
- Key key)
- throws InvalidKeyException
- {
- throw new InvalidKeyException("key type unknown");
- }
-
- protected PrivateKey engineGeneratePrivate(
- KeySpec keySpec)
- throws InvalidKeySpecException
- {
- if (keySpec instanceof ECPrivateKeySpec)
- {
- return new BCECGOST3410PrivateKey((ECPrivateKeySpec)keySpec);
- }
-
- return super.engineGeneratePrivate(keySpec);
- }
-
- protected PublicKey engineGeneratePublic(
- KeySpec keySpec)
- throws InvalidKeySpecException
- {
- if (keySpec instanceof ECPublicKeySpec)
- {
- return new BCECGOST3410PublicKey((ECPublicKeySpec)keySpec);
- }
-
- return super.engineGeneratePublic(keySpec);
- }
-
- public PrivateKey generatePrivate(PrivateKeyInfo keyInfo)
- throws IOException
- {
- ASN1ObjectIdentifier algOid = keyInfo.getPrivateKeyAlgorithm().getAlgorithm();
-
- if (algOid.equals(CryptoProObjectIdentifiers.gostR3410_2001))
- {
- return new BCECGOST3410PrivateKey(keyInfo);
- }
- else
- {
- throw new IOException("algorithm identifier " + algOid + " in key not recognised");
- }
- }
-
- public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo)
- throws IOException
- {
- ASN1ObjectIdentifier algOid = keyInfo.getAlgorithm().getAlgorithm();
-
- if (algOid.equals(CryptoProObjectIdentifiers.gostR3410_2001))
- {
- return new BCECGOST3410PublicKey(keyInfo);
- }
- else
- {
- throw new IOException("algorithm identifier " + algOid + " in key not recognised");
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyPairGeneratorSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyPairGeneratorSpi.java
deleted file mode 100644
index d293cd6c..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/KeyPairGeneratorSpi.java
+++ /dev/null
@@ -1,144 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidParameterException;
-import java.security.KeyPair;
-import java.security.SecureRandom;
-import java.security.spec.AlgorithmParameterSpec;
-
-import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
-import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
-import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
-import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-
-public class KeyPairGeneratorSpi
- extends java.security.KeyPairGenerator
-{
- ECParameterSpec ecParams = null;
- ECKeyPairGenerator engine = new ECKeyPairGenerator();
-
- String algorithm = "ECGOST3410";
- ECKeyGenerationParameters param;
- int strength = 239;
- SecureRandom random = null;
- boolean initialised = false;
-
- public KeyPairGeneratorSpi()
- {
- super("ECGOST3410");
- }
-
- public void initialize(
- int strength,
- SecureRandom random)
- {
- this.strength = strength;
- this.random = random;
-
- if (ecParams != null)
- {
- param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else
- {
- throw new InvalidParameterException("unknown key size.");
- }
- }
-
- public void initialize(
- AlgorithmParameterSpec params,
- SecureRandom random)
- throws InvalidAlgorithmParameterException
- {
- if (params instanceof ECParameterSpec)
- {
- ECParameterSpec p = (ECParameterSpec)params;
- this.ecParams = p;
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params instanceof ECNamedCurveGenParameterSpec)
- {
- String curveName;
-
- curveName = ((ECNamedCurveGenParameterSpec)params).getName();
-
- ECDomainParameters ecP = ECGOST3410NamedCurves.getByName(curveName);
- if (ecP == null)
- {
- throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName);
- }
-
- this.ecParams = new ECNamedCurveParameterSpec(
- curveName,
- ecP.getCurve(),
- ecP.getG(),
- ecP.getN(),
- ecP.getH(),
- ecP.getSeed());
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params == null && BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa() != null)
- {
- ECParameterSpec p = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- this.ecParams = null;
-
- param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);
-
- engine.init(param);
- initialised = true;
- }
- else if (params == null && BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa() == null)
- {
- throw new InvalidAlgorithmParameterException("null parameter passed but no implicitCA set");
- }
- else
- {
- throw new InvalidAlgorithmParameterException("parameter object not a ECParameterSpec: " + params.getClass().getName());
- }
- }
-
- public KeyPair generateKeyPair()
- {
- if (!initialised)
- {
- throw new IllegalStateException("EC Key Pair Generator not initialised");
- }
-
- AsymmetricCipherKeyPair pair = engine.generateKeyPair();
- ECPublicKeyParameters pub = (ECPublicKeyParameters)pair.getPublic();
- ECPrivateKeyParameters priv = (ECPrivateKeyParameters)pair.getPrivate();
-
- if (ecParams == null)
- {
- return new KeyPair(new BCECGOST3410PublicKey(algorithm, pub),
- new BCECGOST3410PrivateKey(algorithm, priv));
- }
- else
- {
- ECParameterSpec p = (ECParameterSpec)ecParams;
-
- BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
- return new KeyPair(pubKey,
- new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
- }
- }
-}
-
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/SignatureSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/SignatureSpi.java
deleted file mode 100644
index 61824be6..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/ecgost/SignatureSpi.java
+++ /dev/null
@@ -1,219 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
-
-import java.math.BigInteger;
-import java.security.InvalidKeyException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.SignatureException;
-import java.security.spec.AlgorithmParameterSpec;
-
-import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
-import org.bouncycastle.crypto.CipherParameters;
-import org.bouncycastle.crypto.DSA;
-import org.bouncycastle.crypto.Digest;
-import org.bouncycastle.crypto.digests.GOST3411Digest;
-import org.bouncycastle.crypto.params.ParametersWithRandom;
-import org.bouncycastle.crypto.signers.ECGOST3410Signer;
-import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
-import org.bouncycastle.jce.interfaces.ECKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.interfaces.GOST3410Key;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jcajce.provider.asymmetric.util.GOST3410Util;
-
-public class SignatureSpi
- extends java.security.Signature
- implements PKCSObjectIdentifiers, X509ObjectIdentifiers
-{
- private Digest digest;
- private DSA signer;
-
- public SignatureSpi()
- {
- super("ECGOST3410");
- this.digest = new GOST3411Digest();
- this.signer = new ECGOST3410Signer();
- }
-
- protected void engineInitVerify(
- PublicKey publicKey)
- throws InvalidKeyException
- {
- CipherParameters param;
-
- if (publicKey instanceof ECPublicKey)
- {
- param = ECUtil.generatePublicKeyParameter(publicKey);
- }
- else if (publicKey instanceof GOST3410Key)
- {
- param = GOST3410Util.generatePublicKeyParameter(publicKey);
- }
- else
- {
- try
- {
- byte[] bytes = publicKey.getEncoded();
-
- publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
-
- if (publicKey instanceof ECPublicKey)
- {
- param = ECUtil.generatePublicKeyParameter(publicKey);
- }
- else
- {
- throw new InvalidKeyException("can't recognise key type in DSA based signer");
- }
- }
- catch (Exception e)
- {
- throw new InvalidKeyException("can't recognise key type in DSA based signer");
- }
- }
-
- digest.reset();
- signer.init(false, param);
- }
-
- protected void engineInitSign(
- PrivateKey privateKey)
- throws InvalidKeyException
- {
- CipherParameters param;
-
- if (privateKey instanceof ECKey)
- {
- param = ECUtil.generatePrivateKeyParameter(privateKey);
- }
- else
- {
- param = GOST3410Util.generatePrivateKeyParameter(privateKey);
- }
-
- digest.reset();
-
- if (appRandom != null)
- {
- signer.init(true, new ParametersWithRandom(param, appRandom));
- }
- else
- {
- signer.init(true, param);
- }
- }
-
- protected void engineUpdate(
- byte b)
- throws SignatureException
- {
- digest.update(b);
- }
-
- protected void engineUpdate(
- byte[] b,
- int off,
- int len)
- throws SignatureException
- {
- digest.update(b, off, len);
- }
-
- protected byte[] engineSign()
- throws SignatureException
- {
- byte[] hash = new byte[digest.getDigestSize()];
-
- digest.doFinal(hash, 0);
-
- try
- {
- byte[] sigBytes = new byte[64];
- BigInteger[] sig = signer.generateSignature(hash);
- byte[] r = sig[0].toByteArray();
- byte[] s = sig[1].toByteArray();
-
- if (s[0] != 0)
- {
- System.arraycopy(s, 0, sigBytes, 32 - s.length, s.length);
- }
- else
- {
- System.arraycopy(s, 1, sigBytes, 32 - (s.length - 1), s.length - 1);
- }
-
- if (r[0] != 0)
- {
- System.arraycopy(r, 0, sigBytes, 64 - r.length, r.length);
- }
- else
- {
- System.arraycopy(r, 1, sigBytes, 64 - (r.length - 1), r.length - 1);
- }
-
- return sigBytes;
- }
- catch (Exception e)
- {
- throw new SignatureException(e.toString());
- }
- }
-
- protected boolean engineVerify(
- byte[] sigBytes)
- throws SignatureException
- {
- byte[] hash = new byte[digest.getDigestSize()];
-
- digest.doFinal(hash, 0);
-
- BigInteger[] sig;
-
- try
- {
- byte[] r = new byte[32];
- byte[] s = new byte[32];
-
- System.arraycopy(sigBytes, 0, s, 0, 32);
-
- System.arraycopy(sigBytes, 32, r, 0, 32);
-
- sig = new BigInteger[2];
- sig[0] = new BigInteger(1, r);
- sig[1] = new BigInteger(1, s);
- }
- catch (Exception e)
- {
- throw new SignatureException("error decoding signature bytes.");
- }
-
- return signer.verifySignature(hash, sig[0], sig[1]);
- }
-
- protected void engineSetParameter(
- AlgorithmParameterSpec params)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- /**
- * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
- */
- protected void engineSetParameter(
- String param,
- Object value)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- /**
- * @deprecated
- */
- protected Object engineGetParameter(
- String param)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/elgamal/CipherSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/elgamal/CipherSpi.java
deleted file mode 100644
index cf96606c..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/elgamal/CipherSpi.java
+++ /dev/null
@@ -1,299 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.elgamal;
-
-import java.security.AlgorithmParameters;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.InvalidParameterException;
-import java.security.Key;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.SecureRandom;
-import java.security.spec.AlgorithmParameterSpec;
-
-import javax.crypto.BadPaddingException;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.interfaces.DHKey;
-
-import org.bouncycastle.crypto.AsymmetricBlockCipher;
-import org.bouncycastle.crypto.BufferedAsymmetricBlockCipher;
-import org.bouncycastle.crypto.CipherParameters;
-import org.bouncycastle.crypto.InvalidCipherTextException;
-import org.bouncycastle.crypto.encodings.ISO9796d1Encoding;
-import org.bouncycastle.crypto.encodings.OAEPEncoding;
-import org.bouncycastle.crypto.encodings.PKCS1Encoding;
-import org.bouncycastle.crypto.engines.ElGamalEngine;
-import org.bouncycastle.crypto.params.ParametersWithRandom;
-import org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi;
-import org.bouncycastle.jce.interfaces.ElGamalKey;
-import org.bouncycastle.jce.interfaces.ElGamalPrivateKey;
-import org.bouncycastle.jce.interfaces.ElGamalPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.util.Strings;
-
-public class CipherSpi
- extends BaseCipherSpi
-{
- private BufferedAsymmetricBlockCipher cipher;
- private AlgorithmParameterSpec paramSpec;
- private AlgorithmParameters engineParams;
-
- public CipherSpi(
- AsymmetricBlockCipher engine)
- {
- cipher = new BufferedAsymmetricBlockCipher(engine);
- }
-
- protected int engineGetBlockSize()
- {
- return cipher.getInputBlockSize();
- }
-
- protected int engineGetKeySize(
- Key key)
- {
- if (key instanceof ElGamalKey)
- {
- ElGamalKey k = (ElGamalKey)key;
-
- return k.getParameters().getP().bitLength();
- }
- else if (key instanceof DHKey)
- {
- DHKey k = (DHKey)key;
-
- return k.getParams().getP().bitLength();
- }
-
- throw new IllegalArgumentException("not an ElGamal key!");
- }
-
- protected int engineGetOutputSize(
- int inputLen)
- {
- return cipher.getOutputBlockSize();
- }
-
- protected AlgorithmParameters engineGetParameters()
- {
- if (engineParams == null)
- {
- if (paramSpec != null)
- {
- try
- {
- engineParams = AlgorithmParameters.getInstance("OAEP", BouncyCastleProvider.PROVIDER_NAME);
- engineParams.init(paramSpec);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e.toString());
- }
- }
- }
-
- return engineParams;
- }
-
- protected void engineSetMode(
- String mode)
- throws NoSuchAlgorithmException
- {
- String md = Strings.toUpperCase(mode);
-
- if (md.equals("NONE") || md.equals("ECB"))
- {
- return;
- }
-
- throw new NoSuchAlgorithmException("can't support mode " + mode);
- }
-
- protected void engineSetPadding(
- String padding)
- throws NoSuchPaddingException
- {
- String pad = Strings.toUpperCase(padding);
-
- if (pad.equals("NOPADDING"))
- {
- cipher = new BufferedAsymmetricBlockCipher(new ElGamalEngine());
- }
- else if (pad.equals("PKCS1PADDING"))
- {
- cipher = new BufferedAsymmetricBlockCipher(new PKCS1Encoding(new ElGamalEngine()));
- }
- else if (pad.equals("ISO9796-1PADDING"))
- {
- cipher = new BufferedAsymmetricBlockCipher(new ISO9796d1Encoding(new ElGamalEngine()));
- }
- else if (pad.equals("OAEPPADDING"))
- {
- cipher = new BufferedAsymmetricBlockCipher(new OAEPEncoding(new ElGamalEngine()));
- }
- else if (pad.equals("OAEPWITHSHA1ANDMGF1PADDING"))
- {
- cipher = new BufferedAsymmetricBlockCipher(new OAEPEncoding(new ElGamalEngine()));
- }
- else
- {
- throw new NoSuchPaddingException(padding + " unavailable with ElGamal.");
- }
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- AlgorithmParameterSpec params,
- SecureRandom random)
- throws InvalidKeyException
- {
- CipherParameters param;
-
- if (params == null)
- {
- if (key instanceof ElGamalPublicKey)
- {
- param = ElGamalUtil.generatePublicKeyParameter((PublicKey)key);
- }
- else if (key instanceof ElGamalPrivateKey)
- {
- param = ElGamalUtil.generatePrivateKeyParameter((PrivateKey)key);
- }
- else
- {
- throw new InvalidKeyException("unknown key type passed to ElGamal");
- }
- }
- else
- {
- throw new IllegalArgumentException("unknown parameter type.");
- }
-
- if (random != null)
- {
- param = new ParametersWithRandom(param, random);
- }
-
- switch (opmode)
- {
- case javax.crypto.Cipher.ENCRYPT_MODE:
- case javax.crypto.Cipher.WRAP_MODE:
- cipher.init(true, param);
- break;
- case javax.crypto.Cipher.DECRYPT_MODE:
- case javax.crypto.Cipher.UNWRAP_MODE:
- cipher.init(false, param);
- break;
- default:
- throw new InvalidParameterException("unknown opmode " + opmode + " passed to ElGamal");
- }
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- AlgorithmParameters params,
- SecureRandom random)
- throws InvalidKeyException, InvalidAlgorithmParameterException
- {
- throw new InvalidAlgorithmParameterException("can't handle parameters in ElGamal");
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- SecureRandom random)
- throws InvalidKeyException
- {
- engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
- }
-
- protected byte[] engineUpdate(
- byte[] input,
- int inputOffset,
- int inputLen)
- {
- cipher.processBytes(input, inputOffset, inputLen);
- return null;
- }
-
- protected int engineUpdate(
- byte[] input,
- int inputOffset,
- int inputLen,
- byte[] output,
- int outputOffset)
- {
- cipher.processBytes(input, inputOffset, inputLen);
- return 0;
- }
-
- protected byte[] engineDoFinal(
- byte[] input,
- int inputOffset,
- int inputLen)
- throws IllegalBlockSizeException, BadPaddingException
- {
- cipher.processBytes(input, inputOffset, inputLen);
- try
- {
- return cipher.doFinal();
- }
- catch (InvalidCipherTextException e)
- {
- throw new BadPaddingException(e.getMessage());
- }
- }
-
- protected int engineDoFinal(
- byte[] input,
- int inputOffset,
- int inputLen,
- byte[] output,
- int outputOffset)
- throws IllegalBlockSizeException, BadPaddingException
- {
- byte[] out;
-
- cipher.processBytes(input, inputOffset, inputLen);
-
- try
- {
- out = cipher.doFinal();
- }
- catch (InvalidCipherTextException e)
- {
- throw new BadPaddingException(e.getMessage());
- }
-
- for (int i = 0; i != out.length; i++)
- {
- output[outputOffset + i] = out[i];
- }
-
- return out.length;
- }
-
- /**
- * classes that inherit from us.
- */
- static public class NoPadding
- extends CipherSpi
- {
- public NoPadding()
- {
- super(new ElGamalEngine());
- }
- }
-
- static public class PKCS1v1_5Padding
- extends CipherSpi
- {
- public PKCS1v1_5Padding()
- {
- super(new PKCS1Encoding(new ElGamalEngine()));
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/AlgorithmParametersSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/AlgorithmParametersSpi.java
deleted file mode 100644
index 9d36b867..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/AlgorithmParametersSpi.java
+++ /dev/null
@@ -1,217 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.rsa;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.security.spec.AlgorithmParameterSpec;
-import java.security.spec.InvalidParameterSpecException;
-import java.security.spec.PSSParameterSpec;
-
-import org.bouncycastle.asn1.ASN1Integer;
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.pkcs.RSAESOAEPparams;
-import org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
-
-public abstract class AlgorithmParametersSpi
- extends java.security.AlgorithmParametersSpi
-{
- protected boolean isASN1FormatString(String format)
- {
- return format == null || format.equals("ASN.1");
- }
-
- protected AlgorithmParameterSpec engineGetParameterSpec(
- Class paramSpec)
- throws InvalidParameterSpecException
- {
- if (paramSpec == null)
- {
- throw new NullPointerException("argument to getParameterSpec must not be null");
- }
-
- return localEngineGetParameterSpec(paramSpec);
- }
-
- protected abstract AlgorithmParameterSpec localEngineGetParameterSpec(Class paramSpec)
- throws InvalidParameterSpecException;
-
- public static class OAEP
- extends AlgorithmParametersSpi
- {
- AlgorithmParameterSpec currentSpec;
-
- /**
- * Return the PKCS#1 ASN.1 structure RSAES-OAEP-params.
- */
- protected byte[] engineGetEncoded()
- {
- return null;
- }
-
- protected byte[] engineGetEncoded(
- String format)
- {
- if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509"))
- {
- return engineGetEncoded();
- }
-
- return null;
- }
-
- protected AlgorithmParameterSpec localEngineGetParameterSpec(
- Class paramSpec)
- throws InvalidParameterSpecException
- {
- throw new InvalidParameterSpecException("unknown parameter spec passed to OAEP parameters object.");
- }
-
- protected void engineInit(
- AlgorithmParameterSpec paramSpec)
- throws InvalidParameterSpecException
- {
- this.currentSpec = paramSpec;
- }
-
- protected void engineInit(
- byte[] params)
- throws IOException
- {
- try
- {
- RSAESOAEPparams oaepP = RSAESOAEPparams.getInstance(params);
-
- throw new IOException("Operation not supported");
- }
- catch (ClassCastException e)
- {
- throw new IOException("Not a valid OAEP Parameter encoding.");
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- throw new IOException("Not a valid OAEP Parameter encoding.");
- }
- }
-
- protected void engineInit(
- byte[] params,
- String format)
- throws IOException
- {
- if (format.equalsIgnoreCase("X.509")
- || format.equalsIgnoreCase("ASN.1"))
- {
- engineInit(params);
- }
- else
- {
- throw new IOException("Unknown parameter format " + format);
- }
- }
-
- protected String engineToString()
- {
- return "OAEP Parameters";
- }
- }
-
- public static class PSS
- extends AlgorithmParametersSpi
- {
- PSSParameterSpec currentSpec;
-
- /**
- * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params.
- */
- protected byte[] engineGetEncoded()
- throws IOException
- {
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- DEROutputStream dOut = new DEROutputStream(bOut);
- PSSParameterSpec pssSpec = (PSSParameterSpec)currentSpec;
- RSASSAPSSparams pssP = new RSASSAPSSparams(RSASSAPSSparams.DEFAULT_HASH_ALGORITHM, RSASSAPSSparams.DEFAULT_MASK_GEN_FUNCTION, new ASN1Integer(pssSpec.getSaltLength()), RSASSAPSSparams.DEFAULT_TRAILER_FIELD);
-
- dOut.writeObject(pssP);
- dOut.close();
-
- return bOut.toByteArray();
- }
-
- protected byte[] engineGetEncoded(
- String format)
- throws IOException
- {
- if (format.equalsIgnoreCase("X.509")
- || format.equalsIgnoreCase("ASN.1"))
- {
- return engineGetEncoded();
- }
-
- return null;
- }
-
- protected AlgorithmParameterSpec localEngineGetParameterSpec(
- Class paramSpec)
- throws InvalidParameterSpecException
- {
- if (paramSpec == PSSParameterSpec.class && currentSpec != null)
- {
- return currentSpec;
- }
-
- throw new InvalidParameterSpecException("unknown parameter spec passed to PSS parameters object.");
- }
-
- protected void engineInit(
- AlgorithmParameterSpec paramSpec)
- throws InvalidParameterSpecException
- {
- if (!(paramSpec instanceof PSSParameterSpec))
- {
- throw new InvalidParameterSpecException("PSSParameterSpec required to initialise an PSS algorithm parameters object");
- }
-
- this.currentSpec = (PSSParameterSpec)paramSpec;
- }
-
- protected void engineInit(
- byte[] params)
- throws IOException
- {
- try
- {
- RSASSAPSSparams pssP = RSASSAPSSparams.getInstance(params);
-
- currentSpec = new PSSParameterSpec(
- pssP.getSaltLength().intValue());
- }
- catch (ClassCastException e)
- {
- throw new IOException("Not a valid PSS Parameter encoding.");
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- throw new IOException("Not a valid PSS Parameter encoding.");
- }
- }
-
- protected void engineInit(
- byte[] params,
- String format)
- throws IOException
- {
- if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509"))
- {
- engineInit(params);
- }
- else
- {
- throw new IOException("Unknown parameter format " + format);
- }
- }
-
- protected String engineToString()
- {
- return "PSS Parameters";
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/CipherSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/CipherSpi.java
deleted file mode 100644
index 312730f6..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/CipherSpi.java
+++ /dev/null
@@ -1,509 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.rsa;
-
-import java.io.ByteArrayOutputStream;
-import java.security.AlgorithmParameters;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.InvalidParameterException;
-import java.security.Key;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-import java.security.interfaces.RSAPrivateKey;
-import java.security.interfaces.RSAPublicKey;
-import java.security.spec.AlgorithmParameterSpec;
-
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-
-import org.bouncycastle.crypto.AsymmetricBlockCipher;
-import org.bouncycastle.crypto.CipherParameters;
-import org.bouncycastle.crypto.InvalidCipherTextException;
-import org.bouncycastle.crypto.digests.MD5Digest;
-import org.bouncycastle.crypto.digests.SHA224Digest;
-import org.bouncycastle.crypto.digests.SHA256Digest;
-import org.bouncycastle.crypto.digests.SHA384Digest;
-import org.bouncycastle.crypto.digests.SHA512Digest;
-import org.bouncycastle.crypto.encodings.ISO9796d1Encoding;
-import org.bouncycastle.crypto.encodings.OAEPEncoding;
-import org.bouncycastle.crypto.encodings.PKCS1Encoding;
-import org.bouncycastle.crypto.engines.RSABlindedEngine;
-import org.bouncycastle.crypto.params.ParametersWithRandom;
-import org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.util.Strings;
-
-public class CipherSpi
- extends BaseCipherSpi
-{
- private AsymmetricBlockCipher cipher;
- private AlgorithmParameterSpec paramSpec;
- private AlgorithmParameters engineParams;
- private boolean publicKeyOnly = false;
- private boolean privateKeyOnly = false;
- private ByteArrayOutputStream bOut = new ByteArrayOutputStream();
-
- public CipherSpi(
- AsymmetricBlockCipher engine)
- {
- cipher = engine;
- }
-
- public CipherSpi(
- boolean publicKeyOnly,
- boolean privateKeyOnly,
- AsymmetricBlockCipher engine)
- {
- this.publicKeyOnly = publicKeyOnly;
- this.privateKeyOnly = privateKeyOnly;
- cipher = engine;
- }
-
- protected int engineGetBlockSize()
- {
- try
- {
- return cipher.getInputBlockSize();
- }
- catch (NullPointerException e)
- {
- throw new IllegalStateException("RSA Cipher not initialised");
- }
- }
-
- protected int engineGetKeySize(
- Key key)
- {
- if (key instanceof RSAPrivateKey)
- {
- RSAPrivateKey k = (RSAPrivateKey)key;
-
- return k.getModulus().bitLength();
- }
- else if (key instanceof RSAPublicKey)
- {
- RSAPublicKey k = (RSAPublicKey)key;
-
- return k.getModulus().bitLength();
- }
-
- throw new IllegalArgumentException("not an RSA key!");
- }
-
- protected int engineGetOutputSize(
- int inputLen)
- {
- try
- {
- return cipher.getOutputBlockSize();
- }
- catch (NullPointerException e)
- {
- throw new IllegalStateException("RSA Cipher not initialised");
- }
- }
-
- protected AlgorithmParameters engineGetParameters()
- {
- if (engineParams == null)
- {
- if (paramSpec != null)
- {
- try
- {
- engineParams = AlgorithmParameters.getInstance("OAEP", BouncyCastleProvider.PROVIDER_NAME);
- engineParams.init(paramSpec);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e.toString());
- }
- }
- }
-
- return engineParams;
- }
-
- protected void engineSetMode(
- String mode)
- throws NoSuchAlgorithmException
- {
- String md = Strings.toUpperCase(mode);
-
- if (md.equals("NONE") || md.equals("ECB"))
- {
- return;
- }
-
- if (md.equals("1"))
- {
- privateKeyOnly = true;
- publicKeyOnly = false;
- return;
- }
- else if (md.equals("2"))
- {
- privateKeyOnly = false;
- publicKeyOnly = true;
- return;
- }
-
- throw new NoSuchAlgorithmException("can't support mode " + mode);
- }
-
- protected void engineSetPadding(
- String padding)
- throws NoSuchPaddingException
- {
- String pad = Strings.toUpperCase(padding);
-
- if (pad.equals("NOPADDING"))
- {
- cipher = new RSABlindedEngine();
- }
- else if (pad.equals("PKCS1PADDING"))
- {
- cipher = new PKCS1Encoding(new RSABlindedEngine());
- }
- else if (pad.equals("ISO9796-1PADDING"))
- {
- cipher = new ISO9796d1Encoding(new RSABlindedEngine());
- }
- else if (pad.equals("OAEPPADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine());
- }
- else if (pad.equals("OAEPWITHSHA1ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine());
- }
- else if (pad.equals("OAEPWITHSHA224ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine(), new SHA224Digest());
- }
- else if (pad.equals("OAEPWITHSHA256ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine(), new SHA256Digest());
- }
- else if (pad.equals("OAEPWITHSHA384ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine(), new SHA384Digest());
- }
- else if (pad.equals("OAEPWITHSHA512ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine(), new SHA512Digest());
- }
- else if (pad.equals("OAEPWITHMD5ANDMGF1PADDING"))
- {
- cipher = new OAEPEncoding(new RSABlindedEngine(), new MD5Digest());
- }
- else
- {
- throw new NoSuchPaddingException(padding + " unavailable with RSA.");
- }
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- AlgorithmParameterSpec params,
- SecureRandom random)
- throws InvalidKeyException, InvalidAlgorithmParameterException
- {
- CipherParameters param;
-
- if (params == null)
- {
- if (key instanceof RSAPublicKey)
- {
- if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE)
- {
- throw new InvalidKeyException(
- "mode 1 requires RSAPrivateKey");
- }
-
- param = RSAUtil.generatePublicKeyParameter((RSAPublicKey)key);
- }
- else if (key instanceof RSAPrivateKey)
- {
- if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE)
- {
- throw new InvalidKeyException(
- "mode 2 requires RSAPublicKey");
- }
-
- param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)key);
- }
- else
- {
- throw new InvalidKeyException("unknown key type passed to RSA");
- }
- }
- else
- {
- throw new IllegalArgumentException("unknown parameter type.");
- }
-
- if (!(cipher instanceof RSABlindedEngine))
- {
- if (random != null)
- {
- param = new ParametersWithRandom(param, random);
- }
- else
- {
- param = new ParametersWithRandom(param, new SecureRandom());
- }
- }
-
- switch (opmode)
- {
- case javax.crypto.Cipher.ENCRYPT_MODE:
- case javax.crypto.Cipher.WRAP_MODE:
- cipher.init(true, param);
- break;
- case javax.crypto.Cipher.DECRYPT_MODE:
- case javax.crypto.Cipher.UNWRAP_MODE:
- cipher.init(false, param);
- break;
- default:
- throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
- }
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- AlgorithmParameters params,
- SecureRandom random)
- throws InvalidKeyException, InvalidAlgorithmParameterException
- {
- AlgorithmParameterSpec paramSpec = null;
-
- if (params != null)
- {
- throw new InvalidAlgorithmParameterException("cannot recognise parameters.");
- }
-
- engineParams = params;
- engineInit(opmode, key, paramSpec, random);
- }
-
- protected void engineInit(
- int opmode,
- Key key,
- SecureRandom random)
- throws InvalidKeyException
- {
- try
- {
- engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
- }
- catch (InvalidAlgorithmParameterException e)
- {
- // this shouldn't happen
- throw new InvalidKeyException("Eeeek! " + e.toString());
- }
- }
-
- protected byte[] engineUpdate(
- byte[] input,
- int inputOffset,
- int inputLen)
- {
- bOut.write(input, inputOffset, inputLen);
-
- if (cipher instanceof RSABlindedEngine)
- {
- if (bOut.size() > cipher.getInputBlockSize() + 1)
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
- else
- {
- if (bOut.size() > cipher.getInputBlockSize())
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
-
- return null;
- }
-
- protected int engineUpdate(
- byte[] input,
- int inputOffset,
- int inputLen,
- byte[] output,
- int outputOffset)
- {
- bOut.write(input, inputOffset, inputLen);
-
- if (cipher instanceof RSABlindedEngine)
- {
- if (bOut.size() > cipher.getInputBlockSize() + 1)
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
- else
- {
- if (bOut.size() > cipher.getInputBlockSize())
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
-
- return 0;
- }
-
- protected byte[] engineDoFinal(
- byte[] input,
- int inputOffset,
- int inputLen)
- throws IllegalBlockSizeException, BadPaddingException
- {
- if (input != null)
- {
- bOut.write(input, inputOffset, inputLen);
- }
-
- if (cipher instanceof RSABlindedEngine)
- {
- if (bOut.size() > cipher.getInputBlockSize() + 1)
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
- else
- {
- if (bOut.size() > cipher.getInputBlockSize())
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
-
- try
- {
- byte[] bytes = bOut.toByteArray();
-
- bOut.reset();
-
- return cipher.processBlock(bytes, 0, bytes.length);
- }
- catch (InvalidCipherTextException e)
- {
- throw new BadPaddingException(e.getMessage());
- }
- }
-
- protected int engineDoFinal(
- byte[] input,
- int inputOffset,
- int inputLen,
- byte[] output,
- int outputOffset)
- throws IllegalBlockSizeException, BadPaddingException
- {
- if (input != null)
- {
- bOut.write(input, inputOffset, inputLen);
- }
-
- if (cipher instanceof RSABlindedEngine)
- {
- if (bOut.size() > cipher.getInputBlockSize() + 1)
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
- else
- {
- if (bOut.size() > cipher.getInputBlockSize())
- {
- throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
- }
- }
-
- byte[] out;
-
- try
- {
- byte[] bytes = bOut.toByteArray();
- bOut.reset();
-
- out = cipher.processBlock(bytes, 0, bytes.length);
- }
- catch (InvalidCipherTextException e)
- {
- throw new BadPaddingException(e.getMessage());
- }
-
- for (int i = 0; i != out.length; i++)
- {
- output[outputOffset + i] = out[i];
- }
-
- return out.length;
- }
-
- /**
- * classes that inherit from us.
- */
-
- static public class NoPadding
- extends CipherSpi
- {
- public NoPadding()
- {
- super(new RSABlindedEngine());
- }
- }
-
- static public class PKCS1v1_5Padding
- extends CipherSpi
- {
- public PKCS1v1_5Padding()
- {
- super(new PKCS1Encoding(new RSABlindedEngine()));
- }
- }
-
- static public class PKCS1v1_5Padding_PrivateOnly
- extends CipherSpi
- {
- public PKCS1v1_5Padding_PrivateOnly()
- {
- super(false, true, new PKCS1Encoding(new RSABlindedEngine()));
- }
- }
-
- static public class PKCS1v1_5Padding_PublicOnly
- extends CipherSpi
- {
- public PKCS1v1_5Padding_PublicOnly()
- {
- super(true, false, new PKCS1Encoding(new RSABlindedEngine()));
- }
- }
-
- static public class OAEPPadding
- extends CipherSpi
- {
- public OAEPPadding()
- {
- super(new OAEPEncoding(new RSABlindedEngine()));
- }
- }
-
- static public class ISO9796d1Padding
- extends CipherSpi
- {
- public ISO9796d1Padding()
- {
- super(new ISO9796d1Encoding(new RSABlindedEngine()));
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/PSSSignatureSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/PSSSignatureSpi.java
deleted file mode 100644
index be337fd5..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/rsa/PSSSignatureSpi.java
+++ /dev/null
@@ -1,405 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.rsa;
-
-import java.io.ByteArrayOutputStream;
-import java.security.AlgorithmParameters;
-import java.security.InvalidKeyException;
-import java.security.InvalidParameterException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.SecureRandom;
-import java.security.Signature;
-import java.security.SignatureException;
-import java.security.interfaces.RSAPrivateKey;
-import java.security.interfaces.RSAPublicKey;
-import java.security.spec.AlgorithmParameterSpec;
-import java.security.spec.PSSParameterSpec;
-
-import org.bouncycastle.crypto.AsymmetricBlockCipher;
-import org.bouncycastle.crypto.CryptoException;
-import org.bouncycastle.crypto.Digest;
-import org.bouncycastle.crypto.digests.SHA1Digest;
-import org.bouncycastle.crypto.digests.SHA224Digest;
-import org.bouncycastle.crypto.digests.SHA256Digest;
-import org.bouncycastle.crypto.digests.SHA384Digest;
-import org.bouncycastle.crypto.digests.SHA512Digest;
-import org.bouncycastle.crypto.engines.RSABlindedEngine;
-import org.bouncycastle.crypto.params.ParametersWithRandom;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-
-public class PSSSignatureSpi
- extends Signature
-{
- private AlgorithmParameters engineParams;
- private PSSParameterSpec paramSpec;
- private AsymmetricBlockCipher signer;
- private Digest contentDigest;
- private Digest mgfDigest;
- private int saltLength;
- private byte trailer;
- private boolean isRaw;
-
- private org.bouncycastle.crypto.signers.PSSSigner pss;
-
- private byte getTrailer(
- int trailerField)
- {
- if (trailerField == 1)
- {
- return org.bouncycastle.crypto.signers.PSSSigner.TRAILER_IMPLICIT;
- }
-
- throw new IllegalArgumentException("unknown trailer field");
- }
-
- private void setupContentDigest()
- {
- if (isRaw)
- {
- this.contentDigest = new NullPssDigest(mgfDigest);
- }
- else
- {
- this.contentDigest = mgfDigest;
- }
- }
-
- protected PSSSignatureSpi(
- String name,
- AsymmetricBlockCipher signer,
- Digest digest)
- {
- super(name);
-
- this.signer = signer;
- this.mgfDigest = digest;
-
- if (digest != null)
- {
- this.saltLength = digest.getDigestSize();
- }
- else
- {
- this.saltLength = 20;
- }
-
- if (paramSpec != null)
- {
- this.saltLength = paramSpec.getSaltLength();
- }
- this.isRaw = false;
-
- setupContentDigest();
- }
-
- // care - this constructor is actually used by outside organisations
- protected PSSSignatureSpi(
- String name,
- AsymmetricBlockCipher signer,
- Digest digest,
- boolean isRaw)
- {
- super(name);
-
- this.signer = signer;
- this.mgfDigest = digest;
-
- if (digest != null)
- {
- this.saltLength = digest.getDigestSize();
- }
- else
- {
- this.saltLength = 20;
- }
-
- if (paramSpec != null)
- {
- this.saltLength = paramSpec.getSaltLength();
- }
-
- this.isRaw = isRaw;
-
- setupContentDigest();
- }
-
- protected void engineInitVerify(
- PublicKey publicKey)
- throws InvalidKeyException
- {
- if (!(publicKey instanceof RSAPublicKey))
- {
- throw new InvalidKeyException("Supplied key is not a RSAPublicKey instance");
- }
-
- pss = new org.bouncycastle.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength);
- pss.init(false,
- RSAUtil.generatePublicKeyParameter((RSAPublicKey)publicKey));
- }
-
- protected void engineInitSign(
- PrivateKey privateKey,
- SecureRandom random)
- throws InvalidKeyException
- {
- if (!(privateKey instanceof RSAPrivateKey))
- {
- throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
- }
-
- pss = new org.bouncycastle.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength);
- pss.init(true, new ParametersWithRandom(RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey), random));
- }
-
- protected void engineInitSign(
- PrivateKey privateKey)
- throws InvalidKeyException
- {
- if (!(privateKey instanceof RSAPrivateKey))
- {
- throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
- }
-
- pss = new org.bouncycastle.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength);
- pss.init(true, RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey));
- }
-
- protected void engineUpdate(
- byte b)
- throws SignatureException
- {
- pss.update(b);
- }
-
- protected void engineUpdate(
- byte[] b,
- int off,
- int len)
- throws SignatureException
- {
- pss.update(b, off, len);
- }
-
- protected byte[] engineSign()
- throws SignatureException
- {
- try
- {
- return pss.generateSignature();
- }
- catch (CryptoException e)
- {
- throw new SignatureException(e.getMessage());
- }
- }
-
- protected boolean engineVerify(
- byte[] sigBytes)
- throws SignatureException
- {
- return pss.verifySignature(sigBytes);
- }
-
- protected void engineSetParameter(
- AlgorithmParameterSpec params)
- throws InvalidParameterException
- {
- if (params instanceof PSSParameterSpec)
- {
- PSSParameterSpec newParamSpec = (PSSParameterSpec)params;
-
- this.engineParams = null;
- this.paramSpec = newParamSpec;
- this.saltLength = paramSpec.getSaltLength();
-
- if (mgfDigest == null)
- {
- switch (saltLength)
- {
- case 20:
- this.mgfDigest = new SHA1Digest();
- break;
- case 28:
- this.mgfDigest = new SHA224Digest();
- break;
- case 32:
- this.mgfDigest = new SHA256Digest();
- break;
- case 48:
- this.mgfDigest = new SHA384Digest();
- break;
- case 64:
- this.mgfDigest = new SHA512Digest();
- break;
- }
- setupContentDigest();
- }
- }
- else
- {
- throw new InvalidParameterException("Only PSSParameterSpec supported");
- }
- }
-
- protected AlgorithmParameters engineGetParameters()
- {
- if (engineParams == null)
- {
- try
- {
- engineParams = AlgorithmParameters.getInstance("PSS", BouncyCastleProvider.PROVIDER_NAME);
- engineParams.init(new PSSParameterSpec(saltLength));
- }
- catch (Exception e)
- {
- throw new RuntimeException(e.toString());
- }
- }
-
- return engineParams;
- }
-
- /**
- * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
- */
- protected void engineSetParameter(
- String param,
- Object value)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- protected Object engineGetParameter(
- String param)
- {
- throw new UnsupportedOperationException("engineGetParameter unsupported");
- }
-
- static public class nonePSS
- extends PSSSignatureSpi
- {
- public nonePSS()
- {
- super("NONEwithRSAandMGF1", new RSABlindedEngine(), null, true);
- }
- }
-
- static public class PSSwithRSA
- extends PSSSignatureSpi
- {
- public PSSwithRSA()
- {
- super("SHA1withRSAandMGF1", new RSABlindedEngine(), null);
- }
- }
-
- static public class SHA1withRSA
- extends PSSSignatureSpi
- {
- public SHA1withRSA()
- {
- super("SHA1withRSAandMGF1", new RSABlindedEngine(), new SHA1Digest());
- }
- }
-
- static public class SHA224withRSA
- extends PSSSignatureSpi
- {
- public SHA224withRSA()
- {
- super("SHA224withRSAandMGF1", new RSABlindedEngine(), new SHA224Digest());
- }
- }
-
- static public class SHA256withRSA
- extends PSSSignatureSpi
- {
- public SHA256withRSA()
- {
- super("SHA256withRSAandMGF1", new RSABlindedEngine(), new SHA256Digest());
- }
- }
-
- static public class SHA384withRSA
- extends PSSSignatureSpi
- {
- public SHA384withRSA()
- {
- super("SHA384withRSAandMGF1", new RSABlindedEngine(), new SHA384Digest());
- }
- }
-
- static public class SHA512withRSA
- extends PSSSignatureSpi
- {
- public SHA512withRSA()
- {
- super("SHA512withRSAandMGF1", new RSABlindedEngine(), new SHA512Digest());
- }
- }
-
- private class NullPssDigest
- implements Digest
- {
- private ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- private Digest baseDigest;
- private boolean oddTime = true;
-
- public NullPssDigest(Digest mgfDigest)
- {
- this.baseDigest = mgfDigest;
- }
-
- public String getAlgorithmName()
- {
- return "NULL";
- }
-
- public int getDigestSize()
- {
- return baseDigest.getDigestSize();
- }
-
- public void update(byte in)
- {
- bOut.write(in);
- }
-
- public void update(byte[] in, int inOff, int len)
- {
- bOut.write(in, inOff, len);
- }
-
- public int doFinal(byte[] out, int outOff)
- {
- byte[] res = bOut.toByteArray();
-
- if (oddTime)
- {
- System.arraycopy(res, 0, out, outOff, res.length);
- }
- else
- {
- baseDigest.update(res, 0, res.length);
-
- baseDigest.doFinal(out, outOff);
- }
-
- reset();
-
- oddTime = !oddTime;
-
- return res.length;
- }
-
- public void reset()
- {
- bOut.reset();
- baseDigest.reset();
- }
-
- public int getByteLength()
- {
- return 0;
- }
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/DSABase.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/DSABase.java
deleted file mode 100644
index 73320c0e..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/DSABase.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.util;
-
-import java.math.BigInteger;
-import java.security.InvalidKeyException;
-import java.security.PrivateKey;
-import java.security.SecureRandom;
-import java.security.Signature;
-import java.security.SignatureException;
-import java.security.spec.AlgorithmParameterSpec;
-
-import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
-import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
-import org.bouncycastle.crypto.DSA;
-import org.bouncycastle.crypto.Digest;
-
-public abstract class DSABase
- extends Signature
- implements PKCSObjectIdentifiers, X509ObjectIdentifiers
-{
- protected Digest digest;
- protected DSA signer;
- protected DSAEncoder encoder;
-
- protected DSABase(
- String name,
- Digest digest,
- DSA signer,
- DSAEncoder encoder)
- {
- super(name);
-
- this.digest = digest;
- this.signer = signer;
- this.encoder = encoder;
- }
-
- protected void engineInitSign(
- PrivateKey privateKey)
- throws InvalidKeyException
- {
- doEngineInitSign(privateKey, appRandom);
- }
-
- protected void engineUpdate(
- byte b)
- throws SignatureException
- {
- digest.update(b);
- }
-
- protected void engineUpdate(
- byte[] b,
- int off,
- int len)
- throws SignatureException
- {
- digest.update(b, off, len);
- }
-
- protected byte[] engineSign()
- throws SignatureException
- {
- byte[] hash = new byte[digest.getDigestSize()];
-
- digest.doFinal(hash, 0);
-
- try
- {
- BigInteger[] sig = signer.generateSignature(hash);
-
- return encoder.encode(sig[0], sig[1]);
- }
- catch (Exception e)
- {
- throw new SignatureException(e.toString());
- }
- }
-
- protected boolean engineVerify(
- byte[] sigBytes)
- throws SignatureException
- {
- byte[] hash = new byte[digest.getDigestSize()];
-
- digest.doFinal(hash, 0);
-
- BigInteger[] sig;
-
- try
- {
- sig = encoder.decode(sigBytes);
- }
- catch (Exception e)
- {
- throw new SignatureException("error decoding signature bytes.");
- }
-
- return signer.verifySignature(hash, sig[0], sig[1]);
- }
-
- protected void engineSetParameter(
- AlgorithmParameterSpec params)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- /**
- * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
- */
- protected void engineSetParameter(
- String param,
- Object value)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- /**
- * @deprecated
- */
- protected Object engineGetParameter(
- String param)
- {
- throw new UnsupportedOperationException("engineSetParameter unsupported");
- }
-
- protected abstract void doEngineInitSign(PrivateKey privateKey, SecureRandom random)
- throws InvalidKeyException;
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/ECUtil.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/ECUtil.java
deleted file mode 100644
index c5ebb771..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/asymmetric/util/ECUtil.java
+++ /dev/null
@@ -1,220 +0,0 @@
-package org.bouncycastle.jcajce.provider.asymmetric.util;
-
-import java.security.InvalidKeyException;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
-import org.bouncycastle.asn1.nist.NISTNamedCurves;
-import org.bouncycastle.asn1.sec.SECNamedCurves;
-import org.bouncycastle.asn1.teletrust.TeleTrusTNamedCurves;
-import org.bouncycastle.asn1.x9.X962NamedCurves;
-import org.bouncycastle.asn1.x9.X9ECParameters;
-import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
-import org.bouncycastle.crypto.params.ECDomainParameters;
-import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
-import org.bouncycastle.crypto.params.ECPublicKeyParameters;
-import org.bouncycastle.jce.interfaces.ECPrivateKey;
-import org.bouncycastle.jce.interfaces.ECPublicKey;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
-import org.bouncycastle.jce.spec.ECParameterSpec;
-
-/**
- * utility class for converting jce/jca ECDSA, ECDH, and ECDHC
- * objects into their org.bouncycastle.crypto counterparts.
- */
-public class ECUtil
-{
- /**
- * Returns a sorted array of middle terms of the reduction polynomial.
- * @param k The unsorted array of middle terms of the reduction polynomial
- * of length 1 or 3.
- * @return the sorted array of middle terms of the reduction polynomial.
- * This array always has length 3.
- */
- static int[] convertMidTerms(
- int[] k)
- {
- int[] res = new int[3];
-
- if (k.length == 1)
- {
- res[0] = k[0];
- }
- else
- {
- if (k.length != 3)
- {
- throw new IllegalArgumentException("Only Trinomials and pentanomials supported");
- }
-
- if (k[0] < k[1] && k[0] < k[2])
- {
- res[0] = k[0];
- if (k[1] < k[2])
- {
- res[1] = k[1];
- res[2] = k[2];
- }
- else
- {
- res[1] = k[2];
- res[2] = k[1];
- }
- }
- else if (k[1] < k[2])
- {
- res[0] = k[1];
- if (k[0] < k[2])
- {
- res[1] = k[0];
- res[2] = k[2];
- }
- else
- {
- res[1] = k[2];
- res[2] = k[0];
- }
- }
- else
- {
- res[0] = k[2];
- if (k[0] < k[1])
- {
- res[1] = k[0];
- res[2] = k[1];
- }
- else
- {
- res[1] = k[1];
- res[2] = k[0];
- }
- }
- }
-
- return res;
- }
-
- public static AsymmetricKeyParameter generatePublicKeyParameter(
- PublicKey key)
- throws InvalidKeyException
- {
- if (key instanceof ECPublicKey)
- {
- ECPublicKey k = (ECPublicKey)key;
- ECParameterSpec s = k.getParameters();
-
- if (s == null)
- {
- s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
-
- return new ECPublicKeyParameters(
- ((BCECPublicKey)k).engineGetQ(),
- new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
- }
- else
- {
- return new ECPublicKeyParameters(
- k.getQ(),
- new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
- }
- }
-
- throw new InvalidKeyException("cannot identify EC public key.");
- }
-
- public static AsymmetricKeyParameter generatePrivateKeyParameter(
- PrivateKey key)
- throws InvalidKeyException
- {
- if (key instanceof ECPrivateKey)
- {
- ECPrivateKey k = (ECPrivateKey)key;
- ECParameterSpec s = k.getParameters();
-
- if (s == null)
- {
- s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
- }
-
- return new ECPrivateKeyParameters(
- k.getD(),
- new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
- }
-
- throw new InvalidKeyException("can't identify EC private key.");
- }
-
- public static ASN1ObjectIdentifier getNamedCurveOid(
- String name)
- {
- ASN1ObjectIdentifier oid = X962NamedCurves.getOID(name);
-
- if (oid == null)
- {
- oid = SECNamedCurves.getOID(name);
- if (oid == null)
- {
- oid = NISTNamedCurves.getOID(name);
- }
- if (oid == null)
- {
- oid = TeleTrusTNamedCurves.getOID(name);
- }
- if (oid == null)
- {
- oid = ECGOST3410NamedCurves.getOID(name);
- }
- }
-
- return oid;
- }
-
- public static X9ECParameters getNamedCurveByOid(
- ASN1ObjectIdentifier oid)
- {
- X9ECParameters params = X962NamedCurves.getByOID(oid);
-
- if (params == null)
- {
- params = SECNamedCurves.getByOID(oid);
- if (params == null)
- {
- params = NISTNamedCurves.getByOID(oid);
- }
- if (params == null)
- {
- params = TeleTrusTNamedCurves.getByOID(oid);
- }
- }
-
- return params;
- }
-
- public static String getCurveName(
- ASN1ObjectIdentifier oid)
- {
- String name = X962NamedCurves.getName(oid);
-
- if (name == null)
- {
- name = SECNamedCurves.getName(oid);
- if (name == null)
- {
- name = NISTNamedCurves.getName(oid);
- }
- if (name == null)
- {
- name = TeleTrusTNamedCurves.getName(oid);
- }
- if (name == null)
- {
- name = ECGOST3410NamedCurves.getName(oid);
- }
- }
-
- return name;
- }
-}
diff --git a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/keystore/pkcs12/PKCS12KeyStoreSpi.java b/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/keystore/pkcs12/PKCS12KeyStoreSpi.java
deleted file mode 100644
index 44ad125d..00000000
--- a/prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/keystore/pkcs12/PKCS12KeyStoreSpi.java
+++ /dev/null
@@ -1,1637 +0,0 @@
-package org.bouncycastle.jcajce.provider.keystore.pkcs12;
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.Key;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.KeyStoreSpi;
-import java.security.NoSuchAlgorithmException;
-import java.security.Principal;
-import java.security.PrivateKey;
-import java.security.Provider;
-import java.security.PublicKey;
-import java.security.SecureRandom;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateEncodingException;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.util.Date;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
-
-import javax.crypto.Cipher;
-import javax.crypto.Mac;
-import javax.crypto.SecretKey;
-import javax.crypto.SecretKeyFactory;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.PBEKeySpec;
-import javax.crypto.spec.PBEParameterSpec;
-
-import org.bouncycastle.asn1.ASN1Encodable;
-import org.bouncycastle.asn1.ASN1EncodableVector;
-import org.bouncycastle.asn1.ASN1Encoding;
-import org.bouncycastle.asn1.ASN1InputStream;
-import org.bouncycastle.asn1.ASN1ObjectIdentifier;
-import org.bouncycastle.asn1.ASN1OctetString;
-import org.bouncycastle.asn1.ASN1Primitive;
-import org.bouncycastle.asn1.ASN1Sequence;
-import org.bouncycastle.asn1.ASN1Set;
-import org.bouncycastle.asn1.BEROctetString;
-import org.bouncycastle.asn1.BEROutputStream;
-import org.bouncycastle.asn1.DERBMPString;
-import org.bouncycastle.asn1.DERNull;
-import org.bouncycastle.asn1.DEROctetString;
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.DERSequence;
-import org.bouncycastle.asn1.DERSet;
-import org.bouncycastle.asn1.pkcs.AuthenticatedSafe;
-import org.bouncycastle.asn1.pkcs.CertBag;
-import org.bouncycastle.asn1.pkcs.ContentInfo;
-import org.bouncycastle.asn1.pkcs.EncryptedData;
-import org.bouncycastle.asn1.pkcs.MacData;
-import org.bouncycastle.asn1.pkcs.PBES2Parameters;
-import org.bouncycastle.asn1.pkcs.PBKDF2Params;
-import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
-import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
-import org.bouncycastle.asn1.pkcs.Pfx;
-import org.bouncycastle.asn1.pkcs.SafeBag;
-import org.bouncycastle.asn1.util.ASN1Dump;
-import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
-import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
-import org.bouncycastle.asn1.x509.DigestInfo;
-import org.bouncycastle.asn1.x509.Extension;
-import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
-import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
-import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
-import org.bouncycastle.crypto.Digest;
-import org.bouncycastle.crypto.digests.SHA1Digest;
-import org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey;
-import org.bouncycastle.jcajce.provider.util.SecretKeyUtil;
-import org.bouncycastle.jce.interfaces.BCKeyStore;
-import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.util.Arrays;
-import org.bouncycastle.util.Strings;
-import org.bouncycastle.util.encoders.Hex;
-
-public class PKCS12KeyStoreSpi
- extends KeyStoreSpi
- implements PKCSObjectIdentifiers, X509ObjectIdentifiers, BCKeyStore
-{
- private static final int SALT_SIZE = 20;
- private static final int MIN_ITERATIONS = 1024;
-
- private static final Provider bcProvider = new BouncyCastleProvider();
-
- private IgnoresCaseHashtable keys = new IgnoresCaseHashtable();
- private Hashtable localIds = new Hashtable();
- private IgnoresCaseHashtable certs = new IgnoresCaseHashtable();
- private Hashtable chainCerts = new Hashtable();
- private Hashtable keyCerts = new Hashtable();
-
- //
- // generic object types
- //
- static final int NULL = 0;
- static final int CERTIFICATE = 1;
- static final int KEY = 2;
- static final int SECRET = 3;
- static final int SEALED = 4;
-
- //
- // key types
- //
- static final int KEY_PRIVATE = 0;
- static final int KEY_PUBLIC = 1;
- static final int KEY_SECRET = 2;
-
- protected SecureRandom random = new SecureRandom();
-
- // use of final causes problems with JDK 1.2 compiler
- private CertificateFactory certFact;
- private ASN1ObjectIdentifier keyAlgorithm;
- private ASN1ObjectIdentifier certAlgorithm;
-
- private class CertId
- {
- byte[] id;
-
- CertId(
- PublicKey key)
- {
- this.id = createSubjectKeyId(key).getKeyIdentifier();
- }
-
- CertId(
- byte[] id)
- {
- this.id = id;
- }
-
- public int hashCode()
- {
- return Arrays.hashCode(id);
- }
-
- public boolean equals(
- Object o)
- {
- if (o == this)
- {
- return true;
- }
-
- if (!(o instanceof CertId))
- {
- return false;
- }
-
- CertId cId = (CertId)o;
-
- return Arrays.areEqual(id, cId.id);
- }
- }
-
- public PKCS12KeyStoreSpi(
- Provider provider,
- ASN1ObjectIdentifier keyAlgorithm,
- ASN1ObjectIdentifier certAlgorithm)
- {
- this.keyAlgorithm = keyAlgorithm;
- this.certAlgorithm = certAlgorithm;
-
- try
- {
- if (provider != null)
- {
- certFact = CertificateFactory.getInstance("X.509", provider);
- }
- else
- {
- certFact = CertificateFactory.getInstance("X.509");
- }
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("can't create cert factory - " + e.toString());
- }
- }
-
- private SubjectKeyIdentifier createSubjectKeyId(
- PublicKey pubKey)
- {
- try
- {
- SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
- (ASN1Sequence)ASN1Primitive.fromByteArray(pubKey.getEncoded()));
-
- return new SubjectKeyIdentifier(getDigest(info));
- }
- catch (Exception e)
- {
- throw new RuntimeException("error creating key");
- }
- }
-
- private static byte[] getDigest(SubjectPublicKeyInfo spki)
- {
- Digest digest = new SHA1Digest();
- byte[] resBuf = new byte[digest.getDigestSize()];
-
- byte[] bytes = spki.getPublicKeyData().getBytes();
- digest.update(bytes, 0, bytes.length);
- digest.doFinal(resBuf, 0);
- return resBuf;
- }
-
- public void setRandom(
- SecureRandom rand)
- {
- this.random = rand;
- }
-
- public Enumeration engineAliases()
- {
- Hashtable tab = new Hashtable();
-
- Enumeration e = certs.keys();
- while (e.hasMoreElements())
- {
- tab.put(e.nextElement(), "cert");
- }
-
- e = keys.keys();
- while (e.hasMoreElements())
- {
- String a = (String)e.nextElement();
- if (tab.get(a) == null)
- {
- tab.put(a, "key");
- }
- }
-
- return tab.keys();
- }
-
- public boolean engineContainsAlias(
- String alias)
- {
- return (certs.get(alias) != null || keys.get(alias) != null);
- }
-
- /**
- * this is not quite complete - we should follow up on the chain, a bit
- * tricky if a certificate appears in more than one chain...
- */
- public void engineDeleteEntry(
- String alias)
- throws KeyStoreException
- {
- Key k = (Key)keys.remove(alias);
-
- Certificate c = (Certificate)certs.remove(alias);
-
- if (c != null)
- {
- chainCerts.remove(new CertId(c.getPublicKey()));
- }
-
- if (k != null)
- {
- String id = (String)localIds.remove(alias);
- if (id != null)
- {
- c = (Certificate)keyCerts.remove(id);
- }
- if (c != null)
- {
- chainCerts.remove(new CertId(c.getPublicKey()));
- }
- }
- }
-
- /**
- * simply return the cert for the private key
- */
- public Certificate engineGetCertificate(
- String alias)
- {
- if (alias == null)
- {
- throw new IllegalArgumentException("null alias passed to getCertificate.");
- }
-
- Certificate c = (Certificate)certs.get(alias);
-
- //
- // look up the key table - and try the local key id
- //
- if (c == null)
- {
- String id = (String)localIds.get(alias);
- if (id != null)
- {
- c = (Certificate)keyCerts.get(id);
- }
- else
- {
- c = (Certificate)keyCerts.get(alias);
- }
- }
-
- return c;
- }
-
- public String engineGetCertificateAlias(
- Certificate cert)
- {
- Enumeration c = certs.elements();
- Enumeration k = certs.keys();
-
- while (c.hasMoreElements())
- {
- Certificate tc = (Certificate)c.nextElement();
- String ta = (String)k.nextElement();
-
- if (tc.equals(cert))
- {
- return ta;
- }
- }
-
- c = keyCerts.elements();
- k = keyCerts.keys();
-
- while (c.hasMoreElements())
- {
- Certificate tc = (Certificate)c.nextElement();
- String ta = (String)k.nextElement();
-
- if (tc.equals(cert))
- {
- return ta;
- }
- }
-
- return null;
- }
-
- public Certificate[] engineGetCertificateChain(
- String alias)
- {
- if (alias == null)
- {
- throw new IllegalArgumentException("null alias passed to getCertificateChain.");
- }
-
- if (!engineIsKeyEntry(alias))
- {
- return null;
- }
-
- Certificate c = engineGetCertificate(alias);
-
- if (c != null)
- {
- Vector cs = new Vector();
-
- while (c != null)
- {
- X509Certificate x509c = (X509Certificate)c;
- Certificate nextC = null;
-
- byte[] bytes = x509c.getExtensionValue(Extension.authorityKeyIdentifier.getId());
- if (bytes != null)
- {
- try
- {
- ASN1InputStream aIn = new ASN1InputStream(bytes);
-
- byte[] authBytes = ((ASN1OctetString)aIn.readObject()).getOctets();
- aIn = new ASN1InputStream(authBytes);
-
- AuthorityKeyIdentifier id = AuthorityKeyIdentifier.getInstance(aIn.readObject());
- if (id.getKeyIdentifier() != null)
- {
- nextC = (Certificate)chainCerts.get(new CertId(id.getKeyIdentifier()));
- }
-
- }
- catch (IOException e)
- {
- throw new RuntimeException(e.toString());
- }
- }
-
- if (nextC == null)
- {
- //
- // no authority key id, try the Issuer DN
- //
- Principal i = x509c.getIssuerDN();
- Principal s = x509c.getSubjectDN();
-
- if (!i.equals(s))
- {
- Enumeration e = chainCerts.keys();
-
- while (e.hasMoreElements())
- {
- X509Certificate crt = (X509Certificate)chainCerts.get(e.nextElement());
- Principal sub = crt.getSubjectDN();
- if (sub.equals(i))
- {
- try
- {
- x509c.verify(crt.getPublicKey());
- nextC = crt;
- break;
- }
- catch (Exception ex)
- {
- // continue
- }
- }
- }
- }
- }
-
- cs.addElement(c);
- if (nextC != c) // self signed - end of the chain
- {
- c = nextC;
- }
- else
- {
- c = null;
- }
- }
-
- Certificate[] certChain = new Certificate[cs.size()];
-
- for (int i = 0; i != certChain.length; i++)
- {
- certChain[i] = (Certificate)cs.elementAt(i);
- }
-
- return certChain;
- }
-
- return null;
- }
-
- public Date engineGetCreationDate(String alias)
- {
- if (alias == null)
- {
- throw new NullPointerException("alias == null");
- }
- if (keys.get(alias) == null && certs.get(alias) == null)
- {
- return null;
- }
- return new Date();
- }
-
- public Key engineGetKey(
- String alias,
- char[] password)
- throws NoSuchAlgorithmException, UnrecoverableKeyException
- {
- if (alias == null)
- {
- throw new IllegalArgumentException("null alias passed to getKey.");
- }
-
- return (Key)keys.get(alias);
- }
-
- public boolean engineIsCertificateEntry(
- String alias)
- {
- return (certs.get(alias) != null && keys.get(alias) == null);
- }
-
- public boolean engineIsKeyEntry(
- String alias)
- {
- return (keys.get(alias) != null);
- }
-
- public void engineSetCertificateEntry(
- String alias,
- Certificate cert)
- throws KeyStoreException
- {
- if (keys.get(alias) != null)
- {
- throw new KeyStoreException("There is a key entry with the name " + alias + ".");
- }
-
- certs.put(alias, cert);
- chainCerts.put(new CertId(cert.getPublicKey()), cert);
- }
-
- public void engineSetKeyEntry(
- String alias,
- byte[] key,
- Certificate[] chain)
- throws KeyStoreException
- {
- throw new RuntimeException("operation not supported");
- }
-
- public void engineSetKeyEntry(
- String alias,
- Key key,
- char[] password,
- Certificate[] chain)
- throws KeyStoreException
- {
- if (!(key instanceof PrivateKey))
- {
- throw new KeyStoreException("PKCS12 does not support non-PrivateKeys");
- }
-
- if ((key instanceof PrivateKey) && (chain == null))
- {
- throw new KeyStoreException("no certificate chain for private key");
- }
-
- if (keys.get(alias) != null)
- {
- engineDeleteEntry(alias);
- }
-
- keys.put(alias, key);
- if (chain != null)
- {
- certs.put(alias, chain[0]);
-
- for (int i = 0; i != chain.length; i++)
- {
- chainCerts.put(new CertId(chain[i].getPublicKey()), chain[i]);
- }
- }
- }
-
- public int engineSize()
- {
- Hashtable tab = new Hashtable();
-
- Enumeration e = certs.keys();
- while (e.hasMoreElements())
- {
- tab.put(e.nextElement(), "cert");
- }
-
- e = keys.keys();
- while (e.hasMoreElements())
- {
- String a = (String)e.nextElement();
- if (tab.get(a) == null)
- {
- tab.put(a, "key");
- }
- }
-
- return tab.size();
- }
-
- protected PrivateKey unwrapKey(
- AlgorithmIdentifier algId,
- byte[] data,
- char[] password,
- boolean wrongPKCS12Zero)
- throws IOException
- {
- ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
- try
- {
- if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds))
- {
- PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
-
- PBEKeySpec pbeSpec = new PBEKeySpec(password);
- PrivateKey out;
-
- SecretKeyFactory keyFact = SecretKeyFactory.getInstance(
- algorithm.getId(), bcProvider);
- PBEParameterSpec defParams = new PBEParameterSpec(
- pbeParams.getIV(),
- pbeParams.getIterations().intValue());
-
- SecretKey k = keyFact.generateSecret(pbeSpec);
-
- ((BCPBEKey)k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
-
- Cipher cipher = Cipher.getInstance(algorithm.getId(), bcProvider);
-
- cipher.init(Cipher.UNWRAP_MODE, k, defParams);
-
- // we pass "" as the key algorithm type as it is unknown at this point
- return (PrivateKey)cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
- }
- else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2))
- {
- PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters());
- PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters());
-
- SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg.getKeyDerivationFunc().getAlgorithm().getId(), bcProvider);
-
- SecretKey k = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), func.getIterationCount().intValue(), SecretKeyUtil.getKeySize(alg.getEncryptionScheme().getAlgorithm())));
-
- Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId(), bcProvider);
-
- cipher.init(Cipher.UNWRAP_MODE, k, new IvParameterSpec(ASN1OctetString.getInstance(alg.getEncryptionScheme().getParameters()).getOctets()));
-
- // we pass "" as the key algorithm type as it is unknown at this point
- return (PrivateKey)cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
- }
- }
- catch (Exception e)
- {
- throw new IOException("exception unwrapping private key - " + e.toString());
- }
-
- throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
- }
-
- protected byte[] wrapKey(
- String algorithm,
- Key key,
- PKCS12PBEParams pbeParams,
- char[] password)
- throws IOException
- {
- PBEKeySpec pbeSpec = new PBEKeySpec(password);
- byte[] out;
-
- try
- {
- SecretKeyFactory keyFact = SecretKeyFactory.getInstance(
- algorithm, bcProvider);
- PBEParameterSpec defParams = new PBEParameterSpec(
- pbeParams.getIV(),
- pbeParams.getIterations().intValue());
-
- Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
-
- cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
-
- out = cipher.wrap(key);
- }
- catch (Exception e)
- {
- throw new IOException("exception encrypting data - " + e.toString());
- }
-
- return out;
- }
-
- protected byte[] cryptData(
- boolean forEncryption,
- AlgorithmIdentifier algId,
- char[] password,
- boolean wrongPKCS12Zero,
- byte[] data)
- throws IOException
- {
- String algorithm = algId.getAlgorithm().getId();
- PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
- PBEKeySpec pbeSpec = new PBEKeySpec(password);
-
- try
- {
- SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
- PBEParameterSpec defParams = new PBEParameterSpec(
- pbeParams.getIV(),
- pbeParams.getIterations().intValue());
- BCPBEKey key = (BCPBEKey)keyFact.generateSecret(pbeSpec);
-
- key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
-
- Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
- int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
- cipher.init(mode, key, defParams);
- return cipher.doFinal(data);
- }
- catch (Exception e)
- {
- throw new IOException("exception decrypting data - " + e.toString());
- }
- }
-
- public void engineLoad(
- InputStream stream,
- char[] password)
- throws IOException
- {
- if (stream == null) // just initialising
- {
- return;
- }
-
- if (password == null)
- {
- throw new NullPointerException("No password supplied for PKCS#12 KeyStore.");
- }
-
- BufferedInputStream bufIn = new BufferedInputStream(stream);
-
- bufIn.mark(10);
-
- int head = bufIn.read();
-
- if (head != 0x30)
- {
- throw new IOException("stream does not represent a PKCS12 key store");
- }
-
- bufIn.reset();
-
- ASN1InputStream bIn = new ASN1InputStream(bufIn);
- ASN1Sequence obj = (ASN1Sequence)bIn.readObject();
- Pfx bag = Pfx.getInstance(obj);
- ContentInfo info = bag.getAuthSafe();
- Vector chain = new Vector();
- boolean unmarkedKey = false;
- boolean wrongPKCS12Zero = false;
-
- if (bag.getMacData() != null) // check the mac code
- {
- MacData mData = bag.getMacData();
- DigestInfo dInfo = mData.getMac();
- AlgorithmIdentifier algId = dInfo.getAlgorithmId();
- byte[] salt = mData.getSalt();
- int itCount = mData.getIterationCount().intValue();
-
- byte[] data = ((ASN1OctetString)info.getContent()).getOctets();
-
- try
- {
- byte[] res = calculatePbeMac(algId.getAlgorithm(), salt, itCount, password, false, data);
- byte[] dig = dInfo.getDigest();
-
- if (!Arrays.constantTimeAreEqual(res, dig))
- {
- if (password.length > 0)
- {
- throw new IOException("PKCS12 key store mac invalid - wrong password or corrupted file.");
- }
-
- // Try with incorrect zero length password
- res = calculatePbeMac(algId.getAlgorithm(), salt, itCount, password, true, data);
-
- if (!Arrays.constantTimeAreEqual(res, dig))
- {
- throw new IOException("PKCS12 key store mac invalid - wrong password or corrupted file.");
- }
-
- wrongPKCS12Zero = true;
- }
- }
- catch (IOException e)
- {
- throw e;
- }
- catch (Exception e)
- {
- throw new IOException("error constructing MAC: " + e.toString());
- }
- }
-
- keys = new IgnoresCaseHashtable();
- localIds = new Hashtable();
-
- if (info.getContentType().equals(data))
- {
- bIn = new ASN1InputStream(((ASN1OctetString)info.getContent()).getOctets());
-
- AuthenticatedSafe authSafe = AuthenticatedSafe.getInstance(bIn.readObject());
- ContentInfo[] c = authSafe.getContentInfo();
-
- for (int i = 0; i != c.length; i++)
- {
- if (c[i].getContentType().equals(data))
- {
- ASN1InputStream dIn = new ASN1InputStream(((ASN1OctetString)c[i].getContent()).getOctets());
- ASN1Sequence seq = (ASN1Sequence)dIn.readObject();
-
- for (int j = 0; j != seq.size(); j++)
- {
- SafeBag b = SafeBag.getInstance(seq.getObjectAt(j));
- if (b.getBagId().equals(pkcs8ShroudedKeyBag))
- {
- org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo eIn = org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo.getInstance(b.getBagValue());
- PrivateKey privKey = unwrapKey(eIn.getEncryptionAlgorithm(), eIn.getEncryptedData(), password, wrongPKCS12Zero);
-
- //
- // set the attributes on the key
- //
- PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier)privKey;
- String alias = null;
- ASN1OctetString localId = null;
-
- if (b.getBagAttributes() != null)
- {
- Enumeration e = b.getBagAttributes().getObjects();
- while (e.hasMoreElements())
- {
- ASN1Sequence sq = (ASN1Sequence)e.nextElement();
- ASN1ObjectIdentifier aOid = (ASN1ObjectIdentifier)sq.getObjectAt(0);
- ASN1Set attrSet = (ASN1Set)sq.getObjectAt(1);
- ASN1Primitive attr = null;
-
- if (attrSet.size() > 0)
- {
- attr = (ASN1Primitive)attrSet.getObjectAt(0);
-
- ASN1Encodable existing = bagAttr.getBagAttribute(aOid);
- if (existing != null)
- {
- // OK, but the value has to be the same
- if (!existing.toASN1Primitive().equals(attr))
- {
- throw new IOException(
- "attempt to add existing attribute with different value");
- }
- }
- else
- {
- bagAttr.setBagAttribute(aOid, attr);
- }
- }
-
- if (aOid.equals(pkcs_9_at_friendlyName))
- {
- alias = ((DERBMPString)attr).getString();
- keys.put(alias, privKey);
- }
- else if (aOid.equals(pkcs_9_at_localKeyId))
- {
- localId = (ASN1OctetString)attr;
- }
- }
- }
-
- if (localId != null)
- {
- String name = new String(Hex.encode(localId.getOctets()));
-
- if (alias == null)
- {
- keys.put(name, privKey);
- }
- else
- {
- localIds.put(alias, name);
- }
- }
- else
- {
- unmarkedKey = true;
- keys.put("unmarked", privKey);
- }
- }
- else if (b.getBagId().equals(certBag))
- {
- chain.addElement(b);
- }
- else
- {
- System.out.println("extra in data " + b.getBagId());
- System.out.println(ASN1Dump.dumpAsString(b));
- }
- }
- }
- else if (c[i].getContentType().equals(encryptedData))
- {
- EncryptedData d = EncryptedData.getInstance(c[i].getContent());
- byte[] octets = cryptData(false, d.getEncryptionAlgorithm(),
- password, wrongPKCS12Zero, d.getContent().getOctets());
- ASN1Sequence seq = (ASN1Sequence)ASN1Primitive.fromByteArray(octets);
-
- for (int j = 0; j != seq.size(); j++)
- {
- SafeBag b = SafeBag.getInstance(seq.getObjectAt(j));
-
- if (b.getBagId().equals(certBag))
- {
- chain.addElement(b);
- }
- else if (b.getBagId().equals(pkcs8ShroudedKeyBag))
- {
- org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo eIn = org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo.getInstance(b.getBagValue());
- PrivateKey privKey = unwrapKey(eIn.getEncryptionAlgorithm(), eIn.getEncryptedData(), password, wrongPKCS12Zero);
-
- //
- // set the attributes on the key
- //
- PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier)privKey;
- String alias = null;
- ASN1OctetString localId = null;
-
- Enumeration e = b.getBagAttributes().getObjects();
- while (e.hasMoreElements())
- {
- ASN1Sequence sq = (ASN1Sequence)e.nextElement();
- ASN1ObjectIdentifier aOid = (ASN1ObjectIdentifier)sq.getObjectAt(0);
- ASN1Set attrSet = (ASN1Set)sq.getObjectAt(1);
- ASN1Primitive attr = null;
-
- if (attrSet.size() > 0)
- {
- attr = (ASN1Primitive)attrSet.getObjectAt(0);
-
- ASN1Encodable existing = bagAttr.getBagAttribute(aOid);
- if (existing != null)
- {
- // OK, but the value has to be the same
- if (!existing.toASN1Primitive().equals(attr))
- {
- throw new IOException(
- "attempt to add existing attribute with different value");
- }
- }
- else
- {
- bagAttr.setBagAttribute(aOid, attr);
- }
- }
-
- if (aOid.equals(pkcs_9_at_friendlyName))
- {
- alias = ((DERBMPString)attr).getString();
- keys.put(alias, privKey);
- }
- else if (aOid.equals(pkcs_9_at_localKeyId))
- {
- localId = (ASN1OctetString)attr;
- }
- }
-
- String name = new String(Hex.encode(localId.getOctets()));
-
- if (alias == null)
- {
- keys.put(name, privKey);
- }
- else
- {
- localIds.put(alias, name);
- }
- }
- else if (b.getBagId().equals(keyBag))
- {
- org.bouncycastle.asn1.pkcs.PrivateKeyInfo kInfo = org.bouncycastle.asn1.pkcs.PrivateKeyInfo.getInstance(b.getBagValue());
- PrivateKey privKey = BouncyCastleProvider.getPrivateKey(kInfo);
-
- //
- // set the attributes on the key
- //
- PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier)privKey;
- String alias = null;
- ASN1OctetString localId = null;
-
- Enumeration e = b.getBagAttributes().getObjects();
- while (e.hasMoreElements())
- {
- ASN1Sequence sq = (ASN1Sequence)e.nextElement();
- ASN1ObjectIdentifier aOid = (ASN1ObjectIdentifier)sq.getObjectAt(0);
- ASN1Set attrSet = (ASN1Set)sq.getObjectAt(1);
- ASN1Primitive attr = null;
-
- if (attrSet.size() > 0)
- {
- attr = (ASN1Primitive)attrSet.getObjectAt(0);
-
- ASN1Encodable existing = bagAttr.getBagAttribute(aOid);
- if (existing != null)
- {
- // OK, but the value has to be the same
- if (!existing.toASN1Primitive().equals(attr))
- {
- throw new IOException(
- "attempt to add existing attribute with different value");
- }
- }
- else
- {
- bagAttr.setBagAttribute(aOid, attr);
- }
- }
-
- if (aOid.equals(pkcs_9_at_friendlyName))
- {
- alias = ((DERBMPString)attr).getString();
- keys.put(alias, privKey);
- }
- else if (aOid.equals(pkcs_9_at_localKeyId))
- {
- localId = (ASN1OctetString)attr;
- }
- }
-
- String name = new String(Hex.encode(localId.getOctets()));
-
- if (alias == null)
- {
- keys.put(name, privKey);
- }
- else
- {
- localIds.put(alias, name);
- }
- }
- else
- {
- System.out.println("extra in encryptedData " + b.getBagId());
- System.out.println(ASN1Dump.dumpAsString(b));
- }
- }
- }
- else
- {
- System.out.println("extra " + c[i].getContentType().getId());
- System.out.println("extra " + ASN1Dump.dumpAsString(c[i].getContent()));
- }
- }
- }
-
- certs = new IgnoresCaseHashtable();
- chainCerts = new Hashtable();
- keyCerts = new Hashtable();
-
- for (int i = 0; i != chain.size(); i++)
- {
- SafeBag b = (SafeBag)chain.elementAt(i);
- CertBag cb = CertBag.getInstance(b.getBagValue());
-
- if (!cb.getCertId().equals(x509Certificate))
- {
- throw new RuntimeException("Unsupported certificate type: " + cb.getCertId());
- }
-
- Certificate cert;
-
- try
- {
- ByteArrayInputStream cIn = new ByteArrayInputStream(
- ((ASN1OctetString)cb.getCertValue()).getOctets());
- cert = certFact.generateCertificate(cIn);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e.toString());
- }
-
- //
- // set the attributes
- //
- ASN1OctetString localId = null;
- String alias = null;
-
- if (b.getBagAttributes() != null)
- {
- Enumeration e = b.getBagAttributes().getObjects();
- while (e.hasMoreElements())
- {
- ASN1Sequence sq = (ASN1Sequence)e.nextElement();
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)sq.getObjectAt(0);
- ASN1Primitive attr = (ASN1Primitive)((ASN1Set)sq.getObjectAt(1)).getObjectAt(0);
- PKCS12BagAttributeCarrier bagAttr = null;
-
- if (cert instanceof PKCS12BagAttributeCarrier)
- {
- bagAttr = (PKCS12BagAttributeCarrier)cert;
-
- ASN1Encodable existing = bagAttr.getBagAttribute(oid);
- if (existing != null)
- {
- // OK, but the value has to be the same
- if (!existing.toASN1Primitive().equals(attr))
- {
- throw new IOException(
- "attempt to add existing attribute with different value");
- }
- }
- else
- {
- bagAttr.setBagAttribute(oid, attr);
- }
- }
-
- if (oid.equals(pkcs_9_at_friendlyName))
- {
- alias = ((DERBMPString)attr).getString();
- }
- else if (oid.equals(pkcs_9_at_localKeyId))
- {
- localId = (ASN1OctetString)attr;
- }
- }
- }
-
- chainCerts.put(new CertId(cert.getPublicKey()), cert);
-
- if (unmarkedKey)
- {
- if (keyCerts.isEmpty())
- {
- String name = new String(Hex.encode(createSubjectKeyId(cert.getPublicKey()).getKeyIdentifier()));
-
- keyCerts.put(name, cert);
- keys.put(name, keys.remove("unmarked"));
- }
- }
- else
- {
- //
- // the local key id needs to override the friendly name
- //
- if (localId != null)
- {
- String name = new String(Hex.encode(localId.getOctets()));
-
- keyCerts.put(name, cert);
- }
- if (alias != null)
- {
- certs.put(alias, cert);
- }
- }
- }
- }
-
- public void engineStore(OutputStream stream, char[] password)
- throws IOException
- {
- doStore(stream, password, false);
- }
-
- private void doStore(OutputStream stream, char[] password, boolean useDEREncoding)
- throws IOException
- {
- if (password == null)
- {
- throw new NullPointerException("No password supplied for PKCS#12 KeyStore.");
- }
-
- //
- // handle the key
- //
- ASN1EncodableVector keyS = new ASN1EncodableVector();
-
-
- Enumeration ks = keys.keys();
-
- while (ks.hasMoreElements())
- {
- byte[] kSalt = new byte[SALT_SIZE];
-
- random.nextBytes(kSalt);
-
- String name = (String)ks.nextElement();
- PrivateKey privKey = (PrivateKey)keys.get(name);
- PKCS12PBEParams kParams = new PKCS12PBEParams(kSalt, MIN_ITERATIONS);
- byte[] kBytes = wrapKey(keyAlgorithm.getId(), privKey, kParams, password);
- AlgorithmIdentifier kAlgId = new AlgorithmIdentifier(keyAlgorithm, kParams.toASN1Primitive());
- org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo kInfo = new org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo(kAlgId, kBytes);
- boolean attrSet = false;
- ASN1EncodableVector kName = new ASN1EncodableVector();
-
- if (privKey instanceof PKCS12BagAttributeCarrier)
- {
- PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier)privKey;
- //
- // make sure we are using the local alias on store
- //
- DERBMPString nm = (DERBMPString)bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
- if (nm == null || !nm.getString().equals(name))
- {
- bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(name));
- }
-
- //
- // make sure we have a local key-id
- //
- if (bagAttrs.getBagAttribute(pkcs_9_at_localKeyId) == null)
- {
- Certificate ct = engineGetCertificate(name);
-
- bagAttrs.setBagAttribute(pkcs_9_at_localKeyId, createSubjectKeyId(ct.getPublicKey()));
- }
-
- Enumeration e = bagAttrs.getBagAttributeKeys();
-
- while (e.hasMoreElements())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement();
- ASN1EncodableVector kSeq = new ASN1EncodableVector();
-
- kSeq.add(oid);
- kSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
-
- attrSet = true;
-
- kName.add(new DERSequence(kSeq));
- }
- }
-
- if (!attrSet)
- {
- //
- // set a default friendly name (from the key id) and local id
- //
- ASN1EncodableVector kSeq = new ASN1EncodableVector();
- Certificate ct = engineGetCertificate(name);
-
- kSeq.add(pkcs_9_at_localKeyId);
- kSeq.add(new DERSet(createSubjectKeyId(ct.getPublicKey())));
-
- kName.add(new DERSequence(kSeq));
-
- kSeq = new ASN1EncodableVector();
-
- kSeq.add(pkcs_9_at_friendlyName);
- kSeq.add(new DERSet(new DERBMPString(name)));
-
- kName.add(new DERSequence(kSeq));
- }
-
- SafeBag kBag = new SafeBag(pkcs8ShroudedKeyBag, kInfo.toASN1Primitive(), new DERSet(kName));
- keyS.add(kBag);
- }
-
- byte[] keySEncoded = new DERSequence(keyS).getEncoded(ASN1Encoding.DER);
- BEROctetString keyString = new BEROctetString(keySEncoded);
-
- //
- // certificate processing
- //
- byte[] cSalt = new byte[SALT_SIZE];
-
- random.nextBytes(cSalt);
-
- ASN1EncodableVector certSeq = new ASN1EncodableVector();
- PKCS12PBEParams cParams = new PKCS12PBEParams(cSalt, MIN_ITERATIONS);
- AlgorithmIdentifier cAlgId = new AlgorithmIdentifier(certAlgorithm, cParams.toASN1Primitive());
- Hashtable doneCerts = new Hashtable();
-
- Enumeration cs = keys.keys();
- while (cs.hasMoreElements())
- {
- try
- {
- String name = (String)cs.nextElement();
- Certificate cert = engineGetCertificate(name);
- boolean cAttrSet = false;
- CertBag cBag = new CertBag(
- x509Certificate,
- new DEROctetString(cert.getEncoded()));
- ASN1EncodableVector fName = new ASN1EncodableVector();
-
- if (cert instanceof PKCS12BagAttributeCarrier)
- {
- PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier)cert;
- //
- // make sure we are using the local alias on store
- //
- DERBMPString nm = (DERBMPString)bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
- if (nm == null || !nm.getString().equals(name))
- {
- bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(name));
- }
-
- //
- // make sure we have a local key-id
- //
- if (bagAttrs.getBagAttribute(pkcs_9_at_localKeyId) == null)
- {
- bagAttrs.setBagAttribute(pkcs_9_at_localKeyId, createSubjectKeyId(cert.getPublicKey()));
- }
-
- Enumeration e = bagAttrs.getBagAttributeKeys();
-
- while (e.hasMoreElements())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement();
- ASN1EncodableVector fSeq = new ASN1EncodableVector();
-
- fSeq.add(oid);
- fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
- fName.add(new DERSequence(fSeq));
-
- cAttrSet = true;
- }
- }
-
- if (!cAttrSet)
- {
- ASN1EncodableVector fSeq = new ASN1EncodableVector();
-
- fSeq.add(pkcs_9_at_localKeyId);
- fSeq.add(new DERSet(createSubjectKeyId(cert.getPublicKey())));
- fName.add(new DERSequence(fSeq));
-
- fSeq = new ASN1EncodableVector();
-
- fSeq.add(pkcs_9_at_friendlyName);
- fSeq.add(new DERSet(new DERBMPString(name)));
-
- fName.add(new DERSequence(fSeq));
- }
-
- SafeBag sBag = new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
-
- certSeq.add(sBag);
-
- doneCerts.put(cert, cert);
- }
- catch (CertificateEncodingException e)
- {
- throw new IOException("Error encoding certificate: " + e.toString());
- }
- }
-
- cs = certs.keys();
- while (cs.hasMoreElements())
- {
- try
- {
- String certId = (String)cs.nextElement();
- Certificate cert = (Certificate)certs.get(certId);
- boolean cAttrSet = false;
-
- if (keys.get(certId) != null)
- {
- continue;
- }
-
- CertBag cBag = new CertBag(
- x509Certificate,
- new DEROctetString(cert.getEncoded()));
- ASN1EncodableVector fName = new ASN1EncodableVector();
-
- if (cert instanceof PKCS12BagAttributeCarrier)
- {
- PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier)cert;
- //
- // make sure we are using the local alias on store
- //
- DERBMPString nm = (DERBMPString)bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
- if (nm == null || !nm.getString().equals(certId))
- {
- bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(certId));
- }
-
- Enumeration e = bagAttrs.getBagAttributeKeys();
-
- while (e.hasMoreElements())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement();
-
- // a certificate not immediately linked to a key doesn't require
- // a localKeyID and will confuse some PKCS12 implementations.
- //
- // If we find one, we'll prune it out.
- if (oid.equals(PKCSObjectIdentifiers.pkcs_9_at_localKeyId))
- {
- continue;
- }
-
- ASN1EncodableVector fSeq = new ASN1EncodableVector();
-
- fSeq.add(oid);
- fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
- fName.add(new DERSequence(fSeq));
-
- cAttrSet = true;
- }
- }
-
- if (!cAttrSet)
- {
- ASN1EncodableVector fSeq = new ASN1EncodableVector();
-
- fSeq.add(pkcs_9_at_friendlyName);
- fSeq.add(new DERSet(new DERBMPString(certId)));
-
- fName.add(new DERSequence(fSeq));
- }
-
- SafeBag sBag = new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
-
- certSeq.add(sBag);
-
- doneCerts.put(cert, cert);
- }
- catch (CertificateEncodingException e)
- {
- throw new IOException("Error encoding certificate: " + e.toString());
- }
- }
-
- cs = chainCerts.keys();
- while (cs.hasMoreElements())
- {
- try
- {
- CertId certId = (CertId)cs.nextElement();
- Certificate cert = (Certificate)chainCerts.get(certId);
-
- if (doneCerts.get(cert) != null)
- {
- continue;
- }
-
- CertBag cBag = new CertBag(
- x509Certificate,
- new DEROctetString(cert.getEncoded()));
- ASN1EncodableVector fName = new ASN1EncodableVector();
-
- if (cert instanceof PKCS12BagAttributeCarrier)
- {
- PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier)cert;
- Enumeration e = bagAttrs.getBagAttributeKeys();
-
- while (e.hasMoreElements())
- {
- ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement();
-
- // a certificate not immediately linked to a key doesn't require
- // a localKeyID and will confuse some PKCS12 implementations.
- //
- // If we find one, we'll prune it out.
- if (oid.equals(PKCSObjectIdentifiers.pkcs_9_at_localKeyId))
- {
- continue;
- }
-
- ASN1EncodableVector fSeq = new ASN1EncodableVector();
-
- fSeq.add(oid);
- fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
- fName.add(new DERSequence(fSeq));
- }
- }
-
- SafeBag sBag = new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
-
- certSeq.add(sBag);
- }
- catch (CertificateEncodingException e)
- {
- throw new IOException("Error encoding certificate: " + e.toString());
- }
- }
-
- byte[] certSeqEncoded = new DERSequence(certSeq).getEncoded(ASN1Encoding.DER);
- byte[] certBytes = cryptData(true, cAlgId, password, false, certSeqEncoded);
- EncryptedData cInfo = new EncryptedData(data, cAlgId, new BEROctetString(certBytes));
-
- ContentInfo[] info = new ContentInfo[]
- {
- new ContentInfo(data, keyString),
- new ContentInfo(encryptedData, cInfo.toASN1Primitive())
- };
-
- AuthenticatedSafe auth = new AuthenticatedSafe(info);
-
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- DEROutputStream asn1Out;
- if (useDEREncoding)
- {
- asn1Out = new DEROutputStream(bOut);
- }
- else
- {
- asn1Out = new BEROutputStream(bOut);
- }
-
- asn1Out.writeObject(auth);
-
- byte[] pkg = bOut.toByteArray();
-
- ContentInfo mainInfo = new ContentInfo(data, new BEROctetString(pkg));
-
- //
- // create the mac
- //
- byte[] mSalt = new byte[20];
- int itCount = MIN_ITERATIONS;
-
- random.nextBytes(mSalt);
-
- byte[] data = ((ASN1OctetString)mainInfo.getContent()).getOctets();
-
- MacData mData;
-
- try
- {
- byte[] res = calculatePbeMac(id_SHA1, mSalt, itCount, password, false, data);
-
- AlgorithmIdentifier algId = new AlgorithmIdentifier(id_SHA1, DERNull.INSTANCE);
- DigestInfo dInfo = new DigestInfo(algId, res);
-
- mData = new MacData(dInfo, mSalt, itCount);
- }
- catch (Exception e)
- {
- throw new IOException("error constructing MAC: " + e.toString());
- }
-
- //
- // output the Pfx
- //
- Pfx pfx = new Pfx(mainInfo, mData);
-
- if (useDEREncoding)
- {
- asn1Out = new DEROutputStream(stream);
- }
- else
- {
- asn1Out = new BEROutputStream(stream);
- }
-
- asn1Out.writeObject(pfx);
- }
-
- private static byte[] calculatePbeMac(
- ASN1ObjectIdentifier oid,
- byte[] salt,
- int itCount,
- char[] password,
- boolean wrongPkcs12Zero,
- byte[] data)
- throws Exception
- {
- SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
- PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
- PBEKeySpec pbeSpec = new PBEKeySpec(password);
- BCPBEKey key = (BCPBEKey)keyFact.generateSecret(pbeSpec);
- key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
-
- Mac mac = Mac.getInstance(oid.getId(), bcProvider);
- mac.init(key, defParams);
- mac.update(data);
- return mac.doFinal();
- }
-
- public static class BCPKCS12KeyStore
- extends PKCS12KeyStoreSpi
- {
- public BCPKCS12KeyStore()
- {
- super(bcProvider, pbeWithSHAAnd3_KeyTripleDES_CBC, pbeWithSHAAnd40BitRC2_CBC);
- }
- }
-
- public static class BCPKCS12KeyStore3DES
- extends PKCS12KeyStoreSpi
- {
- public BCPKCS12KeyStore3DES()
- {
- super(bcProvider, pbeWithSHAAnd3_KeyTripleDES_CBC, pbeWithSHAAnd3_KeyTripleDES_CBC);
- }
- }
-
- public static class DefPKCS12KeyStore
- extends PKCS12KeyStoreSpi
- {
- public DefPKCS12KeyStore()
- {
- super(null, pbeWithSHAAnd3_KeyTripleDES_CBC, pbeWithSHAAnd40BitRC2_CBC);
- }
- }
-
- public static class DefPKCS12KeyStore3DES
- extends PKCS12KeyStoreSpi
- {
- public DefPKCS12KeyStore3DES()
- {
- super(null, pbeWithSHAAnd3_KeyTripleDES_CBC, pbeWithSHAAnd3_KeyTripleDES_CBC);
- }
- }
-
- private static class IgnoresCaseHashtable
- {
- private Hashtable orig = new Hashtable();
- private Hashtable keys = new Hashtable();
-
- public void put(String key, Object value)
- {
- String lower = (key == null) ? null : Strings.toLowerCase(key);
- String k = (String)keys.get(lower);
- if (k != null)
- {
- orig.remove(k);
- }
-
- keys.put(lower, key);
- orig.put(key, value);
- }
-
- public Enumeration keys()
- {
- return orig.keys();
- }
-
- public Object remove(String alias)
- {
- String k = (String)keys.remove(alias == null ? null : Strings.toLowerCase(alias));
- if (k == null)
- {
- return null;
- }
-
- return orig.remove(k);
- }
-
- public Object get(String alias)
- {
- String k = (String)keys.get(alias == null ? null : Strings.toLowerCase(alias));
- if (k == null)
- {
- return null;
- }
-
- return orig.get(k);
- }
-
- public Enumeration elements()
- {
- return orig.elements();
- }
- }
-}