Path: blob/master/test/jdk/java/security/cert/CertPathValidator/targetConstraints/ValidateTargetConstraints.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 that target constraints are processed correctly27* by a PKIX CertPathValidator28*/2930import java.io.File;31import java.io.FileInputStream;32import java.io.IOException;3334import java.math.BigInteger;3536import java.security.cert.CertificateFactory;37import java.security.cert.CertPath;38import java.security.cert.CertPathValidator;39import java.security.cert.CertPathValidatorException;40import java.security.cert.CertPathValidatorResult;41import java.security.cert.PKIXParameters;42import java.security.cert.TrustAnchor;43import java.security.cert.X509Certificate;44import java.security.cert.X509CertSelector;4546import java.util.ArrayList;47import java.util.Collections;48import java.util.List;49import java.util.Set;5051/**52* ValidateTargetConstraints performs a simple validation of a certification53* path, but adds a requirement that the serial number of the last54* certificate match an arbitrarily chosen number. This should cause the55* validation to fail.56*57* @author Steve Hanna58* @author Sean Mullan59*/60public final class ValidateTargetConstraints {6162private static CertPath path;63private static PKIXParameters params;6465public static void main(String[] args) throws Exception {6667String[] certs = { "sun.cer", "sun2labs1.cer" };6869try {70createPath(certs);71validate(path, params);72throw new Exception73("CertPath should not have been validated succesfully");74} catch (CertPathValidatorException cpve) {75System.out.println("Test failed as expected: " + cpve);76}77}7879public static void createPath(String[] certs) throws Exception {80TrustAnchor anchor = new TrustAnchor(getCertFromFile(certs[0]), null);81List list = new ArrayList();82for (int i = 1; i < certs.length; i++) {83list.add(0, getCertFromFile(certs[i]));84}85CertificateFactory cf = CertificateFactory.getInstance("X509");86path = cf.generateCertPath(list);8788Set anchors = Collections.singleton(anchor);89params = new PKIXParameters(anchors);90params.setRevocationEnabled(false);91X509CertSelector sel = new X509CertSelector();92sel.setSerialNumber(new BigInteger("1427"));93params.setTargetCertConstraints(sel);94}9596/**97* Get a DER-encoded X.509 certificate from a file.98*99* @param certFilePath path to file containing DER-encoded certificate100* @return X509Certificate101* @throws IOException on error102*/103public static X509Certificate getCertFromFile(String certFilePath)104throws IOException {105X509Certificate cert = null;106try {107File certFile = new File(System.getProperty("test.src", "."),108certFilePath);109FileInputStream certFileInputStream =110new FileInputStream(certFile);111CertificateFactory cf = CertificateFactory.getInstance("X509");112cert = (X509Certificate)113cf.generateCertificate(certFileInputStream);114} catch (Exception e) {115e.printStackTrace();116throw new IOException("Can't construct X509Certificate: " +117e.getMessage());118}119return cert;120}121122/**123* Perform a PKIX validation.124*125* @param path CertPath to validate126* @param params PKIXParameters to use in validation127* @throws Exception on error128*/129public static void validate(CertPath path, PKIXParameters params)130throws Exception {131CertPathValidator validator =132CertPathValidator.getInstance("PKIX");133CertPathValidatorResult cpvr = validator.validate(path, params);134}135}136137138