jdk/test/sun/security/pkcs/pkcs9/UnknownAttribute.java
author mchung
Thu, 28 May 2015 10:54:48 -0700
changeset 30820 0d4717a011d3
parent 16848 630dd3143428
child 34687 d302ed125dc9
permissions -rw-r--r--
8081347: Add @modules to jdk_core tests Reviewed-by: alanb, joehw, lancea Contributed-by: alexander.kulyakhtin@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16848
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     1
/*
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     4
 *
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     8
 *
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    13
 * accompanied this code).
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    14
 *
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    18
 *
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    21
 * questions.
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    22
 */
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    23
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    24
/*
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    25
 * @test
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    26
 * @bug 8011867
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    27
 * @summary Accept unknown PKCS #9 attributes
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 16848
diff changeset
    28
 * @modules java.base/sun.misc
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 16848
diff changeset
    29
 *          java.base/sun.security.pkcs
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 16848
diff changeset
    30
 *          java.base/sun.security.util
16848
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    31
 */
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    32
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    33
import java.io.*;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    34
import java.util.Arrays;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    35
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    36
import sun.misc.HexDumpEncoder;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    37
import sun.security.pkcs.PKCS9Attribute;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    38
import sun.security.util.DerValue;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    39
import sun.security.util.ObjectIdentifier;
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    40
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    41
public class UnknownAttribute {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    42
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    44
        // Unknown attr
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    45
        PKCS9Attribute p1 = new PKCS9Attribute(
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    46
                PKCS9Attribute.CHALLENGE_PASSWORD_STR, "t0p5ecr3t");
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    47
        if (!p1.isKnown()) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    48
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    49
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    50
        // Unknown attr from DER
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    51
        byte[] data = {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    52
                0x30, 0x08,                 // SEQUENCE OF
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    53
                0x06, 0x02, 0x2A, 0x03,     // OID 1.2.3 and
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    54
                0x31, 0x02, 0x05, 0x00      // an empty SET
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    55
        };
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    56
        PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    57
        if (p2.isKnown()) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    58
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    59
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    60
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    61
        p2.derEncode(bout);
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    62
        new HexDumpEncoder().encodeBuffer(bout.toByteArray(), System.err);
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    63
        if (!Arrays.equals(data, bout.toByteArray())) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    64
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    65
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    66
        // Unknown attr from value
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    67
        try {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    68
            new PKCS9Attribute(new ObjectIdentifier("1.2.3"), "hello");
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    69
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    70
        } catch (IllegalArgumentException iae) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    71
            // Good. Unknown attr must have byte[] value type
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    72
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    73
        PKCS9Attribute p3 = new PKCS9Attribute(
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    74
                new ObjectIdentifier("1.2.3"), new byte[]{0x31,0x02,0x05,0x00});
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    75
        if (p3.isKnown()) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    76
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    77
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    78
        bout = new ByteArrayOutputStream();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    79
        p3.derEncode(bout);
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    80
        if (!Arrays.equals(data, bout.toByteArray())) {
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    81
            throw new Exception();
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    82
        }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    83
    }
630dd3143428 8011867: Accept unknown PKCS #9 attributes
weijun
parents:
diff changeset
    84
}