author | weijun |
Tue, 16 Aug 2016 21:28:10 +0800 | |
changeset 40393 | cb883ad42653 |
parent 30820 | 0d4717a011d3 |
child 42338 | a60f280f803c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21342
diff
changeset
|
2 |
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6468285 |
|
27 |
* @summary keytool ability to backdate self-signed certificates to compensate for clock skew |
|
30820 | 28 |
* @modules java.base/sun.security.tools.keytool |
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10328
diff
changeset
|
29 |
* @compile -XDignore.symbol.file StartDateTest.java |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10328
diff
changeset
|
30 |
* @run main StartDateTest |
2 | 31 |
*/ |
32 |
||
33 |
import java.io.File; |
|
34 |
import java.io.FileInputStream; |
|
35 |
import java.lang.reflect.Method; |
|
36 |
import java.security.KeyStore; |
|
37 |
import java.security.cert.X509Certificate; |
|
38 |
import java.util.Calendar; |
|
39 |
import java.util.Date; |
|
40 |
import java.util.GregorianCalendar; |
|
41 |
||
42 |
public class StartDateTest { |
|
43 |
public static void main(String[] args) throws Exception { |
|
44 |
||
45 |
// Part 1: Test function |
|
46 |
Calendar cal = new GregorianCalendar(); |
|
47 |
int year = cal.get(Calendar.YEAR); |
|
48 |
int month = cal.get(Calendar.MONTH); |
|
49 |
||
50 |
new File("jks").delete(); |
|
51 |
||
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
52 |
run("one", "+1y"); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
53 |
cal.setTime(getIssueDate("one")); |
2 | 54 |
System.out.println(cal); |
55 |
if (cal.get(Calendar.YEAR) != year + 1) { |
|
91
2f9e71bc1bac
6643094: Test on keytool -startdate forgets about December
weijun
parents:
2
diff
changeset
|
56 |
throw new Exception("Function check #1 fails"); |
2 | 57 |
} |
58 |
||
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
59 |
run("two", "+1m"); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
60 |
cal.setTime(getIssueDate("two")); |
2 | 61 |
System.out.println(cal); |
91
2f9e71bc1bac
6643094: Test on keytool -startdate forgets about December
weijun
parents:
2
diff
changeset
|
62 |
if (cal.get(Calendar.MONTH) != (month + 1) % 12) { |
2f9e71bc1bac
6643094: Test on keytool -startdate forgets about December
weijun
parents:
2
diff
changeset
|
63 |
throw new Exception("Function check #2 fails"); |
2 | 64 |
} |
65 |
||
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
66 |
run("three", "2009/10/11 12:34:56"); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
67 |
cal.setTime(getIssueDate("three")); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
68 |
System.out.println(cal); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
69 |
if (cal.get(Calendar.YEAR) != 2009 || |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
70 |
cal.get(Calendar.MONTH) != Calendar.OCTOBER || |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
71 |
cal.get(Calendar.DAY_OF_MONTH) != 11 || |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
72 |
cal.get(Calendar.HOUR_OF_DAY) != 12 || |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
73 |
cal.get(Calendar.MINUTE) != 34 || |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
74 |
cal.get(Calendar.SECOND) != 56) { |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
75 |
throw new Exception("Function check #3 fails"); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
76 |
} |
2 | 77 |
|
78 |
// Part 2: Test format |
|
14182
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10328
diff
changeset
|
79 |
Method m = sun.security.tools.keytool.Main.class.getDeclaredMethod( |
3041082abb40
7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents:
10328
diff
changeset
|
80 |
"getStartDate", String.class); |
2 | 81 |
m.setAccessible(true); |
82 |
for (String s: new String[] { |
|
83 |
null, //NOW! |
|
84 |
"+1m+1d", |
|
85 |
"+1y-1m+1d", |
|
86 |
"+3H", |
|
87 |
"+1M", |
|
88 |
"-5M", |
|
89 |
"+011d", |
|
90 |
"+22S", |
|
91 |
"+500S", |
|
92 |
"2001/01/01", |
|
93 |
"15:15:15", |
|
94 |
"2001/01/01 11:11:11", |
|
95 |
}) { |
|
96 |
try { |
|
97 |
System.out.println(s + " " + m.invoke(null, s)); |
|
98 |
} catch (Exception e) { |
|
99 |
e.printStackTrace(); |
|
100 |
throw new Exception("Failed at " + s); |
|
101 |
} |
|
102 |
} |
|
103 |
for (String s: new String[] { |
|
104 |
"", // empty |
|
105 |
"+3", |
|
106 |
"+3m+", |
|
107 |
"+3m+3", |
|
108 |
"1m", // no sign |
|
109 |
"+0x011d", // hex number |
|
110 |
"+1m1d", // no sign for the 2nd sub value |
|
111 |
"m", |
|
112 |
"+1h", // h is not H |
|
113 |
"-1m1d", |
|
114 |
"-m", |
|
115 |
"x", |
|
116 |
"+1m +1d", |
|
117 |
"2007/07", |
|
118 |
"01:01", |
|
119 |
"+01:01:01", // what's this? |
|
120 |
"1:01:01", |
|
121 |
"12pm", |
|
122 |
"2007/07/07 12:12:12", // extra blank between |
|
123 |
"2001/01/01-11:11:11", |
|
124 |
"2007-07-07", // non-standard date delim |
|
125 |
"2007/7/7", // no padding |
|
126 |
"07/07/07", // year's length not 4 |
|
127 |
"1:1:1", |
|
128 |
}) { |
|
129 |
boolean failed = false; |
|
130 |
try { |
|
131 |
System.out.println(m.invoke(null, s)); |
|
132 |
} catch (Exception e) { |
|
133 |
System.out.println(s + " " + e.getCause()); |
|
134 |
failed = true; |
|
135 |
} |
|
136 |
if (!failed) throw new Exception("Failed at " + s); |
|
137 |
} |
|
138 |
} |
|
139 |
||
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
140 |
// The keytool command line template, alias and startdate TBD |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
141 |
static String[] cmd = ("-alias tbd -startdate tbd -keystore jks " + |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
142 |
"-storetype jks -storepass changeit -keypass changeit " + |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
143 |
"-keyalg rsa -genkeypair -dname CN=Haha -debug").split(" "); |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
144 |
|
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
145 |
static void run(String alias, String startDate) throws Exception { |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
146 |
cmd[1] = alias; |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
147 |
cmd[3] = startDate; |
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
148 |
sun.security.tools.keytool.Main.main(cmd); |
2 | 149 |
} |
150 |
||
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
151 |
static Date getIssueDate(String alias) throws Exception { |
2 | 152 |
KeyStore ks = KeyStore.getInstance("jks"); |
10328 | 153 |
try (FileInputStream fis = new FileInputStream("jks")) { |
154 |
ks.load(fis, "changeit".toCharArray()); |
|
155 |
} |
|
40393
cb883ad42653
8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents:
30820
diff
changeset
|
156 |
X509Certificate cert = (X509Certificate)ks.getCertificate(alias); |
2 | 157 |
return cert.getNotBefore(); |
158 |
} |
|
159 |
} |