use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class RSAPrivateCrtKeySpecTest method testGetPrimeP.
/**
* Test for <code>getPrimeP()</code> method<br>
* Assertion: returns prime P
*/
public final void testGetPrimeP() {
RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeP()));
}
use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class RSAPrivateCrtKeySpecTest method testGetPrimeQ.
/**
* Test for <code>getPrimeQ()</code> method<br>
* Assertion: returns prime Q
*/
public final void testGetPrimeQ() {
RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeQ()));
}
use of java.security.spec.RSAPrivateCrtKeySpec in project keystore-explorer by kaikramer.
the class MsPvkUtil method blobToRsaPrivateKey.
private static RSAPrivateCrtKey blobToRsaPrivateKey(byte[] rsaPrivateKeyBlob) throws CryptoException {
try {
ByteBuffer bb = ByteBuffer.wrap(rsaPrivateKeyBlob);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Get each blob field
// rsapubkey.magic
long magic = UnsignedUtil.getInt(bb);
// Check magic field is valid
if (magic != RSA_PRIV_MAGIC) {
throw new CryptoException(MessageFormat.format(res.getString("InvalidRsaMagicField.exception.message"), Long.toHexString(magic), Long.toHexString(RSA_PRIV_MAGIC)));
}
// rsapubkey.bitlen
long bitLength = UnsignedUtil.getInt(bb);
// Byte lengths divisions may have remainders to take account for if not factors of 16 and/or 8
int add8 = 0;
if ((bitLength % 8) != 0) {
add8++;
}
int add16 = 0;
if ((bitLength % 16) != 0) {
add16++;
}
// rsapubkey.pubexp
BigInteger publicExponent = new BigInteger(Long.toString(UnsignedUtil.getInt(bb)));
// modulus
BigInteger modulus = readBigInteger(bb, (int) (bitLength / 8) + add8);
// prime 1
BigInteger prime1 = readBigInteger(bb, (int) (bitLength / 16) + add16);
// prime 2
BigInteger prime2 = readBigInteger(bb, (int) (bitLength / 16) + add16);
// exponent1
BigInteger exponent1 = readBigInteger(bb, (int) (bitLength / 16) + add16);
// exponent2
BigInteger exponent2 = readBigInteger(bb, (int) (bitLength / 16) + add16);
// coefficient
BigInteger coefficient = readBigInteger(bb, (int) (bitLength / 16) + add16);
// privateExponent
BigInteger privateExponent = readBigInteger(bb, (int) (bitLength / 8) + add8);
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2, coefficient);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateCrtKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoConvertBlobToRsaKey.exception.message"), ex);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoConvertBlobToRsaKey.exception.message"), ex);
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project keystore-explorer by kaikramer.
the class OpenSslPvkUtil method load.
/**
* Load an unencrypted OpenSSL private key from the stream. The encoding of
* the private key may be PEM or DER.
*
* @param is
* Stream to load the unencrypted private key from
* @return The private key
* @throws PrivateKeyEncryptedException
* If private key is encrypted
* @throws CryptoException
* Problem encountered while loading the private key
* @throws IOException
* An I/O error occurred
*/
public static PrivateKey load(InputStream is) throws CryptoException, IOException {
byte[] streamContents = ReadUtil.readFully(is);
EncryptionType encType = getEncryptionType(new ByteArrayInputStream(streamContents));
if (encType == null) {
throw new CryptoException(res.getString("NotValidOpenSsl.exception.message"));
}
if (encType == ENCRYPTED) {
throw new PrivateKeyEncryptedException(res.getString("OpenSslIsEncrypted.exception.message"));
}
// Check if stream is PEM encoded
PemInfo pemInfo = PemUtil.decode(new ByteArrayInputStream(streamContents));
if (pemInfo != null) {
// It is - get DER from PEM
streamContents = pemInfo.getContent();
}
try {
// Read OpenSSL DER structure
ASN1InputStream asn1InputStream = new ASN1InputStream(streamContents);
ASN1Primitive openSsl = asn1InputStream.readObject();
asn1InputStream.close();
if (openSsl instanceof ASN1Sequence) {
ASN1Sequence seq = (ASN1Sequence) openSsl;
if (seq.size() == 9) {
// RSA private key
BigInteger version = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger modulus = ((ASN1Integer) seq.getObjectAt(1)).getValue();
BigInteger publicExponent = ((ASN1Integer) seq.getObjectAt(2)).getValue();
BigInteger privateExponent = ((ASN1Integer) seq.getObjectAt(3)).getValue();
BigInteger primeP = ((ASN1Integer) seq.getObjectAt(4)).getValue();
BigInteger primeQ = ((ASN1Integer) seq.getObjectAt(5)).getValue();
BigInteger primeExponentP = ((ASN1Integer) seq.getObjectAt(6)).getValue();
BigInteger primeExponenetQ = ((ASN1Integer) seq.getObjectAt(7)).getValue();
BigInteger crtCoefficient = ((ASN1Integer) seq.getObjectAt(8)).getValue();
if (!version.equals(VERSION)) {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslVersionIncorrect.exception.message"), "" + VERSION.intValue(), "" + version.intValue()));
}
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponenetQ, crtCoefficient);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} else if (seq.size() == 6) {
// DSA private key
BigInteger version = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger primeModulusP = ((ASN1Integer) seq.getObjectAt(1)).getValue();
BigInteger primeQ = ((ASN1Integer) seq.getObjectAt(2)).getValue();
BigInteger generatorG = ((ASN1Integer) seq.getObjectAt(3)).getValue();
// publicExponentY not req for pvk: sequence.getObjectAt(4);
BigInteger secretExponentX = ((ASN1Integer) seq.getObjectAt(5)).getValue();
if (!version.equals(VERSION)) {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslVersionIncorrect.exception.message"), "" + VERSION.intValue(), "" + version.intValue()));
}
DSAPrivateKeySpec dsaPrivateKeySpec = new DSAPrivateKeySpec(secretExponentX, primeModulusP, primeQ, generatorG);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePrivate(dsaPrivateKeySpec);
} else if (seq.size() >= 2) {
// EC private key (RFC 5915)
org.bouncycastle.asn1.sec.ECPrivateKey pKey = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(seq);
AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, pKey.getParameters());
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey);
return new JcaPEMKeyConverter().getPrivateKey(privInfo);
} else {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslSequenceIncorrectSize.exception.message"), "" + seq.size()));
}
} else {
throw new CryptoException(res.getString("OpenSslSequenceNotFound.exception.message"));
}
} catch (Exception ex) {
throw new CryptoException(res.getString("NoLoadOpenSslPrivateKey.exception.message"), ex);
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project credhub by cloudfoundry-incubator.
the class CryptoWrapper method toKeyPair.
public synchronized KeyPair toKeyPair(Pointer rsa) throws InvalidKeySpecException {
RSA.ByReference rsaStructure = new RSA.ByReference(rsa);
rsaStructure.read();
RSAPublicKeySpec publicKeySpec = getRsaPublicKeySpec(rsaStructure);
RSAPrivateCrtKeySpec privateCrtKeySpec = getRsaPrivateCrtKeySpec(rsaStructure);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
PrivateKey privateKey = keyFactory.generatePrivate(privateCrtKeySpec);
return new KeyPair(publicKey, privateKey);
}
Aggregations