1 /* 2 * Copyright (c) 2015, 2016, 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 8048357 27 * @summary Read in a file containing a DER encoded PKCS10 certificate request, 28 * flanked with "begin" and "end" lines. 29 * @compile -XDignore.symbol.file PKCS10AttributeReader.java 30 * @run main PKCS10AttributeReader 31 */ 32 import java.util.Base64; 33 import java.util.Enumeration; 34 import java.util.HashMap; 35 import java.util.Date; 36 import sun.security.pkcs.PKCS9Attribute; 37 import sun.security.pkcs10.PKCS10Attribute; 38 import sun.security.pkcs10.PKCS10Attributes; 39 import sun.security.util.DerInputStream; 40 import sun.security.util.ObjectIdentifier; 41 42 /* 43 Tests only reads DER encoding files, contents of corresponding asn.1 files 44 are copied below for reference. 45 46 # An attribute set for testing with PKCS10. 47 48 {A0 # implicit tag 49 {SEQ # Content Type 50 {OID 1.2.840.113549.1.9.3} 51 {SET 52 {OID "1234"} 53 } 54 } 55 {SEQ # Challenge Password 56 {OID 1.2.840.113549.1.9.7} 57 {SET 58 {T61String "GuessWhoAmI"} 59 } 60 } 61 {SEQ # Signing Time 62 {OID 1.2.840.113549.1.9.5} 63 {SET 64 {UTCTime "970422145010Z"} 65 } 66 } 67 } 68 */ 69 public class PKCS10AttributeReader { 70 // DER encoded files are binary files, to avoid attaching binary files, 71 // DER files were encoded in base64 72 static final String ATTRIBS = "oE8wEwYJKoZIhvcNAQkDMQYGBDEyMzQwGgYJKoZIhv" 73 + "cNAQkHMQ0UC0d1ZXNzV2hv\nQW1JMBwGCSqGSIb3DQEJBTEPFw05NzA0MjIxND" 74 + "UwMTBa"; 75 76 public static void main(String[] args) throws Exception { 77 78 // Decode base64 encoded DER file 79 byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes()); 80 81 HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() { 82 { 83 put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI"); 84 put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L)); 85 put(PKCS9Attribute.CONTENT_TYPE_OID, 86 new ObjectIdentifier("1.9.50.51.52")); 87 } 88 }; 89 90 int invalidNum = 0; 91 PKCS10Attributes resp = new PKCS10Attributes( 92 new DerInputStream(pkcs10Bytes)); 93 Enumeration eReq = resp.getElements(); 94 int numOfAttrs = 0; 95 while (eReq.hasMoreElements()) { 96 numOfAttrs++; 97 PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement(); 98 if (RequestStander.containsKey(attr.getAttributeId())) { 99 if (RequestStander.get(attr.getAttributeId()) 100 .equals(attr.getAttributeValue())) { 101 System.out.println(attr.getAttributeId() + " " 102 + attr.getAttributeValue()); 103 } else { 104 invalidNum++; 105 System.out.println("< " + attr.getAttributeId() + " " 106 + attr.getAttributeValue()); 107 System.out.println("< " + attr.getAttributeId() + " " 108 + RequestStander.get(attr.getAttributeId())); 109 } 110 } else { 111 invalidNum++; 112 System.out.println("No" + attr.getAttributeId() 113 + "in Certificate Request list"); 114 } 115 } 116 if (numOfAttrs != RequestStander.size()) { 117 invalidNum++; 118 System.out.println("Incorrect number of attributes."); 119 } 120 System.out.println(); 121 if (invalidNum > 0) { 122 throw new RuntimeException( 123 "Attributes Compared with Stander :" + " Failed"); 124 } 125 System.out.println("Attributes Compared with Stander: Pass"); 126 } 127 128 }