@Override protected void engineInit(byte[] bytes, String format) throws IOException { if (isASN1FormatString(format)) { X962Parameters params = X962Parameters.getInstance(bytes); ECCurve curve = EC5Util.getCurve(BouncyCastleProvider.CONFIGURATION, params); if (params.isNamedCurve()) { curveName = ECNamedCurveTable.getName(ASN1ObjectIdentifier.getInstance(params.getParameters())); } ecParameterSpec = EC5Util.convertToSpec(params, curve); } else { throw new IOException("Unknown encoded parameters format in AlgorithmParameters object: " + format); } }
private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException { X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters()); ECCurve curve = EC5Util.getCurve(configuration, params); ecSpec = EC5Util.convertToSpec(params, curve); ASN1Encodable privKey = info.parsePrivateKey(); if (privKey instanceof ASN1Integer) { ASN1Integer derD = ASN1Integer.getInstance(privKey); this.d = derD.getValue(); } else { org.bouncycastle.asn1.sec.ECPrivateKey ec = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(privKey); this.d = ec.getKey(); this.publicKey = ec.getPublicKey(); } }
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) { X962Parameters params = new X962Parameters((ASN1Primitive)info.getAlgorithm().getParameters()); ECCurve curve = EC5Util.getCurve(configuration, params); ecSpec = EC5Util.convertToSpec(params, curve); 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(); }
private void testKeyConversion() throws Exception { KeyPairGenerator kpGen = KeyPairGenerator.getInstance("ECDSA", "BC"); kpGen.initialize(new ECGenParameterSpec("prime192v1")); KeyPair pair = kpGen.generateKeyPair(); PublicKey pubKey = ECKeyUtil.publicToExplicitParameters(pair.getPublic(), "BC"); SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(pubKey.getEncoded())); X962Parameters params = X962Parameters.getInstance(info.getAlgorithmId().getParameters()); if (params.isNamedCurve() || params.isImplicitlyCA()) { fail("public key conversion to explicit failed"); } if (!((ECPublicKey)pair.getPublic()).getW().equals(((ECPublicKey)pubKey).getW())) { fail("public key conversion check failed"); } PrivateKey privKey = ECKeyUtil.privateToExplicitParameters(pair.getPrivate(), "BC"); PrivateKeyInfo privInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(privKey.getEncoded())); params = X962Parameters.getInstance(privInfo.getAlgorithmId().getParameters()); if (params.isNamedCurve() || params.isImplicitlyCA()) { fail("private key conversion to explicit failed"); } if (!((ECPrivateKey)pair.getPrivate()).getS().equals(((ECPrivateKey)privKey).getS())) { fail("private key conversion check failed"); } }