Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs/pkcs7/SignerOrder.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
27
* @summary test PKCS7 data signing, encoding and verification
28
* @library /test/lib
29
* @modules java.base/sun.security.pkcs
30
* java.base/sun.security.util
31
* java.base/sun.security.x509
32
* @run main SignerOrder
33
*/
34
import java.io.ByteArrayOutputStream;
35
import java.io.IOException;
36
import java.math.BigInteger;
37
import java.security.KeyPair;
38
import java.security.KeyPairGenerator;
39
import java.security.PrivateKey;
40
import java.security.Signature;
41
import java.security.SignatureException;
42
import java.security.cert.X509Certificate;
43
import java.util.Date;
44
import sun.security.pkcs.ContentInfo;
45
import sun.security.pkcs.PKCS7;
46
import sun.security.pkcs.SignerInfo;
47
import sun.security.util.DerOutputStream;
48
import sun.security.x509.AlgorithmId;
49
import sun.security.x509.CertificateAlgorithmId;
50
import sun.security.x509.CertificateSerialNumber;
51
import sun.security.x509.CertificateValidity;
52
import sun.security.x509.CertificateVersion;
53
import sun.security.x509.CertificateX509Key;
54
import sun.security.x509.X500Name;
55
import sun.security.x509.X509CertImpl;
56
import sun.security.x509.X509CertInfo;
57
import sun.security.x509.X509Key;
58
import jdk.test.lib.hexdump.HexPrinter;
59
60
public class SignerOrder {
61
62
//signer infos
63
static final byte[] data1 = "12345".getBytes();
64
static final byte[] data2 = "abcde".getBytes();
65
66
public static void main(String[] argv) throws Exception {
67
68
SignerInfo[] signerInfos = new SignerInfo[9];
69
SimpleSigner signer1 = new SimpleSigner(null, null, null, null);
70
signerInfos[8] = signer1.genSignerInfo(data1);
71
signerInfos[7] = signer1.genSignerInfo(new byte[]{});
72
signerInfos[6] = signer1.genSignerInfo(data2);
73
74
SimpleSigner signer2 = new SimpleSigner(null, null, null, null);
75
signerInfos[5] = signer2.genSignerInfo(data1);
76
signerInfos[4] = signer2.genSignerInfo(new byte[]{});
77
signerInfos[3] = signer2.genSignerInfo(data2);
78
79
SimpleSigner signer3 = new SimpleSigner(null, null, null, null);
80
signerInfos[2] = signer3.genSignerInfo(data1);
81
signerInfos[1] = signer3.genSignerInfo(new byte[]{});
82
signerInfos[0] = signer3.genSignerInfo(data2);
83
84
ContentInfo contentInfo = new ContentInfo(data1);
85
86
AlgorithmId[] algIds = {new AlgorithmId(AlgorithmId.SHA256_oid)};
87
88
X509Certificate[] certs = {signer3.getCert(), signer2.getCert(),
89
signer1.getCert()};
90
91
PKCS7 pkcs71 = new PKCS7(algIds, contentInfo,
92
certs,
93
signerInfos);
94
95
System.out.println("SignerInfos in original.");
96
printSignerInfos(pkcs71.getSignerInfos());
97
98
DerOutputStream out = new DerOutputStream();
99
pkcs71.encodeSignedData(out);
100
101
PKCS7 pkcs72 = new PKCS7(out.toByteArray());
102
System.out.println("\nSignerInfos read back in:");
103
printSignerInfos(pkcs72.getSignerInfos());
104
105
System.out.println("Verified signers of original:");
106
SignerInfo[] verifs1 = pkcs71.verify();
107
108
System.out.println("Verified signers of after read-in:");
109
SignerInfo[] verifs2 = pkcs72.verify();
110
111
if (verifs1.length != verifs2.length) {
112
throw new RuntimeException("Length or Original vs read-in "
113
+ "should be same");
114
}
115
}
116
117
static void printSignerInfos(SignerInfo signerInfo) throws IOException {
118
ByteArrayOutputStream strm = new ByteArrayOutputStream();
119
signerInfo.derEncode(strm);
120
System.out.println("SignerInfo, length: "
121
+ strm.toByteArray().length);
122
HexPrinter.simple().format(strm.toByteArray());
123
System.out.println("\n");
124
strm.reset();
125
}
126
127
static void printSignerInfos(SignerInfo[] signerInfos) throws IOException {
128
ByteArrayOutputStream strm = new ByteArrayOutputStream();
129
for (int i = 0; i < signerInfos.length; i++) {
130
signerInfos[i].derEncode(strm);
131
System.out.println("SignerInfo[" + i + "], length: "
132
+ strm.toByteArray().length);
133
HexPrinter.simple().format(strm.toByteArray());
134
System.out.println("\n");
135
strm.reset();
136
}
137
}
138
139
}
140
141
/**
142
* A simple extension of sun.security.x509.X500Signer that adds a no-fuss
143
* signing algorithm.
144
*/
145
class SimpleSigner {
146
147
private final Signature sig;
148
private final X500Name agent;
149
private final AlgorithmId digestAlgId;
150
private final AlgorithmId encryptionAlgId;
151
private final AlgorithmId algId; // signature algid;
152
//combines digest + encryption
153
private final X509Key publicKey;
154
private final PrivateKey privateKey;
155
private final X509Certificate cert;
156
157
public SimpleSigner(String digestAlg,
158
String encryptionAlg,
159
KeyPair keyPair,
160
X500Name agent) throws Exception {
161
162
if (agent == null) {
163
agent = new X500Name("cn=test");
164
}
165
if (digestAlg == null) {
166
digestAlg = "SHA";
167
}
168
if (encryptionAlg == null) {
169
encryptionAlg = "DSA";
170
}
171
if (keyPair == null) {
172
KeyPairGenerator keyGen =
173
KeyPairGenerator.getInstance(encryptionAlg);
174
keyGen.initialize(1024);
175
keyPair = keyGen.generateKeyPair();
176
}
177
publicKey = (X509Key) keyPair.getPublic();
178
privateKey = keyPair.getPrivate();
179
180
if ("DSA".equals(encryptionAlg)) {
181
this.sig = Signature.getInstance(encryptionAlg);
182
} else { // RSA
183
this.sig = Signature.getInstance(digestAlg + "/" + encryptionAlg);
184
}
185
this.sig.initSign(privateKey);
186
187
this.agent = agent;
188
this.digestAlgId = AlgorithmId.get(digestAlg);
189
this.encryptionAlgId = AlgorithmId.get(encryptionAlg);
190
this.algId = AlgorithmId.get(this.sig.getAlgorithm());
191
192
this.cert = getSelfCert();
193
}
194
195
/**
196
* Take the data and sign it.
197
*
198
* @param buf buffer holding the next chunk of the data to be signed
199
* @param offset starting point of to-be-signed data
200
* @param len how many bytes of data are to be signed
201
* @return the signature for the input data.
202
* @exception SignatureException on errors.
203
*/
204
public byte[] simpleSign(byte[] buf, int offset, int len)
205
throws SignatureException {
206
sig.update(buf, offset, len);
207
return sig.sign();
208
}
209
210
/**
211
* Returns the digest algorithm used to sign.
212
*/
213
public AlgorithmId getDigestAlgId() {
214
return digestAlgId;
215
}
216
217
/**
218
* Returns the encryption algorithm used to sign.
219
*/
220
public AlgorithmId getEncryptionAlgId() {
221
return encryptionAlgId;
222
}
223
224
/**
225
* Returns the name of the signing agent.
226
*/
227
public X500Name getSigner() {
228
return agent;
229
}
230
231
public X509Certificate getCert() {
232
return cert;
233
}
234
235
private X509Certificate getSelfCert() throws Exception {
236
long validity = 1000;
237
X509CertImpl certLocal;
238
Date firstDate, lastDate;
239
240
firstDate = new Date();
241
lastDate = new Date();
242
lastDate.setTime(lastDate.getTime() + validity + 1000);
243
244
CertificateValidity interval = new CertificateValidity(firstDate,
245
lastDate);
246
247
X509CertInfo info = new X509CertInfo();
248
// Add all mandatory attributes
249
info.set(X509CertInfo.VERSION,
250
new CertificateVersion(CertificateVersion.V1));
251
info.set(X509CertInfo.SERIAL_NUMBER,
252
new CertificateSerialNumber(
253
(int) (firstDate.getTime() / 1000)));
254
info.set(X509CertInfo.ALGORITHM_ID,
255
new CertificateAlgorithmId(algId));
256
info.set(X509CertInfo.SUBJECT, agent);
257
info.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
258
info.set(X509CertInfo.VALIDITY, interval);
259
info.set(X509CertInfo.ISSUER, agent);
260
261
certLocal = new X509CertImpl(info);
262
certLocal.sign(privateKey, algId.getName());
263
264
return certLocal;
265
}
266
267
public SignerInfo genSignerInfo(byte[] data) throws SignatureException {
268
return new SignerInfo((X500Name) cert.getIssuerDN(),
269
new BigInteger("" + cert.getSerialNumber()),
270
getDigestAlgId(), algId,
271
simpleSign(data, 0, data.length));
272
}
273
}
274
275