Search in sources :

Example 6 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey 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 7 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey 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 8 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey 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 9 with RSAPrivateKey

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

the class RSAPrivateKeyTest method test_getPrivateExponent.

/**
     * java.security.interfaces.RSAPrivateKey
     * #getPrivateExponent()
     */
public void test_getPrivateExponent() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger d = BigInteger.valueOf(2753);
    RSAPrivateKey key = (RSAPrivateKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
    assertEquals("invalid private exponent", d, key.getPrivateExponent());
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 10 with RSAPrivateKey

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

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (params == null || params instanceof OAEPParameterSpec) {
        if (key instanceof RSAPublicKey) {
            if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 2 requires RSAPublicKey");
            }
            param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
        } else {
            throw new InvalidKeyException("unknown key type passed to RSA");
        }
        if (params != null) {
            OAEPParameterSpec spec = (OAEPParameterSpec) params;
            paramSpec = params;
            if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
                throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
            }
            if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unkown MGF parameters");
            }
            Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
            if (mgfDigest == null) {
                throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
            }
            cipher = new OAEPEncoding(new RSABlindedEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
        }
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if (!(cipher instanceof RSABlindedEngine)) {
        if (random != null) {
            param = new ParametersWithRandom(param, random);
        } else {
            param = new ParametersWithRandom(param, new SecureRandom());
        }
    }
    bOut.reset();
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Digest(org.bouncycastle.crypto.Digest) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Aggregations

RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46 RSAPublicKey (java.security.interfaces.RSAPublicKey)24 KeyFactory (java.security.KeyFactory)13 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 InvalidKeyException (java.security.InvalidKeyException)12 PrivateKey (java.security.PrivateKey)12 KeyPair (java.security.KeyPair)11 PublicKey (java.security.PublicKey)11 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)11 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 KeyPairGenerator (java.security.KeyPairGenerator)8 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7 BigInteger (java.math.BigInteger)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Key (java.security.Key)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 Signature (java.security.Signature)4