<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://nagrubb.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://nagrubb.github.io/" rel="alternate" type="text/html" /><updated>2022-04-29T07:11:23+00:00</updated><id>https://nagrubb.github.io/feed.xml</id><title type="html">Nathan Grubb</title><subtitle>Software Engineer</subtitle><entry><title type="html">Creating an easy to update personal website</title><link href="https://nagrubb.github.io/personal-web-setup/" rel="alternate" type="text/html" title="Creating an easy to update personal website" /><published>2019-12-10T00:00:00+00:00</published><updated>2019-12-10T00:00:00+00:00</updated><id>https://nagrubb.github.io/personal-web-setup</id><content type="html" xml:base="https://nagrubb.github.io/personal-web-setup/">&lt;p&gt;This post will go over how to setup a personal website that’s really easy to update
and see your changes. The end goal here is being able to make a change on a GitHub
repository that’s automatically reflected live on a &lt;a href=&quot;staging.nathangrubb.io&quot;&gt;test/staging page&lt;/a&gt;, then if that
looks good being able to merge that to the &lt;a href=&quot;nathangrubb.io&quot;&gt;real page&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;why-do-this&quot;&gt;Why do this?&lt;/h2&gt;

&lt;p&gt;Initially I just setup a simple resume type website. It was primarily just for fun so I just spun up an EC2
machine, installed nginx and pointed it to a static page that was configured on that box. When I wanted to make
changes, I’d have to SSH to that box and manually make the changes and then view them live. Obviously this isn’t
good practice for a production website, but the risk for making a bad change wasn’t that serious since it’s not
like my page is actually used for anything productive, after all it was just fo fun.&lt;/p&gt;

&lt;p&gt;However, over time this became kind of a hassle as I wanted to make little tweaks. I’d have to remember where
I put that SSH key, remember what the username was for that box and then remember the path to where I put the
static content. What started as fun, quickly just became annoying and I also wanted to host similar type pages
for my wife and also a friend so the nginx configuration grew in complexity. In my head, I thought wouldn’t it
be nice if I could just modify something on git and see it live?&lt;/p&gt;

&lt;h2 id=&quot;step-1-containerize-your-website&quot;&gt;Step 1: Containerize your website&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;I will write another article on how to do this, but the just of this is when you are done with this step
you should be able to easily host your website locally with two commands:
    &lt;ul&gt;
      &lt;li&gt;Build your container: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker build -t website .&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Run your container: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -t website -p 8080:80&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Visit http://localhost:8080 in your browser and this works then you have finished this step.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-2-publish-on-github&quot;&gt;Step 2: Publish on GitHub&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Put everything on from Step 1 in a Git repository and push to GitHub.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-3-register-with-docker-hub&quot;&gt;Step 3: Register with Docker Hub&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Create a free account on https://hub.docker.com.&lt;/li&gt;
  &lt;li&gt;Create a repository on Docker Hub.&lt;/li&gt;
  &lt;li&gt;Under Build Settings, connect your GitHub account and chose the repository your made from Step 2.&lt;/li&gt;
  &lt;li&gt;Create two build rules both based on branches. One for “master” and another for “staging”. You can make the Source and Dokcer Tag the same.&lt;/li&gt;
  &lt;li&gt;Now every time you make changes to either of those branches in GitHub, DockerHub will attempt to build a new container. It will take a while for Docker Hub to do this, but when it’s done you should be able to run something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -p 8080:80 &amp;lt;docker_account_name&amp;gt;/&amp;lt;docker_repository_name&amp;gt;:&amp;lt;tag&amp;gt;&lt;/code&gt; (i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -p 8080:80 silentsnowman/resume-website:staging&lt;/code&gt;) locally and visit http://localhost:8080 to see your website.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-4-getting-a-domain-name&quot;&gt;Step 4: Getting a Domain Name&lt;/h2&gt;

&lt;p&gt;The very first thing you need for this is to buy a domain name. You only need to buy one domain as you
can use a sub domain for the staging environment. I purchased this through Route 53 on AWS.&lt;/p&gt;

&lt;h2 id=&quot;step-5-setting-up-a-web-server&quot;&gt;Step 5: Setting Up A Web Server&lt;/h2&gt;

