title: How to extract public key from a JWKS using openssl library over shell
body: <p>I fetched JWKS from an endpoint and it looks something like this:</p>
  <pre><code>{
    &quot;keys&quot;: [
      {
        &quot;kty&quot;: &quot;RSA&quot;,
        &quot;use&quot;: &quot;sig&quot;,
        &quot;x5t&quot;: &quot;M2maFm3VYlMBOn3GetVWGXkrKrk&quot;,
        &quot;kid&quot;: &quot;SIGNING_KEY&quot;,
        &quot;x5c&quot;: &quot;MIIC………(base64 encoded cert)………..tow==&quot;,
        &quot;alg&quot;: &quot;RS256&quot;
      }
    ]
  }
  </code></pre>
  <p>I am trying to convert this x5c value into public key (<code>.pub</code>) file using shell and <code>openssl</code></p>
  <p>I have tried copy pasting the <code>x5c</code> value from the above json and added to a <code>.pem</code> file by doing:</p>
  <pre><code>vi certificate.pem
  fold -w 64 certificate.pem
  </code></pre>
  <p>and then adding the following in the <code>certificate.pem</code> file too</p>
  <pre><code>-----BEGIN CERTIFICATE-----
  &lt;value&gt;
  -----END CERTIFICATE-----
  </code></pre>
  <p>After this I tried running the following to get the public key:</p>
  <p><code>openssl x509 -pubkey -inform pem -in certificate.pem -noout &gt; key.pub </code></p>
  <p>but got an encoding error similar to this:</p>
  <pre><code>unable to load certificate
  140735207381436:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1319:
  140735207381436:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:381:Type=X509_CINF
  140735207381436:error:0D08303A:asn1 encoding routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 error:tasn_dec.c:751:Field=cert_info, Type=X509
  140735207381436:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_oth.c:83:
  </code></pre>
  <p>What have I messed up? Thanks</p>