jdk/test/sun/security/util/DerInputBuffer/PaddedBitString.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4511556
       
    27  * @summary Verify BitString value containing padding bits is accepted.
       
    28  */
       
    29 
       
    30 import java.io.*;
       
    31 import java.util.Arrays;
       
    32 import java.math.BigInteger;
       
    33 
       
    34 import sun.security.util.DerInputStream;
       
    35 
       
    36 public class PaddedBitString {
       
    37 
       
    38     // Relaxed the BitString parsing routine to accept bit strings
       
    39     // with padding bits, ex. treat DER_BITSTRING_PAD6 as the same
       
    40     // 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 ignored
       
    44 
       
    45     // bit string (01011101 11000000)
       
    46     // With 6 padding bits (01011101 11001011)
       
    47     private final static byte[] DER_BITSTRING_PAD6 = { 3, 3, 6,
       
    48                                                    (byte)0x5d, (byte)0xcb };
       
    49 
       
    50     // With no padding bits
       
    51     private final static byte[] DER_BITSTRING_NOPAD = { 3, 3, 0,
       
    52                                                    (byte)0x5d, (byte)0xc0 };
       
    53 
       
    54     public static void main(String args[]) throws Exception {
       
    55         byte[] ba0, ba1;
       
    56         try {
       
    57             DerInputStream derin = new DerInputStream(DER_BITSTRING_PAD6);
       
    58             ba1 = derin.getBitString();
       
    59         } catch( IOException e ) {
       
    60             e.printStackTrace();
       
    61             throw new Exception("Unable to parse BitString with 6 padding bits");
       
    62         }
       
    63 
       
    64         try {
       
    65             DerInputStream derin = new DerInputStream(DER_BITSTRING_NOPAD);
       
    66             ba0 = derin.getBitString();
       
    67         } catch( IOException e ) {
       
    68             e.printStackTrace();
       
    69             throw new Exception("Unable to parse BitString with no padding");
       
    70         }
       
    71 
       
    72         if (Arrays.equals(ba1, ba0) == false ) {
       
    73             throw new Exception("BitString comparison check failed");
       
    74         }
       
    75     }
       
    76 }