понеділок, 29 березня 2021 р.

How to Generate a Keystore and CSR Using the Keytool Command

Keytool Command/Utility to generate a keystore/certificate request(CSR)

How to generate a Keystore/CSR using keytool command/utility

 

certttt

Keytool Utility:

Keytool is a key and certificate management JDK utility which helps in managing a keystore of private/public keys and associated certificates. It allows users to administer their own public/private key pairs and associated certificates for use in self-authentication (where the user authenticates himself/herself to other users/services) or data integrity and authentication services, using digital signatures. It also allows users to cache the public keys (in the form of certificates) of their communicating peers.

Java Keytool stores the keys and certificates in what is called a keystore. Java keystore is implemented as a file by default. It protects private keys with a password.

keytool also enables users to administer secret keys used in symmetric encryption/decryption (e.g. RSA,DES).

Keytool keystore contains the private key and any certificates necessary to complete a chain of trust and establish the trustworthiness of the primary certificate.

All certificates in a Java keystore is associated with a unique alias. Which will be used as a pointer to later perform any of the keytool operation to import/export/delete/change  certificates/key etc.

Keytool Options:

The various keytool options are listed below

KEYTOOL OPTIONSDESCRIPTION
-deleteDeletes an entry from the Keystore
-exportcertExports a certificate from a Keystore
-genkeypairGenerates a key pair
-genseckeyGenerates a secret key pair
-gencertGenerates a certificate from a certificate request
-importcertImport a certificate or a certificate chain to keystore
-importpassImports a password
-importkeystore    Imports one or all entries from another keystore to a keystore
-keypasswdChanges the key password of an entry in keystore
-listLists entries in a keystore
-printcertPrints the content of a certificate
-printcertreqPrints the content of a certificate request
-printcrlPrints the content of a CRL file
-storepasswdChanges the store password of a keystore

 

Various Steps to process the Keystore , CSR and the signed certificate.

 

certttt

  • Create a keystore which contains private key

  • Generate a CSR (Certificate Signing Request) from keystore

  • Generate Signed Primary/Server Certificate from Certificate Authority

  • Import the Primary/Server certificate, root and intermediate CA certificates to keystore.

  • Share the certificate or root certificates to system which use the SSL to communicate to your system/application.

 

Create a keystore using Keytool:

While we create a Java keystore we will first create the .jks file that will initially only contain the private key using keytool utility.

keytool -genkey -keystore keystore.jks -alias ssl -keyalg RSA -sigalg SHA256withRSA -validity 365 -keysize 2048

-alias is an option to mention an Alias Name to your key entry

-keyalg specifies the algorithm to be used to generate the key pair

-keysize specifies the size of each key to be generated.

-sigalg specifies the algorithm that should be used to sign the self-signed certificate; this algorithm must be compatible with keyalg.

validity specifies the validity of the keystore which you want to create.

 

When you execute the command we will be prompted with question which we need answer to add as the key details such as Common Name(website/Application dns name), Organisation, Country,State, province, Country code etc. This is user defined values. Also it will be prompt you to enter keystore and key password which should be used in future to read/write/modify the keystore.

keystore creation

As the keystore name is mentioned keystore.jks while creating keystore.jks file will be created in the current folder.

keystorejks

Use below command to list the entries in keystore to view the content. We will be able to see the entered values reflected on the private key entries on the keystore.jks file.

keytool -list -v -keystore keystore.jks 

keystoreview

 

Generate a CSR (Certificate Signing Request) from keystore:

Next step is to create a Certificate Signing Request(CSR) from the created keystore to share with Certificate Authority(CA) to sign and generate the Primary/Server certificate.

keytool -certreq -alias ssl -keystore keystore.jks -file javaappperfomance.csr

We need to pass the correct alias name and password which we mentioned during the creation of the keystore to extract the certificate request.

csrgeneratationn

csrfile

 

Generate Signed Primary/Server Certificate from Certificate Authority

 

Submit the generated CSR to any of the CA authority which is supported by the SSL community to get the signed the Primary/Server certificate. The CA authority will be selected based on the organisations or your personnel selection.

Top 10 Certificate Authority in the world (Info based on 2017 from wikipedia)

RankIssuer
1Comodo
2IdenTrust
3Symantec
4GoDaddy
5GlobalSign
6DigiCert
7Certum
8Entrust
9Secom
10Actalis

 

