Search in sources :

Example 6 with RSAPrivateCrtKeySpec

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()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 7 with RSAPrivateCrtKeySpec

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()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 8 with RSAPrivateCrtKeySpec

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);
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) IOException(java.io.IOException) CryptoException(org.kse.crypto.CryptoException) ByteBuffer(java.nio.ByteBuffer) KeyFactory(java.security.KeyFactory)

Example 9 with RSAPrivateCrtKeySpec

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);
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) PemInfo(org.kse.utilities.pem.PemInfo) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) CryptoException(org.kse.crypto.CryptoException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) CryptoException(org.kse.crypto.CryptoException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) KeyFactory(java.security.KeyFactory) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 10 with RSAPrivateCrtKeySpec

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);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Aggregations

RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)48 KeyFactory (java.security.KeyFactory)16 BigInteger (java.math.BigInteger)14 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 PrivateKey (java.security.PrivateKey)11 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 PublicKey (java.security.PublicKey)7 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)7 KeySpec (java.security.spec.KeySpec)6 IOException (java.io.IOException)5 KeyPair (java.security.KeyPair)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 GeneralSecurityException (java.security.GeneralSecurityException)4 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)4