The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.http; |
| 18 | |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 20 | import android.os.Build; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import java.security.cert.X509Certificate; |
| 23 | |
| 24 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 25 | * This class represents a set of one or more SSL errors and the associated SSL |
| 26 | * certificate. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | */ |
| 28 | public class SslError { |
| 29 | |
| 30 | /** |
| 31 | * Individual SSL errors (in the order from the least to the most severe): |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * The certificate is not yet valid |
| 36 | */ |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 37 | public static final int SSL_NOTYETVALID = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | /** |
| 39 | * The certificate has expired |
| 40 | */ |
| 41 | public static final int SSL_EXPIRED = 1; |
| 42 | /** |
| 43 | * Hostname mismatch |
| 44 | */ |
| 45 | public static final int SSL_IDMISMATCH = 2; |
| 46 | /** |
| 47 | * The certificate authority is not trusted |
| 48 | */ |
| 49 | public static final int SSL_UNTRUSTED = 3; |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 50 | /** |
| 51 | * The date of the certificate is invalid |
| 52 | */ |
| 53 | public static final int SSL_DATE_INVALID = 4; |
| 54 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 55 | * A generic error occurred |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 56 | */ |
| 57 | public static final int SSL_INVALID = 5; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | |
| 60 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 61 | * The number of different SSL errors. |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 62 | * @deprecated This constant is not necessary for using the SslError API and |
| 63 | * can change from release to release. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | */ |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 65 | // Update if you add a new SSL error!!! |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 66 | @Deprecated |
| 67 | public static final int SSL_MAX_ERROR = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 70 | * The SSL error set bitfield (each individual error is a bit index; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * multiple individual errors can be OR-ed) |
| 72 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 73 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | int mErrors; |
| 75 | |
| 76 | /** |
| 77 | * The SSL certificate associated with the error set |
| 78 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 79 | @UnsupportedAppUsage |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 80 | final SslCertificate mCertificate; |
| 81 | |
| 82 | /** |
| 83 | * The URL associated with the error set. |
| 84 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 85 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 86 | final String mUrl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 89 | * Creates a new SslError object using the supplied error and certificate. |
| 90 | * The URL will be set to the empty string. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | * @param error The SSL error |
| 92 | * @param certificate The associated SSL certificate |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 93 | * @deprecated Use {@link #SslError(int, SslCertificate, String)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | */ |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 95 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | public SslError(int error, SslCertificate certificate) { |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 97 | this(error, certificate, ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 101 | * Creates a new SslError object using the supplied error and certificate. |
| 102 | * The URL will be set to the empty string. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | * @param error The SSL error |
| 104 | * @param certificate The associated SSL certificate |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 105 | * @deprecated Use {@link #SslError(int, X509Certificate, String)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | */ |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 107 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | public SslError(int error, X509Certificate certificate) { |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 109 | this(error, certificate, ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 113 | * Creates a new SslError object using the supplied error, certificate and |
| 114 | * URL. |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 115 | * @param error The SSL error |
| 116 | * @param certificate The associated SSL certificate |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 117 | * @param url The associated URL |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 118 | */ |
| 119 | public SslError(int error, SslCertificate certificate, String url) { |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 120 | assert certificate != null; |
| 121 | assert url != null; |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 122 | addError(error); |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 123 | mCertificate = certificate; |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 124 | mUrl = url; |
| 125 | } |
| 126 | |
| 127 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 128 | * Creates a new SslError object using the supplied error, certificate and |
| 129 | * URL. |
| 130 | * @param error The SSL error |
| 131 | * @param certificate The associated SSL certificate |
| 132 | * @param url The associated URL |
| 133 | */ |
| 134 | public SslError(int error, X509Certificate certificate, String url) { |
| 135 | this(error, new SslCertificate(certificate), url); |
| 136 | } |
| 137 | |
| 138 | /** |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 139 | * Creates an SslError object from a chromium error code. |
| 140 | * @param error The chromium error code |
| 141 | * @param certificate The associated SSL certificate |
| 142 | * @param url The associated URL. |
| 143 | * @hide chromium error codes only available inside the framework |
| 144 | */ |
| 145 | public static SslError SslErrorFromChromiumErrorCode( |
| 146 | int error, SslCertificate cert, String url) { |
| 147 | // The chromium error codes are in: |
| 148 | // external/chromium/net/base/net_error_list.h |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 149 | assert (error >= -299 && error <= -200); |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 150 | if (error == -200) |
| 151 | return new SslError(SSL_IDMISMATCH, cert, url); |
| 152 | if (error == -201) |
| 153 | return new SslError(SSL_DATE_INVALID, cert, url); |
| 154 | if (error == -202) |
| 155 | return new SslError(SSL_UNTRUSTED, cert, url); |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 156 | // Map all other codes to SSL_INVALID. |
Kristian Monsen | 1abd5b3 | 2011-07-12 22:59:15 +0100 | [diff] [blame] | 157 | return new SslError(SSL_INVALID, cert, url); |
| 158 | } |
| 159 | |
| 160 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 161 | * Gets the SSL certificate associated with this object. |
| 162 | * @return The SSL certificate, non-null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | */ |
| 164 | public SslCertificate getCertificate() { |
| 165 | return mCertificate; |
| 166 | } |
| 167 | |
| 168 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 169 | * Gets the URL associated with this object. |
| 170 | * @return The URL, non-null. |
Huahui Wu | 5bb9b19 | 2011-06-28 10:17:45 -0700 | [diff] [blame] | 171 | */ |
| 172 | public String getUrl() { |
| 173 | return mUrl; |
| 174 | } |
| 175 | |
| 176 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 177 | * Adds the supplied SSL error to the set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | * @param error The SSL error to add |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 179 | * @return True if the error being added is a known SSL error, otherwise |
| 180 | * false. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | */ |
| 182 | public boolean addError(int error) { |
| 183 | boolean rval = (0 <= error && error < SslError.SSL_MAX_ERROR); |
| 184 | if (rval) { |
| 185 | mErrors |= (0x1 << error); |
| 186 | } |
| 187 | |
| 188 | return rval; |
| 189 | } |
| 190 | |
| 191 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 192 | * Determines whether this object includes the supplied error. |
| 193 | * @param error The SSL error to check for |
| 194 | * @return True if this object includes the error, otherwise false. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | */ |
| 196 | public boolean hasError(int error) { |
| 197 | boolean rval = (0 <= error && error < SslError.SSL_MAX_ERROR); |
| 198 | if (rval) { |
| 199 | rval = ((mErrors & (0x1 << error)) != 0); |
| 200 | } |
| 201 | |
| 202 | return rval; |
| 203 | } |
| 204 | |
| 205 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 206 | * Gets the most severe SSL error in this object's set of errors. |
Steve Block | 518f72c | 2011-10-05 18:48:09 +0100 | [diff] [blame] | 207 | * Returns -1 if the set is empty. |
| 208 | * @return The most severe SSL error, or -1 if the set is empty. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | */ |
| 210 | public int getPrimaryError() { |
| 211 | if (mErrors != 0) { |
| 212 | // go from the most to the least severe errors |
| 213 | for (int error = SslError.SSL_MAX_ERROR - 1; error >= 0; --error) { |
| 214 | if ((mErrors & (0x1 << error)) != 0) { |
| 215 | return error; |
| 216 | } |
| 217 | } |
Steve Block | 518f72c | 2011-10-05 18:48:09 +0100 | [diff] [blame] | 218 | // mErrors should never be set to an invalid value. |
| 219 | assert false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Steve Block | 518f72c | 2011-10-05 18:48:09 +0100 | [diff] [blame] | 222 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 226 | * Returns a string representation of this object. |
| 227 | * @return A String representation of this object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | */ |
| 229 | public String toString() { |
| 230 | return "primary error: " + getPrimaryError() + |
Steve Block | 9e334db | 2011-10-03 18:59:18 +0100 | [diff] [blame] | 231 | " certificate: " + getCertificate() + |
| 232 | " on URL: " + getUrl(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
| 234 | } |