Search in sources :

Example 66 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project keywhiz by square.

the class ContentCryptographer method computeHmac.

public String computeHmac(byte[] data) {
    SecretKey hmacKey = deriveKey(32, "hmackey");
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(hmacKey);
        return BaseEncoding.base16().encode(mac.doFinal(data));
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        logger.warn("Error computing HMAC: ", e);
        return null;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 67 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project keywhiz by square.

the class ContentCryptographer method gcm.

private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
    try {
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
        SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
        GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
        cipher.init(mode.cipherMode, derivedKey, gcmParameters);
        return cipher.doFinal(data);
    } catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
        throw Throwables.propagate(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 68 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project keywhiz by square.

the class ExpirationExtractor method expirationFromKeystore.

@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
    KeyStore ks;
    try {
        ks = KeyStore.getInstance(type);
    } catch (KeyStoreException e) {
        // Should never occur (assuming JCE is installed)
        throw Throwables.propagate(e);
    }
    try {
        ks.load(new ByteArrayInputStream(content), password.toCharArray());
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        // Failed to parse
        logger.info("Failed to parse keystore", e);
        return null;
    }
    Instant earliest = null;
    try {
        for (String alias : list(ks.aliases())) {
            Certificate[] chain = ks.getCertificateChain(alias);
            if (chain == null) {
                Certificate certificate = ks.getCertificate(alias);
                if (certificate == null) {
                    // No certs in this entry
                    continue;
                }
                chain = new Certificate[] { certificate };
            }
            for (Certificate cert : chain) {
                if (cert instanceof X509Certificate) {
                    X509Certificate c = (X509Certificate) cert;
                    if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
                        earliest = c.getNotAfter().toInstant();
                    }
                }
            }
        }
    } catch (KeyStoreException e) {
        // Should never occur (ks was initialized)
        throw Throwables.propagate(e);
    }
    return earliest;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Nullable(javax.annotation.Nullable)

Example 69 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project keywhiz by square.

the class HttpClients method testSslClient.

/**
   * Create a {@link OkHttpClient} for tests.
   *
   * @param keyStore Use a client certificate from keystore if present. Client certs disabled if null.
   * @param keyStorePassword keyStore password. Client certs disabled if null.
   * @param requestInterceptors Any request interceptors to register with client.
   * @return new http client
   */
private static OkHttpClient testSslClient(@Nullable KeyStore keyStore, @Nullable String keyStorePassword, KeyStore trustStore, List<Interceptor> requestInterceptors) {
    boolean usingClientCert = keyStore != null && keyStorePassword != null;
    SSLContext sslContext;
    try {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(trustStore);
        if (usingClientCert) {
            sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword.toCharArray());
        }
        sslContext = sslContextBuilder.build();
    } catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException | KeyManagementException e) {
        throw Throwables.propagate(e);
    }
    OkHttpClient.Builder client = new OkHttpClient().newBuilder().sslSocketFactory(sslContext.getSocketFactory()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).followSslRedirects(false);
    client.followRedirects(false);
    client.retryOnConnectionFailure(false);
    // Won't use cookies and a client certificate at once.
    if (!usingClientCert) {
        CookieManager cookieManager = new CookieManager();
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        client.cookieJar(new JavaNetCookieJar(cookieManager));
    }
    for (Interceptor interceptor : requestInterceptors) {
        client.networkInterceptors().add(interceptor);
    }
    return client.build();
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) Interceptor(okhttp3.Interceptor) CookieManager(java.net.CookieManager)

Example 70 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project android-common by litesuits.

the class MD5Util method md5.

public static byte[] md5(byte[] bytes) {
    try {
        MessageDigest digest = getDigest("MD5");
        digest.update(bytes);
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1557 MessageDigest (java.security.MessageDigest)590 IOException (java.io.IOException)374 InvalidKeyException (java.security.InvalidKeyException)266 KeyStoreException (java.security.KeyStoreException)200 CertificateException (java.security.cert.CertificateException)163 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)162 UnsupportedEncodingException (java.io.UnsupportedEncodingException)141 KeyManagementException (java.security.KeyManagementException)130 KeyFactory (java.security.KeyFactory)105 NoSuchProviderException (java.security.NoSuchProviderException)102 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)96 SSLContext (javax.net.ssl.SSLContext)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)90 KeyStore (java.security.KeyStore)89 UnrecoverableKeyException (java.security.UnrecoverableKeyException)88 InputStream (java.io.InputStream)82 SecureRandom (java.security.SecureRandom)82 Cipher (javax.crypto.Cipher)79 BadPaddingException (javax.crypto.BadPaddingException)75