<?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>Forem: amirhossein chamideh</title>
    <description>The latest articles on Forem by amirhossein chamideh (@amirhossein_ch__21).</description>
    <link>https://forem.com/amirhossein_ch__21</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%2F3524507%2Fa61fe0d6-dd1f-44ef-8c66-1b17ec994a55.png</url>
      <title>Forem: amirhossein chamideh</title>
      <link>https://forem.com/amirhossein_ch__21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amirhossein_ch__21"/>
    <language>en</language>
    <item>
      <title>Generate Self-Signed SSL Cert</title>
      <dc:creator>amirhossein chamideh</dc:creator>
      <pubDate>Sat, 27 Sep 2025 13:03:58 +0000</pubDate>
      <link>https://forem.com/amirhossein_ch__21/generate-self-signed-ssl-cert-1dl1</link>
      <guid>https://forem.com/amirhossein_ch__21/generate-self-signed-ssl-cert-1dl1</guid>
      <description>&lt;p&gt;Quick guid on how to generate a self-signed ssl cert&lt;/p&gt;

&lt;p&gt;Hope it helps&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenSSL Tool:
&lt;/h2&gt;

&lt;p&gt;Most of the time you'll have &lt;code&gt;openssl&lt;/code&gt; on your Linux system.&lt;/p&gt;

&lt;p&gt;If it was missing you can install it.&lt;/p&gt;

&lt;p&gt;On Debian/Ubuntu:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install openssl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On RHEL8/9:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dnf install openssl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To verify the installations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  1. Generate CA Certificate And Trust It On The System
&lt;/h3&gt;

&lt;p&gt;In order to generate a CA first we need to generate CA's key.&lt;/p&gt;

&lt;p&gt;To do it we can execute the following command on the terminal:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genrsa -out CA.key 2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;genrsa&lt;/code&gt;: Generates a key using RSA encryption algorythem.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-out&lt;/code&gt;: Specifies the key name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2048&lt;/code&gt;: The key size in bits. More the bits are, More secure your key will be.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have the CA key we need to generate CA certificate.&lt;/p&gt;

&lt;p&gt;In order to do that:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;req&lt;/code&gt;: Starts a certificate request generation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x509&lt;/code&gt;: By using this switch openssl skips the CSR step generates a self-signed root CA.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-new&lt;/code&gt;: The combination with &lt;code&gt;req -x509&lt;/code&gt; switches tell openssl to create a new certificate.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-key&lt;/code&gt;: Specifies the private key we generated in the previuse step.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-out&lt;/code&gt;: Spencifies the certificate Name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-days&lt;/code&gt;: Validation duration.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: We can create certificate and its key in one step using &lt;code&gt;-keyout Key_Name.key&lt;/code&gt; option instead of &lt;code&gt;-key&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After executing the command you'll be prompted for some information such as &lt;code&gt;Country Name&lt;/code&gt;, &lt;code&gt;State or Province Name&lt;/code&gt;, &lt;code&gt;Locality Name&lt;/code&gt;, &lt;code&gt;Organization Name&lt;/code&gt;, &lt;code&gt;Organization Unit Name&lt;/code&gt;, &lt;code&gt;Common Name&lt;/code&gt; and &lt;code&gt;Email Address&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we have CA and its key.&lt;/p&gt;

&lt;p&gt;It's time to trust them on our local system.&lt;/p&gt;

&lt;p&gt;On Debian/Ubuntu:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cp CA.pem /usr/local/share/ca-certificates/CA.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On RHEL:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cp CA.pem /etc/pki/ca-trust/source/anchors/CA.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The best practice is to put the private key in the following locations too.&lt;/p&gt;

&lt;p&gt;Debian/Ubuntu: &lt;code&gt;/etc/ssl/private/&lt;/code&gt;&lt;br&gt;
RHEL: &lt;code&gt;/etc/pki/CA/private/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After locating them properly, By executing the following command the list of trusted CA's will be updated:&lt;/p&gt;

