<?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 Janith Weerasinghe on Medium]]></title>
        <description><![CDATA[Stories by Janith Weerasinghe on Medium]]></description>
        <link>https://medium.com/@janithcmw?source=rss-3cc328d6776d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mZJQS2a4OMM9E-X0.jpg</url>
            <title>Stories by Janith Weerasinghe on Medium</title>
            <link>https://medium.com/@janithcmw?source=rss-3cc328d6776d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:40:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@janithcmw/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[Trust Chain of the Certificates]]></title>
            <link>https://medium.com/@janithcmw/trust-chain-of-the-certificates-9af96e33579f?source=rss-3cc328d6776d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9af96e33579f</guid>
            <dc:creator><![CDATA[Janith Weerasinghe]]></dc:creator>
            <pubDate>Tue, 10 Oct 2023 08:56:05 GMT</pubDate>
            <atom:updated>2023-10-10T08:56:05.687Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi Peeps,</p><p>When it comes to modern-day communication, TLS is being used to achieve a secure connection between either server-server or client-sever.</p><p>There are 3 main points when considering a secure connection in TLS.</p><ol><li>Authentication.</li><li>Data Encryption.</li><li>Data Integrity.</li></ol><p><strong>Authentication.</strong></p><p>To achieve the authentication the certificates are used. The certificate will be a unique entity that can be used to identify a server or a client(there can be clients which do not use certificates such as browsers). So this uniqueness can be used for authentication of the TLS communication.</p><p><strong>Data Encryption.</strong></p><p>The data will be encrypted and transmitted to the desired endpoint in a TLS connection. In the TLS channel basically, there will be two types of data encryptions. They are asymmetric key encryption (use the public key of the endpoint for encryption and a private key will be used to decrypt the data once the data reaches the endpoint) and symmetric key encryption(the same key will be used for encryption and description of data).</p><p>Once the data is encrypted it is not possible to access the data by monitoring the network packets.</p><blockquote>Asymmetric key encryption.</blockquote><blockquote>In asymmetric key encryption, asymmetric keys are used to encrypt data and decrypt data. Similar to the public key encryption and private key decryption. This method is somewhat slower and needs considerable compilation power than the symmetric key encryption.</blockquote><blockquote>So during the SSL handshake, once the initiate details are shared a symmetric key will be shared between two parties under asymmetric encryption. Then the shared symmetric key will be used for encrypted communication. Since the symmetric keys are being used the description speed will be increased.</blockquote><blockquote>Symmetric key encryption.</blockquote><blockquote>In symmetric key encryption, equal keys are being used to encrypt and decrypt data. In an SSL handshake, the two parties will agree on a symmetric key and then the particular key will be used for both encryption and decryption in communication.</blockquote><p><strong>Data Integrity.</strong></p><p>Data integrity is there to identify the data modification attacks that can happen similar to the man-in-the-middle attacks. To check the integrity the sender will calculate a hash value of the sending message and then send it to the endpoint. Once the endpoint is read the message the endpoint will also calculate the same hash value over the received message and then validate the generated hash value in the endpoint and the received value from the sender. If there is no difference between two values there will be no changes to the message within the transport layer.</p><p>With this method, it is possible to validate whether there is any modification to the message at the network layer. If there is no modification to the message, it can confirm the integrity of the data.</p><p>Now let’s get back to our original topic, ‘Trust Chain of the Certificates’.</p><p>For a successful communication, it is required to trust the certificates of each other of the communication endpoints. Based on this trust, a communication channel will be established. To build this trust it is possible to use CA-signed certificates or Self-signed certificates.</p><p>Let’s consider a practical scenario. For a TLS communication, it is required to trust the certificate of the endpoint that is being engaged. It is not feasible to trust all the certificates manually of all the servers on the internet(if there is a use-case such as a browser which should be communicated securely for almost all the websites). But this will be solved with the concept of the certificate trust chain. So if a server or client trusts the Root certificate of a particular end/entity certificate, the server or client will trust the corresponding end/entity certificate.</p><p>Since there are few numbers of Root CAs across the globe and if all the Root CAs are integrated with a client or a server, the particular client or server will trust all the CA-Signed certificates issued by the Root CAs.</p><p>The pointed trust will be built using a trust chain with a hierarchical order. So if any of the clients or servers trust the certificate of one point of the trust chain, the particular client or server will trust all the higher(towards the root level of the certificate chain) certificates in the hierarchical order of the certificate chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b5NvJovU7m1ur20q" /><figcaption>Diagram of a sample Certificate Chain</figcaption></figure><p>So for CA-Singed certificates, it is enough to have the corresponding Root CA certificate or any other Intermediate CA certificate to complete the trust chain. But for the Self-signed certificates(it is possible to create a certificate chain using Self-signed certificates as well), there will be no trust chain so it is required to trust the exact certificate to complete the successful TLS connection.</p><p>If we consider a client similar to a browser, it should be capable of communicating with any of the websites securely based on the user’s requirements. In such cases, it is practically difficult to use Self-signed certificates because if the Self-signed certificates are used it is required to add all the certificates under the trusted list of the browser manually. This will cause difficulties in certificate management at the browser(the client) level.</p><p>But with the CA-Singed certificates, this will not be the case and once the certificates of the Root CA are added to the trusted list, the browser will be able to trust all the certificates which are issued/signed by the particular Root CA.</p><p>Because of this, most of the vendors will integrate all the known Root CAs in their systems by default.</p><p>This is why your browser can connect to YouTube, Facebook or any popular site securely even if you have done any modification to the trust certificates.</p><p>Let’s discuss the main components of a certificate chain.</p><p><strong>Root CA certificate.</strong></p><p>Root CA certificates are issued by the Root CA(Root Certificate Authority) and this is the topmost certificate of the trust chain. The root certificate is a self-signed certificate which is owned by the Root CAs[1]. Since the Root CA certificate is a Self-signed certificate the values of the issuer and the owner properties will be equal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7CZ3YNzeF6WmFrZI" /><figcaption>Content of the public Root Certificate.</figcaption></figure><p>To uniquely identify the certificate the ‘certificate fingerprint’ can be used and once the certificate is exported, it is possible to calculate the ‘certificate fingerprint’ using the following command.</p><pre>openssl x509 -noout -fingerprint -sha256 -inform pem -in &lt;certificate_file&gt;</pre><p>To export the public certificate of an endpoint the following command can be used.</p><pre>openssl s_client -connect &lt;server_host&gt;:&lt;server_port&gt; -servername &lt;server_host&gt;</pre><p>Also, you will be able to export a public certificate using the browser as well and the following is a sample clip from the Chrome browser of certificate extraction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZYT2mXTwpeKudje_rlIJgA.gif" /><figcaption>Export a certificate from a browser.</figcaption></figure><p><strong>Intermediate CA certificate.</strong></p><p>So when we consider the Root CAs and End/Entity certificates which are signed by the Root CA, it will be enough to complete the trust chain. But in the trust chain, there is another certificate concept, intermediate certificates. Intermediate certificates are issued by the Intermediate CAs.</p><p>These intermediate certificates are signed by the Root CA(or there can be situations where the intermediate certificate is signed by another intermediate certificate which is in the higher level of the trust chain) and the intermediate certificates are used to issue(sign) the end certificates based on the requests.</p><p>Please refer to the below sample intermediate certificate and focus on the property ‘CN’ of ‘Owner’ and ‘Issuer’ and you can observe that the ‘Issuer’ details are identical to the previously pointed Root CA certificate. This means that this certificate is signed by the Root CA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ueQ2bMJ2dEh0TKH9" /><figcaption>Content of the public Intermediate Certificate.</figcaption></figure><p>The following are the main points that are going to be achieved via the concept of Intermediate CA. Otherwise, it is enough to have Root CA and End/Entity certificate to build a trust chain.</p><ol><li><strong>Scalability</strong> — To handle a high number of certificate sign requests.</li><li><strong>Security and maintaining recovery ease over a security compromise</strong> — Using direct Root CA frequently may cause vulnerabilities and if there is a certificate revocation, it is quite difficult to handle if there is a scenario to revoke a Root CA directly. It will be easier to revoke an Intermediate CA than a Root CA.</li><li><strong>Trust Control</strong> — Based on the issuer there are some mechanisms to do access delegations for a system.</li><li><strong>Possibility for Customizations</strong> — There can be situations to customize the issue policies, certificate extensions etc. based on the certficate user. Direct customizations on the Root CA may affect the larger audience and customization may not be good practice for the Root CAs. So instead of the Root CA, it is possible to use an Intermediate CA to achieve the mentioned requirements.</li></ol><p><strong>Entity/End-user certificate.</strong></p><p>This is the certificate which is relevant to a server or a website and it will be used for the TLS communication to execute asymmetrical data encryption etc. Please refer to the following sample of the ‘Entity/End-user’ certificate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ujdd3TyHIhmMbm9V" /></figure><p>Up to now, we have discussed the importance of the certificate trust chain and the components of a certificate chain.</p><p>Now let’s discuss the steps of certificate chain trust validation from a browser perspective.</p><p>When trying to access ‘www.google.com’ via a browser under the TLS, as the response for the client hello from the browser, the Google server will return a certificate chain to the browser with server hello.</p><p>In this certificate chain, the 0th certificate is relevant to the lowest level of the certificate chain hierarchy(end/entity certificate) and the rest of the certificates(intermediate certificates/root certificates) are numbered accordingly by honouring the hierarchy of the certificate chain.</p><p>For a certificate chain with 3 certificates the 0th certificate will be the end/entity certificate, the 1st certificate will be the intermediate certificate and the 2nd certificate will be the root certificate.</p><p>The certificate chain can be observed using the following command and in the certificate chain, the pointed order can be observed.</p><pre>openssl s_client -showcerts -connect www.google.com:443 -servername www.google.com</pre><p>Following is a sample output and note that I have made some modifications to the output to reduce the size of the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/0*puXHnljtTYHipvbP" /><figcaption>Certificate chain of ‘www.google.com’</figcaption></figure><p>Based on this response the browser will validate the certificates referring to the trust store of the browser and validate whether the connection is going to be established towards the desired endpoint.</p><p>In this validation, the signature of each certificate will be validated using the public key of the next immediate higher certificate of the chain which is used to sign the current certificate.</p><p>This will be done based on the parameters issuer and the owner of the certificate.</p><p>Let’s assume that an endpoint only sends its end/entity certificate without sending the certificate chain.</p><p>In this case, the browser will try to validate whether the browser trusts the immediate certificate of the certificate chain(in the browser’s trust store). If it is trusted, the browser will establish the connection with the corresponding server.</p><p>I was able to develop a simple tool[2] which can used to do a certificate chain validation by feeding a trust store(JKS file) and a certificate chain in a file. This certificate chain file is to simulate the certificate chain response from a particular endpoint(website/server). Once you build the tool with Maven, an executable jar file will be generated. This Jar file can be executed using the following command.</p><pre>java -jar &lt;path_to_jar_file&gt;/cert-validator-1.0-SNAPSHOT-jar-with-dependencies.jar</pre><p>You can use this tool to experience the certificate chain validation.</p><p>Let’s try to create a certificate chain(self-signed) using OpenSSL commands and simulate the chain validation.</p><p>Step 1 — Generate certificate chain.</p><p>Following are the sample commands that can be used to create a certificate chain with a root certificate, one intermediate certificate and one end/entity certificate.</p><pre>Generate Root certificate.<br><br>openssl genpkey -algorithm RSA -out root-ca-private-key.pem -aes256<br>openssl req -new -x509 -key root-ca-private-key.pem -out root-ca-cert.pem -days 3650 -config openssl.conf<br><br>Generate intermediate certificate, using root certificate.<br><br>openssl genpkey -algorithm RSA -out intermediate-ca-private-key.pem -aes256<br>openssl req -new -key intermediate-ca-private-key.pem -out intermediate-ca-csr.pem -config openssl.conf<br>openssl x509 -req -in intermediate-ca-csr.pem -CA root-ca-cert.pem -CAkey root-ca-private-key.pem -CAcreateserial -out intermediate-ca-cert.pem -days 365 -extfile openssl.conf -extensions v3_ca<br><br>Generate end/entity certificate, using intermediate certificate.<br><br>openssl genpkey -algorithm RSA -out end-entity-private-key.pem -aes256<br>openssl req -new -key end-entity-private-key.pem -out end-entity-csr.pem -config openssl.conf<br>openssl x509 -req -in end-entity-csr.pem -CA intermediate-ca-cert.pem -CAkey intermediate-ca-private-key.pem -CAcreateserial -out end-entity-cert.pem -days 365 -extfile openssl.conf -extensions v3_ca</pre><p>Here it is required to point to the below configurations to generate a version-3 certificate with the basic constraint ‘CA: true’. These properties should be included in the certificates because these properties are needed when validating the certificate trust chain via the tool[2](for the 3rd party libraries of the client).</p><pre>[ req ]<br>default_bits        = 2048<br>default_keyfile     = privkey.pem<br>default_md          = sha256<br>distinguished_name  = req_distinguished_name<br>x509_extensions     = v3_ca<br><br>[ req_distinguished_name ]<br>countryName                         = Country Name (2 letter code)<br>countryName_default                 = SL<br>stateOrProvinceName                 = State or Province Name (full name)<br>stateOrProvinceName_default         = Southern Province<br>localityName                        = Locality Name (eg, city)<br>localityName_default                = Galle<br>organizationName                    = Organization Name (eg, company)<br>organizationName_default            = com.jcmw<br>organizationalUnitName              = Organizational Unit Name (eg, department)<br>organizationalUnitName_default      = jcmw-department<br>commonName                          = Common Name (eg, fully qualified host name)<br>commonName_default                  = localhost<br><br>[ v3_ca ]<br>basicConstraints = CA:TRUE<br>keyUsage = cRLSign, keyCertSign</pre><blockquote>Sample content of a certificate after generating with the above OpenSSL configurations.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nhOQ2zyIg2ZguoZTJ4twYg.png" /><figcaption>Version-3 self-signed certificate.</figcaption></figure><p>Otherwise, the certificate chain validation will be rejected with the following error.</p><pre>java.security.cert.CertPathValidatorException: basic constraints check failed: this is not a CA certificate</pre><p>Corresponding validations can be found in the method[3] which is used by the tool[2] as an underlying library.</p><p>Step 2 — Create a trust store with generated certificates.</p><p>After generating the certificates with the suggested commands in Step 1, add the certificate to a trust store JKS file as trusted entries using suitable aliases. For this following command can be used.</p><pre>keytool -import -file &lt;certificate_name&gt;-cert.pem  -keystore ./&lt;path_to_keystore&gt;/&lt;name_of_keystore&gt;.jks -alias &lt;alias&gt;</pre><p>Step 3 — Create a certificate file to simulate the certificate chain response similar to an endpoint.</p><p>Next, we have to create a file with certificates so that we can simulate the certificate chain which is coming from an endpoint(website or server). For this, a text editor can be used and it is required to keep the certificate order within the file. So the topmost entry should be relevant to the end/entity certificate and the bottommost should be the root certificate.</p><p>As an example, open ‘end-entity-cert.pem’ using a text editor and the following will be the sample content of the certificate file.</p><pre>-----BEGIN CERTIFICATE-----<br>MIIDkjCCAnqgAwIBAgIJAIoYXsWFrQKnMA0GCSqGSIb3DQEBCwUAMHwxCzAJBgNV<br>BAYTAlNMMRowGAYDVQQIDBFTb3V0aGVybiBQcm92aW5jZTEOMAwGA1UEBwwFR2Fs<br>bGUxETAPBgNVBAoMCGNvbS5qY213MRowGAYDVQQLDBFqY213LWludGVybWVkaWF0<br>ZTESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIzMTAwOTE1NTUzMFoXDTI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content to the target file which is going to be used to simulate the certificate chain. Once you add all 3 certificates, the final view of the file will be similar to the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/0*18YOu44Rnv-mkjsQ" /><figcaption>Combined certificate file with root, intermediate and end/entity certificate.</figcaption></figure><p>Now you have created a trust store file with trusted entries and a certificate file to simulate the certificate chain which is coming from an endpoint.</p><p>Step 3 — Possible steps to experiment with the certificate chain trust validation.</p><p>Experience the following cases for a better understanding of the trust chain.</p><ol><li><strong>Case 1</strong> — The trust store contains all three certs and the certificate file contains all three certs.<br>In this case, you will observe the successful trust chain validation.</li><li><strong>Case 2</strong> — The trust store contains all three certs and the certificate file contains only the end/entity cert and the intermediate cert.<br>In this case, it is possible to observe the success of trust chain validation.</li><li><strong>Case 3</strong> — The trust store contains only the root cert, and the certificate file contains only the end/entity cert and the intermediate cert.<br>In this case, it is possible to observe the success of trust chain validation because the issuer of the intermediate cert(the root cert) is trusted by the trust store.</li><li><strong>Case 4</strong> — The trust store contains only the root cert, and the certificate file contains only the end/entity cert.<br>In this case, the trust chain validation will fail because the issuer of the end/entity certificate(intermediate certificate) is not trusted by the trust store. Even if the root certificate is trusted, the trust chain cannot be built due to the unavailability of the intermediate cert either in the keystore or in the certificate file.</li><li><strong>Case 5</strong> — The trust store contains the root cert and intermediate cert. The certificate file contains only the end/entity cert.<br>In this case, the trust chain will be validated because the issuer of the end/entity cert is trusted by the Keystore since it contains the intermediate certificate.</li></ol><p><strong>Summery.</strong></p><p>Certificates are needed for secure connections and certificate chains are useful when managing the trusted entities. Otherwise when using a browser or similar the user may have to manage the trusted websites manually even for the well-known websites. Now the user only has to manage the trust of the websites, which are on Self-signed certs or use certificates not from a valid root CA.</p><p>[1] <a href="https://www.checktls.com/showcas.html">https://www.checktls.com/showcas.html</a></p><p>[2] <a href="https://github.com/janithcmw/mini-tools/tree/main/cert-validator">https://github.com/janithcmw/mini-tools/tree/main/cert-validator</a></p><p>[3] <a href="https://github.com/openjdk/jdk/blob/e882718a6026d4d8dcc07da19ed1fa685ac49f17/src/java.base/share/classes/sun/security/provider/certpath/ConstraintsChecker.java#L222">https://github.com/openjdk/jdk/blob/e882718a6026d4d8dcc07da19ed1fa685ac49f17/src/java.base/share/classes/sun/security/provider/certpath/ConstraintsChecker.java#L222</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9af96e33579f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>