Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs/pkcs10/PKCS10AttrEncoding.java
41153 views
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 test DER encoding of PKCS10 attributes
28
* @modules java.base/sun.security.pkcs
29
* java.base/sun.security.pkcs10
30
* java.base/sun.security.util
31
* java.base/sun.security.x509
32
* @compile -XDignore.symbol.file PKCS10AttrEncoding.java
33
* @run main PKCS10AttrEncoding
34
*/
35
import java.security.KeyPair;
36
import java.security.KeyPairGenerator;
37
import java.security.PrivateKey;
38
import java.util.Enumeration;
39
import java.util.GregorianCalendar;
40
import java.util.HashMap;
41
import sun.security.pkcs.PKCS9Attribute;
42
import sun.security.pkcs10.PKCS10;
43
import sun.security.pkcs10.PKCS10Attribute;
44
import sun.security.pkcs10.PKCS10Attributes;
45
import sun.security.util.ObjectIdentifier;
46
import sun.security.x509.X500Name;
47
import sun.security.x509.X509Key;
48
49
public class PKCS10AttrEncoding {
50
51
static final ObjectIdentifier[] ids = {
52
PKCS9Attribute.CONTENT_TYPE_OID, // ContentType
53
PKCS9Attribute.SIGNING_TIME_OID, // SigningTime
54
PKCS9Attribute.CHALLENGE_PASSWORD_OID // ChallengePassword
55
};
56
static int failedCount = 0;
57
static HashMap<ObjectIdentifier, Object> constructedMap = new HashMap<>();
58
59
public static void main(String[] args) throws Exception {
60
61
// initializations
62
int len = ids.length;
63
Object[] values = {
64
ObjectIdentifier.of("1.2.3.4"),
65
new GregorianCalendar(1970, 1, 25, 8, 56, 7).getTime(),
66
"challenging"
67
};
68
for (int j = 0; j < len; j++) {
69
constructedMap.put(ids[j], values[j]);
70
}
71
72
X500Name subject = new X500Name("cn=Test");
73
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
74
String sigAlg = "DSA";
75
76
keyGen.initialize(512);
77
78
KeyPair pair = keyGen.generateKeyPair();
79
X509Key publicKey = (X509Key) pair.getPublic();
80
PrivateKey privateKey = pair.getPrivate();
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, privateKey, sigAlg);
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
}
141
142