&lt;p&gt;As you’ll later find out this is a little bit more than a web server, but rather more a docker container host.
For this setup, I decided on using AWS EC2 with the latest version of Ubuntu. At the end of this setup we will
have nginx running on the host machine which will proxy the request to either our production container or our
staging container.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a Ubuntu 18.04 EC2 Machine on AWS.&lt;/li&gt;
  &lt;li&gt;Run the following to install nginx, docker:
    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Install Nginx
sudo apt install nginx
# Install Docker
sudo apt install docker.io
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Configure nginx:
    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# TODO: Write this
# Reload nginx configuration
nginx -s reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Run two docker containers
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -d -p 8000:80 &amp;lt;docker_account_name&amp;gt;/&amp;lt;docker_repository_name&amp;gt;:&amp;lt;tag&amp;gt;&lt;/code&gt; (i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -p 8000:80 silentsnowman/resume-website:staging&lt;/code&gt;)&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -d -p 8000:80 &amp;lt;docker_account_name&amp;gt;/&amp;lt;docker_repository_name&amp;gt;:&amp;lt;tag&amp;gt;&lt;/code&gt; (i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -p 9000:80 silentsnowman/resume-website:production&lt;/code&gt;)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Create a watchtower container (this will keep your other two containers above up to date)
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;You probably want to consider telling Docker to start all these containers on boot or restart when they crash. See https://docs.docker.com/config/containers/start-containers-automatically/ for more details.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-4-configure-your-domain-name&quot;&gt;Step 4: Configure your Domain Name&lt;/h2&gt;

&lt;p&gt;Now that you have a server and a domain, you need to configure your domain to map to your server.
Since I purchased my domain through Route 53 on AWS, I also configured it there.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a “A” record with the name of your domain (i.e. nathangrubb.io) and with the value
of your EC2 machine’s IPv4 address (i.e. 3.80.251.117)&lt;/li&gt;
  &lt;li&gt;Create a “CNAME” record with the name of your domain with www. prepended (i.e. www.nathangrubb.io) and the
value without the www. prepended (i.e. nathangrubb.io). This will make your website work regardless of if
someone uses www. when visiting your site or chooses to exclude it.&lt;/li&gt;
  &lt;li&gt;Repeat 1 &amp;amp; 2 for a staging subdomain (i.e. staging.nathangrubb.io).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-5-add-security-with-certbot&quot;&gt;Step 5: Add Security with Certbot&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Run the following to install certbox:
    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Add Certbot PPA