Import the Primary/Server certificate, root and intermediate CA certificates to keystore

Once the CA signed the certificate and share it with us, we need to import the certificate to the keystore for the privatekey entry we created.

Below keytool commands can be used to import the signed certificate to keystore, we should use the alias name same as the alias name on the private key entry.

keytool -import -alias ssl -keystore keystore.jks -file javaappperfomance.crt  
keytool -import -trustcacerts -alias ssl -file javaappperfomance.crt -keystore keystore.jks

Second one is optional using -trustcacerts.

If the –trustcacerts option has been specified, additional certificates are considered for the chain of trust, namely the certificates in a file named “cacerts

If the alias does not point to a key entry, then keytool assumes you are adding a trusted certificate entry. In this case, the alias should not already exist in the keystore. If the alias does already exist, then keytool outputs an error, since there is already a trusted certificate for that alias, and does not import the certificate.

If the alias points to a key entry, then keytool assumes you are importing a certificate reply.

The old chain can only be replaced if a valid keypass, the password used to protect the private key of the entry, is supplied. If no password is provided, and the private key password is different from the keystore password, the user will be prompted for it.

Import a root or intermediate CA certificate to an existing Java keystore

keytool -import -trustcacerts -alias root -file entrust.cer -keystore keystore.jks

entrust(CA) is used as an example, File will be different and supplied by the Certificate Authority(CA) based on your CA.

entrustcert

To View/List the certificate we have added below command can be used

keytool -list -v -keystore keystore.jks 

Share the certificate or root certificates to system which use the SSL to communicate to your system/application.

As you have created a new Private/Public key for you DNS name we need to share the certificate with any of the interfacing applications (Not Browser as it will be having CA root/intermediate on its list).

 

Important commands for keytool which can be used while create/import/export/delete/change certificate in a keystore.

  • Generate a Java keystore and key pair:
keytool -genkey -alias aliasname -keyalg RSA -keystore keystore.jks -keysize 2048
  • Generate a certificate signing request (CSR) for an existing Java keystore: 
keytool -certreq -alias aliasname -keystore keystore.jks -file domainname.csr
  • Generate a keystore and self-signed certificate:
keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
  • View/List the certificate we have added below command can be used 
 keytool -list -v -keystore keystore.jks 
  • Import a root or intermediate CA certificate to an existing Java keystore
keytool -import -trustcacerts -alias root -file domainname.crt -keystore keystore.jks
  • Delete a certificate from a Java Keytool keystore
keytool -delete -alias aliasname -keystore keystore.jks
  • Change a Java keystore password
keytool -storepasswd -new new_storepass -keystore keystore.jks
  • Export a certificate from a keystore
keytool -export -alias aliasname -file filename.crt -keystore keystore.jks

An original article is here.



NOTE from me: keystore.jks in Java is exactly the same as *.keystore file in Visual Studio (Xamarin). So when you miss your *.keystore file, use a part of below instructions to regenerate your new keystore file with the same password. Then attach newly generated .pem file and ask Google support team to update your existence application in Play Market with than newly created one keystore file. And it works. Please remember, you can do it once per application. So always store a copy of your keystore files somewhere in safe place.

Sign android app with new keystore file if you missing password or lost jks file.

  1. Create new keystore.jks file with comand line (not android studio build menu)

    Linux: keytool -genkeypair -alias upload -keyalg RSA -keysize 2048 -validity 9125 -keystore keystore.jks

    Windows: "C:\Program Files\Android\Android Studio\jre\bin\keytool.exe" -genkeypair -alias upload -keyalg RSA -keysize 2048 -validity 9125 -keystore "C:\keystore_new.jks"

  2. Generate a .pem file from new keystore

    Linux: keytool -export -rfc -alias upload -file upload_certificate.pem -keystore keystore.jks

    Windows: "C:\Program Files\Android\Android Studio\jre\bin\keytool.exe" -export -rfc -alias upload -file "C:\upload_cert.pem" -keystore "C:\keystore_new.jks"

  3. Use this support form, set "keystore problem" and with attachment add .pem file: https://support.google.com/googleplay/android-developer/contact/otherbugs

  4. 12-48h you new keystore is enabled. Update your app on playstore with new apk signed with new keystore :D