Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/AlgorithmId/NonStandardNames.java
38853 views
1
/*
2
* Copyright (c) 2012, 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 7180907
27
* @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
28
* @compile -XDignore.symbol.file NonStandardNames.java
29
* @run main NonStandardNames
30
*/
31
32
import java.security.MessageDigest;
33
import java.security.Signature;
34
import java.security.cert.X509Certificate;
35
import sun.security.pkcs.ContentInfo;
36
import sun.security.pkcs.PKCS7;
37
import sun.security.pkcs.PKCS9Attribute;
38
import sun.security.pkcs.PKCS9Attributes;
39
import sun.security.pkcs.SignerInfo;
40
import sun.security.tools.keytool.CertAndKeyGen;
41
import sun.security.x509.AlgorithmId;
42
import sun.security.x509.X500Name;
43
44
public class NonStandardNames {
45
46
public static void main(String[] args) throws Exception {
47
48
byte[] data = "Hello".getBytes();
49
X500Name n = new X500Name("cn=Me");
50
51
CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");
52
cakg.generate(1024);
53
X509Certificate cert = cakg.getSelfCertificate(n, 1000);
54
55
MessageDigest md = MessageDigest.getInstance("SHA-256");
56
PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{
57
new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),
58
new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),
59
});
60
61
Signature s = Signature.getInstance("SHA256withRSA");
62
s.initSign(cakg.getPrivateKey());
63
s.update(authed.getDerEncoding());
64
byte[] sig = s.sign();
65
66
SignerInfo signerInfo = new SignerInfo(
67
n,
68
cert.getSerialNumber(),
69
AlgorithmId.get("SHA-256"),
70
authed,
71
AlgorithmId.get("SHA256withRSA"),
72
sig,
73
null
74
);
75
76
PKCS7 pkcs7 = new PKCS7(
77
new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},
78
new ContentInfo(data),
79
new X509Certificate[] {cert},
80
new SignerInfo[] {signerInfo});
81
82
if (pkcs7.verify(signerInfo, data) == null) {
83
throw new Exception("Not verified");
84
}
85
}
86
}
87
88