blob: 36973f1686943f3f2d75590866e33dbbcbd42020 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Makoto Onuki8f028a92010-01-08 13:34:57 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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 */
Dianne Hackborn2269d1572010-02-24 19:54:22 -080016package com.android.internal.net;
17
Robert Greenwalte5903732011-02-22 16:00:42 -080018import android.net.NetworkUtils;
Makoto Onuki8f028a92010-01-08 13:34:57 -080019import android.util.Config;
20import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.security.cert.CertificateParsingException;
Makoto Onuki8f028a92010-01-08 13:34:57 -080024import java.security.cert.X509Certificate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Collection;
26import java.util.Iterator;
27import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.util.regex.Pattern;
29import java.util.regex.PatternSyntaxException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Makoto Onuki8f028a92010-01-08 13:34:57 -080031import javax.security.auth.x500.X500Principal;
32
Dianne Hackborn2269d1572010-02-24 19:54:22 -080033/** @hide */
Makoto Onuki8f028a92010-01-08 13:34:57 -080034public class DomainNameValidator {
35 private final static String TAG = "DomainNameValidator";
36
37 private static final boolean DEBUG = false;
38 private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 private static final int ALT_DNS_NAME = 2;
41 private static final int ALT_IPA_NAME = 7;
42
43 /**
44 * Checks the site certificate against the domain name of the site being visited
45 * @param certificate The certificate to check
46 * @param thisDomain The domain name of the site being visited
47 * @return True iff if there is a domain match as specified by RFC2818
48 */
49 public static boolean match(X509Certificate certificate, String thisDomain) {
50 if (certificate == null || thisDomain == null || thisDomain.length() == 0) {
51 return false;
52 }
53
54 thisDomain = thisDomain.toLowerCase();
55 if (!isIpAddress(thisDomain)) {
56 return matchDns(certificate, thisDomain);
57 } else {
58 return matchIpAddress(certificate, thisDomain);
59 }
60 }
61
62 /**
63 * @return True iff the domain name is specified as an IP address
64 */
65 private static boolean isIpAddress(String domain) {
66 boolean rval = (domain != null && domain.length() != 0);
67 if (rval) {
68 try {
69 // do a quick-dirty IP match first to avoid DNS lookup
Robert Greenwalte5903732011-02-22 16:00:42 -080070 rval = domain.equals(
71 NetworkUtils.numericToInetAddress(domain).getHostAddress());
72 } catch (IllegalArgumentException e) {
Makoto Onuki8f028a92010-01-08 13:34:57 -080073 if (LOG_ENABLED) {
Robert Greenwalte5903732011-02-22 16:00:42 -080074 Log.v(TAG, "DomainNameValidator.isIpAddress(): " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 }
76
77 rval = false;
78 }
79 }
80
81 return rval;
82 }
83
84 /**
85 * Checks the site certificate against the IP domain name of the site being visited
86 * @param certificate The certificate to check
87 * @param thisDomain The DNS domain name of the site being visited
88 * @return True iff if there is a domain match as specified by RFC2818
89 */
90 private static boolean matchIpAddress(X509Certificate certificate, String thisDomain) {
Makoto Onuki8f028a92010-01-08 13:34:57 -080091 if (LOG_ENABLED) {
92 Log.v(TAG, "DomainNameValidator.matchIpAddress(): this domain: " + thisDomain);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94
95 try {
96 Collection subjectAltNames = certificate.getSubjectAlternativeNames();
97 if (subjectAltNames != null) {
98 Iterator i = subjectAltNames.iterator();
99 while (i.hasNext()) {
100 List altNameEntry = (List)(i.next());
101 if (altNameEntry != null && 2 <= altNameEntry.size()) {
102 Integer altNameType = (Integer)(altNameEntry.get(0));
103 if (altNameType != null) {
104 if (altNameType.intValue() == ALT_IPA_NAME) {
105 String altName = (String)(altNameEntry.get(1));
106 if (altName != null) {
Makoto Onuki8f028a92010-01-08 13:34:57 -0800107 if (LOG_ENABLED) {
108 Log.v(TAG, "alternative IP: " + altName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 }
110 if (thisDomain.equalsIgnoreCase(altName)) {
111 return true;
112 }
113 }
114 }
115 }
116 }
117 }
118 }
119 } catch (CertificateParsingException e) {}
120
121 return false;
122 }
123
124 /**
125 * Checks the site certificate against the DNS domain name of the site being visited
126 * @param certificate The certificate to check
127 * @param thisDomain The DNS domain name of the site being visited
128 * @return True iff if there is a domain match as specified by RFC2818
129 */
130 private static boolean matchDns(X509Certificate certificate, String thisDomain) {
131 boolean hasDns = false;
132 try {
133 Collection subjectAltNames = certificate.getSubjectAlternativeNames();
134 if (subjectAltNames != null) {
135 Iterator i = subjectAltNames.iterator();
136 while (i.hasNext()) {
137 List altNameEntry = (List)(i.next());
138 if (altNameEntry != null && 2 <= altNameEntry.size()) {
139 Integer altNameType = (Integer)(altNameEntry.get(0));
140 if (altNameType != null) {
141 if (altNameType.intValue() == ALT_DNS_NAME) {
142 hasDns = true;
143 String altName = (String)(altNameEntry.get(1));
144 if (altName != null) {
145 if (matchDns(thisDomain, altName)) {
146 return true;
147 }
148 }
149 }
150 }
151 }
152 }
153 }
154 } catch (CertificateParsingException e) {
Makoto Onuki2683c6f2010-01-20 09:56:01 -0800155 String errorMessage = e.getMessage();
156 if (errorMessage == null) {
157 errorMessage = "failed to parse certificate";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
Makoto Onuki2683c6f2010-01-20 09:56:01 -0800159
160 Log.w(TAG, "DomainNameValidator.matchDns(): " + errorMessage);
161 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
163
164 if (!hasDns) {
Makoto Onuki8f028a92010-01-08 13:34:57 -0800165 final String cn = new DNParser(certificate.getSubjectX500Principal())
166 .find("cn");
167 if (LOG_ENABLED) {
168 Log.v(TAG, "Validating subject: DN:"
169 + certificate.getSubjectX500Principal().getName(X500Principal.CANONICAL)
170 + " CN:" + cn);
171 }
172 if (cn != null) {
173 return matchDns(thisDomain, cn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175 }
176
177 return false;
178 }
179
180 /**
181 * @param thisDomain The domain name of the site being visited
182 * @param thatDomain The domain name from the certificate
183 * @return True iff thisDomain matches thatDomain as specified by RFC2818
184 */
Makoto Onuki8f028a92010-01-08 13:34:57 -0800185 // not private for testing
186 public static boolean matchDns(String thisDomain, String thatDomain) {
187 if (LOG_ENABLED) {
188 Log.v(TAG, "DomainNameValidator.matchDns():" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 " this domain: " + thisDomain +
190 " that domain: " + thatDomain);
191 }
192
193 if (thisDomain == null || thisDomain.length() == 0 ||
194 thatDomain == null || thatDomain.length() == 0) {
195 return false;
196 }
197
198 thatDomain = thatDomain.toLowerCase();
199
200 // (a) domain name strings are equal, ignoring case: X matches X
201 boolean rval = thisDomain.equals(thatDomain);
202 if (!rval) {
203 String[] thisDomainTokens = thisDomain.split("\\.");
204 String[] thatDomainTokens = thatDomain.split("\\.");
205
206 int thisDomainTokensNum = thisDomainTokens.length;
207 int thatDomainTokensNum = thatDomainTokens.length;
208
209 // (b) OR thatHost is a '.'-suffix of thisHost: Z.Y.X matches X
210 if (thisDomainTokensNum >= thatDomainTokensNum) {
211 for (int i = thatDomainTokensNum - 1; i >= 0; --i) {
212 rval = thisDomainTokens[i].equals(thatDomainTokens[i]);
213 if (!rval) {
214 // (c) OR we have a special *-match:
Grace Kloba106f9b22010-01-05 10:50:39 -0800215 // *.Y.X matches Z.Y.X but *.X doesn't match Z.Y.X
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 rval = (i == 0 && thisDomainTokensNum == thatDomainTokensNum);
217 if (rval) {
218 rval = thatDomainTokens[0].equals("*");
219 if (!rval) {
220 // (d) OR we have a *-component match:
221 // f*.com matches foo.com but not bar.com
222 rval = domainTokenMatch(
223 thisDomainTokens[0], thatDomainTokens[0]);
224 }
225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 break;
227 }
228 }
Grace Kloba106f9b22010-01-05 10:50:39 -0800229 } else {
230 // (e) OR thatHost has a '*.'-prefix of thisHost:
231 // *.Y.X matches Y.X
232 rval = thatDomain.equals("*." + thisDomain);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234 }
235
236 return rval;
237 }
238
239 /**
240 * @param thisDomainToken The domain token from the current domain name
241 * @param thatDomainToken The domain token from the certificate
242 * @return True iff thisDomainToken matches thatDomainToken, using the
243 * wildcard match as specified by RFC2818-3.1. For example, f*.com must
244 * match foo.com but not bar.com
245 */
246 private static boolean domainTokenMatch(String thisDomainToken, String thatDomainToken) {
247 if (thisDomainToken != null && thatDomainToken != null) {
248 int starIndex = thatDomainToken.indexOf('*');
249 if (starIndex >= 0) {
250 if (thatDomainToken.length() - 1 <= thisDomainToken.length()) {
251 String prefix = thatDomainToken.substring(0, starIndex);
252 String suffix = thatDomainToken.substring(starIndex + 1);
253
254 return thisDomainToken.startsWith(prefix) && thisDomainToken.endsWith(suffix);
255 }
256 }
257 }
258
259 return false;
260 }
261}