Path: blob/master/test/jdk/sun/security/x509/X509CertImpl/CertificateValidation.java
41153 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 823845226* @modules java.base/sun.security.x50927* java.base/sun.security.tools.keytool28* @summary This test generates V3 certificate with certain validity period29* and checks whether the validity has expired or not.30*/3132import sun.security.tools.keytool.CertAndKeyGen;33import java.security.cert.X509Certificate;34import java.util.Calendar;35import java.util.Date;36import java.util.TimeZone;37import sun.security.x509.X509CertImpl;38import sun.security.x509.X500Name;394041public class CertificateValidation {4243public static void main(String[] args) throws Exception {4445Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));46cal.set(2050, 00, 01, 01, 00, 00);47Date lastDate = cal.getTime();48// Seconds till lastDate plus one hour49long validity = (lastDate.getTime() - System.currentTimeMillis())/1000L + 3600;50Date firstDate = new Date(lastDate.getTime() - validity * 1000L);51CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA256withRSA");52ckg.generate(2048);53X509Certificate crt = ckg.getSelfCertificate(54new X500Name("CN=Me"), firstDate, validity);55byte[] encoded = crt.getEncoded();56X509CertImpl certImpl = new X509CertImpl(encoded);57certImpl.checkValidity();58}59}606162