Path: blob/master/src/java.base/share/classes/sun/security/util/AnchorCertificates.java
41159 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.io.File;28import java.io.FileInputStream;29import java.security.AccessController;30import java.security.KeyStore;31import java.security.PrivilegedAction;32import java.security.cert.X509Certificate;33import java.util.Collections;34import java.util.Enumeration;35import java.util.HashSet;36import java.util.Set;3738import javax.security.auth.x500.X500Principal;39import sun.security.x509.X509CertImpl;4041/**42* The purpose of this class is to determine the trust anchor certificates is in43* the cacerts file. This is used for PKIX CertPath checking.44*/45public class AnchorCertificates {4647private static final Debug debug = Debug.getInstance("certpath");48private static final String HASH = "SHA-256";49private static Set<String> certs = Collections.emptySet();50private static Set<X500Principal> certIssuers = Collections.emptySet();5152static {53@SuppressWarnings("removal")54var dummy = AccessController.doPrivileged(new PrivilegedAction<>() {55@Override56public Void run() {57File f = new File(FilePaths.cacerts());58try {59KeyStore cacerts;60cacerts = KeyStore.getInstance("JKS");61try (FileInputStream fis = new FileInputStream(f)) {62cacerts.load(fis, null);63certs = new HashSet<>();64certIssuers = new HashSet<>();65Enumeration<String> list = cacerts.aliases();66while (list.hasMoreElements()) {67String alias = list.nextElement();68// Check if this cert is labeled a trust anchor.69if (alias.contains(" [jdk")) {70X509Certificate cert = (X509Certificate) cacerts71.getCertificate(alias);72certs.add(X509CertImpl.getFingerprint(HASH, cert));73certIssuers.add(cert.getSubjectX500Principal());74}75}76}77} catch (Exception e) {78if (debug != null) {79debug.println("Error parsing cacerts");80e.printStackTrace();81}82}83return null;84}85});86}8788/**89* Checks if a certificate is a JDK trust anchor.90*91* @param cert the certificate to check92* @return true if the certificate is a JDK trust anchor93*/94public static boolean contains(X509Certificate cert) {95String key = X509CertImpl.getFingerprint(HASH, cert);96boolean result = certs.contains(key);97if (result && debug != null) {98debug.println("AnchorCertificate.contains: matched " +99cert.getSubjectX500Principal());100}101return result;102}103104/**105* Checks if a JDK trust anchor is the issuer of a certificate.106*107* @param cert the certificate to check108* @return true if the certificate is issued by a trust anchor109*/110public static boolean issuerOf(X509Certificate cert) {111return certIssuers.contains(cert.getIssuerX500Principal());112}113114private AnchorCertificates() {}115}116117118