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 test DER encoding of PKCS10 attributes
  28  * @compile -XDignore.symbol.file PKCS10AttrEncoding.java
  29  * @run main PKCS10AttrEncoding
  30  */
  31 import java.security.KeyPair;
  32 import java.security.KeyPairGenerator;
  33 import java.security.PrivateKey;
  34 import java.security.Signature;
  35 import java.util.Enumeration;
  36 import java.util.GregorianCalendar;
  37 import java.util.HashMap;
  38 import sun.security.pkcs.PKCS9Attribute;
  39 import sun.security.pkcs10.PKCS10;
  40 import sun.security.pkcs10.PKCS10Attribute;
  41 import sun.security.pkcs10.PKCS10Attributes;
  42 import sun.security.util.ObjectIdentifier;
  43 import sun.security.x509.X500Name;
  44 import sun.security.x509.X509Key;
  45 
  46 public class PKCS10AttrEncoding {
  47 
  48     static final ObjectIdentifier[] ids = {
  49         PKCS9Attribute.CONTENT_TYPE_OID, // ContentType
  50         PKCS9Attribute.SIGNING_TIME_OID, // SigningTime
  51         PKCS9Attribute.CHALLENGE_PASSWORD_OID // ChallengePassword
  52     };
  53     static int failedCount = 0;
  54     static HashMap<ObjectIdentifier, Object> constructedMap = new HashMap<>();
  55 
  56     public static void main(String[] args) throws Exception {
  57 
  58         // initializations
  59         int len = ids.length;
  60         Object[] values = {
  61             new ObjectIdentifier("1.2.3.4"),
  62             new GregorianCalendar(1970, 1, 25, 8, 56, 7).getTime(),
  63             "challenging"
  64         };
  65         for (int j = 0; j < len; j++) {
  66             constructedMap.put(ids[j], values[j]);
  67         }
  68 
  69         X500Name subject = new X500Name("cn=Test");
  70         KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
  71         String sigAlg = "DSA";
  72 
  73         keyGen.initialize(512);
  74 
  75         KeyPair pair = keyGen.generateKeyPair();
  76         X509Key publicKey = (X509Key) pair.getPublic();
  77         PrivateKey privateKey = pair.getPrivate();
  78 
  79         Signature signature = Signature.getInstance(sigAlg);
  80         signature.initSign(privateKey);
  81 
  82         // Create the PKCS10 request
  83         PKCS10Attribute[] attrs = new PKCS10Attribute[len];
  84         for (int j = 0; j < len; j++) {
  85             attrs[j] = new PKCS10Attribute(ids[j], values[j]);
  86         }
  87         PKCS10 req = new PKCS10(publicKey, new PKCS10Attributes(attrs));
  88         System.out.println("List of attributes in constructed PKCS10 "
  89                 + "request: ");
  90         checkAttributes(req.getAttributes().getElements());
  91 
  92         // Encode the PKCS10 request and generate another PKCS10 request from
  93         // the encoded byte array
  94         req.encodeAndSign(subject, signature);
  95         PKCS10 resp = new PKCS10(req.getEncoded());
  96         System.out.println("List of attributes in DER encoded PKCS10 Request:");
  97         checkAttributes(resp.getAttributes().getElements());
  98 
  99         if (failedCount > 0) {
 100             throw new RuntimeException("Attributes Compared : Failed");
 101         }
 102         System.out.println("Attributes Compared : Pass");
 103     }
 104 
 105     static void checkAttributes(Enumeration attrs) {
 106         int numOfAttrs = 0;
 107         while (attrs.hasMoreElements()) {
 108             numOfAttrs ++;
 109             PKCS10Attribute attr = (PKCS10Attribute) attrs.nextElement();
 110 
 111             if (constructedMap.containsKey(attr.getAttributeId())) {
 112                 if (constructedMap.get(attr.getAttributeId()).
 113                         equals(attr.getAttributeValue())) {
 114                     System.out.print("AttributeId: " + attr.getAttributeId());
 115                     System.out.println(" AttributeValue: "
 116                             + attr.getAttributeValue());
 117                 } else {
 118                     failedCount++;
 119                     System.out.print("< AttributeId: " + attr.getAttributeId());
 120                     System.out.println("  AttributeValue: " + constructedMap.
 121                             get(attr.getAttributeId()));
 122                     System.out.print("< AttributeId: " + attr.getAttributeId());
 123                     System.out.println("  AttributeValue: "
 124                             + attr.getAttributeValue());
 125                 }
 126             } else {
 127                 failedCount++;
 128                 System.out.println("No " + attr.getAttributeId()
 129                         + " in DER encoded PKCS10 Request");
 130             }
 131         }
 132         if(numOfAttrs != constructedMap.size()){
 133             failedCount++;
 134             System.out.println("Incorrect number of attributes.");
 135 
 136         }
 137         System.out.println();
 138     }
 139 
 140 }