Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs/pkcs10/PKCS10AttributeReader.java
38854 views
/*1* Copyright (c) 2015, 2016, 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 804835726* @summary Read in a file containing a DER encoded PKCS10 certificate request,27* flanked with "begin" and "end" lines.28* @compile -XDignore.symbol.file PKCS10AttributeReader.java29* @run main PKCS10AttributeReader30*/31import java.util.Base64;32import java.util.Enumeration;33import java.util.HashMap;34import java.util.Date;35import sun.security.pkcs.PKCS9Attribute;36import sun.security.pkcs10.PKCS10Attribute;37import sun.security.pkcs10.PKCS10Attributes;38import sun.security.util.DerInputStream;39import sun.security.util.ObjectIdentifier;4041/*42Tests only reads DER encoding files, contents of corresponding asn.1 files43are copied below for reference.4445# An attribute set for testing with PKCS10.4647{A0 # implicit tag48{SEQ # Content Type49{OID 1.2.840.113549.1.9.3}50{SET51{OID "1234"}52}53}54{SEQ # Challenge Password55{OID 1.2.840.113549.1.9.7}56{SET57{T61String "GuessWhoAmI"}58}59}60{SEQ # Signing Time61{OID 1.2.840.113549.1.9.5}62{SET63{UTCTime "970422145010Z"}64}65}66}67*/68public class PKCS10AttributeReader {69// DER encoded files are binary files, to avoid attaching binary files,70// DER files were encoded in base6471static final String ATTRIBS = "oE8wEwYJKoZIhvcNAQkDMQYGBDEyMzQwGgYJKoZIhv"72+ "cNAQkHMQ0UC0d1ZXNzV2hv\nQW1JMBwGCSqGSIb3DQEJBTEPFw05NzA0MjIxND"73+ "UwMTBa";7475public static void main(String[] args) throws Exception {7677// Decode base64 encoded DER file78byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());7980HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {81{82put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");83put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));84put(PKCS9Attribute.CONTENT_TYPE_OID,85new ObjectIdentifier("1.9.50.51.52"));86}87};8889int invalidNum = 0;90PKCS10Attributes resp = new PKCS10Attributes(91new DerInputStream(pkcs10Bytes));92Enumeration eReq = resp.getElements();93int numOfAttrs = 0;94while (eReq.hasMoreElements()) {95numOfAttrs++;96PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();97if (RequestStander.containsKey(attr.getAttributeId())) {98if (RequestStander.get(attr.getAttributeId())99.equals(attr.getAttributeValue())) {100System.out.println(attr.getAttributeId() + " "101+ attr.getAttributeValue());102} else {103invalidNum++;104System.out.println("< " + attr.getAttributeId() + " "105+ attr.getAttributeValue());106System.out.println("< " + attr.getAttributeId() + " "107+ RequestStander.get(attr.getAttributeId()));108}109} else {110invalidNum++;111System.out.println("No" + attr.getAttributeId()112+ "in Certificate Request list");113}114}115if (numOfAttrs != RequestStander.size()) {116invalidNum++;117System.out.println("Incorrect number of attributes.");118}119System.out.println();120if (invalidNum > 0) {121throw new RuntimeException(122"Attributes Compared with Stander :" + " Failed");123}124System.out.println("Attributes Compared with Stander: Pass");125}126127}128129130