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  * @modules java.base/sun.misc
  29  *          java.base/sun.security.pkcs
  30  *          java.base/sun.security.util
  31  */
  32 
  33 import java.io.*;
  34 import java.util.Arrays;
  35 
  36 import sun.misc.HexDumpEncoder;
  37 import sun.security.pkcs.PKCS9Attribute;
  38 import sun.security.util.DerValue;
  39 import sun.security.util.ObjectIdentifier;
  40 
  41 public class UnknownAttribute {
  42 
  43     public static void main(String[] args) throws Exception {
  44         // Unknown attr
  45         PKCS9Attribute p1 = new PKCS9Attribute(
  46                 PKCS9Attribute.CHALLENGE_PASSWORD_STR, "t0p5ecr3t");
  47         if (!p1.isKnown()) {
  48             throw new Exception();
  49         }
  50         // Unknown attr from DER
  51         byte[] data = {
  52                 0x30, 0x08,                 // SEQUENCE OF
  53                 0x06, 0x02, 0x2A, 0x03,     // OID 1.2.3 and
  54                 0x31, 0x02, 0x05, 0x00      // an empty SET
  55         };
  56         PKCS9Attribute p2 = new PKCS9Attribute(new DerValue(data));
  57         if (p2.isKnown()) {
  58             throw new Exception();
  59         }
  60         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  61         p2.derEncode(bout);
  62         new HexDumpEncoder().encodeBuffer(bout.toByteArray(), System.err);
  63         if (!Arrays.equals(data, bout.toByteArray())) {
  64             throw new Exception();
  65         }
  66         // Unknown attr from value
  67         try {
  68             new PKCS9Attribute(new ObjectIdentifier("1.2.3"), "hello");
  69             throw new Exception();
  70         } catch (IllegalArgumentException iae) {
  71             // Good. Unknown attr must have byte[] value type
  72         }
  73         PKCS9Attribute p3 = new PKCS9Attribute(
  74                 new ObjectIdentifier("1.2.3"), new byte[]{0x31,0x02,0x05,0x00});
  75         if (p3.isKnown()) {
  76             throw new Exception();
  77         }
  78         bout = new ByteArrayOutputStream();
  79         p3.derEncode(bout);
  80         if (!Arrays.equals(data, bout.toByteArray())) {
  81             throw new Exception();
  82         }
  83     }
  84 }