<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Posts on /home/martijn</title><link>https://www.martijnvandijk.net/posts/</link><description>Recent content in Posts on /home/martijn</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 10 Nov 2023 08:52:25 +0100</lastBuildDate><atom:link href="https://www.martijnvandijk.net/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>ASN.1 Decoder</title><link>https://www.martijnvandijk.net/posts/asn1-decoder/</link><pubDate>Fri, 10 Nov 2023 08:52:25 +0100</pubDate><guid>https://www.martijnvandijk.net/posts/asn1-decoder/</guid><description>Lapo Luchini created a useful decoder for ASN.1. For me it&amp;rsquo;s useful for doing in-depth inspection of X.509 certificates and signing requests.
https://lapo.it/asn1js/</description><content:encoded><![CDATA[<p><a href="https://lapo.it">Lapo Luchini</a> created a useful decoder for ASN.1. For me it&rsquo;s useful for doing in-depth inspection of X.509 certificates and signing requests.</p>
<p><a href="https://lapo.it/asn1js/">https://lapo.it/asn1js/</a></p>
]]></content:encoded></item><item><title>TLS Illustrated</title><link>https://www.martijnvandijk.net/posts/tls-illustrated/</link><pubDate>Fri, 10 Nov 2023 08:42:53 +0100</pubDate><guid>https://www.martijnvandijk.net/posts/tls-illustrated/</guid><description>Michael Driscoll created a very nice illustrated guide on how the TLS 1.2 and TLS 1.3 handshakes work, including how the various cryptographic primitives are used to secure the connection.
TLS 1.3: https://tls13.xargs.org/
TLS 1.2: https://tls12.xargs.org/
Bonus: An animated explanation on how elliptic curve crypto works: https://curves.xargs.org/</description><content:encoded><![CDATA[<p><a href="https://xargs.org/">Michael Driscoll </a> created a very nice illustrated guide on how the TLS 1.2 and TLS 1.3 handshakes work, including how the various cryptographic primitives are used to secure the connection.</p>
<p><strong>TLS 1.3:</strong> <a href="https://tls13.xargs.org/">https://tls13.xargs.org/</a></p>
<p><strong>TLS 1.2:</strong> <a href="https://tls12.xargs.org/">https://tls12.xargs.org/</a></p>
<p>Bonus: An animated explanation on how elliptic curve crypto works: <a href="https://curves.xargs.org/">https://curves.xargs.org/</a></p>
]]></content:encoded></item><item><title>Writing a basic Flask webapp</title><link>https://www.martijnvandijk.net/posts/20221125-ohwauw/</link><pubDate>Fri, 25 Nov 2022 17:00:00 +0100</pubDate><guid>https://www.martijnvandijk.net/posts/20221125-ohwauw/</guid><description>The Idea 💡 The app is a simple &amp;ldquo;quote generator&amp;rdquo;, similar to fortune. It will display a different line of text every time you load the page. I wanted to build this for displaying some quotes and catchphrases from my colleagues. From the initial idea to the first prototype took me about 1 hour.
First, make it work The app in the most basic form consists of two python files: app.</description><content:encoded><![CDATA[<h1 id="the-idea-">The Idea 💡</h1>
<p>The app is a simple &ldquo;quote generator&rdquo;, similar to fortune. It will display a different line of text every time you load the page. I wanted to build this for displaying some quotes and catchphrases from my colleagues. From the initial idea to the first prototype took me about 1 hour.</p>
<h1 id="first-make-it-work">First, make it work</h1>
<p>The app in the most basic form consists of two python files: <code>app.py</code> and <code>lines.py</code>.</p>
<p><code>lines.py</code> provides an array of strings:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>lines <span style="color:#f92672">=</span> [
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;They did not do the needful.&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;børk børk børk!&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;lapopm&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;User could not open cryptic email.&#34;</span>
</span></span><span style="display:flex;"><span>]
</span></span></code></pre></div><p><code>app.py</code> contains the Flask app that makes things work. This is the most basic form that will only output a single string without formatting.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> flask <span style="color:#f92672">import</span> Flask
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> lines <span style="color:#f92672">import</span> lines
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> random
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>app <span style="color:#f92672">=</span> Flask(__name__)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@app.route</span>(<span style="color:#e6db74">&#34;/&#34;</span>, methods <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;GET&#34;</span>])
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ohwauw</span>():
</span></span><span style="display:flex;"><span>    wauw <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>choice(lines)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> wauw
</span></span></code></pre></div><h1 id="then-make-it-pretty-">Then, make it pretty 🦋</h1>
<p>The code above results in a rather bland page. Let&rsquo;s use Flask&rsquo;s template engine to add some formatting and css.</p>
<p>Here is our template, Flask expects it in <code>templates\template_name.html</code> relative to the app&rsquo;s root directory.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-html" data-lang="html"><span style="display:flex;"><span><span style="color:#75715e">&lt;!DOCTYPE html&gt;</span>
</span></span><span style="display:flex;"><span>&lt;<span style="color:#f92672">html</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">head</span>&gt;
</span></span><span style="display:flex;"><span>        &lt;<span style="color:#f92672">title</span>&gt;Oh Wauw! (tm)&lt;/<span style="color:#f92672">title</span>&gt;
</span></span><span style="display:flex;"><span>        &lt;<span style="color:#f92672">style</span>&gt;
</span></span><span style="display:flex;"><span>            .<span style="color:#a6e22e">wauw</span>{
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">width</span>: <span style="color:#ae81ff">50</span><span style="color:#66d9ef">%</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">text-align</span>: <span style="color:#66d9ef">center</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">font-family</span>:<span style="color:#e6db74">&#39;Segoe UI&#39;</span>, Tahoma, Geneva, Verdana, <span style="color:#66d9ef">sans-serif</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">margin</span>: <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">position</span>: <span style="color:#66d9ef">absolute</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">top</span>: <span style="color:#ae81ff">50</span><span style="color:#66d9ef">%</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">left</span>: <span style="color:#ae81ff">50</span><span style="color:#66d9ef">%</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">transform</span>: translate(<span style="color:#ae81ff">-50</span><span style="color:#66d9ef">%</span>, <span style="color:#ae81ff">-50</span><span style="color:#66d9ef">%</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        &lt;/<span style="color:#f92672">style</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;/<span style="color:#f92672">head</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">body</span>&gt;
</span></span><span style="display:flex;"><span>        &lt;<span style="color:#f92672">span</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;wauw&#34;</span>&gt;&lt;<span style="color:#f92672">h1</span>&gt;{{wauw}}&lt;/<span style="color:#f92672">h1</span>&gt;&lt;/<span style="color:#f92672">span</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;/<span style="color:#f92672">body</span>&gt;
</span></span><span style="display:flex;"><span>&lt;/<span style="color:#f92672">html</span>&gt;
</span></span></code></pre></div><p>Now, we can pass the <code>{{wauw}}</code> parameter to the page:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#a6e22e">@app.route</span>(<span style="color:#e6db74">&#34;/&#34;</span>, methods <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;GET&#34;</span>])
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ohwauw</span>():
</span></span><span style="display:flex;"><span>    wauw <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>choice(lines)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> render_template(<span style="color:#e6db74">&#39;homepage.html&#39;</span>, wauw<span style="color:#f92672">=</span>wauw)
</span></span></code></pre></div>]]></content:encoded></item><item><title>Certreq command snippets for AD CS</title><link>https://www.martijnvandijk.net/posts/adcs-certreq-snippets/</link><pubDate>Mon, 14 Nov 2022 13:55:00 +0100</pubDate><guid>https://www.martijnvandijk.net/posts/adcs-certreq-snippets/</guid><description>This is a brief overview of how to issue certificates with certreq commands. It might be of use for someone administrating an ADCS instance.
Issuing a leaf certificate This will create a certificate of the CertTemplateName template.
certreq -submit -attrib &amp;#34;CertificateTemplate:CertTemplateName&amp;#34; .\signing_request.csr Issuing an issuing CA template This assumes that the root CA is not running in enterprise CA mode. This command will create an issuing CA certificate valid for 1 (one) year.</description><content:encoded><![CDATA[<p>This is a brief overview of how to issue certificates with certreq commands. It might be of use for someone administrating an ADCS instance.</p>
<h1 id="issuing-a-leaf-certificate">Issuing a leaf certificate</h1>
<p>This will create a certificate of the CertTemplateName template.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-powershell" data-lang="powershell"><span style="display:flex;"><span>certreq -submit -attrib <span style="color:#e6db74">&#34;CertificateTemplate:CertTemplateName&#34;</span> .\signing_request.csr
</span></span></code></pre></div><h1 id="issuing-an-issuing-ca-template">Issuing an issuing CA template</h1>
<p>This assumes that the root CA is not running in enterprise CA mode. This command will create an issuing CA certificate valid for 1 (one) year.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-powershell" data-lang="powershell"><span style="display:flex;"><span>certreq -attrib <span style="color:#e6db74">&#34;CertificateTemplate:SubCA&#34;</span> -attrib <span style="color:#e6db74">&#34;ValidityPeriod:1&#34;</span> -attrib<span style="color:#e6db74">&#34;ValidityPeriodUnits:Years&#34;</span> .\SSL_CERT_R.csr
</span></span></code></pre></div><p>The <code>certreq</code> tool will then output a RequestId. Look this up in <code>certsrv.msc</code>, approve it and export the certificate.</p>
]]></content:encoded></item><item><title>The First Line's Prayer</title><link>https://www.martijnvandijk.net/posts/the-first-lines-prayer/</link><pubDate>Thu, 14 Apr 2022 13:01:15 +0200</pubDate><guid>https://www.martijnvandijk.net/posts/the-first-lines-prayer/</guid><description>The ever-repeating prayer of end-user support</description><content:encoded><![CDATA[<p>The ever-repeating prayer of end-user support goes a bit like this:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-txt" data-lang="txt"><span style="display:flex;"><span>Let the reboot fix the issue
</span></span><span style="display:flex;"><span>May the GPupdate bless our systems
</span></span><span style="display:flex;"><span>Hope for the Reinstall to resolve our problems
</span></span><span style="display:flex;"><span>3rd line support, save our souls!
</span></span></code></pre></div>]]></content:encoded></item><item><title>Encryption Adventures in SQL Server</title><link>https://www.martijnvandijk.net/posts/sqlserver-cryptographic-message/</link><pubDate>Thu, 14 Apr 2022 11:08:07 +0200</pubDate><guid>https://www.martijnvandijk.net/posts/sqlserver-cryptographic-message/</guid><description>When calling EncryptByKey() in SQL server you will get a bunch of bytes in return. This article from Microsoft describes the structure of these bytes.
If you pay close attention, it is even possible to work with this format outside of SQL server. Both decrypting and encrypting is possible, given that you can get the encryption key available outside of SQL Server. Using a HSM with the EKM integration is one way to do this, as SQL server has no native integration for exporting and importing keys.</description><content:encoded><![CDATA[<p>When calling <code>EncryptByKey()</code> in SQL server you will get a bunch of bytes in return. <a href="https://techcommunity.microsoft.com/t5/sql-server-blog/sql-server-encryptbykey-cryptographic-message-description/ba-p/383541">This article</a> from Microsoft describes the structure of these bytes.</p>
<p>If you pay close attention, it is even possible to work with this format outside of SQL server. Both decrypting and encrypting is possible, given that you can get the encryption key available outside of SQL Server. Using a HSM with the EKM integration is one way to do this, as SQL server has no native integration for exporting and importing keys.</p>
<h1 id="cryptographic-message">Cryptographic message</h1>
<p>Long story short:</p>
<ul>
<li>16 bytes: key GUID</li>
<li>x bytes: header</li>
<li>x bytes: IV</li>
<li>n bytes: ciphertext</li>
</ul>
<h1 id="creating-the-ciphertext">Creating the ciphertext</h1>
<p>The plaintext must be prepended with the following bytes:</p>
<ul>
<li>Magic number:  <code>0x0DF0ADBA</code>. Decryption will fail if the plaintext does not start with this number.</li>
<li>Integrity bytes length: <code>0x0000</code> because we don&rsquo;t use authentication in this example</li>
<li>Plaintext Length: litle endian 16-bit integer. 12 bytes of plaintext will give <code>0x0C00</code></li>
<li>IntegrityBytes: nothing, we don&rsquo;t use this in this example</li>
</ul>
<p>The header will have these bytes: <code>0x0DF0ADBA00000C00</code>. The plaintext bytes follow immediately after it.</p>
<p>After these bytes, the padded plaintext follows. PKCS#5 padding seems to work just fine. The plaintext with header bytes is then fed into the AES-CBC cipher.</p>
<h1 id="references">References</h1>
<ul>
<li><a href="https://techcommunity.microsoft.com/t5/sql-server-blog/sql-server-encryptbykey-cryptographic-message-description/ba-p/383541">https://techcommunity.microsoft.com/t5/sql-server-blog/sql-server-encryptbykey-cryptographic-message-description/ba-p/383541</a></li>
<li><a href="https://stackoverflow.com/questions/63666755/converting-uniqueidentifier-to-hex-string">https://stackoverflow.com/questions/63666755/converting-uniqueidentifier-to-hex-string</a></li>
</ul>
]]></content:encoded></item></channel></rss>