Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an "expired" SSL certificate with keytool

I am creating my keystore with following command:

keytool -genkey -keystore myStore.keystore -keyalg RSA -keysize 1024 -alias myAlias

How could I generate one with a past expiry date (the use of this? I want to test the behavior of my app with an expired certificate).

like image 504
ptpdlc Avatar asked Dec 27 '12 09:12

ptpdlc


People also ask

How do I create an expired certificate?

You must use the openssl command to create a self-signed certificate that expires in a different value than the default value of 10 years. To do so, you must perform the following procedure: Create a private key and self-signed certificate using the openssl command.

How do I extend certificate expiration using Keytool?

When creating a new self-signed certificate and keystore using Java's keytool command, the default validity is 90 days. In order to extend this, you can modify the keystore creation command to include the validity parameter.


3 Answers

You can generate expired certificate using keytool command by using the following parameters.

-startdate

-validity

while validity parameter takes only number of days as input, startdate parameter can be used to mention since when validity begins. The format for input to startdate parameter [yyyy/mm/dd][HH:MM:SS]

Refer to this link for details http://docs.oracle.com/javase/7/docs/technotes/tools/windows/keytool.html

like image 173
shyam0191 Avatar answered Oct 12 '22 21:10

shyam0191


Using the java keytool, the minimum validity for a keystore certificate can be 1 day.

EDIT: looks like there's an option for -startdate as @shyam0191 has answered.

So, you can't(correction: you can actually) generate a certificate with a past date. I suggest using the following command, which will generate a certificate with a 1-day validity and the next day you will be able to test with it:

keytool -selfcert -alias Test -genkey -keystore myStore.keystore -keyalg RSA -validity 1

or use @shyam0191's answer which will have the same end result in the end (but sooner).

like image 30
bazyle Avatar answered Oct 12 '22 23:10

bazyle


You can use below openssl commands to generate expired certificates, which mimics the official process to sign certificates.

Note: Only tested on Linux.

Assume yourself as a CA

#Create CA key, which means you are now the CA using root.key and root.cer to sign certificates
openssl genrsa 4096 > root.key
#Create CA certificate expired ten years later
openssl req -new -x509 -key root.key -out root.cer -days 3650

Now, you are the one applying a signed certificate from CA

#Generates your own private key 
openssl genrsa 4096 > server.key
#Build a Certificate Signing Request
openssl req -new -key server.key -out server.csr

Now you are the CA again

#sign the certificate and make the certificate expired 1 day ago. Pay attention to the negative -days argument( not working on MacOS )
openssl x509 -req -in server.csr -CA root.cer -CAkey root.key -CAcreateserial -out server.cer -days -1

Then you can check the dates

openssl x509 -noout -text -in server.cer

Validity Not Before: Mar 7 09:11:13 2019 GMT Not After : Mar 6 09:11:13 2019 GMT

like image 20
Popeye Avatar answered Oct 12 '22 23:10

Popeye