CertBlacklist.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1/* 2 * Copyright (C) 2012 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 org.bouncycastle.jce.provider; 18 19import java.io.Closeable; 20import java.io.ByteArrayOutputStream; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.io.RandomAccessFile; 24import java.math.BigInteger; 25import java.security.PublicKey; 26import java.util.Arrays; 27import java.util.Collections; 28import java.util.HashSet; 29import java.util.Set; 30import java.util.logging.Level; 31import java.util.logging.Logger; 32import org.bouncycastle.crypto.Digest; 33import org.bouncycastle.crypto.digests.AndroidDigestFactory; 34import org.bouncycastle.util.encoders.Hex; 35 36public class CertBlacklist { 37 38 private static final String ANDROID_DATA = System.getenv("ANDROID_DATA"); 39 private static final String BLACKLIST_ROOT = ANDROID_DATA + "/misc/keychain/"; 40 public static final String DEFAULT_PUBKEY_BLACKLIST_PATH = BLACKLIST_ROOT + "pubkey_blacklist.txt"; 41 public static final String DEFAULT_SERIAL_BLACKLIST_PATH = BLACKLIST_ROOT + "serial_blacklist.txt"; 42 43 private static final Logger logger = Logger.getLogger(CertBlacklist.class.getName()); 44 45 // public for testing 46 public final Set<BigInteger> serialBlacklist; 47 public final Set<byte[]> pubkeyBlacklist; 48 49 public CertBlacklist() { 50 this(DEFAULT_PUBKEY_BLACKLIST_PATH, DEFAULT_SERIAL_BLACKLIST_PATH); 51 } 52 53 /** Test only interface, not for public use */ 54 public CertBlacklist(String pubkeyBlacklistPath, String serialBlacklistPath) { 55 serialBlacklist = readSerialBlackList(serialBlacklistPath); 56 pubkeyBlacklist = readPublicKeyBlackList(pubkeyBlacklistPath); 57 } 58 59 private static boolean isHex(String value) { 60 try { 61 new BigInteger(value, 16); 62 return true; 63 } catch (NumberFormatException e) { 64 logger.log(Level.WARNING, "Could not parse hex value " + value, e); 65 return false; 66 } 67 } 68 69 private static boolean isPubkeyHash(String value) { 70 if (value.length() != 40) { 71 logger.log(Level.WARNING, "Invalid pubkey hash length: " + value.length()); 72 return false; 73 } 74 return isHex(value); 75 } 76 77 private static String readBlacklist(String path) { 78 try { 79 return readFileAsString(path); 80 } catch (FileNotFoundException ignored) { 81 } catch (IOException e) { 82 logger.log(Level.WARNING, "Could not read blacklist", e); 83 } 84 return ""; 85 } 86 87 // From IoUtils.readFileAsString 88 private static String readFileAsString(String path) throws IOException { 89 return readFileAsBytes(path).toString("UTF-8"); 90 } 91 92 // Based on IoUtils.readFileAsBytes 93 private static ByteArrayOutputStream readFileAsBytes(String path) throws IOException { 94 RandomAccessFile f = null; 95 try { 96 f = new RandomAccessFile(path, "r"); 97 ByteArrayOutputStream bytes = new ByteArrayOutputStream((int) f.length()); 98 byte[] buffer = new byte[8192]; 99 while (true) { 100 int byteCount = f.read(buffer); 101 if (byteCount == -1) { 102 return bytes; 103 } 104 bytes.write(buffer, 0, byteCount); 105 } 106 } finally { 107 closeQuietly(f); 108 } 109 } 110 111 // Base on IoUtils.closeQuietly 112 private static void closeQuietly(Closeable closeable) { 113 if (closeable != null) { 114 try { 115 closeable.close(); 116 } catch (RuntimeException rethrown) { 117 throw rethrown; 118 } catch (Exception ignored) { 119 } 120 } 121 } 122 123 private static final Set<BigInteger> readSerialBlackList(String path) { 124 125 // start out with a base set of known bad values 126 Set<BigInteger> bl = new HashSet<BigInteger>(Arrays.asList( 127 // From http://src.chromium.org/viewvc/chrome/trunk/src/net/base/x509_certificate.cc?revision=78748&view=markup 128 // Not a real certificate. For testing only. 129 new BigInteger("077a59bcd53459601ca6907267a6dd1c", 16), 130 new BigInteger("047ecbe9fca55f7bd09eae36e10cae1e", 16), 131 new BigInteger("d8f35f4eb7872b2dab0692e315382fb0", 16), 132 new BigInteger("b0b7133ed096f9b56fae91c874bd3ac0", 16), 133 new BigInteger("9239d5348f40d1695a745470e1f23f43", 16), 134 new BigInteger("e9028b9578e415dc1a710a2b88154447", 16), 135 new BigInteger("d7558fdaf5f1105bb213282b707729a3", 16), 136 new BigInteger("f5c86af36162f13a64f54f6dc9587c06", 16), 137 new BigInteger("392a434f0e07df1f8aa305de34e0c229", 16), 138 new BigInteger("3e75ced46b693021218830ae86a82a71", 16) 139 )); 140 141 // attempt to augment it with values taken from gservices 142 String serialBlacklist = readBlacklist(path); 143 if (!serialBlacklist.equals("")) { 144 for(String value : serialBlacklist.split(",")) { 145 try { 146 bl.add(new BigInteger(value, 16)); 147 } catch (NumberFormatException e) { 148 logger.log(Level.WARNING, "Tried to blacklist invalid serial number " + value, e); 149 } 150 } 151 } 152 153 // whether that succeeds or fails, send it on its merry way 154 return Collections.unmodifiableSet(bl); 155 } 156 157 private static final Set<byte[]> readPublicKeyBlackList(String path) { 158 159 // start out with a base set of known bad values 160 Set<byte[]> bl = new HashSet<byte[]>(Arrays.asList( 161 // From http://src.chromium.org/viewvc/chrome/branches/782/src/net/base/x509_certificate.cc?r1=98750&r2=98749&pathrev=98750 162 // C=NL, O=DigiNotar, CN=DigiNotar Root CA/emailAddress=info@diginotar.nl 163 "410f36363258f30b347d12ce4863e433437806a8".getBytes(), 164 // Subject: CN=DigiNotar Cyber CA 165 // Issuer: CN=GTE CyberTrust Global Root 166 "ba3e7bd38cd7e1e6b9cd4c219962e59d7a2f4e37".getBytes(), 167 // Subject: CN=DigiNotar Services 1024 CA 168 // Issuer: CN=Entrust.net 169 "e23b8d105f87710a68d9248050ebefc627be4ca6".getBytes(), 170 // Subject: CN=DigiNotar PKIoverheid CA Organisatie - G2 171 // Issuer: CN=Staat der Nederlanden Organisatie CA - G2 172 "7b2e16bc39bcd72b456e9f055d1de615b74945db".getBytes(), 173 // Subject: CN=DigiNotar PKIoverheid CA Overheid en Bedrijven 174 // Issuer: CN=Staat der Nederlanden Overheid CA 175 "e8f91200c65cee16e039b9f883841661635f81c5".getBytes(), 176 // From http://src.chromium.org/viewvc/chrome?view=rev&revision=108479 177 // Subject: O=Digicert Sdn. Bhd. 178 // Issuer: CN=GTE CyberTrust Global Root 179 "0129bcd5b448ae8d2496d1c3e19723919088e152".getBytes() 180 )); 181 182 // attempt to augment it with values taken from gservices 183 String pubkeyBlacklist = readBlacklist(path); 184 if (!pubkeyBlacklist.equals("")) { 185 for (String value : pubkeyBlacklist.split(",")) { 186 value = value.trim(); 187 if (isPubkeyHash(value)) { 188 bl.add(value.getBytes()); 189 } else { 190 logger.log(Level.WARNING, "Tried to blacklist invalid pubkey " + value); 191 } 192 } 193 } 194 195 return bl; 196 } 197 198 public boolean isPublicKeyBlackListed(PublicKey publicKey) { 199 byte[] encoded = publicKey.getEncoded(); 200 Digest digest = AndroidDigestFactory.getSHA1(); 201 digest.update(encoded, 0, encoded.length); 202 byte[] out = new byte[digest.getDigestSize()]; 203 digest.doFinal(out, 0); 204 for (byte[] blacklisted : pubkeyBlacklist) { 205 if (Arrays.equals(blacklisted, Hex.encode(out))) { 206 return true; 207 } 208 } 209 return false; 210 } 211 212 public boolean isSerialNumberBlackListed(BigInteger serial) { 213 return serialBlacklist.contains(serial); 214 } 215 216} 217