Search in sources :

Example 96 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Klyph by jonathangerbaud.

the class LicenseValidator method verify.

/**
     * Verifies the response from server and calls appropriate callback method.
     *
     * @param publicKey public key associated with the developer account
     * @param responseCode server response code
     * @param signedData signed data from server
     * @param signature server signature
     */
public void verify(PublicKey publicKey, int responseCode, String signedData, String signature) {
    String userId = null;
    // Skip signature check for unsuccessful requests
    ResponseData data = null;
    if (responseCode == LICENSED || responseCode == NOT_LICENSED || responseCode == LICENSED_OLD_KEY) {
        // Verify signature.
        try {
            Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
            sig.initVerify(publicKey);
            sig.update(signedData.getBytes());
            if (!sig.verify(Base64.decode(signature))) {
                Log.e(TAG, "Signature verification failed.");
                handleInvalidResponse();
                return;
            }
        } catch (NoSuchAlgorithmException e) {
            // This can't happen on an Android compatible device.
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PUBLIC_KEY);
            return;
        } catch (SignatureException e) {
            throw new RuntimeException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Could not Base64-decode signature.");
            handleInvalidResponse();
            return;
        }
        // Parse and validate response.
        try {
            data = ResponseData.parse(signedData);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Could not parse response.");
            handleInvalidResponse();
            return;
        }
        if (data.responseCode != responseCode) {
            Log.e(TAG, "Response codes don't match.");
            handleInvalidResponse();
            return;
        }
        if (data.nonce != mNonce) {
            Log.e(TAG, "Nonce doesn't match.");
            handleInvalidResponse();
            return;
        }
        if (!data.packageName.equals(mPackageName)) {
            Log.e(TAG, "Package name doesn't match.");
            handleInvalidResponse();
            return;
        }
        if (!data.versionCode.equals(mVersionCode)) {
            Log.e(TAG, "Version codes don't match.");
            handleInvalidResponse();
            return;
        }
        // Application-specific user identifier.
        userId = data.userId;
        if (TextUtils.isEmpty(userId)) {
            Log.e(TAG, "User identifier is empty.");
            handleInvalidResponse();
            return;
        }
    }
    switch(responseCode) {
        case LICENSED:
        case LICENSED_OLD_KEY:
            int limiterResponse = mDeviceLimiter.isDeviceAllowed(userId);
            handleResponse(limiterResponse, data);
            break;
        case NOT_LICENSED:
            handleResponse(Policy.NOT_LICENSED, data);
            break;
        case ERROR_CONTACTING_SERVER:
            Log.w(TAG, "Error contacting licensing server.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_SERVER_FAILURE:
            Log.w(TAG, "An error has occurred on the licensing server.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_OVER_QUOTA:
            Log.w(TAG, "Licensing server is refusing to talk to this device, over quota.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_INVALID_PACKAGE_NAME:
            handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PACKAGE_NAME);
            break;
        case ERROR_NON_MATCHING_UID:
            handleApplicationError(LicenseCheckerCallback.ERROR_NON_MATCHING_UID);
            break;
        case ERROR_NOT_MARKET_MANAGED:
            handleApplicationError(LicenseCheckerCallback.ERROR_NOT_MARKET_MANAGED);
            break;
        default:
            Log.e(TAG, "Unknown response code for license check.");
            handleInvalidResponse();
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 97 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project jjwt by jwtk.

the class RsaProvider method generateKeyPair.

/**
     * Generates a new secure-random key pair of the specified size using the specified SecureRandom according to the
     * specified {@code jcaAlgorithmName}.
     *
     * @param jcaAlgorithmName the name of the JCA algorithm to use for key pair generation, for example, {@code RSA}.
     * @param keySizeInBits    the key size in bits (<em>NOT bytes</em>)
     * @param random           the SecureRandom generator to use during key generation.
     * @return a new secure-randomly generated key pair of the specified size using the specified SecureRandom according
     * to the specified {@code jcaAlgorithmName}.
     * @see #generateKeyPair()
     * @see #generateKeyPair(int)
     * @see #generateKeyPair(int, SecureRandom)
     * @since 0.5
     */
protected static KeyPair generateKeyPair(String jcaAlgorithmName, int keySizeInBits, SecureRandom random) {
    KeyPairGenerator keyGenerator;
    try {
        keyGenerator = KeyPairGenerator.getInstance(jcaAlgorithmName);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Unable to obtain an RSA KeyPairGenerator: " + e.getMessage(), e);
    }
    keyGenerator.initialize(keySizeInBits, random);
    return keyGenerator.genKeyPair();
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 98 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project k-9 by k9mail.

the class WebDavStore method getHttpClient.

public WebDavHttpClient getHttpClient() throws MessagingException {
    if (httpClient == null) {
        httpClient = httpClientFactory.create();
        // Disable automatic redirects on the http client.
        httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);
        // Setup a cookie store for forms-based authentication.
        httpContext = new BasicHttpContext();
        authCookies = new BasicCookieStore();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies);
        SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
        try {
            Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443);
            reg.register(s);
        } catch (NoSuchAlgorithmException nsa) {
            Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa);
            throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa);
        } catch (KeyManagementException kme) {
            Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme);
            throw new MessagingException("KeyManagementException in getHttpClient: " + kme);
        }
    }
    return httpClient;
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Scheme(org.apache.http.conn.scheme.Scheme) MessagingException(com.fsck.k9.mail.MessagingException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Example 99 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project adbcj by mheath.

the class PasswordHash method hashPasswordMD5.

public static byte[] hashPasswordMD5(String user, String password, byte[] salt) {
    try {
        Charset charset = Charset.forName("US-ASCII");
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes(charset));
        md.update(user.getBytes(charset));
        byte[] userPasswordDigest = md.digest();
        byte[] userPassword = new byte[userPasswordDigest.length * 2];
        digestToHex(userPasswordDigest, userPassword, 0);
        md.update(userPassword);
        md.update(salt);
        byte[] digest = md.digest();
        byte[] hex = new byte[userPasswordDigest.length * 2 + 3];
        hex[0] = 'm';
        hex[1] = 'd';
        hex[2] = '5';
        digestToHex(digest, hex, 3);
        return hex;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
Also used : Charset(java.nio.charset.Charset) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 100 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project okhttputils by hongyangAndroid.

the class HttpsUtils method prepareTrustManager.

private static TrustManager[] prepareTrustManager(InputStream... certificates) {
    if (certificates == null || certificates.length <= 0)
        return null;
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);
        int index = 0;
        for (InputStream certificate : certificates) {
            String certificateAlias = Integer.toString(index++);
            keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
            try {
                if (certificate != null)
                    certificate.close();
            } catch (IOException e) {
            }
        }
        TrustManagerFactory trustManagerFactory = null;
        trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        return trustManagers;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1403 MessageDigest (java.security.MessageDigest)548 IOException (java.io.IOException)328 InvalidKeyException (java.security.InvalidKeyException)242 KeyStoreException (java.security.KeyStoreException)168 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)145 CertificateException (java.security.cert.CertificateException)138 UnsupportedEncodingException (java.io.UnsupportedEncodingException)131 KeyManagementException (java.security.KeyManagementException)105 KeyFactory (java.security.KeyFactory)96 NoSuchProviderException (java.security.NoSuchProviderException)93 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)79 UnrecoverableKeyException (java.security.UnrecoverableKeyException)78 KeyStore (java.security.KeyStore)73 SecureRandom (java.security.SecureRandom)72 SSLContext (javax.net.ssl.SSLContext)72 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)69 BadPaddingException (javax.crypto.BadPaddingException)69 Cipher (javax.crypto.Cipher)69