Path: blob/master/test/jdk/sun/security/provider/certpath/OCSP/OCSPSingleExtensions.java
41155 views
/*1* Copyright (c) 2015, 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 807406426* @summary OCSPResponse.SingleResponse objects do not parse singleExtensions27* @modules java.base/sun.security.x50928* java.base/sun.security.provider.certpath29* @run main/othervm OCSPSingleExtensions30*/3132import java.io.*;33import java.util.*;34import java.security.cert.*;3536import sun.security.x509.SerialNumber;37import sun.security.provider.certpath.*;3839/*40* Tester note:41* For this test, all input files should be co-located with the test source42* code. All test input data should be in PEM format, and may be commented43* with the '#' character at the beginning of any comment line. Most tests were44* generated using the "openssl ocsp" utility in server mode and used the same45* utility as a client to drive the responses. In rare cases46* (ocsp-good-witharchcut.resp, etc.) the test input was manually modified47* because openssl's ocsp could not generate data in that format (e.g. a48* "good" response with singleExtensions in the SingleResponse structure.)49* These tests were created to force the code to walk codepaths reached only50* with invalid OCSP data or legal formats that are not easily generated using51* the tools at hand. These hand-modified test cases will not verify.52*/5354public class OCSPSingleExtensions {55public static CertificateFactory CF;56public static final File testDir =57new File(System.getProperty("test.src", "."));58public static final Base64.Decoder B64D = Base64.getMimeDecoder();5960public static void main(String [] args) throws Exception {61// Get a CertificateFactory for various tests62CF = CertificateFactory.getInstance("X509");63ByteArrayInputStream bais =64new ByteArrayInputStream(readFile("int.crt").getBytes());65X509Certificate intCA = (X509Certificate)CF.generateCertificate(bais);66System.out.println("Successfully instantiated CA cert \"" +67intCA.getSubjectX500Principal() + "\"");6869CertId cid0x1500 = new CertId(intCA, new SerialNumber(0x1500));70boolean noFailures = true;7172OCSPResponse.SingleResponse sr =73getSRByFilename("ocsp-good-nonext.resp", cid0x1500);74noFailures &= checkSingleExts(sr, 0);7576if (sr.getRevocationTime() != null) {77throw new RuntimeException("Oops. revocationTime is non-null " +78sr.getRevocationTime());79} else if (sr.getRevocationReason() != null) {80throw new RuntimeException("Oops. revocationReason is non-null " +81sr.getRevocationReason());82}8384sr = getSRByFilename("ocsp-good-withnext.resp", cid0x1500);85noFailures &= checkSingleExts(sr, 0);8687sr = getSRByFilename("ocsp-good-witharchcut.resp", cid0x1500);88noFailures &= checkSingleExts(sr, 1);8990sr = getSRByFilename("ocsp-rev-nocerts.resp", cid0x1500);91noFailures &= checkSingleExts(sr, 1);9293sr = getSRByFilename("ocsp-rev-nonext-noinv.resp", cid0x1500);94noFailures &= checkSingleExts(sr, 0);9596sr = getSRByFilename("ocsp-rev-withnext-noinv.resp", cid0x1500);97noFailures &= checkSingleExts(sr, 0);9899sr = getSRByFilename("ocsp-rev-nonext-withinv.resp", cid0x1500);100noFailures &= checkSingleExts(sr, 1);101102sr = getSRByFilename("ocsp-rev-withnext-withinv.resp", cid0x1500);103noFailures &= checkSingleExts(sr, 1);104105try {106sr = getSRByFilename("ocsp-rev-twonext.resp", cid0x1500);107System.out.println("FAIL: Allowed two nextUpdate fields");108noFailures = false;109} catch (IOException ioe) {110System.out.println("Caught expected exception: " + ioe);111}112113try {114sr = getSRByFilename("ocsp-rev-bad-sr-tag.resp", cid0x1500);115System.out.println("FAIL: Allowed invalid singleResponse item");116noFailures = false;117} catch (IOException ioe) {118System.out.println("Caught expected exception: " + ioe);119}120121try {122sr = getSRByFilename("ocsp-rev-sr-cont-reverse.resp", cid0x1500);123System.out.println("FAIL: Allowed reversed " +124"nextUpdate/singleExtensions");125noFailures = false;126} catch (IOException ioe) {127System.out.println("Caught expected exception: " + ioe);128}129130if (!noFailures) {131throw new RuntimeException("One or more tests failed");132}133}134135private static OCSPResponse.SingleResponse getSRByFilename(String fileName,136CertId cid) throws IOException {137byte[] respDER = B64D.decode(readFile(fileName));138OCSPResponse or = new OCSPResponse(respDER);139OCSPResponse.SingleResponse sr = or.getSingleResponse(cid);140return sr;141}142143private static String readFile(String fileName) throws IOException {144String filePath = testDir + "/" + fileName;145StringBuilder sb = new StringBuilder();146147try (FileReader fr = new FileReader(filePath);148BufferedReader br = new BufferedReader(fr)) {149String line;150while ((line = br.readLine()) != null) {151if (!line.trim().startsWith("#")) {152sb.append(line).append("\n");153}154}155}156157System.out.println("Successfully read " + fileName);158return sb.toString();159}160161private static boolean checkSingleExts(OCSPResponse.SingleResponse sr,162int singleExtCount) {163Map<String, Extension> singleExts;164try {165singleExts = sr.getSingleExtensions();166} catch (NullPointerException npe) {167System.out.println(168"Warning: Sent null singleResponse into checkSingleExts");169return false;170}171172for (String key : singleExts.keySet()) {173System.out.println("singleExtension: " + singleExts.get(key));174}175176if (singleExts.size() != singleExtCount) {177System.out.println("Single Extension count mismatch, " +178"expected " + singleExtCount + ", got " +179singleExts.size());180return false;181} else {182return true;183}184}185}186187188