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. |
4 | |
5 | package org.chromium.net; |
6 | |
7 | import android.content.Context; |
8 | import android.content.Intent; |
9 | import android.net.ConnectivityManager; |
10 | import android.telephony.TelephonyManager; |
11 | import android.test.UiThreadTest; |
12 | import android.test.suitebuilder.annotation.MediumTest; |
13 | import android.test.InstrumentationTestCase; |
14 | import android.util.Base64; |
15 | |
16 | import java.io.BufferedReader; |
17 | import java.io.FileReader; |
18 | import java.io.IOException; |
19 | import java.io.RandomAccessFile; |
20 | import java.security.GeneralSecurityException; |
21 | import java.security.cert.CertificateException; |
22 | import java.security.cert.CertificateParsingException; |
23 | import java.security.cert.X509Certificate; |
24 | import java.util.Arrays; |
25 | |
26 | import org.chromium.base.PathUtils; |
27 | |
28 | /** |
29 | * Tests for org.chromium.net.X509Util. |
30 | */ |
31 | public class X509UtilTest extends InstrumentationTestCase { |
32 | private static final String CERTS_DIRECTORY = |
33 | PathUtils.getExternalStorageDirectory() + "/net/data/ssl/certificates/"; |
34 | private static final String BAD_EKU_TEST_ROOT = "eku-test-root.pem"; |
35 | private static final String CRITICAL_CODE_SIGNING_EE = "crit-codeSigning-chain.pem"; |
36 | private static final String NON_CRITICAL_CODE_SIGNING_EE = "non-crit-codeSigning-chain.pem"; |
37 | private static final String WEB_CLIENT_AUTH_EE = "invalid_key_usage_cert.der"; |
38 | private static final String OK_CERT = "ok_cert.pem"; |
39 | private static final String GOOD_ROOT_CA = "root_ca_cert.pem"; |
40 | |
41 | private static final String BEGIN_MARKER = "-----BEGIN CERTIFICATE-----"; |
42 | private static final String END_MARKER = "-----END CERTIFICATE-----"; |
43 | |
44 | private static byte[] pemToDer(String pemPathname) throws IOException { |
45 | BufferedReader reader = new BufferedReader(new FileReader(pemPathname)); |
46 | StringBuilder builder = new StringBuilder(); |
47 | |
48 | // Skip past leading junk lines, if any. |
49 | String line = reader.readLine(); |
50 | while (line != null && !line.contains(BEGIN_MARKER)) line = reader.readLine(); |
51 | |
52 | // Then skip the BEGIN_MARKER itself, if present. |
53 | while (line != null && line.contains(BEGIN_MARKER)) line = reader.readLine(); |
54 | |
55 | // Now gather the data lines into the builder. |
56 | while (line != null && !line.contains(END_MARKER)) { |
57 | builder.append(line.trim()); |
58 | line = reader.readLine(); |
59 | } |
60 | |
61 | reader.close(); |
62 | return Base64.decode(builder.toString(), Base64.DEFAULT); |
63 | } |
64 | |
65 | private static byte[] readFileBytes(String pathname) throws IOException { |
66 | RandomAccessFile file = new RandomAccessFile(pathname, "r"); |
67 | byte[] bytes = new byte[(int) file.length()]; |
68 | int bytesRead = file.read(bytes); |
69 | if (bytesRead != bytes.length) |
70 | return Arrays.copyOfRange(bytes, 0, bytesRead); |
71 | return bytes; |
72 | } |
73 | |
74 | @MediumTest |
75 | public void testEkusVerified() throws GeneralSecurityException, IOException { |
76 | X509Util.addTestRootCertificate(pemToDer(CERTS_DIRECTORY + BAD_EKU_TEST_ROOT)); |
77 | X509Util.addTestRootCertificate(pemToDer(CERTS_DIRECTORY + GOOD_ROOT_CA)); |
78 | |
79 | assertFalse(X509Util.verifyKeyUsage( |
80 | X509Util.createCertificateFromBytes( |
81 | pemToDer(CERTS_DIRECTORY + CRITICAL_CODE_SIGNING_EE)))); |
82 | |
83 | assertFalse(X509Util.verifyKeyUsage( |
84 | X509Util.createCertificateFromBytes( |
85 | pemToDer(CERTS_DIRECTORY + NON_CRITICAL_CODE_SIGNING_EE)))); |
86 | |
87 | assertFalse(X509Util.verifyKeyUsage( |
88 | X509Util.createCertificateFromBytes( |
89 | readFileBytes(CERTS_DIRECTORY + WEB_CLIENT_AUTH_EE)))); |
90 | |
91 | assertTrue(X509Util.verifyKeyUsage( |
92 | X509Util.createCertificateFromBytes( |
93 | pemToDer(CERTS_DIRECTORY + OK_CERT)))); |
94 | |
95 | try { |
96 | X509Util.clearTestRootCertificates(); |
97 | } catch (Exception e) { |
98 | fail("Could not clear test root certificates: " + e.toString()); |
99 | } |
100 | } |
101 | } |
102 | |