1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import android.net.http.SslCertificate;
8 import android.net.http.SslError;
9 import android.util.Log;
11 import org.chromium.net.NetError;
12 import org.chromium.net.X509Util;
14 import java.security.KeyStoreException;
15 import java.security.NoSuchAlgorithmException;
16 import java.security.cert.CertificateException;
17 import java.security.cert.X509Certificate;
20 private static final String TAG = "SslUtil";
23 * Creates an SslError object from a chromium net error code.
25 public static SslError sslErrorFromNetErrorCode(int error, SslCertificate cert, String url) {
26 assert (error >= NetError.ERR_CERT_END && error <= NetError.ERR_CERT_COMMON_NAME_INVALID);
28 case NetError.ERR_CERT_COMMON_NAME_INVALID:
29 return new SslError(SslError.SSL_IDMISMATCH, cert, url);
30 case NetError.ERR_CERT_DATE_INVALID:
31 return new SslError(SslError.SSL_DATE_INVALID, cert, url);
32 case NetError.ERR_CERT_AUTHORITY_INVALID:
33 return new SslError(SslError.SSL_UNTRUSTED, cert, url);
37 // Map all other codes to SSL_INVALID.
38 return new SslError(SslError.SSL_INVALID, cert, url);
41 public static SslCertificate getCertificateFromDerBytes(byte[] derBytes) {
42 if (derBytes == null) {
47 X509Certificate x509Certificate =
48 X509Util.createCertificateFromBytes(derBytes);
49 return new SslCertificate(x509Certificate);
50 } catch (CertificateException e) {
51 // A SSL related exception must have occured. This shouldn't happen.
52 Log.w(TAG, "Could not read certificate: " + e);
53 } catch (KeyStoreException e) {
54 // A SSL related exception must have occured. This shouldn't happen.
55 Log.w(TAG, "Could not read certificate: " + e);
56 } catch (NoSuchAlgorithmException e) {
57 // A SSL related exception must have occured. This shouldn't happen.
58 Log.w(TAG, "Could not read certificate: " + e);