Path: blob/master/test/jdk/java/security/cert/CertPathBuilder/zeroLengthPath/ZeroLengthPath.java
41161 views
/*1* Copyright (c) 2012, 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 4442566 717632626* @summary check that we can build and validate a zero-length27* certpath when a trust anchor cert satisfies the target constraints28*/29import java.io.ByteArrayInputStream;30import java.security.cert.*;31import java.util.Collections;3233public class ZeroLengthPath {3435private static final String ANCHOR =36"-----BEGIN CERTIFICATE-----\n" +37"MIIBFzCBwgIBATANBgkqhkiG9w0BAQQFADAXMRUwEwYDVQQDEwxUcnVzdCBBbmNo\n" +38"b3IwHhcNMDIxMTA3MTE1NzAzWhcNMjIxMTA3MTE1NzAzWjAXMRUwEwYDVQQDEwxU\n" +39"cnVzdCBBbmNob3IwXDANBgkqhkiG9w0BAQEFAANLADBIAkEA9uCj12hwDgC1n9go\n" +40"0ozQAVMM+DfX0vpKOemyGNp+ycSLfAq3pxBcUKbQhjSRL7YjPkEL8XC6pRLwyEoF\n" +41"osWweQIDAQABMA0GCSqGSIb3DQEBBAUAA0EAzZta5M1qbbozj7jWnNyTgB4HUpzv\n" +42"4eP0VYQb1pQY1/xEMczaRt+RuoIDnHCq5a1vOiwk6ZbdG6GlJKx9lj0oMQ==\n" +43"-----END CERTIFICATE-----";444546public static void main(String[] args) throws Exception {4748ByteArrayInputStream is = new ByteArrayInputStream(ANCHOR.getBytes());49CertificateFactory cf = CertificateFactory.getInstance("X.509");50X509Certificate cert = (X509Certificate)cf.generateCertificate(is);5152X509CertSelector xcs = new X509CertSelector();53xcs.setSubject(cert.getSubjectX500Principal().getName());54PKIXBuilderParameters p = new PKIXBuilderParameters55(Collections.singleton(new TrustAnchor(cert, null)), xcs);56CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");57CertPath cp = buildCertPath(cpb, p);58validateCertPath(cp, p);59}6061private static CertPath buildCertPath(CertPathBuilder cpb,62PKIXBuilderParameters params)63throws Exception64{65CertPathBuilderResult res = cpb.build(params);66if (res.getCertPath().getCertificates().size() != 0) {67throw new Exception("built path is not zero-length");68}69return res.getCertPath();70}7172private static void validateCertPath(CertPath cp, PKIXParameters params)73throws Exception74{75CertPathValidator cpv = CertPathValidator.getInstance("PKIX");76CertPathValidatorResult cpvr = cpv.validate(cp, params);77}78}798081