Path: blob/master/test/jdk/sun/security/util/DerInputBuffer/PaddedBitString.java
41152 views
/*1* Copyright (c) 2002, 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 451155626* @summary Verify BitString value containing padding bits is accepted.27* @modules java.base/sun.security.util28*/2930import java.io.*;31import java.util.Arrays;32import java.math.BigInteger;3334import sun.security.util.DerInputStream;3536public class PaddedBitString {3738// Relaxed the BitString parsing routine to accept bit strings39// with padding bits, ex. treat DER_BITSTRING_PAD6 as the same40// bit string as DER_BITSTRING_NOPAD.41// Note:42// 1. the number of padding bits has to be in [0...7]43// 2. value of the padding bits is ignored4445// bit string (01011101 11000000)46// With 6 padding bits (01011101 11001011)47private final static byte[] DER_BITSTRING_PAD6 = { 3, 3, 6,48(byte)0x5d, (byte)0xcb };4950// With no padding bits51private final static byte[] DER_BITSTRING_NOPAD = { 3, 3, 0,52(byte)0x5d, (byte)0xc0 };5354public static void main(String args[]) throws Exception {55byte[] ba0, ba1;56try {57DerInputStream derin = new DerInputStream(DER_BITSTRING_PAD6);58ba1 = derin.getBitString();59} catch( IOException e ) {60e.printStackTrace();61throw new Exception("Unable to parse BitString with 6 padding bits");62}6364try {65DerInputStream derin = new DerInputStream(DER_BITSTRING_NOPAD);66ba0 = derin.getBitString();67} catch( IOException e ) {68e.printStackTrace();69throw new Exception("Unable to parse BitString with no padding");70}7172if (Arrays.equals(ba1, ba0) == false ) {73throw new Exception("BitString comparison check failed");74}75}76}777879