Search in sources :

Example 16 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testGetPublicExponent.

/**
 * Test for <code>getPublicExponent()</code> method<br>
 * Assertion: returns public exponent
 */
public final void testGetPublicExponent() {
    RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
    assertTrue(BigInteger.valueOf(5L).equals(ks.getPublicExponent()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 17 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec02.

/**
 * Test #2 for <code>RSAPrivateCrtKeySpec</code> constructor
 * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
 * object using valid parameters
 */
public final void testRSAPrivateCrtKeySpec02() {
    KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
    assertTrue(ks instanceof RSAPrivateKeySpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 18 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec01.

/**
 * Test #1 for <code>RSAPrivateCrtKeySpec</code> constructor
 * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
 * object using valid parameters
 */
public final void testRSAPrivateCrtKeySpec01() {
    KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
    assertTrue(ks instanceof RSAPrivateCrtKeySpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 19 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project qpid-broker-j by apache.

the class SSLUtil method getRSAKeySpec.

private static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws InvalidKeySpecException {
    ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
    try {
        // PKCS#1 is encoded as a DER sequence of:
        // (version, modulus, publicExponent, privateExponent, primeP, primeQ,
        // primeExponentP, primeExponentQ, crtCoefficient)
        int tag = ((int) buffer.get()) & 0xff;
        // check tag is that of a sequence
        if (((tag & 0x20) != 0x20) || ((tag & 0x1F) != 0x10)) {
            throw new InvalidKeySpecException("Unable to parse key as PKCS#1 format");
        }
        int length = getLength(buffer);
        buffer = buffer.slice();
        buffer.limit(length);
        // first tlv is version - which we'll ignore
        byte versionTag = buffer.get();
        int versionLength = getLength(buffer);
        buffer.position(buffer.position() + versionLength);
        RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer));
        return keySpec;
    } catch (BufferUnderflowException e) {
        throw new InvalidKeySpecException("Unable to parse key as PKCS#1 format");
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 20 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project java by kubernetes-client.

the class SSLUtils method loadKey.

public static PrivateKey loadKey(InputStream keyInputStream, String clientKeyAlgo) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Try PKCS7 / EC
    if (clientKeyAlgo.equals("EC")) {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        PEMParser pemParser = new PEMParser(new InputStreamReader(keyInputStream));
        Object pemObject;
        while ((pemObject = pemParser.readObject()) != null) {
            if (pemObject instanceof PEMKeyPair) {
                return new JcaPEMKeyConverter().getKeyPair(((PEMKeyPair) pemObject)).getPrivate();
            }
        }
    }
    byte[] keyBytes = decodePem(keyInputStream);
    // Try PKCS1 / RSA
    if (clientKeyAlgo.equals("RSA")) {
        RSAPrivateCrtKeySpec keySpec = decodePKCS1(keyBytes);
        return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
    }
    // Try PKCS8
    // TODO: There _has_ to be a better way to do this, but I spent >
    // 2 hours trying to find it and failed...
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    try {
        return KeyFactory.getInstance("RSA").generatePrivate(spec);
    } catch (InvalidKeySpecException ex) {
    // ignore if it's not RSA
    }
    try {
        return KeyFactory.getInstance("ECDSA").generatePrivate(spec);
    } catch (InvalidKeySpecException ex) {
    // ignore if it's not DSA
    }
    throw new InvalidKeySpecException("Unknown type of PKCS8 Private Key, tried RSA and ECDSA");
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PEMParser(org.bouncycastle.openssl.PEMParser) InputStreamReader(java.io.InputStreamReader) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

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