Search in sources :

Example 1 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project platformlayer by platformlayer.

the class KeyParser method parse.

public Object parse(String s) {
    Object key = null;
    if (key == null) {
        if (s.contains(BEGIN_PRIVATE_KEY)) {
            String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
            if (payload.contains(END_PRIVATE_KEY)) {
                payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
                key = tryParsePemFormat(payload);
            }
        }
    }
    if (key == null) {
        try {
            PemReader reader = new PemReader(new StringReader(s));
            PemObject pemObject = reader.readPemObject();
            reader.close();
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = kf.generatePrivate(keySpec);
            if (privateKey instanceof RSAPrivateCrtKey) {
                RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
                RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
                PublicKey publicKey = kf.generatePublic(publicKeySpec);
                key = new KeyPair(publicKey, privateKey);
            } else {
                key = privateKey;
            }
        } catch (Exception e) {
            log.debug("Error reading pem data", e);
            return null;
        }
    }
    if (key == null) {
        try {
            // TODO: Check if looks like base64??
            byte[] fromBase64 = Base64.decode(s);
            key = parse(fromBase64);
        } catch (Exception e) {
            log.debug("Cannot decode as base64", e);
        }
    }
    return key;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeyFactory(java.security.KeyFactory)

Example 2 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project robovm by robovm.

the class OpenSSLSignature method engineInitSign.

@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    destroyContextIfExists();
    if (privateKey instanceof OpenSSLKeyHolder) {
        OpenSSLKey pkey = ((OpenSSLKeyHolder) privateKey).getOpenSSLKey();
        checkEngineType(pkey);
        key = pkey;
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        key = OpenSSLRSAPrivateCrtKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof RSAPrivateKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        key = OpenSSLRSAPrivateKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof DSAPrivateKey) {
        if (engineType != EngineType.DSA) {
            throw new InvalidKeyException("Signature not initialized as DSA");
        }
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        key = OpenSSLDSAPrivateKey.getInstance(dsaPrivateKey);
    } else if (privateKey instanceof ECPrivateKey) {
        if (engineType != EngineType.EC) {
            throw new InvalidKeyException("Signature not initialized as EC");
        }
        ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
        key = OpenSSLECPrivateKey.getInstance(ecPrivateKey);
    } else {
        throw new InvalidKeyException("Need DSA or RSA or EC private key");
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 3 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project robovm by robovm.

the class OpenSSLSignatureRawRSA method engineInitSign.

@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    if (privateKey instanceof OpenSSLRSAPrivateKey) {
        OpenSSLRSAPrivateKey rsaPrivateKey = (OpenSSLRSAPrivateKey) privateKey;
        key = rsaPrivateKey.getOpenSSLKey();
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        key = OpenSSLRSAPrivateCrtKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        key = OpenSSLRSAPrivateKey.getInstance(rsaPrivateKey);
    } else {
        throw new InvalidKeyException("Need RSA private key");
    }
    // Allocate buffer according to RSA modulus size.
    int maxSize = NativeCrypto.RSA_size(key.getPkeyContext());
    inputBuffer = new byte[maxSize];
    inputOffset = 0;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 4 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project robovm by robovm.

the class OpenSSLRSAPrivateCrtKey method equals.

@Override
public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (o instanceof OpenSSLRSAPrivateKey) {
        OpenSSLRSAPrivateKey other = (OpenSSLRSAPrivateKey) o;
        return getOpenSSLKey().equals(other.getOpenSSLKey());
    }
    if (o instanceof RSAPrivateCrtKey) {
        ensureReadParams();
        RSAPrivateCrtKey other = (RSAPrivateCrtKey) o;
        if (getOpenSSLKey().isEngineBased()) {
            return getModulus().equals(other.getModulus()) && publicExponent.equals(other.getPublicExponent());
        } else {
            return getModulus().equals(other.getModulus()) && publicExponent.equals(other.getPublicExponent()) && getPrivateExponent().equals(other.getPrivateExponent()) && primeP.equals(other.getPrimeP()) && primeQ.equals(other.getPrimeQ()) && primeExponentP.equals(other.getPrimeExponentP()) && primeExponentQ.equals(other.getPrimeExponentQ()) && crtCoefficient.equals(other.getCrtCoefficient());
        }
    } else if (o instanceof RSAPrivateKey) {
        ensureReadParams();
        RSAPrivateKey other = (RSAPrivateKey) o;
        if (getOpenSSLKey().isEngineBased()) {
            return getModulus().equals(other.getModulus());
        } else {
            return getModulus().equals(other.getModulus()) && getPrivateExponent().equals(other.getPrivateExponent());
        }
    }
    return false;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 5 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project spring-security-oauth by spring-projects.

the class KeyStoreKeyFactory method getKeyPair.

public KeyPair getKeyPair(String alias, char[] password) {
    try {
        synchronized (lock) {
            if (store == null) {
                synchronized (lock) {
                    store = KeyStore.getInstance("jks");
                    store.load(resource.getInputStream(), this.password);
                }
            }
        }
        RSAPrivateCrtKey key = (RSAPrivateCrtKey) store.getKey(alias, password);
        RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
        return new KeyPair(publicKey, key);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot load keys from store: " + resource, e);
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Aggregations

RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)21 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)11 RSAPublicKey (java.security.interfaces.RSAPublicKey)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 InvalidKeyException (java.security.InvalidKeyException)6 PrivateKey (java.security.PrivateKey)6 PublicKey (java.security.PublicKey)6 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)6 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)6 BigInteger (java.math.BigInteger)5 KeyPair (java.security.KeyPair)5 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 KeyFactory (java.security.KeyFactory)3 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)3 KeyPairGenerator (java.security.KeyPairGenerator)2 KeyStore (java.security.KeyStore)2 X509Certificate (java.security.cert.X509Certificate)2 DSAParams (java.security.interfaces.DSAParams)2