Path: blob/master/src/java.base/share/classes/sun/security/util/AnchorCertificates.java
67760 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);72String fp =73X509CertImpl.getFingerprint(HASH, cert, debug);74// only add trust anchor if fingerprint can75// be calculated76if (fp != null) {77certs.add(fp);78certIssuers.add(cert.getSubjectX500Principal());79}80}81}82}83} catch (Exception e) {84if (debug != null) {85debug.println("Error parsing cacerts");86e.printStackTrace();87}88}89return null;90}91});92}9394/**95* Checks if a certificate is a JDK trust anchor.96*97* @param cert the certificate to check98* @return true if the certificate is a JDK trust anchor99*/100public static boolean contains(X509Certificate cert) {101String key = X509CertImpl.getFingerprint(HASH, cert, debug);102boolean result = (key == null ? false : certs.contains(key));103if (result && debug != null) {104debug.println("AnchorCertificate.contains: matched " +105cert.getSubjectX500Principal());106}107return result;108}109110/**111* Checks if a JDK trust anchor is the issuer of a certificate.112*113* @param cert the certificate to check114* @return true if the certificate is issued by a trust anchor115*/116public static boolean issuerOf(X509Certificate cert) {117return certIssuers.contains(cert.getIssuerX500Principal());118}119120private AnchorCertificates() {}121}122123124