/** Construct a key from its components. Used by the KeyFactory. */ ECPrivateKeyImpl(BigInteger s, ECParameterSpec params) throws InvalidKeyException { this.s = s; this.params = params; // generate the encoding algid = new AlgorithmId(AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params)); try { DerOutputStream out = new DerOutputStream(); out.putInteger(1); // version 1 byte[] privBytes = ECUtil.trimZeroes(s.toByteArray()); out.putOctetString(privBytes); DerValue val = new DerValue(DerValue.tag_Sequence, out.toByteArray()); key = val.toByteArray(); } catch (IOException exc) { // should never occur throw new InvalidKeyException(exc); } }
/** Parse the key. Called by PKCS8Key. */ protected void parseKeyBits() throws InvalidKeyException { try { DerInputStream in = new DerInputStream(key); DerValue derValue = in.getDerValue(); if (derValue.tag != DerValue.tag_Sequence) { throw new IOException("Not a SEQUENCE"); } DerInputStream data = derValue.data; int version = data.getInteger(); if (version != 1) { throw new IOException("Version must be 1"); } byte[] privData = data.getOctetString(); s = new BigInteger(1, privData); while (data.available() != 0) { DerValue value = data.getDerValue(); if (value.isContextSpecific((byte) 0)) { // ignore for now } else if (value.isContextSpecific((byte) 1)) { // ignore for now } else { throw new InvalidKeyException("Unexpected value: " + value); } } AlgorithmParameters algParams = this.algid.getParameters(); if (algParams == null) { throw new InvalidKeyException( "EC domain parameters must be " + "encoded in the algorithm identifier"); } params = algParams.getParameterSpec(ECParameterSpec.class); } catch (IOException e) { throw new InvalidKeyException("Invalid EC private key", e); } catch (InvalidParameterSpecException e) { throw new InvalidKeyException("Invalid EC private key", e); } }