sudo apt update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
# Install Certbox
sudo apt install certbot python-certbot-nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;certbot&lt;/code&gt; on command line and follow instructions to create certs for your main domain and staging subdomain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;step-6-visit-your-domain&quot;&gt;Step 6: Visit your domain!&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Type &lt;your_domain&gt; and/or staging.&lt;your_domain&gt; in a browser! (You can also go to your GitHub repository on your staging branch and make changes. When you commit them, it'll be about an hour before they go live to your staging domain though).&lt;/your_domain&gt;&lt;/your_domain&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">This post will go over how to setup a personal website that’s really easy to update and see your changes. The end goal here is being able to make a change on a GitHub repository that’s automatically reflected live on a test/staging page, then if that looks good being able to merge that to the real page.</summary></entry><entry><title type="html">ASN.1 - Common crypto encoding schemes</title><link href="https://nagrubb.github.io/asn1/" rel="alternate" type="text/html" title="ASN.1 - Common crypto encoding schemes" /><published>2018-06-12T00:00:00+00:00</published><updated>2018-06-12T00:00:00+00:00</updated><id>https://nagrubb.github.io/asn1</id><content type="html" xml:base="https://nagrubb.github.io/asn1/">&lt;p&gt;What is ASN.1 and how can I read something that’s encoded with “asn.1”?
I’m not going to go into too much detail as there’s other online resources that do this.
However, I will link to those resources that do a good job of explaining the details
of these standards. Also I’m not an expert, this is just what I have learnt so
far (some of which may be wrong).&lt;/p&gt;

&lt;h2 id=&quot;asn1-abstract-syntax-notation-number-one&quot;&gt;ASN.1 (Abstract Syntax Notation number One)&lt;/h2&gt;

&lt;p&gt;I wouldn’t get caught up too much on ASN.1, it’s just an &lt;a href=&quot;https://en.wikipedia.org/wiki/IDL_specification_language&quot;&gt;IDL&lt;/a&gt; 
and not an extremely useful thing to know unless you were defining some new key structure 
(which you probably shouldn’t be doing). As the name suggests this actually isn’t an encoding or
even a way to encode but an abstract syntax. The easiest way to relate to this is
&lt;a href=&quot;https://thrift.apache.org/&quot;&gt;Apache Thrift&lt;/a&gt; or &lt;a href=&quot;https://developers.google.com/protocol-buffers/&quot;&gt;Google Protocol Buffers&lt;/a&gt;.
These technologies themselves don’t offer anything concrete, but they are the frameworks 
on which to create new encodings and object types. Two common encoding types of ASN.1 for cryptography are DER and PEM.
There are various object types such as public keys, private keys, signatures, curve parameters, etc.
To learn more about ASN see &lt;a href=&quot;https://www.itu.int/en/ITU-T/asn1/Pages/introduction.aspx.&quot;&gt;Introduction to ASN.1​&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also I know you could use ASN.1 for other things outside of cryptography, but there
are IDLs with much better tooling and support like Apache Thrift or Google Protocol Buffers.
It’s sort of like saying you can still use &lt;a href=&quot;https://www.w3.org/TR/soap/&quot;&gt;SOAP&lt;/a&gt;, but
why would you when better things exist like &lt;a href=&quot;http://jsonapi.org/&quot;&gt;JSON API&lt;/a&gt;?&lt;/p&gt;

&lt;h2 id=&quot;der-distinguished-encoding-rules&quot;&gt;DER (Distinguished Encoding Rules)&lt;/h2&gt;

&lt;p&gt;ASN.1 defines a bunch of different encoding rules like BER, CER, PER, and DER. However,
by far the most common is DER. There’s a pretty good write up about DER on MSDN that
goes into detail about DER: &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/dd408078.aspx&quot;&gt;Distinguished Encoding Rules&lt;/a&gt;
so I won’t talk about it too much. However, it’s a pretty simple encoding
of Tag, Length, Value (or TLV for short). This is a pretty common encoding
scheme across computer science to describe nested data. Just off the top of
my head I can think of four other places this scheme is used: Apache Thrift,
Google Protocol Buffers, Bluetooth Service Discovery Protocol (SDP), and
Bluetooth Low Energy Advertisements. I’m sure there are many others as well.&lt;/p&gt;

&lt;p&gt;Again MSDN has some good write ups on DER’s TLV encoding scheme:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/bb540801.aspx&quot;&gt;TLV&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/bb648642.aspx&quot;&gt;Tag Encoding&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/bb648642.aspx&quot;&gt;Length and Value Encoding&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pem-privacy-enhanced-electronic-mail&quot;&gt;PEM (Privacy-enhanced Electronic Mail)&lt;/h2&gt;

&lt;p&gt;One thing I’ve noticed when learning about cryptography is that a lot of the standards
are based and designed around email. Since a lot of web standards rely on text encoding such
as JSON and XML, it makes sense to re-use email standards like PEM designed around text encoding. 
PEM is nothing more than base64 encoded DER with some extra metadata. The extra metadata is
literally a string at the start and end to describe the base64 encoded DER such as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-----BEGIN CERTIFICATE-----&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-----END CERTIFICATE-----&lt;/code&gt; for certificates.&lt;/p&gt;

&lt;h2 id=&quot;putting-it-all-together-and-playing-in-bash-on-macos&quot;&gt;Putting it all together and playing in bash (on macOS)&lt;/h2&gt;

&lt;p&gt;Now that I have gone over the basics, we can play around with some commands to solidify this
understanding.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First I created some handy bash commands in my ~/.bash_profile to
make what’s going on a little bit easier to read:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bash_profile&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;remove-first-and-last-line &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;1d&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;remove-line-endings &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;tr&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;n
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;hex-to-bin &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  xxd &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; -
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;bin-to-hex &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  hexdump &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/1 &quot;%02X&quot;'&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;#OR&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;#xxd -p | remove-line-endings&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;parse-der &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  openssl asn1parse &lt;span class=&quot;nt&quot;&gt;-inform&lt;/span&gt; der
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;parse-pem &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  openssl asn1parse &lt;span class=&quot;nt&quot;&gt;-inform&lt;/span&gt; pem
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Next, lets create a folder for playing around:
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~
&lt;span class=&quot;nb&quot;&gt;mkdir &lt;/span&gt;crypto-fun
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;crypto-fun
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Lets generate a private key using the prime256v1 curve.
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;openssl ecparam &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; prime256v1 &lt;span class=&quot;nt&quot;&gt;-genkey&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; key.pem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Now we have a elliptic curve private key in PEM format so lets see what it looks like in ASN.1:
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.pem | parse-pem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Given what I described above lets try to manually convert the PEM file to DER to make sure the output is the same:
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# First dump the file contents to stdout with cat key.pem&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Next remove the PEM metadata lines: -----BEGIN EC PRIVATE KEY----- and -----END EC PRIVATE KEY----- with remove-first-and-last-line&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Next remove al the line endings to get one full blob of base64 with remove-line-endings&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Next decode the base64 into binary with base64 --decode&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Finally save the output to key.der with &amp;gt; key.der&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.pem | remove-first-and-last-line | remove-line-endings | &lt;span class=&quot;nb&quot;&gt;base64&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--decode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; key.der
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.der | parse-der
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Lets even diff them just to make sure
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;diff &amp;lt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.pem | parse-pem&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &amp;lt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.der | parse-der&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;extra-information&quot;&gt;Extra Information&lt;/h2&gt;

&lt;p&gt;Note that the above experiment in bash was only to solidify an understanding of ASN.1, PEM and DER. There
are much better commands for parsing specific types of cryptographic objects. For instance, objects like
x509 Certificates often contain DER encoding inside of a DER encoding. Thus instead you should use specific 
commands for parsing those objects. Here’s a couple examples 
(I also save these as functions in my ``~/.bash_profile`):&lt;/p&gt;

&lt;h4 id=&quot;parsing-elliptic-private-keys-like-keypem-or-keyder-we-created-earlier&quot;&gt;Parsing elliptic private keys (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;key.pem&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;key.der&lt;/code&gt; we created earlier):&lt;/h4&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.pem | openssl ec &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-text&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Must specify the input type for OpenSSL as PEM is the default&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.der | openssl ec &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-text&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-inform&lt;/span&gt; der
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;parsing-elliptic-public-keys&quot;&gt;Parsing elliptic public keys&lt;/h4&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# First lets create the corresponding public key to our private&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;key.pem | openssl ec &lt;span class=&quot;nt&quot;&gt;-pubout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; pub.pem

&lt;span class=&quot;c&quot;&gt;# Parse the PEM public key&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;pub.pem | openssl ec &lt;span class=&quot;nt&quot;&gt;-pubin&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-text&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Now lets use openssl to convert the encoding&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;pub.pem | openssl ec &lt;span class=&quot;nt&quot;&gt;-pubin&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-pubout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; pub.der &lt;span class=&quot;nt&quot;&gt;-outform&lt;/span&gt; der

&lt;span class=&quot;c&quot;&gt;# Parse the DER public key&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;pub.der | openssl ec &lt;span class=&quot;nt&quot;&gt;-pubin&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-text&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-inform&lt;/span&gt; der
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There’s very similar commands for parsing all types of objects in openssl
basically with this same format. There are some exceptions like public keys where
you have to specify it’s a public key:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;file | openssl &amp;lt;object_type&amp;gt; &lt;span class=&quot;nt&quot;&gt;-noout&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-text&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-inform&lt;/span&gt; &amp;lt;der/pem&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately OpenSSL is not the smart to read the context given by PEM and automatically 
parse that given structure. However, a plus side to that is that it’s symmetrical for 
parsing PEM and DER files of the same object type.&lt;/p&gt;

&lt;h2 id=&quot;final-notes&quot;&gt;Final Notes&lt;/h2&gt;

&lt;p&gt;Hopefully you can parse any PEM or DER file with OpenSSL. You can even deconstruct it down to its rawest 
form in bytes and dig through the RFC specifications to map the values. I hope if you read this far that
this was informative and not a huge waste of time. If it was, well sorry.&lt;/p&gt;</content><author><name></name></author><summary type="html">What is ASN.1 and how can I read something that’s encoded with “asn.1”? I’m not going to go into too much detail as there’s other online resources that do this. However, I will link to those resources that do a good job of explaining the details of these standards. Also I’m not an expert, this is just what I have learnt so far (some of which may be wrong).</summary></entry><entry><title type="html">Mitigations with TLS</title><link href="https://nagrubb.github.io/tls-security/" rel="alternate" type="text/html" title="Mitigations with TLS" /><published>2018-01-14T00:00:00+00:00</published><updated>2018-01-14T00:00:00+00:00</updated><id>https://nagrubb.github.io/tls-security</id><content type="html" xml:base="https://nagrubb.github.io/tls-security/">&lt;p&gt;Transport Layer Security TLS) puts a lot of trust on the Certificate Authorities (CAs) and the Domain Name System (DNS). Thus you may want to mitigate some of the risks with TLS rather than just assumpting that because something supports TLS it is secure.&lt;/p&gt;

&lt;p&gt;TLS can be summarized into two parts: authenticity and privacy. Authenticity is given with two dependencies: one that there is a certificate authority that is trusted and two that you actually talking to a server that owns the domain name you’ve resolved. Privacy is after you’ve established your endpoint is established, you created an encrypted channel to prevent eavesdropping. This writting focuses on the authenticity aspect of TLS and why you may think about adding additional security ontop of it if you can.&lt;/p&gt;

&lt;p&gt;Lets dig into DNS a bit. DNS isn’t secure at all and wasn’t designed to be secure. All DNS looks like is finding the first entry for a given domain name (i.e. nathangrubb.io) to an IP address. DNS servers are often chained together so if I configure my DNS server as DNS Google’s Public DNS (8.8.8.8) and the entry isn’t found for nathangrubb.io at 8.8.8.8 chances are it’s chained to another server in which it can continue the search until it either resolves nathangrubb.io to an IP address or runs out of options. Thus DNS can be view as giant hash tables of domain names to IP addresses chained together. Note at no point is there any security to establish that when you do find an IP address that is indeed the server or endpoint owned by the owner of the nathangrubb.io domain. There is Domain Name System Security Extensions (DNSSEC), but it’s not widely implemented at least at the time of this writing.&lt;/p&gt;

&lt;p&gt;Alright so with this knowledge that traditional DNS has no security, before we talk about Certificate Authorities (CAs) we need to briefly talk about asymmetric cryptography. Asymmetric cryptography is the idea that is very easy to do a mathematical operation one way that’s very difficult to do in reverse. I don’t want to dig into it too much but for the sake of this writting lets just simplify it so say asymmetric cryptography is the idea that I can do something with a secret (private key) that can be verified with something that is public knowledge (public key). This is known as cryptographic signing or signatures.&lt;/p&gt;

&lt;p&gt;Now we can dig into what a Certificate Authority is and how they contribute to TLS (FYI these aren’t specific details, this is just very high level explanation). Most browsers and operating systems come installed with a certificate store. This certificate store is all the root certificates that the browser and/or operating system trusts. A certificate is nothing more than a signed public key and some other metadata such as the signer/issuer, common name, expiration, and zero or more domain names. There’s some other metadata in here like what the certificate can be used for and algorithms, but this is the jist. A root certificate means that it is self-signed (i.e. it was signed by the private key corresponding to the public key that’s in it). These root certificates are each a representation of a Certificate Authority or “trustable entity”.&lt;/p&gt;

&lt;p&gt;Going back to TLS, after I’ve resolved a domain name to an IP address, the next step in this is to ask for a certificate at the endpoint for that endpoint. The assumption here is that the owner of the domain name has a private key on their server that corresponds to the public key in this certificate. This certificate will have an issuer and we can see if that issuer is in our certificate store. If it is, then we can take the public key from that certificate in the certificate store and verify the signature of the certificate we downloaded from the endpoint. Next we can verify the domain name in the certificate is the one we resolved to an IP address. The next step in this is issueing a random piece of data to the endpoint so they can sign it with the private key they have on the server. This ensures that I know I’m talking to the right endpoint and not just some random server returning the public certificate (after all the public certificate is just public information).&lt;/p&gt;

&lt;p&gt;There’s a pretty big hole here. What if a certificate authority doesn’t do all the checks when it issues a certificate to the “owner” of a domain or any of the Certificate Authorities is compromised. There’s a lot of trust put on the certificate authority to only sign certificates that are “valid” and keep their private keys well protected (ideally offline). The criteria for determining if you own a domain name isn’t standard and different CAs have different protocols for doing this. Thus say I know a certificate authority isn’t that great and I can get them to sign a certificate saying I own “google.com”. Now all I have to do is handle requests as if I’m a public DNS like 8.8.8.8 and give out my IP address for my google.com server which has the private key to the signed certificate which I shouldn’t have. Thus I’ve hacked a TLS endpoint so you may think you are talking to google.com and it shows secure in your browser, but you’re actually talking to my endpoint. I may even feed the requests to google and act as a man in the middle so you don’t even notice that I’m there. Except maybe I save your username and password when you login ;0.&lt;/p&gt;

&lt;h3 id=&quot;with-this-all-in-mind-what-are-some-of-the-mitigations-i-can-implement-if-i-want-to-develop-a-secure-ecosystem&quot;&gt;With this all in mind, what are some of the mitigations I can implement if I want to develop a secure ecosystem?&lt;/h3&gt;

&lt;p&gt;If you control just the client side of things, you can limit the certificate store to just the certificate authorities you rely on. However this can be pretty dangerous if you just control the client side and don’t have any backend system to update that certificate store. For instance, say I’m developing a client that relies on Facebook.com and I limit my certificate store to “DigiCert High Assurance EV Root CA” on that client I deploy. If Facebook decides they want to switch CAs, the client you’ve deployed out in the field won’t be able to talk to Facebook.com anymore.&lt;/p&gt;

&lt;p&gt;If you control the full ecosystem, this is where you really can safely mitigate against TLS. There’s a few ways you can do this such as the above point of limiting your certificate trust store and also implementing a system for renewing/refresing that certificate store. However to do this, you probably want to act as your own certificate authority to do so. Thus you could rely on TLS for most things, but you can provide additional security within your own ecosystem by taking advantage of a certificate chain you control. For instance, you could install your public certificate root on your clients and develop a system which clients can download a certificate chain that chains back to the public root installed on your clients and a new certificate trust store that’s signed with a certificate in that certificate chain.&lt;/p&gt;

&lt;p&gt;If you do decide to act as your own Certificate Authority, you should use a Hardware Security Module (HSM) for managing your root private key. The idea with an HSM is that the private key never leaves the hardware but can perform actions with the private key on the hardware. This provides a physical barrier and also audit trail when anyone accesses that root private key. Also you can keep that HSM completely offline and only use it to sign other certificates. In fact most Certificate Authorities do this and when they are fullfilling a signing request they are using an intermediate certificate’s private key.&lt;/p&gt;

&lt;h3 id=&quot;what-if-i-dont-control-the-full-ecosystem&quot;&gt;What if I don’t control the full ecosystem?&lt;/h3&gt;

&lt;p&gt;If you don’t control the full ecosystem, you can still mitigate some other attack vectors with TLS but only allowing strong ciphers and the newest version of TLS. For instance TLS supports many different encryption ciphers and algorithms for signing and you can have your client deny anything that’s not GCM-256 bit AES encryption or better and only trust signatures with prime256 Elliptic Curve signatures or better. This tends to all come down to what your dependencies are and what they support. Security is just about raising the minimum bar, you never have a completely secure system. Even if it was impossible to break from a technological point of view, there’s always social engineering :).&lt;/p&gt;

&lt;p&gt;Links:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.techrepublic.com/blog/it-security/compromised-certificate-authorities-how-to-protect-yourself&quot;&gt;Compromised Certificate Authorities&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://techgenix.com/DNS-Security-Part-1/&quot;&gt;DNS Security&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">Transport Layer Security TLS) puts a lot of trust on the Certificate Authorities (CAs) and the Domain Name System (DNS). Thus you may want to mitigate some of the risks with TLS rather than just assumpting that because something supports TLS it is secure.</summary></entry></feed>