UnknownAttribute.java revision 5872
5872N/A/*
5872N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
5872N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5872N/A *
5872N/A * This code is free software; you can redistribute it and/or modify it
5872N/A * under the terms of the GNU General Public License version 2 only, as
5872N/A * published by the Free Software Foundation.
5872N/A *
5872N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5872N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5872N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5872N/A * version 2 for more details (a copy is included in the LICENSE file that
5872N/A * accompanied this code).
5872N/A *
5872N/A * You should have received a copy of the GNU General Public License version
5872N/A * 2 along with this work; if not, write to the Free Software Foundation,
5872N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5872N/A *
5872N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5872N/A * or visit www.oracle.com if you need additional information or have any
5872N/A * questions.
5872N/A */
5872N/A
5872N/A/*
5872N/A * @test
5872N/A * @bug 8011867
5872N/A * @summary Accept unknown PKCS #9 attributes
5872N/A */
5872N/A
5872N/Aimport java.io.*;
5872N/Aimport java.util.Arrays;
5872N/A
5872N/Aimport sun.misc.HexDumpEncoder;
5872N/Aimport sun.security.pkcs.PKCS9Attribute;
5872N/Aimport sun.security.util.DerValue;
5872N/Aimport sun.security.util.ObjectIdentifier;
5872N/A
5872N/Apublic class UnknownAttribute {
5872N/A
5872N/A public static void main(String[] args) throws Exception {
5872N/A // Unknown attr
5872N/A PKCS9Attribute p1 = new PKCS9Attribute(
5872N/A PKCS9Attribute.CHALLENGE_PASSWORD_STR, "t0p5ecr3t");
5872N/A if (!p1.isKnown()) {
5872N/A throw new Exception();
5872N/A }
5872N/A // Unknown attr from DER
5872N/A byte[] data = {
5872N/A 0x30, 0x08, // SEQUENCE OF
5872N/A 0x06, 0x02, 0x2A, 0x03, // OID 1.2.3 and
5872N/A 0x31, 0x02, 0x05, 0x00 // an empty SET
5872N/A };
5872N/A PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));
5872N/A if (p2.isKnown()) {
5872N/A throw new Exception();
5872N/A }
5872N/A ByteArrayOutputStream bout = new ByteArrayOutputStream();
5872N/A p2.derEncode(bout);
5872N/A new HexDumpEncoder().encodeBuffer(bout.toByteArray(), System.err);
5872N/A if (!Arrays.equals(data, bout.toByteArray())) {
5872N/A throw new Exception();
5872N/A }
5872N/A // Unknown attr from value
5872N/A try {
5872N/A new PKCS9Attribute(new ObjectIdentifier("1.2.3"), "hello");
5872N/A throw new Exception();
5872N/A } catch (IllegalArgumentException iae) {
5872N/A // Good. Unknown attr must have byte[] value type
5872N/A }
5872N/A PKCS9Attribute p3 = new PKCS9Attribute(
5872N/A new ObjectIdentifier("1.2.3"), new byte[]{0x31,0x02,0x05,0x00});
5872N/A if (p3.isKnown()) {
5872N/A throw new Exception();
5872N/A }
5872N/A bout = new ByteArrayOutputStream();
5872N/A p3.derEncode(bout);
5872N/A if (!Arrays.equals(data, bout.toByteArray())) {
5872N/A throw new Exception();
5872N/A }
5872N/A }
5872N/A}