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/tools/keytool/StartDateTest.java
38853 views
1
/*
2
* Copyright (c) 2007, 2013, 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 6468285
27
* @summary keytool ability to backdate self-signed certificates to compensate for clock skew
28
* @compile -XDignore.symbol.file StartDateTest.java
29
* @run main StartDateTest
30
*/
31
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.lang.reflect.Method;
35
import java.security.KeyStore;
36
import java.security.cert.X509Certificate;
37
import java.util.Calendar;
38
import java.util.Date;
39
import java.util.GregorianCalendar;
40
41
public class StartDateTest {
42
public static void main(String[] args) throws Exception {
43
44
// Part 1: Test function
45
Calendar cal = new GregorianCalendar();
46
int year = cal.get(Calendar.YEAR);
47
int month = cal.get(Calendar.MONTH);
48
49
new File("jks").delete();
50
51
run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
52
"-keyalg rsa -genkeypair -dname CN=Haha -startdate +1y");
53
cal.setTime(getIssueDate());
54
System.out.println(cal);
55
if (cal.get(Calendar.YEAR) != year + 1) {
56
throw new Exception("Function check #1 fails");
57
}
58
59
run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
60
"-selfcert -startdate +1m");
61
cal.setTime(getIssueDate());
62
System.out.println(cal);
63
if (cal.get(Calendar.MONTH) != (month + 1) % 12) {
64
throw new Exception("Function check #2 fails");
65
}
66
67
new File("jks").delete();
68
69
// Part 2: Test format
70
Method m = sun.security.tools.keytool.Main.class.getDeclaredMethod(
71
"getStartDate", String.class);
72
m.setAccessible(true);
73
for (String s: new String[] {
74
null, //NOW!
75
"+1m+1d",
76
"+1y-1m+1d",
77
"+3H",
78
"+1M",
79
"-5M",
80
"+011d",
81
"+22S",
82
"+500S",
83
"2001/01/01",
84
"15:15:15",
85
"2001/01/01 11:11:11",
86
}) {
87
try {
88
System.out.println(s + " " + m.invoke(null, s));
89
} catch (Exception e) {
90
e.printStackTrace();
91
throw new Exception("Failed at " + s);
92
}
93
}
94
for (String s: new String[] {
95
"", // empty
96
"+3",
97
"+3m+",
98
"+3m+3",
99
"1m", // no sign
100
"+0x011d", // hex number
101
"+1m1d", // no sign for the 2nd sub value
102
"m",
103
"+1h", // h is not H
104
"-1m1d",
105
"-m",
106
"x",
107
"+1m +1d",
108
"2007/07",
109
"01:01",
110
"+01:01:01", // what's this?
111
"1:01:01",
112
"12pm",
113
"2007/07/07 12:12:12", // extra blank between
114
"2001/01/01-11:11:11",
115
"2007-07-07", // non-standard date delim
116
"2007/7/7", // no padding
117
"07/07/07", // year's length not 4
118
"1:1:1",
119
}) {
120
boolean failed = false;
121
try {
122
System.out.println(m.invoke(null, s));
123
} catch (Exception e) {
124
System.out.println(s + " " + e.getCause());
125
failed = true;
126
}
127
if (!failed) throw new Exception("Failed at " + s);
128
}
129
}
130
131
static void run(String s) throws Exception {
132
sun.security.tools.keytool.Main.main((s+" -debug").split(" "));
133
}
134
135
static Date getIssueDate() throws Exception {
136
KeyStore ks = KeyStore.getInstance("jks");
137
try (FileInputStream fis = new FileInputStream("jks")) {
138
ks.load(fis, "changeit".toCharArray());
139
}
140
X509Certificate cert = (X509Certificate)ks.getCertificate("me");
141
return cert.getNotBefore();
142
}
143
}
144
145