use of java.security.spec.ECParameterSpec in project robovm by robovm.
the class JCEECPrivateKey method populateFromPrivKeyInfo.
private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
X962Parameters params = new X962Parameters((ASN1Primitive) info.getPrivateKeyAlgorithm().getParameters());
if (params.isNamedCurve()) {
ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
// BEGIN android-removed
// if (ecP == null) // GOST Curve
// {
// ECDomainParameters gParam = ECGOST3410NamedCurves.getByOID(oid);
// EllipticCurve ellipticCurve = EC5Util.convertCurve(gParam.getCurve(), gParam.getSeed());
//
// ecSpec = new ECNamedCurveSpec(
// ECGOST3410NamedCurves.getName(oid),
// ellipticCurve,
// new ECPoint(
// gParam.getG().getX().toBigInteger(),
// gParam.getG().getY().toBigInteger()),
// gParam.getN(),
// gParam.getH());
// }
// else
// END android-removed
{
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH());
}
} else if (params.isImplicitlyCA()) {
ecSpec = null;
} else {
X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
this.ecSpec = new ECParameterSpec(ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH().intValue());
}
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof DERInteger) {
DERInteger derD = DERInteger.getInstance(privKey);
this.d = derD.getValue();
} else {
ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence) privKey);
this.d = ec.getKey();
this.publicKey = ec.getPublicKey();
}
}
use of java.security.spec.ECParameterSpec in project robovm by robovm.
the class OpenSSLECKeyFactory method engineTranslateKey.
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("key == null");
}
if ((key instanceof OpenSSLECPublicKey) || (key instanceof OpenSSLECPrivateKey)) {
return key;
} else if (key instanceof ECPublicKey) {
ECPublicKey ecKey = (ECPublicKey) key;
ECPoint w = ecKey.getW();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePublic(new ECPublicKeySpec(w, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if (key instanceof ECPrivateKey) {
ECPrivateKey ecKey = (ECPrivateKey) key;
BigInteger s = ecKey.getS();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePrivate(new ECPrivateKeySpec(s, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePublic(new X509EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else {
throw new InvalidKeyException("Key must be EC public or private key; was " + key.getClass().getName());
}
}
use of java.security.spec.ECParameterSpec in project robovm by robovm.
the class OpenSSLECKeyPairGenerator method initialize.
@Override
public void initialize(AlgorithmParameterSpec param, SecureRandom random) throws InvalidAlgorithmParameterException {
if (param instanceof ECParameterSpec) {
ECParameterSpec ecParam = (ECParameterSpec) param;
group = OpenSSLECGroupContext.getInstance(ecParam);
} else if (param instanceof ECGenParameterSpec) {
ECGenParameterSpec ecParam = (ECGenParameterSpec) param;
final String curveName = ecParam.getName();
/*
* Store the group in a temporary variable until we know this is a
* valid group.
*/
final OpenSSLECGroupContext possibleGroup = OpenSSLECGroupContext.getCurveByName(curveName);
if (possibleGroup == null) {
throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName);
}
group = possibleGroup;
} else {
throw new InvalidAlgorithmParameterException("parameter must be ECParameterSpec or ECGenParameterSpec");
}
}
use of java.security.spec.ECParameterSpec in project robovm by robovm.
the class OpenSSLECPublicKey method equals.
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof OpenSSLECPrivateKey) {
OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o;
return key.equals(other.key);
}
if (!(o instanceof ECPublicKey)) {
return false;
}
final ECPublicKey other = (ECPublicKey) o;
if (!getPublicKey().equals(other.getW())) {
return false;
}
final ECParameterSpec spec = getParams();
final ECParameterSpec otherSpec = other.getParams();
return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor();
}
use of java.security.spec.ECParameterSpec in project robovm by robovm.
the class ECPrivateKeySpecTest method setUp.
protected void setUp() throws Exception {
super.setUp();
ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
s = BigInteger.valueOf(1);
ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
ecpks = new ECPrivateKeySpec(s, ecparams);
}
Aggregations