blob: d43c616a4f13b082c443750f1fe82aa16080c713 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.net.http;
18
Artur Satayev26958002019-12-10 17:47:52 +000019import android.compat.annotation.UnsupportedAppUsage;
Mathew Inwood31755f92018-12-20 13:53:36 +000020import android.os.Build;
Artur Satayev26958002019-12-10 17:47:52 +000021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import java.security.cert.X509Certificate;
23
24/**
Steve Block9e334db2011-10-03 18:59:18 +010025 * This class represents a set of one or more SSL errors and the associated SSL
26 * certificate.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 */
28public 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 Monsen1abd5b32011-07-12 22:59:15 +010037 public static final int SSL_NOTYETVALID = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 /**
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 Monsen1abd5b32011-07-12 22:59:15 +010050 /**
51 * The date of the certificate is invalid
52 */
53 public static final int SSL_DATE_INVALID = 4;
54 /**
Steve Block9e334db2011-10-03 18:59:18 +010055 * A generic error occurred
Kristian Monsen1abd5b32011-07-12 22:59:15 +010056 */
57 public static final int SSL_INVALID = 5;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59
60 /**
Steve Block9e334db2011-10-03 18:59:18 +010061 * The number of different SSL errors.
Kristian Monsen1abd5b32011-07-12 22:59:15 +010062 * @deprecated This constant is not necessary for using the SslError API and
63 * can change from release to release.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 */
Steve Block9e334db2011-10-03 18:59:18 +010065 // Update if you add a new SSL error!!!
Kristian Monsen1abd5b32011-07-12 22:59:15 +010066 @Deprecated
67 public static final int SSL_MAX_ERROR = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090070 * The SSL error set bitfield (each individual error is a bit index;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * multiple individual errors can be OR-ed)
72 */
Mathew Inwood31755f92018-12-20 13:53:36 +000073 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 int mErrors;
75
76 /**
77 * The SSL certificate associated with the error set
78 */
Mathew Inwood53f089f2018-08-08 14:44:44 +010079 @UnsupportedAppUsage
Huahui Wu5bb9b192011-06-28 10:17:45 -070080 final SslCertificate mCertificate;
81
82 /**
83 * The URL associated with the error set.
84 */
Mathew Inwood31755f92018-12-20 13:53:36 +000085 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Huahui Wu5bb9b192011-06-28 10:17:45 -070086 final String mUrl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88 /**
Steve Block9e334db2011-10-03 18:59:18 +010089 * 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 Project9066cfe2009-03-03 19:31:44 -080091 * @param error The SSL error
92 * @param certificate The associated SSL certificate
Huahui Wu5bb9b192011-06-28 10:17:45 -070093 * @deprecated Use {@link #SslError(int, SslCertificate, String)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
Huahui Wu5bb9b192011-06-28 10:17:45 -070095 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public SslError(int error, SslCertificate certificate) {
Steve Block9e334db2011-10-03 18:59:18 +010097 this(error, certificate, "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 }
99
100 /**
Steve Block9e334db2011-10-03 18:59:18 +0100101 * 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 Project9066cfe2009-03-03 19:31:44 -0800103 * @param error The SSL error
104 * @param certificate The associated SSL certificate
Huahui Wu5bb9b192011-06-28 10:17:45 -0700105 * @deprecated Use {@link #SslError(int, X509Certificate, String)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
Huahui Wu5bb9b192011-06-28 10:17:45 -0700107 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public SslError(int error, X509Certificate certificate) {
Steve Block9e334db2011-10-03 18:59:18 +0100109 this(error, certificate, "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
111
112 /**
Steve Block9e334db2011-10-03 18:59:18 +0100113 * Creates a new SslError object using the supplied error, certificate and
114 * URL.
Huahui Wu5bb9b192011-06-28 10:17:45 -0700115 * @param error The SSL error
116 * @param certificate The associated SSL certificate
Steve Block9e334db2011-10-03 18:59:18 +0100117 * @param url The associated URL
Huahui Wu5bb9b192011-06-28 10:17:45 -0700118 */
119 public SslError(int error, SslCertificate certificate, String url) {
Steve Block9e334db2011-10-03 18:59:18 +0100120 assert certificate != null;
121 assert url != null;
Huahui Wu5bb9b192011-06-28 10:17:45 -0700122 addError(error);
Huahui Wu5bb9b192011-06-28 10:17:45 -0700123 mCertificate = certificate;
Huahui Wu5bb9b192011-06-28 10:17:45 -0700124 mUrl = url;
125 }
126
127 /**
Steve Block9e334db2011-10-03 18:59:18 +0100128 * 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 Monsen1abd5b32011-07-12 22:59:15 +0100139 * 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 Block9e334db2011-10-03 18:59:18 +0100149 assert (error >= -299 && error <= -200);
Kristian Monsen1abd5b32011-07-12 22:59:15 +0100150 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 Block9e334db2011-10-03 18:59:18 +0100156 // Map all other codes to SSL_INVALID.
Kristian Monsen1abd5b32011-07-12 22:59:15 +0100157 return new SslError(SSL_INVALID, cert, url);
158 }
159
160 /**
Steve Block9e334db2011-10-03 18:59:18 +0100161 * Gets the SSL certificate associated with this object.
162 * @return The SSL certificate, non-null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 */
164 public SslCertificate getCertificate() {
165 return mCertificate;
166 }
167
168 /**
Steve Block9e334db2011-10-03 18:59:18 +0100169 * Gets the URL associated with this object.
170 * @return The URL, non-null.
Huahui Wu5bb9b192011-06-28 10:17:45 -0700171 */
172 public String getUrl() {
173 return mUrl;
174 }
175
176 /**
Steve Block9e334db2011-10-03 18:59:18 +0100177 * Adds the supplied SSL error to the set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * @param error The SSL error to add
Steve Block9e334db2011-10-03 18:59:18 +0100179 * @return True if the error being added is a known SSL error, otherwise
180 * false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
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 Block9e334db2011-10-03 18:59:18 +0100192 * 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 Project9066cfe2009-03-03 19:31:44 -0800195 */
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 Block9e334db2011-10-03 18:59:18 +0100206 * Gets the most severe SSL error in this object's set of errors.
Steve Block518f72c2011-10-05 18:48:09 +0100207 * 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 Project9066cfe2009-03-03 19:31:44 -0800209 */
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 Block518f72c2011-10-05 18:48:09 +0100218 // mErrors should never be set to an invalid value.
219 assert false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221
Steve Block518f72c2011-10-05 18:48:09 +0100222 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
224
225 /**
Steve Block9e334db2011-10-03 18:59:18 +0100226 * Returns a string representation of this object.
227 * @return A String representation of this object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
229 public String toString() {
230 return "primary error: " + getPrimaryError() +
Steve Block9e334db2011-10-03 18:59:18 +0100231 " certificate: " + getCertificate() +
232 " on URL: " + getUrl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234}