Skip to content

Commit c395fb4

Browse files
author
duke
committed
Automatic merge of jdk:master into master
2 parents 31c4a87 + b21b96d commit c395fb4

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (c) 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+
package org.openjdk.bench.java.security;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.math.BigInteger;
28+
import java.security.*;
29+
import java.security.cert.Certificate;
30+
import java.security.cert.CertificateFactory;
31+
import java.security.spec.PKCS8EncodedKeySpec;
32+
import java.util.concurrent.TimeUnit;
33+
34+
import org.openjdk.jmh.annotations.*;
35+
36+
/**
37+
* Tests various algorithm settings for PKCS12 keystores.
38+
*/
39+
@State(Scope.Benchmark)
40+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
41+
@Warmup(iterations = 2)
42+
@Measurement(iterations = 10)
43+
@BenchmarkMode(Mode.AverageTime)
44+
@Fork(jvmArgsAppend = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5)
45+
public class PKCS12KeyStores {
46+
47+
private static final char[] PASS = "changeit".toCharArray();
48+
49+
private Key pk;
50+
private Certificate[] certs;
51+
52+
// Several pkcs12 keystores in byte arrays
53+
private byte[] bw2048;
54+
private byte[] bw50000; // Default old
55+
private byte[] bs50000;
56+
private byte[] bs10000; // Default new
57+
private byte[] bs2048;
58+
59+
// Decodes HEX string to byte array
60+
private static byte[] xeh(String in) {
61+
return new BigInteger(in, 16).toByteArray();
62+
}
63+
64+
@Setup
65+
public void setup() throws Exception {
66+
// Just generate a keypair and dump getEncoded() of key and cert.
67+
byte[] x1 = xeh("3041020100301306072A8648CE3D020106082A8648CE3D03" +
68+
"0107042730250201010420B561D1FBE150488508BBE8FF4540F09057" +
69+
"58712F5D2D3CC80F9A15BA5D481117");
70+
byte[] x2 = xeh("3082012D3081D5A00302010202084EE6ECC5585640A7300A" +
71+
"06082A8648CE3D040302300C310A30080603550403130161301E170D" +
72+
"3230313131373230343730355A170D3233303831343230343730355A" +
73+
"300C310A300806035504031301613059301306072A8648CE3D020106" +
74+
"082A8648CE3D030107034200041E761F511841602E272B40A021995D" +
75+
"1BD828DDC7F71412D6A66CC0CB858C856D32C58273E494676D1D2B05" +
76+
"B8E9B08207A122265C2AA5FCBDCE19E5E88CA7A1B6A321301F301D06" +
77+
"03551D0E04160414173F278D77096E5C8EA182D12F147694587B5D9A" +
78+
"300A06082A8648CE3D04030203470030440220760CEAF1FA7041CB8C" +
79+
"1CA80AF60E4F9C9D5136D96B2AF0AAA9440F79561C44E502205D5C72" +
80+
"886C92B95A681C4393C67AAEC8DA9FD7910FF9BF2BCB721AE71D1B6F88");
81+
KeyFactory kf = KeyFactory.getInstance("EC");
82+
pk = kf.generatePrivate(new PKCS8EncodedKeySpec(x1));
83+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
84+
certs = new Certificate[]{cf.generateCertificate(new ByteArrayInputStream(x2))};
85+
86+
bw2048 = outweak2048();
87+
bw50000 = outweak50000_Old();
88+
bs50000 = outstrong50000();
89+
bs10000 = outstrong10000_New();
90+
bs2048 = outstrong2048();
91+
}
92+
93+
// Reads in a pkcs12 keystore
94+
private KeyStore in(byte[] b) throws Exception {
95+
KeyStore ks = KeyStore.getInstance("pkcs12");
96+
ks.load(new ByteArrayInputStream(b), PASS);
97+
if (!ks.getCertificate("a").getPublicKey().getAlgorithm().equals(
98+
ks.getKey("a", PASS).getAlgorithm())) {
99+
throw new RuntimeException("Not same alg");
100+
}
101+
return ks;
102+
}
103+
104+
// Generates a pkcs12 keystore with the specified algorithm/ic
105+
private byte[] out(String cAlg, String cIc, String kAlg, String kIc,
106+
String mAlg, String mIc) throws Exception {
107+
System.setProperty("keystore.pkcs12.certProtectionAlgorithm", cAlg);
108+
System.setProperty("keystore.pkcs12.certPbeIterationCount", cIc);
109+
System.setProperty("keystore.pkcs12.keyProtectionAlgorithm", kAlg);
110+
System.setProperty("keystore.pkcs12.keyPbeIterationCount", kIc);
111+
System.setProperty("keystore.pkcs12.macAlgorithm", mAlg);
112+
System.setProperty("keystore.pkcs12.macIterationCount", mIc);
113+
KeyStore ks = KeyStore.getInstance("pkcs12");
114+
ks.load(null, null);
115+
ks.setKeyEntry("a", pk, PASS, certs);
116+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
117+
ks.store(bout, PASS);
118+
return bout.toByteArray();
119+
}
120+
121+
// Benchmark methods start here:
122+
123+
// Reading a keystore
124+
@Benchmark
125+
public KeyStore inweak2048() throws Exception {
126+
return in(bw2048);
127+
}
128+
129+
@Benchmark
130+
public KeyStore inweak50000_Old() throws Exception {
131+
return in(bw50000);
132+
}
133+
134+
@Benchmark
135+
public KeyStore instrong50000() throws Exception {
136+
return in(bs50000);
137+
}
138+
139+
@Benchmark
140+
public KeyStore instrong10000_New() throws Exception {
141+
return in(bs10000);
142+
}
143+
144+
@Benchmark
145+
public KeyStore instrong2048() throws Exception {
146+
return in(bs2048);
147+
}
148+
149+
// Writing a keystore
150+
@Benchmark
151+
public byte[] outweak2048() throws Exception {
152+
return out("PBEWithSHA1AndRC2_40", "2048",
153+
"PBEWithSHA1AndDESede", "2048",
154+
"HmacPBESHA1", "2048");
155+
}
156+
157+
@Benchmark
158+
public byte[] outweak50000_Old() throws Exception {
159+
return out("PBEWithSHA1AndRC2_40", "50000",
160+
"PBEWithSHA1AndDESede", "50000",
161+
"HmacPBESHA1", "100000");
162+
// Attention: 100000 is old default Mac ic
163+
}
164+
165+
@Benchmark
166+
public byte[] outstrong50000() throws Exception {
167+
return out("PBEWithHmacSHA256AndAES_256", "50000",
168+
"PBEWithHmacSHA256AndAES_256", "50000",
169+
"HmacPBESHA256", "100000");
170+
// Attention: 100000 is old default Mac ic
171+
}
172+
173+
@Benchmark
174+
public byte[] outstrong10000_New() throws Exception {
175+
return out("PBEWithHmacSHA256AndAES_256", "10000",
176+
"PBEWithHmacSHA256AndAES_256", "10000",
177+
"HmacPBESHA256", "10000");
178+
}
179+
180+
@Benchmark
181+
public byte[] outstrong2048() throws Exception {
182+
return out("PBEWithHmacSHA256AndAES_256", "2048",
183+
"PBEWithHmacSHA256AndAES_256", "2048",
184+
"HmacPBESHA256", "2048");
185+
}
186+
}

0 commit comments

Comments
 (0)