Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeansymolanza/dbae6a9bb9751d078601e50e731a36e3 to your computer and use it in GitHub Desktop.
Save jeansymolanza/dbae6a9bb9751d078601e50e731a36e3 to your computer and use it in GitHub Desktop.
Converting a Key Database (KDB) to a Java KeyStore (JKS) involves several steps. Here's a simple and effective way to perform the conversion using OpenSSL and keytool. This method assumes that you have the necessary tools installed on your system:
1. **Export the Certificate and Key from KDB:**
Use `gsk8capicmd_64` to export the certificate and private key from the KDB file to PEM format.
2. **Convert the PEM files to a PKCS12 file:**
Use OpenSSL to combine the certificate and private key into a PKCS12 file.
3. **Import the PKCS12 file into a JKS:**
Use the `keytool` command to import the PKCS12 file into a Java KeyStore (JKS).
### Step-by-Step Guide
#### Step 1: Export the Certificate and Key from KDB
First, use `gsk8capicmd_64` to export the certificate and private key from the KDB file. If the private key is not exportable, you may only be able to export the certificate.
```sh
# Export the certificate to PEM format
gsk8capicmd_64 -cert -extract -db "your_database.kdb" -pw "your_password" -label "your_cert_label" -target "cert.pem" -format ascii
# Export the private key to PEM format (if possible)
gsk8capicmd_64 -key -extract -db "your_database.kdb" -pw "your_password" -label "your_cert_label" -target "key.pem" -format ascii
```
#### Step 2: Convert the PEM Files to a PKCS12 File
Use OpenSSL to combine the certificate and private key into a PKCS12 file. If you cannot export the private key, this step will not be possible, and you will need to create a new key pair.
```sh
openssl pkcs12 -export -in cert.pem -inkey key.pem -out keystore.p12 -name "your_alias"
```
#### Step 3: Import the PKCS12 File into a JKS
Use the `keytool` command to import the PKCS12 file into a Java KeyStore (JKS).
```sh
keytool -importkeystore -deststorepass "your_keystore_password" -destkeypass "your_key_password" -destkeystore "keystore.jks" -srckeystore "keystore.p12" -srcstoretype PKCS12 -srcstorepass "your_pkcs12_password" -alias "your_alias"
```
### Example Workflow
1. **Export the certificate and key from KDB:**
```sh
gsk8capicmd_64 -cert -extract -db "/path/to/your_database.kdb" -pw "your_password" -label "your_cert_label" -target "cert.pem" -format ascii
gsk8capicmd_64 -key -extract -db "/path/to/your_database.kdb" -pw "your_password" -label "your_cert_label" -target "key.pem" -format ascii
```
2. **Convert the PEM files to a PKCS12 file:**
```sh
openssl pkcs12 -export -in cert.pem -inkey key.pem -out keystore.p12 -name "your_alias"
```
3. **Import the PKCS12 file into a JKS:**
```sh
keytool -importkeystore -deststorepass "your_keystore_password" -destkeypass "your_key_password" -destkeystore "keystore.jks" -srckeystore "keystore.p12" -srcstoretype PKCS12 -srcstorepass "your_pkcs12_password" -alias "your_alias"
```
### Notes
- **Ensure you have the necessary permissions** to read the KDB file and write the output files.
- **Backup your KDB file** and any other important files before starting the process.
- **If the private key cannot be exported** from the KDB file, you may need to generate a new key pair and certificate.
By following these steps, you should be able to convert a KDB file to a JKS file effectively. If you encounter any issues, please provide more details, and I can assist further.
@jeansymolanza
Copy link
Author

keytool -import -alias "trusted_cert1_alias" -file "trusted_cert1.pem" -keystore "keystore.jks" -storepass "your_keystore_password"

keytool -import -alias "trusted_cert2_alias" -file "trusted_cert2.pem" -keystore "keystore.jks" -storepass "your_keystore_password"

@jeansymolanza
Copy link
Author

To generate a Certificate Signing Request (CSR) using runmqckm, follow these steps. This process involves creating a new Key Database (KDB) if you don't already have one, creating a key pair, and then generating the CSR.

Step-by-Step Guide

Step 1: Create a New KDB File (if you don't already have one)

First, create a new KDB file along with a stash file to store the password.

runmqckm -keydb -create -db /path/to/your_key_database.kdb -pw your_password -type cms -stash
  • /path/to/your_key_database.kdb is the path to the KDB file you want to create.
  • your_password is the password for the KDB file.

Step 2: Create a New Key Pair

Create a new key pair for the CSR.

runmqckm -cert -create -db /path/to/your_key_database.kdb -pw your_password -label "your_cert_label" -dn "CN=your_common_name,O=your_organization,C=your_country" -size 2048 -x509version 3 -expire 365
  • your_cert_label is a label for the certificate.
  • CN=your_common_name,O=your_organization,C=your_country is the Distinguished Name (DN) for the certificate. Adjust it according to your needs.
  • 2048 is the size of the key.
  • 3 is the X.509 version.
  • 365 is the number of days the certificate is valid for.

Step 3: Generate the CSR

Generate the CSR using the key pair created in the previous step.

runmqckm -certreq -create -db /path/to/your_key_database.kdb -pw your_password -label "your_cert_label" -file /path/to/your_csr_file.csr
  • your_cert_label is the label for the key pair you created.
  • /path/to/your_csr_file.csr is the path to the CSR file you want to generate.

Example Workflow

Here’s a full example assuming you are creating a KDB file named mykeystore.kdb and generating a CSR.

  1. Create the KDB File:
runmqckm -keydb -create -db /home/user/mykeystore.kdb -pw mypassword -type cms -stash
  1. Create a New Key Pair:
runmqckm -cert -create -db /home/user/mykeystore.kdb -pw mypassword -label "mykeypair" -dn "CN=mycommonname,O=myorganization,C=US" -size 2048 -x509version 3 -expire 365
  1. Generate the CSR:
runmqckm -certreq -create -db /home/user/mykeystore.kdb -pw mypassword -label "mykeypair" -file /home/user/mykeypair.csr

After running these commands, you will have a CSR file at /home/user/mykeypair.csr that you can submit to a Certificate Authority (CA) to obtain a signed certificate.

Summary

  1. Create a new KDB file: Use runmqckm -keydb -create.
  2. Create a new key pair: Use runmqckm -cert -create.
  3. Generate a CSR: Use runmqckm -certreq -create.

By following these steps, you can generate a CSR using runmqckm. If you encounter any issues or need further assistance, please provide more details, and I'll be happy to help.

@jeansymolanza
Copy link
Author

To import a personal certificate in .cer format using runmqckm, you can follow these steps:

  1. Ensure you have the certificate in .cer format.
  2. Use the following command to import the certificate into a key database:
runmqckm -cert -add -db "key.kdb" -pw "keydbpassword" -label "cert_label" -file "path/to/certificate.cer"

Replace key.kdb with the name of your key database, keydbpassword with the password for your key database, cert_label with a label for the certificate, and path/to/certificate.cer with the actual path to your certificate file.

Example:

runmqckm -cert -add -db "mykey.kdb" -pw "mypassword" -label "mycert" -file "/path/to/mycert.cer"

This command adds the certificate to the specified key database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment