Path: blob/master/test/jdk/java/security/cert/CertPathBuilder/targetConstraints/BuildOddSel.java
41161 views
/*1* Copyright (c) 2001, 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 445953826* @summary make sure a PKIX CertPathBuilder throws an27* InvalidAlgorithmParameterException if the target constraints28* specified in the PKIXBuilderParameters is not an instance of29* X509CertSelector.30*/3132import java.io.File;33import java.io.FileInputStream;34import java.io.IOException;3536import java.security.InvalidAlgorithmParameterException;37import java.security.cert.Certificate;38import java.security.cert.CertificateFactory;39import java.security.cert.CertPathBuilder;40import java.security.cert.CertPathBuilderResult;41import java.security.cert.PKIXBuilderParameters;42import java.security.cert.TrustAnchor;43import java.security.cert.X509Certificate;44import java.security.cert.CertSelector;4546import java.util.Collections;47import java.util.Set;4849/**50* BuildOddSel tries to perform a simple build of a certification path51* using the PKIX algorithm and a bogus target constraints CertSelector52* (one that is not an instance of X509CertSelector). On success, it should53* throw an InvalidAlgorithmParameterException.54*55* @author Steve Hanna56* @author Sean Mullan57*/58public final class BuildOddSel {5960private static PKIXBuilderParameters params;61private static CertSelector sel;6263public static void main(String[] args) throws Exception {6465try {66createParams();67build(params);68throw new Exception69("CertPath should not have been built successfully");70} catch (InvalidAlgorithmParameterException iape) {71}72}7374/**75* CertSelector class that should cause SunCertPathBuilder to76* throw an InvalidAlgorithmParameterException.77*/78static class OddSel implements CertSelector {79public Object clone() {80try {81return super.clone();82} catch (CloneNotSupportedException e) {83throw new UnknownError();84}85}86public boolean match(Certificate cert) {87return(false);88}89}9091public static void createParams() throws Exception {92TrustAnchor anchor = new TrustAnchor(getCertFromFile("sun.cer"), null);93Set anchors = Collections.singleton(anchor);94// Create odd CertSelector95sel = new OddSel();96params = new PKIXBuilderParameters(anchors, sel);97params.setRevocationEnabled(false);98}99100/**101* Get a DER-encoded X.509 certificate from a file.102*103* @param certFilePath path to file containing DER-encoded certificate104* @return X509Certificate105* @throws IOException on error106*/107public static X509Certificate getCertFromFile(String certFilePath)108throws IOException {109X509Certificate cert = null;110try {111File certFile = new File(System.getProperty("test.src", "."),112certFilePath);113FileInputStream certFileInputStream =114new FileInputStream(certFile);115CertificateFactory cf = CertificateFactory.getInstance("X509");116cert = (X509Certificate)117cf.generateCertificate(certFileInputStream);118} catch (Exception e) {119e.printStackTrace();120throw new IOException("Can't construct X509Certificate: " +121e.getMessage());122}123return cert;124}125126/**127* Perform a PKIX build.128*129* @param params PKIXBuilderParameters to use in building130* @throws Exception on error131*/132public static void build(PKIXBuilderParameters params)133throws Exception {134CertPathBuilder builder =135CertPathBuilder.getInstance("PKIX");136CertPathBuilderResult cpbr = builder.build(params);137}138}139140141