Path: blob/master/test/jdk/sun/security/pkcs/pkcs10/PKCS10AttributeReader.java
41153 views
/*1* Copyright (c) 2015, 2020, 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 8048357 824215126* @summary Read in a file containing a DER encoded PKCS10 certificate request,27* flanked with "begin" and "end" lines.28* @modules java.base/sun.security.pkcs29* java.base/sun.security.pkcs1030* java.base/sun.security.util31* @compile -XDignore.symbol.file PKCS10AttributeReader.java32* @run main PKCS10AttributeReader33*/34import java.util.Base64;35import java.util.Enumeration;36import java.util.HashMap;37import java.util.Date;38import sun.security.pkcs.PKCS9Attribute;39import sun.security.pkcs10.PKCS10Attribute;40import sun.security.pkcs10.PKCS10Attributes;41import sun.security.util.DerInputStream;42import sun.security.util.ObjectIdentifier;4344/*45Tests only reads DER encoding files, contents of corresponding asn.1 files46are copied below for reference.4748# An attribute set for testing with PKCS10.4950{A0 # implicit tag51{SEQ # Content Type52{OID 1.2.840.113549.1.9.3}53{SET54{OID "1234"}55}56}57{SEQ # Challenge Password58{OID 1.2.840.113549.1.9.7}59{SET60{T61String "GuessWhoAmI"}61}62}63{SEQ # Signing Time64{OID 1.2.840.113549.1.9.5}65{SET66{UTCTime "970422145010Z"}67}68}69}70*/71public class PKCS10AttributeReader {72// DER encoded files are binary files, to avoid attaching binary files,73// DER files were encoded in base6474static final String ATTRIBS = "oE8wEwYJKoZIhvcNAQkDMQYGBDEyMzQwGgYJKoZIhv"75+ "cNAQkHMQ0UC0d1ZXNzV2hv\nQW1JMBwGCSqGSIb3DQEJBTEPFw05NzA0MjIxND"76+ "UwMTBa";7778public static void main(String[] args) throws Exception {7980// Decode base64 encoded DER file81byte[] pkcs10Bytes = Base64.getMimeDecoder().decode(ATTRIBS.getBytes());8283HashMap<ObjectIdentifier, Object> RequestStander = new HashMap() {84{85put(PKCS9Attribute.CHALLENGE_PASSWORD_OID, "GuessWhoAmI");86put(PKCS9Attribute.SIGNING_TIME_OID, new Date(861720610000L));87put(PKCS9Attribute.CONTENT_TYPE_OID,88ObjectIdentifier.of("1.9.50.51.52"));89}90};9192int invalidNum = 0;93PKCS10Attributes resp = new PKCS10Attributes(94new DerInputStream(pkcs10Bytes));95Enumeration eReq = resp.getElements();96int numOfAttrs = 0;97while (eReq.hasMoreElements()) {98numOfAttrs++;99PKCS10Attribute attr = (PKCS10Attribute) eReq.nextElement();100if (RequestStander.containsKey(attr.getAttributeId())) {101if (RequestStander.get(attr.getAttributeId())102.equals(attr.getAttributeValue())) {103System.out.println(attr.getAttributeId() + " "104+ attr.getAttributeValue());105} else {106invalidNum++;107System.out.println("< " + attr.getAttributeId() + " "108+ attr.getAttributeValue());109System.out.println("< " + attr.getAttributeId() + " "110+ RequestStander.get(attr.getAttributeId()));111}112} else {113invalidNum++;114System.out.println("No" + attr.getAttributeId()115+ "in Certificate Request list");116}117}118if (numOfAttrs != RequestStander.size()) {119invalidNum++;120System.out.println("Incorrect number of attributes.");121}122System.out.println();123if (invalidNum > 0) {124throw new RuntimeException(125"Attributes Compared with Stander :" + " Failed");126}127System.out.println("Attributes Compared with Stander: Pass");128}129130}131132133