<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: hobbyist-programmer-ar</title>
    <description>The latest articles on DEV Community by hobbyist-programmer-ar (@hobbyistprogrammerar).</description>
    <link>https://dev.to/hobbyistprogrammerar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2966340%2Ff5a5f512-0ae0-4e75-af2d-52f10d4c8d2f.jpeg</url>
      <title>DEV Community: hobbyist-programmer-ar</title>
      <link>https://dev.to/hobbyistprogrammerar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hobbyistprogrammerar"/>
    <language>en</language>
    <item>
      <title>PEM, DER, PFX, CER, CRT, and CSR for connecting to servers</title>
      <dc:creator>hobbyist-programmer-ar</dc:creator>
      <pubDate>Sat, 05 Apr 2025 12:46:23 +0000</pubDate>
      <link>https://dev.to/hobbyistprogrammerar/pem-der-pfx-cer-crt-and-csr-for-connecting-to-servers-54k1</link>
      <guid>https://dev.to/hobbyistprogrammerar/pem-der-pfx-cer-crt-and-csr-for-connecting-to-servers-54k1</guid>
      <description>&lt;p&gt;Hey All&lt;br&gt;
Following my recent obsession with the the certificates and private key used to communicate between server and a client I am writing this post about my understanding of what the file extension we use as a backend engineer trying to connect to various server like kafka.&lt;/p&gt;

&lt;h2&gt;
  
  
  CER and CRT
&lt;/h2&gt;

&lt;p&gt;The CER file is used to store information about the owner certificate and the specific public key. These files can hold only one certificate(x509) at max and it does not have the capacity to hold the private key. The specifically secured certificate authorities are those which belong to HTTPS, a trusted and secured protocol for browsing.The CER is a certificate of your server. It is usually received by the certificate authority for the domain. CER is mostly considered the same as CRT, although both are the same format of SSL certificate but are different filename extensions. The file can be generated by the above mentioned CA or we can generate it on our own which is also called an Self Signed Certificate. The self signed certificates are only intended for local development and testing. Incase we need to use it in production out in the real world we need to have it signed by a certificate authority.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference between CER and CRT
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;File Extensions&lt;/strong&gt;: The most noticeable difference is the file extensions themselves: CER and CRT. This might seem insignificant, but it can cause compatibility issues with certain software or systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Usage and Convention:&lt;/strong&gt; While both file types contain the same information, the CER extension is often used for security files installed in various operating systems. CRT is frequently associated with certificates that are specifically part of SSL/TLS implementations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operating System Compatibility:&lt;/strong&gt; Some operating systems have preferences for one format over the other. For instance, Windows systems might prefer CER files, while UNIX/Linux systems prefer CRT files.
### Converting CRT to CER and Vice Versa
#### CRT to CER
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl x509 &lt;span class="nt"&gt;-in&lt;/span&gt; inputFile.crt &lt;span class="nt"&gt;-out&lt;/span&gt; outputFile.cer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  CER to CRT
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl x509 &lt;span class="nt"&gt;-inform&lt;/span&gt; der &lt;span class="nt"&gt;-in&lt;/span&gt; certificate.cer &lt;span class="nt"&gt;-out&lt;/span&gt; certificate.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CSR
&lt;/h2&gt;

&lt;p&gt;A Certificate Signing Request(CSR) is the precursor of the certificate. It is usually generated on the same server you are planning to install your certificate on. The CSR contains information about the Common name, Organization, Country etc. which is used by the Certificate Authority to create your certificate. One a Self signed certificate we would be creating our own Certificate(CER or CRT) from the CSR. We generally used the Private Key to generate the CSR&lt;/p&gt;

&lt;h3&gt;
  
  
  Data stored in the CSR
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Information about your website you’re trying to equip with SSL

