Path: blob/master/test/jdk/java/security/cert/pkix/nameConstraintsMinMax/VerifyNameConstraints.java
41154 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 445877826* @summary verify name constraints check for min and max fields27*/2829import java.io.ByteArrayOutputStream;30import java.io.File;31import java.io.FileInputStream;32import java.io.InputStream;33import java.io.IOException;3435import java.security.cert.CertificateFactory;36import java.security.cert.CertPath;37import java.security.cert.CertPathValidator;38import java.security.cert.CertPathValidatorException;39import java.security.cert.CertPathValidatorResult;40import java.security.cert.PKIXParameters;41import java.security.cert.TrustAnchor;42import java.security.cert.X509Certificate;4344import java.util.ArrayList;45import java.util.Collections;46import java.util.List;47import java.util.Set;4849public final class VerifyNameConstraints {5051private static PKIXParameters params;52private static CertPath path;5354public static void main(String[] args) throws Exception {5556String[] certs = { "sun.cer", "sun2labs2.cer", "labs2isrg2.cer" };57try {58createPath(certs);59validate(path, params);60throw new Exception61("CertPath should not have been validated succesfully");62} catch (CertPathValidatorException cve) {63System.out.println("Test failed as expected: " + cve);64}65}6667public static void createPath(String[] certs) throws Exception {68TrustAnchor anchor = new TrustAnchor(getCertFromFile(certs[0]), null);69List list = new ArrayList();70for (int i = 1; i < certs.length; i++) {71list.add(0, getCertFromFile(certs[i]));72}73CertificateFactory cf = CertificateFactory.getInstance("X509");74path = cf.generateCertPath(list);7576Set anchors = Collections.singleton(anchor);77params = new PKIXParameters(anchors);78params.setRevocationEnabled(false);79}8081/*82* Reads the entire input stream into a byte array.83*/84private static byte[] getTotalBytes(InputStream is) throws IOException {85byte[] buffer = new byte[8192];86ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);87int n;88baos.reset();89while ((n = is.read(buffer, 0, buffer.length)) != -1) {90baos.write(buffer, 0, n);91}92return baos.toByteArray();93}9495/**96* Get a DER-encoded X.509 certificate from a file.97*98* @param certFilePath path to file containing DER-encoded certificate99* @return X509Certificate100* @throws IOException on error101*/102public static X509Certificate getCertFromFile(String certFilePath)103throws IOException {104X509Certificate cert = null;105try {106File certFile = new File(System.getProperty("test.src", "."),107certFilePath);108FileInputStream certFileInputStream =109new FileInputStream(certFile);110CertificateFactory cf = CertificateFactory.getInstance("X509");111cert = (X509Certificate)112cf.generateCertificate(certFileInputStream);113} catch (Exception e) {114e.printStackTrace();115throw new IOException("Can't construct X509Certificate: " +116e.getMessage());117}118return cert;119}120121/**122* Perform a PKIX validation. On success, print the123* CertPathValidatorResult on System.out. On failure,124* throw an exception.125*126* @param path CertPath to validate127* @param params PKIXParameters to use in validation128* @throws Exception on error129*/130public static void validate(CertPath path, PKIXParameters params)131throws Exception {132CertPathValidator validator =133CertPathValidator.getInstance("PKIX");134CertPathValidatorResult cpvr = validator.validate(path, params);135}136}137138139