<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arpana Gupta on Medium]]></title>
        <description><![CDATA[Stories by Arpana Gupta on Medium]]></description>
        <link>https://medium.com/@arpanagupta10?source=rss-a352ad164009------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qyQ963mUQRwwt7cBCaul8A.jpeg</url>
            <title>Stories by Arpana Gupta on Medium</title>
            <link>https://medium.com/@arpanagupta10?source=rss-a352ad164009------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 12:27:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arpanagupta10/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Certificate Revocation with OCSP using OpenSSL]]></title>
            <link>https://medium.com/@arpanagupta10/certificate-revocation-with-ocsp-using-openssl-a9c30f37a10a?source=rss-a352ad164009------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9c30f37a10a</guid>
            <dc:creator><![CDATA[Arpana Gupta]]></dc:creator>
            <pubDate>Wed, 13 Sep 2023 10:30:17 GMT</pubDate>
            <atom:updated>2023-09-13T10:30:17.192Z</atom:updated>
            <content:encoded><![CDATA[<p>This piece of document is in continuation to the <a href="https://medium.com/@arpanagupta10/exploring-cryptographic-content-using-openssl-42e1916b7d09">Generating cryptographic content using OpenSSL made easy</a>.</p><p>Public certificates are important for establishing a secure communication between two parties. Hence verifying that certificates the third party is using is equally important. There are two ways which validity can be checked:</p><p>1) Downloading Certificate Revocation List published by the certificate authority (CA)</p><p>2) By Querying the Online Certificate Status Protocol (OCSP) Server/Responder for status of single certificate. The OCSP server is set up by Certificate Authority itself. Before receiving data from webservers with https connection, web browsers normally perform a OCSP check with the CA that issued the certificate to that webserver.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*DVGuUcJeK5Kbwv2PUf8xmg.png" /></figure><p>Let’s understand about OCSP in detail, then we have a demonstration that shows using OCSP on CA and client side via OpenSSL. The <a href="http://en.wikipedia.org/wiki/OpenSSL">OpenSSL</a> suite provides tools and utilities for deploying a simple OCSP responder.</p><h3><strong>Online Certificate Status Protocol</strong></h3><p>It is a alternative way to CRL and provides real-time revocation status of an X509 certificate. This protocol is described in <a href="https://datatracker.ietf.org/doc/html/rfc6960">RFC6960</a>.</p><p>The client does not need to download the whole CRL document containing all the revoked certificates and check for status of its concerned certificate. The client simply sends its query with the certificate to be checked as a request and gets back the status of the certificate.</p><p>The CA (server) has an OCSP responder set up that does the below listed tasks:</p><p>1) listens for client’s request on a port.</p><p>2) verifies the request.</p><p>3) Checks for status of certificate in request by querying its database that contains latest information about its certificates.</p><p>4) Digitally Signs and Prepares a OCSP response with the concerned certificate’s status.</p><p>5) Sends this OCSP response back to the client.</p><h4><strong>Setting up OCSP responder on CA side</strong></h4><p>1) A key pair that is generated by CA and it would be entrusted to sign the OCSP response document.</p><p>2) Corresponding public certificate with extendedKeyUsage as ‘OCSPSigning’ is generated by CA. Note: The key pair and certificate generation process is same as used for other certificates.</p><p>3) The OCSP responder is started using the above generated private key and certificate along with port on which it will listen.</p><p>4) After the above steps are completed the OCSP Responder URL is set in the future certificates as part of X509V3 extension. This process is same as setting CRL Distribution Point URL value in the certificates with ocsp as the key name.</p><h4><strong>Using OCSP for certificate verification</strong></h4><p>1) Client obtains certificate from webserver/entity.</p><p>2) It reads the CA certificate.</p><p>3) Reads the OCSP URL from the user’s certificate.</p><p>4) Sends the user’s certificate as request to the OCSP URL which is served by CA.</p><p>5) OCSP Responder internally queries the CA and returns the OCSP response document containing the status information.</p><p>6) It verifies the response document and gets the revocation status of the certificate.</p><h3><strong>OpenSSL commands to see OCSP working</strong></h3><h4><strong>OCSP from server side</strong></h4><p><em>1) Generating an OCSP specific key-pair</em></p><p>The key pair and CSR generation process is same as any key pair with any algorithm</p><pre>openssl req -new -out ocsp.csr -keyout ocsp.key -pkeyopt ec_paramgen_curve:P-256 -newkey EC:256 -noenc</pre><p><em>2) Generating OCSP certificate</em></p><p>This same as any certificate generation, only one extra x509V3 <strong>extendedKeyUsage=OCSPSigning</strong> extension is needed in the OCSP certificate. This is not a CA. Following is a sample extension that can be set in the CA configuration file(openssl.cnf) and can be called from command line when generating the certificate. Alternatively, we can also prepare a file with the following extension(without section name) and pass this file while generating certificate,</p><pre>[ ocsp_cert ]<br># Extension for OCSP signing certificates (`man ocsp`).<br>basicConstraints = CA:FALSE<br>subjectKeyIdentifier = hash<br>authorityKeyIdentifier = keyid,issuer<br>keyUsage = critical, digitalSignature<br>extendedKeyUsage = critical, OCSPSigning</pre><pre>#Command for generating certificate.<br>openssl ca -in ocsp.csr -out ocsp.crt -extensions ocsp_cert -config openssl.cnf</pre><p><em>3) Starting OCSP responder</em></p><p>The openssl’s ocsp command can be used as server or as a client. It can perform multiple ocsp related tasks:</p><p>· Act as a mini ocsp server/responder</p><p>· Send queries to an ocsp responder</p><p>· Print ocsp request</p><p>· Print ocsp response</p><p>For starting OCSP responder:</p><pre>openssl ocsp -index &lt;&lt;ca index file used by openssl&gt;&gt; -CA &lt;&lt;CA file&gt;&gt; -rsigner&lt;&lt;OCSP certificate&gt;&gt; -rkey &lt;&lt;OCSP private key&gt;&gt; -port&lt;&lt;port in which openssl will listen for incoming req&gt;&gt; -text -out log.txt</pre><p>Note that usage of -index attribute indicates that the ocsp will start an ocsp responder.</p><h4><strong>OCSP from client side</strong></h4><p><em>1) We need to have private key, certificate issued by the CA.</em></p><p>The process is the same only point to be noted is that OCSP URL need to be specified in the user/server certificate, so that client can extract the OCSP URL and use it for testing if the user cert is valid/revoked. We add <strong><em>authorityInfoAccess</em></strong> extension for this purpose.</p><p>The sample extension is shown below:</p><pre>[ usr_cert_ocsp ]<br># Extensions for client certificates (`man x509v3_config`).<br>basicConstraints = CA:FALSE<br>nsCertType = client, email<br>nsComment = &quot;OpenSSL Generated Client Certificate&quot;<br>subjectKeyIdentifier = hash<br>authorityKeyIdentifier = keyid,issuer<br>keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment<br>extendedKeyUsage = clientAuth, emailProtection<br>authorityInfoAccess = OCSP;URI:http://localhost:8081</pre><p><em>2) Extract OCSP Responder URL from the certificate</em></p><pre>openssl x509 -in &lt;&lt;certificate to be verified&gt;&gt; -noout -ocsp_uri</pre><p><em>3) Command to check revocation status of a certificate.</em></p><pre>#It prints both the request and response<br>openssl ocsp -issuer &lt;&lt;CA cert file&gt;&gt; -nonce -cert &lt;&lt;cert to be checked&gt;&gt; -url &lt;&lt;ocsp responder url mentioned in the cert&gt;&gt; -text<br><br># prints only the response<br>openssl ocsp -issuer &lt;&lt;CA cert file&gt;&gt; -nonce -cert &lt;&lt;cert to be checked&gt;&gt; -url &lt;&lt;ocsp responder url mentioned in the cert&gt;&gt; -resp_text</pre><p>Nonce is a random number that is attached to a request and same number is placed in the response as well. This prevents a replay attack. A replay attack is when an attacker reads a response or a request and tries to reuse it by impersonating as the person/party and get access to a secured information or using same response everytime. A nonce makes sure that the OCSP response is always fresh. If we use -no_nonce attribute, then nonce is not added in request and response.</p><h3>Conclusion:</h3><p>Revocation status of certificates can be checked using CRLs or OCSP responders provided by Certificate Authority. OCSP has advantages over CRLs in being always giving latest status of certificates and client just needs to query status of a single certificate instead of downloading a document having a list of revoked certificates. It should be noted that the OCSP responder provided by OpenSSL is recommended only to be used while testing and development purposes, but its definitely a good tool to gain knowledge about OCSP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9c30f37a10a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Certificate Revocation List using OpenSSL]]></title>
            <link>https://medium.com/@arpanagupta10/understanding-certificate-revocation-list-using-openssl-c57f1b7dc43c?source=rss-a352ad164009------2</link>
            <guid isPermaLink="false">https://medium.com/p/c57f1b7dc43c</guid>
            <dc:creator><![CDATA[Arpana Gupta]]></dc:creator>
            <pubDate>Wed, 13 Sep 2023 02:56:21 GMT</pubDate>
            <atom:updated>2023-09-13T10:34:03.963Z</atom:updated>
            <content:encoded><![CDATA[<p>This document is in continuation to the <a href="https://medium.com/@arpanagupta10/exploring-cryptographic-content-using-openssl-42e1916b7d09">Generating cryptographic content using OpenSSL made easy</a>.</p><p>Below article explains about revocation process of certificates by Certificate Authority(CA). It is explained using OpenSSL as implementation software.</p><h3><strong>Brief on Basics</strong></h3><p>Public Key Infrastructure (PKI) Certificate is a trusted document generated by the Issuer at the request of Subject. Using this document, the issuer testifies that the subject’s information and subject’s public key present is valid for the giving time period mentioned in the certificate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*jnLegZbjbtS-M68ofS5vMA.png" /></figure><p>The PKI certificate is considered valid if following 2 conditions are met:</p><p>1) The certificate is valid as of the date of checking.</p><p>2) The certificate is not present in revoked certificates published as Certificate Revocation List (CRL) document by the CA.</p><p>Revoking a certificate by a CA, means warning the entity who wishes to use the certificate, that the certificate can no longer be trusted. There can be multiple reason for revocation: Private key gets compromised, CA is compromised, Certificate is on hold, if the current certificate is superseded by a different one.</p><p>There are two ways in which a client can verify if a certificate is valid:</p><p>1) Checking if the certificate’s serial number is present in the CRL document published by the CA. This URL is published as a CRL Distribution Point(s) in the certificate.</p><p>2) Querying the OCSP (Online Certificate Status Protocol) responder URL published by the CA. This URL is published as OCSP in the certificate.</p><p>Here in this article, we will use CRL to check the status of certificate issued by a CA.</p><h4><strong>Certificate Revocation List</strong></h4><h4><strong>Working of CRL</strong></h4><ol><li>CAs use CRL document to publicize the list of certificates that are revoked before their assigned expiration date.</li><li>This document is generated regularly (hourly, daily, weekly or monthly basis) by CA in the assigned directory.</li><li>It is downloaded by the third party to verify if user’s public certificate serial number is present in the document. If its present, then the certificate is considered as revoked.</li><li>This document is signed by the CA itself to make it trustworthy.</li><li>For the third party to access CRL document, the CA specifies a CRL Distribution Point(s)(CDP) attribute in the user’s public certificate at the time of generation. The URL is HTTP/LDAP protocol. The CRL is made available on this url.</li><li>A certificate may be revoked by CA, but its CRL may not have that certificate info present if the CRL document is not updated after the revocation. So, the CRL document must be created regularly to make sure it has latest and up-to-date list of invalid certificates.</li></ol><h3><strong>Format of CRL document</strong></h3><p><strong><em>It has X509 Version 2 CRL format </em></strong><em>as specified in RFC 5280 (</em><a href="https://datatracker.ietf.org/doc/html/rfc5280"><em>https://datatracker.ietf.org/doc/html/rfc5280</em></a><em>)</em></p><p>Following are the list of fields in a CRL:</p><p>i) Issuer details,</p><p>ii) The date the current CRL was generated,</p><p>iii) The date by which the next CRL will be generated,</p><p>iv) Revoked users’ certificates serial number and date of revocation</p><p>v) The Signature algorithm used.</p><h4>Steps followed by client side to check for revocation of a certificate:</h4><p>1) Obtain the user certificate for verification.</p><p>2) Obtain its CA’s certificate.</p><p>3) Read the CRL distribution point URL from the certificate.</p><p>4) Download and verify the CRL document.</p><p>5) Parse the CRL document, to look for serial number of certificates. The certificate is considered as revoked if its serial number is found.</p><p>Else, the third party can also use a single Openssl command (crl command) to verify the certificate.</p><p>Incase of Openssl CA, it maintains a database (i.e its index file) with details and status of certificates issued so far. The status values are R(Revoked) or V (Valid).</p><h4><strong>Openssl Commands used for CRL</strong></h4><p><em>1) Setting CRL Distribution Point in Public Certificate</em></p><p>There are two ways in which we can set cRLDistributionPoint URL in user’s certificate.</p><p>i. By adding the URL in openssl.cnf file, under specific extensions section, as shown in the sample below under <em>cdp1</em> section while issuing an user certificate.</p><pre>[ usr_cert ]<br># Extensions for client certificates (`man x509v3_config`).<br>basicConstraints = CA:FALSE<br>nsCertType = client, email<br>nsComment = &quot;OpenSSL Generated Client Certificate&quot;<br>subjectKeyIdentifier = hash<br>authorityKeyIdentifier = keyid,issuer<br>keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment<br>extendedKeyUsage = clientAuth, emailProtection<br>crlDistributionPoints = cdp1<br><br>[ cdp1]<br>fullname=URI:http://localhost:8080/ca/test.crl</pre><p>ii. By creating a file with URL data and passing it as attribute while generating certificate by CA. The above content can be used in file without specifying section name.</p><p>For more information about various other name-value pair approved for distribution point refer to “CRL distribution points” section in <a href="https://www.openssl.org/docs/man3.1/man5/x509v3_config.html">https://www.openssl.org/docs/man3.1/man5/x509v3_config.html</a></p><p><em>2) Revoking a certificate</em></p><pre>openssl ca -revoke /newcerts/sampleCert.crt -config location/of/openssl.cnf</pre><p>After successful execution of revoke command, only the CA database,i.e. index file is updated. There is no change in revoked certificate. The index file can be checked with the certificate’s entry marked as ‘R’ (revoked)</p><p><em>3) Generating CRL</em></p><pre>openssl ca -gencrl -config subca1.conf -out crl/crl.txt</pre><p>This certificate revocation list (CRL) is a X509 version 2 PEM file. It contains serial numbers of certificates generated by this CA that are revoked. They also contain many fields like revocation date of certificates, crl’s version number, issuer’s details, signature algorithms etc. Its PEM file has standard header and footer lines as</p><p><em>— — -BEGIN X509 CRL — — -</em></p><p><em>— — -END X509 CRL — — -</em></p><p><em>4) Viewing contents of CRL</em></p><pre>openssl crl -in location/of/crl/doc -text -noout</pre><p>The crl command processes the crl in pem or der format. We can visually verify the list of revoked certificates via this command.</p><p><em>5) Verifying validity of certificate by third party</em></p><pre>openssl verify -crl_check -crl_download -CAfile &lt;&lt;CA chain file&gt;&gt; &lt;&lt;certificate to be verified&gt;&gt;</pre><p>Note: The above command automatically downloads the CRL present in CRLDistributionPoint mentioned in the public certificate and verifies if the certificate is not revoked. We also need to make sure that the CRL document published on the URL is in DER form and not PEM format.</p><h3>Conclusion</h3><p>I hope this article gives clarity on Certificate Revocation List and its corresponding commands in openssl.</p><p><em>Next:</em></p><p>In continuation to our certificate verification topic, we have next article on alternative method to check revocation of certificate-<a href="https://medium.com/@arpanagupta10/certificate-revocation-with-ocsp-using-openssl-a9c30f37a10a">Online Certificate Status Protocol(OCSP)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c57f1b7dc43c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring cryptographic content using OpenSSL]]></title>
            <link>https://medium.com/@arpanagupta10/exploring-cryptographic-content-using-openssl-42e1916b7d09?source=rss-a352ad164009------2</link>
            <guid isPermaLink="false">https://medium.com/p/42e1916b7d09</guid>
            <dc:creator><![CDATA[Arpana Gupta]]></dc:creator>
            <pubDate>Sun, 10 Sep 2023 12:54:56 GMT</pubDate>
            <atom:updated>2023-11-07T11:41:01.645Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7HAKZHntVDNfvhcM" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When I started exploring and experimenting Distributed Ledger Technologies, one of the prime requirements was understanding cryptography. I started searching for good articles to learn cryptography. After reading multiple of them, realised that now the knowledge needs to be expanded towards generating cryptographic content. There are many opensource implementation cryptographic libraries available — OpenSSL, Let’sEncrypt, LibreSSL etc. Read about the widely used opensource tool — OpenSSL. Openssl is a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library. In this article, I would like to share my learnings,so that readers can use openssl easily.</p><p>Cryptography is foundation in building trust while communicating between two parties or broadcasting messages in a secure way. It is used in various communication protocols (https), webservers. It is also used in endorsing content via signature, verification, encryption/decryption, hashing processes and as identity authentication in decentralized ledger technology (blockchain).</p><p>OpenSSL is a widely used free, open-source software to generate, process and test cryptographic content. It has evolved over years and support majority of algorithms. Before we go into detail of steps of generating content using OpenSSL let us start with basic terms used to understand the concept thoroughly.</p><p><strong>Brief on basics:</strong></p><p><strong>Types of Cryptography:</strong></p><p>1) Symmetric Cryptography</p><p>A single key is used for encryption and decryption of messages. This key is known to both the communicating parties. The key is shared via a secure channel or offline. Most widely used algorithm is AES (Advanced Encryption Standard), DES.</p><p>2) Asymmetric Cryptography — also known as PKI- Public Key Infrastructure. Two different keys (Private and Public key pair) generated by an algorithm (RSA, ECDSA, DSA) are used for encryption and decryption of messages.</p><p><strong>Following are few important entities involved in the PKI lifecycle:</strong></p><p>1) <strong>Subject </strong>— The entity that holds the key pair and has a public certificate. This may be a user or server.</p><p>2) <strong>Issuer </strong>— The entity that generates/issues the public certificate after receiving request from the Subject. They are publicly known Certificate Authority (CA) or an Intermediate Certificate Authority (ICA) which in turn has certificate issued by a known CA.</p><p>3) <strong>Registration Authority (RA)</strong> — This is an optional entity (between subject and issuer) that accepts the certificate creation request from subject and sends it to Issuer after validation of data. This may or may not be present. In this article we are going to assume that the Issuer (CA) and RA are taken care within Openssl. This is for making understanding of usage of OpenSSL easier.</p><p>4) <strong>Third party </strong>— This entity may be a server/client/end-user. They use the Subject’s certificate for performing a trusted communication with the subject.</p><p><strong>Cryptographic content in PKI:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*QMC9Wn0NElBVRhShJ0pk1w.png" /></figure><p><strong><em>1) Private and Public Key-</em></strong></p><p>a. This is always generated at the client/requestor end and no one else knows about it. So, in order to generate it we need to have requisite generation tool at client’s end.</p><p>b. This is most important file for cryptography to work in all applications.</p><p>c. Private and Public keys are always generated in pair.</p><p>d. Private key is set randomly by the software and then public key is generated internally depending on the public key algorithm. Some software may also use seed data from user to generate the private key.</p><p>e. Private key is used for encryption/decryption, endorsement/signing a document.</p><p>f. Public key is used for encryption/decryption, verification of a digitally signed document.</p><p>g. This keypair does not expire and is always valid.</p><p>h. Private key is to be kept in safe and secure location.</p><p><strong><em>2) CSR-Certificate Signing Request</em></strong></p><p>a. This is also generated at the client’s end where certificate is to be installed.</p><p>b. It is a document that contains — Client’s identity information (Country name, District, Locality, Organization Name, Organizational Unit, Common Name, email address), Public Key, signature algorithm identifier and digital signature on the certificate request information.</p><p>c. It may also contain extra extensions information, that the client’s wants on the certificate. Like, if its a CA, keyUsage values, etc. Note: these values may also be set by CA while creation of certificate using CSR.</p><p>d. The digital signature in CSR is generated using Client’s private key. It is considered as a ‘Proof of Possession’ to the Certificate Authority (CA) that the requestor/client has a private and public key pair.</p><p>e. CA on receiving the requested document verifies the signature using the client’s public key. After performing all checks on identity information, the certificate is generated.</p><p>f. CSR document is not used anywhere once the document is sent to CA and certificate is obtained from it. It may be used again when we want to create another certificate.</p><p><strong><em>3) Public Certificate</em></strong></p><p>a. This is generated at the Certificate Authority’s (server’s) end.</p><p>b. On a high level it is basically a document with requestor’s public key which is signed by private key of a certificate authority.</p><p>c. This is analogous to paper version of degree certificates that are signed by a competent, trusted university authority, which are accepted globally.</p><p>d. Once the CA verifies the client’s data it prepares a certificate containing following info:</p><p>i. All identity related information received from client in CSR. This also include the validity dates (start-date and end-date) of certificate.</p><p>ii. Public Key of requestor/client</p><p>iii. Specific information/role of client that only CA can provide — Like if the requestor is a CA itself (intermediate CA), or can endorse/sign, or requestor is a user / a server etc. These are X509 V3 extensions.</p><p>iv. Certificate Authority’s signs this document using its private key.</p><p>e. For certificate verification purposes, we use the public certificate of CA. The client/requestor always has its own public certificate and public certificate of CA(s). The public certificate of CA is required to verify the client’s public certificate.</p><p><strong>vi.</strong> Usually, we have an end-user’s certificate, obtained from an intermediate CA. The public certificate of this Intermediate CA is in turn signed by a Root CA or yet another Intermediate CA. Then in this case, the end user must have all the Intermediate CA’s certificates and Root CA’s certificate. This concept is “chain-of trust”. Such a single chain certificate is created simply by concatenating all the CA’s certificate starting from the last Intermediate certificate (CA which generated certificate for the end-user)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qVe9YVBrTXHegv_6kSGSew.png" /><figcaption>Source : <a href="https://en.wikipedia.org/wiki/Root_certificate#/media/File:Chain_of_trust_v2.svg">https://en.wikipedia.org/wiki/Root_certificate#/media/File:Chain_of_trust_v2.svg</a></figcaption></figure><p><strong>Formats and Standards used:</strong></p><p>There are various algorithms and implementation tools for generating cryptographic material, but all are represented in standard formats/protocols — PKCS (Private Key Cryptographic Standard). There are 15 PKCS standards. Few PKCS standards are used widely, and they are as follows:</p><p>i) PKCS #8 — Private Key information syntax standard- specifies algorithm used, key. It can be encrypted (using pass phrase) or unencrypted.</p><p>ii) PKCS #10 — Certificate Signing Request — Format of message sent to Certificate Authority to sign its requestor’s public key.</p><p>iii) PKCS #12 — defines a format to bundle multiple files together — like a private key and its certificates, Chain certificates.</p><h4><strong><em>About X509</em></strong></h4><p>Apart from PKCS we have X509 format for Certificates and CRLs.</p><p><strong><em>i) X509 V3 — PKI certificate format</em></strong></p><p>This contains —</p><ol><li>Certificate Version number, Subject Details, Subject’s Public key, Issuer’s details and Issuer’s signature,Subject key Identifier, Authority Key Identifier</li><li>Certificate Validity date and time.</li><li>It also contains the Algorithm used by Subject’s Public key and Issuer’s Signature.</li><li>To this base certificate various extensions are added — Subject Alternative Names, CRL Distribution Points, OCSP URL, Key Usages etc.</li></ol><p>The publicly known signing authority digitally signs the above certificate information via its(CA’s) own private key.</p><p><strong><em>ii) X509 Version 2 –Certificate Revocation List (CRL) format</em></strong></p><p>The private key file, CSR, certificate or CRL files can be in PEM or DER files.</p><p>i) PEM (Privacy Enhanced Mail) is Base64 encoded format. It contains an encoded data with a distinct text header (“ — — -BEGIN &lt;&lt;file type&gt;&gt;&gt; — — -”) and text footer (“ — — -END &lt;&lt;file type&gt;&gt;&gt; — — -”). So, we can always open a .pem file and verify the type of file (private key file/csr/certificate/encrypted private key/X509 CRL etc) by reading its header and footer.</p><p>ii) DER (Distinguished Encoding Rules)- It’s a binary encoded/raw form.</p><h3><strong>OpenSSL</strong></h3><p><strong>Prerequisites:</strong></p><p>1) Openssl needs to be installed on</p><p>a. Client machine — For generating private key, and Certificate Signing Request (CSR)</p><p>b. Server machine acting as a Certificate Authority (CA) — For generating public certificates, Certificate renewal and revocation.</p><p><strong>Understanding Openssl directory in case of Root CA / Intermediate CA</strong></p><p>1)Create following folder and sub-folders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*kRa1jW1fC_Ljz4vh7QJEtA.png" /></figure><p>2) Create files under db folder using following commands that inputs relevant data in them:</p><pre>#Empty index file<br>touch db/index<br><br>#serial number (on certificates) file with a starting number <br>openssl rand -hex 16 &gt; db/serial<br><br>#crl number file, with initial value that keeps on incrementing<br>echo 1001 &gt; db/crlnumber</pre><p>3) Let us see about the created folders and files.</p><p>a. ca folder — This is a root directory containing all the openssl related files.</p><p>b. db folder — This sub-folder contains files having data, sequence numbers that would be referenced while generating certs, crl. It also contains a list of valid and revoked certificates.</p><p>c. private folder — It contains the private key file of the CA.</p><p>d. certs — This contains all the public certificate file of CA.</p><p>e. newcerts — This contains all the public certificates that CA has issued so far.</p><p>f. index file — This file is initially empty and contains a list of all valid and revoked certs issued.</p><p>g. serial file — This serial file contains a serial number used in certificates. This number is autoincremented once a new certificate is generated.</p><p>h. crlnumber file — This serial file contains a number used to assign to the certificate to be revoked. This number is autoincremented once a certificate is revoked.</p><p><strong>About Openssl configuration file</strong></p><p>1) The default configuration file is openssl.cnf file.</p><p>This file comes with openssl software installation. It needs to be customised when using this software as a CA but need not be updated while using software as a client for private key and CSR generation. The file contains:</p><p>a) Location of public certificate, private key, serial number, crl number etc.</p><p>b) Standard/default identity related attributes to de displayed while generating CSR.</p><p>c) Requirements for checking identity values may match existing identity values while certificate generation.</p><p>d) Required Extensions (CA or not, other values) to be added in public certificate while cert generation process.</p><p>2) Following is a sample openssl.cnf file for reference that can be used as is.</p><pre>[ ca ]<br> # `man ca`<br> default_ca = CA_default<br> <br> [ CA_default ]<br> # Directory and file locations.<br> dir = /ca<br> certs = $dir/certs<br> crl_dir = $dir/crl<br> new_certs_dir = $dir/newcerts<br> database = $dir/db/index<br> serial = $dir/db/serial<br> RANDFILE = $dir/private/.rand<br> <br> # The root key and root certificate.<br> private_key = $dir/private/sub-ca1.key<br> certificate = $dir/certs/sub-ca1–3.crt<br> <br> # For certificate revocation lists.<br> crlnumber = $dir/db/crlnumber<br> crl = $dir/crl/ca.crl.pem<br> crl_extensions = crl_ext<br> default_crl_days = 30<br> <br> # SHA-1 is deprecated, so use SHA-2 instead.<br> default_md = sha256<br> <br> name_opt = ca_default<br> cert_opt = ca_default<br> default_days = 375<br> preserve = no<br> policy = policy_loose<br> <br> [ policy_strict ]<br> # The root CA should only sign intermediate certificates that match.<br> # See the POLICY FORMAT section of `man ca`.<br> countryName = match<br> stateOrProvinceName = match<br> organizationName = match<br> organizationalUnitName = optional<br> commonName = supplied<br> emailAddress = optional<br> <br> [ policy_loose ]<br> # Allow the intermediate CA to sign a more diverse range of certificates.<br> # See the POLICY FORMAT section of the `ca` man page.<br> countryName = optional<br> stateOrProvinceName = optional<br> localityName = optional<br> organizationName = optional<br> organizationalUnitName = optional<br> commonName = supplied<br> emailAddress = optional<br> <br> [ req ]<br> # Options for the `req` tool (`man req`).<br> default_bits = 2048<br> distinguished_name = req_distinguished_name<br> string_mask = utf8only<br> <br> # SHA-1 is deprecated, so use SHA-2 instead.<br> default_md = sha256<br> <br> # Extension to add when the -x509 option is used.<br> x509_extensions = v3_intermediate_ca<br> <br> [ req_distinguished_name ]<br> # See &lt;https://en.wikipedia.org/wiki/Certificate_signing_request&gt;.<br> countryName = Country Name (2 letter code)<br> stateOrProvinceName = State or Province Name<br> localityName = Locality Name<br> 0.organizationName = Organization Name<br> organizationalUnitName = Organizational Unit Name<br> commonName = Common Name<br> emailAddress = Email Address<br> <br> # Optionally, specify some defaults.<br> countryName_default = GB<br> stateOrProvinceName_default = England<br> localityName_default =<br> 0.organizationName_default = Alice Ltd<br> organizationalUnitName_default =<br> emailAddress_default =<br> <br> [ v3_ca ]<br> # Extensions for a typical CA (`man x509v3_config`).<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid:always,issuer<br> basicConstraints = critical, CA:true<br> keyUsage = critical, digitalSignature, cRLSign, keyCertSign<br> <br> [ v3_intermediate_ca ]<br> # Extensions for a typical intermediate CA (`man x509v3_config`).<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid:always,issuer<br> basicConstraints = critical, CA:true, pathlen:0<br> keyUsage = critical, digitalSignature, cRLSign, keyCertSign<br> <br> [ usr_cert ]<br> # Extensions for client certificates (`man x509v3_config`).<br> basicConstraints = CA:FALSE<br> nsCertType = client, email<br> nsComment = &quot;OpenSSL Generated Client Certificate&quot;<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid,issuer<br> keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment<br> extendedKeyUsage = clientAuth, emailProtection<br> crlDistributionPoints = cdp1<br> <br> [ usr_cert_ocsp ]<br> # Extensions for client certificates (`man x509v3_config`).<br> basicConstraints = CA:FALSE<br> nsCertType = client, email<br> nsComment = &quot;OpenSSL Generated Client Certificate&quot;<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid,issuer<br> keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment<br> extendedKeyUsage = clientAuth, emailProtection<br> authorityInfoAccess = OCSP;URI:http://localhost:8081<br> <br> <br> [ server_cert ]<br> # Extensions for server certificates (`man x509v3_config`).<br> basicConstraints = CA:FALSE<br> nsCertType = server<br> nsComment = &quot;OpenSSL Generated Server Certificate&quot;<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid,issuer:always<br> keyUsage = critical, digitalSignature, keyEncipherment<br> extendedKeyUsage = serverAuth<br> <br> [ crl_ext ]<br> # Extension for CRLs (`man x509v3_config`).<br> authorityKeyIdentifier=keyid:always<br> <br> [ ocsp_cert ]<br> # Extension for OCSP signing certificates (`man ocsp`).<br> basicConstraints = CA:FALSE<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid,issuer<br> keyUsage = critical, digitalSignature<br> extendedKeyUsage = critical, OCSPSigning<br> <br> [ cdp1]<br> fullname=URI:http://localhost:8080/ca/test.crl</pre><ol><li>Location of openssl.cnf can be found by executing the following command.</li></ol><pre>openssl version -a</pre><p>Then search for value of ‘OPENSSLDIR’ attribute.</p><p>Note: This is default/set location of config file. But we can copy and update config file and specify it as <em>-config</em> option while executing required openssl commands. We will see this below.</p><p>2. When using as a CA following sections need to be updated.</p><p>[ CA_default ] — This section stores the locations of the above created folders and files. Update them with exact values. It also stores default validity days that needs to be provided in issued certificates. Note this also contains the file path of CA’s private key file and its corresponding certificate. These two files can be updated, only after its private key and certificate are generated.</p><pre>[ CA_default ]<br> # Directory and file locations.<br> dir = /ca<br> certs = $dir/certs<br> crl_dir = $dir/crl<br> new_certs_dir = $dir/newcerts<br> database = $dir/db/index<br> serial = $dir/db/serial<br> RANDFILE = $dir/private/.rand<br> <br> # The root key and root certificate.<br> private_key = $dir/private/root-ca.key<br> certificate = $dir/certs/root-ca.crt<br> <br> # For certificate revocation lists.<br> crlnumber = $dir/db/crl<br> crl = $dir/crl/ca.crl.pem<br> crl_extensions = crl_ext<br> default_crl_days = 30<br> <br> # SHA-1 is deprecated, so use SHA-2 instead.<br> default_md = sha256<br> <br> name_opt = ca_default<br> cert_opt = ca_default<br> default_days = 375<br> preserve = no<br> policy = policy_strict</pre><p>[ req_distinguished_name ] — This contains default values of identity related attributes that gets displayed when creating CSR. It can be overridden when CSR is being created.</p><p>[ usr_cert ] &amp; [ v3_ca ] — These are extensions will be used while creating certificates. They are discussed below.</p><h3><strong>Openssl Commands:</strong></h3><p><strong>1) Ways to generate Private-Public key pair.</strong></p><p><em>a. Generating key pair using generic command for any algorithm</em></p><p><em>i. Unencrypted</em></p><p><em>ii. Encrypted (using passphrase)</em></p><p><em>b. Generating keypair using algorithm specific commands</em></p><p><strong>2) Processing the keypair</strong></p><p><em>a. Reading keys</em></p><p><em>b. Converting from one form to another</em></p><p><strong>3) Generating CSR</strong></p><p><em>a. Generating CSR using existing private-public key</em></p><p><em>b. Generating CSR and Private-public key together</em></p><p><strong>4) Generating public certificate</strong></p><p><em>a. Self-signed certificate for Root CA</em></p><p><em>b. Certificate for Intermediate CA</em></p><p><em>c. Certificate for non-CA entity</em></p><p><strong>5) Processing and reading certificate.</strong></p><p><strong>6) Generating chain certificate</strong></p><p><strong>7) Verifying a certificate</strong></p><h3><strong>Commands in detail</strong></h3><p><strong>1) Generating Private -public key pair</strong></p><p>i) <strong><em>genpkey</em></strong> — single command to generates all types of private keys depending on the algorithm and its options specified.</p><p>ii) <strong><em>rsa /ecparam</em></strong> — private key generators based on the command chosen.</p><p><em>a. Generating key pair using generic command for any algorithm with/without passphrase</em></p><p>Various Algorithms supported are RSA, RSA-PSS, EC, X25519, X448, ED25519 and ED448. For few algorithms, the command must include pkeyopt attribute(s) that contains required parameters. For example, if we use elliptic curve (EC) Algorithm, then we need to specify the NIST curve name</p><pre>  #We can get the list of curve names for pkeyopt using<br> openssl ecparam -list_curves<br> <br> #genpkey command generates private key using EC and prime256v1 curve<br> openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:prime256v1 -out fd.key<br> <br> #genpkey command generates private key and encrypts it with a passphrase using AES<br> openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:prime256v1 -out fd.key -aes-128-cbc</pre><p>More details about genpkey: <a href="https://www.openssl.org/docs/man3.0/man1/openssl-genpkey.html">https://www.openssl.org/docs/man3.0/man1/openssl-genpkey.html</a></p><p>b. Generating keypair using algorithm specific commands</p><pre>#RSA Key<br> openssl genrsa -out rsakey.pem 1024<br> <br> #EC Key<br> openssl ecparam -genkey -name prime256v1 -noout -out ec256key.pem</pre><p><em>2) Processing the keypair</em></p><p>There are many commands for this purpose with similar way of processing key– <strong><em>pkey, ec, rsa</em></strong></p><pre>#This will display private and public key in a formatted manner and not as a blob of random data<br> openssl pkey -in fd.key -text -noout<br> <br> #This will output only the public key in a new file<br> openssl pkey -in fd.key -pubout -out fd-pub.key</pre><p><em>3) Generating CSR</em></p><p><em>a. Generating CSR and Private-public key together</em></p><pre>openssl req -new -out root-ec-ca.csr -keyout root-ec-ca.key -pkeyopt ec_paramgen_curve:P-256 -newkey EC:256</pre><p>This command will generate private key and generate CSR file. For CSR file it will take input about user’s details via screen. We can also prespecify user details — Subject Name, Email Address, Org details via <strong><em>subj</em></strong> attribute.</p><p>Incase we want to change the openssl.cnf file, then we can specify the updated configuration file via <strong><em>config</em></strong> attribute in the command.</p><p><em>b. Generating CSR using existing private-public key</em></p><pre>openssl req -new -key fd.key -out newCsr.csr</pre><p><em>c. Reading CSR</em></p><pre>openssl req -in newCsr.csr -text -noout</pre><p><em>4) Generating public certificate</em></p><p><em>a. Self-signed certificate for Root CA</em> — This is top-level certificate in chain of trust and in this certificate the Subject and Issuer are the same. The Root CA can issue certificates to other Intermediate CAs or to End-users(who are not CA).</p><pre>#Generating root cert using private key<br> openssl req -new -x509 -days 365 -key root-ca.key -out root-ca.crt -extensions v3_ca<br> <br> #Generating new root cert and new private key<br> openssl req -new -x509 -out root-ca.crt -keyout root-ca.key -pkeyopt ec_paramgen_curve:P-256 -newkey EC:256 -extensions v3_ca -noenc -days 365<br> <br> #Generating root cert using previously generated CSR<br> openssl ca -selfsign -config &lt;&lt;openssl.cnf file&gt;&gt; -in &lt;&lt;csr file generated&gt;&gt; -out root-ca.crt<br> -extensions v3_ca</pre><p>Points to note:</p><p>· Here the certificate may be generated with/without generating CSR and with/without private key. If no private key is passed as input then appropriate key generation algo must be specified to generate the same.</p><p>· The <strong>-extensions</strong> in above commands, specifies a section name ‘v3_ca’ in openssl.cnf. This section has X509v3 attributes that need to be specified in certificate. While generating Root Certificate, this specifies that the subject is a CA and other roles.</p><p>· Alternatively, if we don’t want to update the opnessl.cnf file, we can save the desired extensions in a file and pass it as a parameter to the extfile attribute in ca command.</p><pre>openssl ca -selfsign -config &lt;&lt;openssl.cnf file&gt;&gt; -in &lt;&lt; csr file generated&gt;&gt; -out root-ca.crt -extfile rootca.ext</pre><p>where the contents of rootca.ext file is as follows:</p><pre>subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid:always,issuer<br> basicConstraints = critical, CA:true<br> keyUsage = critical, digitalSignature, cRLSign, keyCertSign</pre><p>· When root key and root cert is generated, we need to update the private_key and certificate values in [ca-default] section in openssl.cnf. Then this config file should be used in all further certificate generation commands. It can either saved in default OPENSSLDIR location as openssl.cnf or a separate file. When using a separate file, we always need to specify it in the ca commands of openssl.</p><p>· Note: more detail about various attributes of req and ca commands can be found in</p><p>· <a href="https://www.openssl.org/docs/man3.0/man1/openssl-req.html">https://www.openssl.org/docs/man3.0/man1/openssl-req.html</a></p><p>· <a href="https://www.openssl.org/docs/man3.0/man1/openssl-ca.html">https://www.openssl.org/docs/man3.0/man1/openssl-ca.html</a></p><p><em>b. Certificate for Intermediate CA</em></p><p>In this process, we would be needing CSR generated for intermediate CA.</p><pre>openssl ca -in &lt;&lt;CSR generated for Intermediate CA&gt;&gt; -config &lt;&lt;openssl.cnf file&gt;&gt; -days 365 -out ica.crt -extensions v3_ca</pre><p>Points to note:</p><p>· Here we need to make sure the extensions is pointing to the correct section in openssl.cnf</p><p>· After both the Intermediate CA’s key and Cert are generated, we need to update the private_key and certificate values in [ca-default] section in corresponding openssl.cnf. Now whenever we use this file the CA will act as Intermediate CA.</p><p>· This certificate can be generated by a Root CA or another Intermediate CA</p><p><em>c. Certificate for non-CA entity</em></p><pre>openssl ca -in &lt;&lt;end-user csr file&gt;&gt; -config &lt;&lt;openssl.cnf file of CA&gt;&gt; -days 365 -out end-user.crt -extensions usr_cert</pre><p>Points to note:</p><p>· The command is same as for Intermediate CA, only difference is the extensions attribute. Here [usr_cert] section is in the appropriate openssl.cnf file.</p><p>· We can also have a [server_cert] section in openssl.cnf that could be used by non-CA webservers where few attributes may be different. An example is posted below:</p><pre>[ server_cert ]<br> # Extensions for server certificates (`man x509v3_config`).<br> basicConstraints = CA:FALSE<br> nsCertType = server<br> nsComment = &quot;OpenSSL Generated Server Certificate&quot;<br> subjectKeyIdentifier = hash<br> authorityKeyIdentifier = keyid,issuer:always<br> keyUsage = critical, digitalSignature, keyEncipherment<br> extendedKeyUsage = serverAuth</pre><p><em>5) Processing and reading certificate.</em></p><pre>#Command to print the certificate file on console<br> openssl x509 -in end-user.crt -noout -text<br> <br> #Converting the PEM format of certificate to DER format<br> openssl x509 -in &lt;&lt;certificate file&gt;&gt; -inform PEM -outform DER -out &lt;&lt;new DER file name&gt;&gt;</pre><p>More details about various attributes of x509 command can be found in</p><p><a href="https://www.openssl.org/docs/man3.0/man1/openssl-x509.html">https://www.openssl.org/docs/man3.0/man1/openssl-x509.html</a></p><p><em>6) Generating chain certificate</em></p><p>Any command that concatenates multiple files together</p><pre>cat ica1.crt ica2.crt rootca.crt &gt; chaincert.crt</pre><p>Where ica1.crt, ica2.crt — Intermediate CA certificates</p><p>rootca.crt — Root CA’s certificate</p><p><em>7) Verifying a certificate</em></p><pre>openssl verify -CAfile chaincert.crt &lt;&lt;certToVerify.crt&gt;&gt;</pre><p>Note: The command verifies the issuer(s) in the certificate till root issuer. So, the -<em>CAfile</em> should a file of trusted certificate(s)</p><h3><strong>Conclusion:</strong></h3><p>In this article we took a deep dive on cryptographic content. We explored various pieces of openssl in detail. Openssl is an excellent command line cryptographic library, that can act as a client or server depending on requirement. It is widely used by many web servers. It can be helpful in following ways:</p><ol><li>Used in commercial and non-commercial applications.</li><li>In getting a detailed knowledge of lifecycle of public certificates. We can get an in-depth hands-on experience trying various commands and understanding full process.</li><li>It can be easily used as an private CA for establishing internal security of an organization.</li><li>It can be used as a go-to CA for development environments, where we can do complete production-like set-up and testing of certificates. We don’t need to simply use self-signed certificates for such environments.</li></ol><p>I will share detailed understanding on using CRL and OCSP Responder in Openssl in next few articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42e1916b7d09" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>