1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8011867
  27  * @summary Accept unknown PKCS #9 attributes
  28  */
  29 
  30 import java.io.*;
  31 import java.util.Arrays;
  32 
  33 import sun.misc.HexDumpEncoder;
  34 import sun.security.pkcs.PKCS9Attribute;
  35 import sun.security.util.DerValue;
  36 import sun.security.util.ObjectIdentifier;
  37 
  38 public class UnknownAttribute {
  39 
  40     public static void main(String[] args) throws Exception {
  41         // Unknown attr
  42         PKCS9Attribute p1 = new PKCS9Attribute(
  43                 PKCS9Attribute.CHALLENGE_PASSWORD_STR, "t0p5ecr3t");
  44         if (!p1.isKnown()) {
  45             throw new Exception();
  46         }
  47         // Unknown attr from DER
  48         byte[] data = {
  49                 0x30, 0x08,                 // SEQUENCE OF
  50                 0x06, 0x02, 0x2A, 0x03,     // OID 1.2.3 and
  51                 0x31, 0x02, 0x05, 0x00      // an empty SET
  52         };
  53         PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));
  54         if (p2.isKnown()) {
  55             throw new Exception();
  56         }
  57         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  58         p2.derEncode(bout);
  59         new HexDumpEncoder().encodeBuffer(bout.toByteArray(), System.err);
  60         if (!Arrays.equals(data, bout.toByteArray())) {
  61             throw new Exception();
  62         }
  63         // Unknown attr from value
  64         try {
  65             new PKCS9Attribute(new ObjectIdentifier("1.2.3"), "hello");
  66             throw new Exception();
  67         } catch (IllegalArgumentException iae) {
  68             // Good. Unknown attr must have byte[] value type
  69         }
  70         PKCS9Attribute p3 = new PKCS9Attribute(
  71                 new ObjectIdentifier("1.2.3"), new byte[]{0x31,0x02,0x05,0x00});
  72         if (p3.isKnown()) {
  73             throw new Exception();
  74         }
  75         bout = new ByteArrayOutputStream();
  76         p3.derEncode(bout);
  77         if (!Arrays.equals(data, bout.toByteArray())) {
  78             throw new Exception();
  79         }
  80     }
  81 }