&lt;ol&gt;
&lt;li&gt;Common Name : The fully qualified domain name (FQDN) of your server.&lt;/li&gt;
&lt;li&gt;Organization : The legal name of your organization. Do not abbreviate and include any suffixes, such as Inc., Corp., or LLC.&lt;/li&gt;
&lt;li&gt;Organizational Unit : The division of your organization handling the certificate.&lt;/li&gt;
&lt;li&gt;City/Locality : The city where your organization is located. This shouldn’t be abbreviated.&lt;/li&gt;
&lt;li&gt;State/County/Region :The state/region where your organization is located. This shouldn't be abbreviated.&lt;/li&gt;
&lt;li&gt;Country : Two-letter country code where organization is located.&lt;/li&gt;
&lt;li&gt;Email Address : Email address used to contact your organization.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;The public key that will be included in the certificate.

&lt;ol&gt;
&lt;li&gt;Since the SSL use Assymetric keys to encrpyt the data transmitted suring the SSL session the public key is included in the CSR&lt;/li&gt;
&lt;li&gt;The Public key is used to encrypt the message and the private key is used to decrypt it.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Information about the key type and length

&lt;ol&gt;
&lt;li&gt;Used to carry information about the key wether its RSA 2048 or ECC.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  PFX
&lt;/h3&gt;

&lt;p&gt;PFX is a password protected file certificate commonly used for code signing you app. It derives from the PKCS 12 Archive File format certificate. The PFX files generally holds the below contents&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;x509 public key certificates&lt;/li&gt;
&lt;li&gt;x509 private keys&lt;/li&gt;
&lt;li&gt;Intermediate Certificate (Optional) Establish a chain of trust from the certificate to a trusted root certificate.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Convert CER to PFX
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl pkcs12 &lt;span class="nt"&gt;-export&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; certificate.pfx &lt;span class="nt"&gt;-inkey&lt;/span&gt; privateKey.key &lt;span class="nt"&gt;-in&lt;/span&gt; certificate.cer &lt;span class="nt"&gt;-certfile&lt;/span&gt; CACert.cer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Convert PEM to PFX
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl pkcs12 &lt;span class="nt"&gt;-export&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; Cert.p12 &lt;span class="nt"&gt;-in&lt;/span&gt; cert.pem &lt;span class="nt"&gt;-inkey&lt;/span&gt; key.pem &lt;span class="nt"&gt;-passin&lt;/span&gt; pass:root &lt;span class="nt"&gt;-passout&lt;/span&gt; pass:root

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll be covering what an PKCS is in a separate post.&lt;/p&gt;

&lt;h2&gt;
  
  
  PEM
&lt;/h2&gt;

&lt;p&gt;PEM - Privacy Enhanced Email. It is the most common forat for x509 certificates. It contains one or more formats in a Base64 ASCII encoding each with a plain text header and footer (&lt;code&gt;-----BEGIN CERTIFICATE-----&lt;/code&gt; and &lt;code&gt;-----END CERTIFICATE-----&lt;/code&gt;). A single PEM file could contain an end-entity certificate, a private key, or multiple certificates forming a complete chain of trust. Most certificate files downloaded from SSL.com will be in PEM format.&lt;/p&gt;

&lt;h3&gt;
  
  
  Convert DER to PEM
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl x509 &lt;span class="nt"&gt;-inform&lt;/span&gt; der &lt;span class="nt"&gt;-in&lt;/span&gt; certificate.cer &lt;span class="nt"&gt;-out&lt;/span&gt; certificate.pem

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  DER
&lt;/h2&gt;