&lt;p&gt;Debian/Ubuntu: &lt;code&gt;sudo update-ca-certificates&lt;/code&gt;&lt;br&gt;
RHEL: &lt;code&gt;sudo update-ca-trust&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Generate CSR
&lt;/h3&gt;

&lt;p&gt;CSR contains a public key and metadata (such as domain name, OU and ...) and is used to request a certificate from a CA.&lt;/p&gt;

&lt;p&gt;First we need to generate a private key for our certificate just like we did for the CA:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genrsa -out server.key 2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now to get the CSR we need to execute the following command:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;req -new&lt;/code&gt;: This combination is used to create CSR. If &lt;code&gt;-x509&lt;/code&gt; is used it'll turn into generating self-signed CA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Create Certificate Extension File
&lt;/h3&gt;

&lt;p&gt;This step is optional but it's suggested.&lt;/p&gt;

&lt;p&gt;Extension files have &lt;code&gt;.ext&lt;/code&gt; postfix and they look like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = www.example.com
IP.1 = 192.168.1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Component Breakdown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;authorityKeyIdentifier&lt;/code&gt;: Links the certificate to the issuing CA by key ID and issuer name. Also enables the certificate chain validation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;basicConstraints&lt;/code&gt;: Specifies whether the cert can be a CA and sign other certificates or no. By setting it to &lt;code&gt;CA:False&lt;/code&gt; It would not be a CA and if set to &lt;code&gt;CA:True&lt;/code&gt; It will be. Also if set to true you can specify &lt;code&gt;pathlen:NUM&lt;/code&gt; to set a limit on chain depth.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;keyUsage&lt;/code&gt;: This defines which cryptographic operations the certificates public key is allowed to perform. Operations are listed below.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;extendedKeyUsage&lt;/code&gt;: This gives us more operations than keyUsage. They're listed below.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;subjectAltName&lt;/code&gt;: Defines additional identities like DNS names and IP addresses and emails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;keyUsage operations list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;digitalSignature: Allows use for signing like TLS handshake.
-
nonRepudiation: Ensures the signer cannot deny having signed.
-
keyEncipherment: Allows encrypting symmetric keys used in TLS key exchange.
-
dataEncipherment: Allows direct encrypting on data.
-
keyAgreement: Allows key exchange protocols like Diffie-Hellman. 
-
keyCertSign: Required for CAs to sign other certs.
-
cRLSign: Allows signing certificate revocation list.
-
encipherOnly: Used only with keyAgreement which limits to encryption.
-
decipherOnly: Used only with keyAgreement which limits to deencryption.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;extendedKeyUsage operations list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;serverAuth: Used to authenticate a TLS/SSL server like HTTPS websites.
-
clientAuth: Used to authenticate TLS clients.
-
codeSigning: For digitally signing software/code.
-
emailProtection: For signing or encrypting email.
-
timeStamping: For trusted timestamps like legal documents.
-
OCSPSigning: For signing OCSP responses.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  4. Generating The Certificate
&lt;/h3&gt;

&lt;p&gt;Now that we have every thing lets generate our certificate.&lt;/p&gt;

&lt;p&gt;Execute the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl x509 -req -in server.csr -CA /path/to/CA.pem -CAkey /path/to/CA.key -CAcreateserial -out server.pem -days 365 -sha256 -extfile server.ext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Command Breakdown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x509&lt;/code&gt;: Tells openssl to create a x.509 certificate.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-req&lt;/code&gt;: Indicates the input is a CSR file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-in&lt;/code&gt;: Specifies the input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-CA&lt;/code&gt;: Specifies the self-signed CA used to sign CSR.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-CAkey&lt;/code&gt;: Specifies the private key of the CA to digitally sign the certificate.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CAcreateserial&lt;/code&gt;: Creates serialnumber file like myCA.srl for tracking issued certs.
&lt;code&gt;-sha256&lt;/code&gt;: Uses SHA256 hashing algorithm for signing the certificate.
&lt;code&gt;-exrfile&lt;/code&gt;: Specifies the extention file.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tutorial</category>
      <category>cli</category>
      <category>security</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
