Path: blob/master/test/jdk/sun/security/pkcs/pkcs10/PKCS10AttrEncoding.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 test DER encoding of PKCS10 attributes27* @modules java.base/sun.security.pkcs28* java.base/sun.security.pkcs1029* java.base/sun.security.util30* java.base/sun.security.x50931* @compile -XDignore.symbol.file PKCS10AttrEncoding.java32* @run main PKCS10AttrEncoding33*/34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.PrivateKey;37import java.util.Enumeration;38import java.util.GregorianCalendar;39import java.util.HashMap;40import sun.security.pkcs.PKCS9Attribute;41import sun.security.pkcs10.PKCS10;42import sun.security.pkcs10.PKCS10Attribute;43import sun.security.pkcs10.PKCS10Attributes;44import sun.security.util.ObjectIdentifier;45import sun.security.x509.X500Name;46import sun.security.x509.X509Key;4748public class PKCS10AttrEncoding {4950static final ObjectIdentifier[] ids = {51PKCS9Attribute.CONTENT_TYPE_OID, // ContentType52PKCS9Attribute.SIGNING_TIME_OID, // SigningTime53PKCS9Attribute.CHALLENGE_PASSWORD_OID // ChallengePassword54};55static int failedCount = 0;56static HashMap<ObjectIdentifier, Object> constructedMap = new HashMap<>();5758public static void main(String[] args) throws Exception {5960// initializations61int len = ids.length;62Object[] values = {63ObjectIdentifier.of("1.2.3.4"),64new GregorianCalendar(1970, 1, 25, 8, 56, 7).getTime(),65"challenging"66};67for (int j = 0; j < len; j++) {68constructedMap.put(ids[j], values[j]);69}7071X500Name subject = new X500Name("cn=Test");72KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");73String sigAlg = "DSA";7475keyGen.initialize(512);7677KeyPair pair = keyGen.generateKeyPair();78X509Key publicKey = (X509Key) pair.getPublic();79PrivateKey privateKey = pair.getPrivate();8081// Create the PKCS10 request82PKCS10Attribute[] attrs = new PKCS10Attribute[len];83for (int j = 0; j < len; j++) {84attrs[j] = new PKCS10Attribute(ids[j], values[j]);85}86PKCS10 req = new PKCS10(publicKey, new PKCS10Attributes(attrs));87System.out.println("List of attributes in constructed PKCS10 "88+ "request: ");89checkAttributes(req.getAttributes().getElements());9091// Encode the PKCS10 request and generate another PKCS10 request from92// the encoded byte array93req.encodeAndSign(subject, privateKey, sigAlg);94PKCS10 resp = new PKCS10(req.getEncoded());95System.out.println("List of attributes in DER encoded PKCS10 Request:");96checkAttributes(resp.getAttributes().getElements());9798if (failedCount > 0) {99throw new RuntimeException("Attributes Compared : Failed");100}101System.out.println("Attributes Compared : Pass");102}103104static void checkAttributes(Enumeration attrs) {105int numOfAttrs = 0;106while (attrs.hasMoreElements()) {107numOfAttrs ++;108PKCS10Attribute attr = (PKCS10Attribute) attrs.nextElement();109110if (constructedMap.containsKey(attr.getAttributeId())) {111if (constructedMap.get(attr.getAttributeId()).112equals(attr.getAttributeValue())) {113System.out.print("AttributeId: " + attr.getAttributeId());114System.out.println(" AttributeValue: "115+ attr.getAttributeValue());116} else {117failedCount++;118System.out.print("< AttributeId: " + attr.getAttributeId());119System.out.println(" AttributeValue: " + constructedMap.120get(attr.getAttributeId()));121System.out.print("< AttributeId: " + attr.getAttributeId());122System.out.println(" AttributeValue: "123+ attr.getAttributeValue());124}125} else {126failedCount++;127System.out.println("No " + attr.getAttributeId()128+ " in DER encoded PKCS10 Request");129}130}131if(numOfAttrs != constructedMap.size()){132failedCount++;133System.out.println("Incorrect number of attributes.");134135}136System.out.println();137}138139}140141142