&lt;p&gt;DER (Distinguished Encoding Rules) is a binary encoding for x509 certificates and private keys. Unlike PEM, DER-encoded files do not contain plain text statements such as &lt;code&gt;-----BEGIN CERTIFICATE-----&lt;/code&gt;. DER files are most commonly seen in Java contexts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Convert PEM to DER and Certificates
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Convert PEM to DER
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl rsa &lt;span class="nt"&gt;-in&lt;/span&gt; &amp;lt;file_with_key&amp;gt; &lt;span class="nt"&gt;-out&lt;/span&gt; &amp;lt;new_der_key_file&amp;gt; &lt;span class="nt"&gt;-outform&lt;/span&gt; DER
openssl rsa &lt;span class="nt"&gt;-in&lt;/span&gt; &amp;lt;file_with_key&amp;gt; &lt;span class="nt"&gt;-inform&lt;/span&gt; PEM &lt;span class="nt"&gt;-out&lt;/span&gt; &amp;lt;new_der_key_file&amp;gt; &lt;span class="nt"&gt;-outform&lt;/span&gt; DER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Convert PEM to Certificate
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl x509 &lt;span class="nt"&gt;-in&lt;/span&gt; &amp;lt;cert_file&amp;gt; &lt;span class="nt"&gt;-out&lt;/span&gt; &amp;lt;new_der_cert&amp;gt; &lt;span class="nt"&gt;-outform&lt;/span&gt; DER
openssl x509 &lt;span class="nt"&gt;-in&lt;/span&gt; &amp;lt;cert_file&amp;gt; &lt;span class="nt"&gt;-inform&lt;/span&gt; PEM &lt;span class="nt"&gt;-out&lt;/span&gt; &amp;lt;new_der_cert&amp;gt; &lt;span class="nt"&gt;-outform&lt;/span&gt; DER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PEM is a base64 encoded file with where as the DER is the binary form. Since we do not use the headers in the DER file we can distinguish between different objects and hence DER can only store one object where as a PEM has simple headers and footers which allows us to distinguish and store multiple objects&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>security</category>
      <category>ssl</category>
    </item>
    <item>
      <title>Generating a Self Signed ECC Certificate and Private Key and Validating the same</title>
      <dc:creator>hobbyist-programmer-ar</dc:creator>
      <pubDate>Thu, 03 Apr 2025 17:09:13 +0000</pubDate>
      <link>https://dev.to/hobbyistprogrammerar/generating-a-self-signed-ecc-certificate-and-private-key-and-validating-the-same-3mbf</link>
      <guid>https://dev.to/hobbyistprogrammerar/generating-a-self-signed-ecc-certificate-and-private-key-and-validating-the-same-3mbf</guid>
      <description>&lt;h2&gt;
  
  
  Creating my Own Self Signed ECC Certificate
&lt;/h2&gt;

&lt;p&gt;Hey All&lt;br&gt;
This is a continuation for the last post where I mentioned the &lt;a href="https://dev.to/hobbyistprogrammerar/creating-a-self-signed-certificate-and-verifying-the-private-key-and-certificate-57m"&gt;steps to generate and validate a RSA Certificate and Private Key&lt;/a&gt;. Here I wanted to add the steps to create and validate an Certificate and Private key that uses ECC.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a ECC Cert and Private Key.&lt;/li&gt;
&lt;li&gt;Validate an ECC Cert and Private Key Pair&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create a ECC Cert and Private Key
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Generating a Private Key&lt;/strong&gt;
This command generates an ECC private key using the prime256v1 curve and saves it to a file.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl ecparam -name prime256v1 -genkey -noout -out ecc_private_key.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;openssl&lt;/em&gt; → The command-line tool for cryptographic operations.&lt;br&gt;
&lt;em&gt;ecparam&lt;/em&gt; → This tells OpenSSL that we are working with Elliptic Curve parameters.&lt;br&gt;
&lt;em&gt;-name prime256v1&lt;/em&gt; → Specifies the elliptic curve to use.&lt;br&gt;
&lt;em&gt;prime256v1&lt;/em&gt; is the same as &lt;em&gt;secp256r1&lt;/em&gt;, which is a widely used and secure curve.&lt;/p&gt;

