Path: blob/master/test/jdk/java/security/cert/PolicyNode/GetPolicyQualifiers.java
41153 views
/*1* Copyright (c) 2001, 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 441426326* @summary Make sure PolicyNode.getPolicyQualifiers() returns27* Set of PolicyQualifierInfos.28*/29import java.io.File;30import java.io.FileInputStream;31import java.security.cert.*;32import java.text.DateFormat;33import java.util.Collections;34import java.util.Iterator;35import java.util.List;36import java.util.Locale;37import java.util.Set;3839public class GetPolicyQualifiers {4041public static void main(String[] args) throws Exception {4243CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN");44File f = new File(System.getProperty("test.src", "."), "speech2speech");45X509Certificate mostTrustedCaCert = (X509Certificate)46cf.generateCertificate(new FileInputStream(f));47Set trustAnchors = Collections.singleton(48new TrustAnchor(mostTrustedCaCert, null));49f = new File(System.getProperty("test.src", "."), "speech2eve");50X509Certificate eeCert = (X509Certificate)51cf.generateCertificate(new FileInputStream(f));52CertPathValidator cpv = CertPathValidator.getInstance("PKIX", "SUN");53PKIXParameters params = new PKIXParameters(trustAnchors);54params.setPolicyQualifiersRejected(false);55params.setRevocationEnabled(false);56// Certificates expired on Oct 6th, 202057params.setDate(DateFormat.getDateInstance(DateFormat.MEDIUM,58Locale.US).parse("July 01, 2020"));59List certList = Collections.singletonList(eeCert);60CertPath cp = cf.generateCertPath(certList);61PKIXCertPathValidatorResult result =62(PKIXCertPathValidatorResult) cpv.validate(cp, params);6364PolicyNode policyTree = result.getPolicyTree();65Iterator children = policyTree.getChildren();66PolicyNode child = (PolicyNode) children.next();67Set policyQualifiers = child.getPolicyQualifiers();68Iterator i = policyQualifiers.iterator();69while (i.hasNext()) {70Object next = i.next();71if (!(next instanceof PolicyQualifierInfo))72throw new Exception("not a PolicyQualifierInfo");73}7475params.setPolicyQualifiersRejected(true);76try {77result = (PKIXCertPathValidatorResult) cpv.validate(cp, params);78throw new Exception("Validation of CertPath containing critical " +79"qualifiers should have failed when policyQualifiersRejected " +80"flag is true");81} catch (CertPathValidatorException cpve) {82if (cpve.getReason() != PKIXReason.INVALID_POLICY) {83throw new Exception("unexpected reason: " + cpve.getReason());84}85}86}87}888990