Search in sources :

Example 56 with KeyException

use of java.security.KeyException in project scheduling by ow2-proactive.

the class KeyPairUtil method decrypt.

/**
 * Decrypt a message using asymmetric keys
 *
 * @param privKey Private key used for decryption
 * @param cipherParams cipher parameters: transformations (ie RSA/ECB/NoPadding)
 * @param message the encrypted message
 * @return the decrypted message
 * @throws KeyException private key recovery failed, decryption failed
 */
public static synchronized byte[] decrypt(PrivateKey privKey, String cipherParams, byte[] message) throws KeyException {
    Cipher ciph = null;
    try {
        ciph = Cipher.getInstance(cipherParams);
        ciph.init(Cipher.DECRYPT_MODE, privKey, KeyUtil.getSecureRandom());
    } catch (Exception e) {
        throw new KeyException("Could not initialize cipher", e);
    }
    byte[] res = null;
    try {
        res = ciph.doFinal(message);
    } catch (Exception e) {
        throw new KeyException("Could not descrypt message.", e);
    }
    return res;
}
Also used : Cipher(javax.crypto.Cipher) KeyException(java.security.KeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 57 with KeyException

use of java.security.KeyException in project scheduling by ow2-proactive.

the class KeyUtil method encrypt.

/**
 * Encrypt a message using a symmetric key
 *
 * @param key secret key used for encryption
 * @param cipherParams cipher parameters: transformations, ie AES
 * @param message the message to encrypt
 * @return the encrypted message
 * @throws KeyException encryption failed, public key recovery failed
 */
public static synchronized byte[] encrypt(SecretKey key, String cipherParams, byte[] message) throws KeyException {
    Cipher ciph = null;
    try {
        ciph = Cipher.getInstance(cipherParams);
        ciph.init(Cipher.ENCRYPT_MODE, key, getSecureRandom());
    } catch (Exception e) {
        throw new KeyException("Coult not initialize cipher", e);
    }
    byte[] res = null;
    try {
        res = ciph.doFinal(message);
    } catch (Exception e) {
        throw new KeyException("Could not encrypt message", e);
    }
    return res;
}
Also used : Cipher(javax.crypto.Cipher) KeyException(java.security.KeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 58 with KeyException

use of java.security.KeyException in project scheduling by ow2-proactive.

the class SmartProxyImpl method init.

@Override
public void init(ConnectionInfo connectionInfo) throws SchedulerException, LoginException {
    this.connectionInfo = connectionInfo;
    if (connectionInfo.getCredentialFile() != null) {
        try {
            Credentials credentials = Credentials.getCredentials(connectionInfo.getCredentialFile().getAbsolutePath());
            init(connectionInfo.getUrl(), credentials);
        } catch (KeyException e) {
            throw new LoginException(e.getMessage());
        }
    } else {
        CredData cred = new CredData(CredData.parseLogin(connectionInfo.getLogin()), CredData.parseDomain(connectionInfo.getLogin()), connectionInfo.getPassword());
        init(connectionInfo.getUrl(), cred);
    }
}
Also used : CredData(org.ow2.proactive.authentication.crypto.CredData) LoginException(javax.security.auth.login.LoginException) Credentials(org.ow2.proactive.authentication.crypto.Credentials) KeyException(java.security.KeyException)

Example 59 with KeyException

use of java.security.KeyException in project scheduling by ow2-proactive.

the class SchedulingService method addThirdPartyCredentials.

/**
 * Create a new Credential object containing users' 3rd Party Credentials.
 *
 * @param creds credentials for specific user
 * @return in case of success new object containing the 3rd party credentials used to create bindings
 * at clean script
 */
Credentials addThirdPartyCredentials(Credentials creds) throws KeyException, IllegalAccessException {
    // retrieve scheduler key pair
    String privateKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PRIVKEY_PATH.getValueAsString());
    String publicKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PUBKEY_PATH.getValueAsString());
    // get keys from task
    PrivateKey privateKey = Credentials.getPrivateKey(privateKeyPath);
    PublicKey publicKey = Credentials.getPublicKey(publicKeyPath);
    // retrieve the current creData from task
    CredData credData = creds.decrypt(privateKey);
    // retrive database to get third party credentials from
    SchedulerDBManager dbManager = getInfrastructure().getDBManager();
    if (dbManager != null) {
        Map<String, HybridEncryptedData> thirdPartyCredentials = dbManager.thirdPartyCredentialsMap(credData.getLogin());
        if (thirdPartyCredentials == null) {
            logger.error("Failed to retrieve Third Party Credentials!");
            throw new KeyException("Failed to retrieve thirdPartyCredentials!");
        } else {
            // cycle third party credentials, add one-by-one to the decrypter
            for (Map.Entry<String, HybridEncryptedData> thirdPartyCredential : thirdPartyCredentials.entrySet()) {
                String decryptedValue = HybridEncryptionUtil.decryptString(thirdPartyCredential.getValue(), privateKey);
                credData.addThirdPartyCredential(thirdPartyCredential.getKey(), decryptedValue);
            }
        }
    }
    return Credentials.createCredentials(credData, publicKey);
}
Also used : PrivateKey(java.security.PrivateKey) HybridEncryptedData(org.ow2.proactive.authentication.crypto.HybridEncryptionUtil.HybridEncryptedData) PublicKey(java.security.PublicKey) SchedulerDBManager(org.ow2.proactive.scheduler.core.db.SchedulerDBManager) CredData(org.ow2.proactive.authentication.crypto.CredData) Map(java.util.Map) KeyException(java.security.KeyException)

Aggregations

KeyException (java.security.KeyException)59 IOException (java.io.IOException)22 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 File (java.io.File)10 PublicKey (java.security.PublicKey)8 FileInputStream (java.io.FileInputStream)7 Cipher (javax.crypto.Cipher)6 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 LoginException (javax.security.auth.login.LoginException)5 Credentials (org.ow2.proactive.authentication.crypto.Credentials)5 FileNotFoundException (java.io.FileNotFoundException)4 PrivateKey (java.security.PrivateKey)4 CredData (org.ow2.proactive.authentication.crypto.CredData)4 RMException (org.ow2.proactive.resourcemanager.exception.RMException)4 CommandLineBuilder (org.ow2.proactive.resourcemanager.utils.CommandLineBuilder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 InputStream (java.io.InputStream)3 Key (java.security.Key)3