&lt;p&gt;You can list available curves using:&lt;br&gt;
&lt;code&gt;openssl ecparam -list_curves&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;-genkey&lt;/em&gt; → Generates a new private key based on the selected curve.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-noout&lt;/em&gt; → Prevents OpenSSL from printing the EC parameters to the output (keeps output clean).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-out ecc_private_key.pem&lt;/em&gt; → Saves the generated private key to a file named ecc_private_key.pem.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Generating a CSR&lt;/strong&gt;
OpenSSL will ask for identity details (like domain name, organization, location).
The output file &lt;em&gt;ecc_csr.pem&lt;/em&gt; contains:&lt;/li&gt;
&lt;li&gt;Your public key&lt;/li&gt;
&lt;li&gt;Your identity information&lt;/li&gt;
&lt;li&gt;A digital signature using your private key
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -new -key ecc_private_key.pem -out ecc_csr.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;req&lt;/em&gt; → This tells OpenSSL we are working with a certificate request.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-new&lt;/em&gt; → Creates a new CSR (Certificate Signing Request).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-key ecc_private_key.pem&lt;/em&gt; → Uses the previously generated ECC private key.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-out ecc_csr.pem&lt;/em&gt; → Saves the CSR to a file named ecc_csr.pem.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Generate a Self Signed Cert
A self-signed certificate is useful for testing but isn't trusted by browsers or CAs. If you're using it for a personal system or internal network, it's fine. Otherwise, you'll need a CA-signed certificate.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -x509 -key ecc_private_key.pem -days 365 -out ecc_certificate.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;req&lt;/em&gt; → Again, we're working with a certificate request.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-x509&lt;/em&gt; → This tells OpenSSL to create a self-signed certificate instead of a CSR.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-key ecc_private_key.pem&lt;/em&gt; → Uses your ECC private key to sign the certificate.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-days 365&lt;/em&gt; → Specifies the validity period (365 days = 1 year).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-out ecc_certificate.pem&lt;/em&gt; → Saves the self-signed certificate as &lt;em&gt;ecc_certificate.pem&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Verify the Private Key
This command lets you &lt;strong&gt;inspect&lt;/strong&gt; the ECC private key, showing:&lt;/li&gt;
&lt;li&gt;The chosen &lt;strong&gt;elliptic curve&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;private key value&lt;/strong&gt; (big number).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;public key (derived from the private key)&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl ec -in ecc_private_key.pem -noout -text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;ec&lt;/em&gt; → This tells OpenSSL that we are working with an &lt;strong&gt;Elliptic Curve (EC) private key&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-in ecc_private_key.pem&lt;/em&gt; → Specifies the input file (your private key).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-noout&lt;/em&gt; → Prevents OpenSSL from printing the key in PEM format.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-text&lt;/em&gt; → Prints the private key details in human-readable form.&lt;/li&gt;
&lt;li&gt;Verify the Certificate&lt;/li&gt;
&lt;li&gt;Shows certificate details: &lt;strong&gt;issuer, subject, validity, public key, signature&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Verifies that the certificate was correctly generated.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl x509 -in ecc_certificate.pem -noout -text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;x509&lt;/em&gt; → Specifies that we are working with an &lt;strong&gt;X.509 certificate&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-in ecc_certificate.pem&lt;/em&gt; → Reads the &lt;strong&gt;self-signed certificate&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-noout&lt;/em&gt; → Prevents OpenSSL from outputting the raw certificate in PEM format.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-text&lt;/em&gt;→ Prints detailed certificate information in human-readable format.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Verify the ECC Cert and Private Key Pair
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Command to extract the Key value pair
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl pkey -in ecc_private_key.pem -pubout -outform pem | sha256sum
openssl x509 -in ecc_certificate.pem -pubkey -noout -outform pem | sha256sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;openssl pkey&lt;/em&gt; → Works with private keys.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-in ecc_private_key.pem&lt;/em&gt; → Reads the &lt;strong&gt;private key file&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-pubout&lt;/em&gt; → Extracts the &lt;strong&gt;public key&lt;/strong&gt; from the private key.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-outform pem&lt;/em&gt; → Outputs the public key in &lt;strong&gt;PEM format&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;openssl x509&lt;/em&gt; → Works with X.509 certificates.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-in ecc_certificate.pem&lt;/em&gt; → Reads the &lt;strong&gt;certificate file&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-pubkey&lt;/em&gt; → Extracts the &lt;strong&gt;public key&lt;/strong&gt; from the certificate.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-noout&lt;/em&gt; → Prevents the certificate details from being printed.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;-outform pem&lt;/em&gt; → Outputs the public key in &lt;strong&gt;PEM format&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;sha256sum&lt;/em&gt; → Computes a &lt;strong&gt;SHA-256 hash&lt;/strong&gt; of the public keys.&lt;/li&gt;
&lt;li&gt;If the &lt;strong&gt;hash values match&lt;/strong&gt;, it means the private key and certificate belong to the same key pair.&lt;/li&gt;
&lt;li&gt;If the &lt;strong&gt;hash values don’t match&lt;/strong&gt;, then the certificate does not correspond to the private key.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>ssl</category>
      <category>cryptography</category>
    </item>
    <item>
      <title>Creating a Self Signed Certificate and Verifying the Private Key and Certificate</title>
      <dc:creator>hobbyist-programmer-ar</dc:creator>
      <pubDate>Sat, 22 Mar 2025 15:49:26 +0000</pubDate>
      <link>https://dev.to/hobbyistprogrammerar/creating-a-self-signed-certificate-and-verifying-the-private-key-and-certificate-57m</link>
      <guid>https://dev.to/hobbyistprogrammerar/creating-a-self-signed-certificate-and-verifying-the-private-key-and-certificate-57m</guid>
      <description>&lt;p&gt;Hey all &lt;/p&gt;

