<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>jks on Digi Hunch</title><link>https://static.digihunch.com/tag/jks/</link><description>Recent content in jks on Digi Hunch</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Sat, 20 Jul 2024 16:37:09 -0400</lastBuildDate><atom:link href="https://static.digihunch.com/tag/jks/index.xml" rel="self" type="application/rss+xml"/><item><title>Cryptography basics 2 of 2</title><link>https://static.digihunch.com/2019/09/cryptographic-concepts-for-busy-it-professionals-2-of-2/</link><pubDate>Sun, 08 Sep 2019 21:21:41 -0400</pubDate><guid>https://static.digihunch.com/2019/09/cryptographic-concepts-for-busy-it-professionals-2-of-2/</guid><description>&lt;p class="wp-block-paragraph"&gt;My previous &lt;a href="https://static.digihunch.com/2019/07/practical-cryptography-for-it-professional/"&gt;post&lt;/a&gt; outlines several core concepts around cryptography, such as asymmetric key encryption, digital certificate, the encoding formats and relevant file extensions. In this article, we continue to explore cryptography use cases, where these concepts are connected and put into application.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The most important use case is TLS handshake. I cannot stress enough how paramount this scenario is. This use case and its variation can be found in almost every situation where connection needs to be secured.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;TLS handshake&lt;/strong&gt;: the process in which client and server establish secure connection. During the handshake, two parties agree on TLS version, decide on cipher suite, authenticate the identity of each other (although client identity authentication is less common), and generate session key for symmetric encryption after the handshake. Details steps are very important. &lt;a href="https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.1.0/com.ibm.mq.doc/sy10660_.htm"&gt;Here&lt;/a&gt; is a fairly thorough reference, and here is a great diagram:&lt;/p&gt;&#10;&lt;figure class="wp-block-image"&gt;&lt;img loading="lazy" decoding="async" width="1496" height="792" src="https://static.digihunch.com/wp-content/uploads/2019/10/TLS.png" alt="" class="wp-image-152"/&gt;&lt;figcaption class="wp-element-caption"&gt;TLS handshake&lt;/figcaption&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Secure browser connection&lt;/strong&gt;: The most widespread use of TLS handshake is to secure browser connection with HTTPS:&lt;/p&gt;&#10;&lt;ol class="wp-block-list"&gt;&#10;&lt;li&gt;Browser initiates connection to the server;&lt;/li&gt;&#10;&lt;li&gt;Server sends browser its certificate (public key + digital signature signed by CA);&lt;/li&gt;&#10;&lt;li&gt;Browser has preloaded public key of CA and uses it to decrypt digital signature and get the digest of public key;&amp;nbsp;&lt;/li&gt;&#10;&lt;li&gt;Browser calculate digest of received public key and compares it against the digest from the previous step;&lt;/li&gt;&#10;&lt;li&gt;If the result is the same, public key is trusted, a green lock is displayed; otherwise, a warning is displayed;&lt;/li&gt;&#10;&lt;li&gt;proceed to the rest of the steps in TLS handshake.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Secure connection in Java&lt;/strong&gt;: Java applications manages keys and certificates through two classes: java.security.KeyStore and java.security.TrustStore. Suppose a Java application client initiates TLS connection to server. The server application will present its certificates from server&amp;#8217;s Key Store. The client will use certificates stored in client&amp;#8217;s Trust Store to verify the identity of the server. Once validated, the client then presents certificates stored in client&amp;#8217;s Key Store back to the server for validation.&lt;/p&gt;&#10;&lt;ul class="wp-block-list"&gt;&#10;&lt;li&gt;A KeyStore keeps keys and certificates for your own application. Typically, you store a KeyPair in a KeyStore file.&lt;/li&gt;&#10;&lt;li&gt;A TrustStore keeps the certificates of external systems that your application trusts.&amp;nbsp;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Below is a great diagram:&lt;/p&gt;&#10;&lt;figure class="wp-block-image"&gt;&lt;img loading="lazy" decoding="async" width="1362" height="956" src="https://static.digihunch.com/wp-content/uploads/2019/10/JavaJKS.png" alt="" class="wp-image-153"/&gt;&lt;figcaption class="wp-element-caption"&gt;Secure Java application in TLS&lt;/figcaption&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;JKS file &lt;/strong&gt;is Java&amp;#8217;s version of PKCS#12 (private key + certificate, password protected). Entries in a JKS file must have an &amp;#8220;alias&amp;#8221; that is unique. The JKS file type can be used for both Key Store and Trust Store. When it&amp;#8217;s used in Key Store, it contains a certificate and private key for the Java application. When it&amp;#8217;s used in Trust Store, it only contains certificate from external trusted applications. Note that JKS is the default keystore format until Java 8. Since Java 9 the default keystore format is PKCS12.&amp;nbsp;&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Finally, we can introduce some tools:&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Command line Tools&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;openssl &lt;/strong&gt;is a versatile tool for cryptography and keytool is a similar tool for Java applications.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;keytool &lt;/strong&gt;is for store keys/certificates in Java Key Store or Trust Store.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;I do not include any openssl or keytool command in this article in order to remain theoretical. However, once through the conceptual hurdles, one should become fairly comfortable picking up the tools and understand why each command is needed to achieve its purpose.&lt;/p&gt;&#10;&lt;nav class="wp-post-navigation" aria-label="Post navigation"&gt;&#10;&lt;a rel="prev" href="https://static.digihunch.com/2019/08/aws-certified-devops-engineer-exam-tips/"&gt;&lt;span class="wp-post-navigation-label"&gt;Previous Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;AWS Certified DevOps Engineer Exam Tips&lt;/strong&gt;&lt;/a&gt;&#10;&lt;a rel="next" href="https://static.digihunch.com/2019/10/personal-vim-cheatsheet/"&gt;&lt;span class="wp-post-navigation-label"&gt;Next Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;Personal Vim cheatsheet&lt;/strong&gt;&lt;/a&gt;&#10;&lt;/nav&gt;&#10;</description></item><item><title>Cryptography Basics 1 of 2</title><link>https://static.digihunch.com/2019/07/practical-cryptography-for-it-professional/</link><pubDate>Wed, 10 Jul 2019 20:07:18 -0400</pubDate><guid>https://static.digihunch.com/2019/07/practical-cryptography-for-it-professional/</guid><description>&lt;p class="wp-block-paragraph"&gt;I have been dabbling with OpenSSL commands to achieve what I needed during IT implementation, but I decided to spent some time to overcome the conceptual hurdles around cryptography. In this domain, following other people&amp;#8217;s instructions through the project does not produce much learning value when too many concepts cloud around. Let&amp;#8217;s take the bull by the horn.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;This article is purely conceptual. There are already lots of step-by-step guideline about acquiring a website certificate. The intention is to elucidate the core concepts on IT cryptography, and then connect the dots to form the big picture in cryptography.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;First, let&amp;#8217;s distinguish three basic concepts: encoding, hashing and encryption. They are in essence all mathematical functions, but one does not need to understand the underlying algorithm in order to understand what they are.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Encoding&lt;/strong&gt; &amp;#8211; Transform data into a format so it is readable by external system. Encoding is about interoperability. It is not about security whatsoever. Example: ASCII, BASE64, UNICODE.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Hashing&lt;/strong&gt; &amp;#8211; Mathematic algorithms to generate digest of content. A digest is usually fixed-size, non-reversible and deterministic. You cannot restore content from its digest (non-reversible, one-way calculation). Two different contents results guarantees different digests (deterministic). Digest as a result of hashing is mostly about data integrity. For example, in file download you can calculate MD5 hash and compare the result against the digest given by the source. Another common use, is to hash all password in database. In that sense, hashing has to do with security. Popular algorithms are MD5 and SHA-256.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Encryption&lt;/strong&gt; &amp;#8211; Mathematic algorithms that only succeeds if correct parameter (key) is provided. The function is deterministic and reversible (two-way). The parameter (key) used for calculation (encryption) and reverse calculation (decryption) can either be the same or different. &lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Cryptography involves all three concepts, but it mainly addresses issues around encryption. We break down encryption into two categories, symmetric key encryption (aka private key encryption) and asymmetric key encryption (aka public key encryption).&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Symmetric Key Encryption&lt;/strong&gt;: using the same key (shared secret) for encryption and decryption. Both parties need to keep it secret. Popular algorithm is AES.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Asymmetric Key Encryption&lt;/strong&gt;: involves a pair of public key and private key. Public key is given out to external systems. Private key is kept secret. Popular algorithm is RSA. You can run some experiments on &lt;a href="https://www.devglan.com/online-tools/rsa-encryption-decryption"&gt;this page&lt;/a&gt;, and I&amp;#8217;ve made some additional notes (all based on RSA algorithm):&lt;/p&gt;&#10;&lt;ol class="wp-block-list"&gt;&lt;li&gt;Public Key and Private Key are NOT interchangeable. The size of an RSA private key is usually much larger than its public key;&lt;/li&gt;&lt;li&gt;You can encrypt with either key, and decrypt with the other, so long as you specify the key type at the time of encryption or decryption;&lt;/li&gt;&lt;li&gt;You can generate public key from private key (ssh-keygen -y); but not the other way round;&lt;/li&gt;&lt;li&gt;Using a wrong key to encrypt or decrypt leads to failure, instead of wrong result.&lt;/li&gt;&lt;/ol&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Obviously symmetric key encryption is the original form of encryption and requires both parties to keep the key secret, permanently. This is not realistic in real life between organizations. This challenge leads to the adoption of asymmetric key where the public key can be published, for external party to encrypt outgoing messages, whereas the private key is kept secret within the owner, only to decrypt incoming messages. &lt;/p&gt;&#10;&lt;figure class="wp-block-image"&gt;&lt;img decoding="async" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Public_key_encryption.svg/1024px-Public_key_encryption.svg.png" alt=""/&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Knowing this distinction, we can introduce two concepts that are built on top of asymmetric key encryption.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Digital Signature&lt;/strong&gt;: if an entity signs a document digitally. The digital signature is the digest of the document (hash of the content) encrypted with the signer&amp;#8217;s private key.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Digital Certificate&lt;/strong&gt;: contains owner&amp;#8217;s public key and digital signature of issuer (digest of owner&amp;#8217;s public key encrypted by issuer&amp;#8217;s private key). Client (e.g. browser with CA&amp;#8217;s public key preloaded) should not trust the owner&amp;#8217;s public key until it compares its digest against decryption result of digital signature (using CA&amp;#8217;s public key).&amp;nbsp;&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The key difference here is that a digital certificate involves third party. Digital signature by itself cannot address impersonation, which is addressed by digital certificate issued by third party (certified authority). This requires digital certificate must follow some standard, and the most prevalent one is:&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;X.509&lt;/strong&gt;: a standard format for digital certificates. It contains:&lt;/p&gt;&#10;&lt;ul class="wp-block-list"&gt;&lt;li&gt;a public key&lt;/li&gt;&lt;li&gt;an identity (a hostname, or&amp;nbsp; an organization, or an individual)&lt;/li&gt;&lt;li&gt;a signature (either signed by CA or self-signed)&lt;/li&gt;&lt;/ul&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Since the advent of certificate, there are tons of global organizations that need to manage keys and certificates. When we create a secure connection now, we only need to get a certificate from an intermediate CA, thanks to existing &lt;a href="https://knowledge.digicert.com/solution/SO16297.html"&gt;certificate chain&lt;/a&gt;. It involves lots of work for large organizations to maintain keys and certificates, which requires:&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;Public Key Infrastructure (PKI)&lt;/strong&gt;: the IT infrastructure to create, manage, distribute, use, store and revoke digital certificates and public keys.&amp;nbsp;&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;One of the important technique to manage keys and certificates is the encoding. Keys and certificates are usually wrapped with different encoding formats based on what need to be done on them. Here is a summary of encoding formats&lt;/p&gt;&#10;&lt;figure class="wp-block-table aligncenter is-style-stripes"&gt;&lt;table class="has-background" style="background-color:#e7f5fe"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Format&lt;/td&gt;&lt;td&gt;Encoding&lt;/td&gt;&lt;td&gt;what is stored&lt;/td&gt;&lt;td&gt;Possible Suffix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PEM&lt;/td&gt;&lt;td&gt;DER file (binary content) encoded in Base64. Certificate files typically include clear text statement &amp;#8220;BEGIN CERTIFICATE&amp;#8221; and &amp;#8220;END CERTIFICATE&amp;#8221;&lt;/td&gt;&lt;td&gt;single certificate, certificate chains or private keys&lt;/td&gt;&lt;td&gt;.pem, .crt, .cer, .key&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DER&lt;/td&gt;&lt;td&gt;Binary format in early days; it&amp;#8217;s complex and don&amp;#8217;t use unless with a specific purpose.&lt;/td&gt;&lt;td&gt;single certificate, certificate chains, or private keys&lt;/td&gt;&lt;td&gt;.der, .cer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PKCS#7&lt;/td&gt;&lt;td&gt;Base64 encoded ASCII file, typically include clear text statement &amp;#8220;BEGIN PKCS7&amp;#8221; and &amp;#8220;END PKCS7&amp;#8221;&lt;/td&gt;&lt;td&gt;only certificates or certificate chains; no private keys&lt;/td&gt;&lt;td&gt;.p7b .p7s&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PKCS#8&lt;/td&gt;&lt;td&gt;Similar to PEM as base64 encoded format but for storing private key only, can be password protected&lt;/td&gt;&lt;td&gt;Private key&lt;/td&gt;&lt;td&gt;.key&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PKCS#12&lt;/td&gt;&lt;td&gt;a binary format, heavily used by Microsoft products&lt;/td&gt;&lt;td&gt;certificate, certificate chains or private keys; public private key pair&lt;/td&gt;&lt;td&gt;.pfx .p12&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OpenSSH&lt;/td&gt;&lt;td&gt;used by OpenSSH to store public keys (as specified in RFC4253)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;.pub&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Note: above are just encoding formats. Just by file extension you cannot tell whether the file is a key or a X.509 certificate. When you are configuring certificates, you may come across the following file extensions as well:&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;.key&lt;/strong&gt; this extension can indicate any kind of key, but usually it is a private key (used along with .crt file) &lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;.csr&lt;/strong&gt; certificate signing request, including a public key and an identity required by CA. CA needs this file to issue a certificate. CSR could be encoded in Base-64 or DER&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;.cer or .crt&lt;/strong&gt; a certificate, usually in X.509 v3 (public key + identity + signature), the encoding could be PEM or DER. &lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;&lt;strong&gt;.jks&lt;/strong&gt; -&amp;gt; java key store file type. It can be either a key store (private key along with certificate) or trust store (certificate) for Java application. Refer to the section for Java applications.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;In the &lt;a href="https://static.digihunch.com/2019/09/cryptographic-concepts-for-busy-it-professionals-2-of-2/"&gt;next article&lt;/a&gt;, we will examine some use case involving the concepts introduced in this post.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt; &lt;/p&gt;&#10;&lt;nav class="wp-post-navigation" aria-label="Post navigation"&gt;&#10;&lt;a rel="prev" href="https://static.digihunch.com/2019/06/git-explained-2-of-2/"&gt;&lt;span class="wp-post-navigation-label"&gt;Previous Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;Git Explained 2 of 2&lt;/strong&gt;&lt;/a&gt;&#10;&lt;a rel="next" href="https://static.digihunch.com/2019/07/storage-nitty-gritty-3-of-5-nas-and-object-storage/"&gt;&lt;span class="wp-post-navigation-label"&gt;Next Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;Storage Nitty-Gritty 3 of 5 – NAS and Object Storage&lt;/strong&gt;&lt;/a&gt;&#10;&lt;/nav&gt;&#10;</description></item></channel></rss>