Path: blob/master/test/jdk/sun/security/pkcs/pkcs9/UnknownAttribute.java
41155 views
/*1* Copyright (c) 2013, 2020, 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 8011867 824215126* @summary Accept unknown PKCS #9 attributes27* @library /test/lib28* @modules java.base/sun.security.pkcs29* java.base/sun.security.util30*/3132import java.io.*;33import java.util.Arrays;3435import sun.security.pkcs.PKCS9Attribute;36import sun.security.util.DerValue;37import sun.security.util.ObjectIdentifier;38import jdk.test.lib.hexdump.HexPrinter;3940public class UnknownAttribute {4142public static void main(String[] args) throws Exception {43// Unknown attr44PKCS9Attribute p1 = new PKCS9Attribute(45PKCS9Attribute.CHALLENGE_PASSWORD_OID, "t0p5ecr3t");46if (!p1.isKnown()) {47throw new Exception();48}49// Unknown attr from DER50byte[] data = {510x30, 0x08, // SEQUENCE OF520x06, 0x02, 0x2A, 0x03, // OID 1.2.3 and530x31, 0x02, 0x05, 0x00 // an empty SET54};55PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));56if (p2.isKnown()) {57throw new Exception();58}59ByteArrayOutputStream bout = new ByteArrayOutputStream();60p2.derEncode(bout);61HexPrinter.simple().dest(System.err).format(bout.toByteArray());62if (!Arrays.equals(data, bout.toByteArray())) {63throw new Exception();64}65// Unknown attr from value66try {67new PKCS9Attribute(ObjectIdentifier.of("1.2.3"), "hello");68throw new Exception();69} catch (IllegalArgumentException iae) {70// Good. Unknown attr must have byte[] value type71}72PKCS9Attribute p3 = new PKCS9Attribute(73ObjectIdentifier.of("1.2.3"), new byte[]{0x31,0x02,0x05,0x00});74if (p3.isKnown()) {75throw new Exception();76}77bout = new ByteArrayOutputStream();78p3.derEncode(bout);79if (!Arrays.equals(data, bout.toByteArray())) {80throw new Exception();81}82}83}848586