325N/A/*
325N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/A/*
325N/A * @test
325N/A * @bug 6468285
325N/A * @summary keytool ability to backdate self-signed certificates to compensate for clock skew
325N/A */
325N/A
325N/Aimport java.io.File;
325N/Aimport java.io.FileInputStream;
325N/Aimport java.lang.reflect.Method;
325N/Aimport java.security.KeyStore;
325N/Aimport java.security.cert.X509Certificate;
325N/Aimport java.util.Calendar;
325N/Aimport java.util.Date;
325N/Aimport java.util.GregorianCalendar;
325N/Aimport sun.security.tools.KeyTool;
325N/A
325N/Apublic class StartDateTest {
325N/A public static void main(String[] args) throws Exception {
325N/A
325N/A // Part 1: Test function
325N/A Calendar cal = new GregorianCalendar();
325N/A int year = cal.get(Calendar.YEAR);
325N/A int month = cal.get(Calendar.MONTH);
325N/A
325N/A new File("jks").delete();
325N/A
325N/A run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
325N/A "-genkeypair -dname CN=Haha -startdate +1y");
325N/A cal.setTime(getIssueDate());
System.out.println(cal);
if (cal.get(Calendar.YEAR) != year + 1) {
throw new Exception("Function check #1 fails");
}
run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
"-selfcert -startdate +1m");
cal.setTime(getIssueDate());
System.out.println(cal);
if (cal.get(Calendar.MONTH) != (month + 1) % 12) {
throw new Exception("Function check #2 fails");
}
new File("jks").delete();
// Part 2: Test format
Method m = KeyTool.class.getDeclaredMethod("getStartDate", String.class);
m.setAccessible(true);
for (String s: new String[] {
null, //NOW!
"+1m+1d",
"+1y-1m+1d",
"+3H",
"+1M",
"-5M",
"+011d",
"+22S",
"+500S",
"2001/01/01",
"15:15:15",
"2001/01/01 11:11:11",
}) {
try {
System.out.println(s + " " + m.invoke(null, s));
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Failed at " + s);
}
}
for (String s: new String[] {
"", // empty
"+3",
"+3m+",
"+3m+3",
"1m", // no sign
"+0x011d", // hex number
"+1m1d", // no sign for the 2nd sub value
"m",
"+1h", // h is not H
"-1m1d",
"-m",
"x",
"+1m +1d",
"2007/07",
"01:01",
"+01:01:01", // what's this?
"1:01:01",
"12pm",
"2007/07/07 12:12:12", // extra blank between
"2001/01/01-11:11:11",
"2007-07-07", // non-standard date delim
"2007/7/7", // no padding
"07/07/07", // year's length not 4
"1:1:1",
}) {
boolean failed = false;
try {
System.out.println(m.invoke(null, s));
} catch (Exception e) {
System.out.println(s + " " + e.getCause());
failed = true;
}
if (!failed) throw new Exception("Failed at " + s);
}
}
static void run(String s) throws Exception {
KeyTool.main((s+" -debug").split(" "));
}
static Date getIssueDate() throws Exception {
KeyStore ks = KeyStore.getInstance("jks");
try (FileInputStream fis = new FileInputStream("jks")) {
ks.load(fis, "changeit".toCharArray());
}
X509Certificate cert = (X509Certificate)ks.getCertificate("me");
return cert.getNotBefore();
}
}