&lt;p&gt;I wanted to share one of the issue I faced at work this week and the steps I did to debug this issues. The issue mostly revolved around the keystore that we use in our application and it was a simple issue that took us around two days to figure out. It was a simple issue where we used the wrong private key and certificate combination and was overlooked with led to two days of unnecessary work. I am sharing some of the steps I did to figure it out which could be useful for you the next time. I am writing this post as two parts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: You create your own ssl certificates&lt;/li&gt;
&lt;li&gt;Part 2: You debug the certificate to see if the PK and Cert matches&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Part 1 : Creating my own Self Signed Certificate
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating a Private Key
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;To create a new Private key in the PKCS#1 format execute the below command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# openssl genrsa -des3 -out key_name.key key_strength&lt;/span&gt;
openssl genrsa &lt;span class="nt"&gt;-des3&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; private_key.key 2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a Certificate Signing Request
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;To Create a new certificate signing request with the above private key execute the following command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# openssl genrsa -des3 -out key_name.key key_strength&lt;/span&gt;
openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-key&lt;/span&gt; private_key.key &lt;span class="nt"&gt;-out&lt;/span&gt; certificate_signing_request.csr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; Your certificate signing request is not your actual cert. It only contains the information which is needed to generate a certificate based on your private key&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Certificate
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Execute the below command to get a certificate out of the csr we created above
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl x509 &lt;span class="nt"&gt;-req&lt;/span&gt; &lt;span class="nt"&gt;-in&lt;/span&gt; certificate_signing_request.csr &lt;span class="nt"&gt;-signkey&lt;/span&gt; private_key.key &lt;span class="nt"&gt;-out&lt;/span&gt; certificate.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a Keystore out of the JKS
&lt;/h3&gt;

