in src/com/amazon/corretto/crypto/provider/RsaCipher.java [374:429]
private void parseKey() throws InvalidKeyException {
synchronized (lock_) {
if (nativeKey_ != null && !nativeKey_.isReleased()) {
return;
}
n = null;
e = null;
d = null;
p = null;
q = null;
dmp1 = null;
dmq1 = null;
iqmp = null;
if (key_ instanceof PrivateKey) {
boolean parsedKey = false;
try {
final RSAPrivateCrtKeySpec spec = KEY_FACTORY.getKeySpec((Key) key_, RSAPrivateCrtKeySpec.class);
n = spec.getModulus().toByteArray();
e = spec.getPublicExponent().toByteArray();
d = spec.getPrivateExponent().toByteArray();
p = spec.getPrimeP().toByteArray();
q = spec.getPrimeQ().toByteArray();
dmp1 = spec.getPrimeExponentP().toByteArray();
dmq1 = spec.getPrimeExponentQ().toByteArray();
iqmp = spec.getCrtCoefficient().toByteArray();
parsedKey = true;
} catch (final InvalidKeySpecException e) {
// swallow the exception
}
if (!parsedKey) {
try {
final RSAPrivateKeySpec spec = KEY_FACTORY.getKeySpec((Key) key_, RSAPrivateKeySpec.class);
n = spec.getModulus().toByteArray();
d = spec.getPrivateExponent().toByteArray();
parsedKey = true;
} catch (final InvalidKeySpecException e) {
// swallow the exception
}
}
if (!parsedKey) {
throw new InvalidKeyException("Unable to parse the key " + key_);
}
} else if (key_ instanceof PublicKey) {
try {
final RSAPublicKeySpec spec = KEY_FACTORY.getKeySpec((Key) key_, RSAPublicKeySpec.class);
n = spec.getModulus().toByteArray();
e = spec.getPublicExponent().toByteArray();
} catch (final InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else {
throw new IllegalArgumentException("Unexpected key type: " + key_.getClass());
}
}
}