Path: blob/master/test/jdk/sun/security/ssl/X509TrustManagerImpl/CacertsLimit.java
41152 views
/*1* Copyright (c) 2020, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 820692526* @library /javax/net/ssl/templates27* @summary Support the certificate_authorities extension28*/29import javax.net.ssl.TrustManager;30import javax.net.ssl.TrustManagerFactory;31import javax.net.ssl.X509TrustManager;32import javax.security.auth.x500.X500Principal;33import java.security.KeyStore;34import java.security.cert.X509Certificate;3536public class CacertsLimit {37public static void main(String[] args) throws Exception {38for (String algorithm : new String[] {"SunX509", "PKIX"}) {39CacertsLimit.ensureLimit(algorithm);40}41}4243private static void ensureLimit(String algorithm) throws Exception {44TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);45tmf.init((KeyStore)null);46TrustManager[] tms = tmf.getTrustManagers();4748if (tms == null || tms.length == 0) {49throw new Exception("No default key store used for trust manager");50}5152if (!(tms[0] instanceof X509TrustManager)) {53throw new Exception(54"The trust manger is not an instance of X509TrustManager");55}5657checkLimit(((X509TrustManager)tms[0]).getAcceptedIssuers());58}5960private static void checkLimit(61X509Certificate[] trustedCerts) throws Exception {62int sizeAccount = 0;63for (X509Certificate cert : trustedCerts) {64X500Principal x500Principal = cert.getSubjectX500Principal();65byte[] encodedPrincipal = x500Principal.getEncoded();66sizeAccount += encodedPrincipal.length;67if (sizeAccount > 0xFFFF) {68throw new Exception(69"There are too many trusted CAs in cacerts. The " +70"certificate_authorities extension cannot be used " +71"for TLS connections. Please rethink about the size" +72"of the cacerts, or have a release note for the " +73"impacted behaviors");74} else if (sizeAccount > 0x4000) {75throw new Exception(76"There are too many trusted CAs in cacerts. The " +77"certificate_authorities extension cannot be " +78"packaged in one TLS record, which would result in " +79"interoperability issues. Please rethink about the " +80"size of the cacerts, or have a release note for " +81"the impacted behaviors");82}83}84}85}86878889