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