Path: blob/master/test/jdk/sun/security/util/DerValue/Indefinite.java
41152 views
/*1* Copyright (c) 2008, 2020, 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 6731685 824978326* @summary CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain27* @modules java.base/sun.security.util:+open28* @library /test/lib29*/3031import java.io.*;32import java.lang.reflect.Field;33import java.util.Arrays;3435import jdk.test.lib.Asserts;36import jdk.test.lib.Utils;37import sun.security.util.*;3839public class Indefinite {4041public static void main(String[] args) throws Exception {4243// Indefinite length with trailing bytes44test(true, new byte[] {45// An OCTET-STRING in 2 parts460x24, (byte) 0x80, 4, 2, 'a', 'b', 4, 2, 'c', 'd', 0, 0,47// Garbage follows, may be falsely recognized as EOC480, 0, 0, 0,49// and more507, 8, 9, 10});5152// Definite length with trailing bytes53test(false, new byte[] {544, 4, 'a', 'b', 'c', 'd',550, 0, 0, 0, 7, 8, 9, 10 });56}5758static void test(boolean indefinite, byte[] input) throws Exception {5960// 1. parse stream61InputStream ins = new ByteArrayInputStream(input);62DerValue v = new DerValue(ins);63Asserts.assertEQ(new String(v.getOctetString()), "abcd");6465if (indefinite) {66// Trailing bytes might be consumed by the conversion but can67// be found in DerValue "after end".68Field buffer = DerValue.class.getDeclaredField("buffer");69Field end = DerValue.class.getDeclaredField("end");70buffer.setAccessible(true);71end.setAccessible(true);72int bufferLen = ((byte[])buffer.get(v)).length;73int endPos = end.getInt(v);74// Data "after end": bufferLen - endPos75// Data remained in stream: ins.available()x`76Asserts.assertEQ(bufferLen - endPos + ins.available(), 8);77} else {78// Trailing bytes remain in the stream for definite length79Asserts.assertEQ(ins.available(), 8);80}8182// 2. parse DerInputStream83DerInputStream ds = new DerInputStream(input);84Asserts.assertEQ(new String(ds.getDerValue().getOctetString()), "abcd");85Asserts.assertEQ(ds.available(), 8);86Asserts.assertTrue(Arrays.equals(ds.toByteArray(),87new byte[]{0,0,0,0,7,8,9,10}));8889// 3. Parse full byte array90Utils.runAndCheckException(() -> new DerValue(input),91e -> Asserts.assertTrue(e instanceof IOException92&& e.getMessage().equals("extra data at the end")));9394// 4. Parse exact byte array95Asserts.assertEQ(new String(new DerValue(Arrays.copyOf(input, input.length - 8))96.getOctetString()), "abcd");97}98}99100101