Search in sources :

Example 46 with GeneralSecurityException

use of java.security.GeneralSecurityException in project poi by apache.

the class BinaryRC4Decryptor method verifyPassword.

@Override
public boolean verifyPassword(String password) {
    EncryptionVerifier ver = getEncryptionInfo().getVerifier();
    SecretKey skey = generateSecretKey(password, ver);
    try {
        Cipher cipher = initCipherForBlock(null, 0, getEncryptionInfo(), skey, Cipher.DECRYPT_MODE);
        byte[] encryptedVerifier = ver.getEncryptedVerifier();
        byte[] verifier = new byte[encryptedVerifier.length];
        cipher.update(encryptedVerifier, 0, encryptedVerifier.length, verifier);
        setVerifier(verifier);
        byte[] encryptedVerifierHash = ver.getEncryptedVerifierHash();
        byte[] verifierHash = cipher.doFinal(encryptedVerifierHash);
        HashAlgorithm hashAlgo = ver.getHashAlgorithm();
        MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
        byte[] calcVerifierHash = hashAlg.digest(verifier);
        if (Arrays.equals(calcVerifierHash, verifierHash)) {
            setSecretKey(skey);
            return true;
        }
    } catch (GeneralSecurityException e) {
        throw new EncryptedDocumentException(e);
    }
    return false;
}
Also used : SecretKey(javax.crypto.SecretKey) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 47 with GeneralSecurityException

use of java.security.GeneralSecurityException in project karaf by apache.

the class OsgiKeystoreManager method createSSLContext.

public SSLContext createSSLContext(String provider, String protocol, String algorithm, String keyStore, String keyAlias, String trustStore, long timeout) throws GeneralSecurityException {
    if (!this.checkForKeystoresAvailability(keyStore, keyAlias, trustStore, timeout)) {
        throw new GeneralSecurityException("Unable to lookup configured keystore and/or truststore");
    }
    KeystoreInstance keyInstance = getKeystore(keyStore);
    if (keyInstance != null && keyInstance.isKeystoreLocked()) {
        throw new KeystoreIsLocked("Keystore '" + keyStore + "' is locked");
    }
    if (keyInstance != null && keyInstance.isKeyLocked(keyAlias)) {
        throw new KeystoreIsLocked("Key '" + keyAlias + "' in keystore '" + keyStore + "' is locked");
    }
    KeystoreInstance trustInstance = trustStore == null ? null : getKeystore(trustStore);
    if (trustInstance != null && trustInstance.isKeystoreLocked()) {
        throw new KeystoreIsLocked("Keystore '" + trustStore + "' is locked");
    }
    SSLContext context;
    if (provider == null) {
        context = SSLContext.getInstance(protocol);
    } else {
        context = SSLContext.getInstance(protocol, provider);
    }
    context.init(keyInstance == null ? null : keyInstance.getKeyManager(algorithm, keyAlias), trustInstance == null ? null : trustInstance.getTrustManager(algorithm), new SecureRandom());
    return context;
}
Also used : KeystoreIsLocked(org.apache.karaf.jaas.config.KeystoreIsLocked) GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) KeystoreInstance(org.apache.karaf.jaas.config.KeystoreInstance)

Example 48 with GeneralSecurityException

use of java.security.GeneralSecurityException in project spring-boot by spring-projects.

the class TokenValidator method hasValidSignature.

private boolean hasValidSignature(Token token, String key) {
    try {
        PublicKey publicKey = getPublicKey(key);
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(token.getContent());
        return signature.verify(token.getSignature());
    } catch (GeneralSecurityException ex) {
        return false;
    }
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException)

Example 49 with GeneralSecurityException

use of java.security.GeneralSecurityException in project robovm by robovm.

the class GeneralSecurityExceptionTest method testGeneralSecurityException02.

/**
     * Test for <code>GeneralSecurityException(String)</code> constructor
     * Assertion: constructs GeneralSecurityException with detail message msg.
     * Parameter <code>msg</code> is not null.
     */
public void testGeneralSecurityException02() {
    GeneralSecurityException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new GeneralSecurityException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException)

Example 50 with GeneralSecurityException

use of java.security.GeneralSecurityException in project robovm by robovm.

the class GeneralSecurityExceptionTest method testGeneralSecurityException04.

/**
     * Test for <code>GeneralSecurityException(Throwable)</code> constructor
     * Assertion: constructs GeneralSecurityException when <code>cause</code>
     * is null
     */
public void testGeneralSecurityException04() {
    Throwable cause = null;
    GeneralSecurityException tE = new GeneralSecurityException(cause);
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException)

Aggregations

GeneralSecurityException (java.security.GeneralSecurityException)1171 IOException (java.io.IOException)435 Cipher (javax.crypto.Cipher)144 Test (org.junit.Test)136 X509Certificate (java.security.cert.X509Certificate)124 KeyStore (java.security.KeyStore)89 SSLContext (javax.net.ssl.SSLContext)84 SecretKeySpec (javax.crypto.spec.SecretKeySpec)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)72 ArrayList (java.util.ArrayList)72 File (java.io.File)61 InputStream (java.io.InputStream)57 Certificate (java.security.cert.Certificate)57 PublicKey (java.security.PublicKey)53 PrivateKey (java.security.PrivateKey)50 FileInputStream (java.io.FileInputStream)49 BigInteger (java.math.BigInteger)49 SecretKey (javax.crypto.SecretKey)48 IvParameterSpec (javax.crypto.spec.IvParameterSpec)43 SecureRandom (java.security.SecureRandom)42