|
1 | 1 | /* |
2 | | - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
26 | 26 | * @bug 4511556 |
27 | 27 | * @summary Verify BitString value containing padding bits is accepted. |
28 | 28 | * @modules java.base/sun.security.util |
| 29 | + * @library /test/lib |
29 | 30 | */ |
30 | | - |
31 | 31 | import java.io.*; |
32 | | -import java.util.Arrays; |
33 | 32 | import java.math.BigInteger; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.HexFormat; |
| 35 | +import jdk.test.lib.Asserts; |
| 36 | +import jdk.test.lib.Utils; |
34 | 37 |
|
| 38 | +import sun.security.util.BitArray; |
35 | 39 | import sun.security.util.DerInputStream; |
36 | 40 |
|
37 | 41 | public class PaddedBitString { |
38 | 42 |
|
39 | 43 | // Relaxed the BitString parsing routine to accept bit strings |
40 | | - // with padding bits, ex. treat DER_BITSTRING_PAD6 as the same |
41 | | - // bit string as DER_BITSTRING_NOPAD. |
| 44 | + // with padding bits, ex. treat DER_BITSTRING_PAD6_b as the same |
| 45 | + // bit string as DER_BITSTRING_PAD6_0/DER_BITSTRING_NOPAD. |
42 | 46 | // Note: |
43 | 47 | // 1. the number of padding bits has to be in [0...7] |
44 | 48 | // 2. value of the padding bits is ignored |
45 | 49 |
|
46 | | - // bit string (01011101 11000000) |
47 | | - // With 6 padding bits (01011101 11001011) |
48 | | - private final static byte[] DER_BITSTRING_PAD6 = { 3, 3, 6, |
49 | | - (byte)0x5d, (byte)0xcb }; |
50 | | - |
51 | 50 | // With no padding bits |
52 | 51 | private final static byte[] DER_BITSTRING_NOPAD = { 3, 3, 0, |
53 | 52 | (byte)0x5d, (byte)0xc0 }; |
| 53 | + // With 6 zero padding bits (01011101 11000000) |
| 54 | + private final static byte[] DER_BITSTRING_PAD6_0 = { 3, 3, 6, |
| 55 | + (byte)0x5d, (byte)0xc0 }; |
54 | 56 |
|
55 | | - public static void main(String args[]) throws Exception { |
56 | | - byte[] ba0, ba1; |
57 | | - try { |
58 | | - DerInputStream derin = new DerInputStream(DER_BITSTRING_PAD6); |
59 | | - ba1 = derin.getBitString(); |
60 | | - } catch( IOException e ) { |
61 | | - e.printStackTrace(); |
62 | | - throw new Exception("Unable to parse BitString with 6 padding bits"); |
63 | | - } |
| 57 | + // With 6 nonzero padding bits (01011101 11001011) |
| 58 | + private final static byte[] DER_BITSTRING_PAD6_b = { 3, 3, 6, |
| 59 | + (byte)0x5d, (byte)0xcb }; |
64 | 60 |
|
65 | | - try { |
66 | | - DerInputStream derin = new DerInputStream(DER_BITSTRING_NOPAD); |
67 | | - ba0 = derin.getBitString(); |
68 | | - } catch( IOException e ) { |
69 | | - e.printStackTrace(); |
70 | | - throw new Exception("Unable to parse BitString with no padding"); |
71 | | - } |
| 61 | + // With 8 padding bits |
| 62 | + private final static byte[] DER_BITSTRING_PAD8_0 = { 3, 3, 8, |
| 63 | + (byte)0x5d, (byte)0xc0 }; |
| 64 | + |
| 65 | + private final static byte[] BITS = { (byte)0x5d, (byte)0xc0 }; |
| 66 | + |
| 67 | + static enum Type { |
| 68 | + BIT_STRING, |
| 69 | + UNALIGNED_BIT_STRING; |
| 70 | + } |
| 71 | + |
| 72 | + public static void main(String args[]) throws Exception { |
| 73 | + test(DER_BITSTRING_NOPAD, new BitArray(16, BITS)); |
| 74 | + test(DER_BITSTRING_PAD6_0, new BitArray(10, BITS)); |
| 75 | + test(DER_BITSTRING_PAD6_b, new BitArray(10, BITS)); |
| 76 | + test(DER_BITSTRING_PAD8_0, null); |
| 77 | + System.out.println("Tests Passed"); |
| 78 | + } |
72 | 79 |
|
73 | | - if (Arrays.equals(ba1, ba0) == false ) { |
74 | | - throw new Exception("BitString comparison check failed"); |
| 80 | + private static void test(byte[] in, BitArray ans) throws IOException { |
| 81 | + System.out.println("Testing " + |
| 82 | + HexFormat.of().withUpperCase().formatHex(in)); |
| 83 | + for (Type t : Type.values()) { |
| 84 | + DerInputStream derin = new DerInputStream(in); |
| 85 | + boolean shouldPass = (ans != null); |
| 86 | + switch (t) { |
| 87 | + case BIT_STRING: |
| 88 | + if (shouldPass) { |
| 89 | + Asserts.assertTrue(Arrays.equals(ans.toByteArray(), |
| 90 | + derin.getBitString())); |
| 91 | + } else { |
| 92 | + Utils.runAndCheckException(() -> derin.getBitString(), |
| 93 | + IOException.class); |
| 94 | + } |
| 95 | + break; |
| 96 | + case UNALIGNED_BIT_STRING: |
| 97 | + if (shouldPass) { |
| 98 | + Asserts.assertEQ(ans, derin.getUnalignedBitString()); |
| 99 | + } else { |
| 100 | + Utils.runAndCheckException(() -> |
| 101 | + derin.getUnalignedBitString(), IOException.class); |
| 102 | + } |
| 103 | + break; |
| 104 | + } |
75 | 105 | } |
76 | 106 | } |
77 | 107 | } |
0 commit comments