/** 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 != 0) { throw new IOException("Version must be 0"); } n = getBigInteger(data); e = getBigInteger(data); d = getBigInteger(data); p = getBigInteger(data); q = getBigInteger(data); pe = getBigInteger(data); qe = getBigInteger(data); coeff = getBigInteger(data); if (derValue.data.available() != 0) { throw new IOException("Extra data available"); } } catch (IOException e) { throw new InvalidKeyException("Invalid RSA private key", e); } }
/** Read a BigInteger from the DerInputStream. */ static BigInteger getBigInteger(DerInputStream data) throws IOException { BigInteger b = data.getBigInteger(); /* * Some implementations do not correctly encode ASN.1 INTEGER values * in 2's complement format, resulting in a negative integer when * decoded. Correct the error by converting it to a positive integer. * * See CR 6255949 */ if (b.signum() < 0) { b = new BigInteger(1, b.toByteArray()); } return b; }