&lt;p&gt;In order to add the certificates to a key store the following command is required&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert your certificate and private key in to a PKCS12(.p12 of .pfx) file using the below command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl pkcs12 &lt;span class="nt"&gt;-export&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; keystore_pkcs.p12 &lt;span class="nt"&gt;-inkey&lt;/span&gt; private_key.key &lt;span class="nt"&gt;-in&lt;/span&gt; certificate.crt &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;"faustus"&lt;/span&gt; &lt;span class="nt"&gt;-password&lt;/span&gt; pass:bulgogi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Once the pkcs is created we can create the jks out of it using the below command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;keytool &lt;span class="nt"&gt;-importkeystore&lt;/span&gt; &lt;span class="nt"&gt;-srckeystore&lt;/span&gt; keystore_to_test.p12 &lt;span class="nt"&gt;-srcstoretype&lt;/span&gt; PKCS12 &lt;span class="nt"&gt;-srcalias&lt;/span&gt; faustus &lt;span class="nt"&gt;-srcstorepass&lt;/span&gt; bulgogi2902 &lt;span class="nt"&gt;-destkeystore&lt;/span&gt; dest_ks.jks &lt;span class="nt"&gt;-deststoretype&lt;/span&gt; JKS &lt;span class="nt"&gt;-deststorepass&lt;/span&gt; bulgogi2902 &lt;span class="nt"&gt;-destalias&lt;/span&gt; fausto
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The keystore can be check with the following command to list the contents
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;keytool &lt;span class="nt"&gt;-list&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="nt"&gt;-keystore&lt;/span&gt; dest_ks.jks &lt;span class="nt"&gt;-storepass&lt;/span&gt; bulgogi2902
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Part 2 : Validating the SSL Certificates
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Checking the private Key
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl rsa -in [key-file.key] -check -noout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command lets us know if the private key has been tampered with&lt;br&gt;
Some of the faulty responses are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;RSA key error : p not prime&lt;/li&gt;
&lt;li&gt;RSA key error : n does not equal p q&lt;/li&gt;
&lt;li&gt;RSA key error : d e not congruent to 1&lt;/li&gt;
&lt;li&gt;RSA key error : dmp1 not congruent to d&lt;/li&gt;
&lt;li&gt;RSA key error : iqmp is not inverse of q&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Incase the private key is not tampered with we can get the following response&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;RSA key ok&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Confirm the Modulus Value Matching with Private Key and SSL/TLS certificate Key Pair
&lt;/h3&gt;

&lt;p&gt;The modulus of the private key and the certificate must match exactly&lt;/p&gt;
&lt;h4&gt;
  
  
  To view the certificate modulus
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl x509 -noout -modulus -in [certificate-file.cer]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  To view the private key modulus
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl rsa -noout -modulus -in [key-file.key]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Perform Encryption with Public Key from certificate and Decryption with Private Key
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Get public key from the certificate
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl x509 -in [certificate-file.cer] -noout -pubkey &amp;gt; certificatefile.pub.cer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Encrypt test.txt file content using the public key. Create a new file called test.txt file with the content "&lt;strong&gt;message test&lt;/strong&gt;". Perform the following command to &lt;strong&gt;create an encrypted message&lt;/strong&gt; to cipher.txt file.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl pkeyutl -encrypt -in test.txt -pubin -inkey certificatefile.pub.cer -out cipher.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Decrypt from cipher.txt using the private key
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl  pkeyutl  -decrypt -in cipher.txt -inkey [key-file.key]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Confirming the integrity of file which is signed with private key. Perform following command to sign test.sig and test.txt file with your private key
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl dgst -sha256 -sign  [key-file.key] -out test.sig test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Verify the signed files with your public key that was extracted from step &lt;strong&gt;1. Get public key from certificate.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl dgst -sha256 -verify certificatefile.pub.cer -signature test.sig test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output from the terminal should show the below message&lt;br&gt;
&lt;strong&gt;verified ok&lt;/strong&gt;&lt;br&gt;
Incase the private key has been tampered with the following message will appear&lt;br&gt;
&lt;strong&gt;Verification Failure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like most post this is just information I compiled from different source to one post so that it could be a one stop shop. Hopefully this help you solve the issues or at least put you on the right path.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>security</category>
      <category>ssl</category>
      <category>openssl</category>
    </item>
  </channel>
</rss>
