<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Using X509 certificates</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/124297/using-x509-certificates</link><description>Hello, 
 I am using NRF5340-DK for my project and I wanted to know if there is some example on how to read and extract the public key from this type of certificate. I have read something about mbedtls library but did not find any example. Is there some</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 12 Sep 2025 08:19:34 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/124297/using-x509-certificates" /><item><title>RE: Using X509 certificates</title><link>https://devzone.nordicsemi.com/thread/548568?ContentTypeID=1</link><pubDate>Fri, 12 Sep 2025 08:19:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5fcd428c-7136-48a8-bc3a-ade33e8e8b69</guid><dc:creator>Portilha</dc:creator><description>&lt;p&gt;I will leave this code here for anyone interested. It can verify and extract public key from X509 DER certificates.&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;

#include &amp;quot;mbedtls/x509_crt.h&amp;quot;
#include &amp;quot;mbedtls/pk.h&amp;quot;
#include &amp;quot;mbedtls/ctr_drbg.h&amp;quot;
#include &amp;quot;mbedtls/entropy.h&amp;quot;

const unsigned char cert_der[] = {
  0x30, 0x82, 0x01, 0xf2, 0x30, 0x82, 0x01, 0x97, 0xa0, 0x03, 0x02, 0x01,
  0x02, 0x02, 0x14, 0x20, 0x8b, 0xaf, 0xbf, 0x1d, 0x3a, 0x20, 0x79, 0xcd,
  0xcb, 0x64, 0x95, 0x85, 0x5e, 0x5e, 0xb3, 0x2d, 0x51, 0x1d, 0xe0, 0x30,
  0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30,
  0x4e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  0x50, 0x54, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  0x05, 0x42, 0x72, 0x61, 0x67, 0x61, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03,
  0x55, 0x04, 0x07, 0x0c, 0x09, 0x47, 0x75, 0x69, 0x6d, 0x61, 0x72, 0x61,
  0x65, 0x73, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  0x03, 0x44, 0x54, 0x78, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04,
  0x03, 0x0c, 0x04, 0x4a, 0x6f, 0x73, 0x65, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  0x35, 0x30, 0x39, 0x31, 0x31, 0x30, 0x39, 0x30, 0x31, 0x35, 0x39, 0x5a,
  0x17, 0x0d, 0x32, 0x36, 0x30, 0x39, 0x31, 0x31, 0x30, 0x39, 0x30, 0x31,
  0x35, 0x39, 0x5a, 0x30, 0x4e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  0x04, 0x06, 0x13, 0x02, 0x50, 0x54, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03,
  0x55, 0x04, 0x08, 0x0c, 0x05, 0x42, 0x72, 0x61, 0x67, 0x61, 0x31, 0x12,
  0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x47, 0x75, 0x69,
  0x6d, 0x61, 0x72, 0x61, 0x65, 0x73, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03,
  0x55, 0x04, 0x0a, 0x0c, 0x03, 0x44, 0x54, 0x78, 0x31, 0x0d, 0x30, 0x0b,
  0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, 0x4a, 0x6f, 0x73, 0x65, 0x30,
  0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01,
  0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42,
  0x00, 0x04, 0x3d, 0xf7, 0x62, 0xb2, 0xef, 0x4e, 0xc1, 0x5b, 0xc5, 0xe7,
  0x13, 0x09, 0x3d, 0x7d, 0x4c, 0x6c, 0x8d, 0x25, 0x3b, 0x19, 0xd6, 0xa0,
  0x5c, 0xfa, 0xac, 0x55, 0x1c, 0xc5, 0x4e, 0x28, 0xbd, 0xea, 0x49, 0x56,
  0x23, 0xe3, 0xa0, 0x34, 0xf6, 0xeb, 0x70, 0x59, 0x62, 0x54, 0x41, 0xcc,
  0xcf, 0x57, 0x48, 0x5d, 0x18, 0xa2, 0x02, 0xd3, 0xc2, 0x0d, 0xba, 0xdc,
  0x4a, 0x8b, 0x8d, 0x58, 0x47, 0x62, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d,
  0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x7e, 0xfd, 0x9a,
  0x52, 0x1f, 0x19, 0xa0, 0x70, 0xd9, 0x89, 0x69, 0xaa, 0x3e, 0x94, 0x94,
  0xad, 0x74, 0x9c, 0x4f, 0xce, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
  0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x7e, 0xfd, 0x9a, 0x52, 0x1f, 0x19,
  0xa0, 0x70, 0xd9, 0x89, 0x69, 0xaa, 0x3e, 0x94, 0x94, 0xad, 0x74, 0x9c,
  0x4f, 0xce, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
  0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a,
  0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46,
  0x02, 0x21, 0x00, 0xb4, 0x16, 0x73, 0xac, 0x73, 0xb0, 0xa3, 0x00, 0xd9,
  0x77, 0x05, 0xc6, 0x25, 0xe7, 0x94, 0x76, 0x53, 0xd4, 0xc6, 0x8e, 0x6a,
  0xb4, 0x71, 0x9d, 0x21, 0xa5, 0x89, 0x85, 0x2f, 0xe4, 0x93, 0xf8, 0x02,
  0x21, 0x00, 0xed, 0xdd, 0xfe, 0x29, 0xdc, 0xb6, 0xb0, 0xce, 0x24, 0xf2,
  0xab, 0x42, 0x49, 0xed, 0xe8, 0xfe, 0xf9, 0xd5, 0xbb, 0x57, 0x93, 0x24,
  0x1b, 0x32, 0x37, 0xd2, 0x49, 0x5d, 0x83, 0xc8, 0x3f, 0xd1
};

const unsigned int cert_der_len = 502;

int main(void)
{
    printk(&amp;quot;Start X509 test\n&amp;quot;);
    
    int ret;
    mbedtls_x509_crt cert;
    mbedtls_x509_crt_init(&amp;amp;cert);

    /* Load certificate from DER array */
    ret = mbedtls_x509_crt_parse_der(&amp;amp;cert, cert_der, cert_der_len);
    if (ret &amp;lt; 0) 
    {
        printk(&amp;quot;Failed to parse certificate (err %d)\n&amp;quot;, ret);
        return -1;
    }

    // Verify self-signed certificate (no RNG)
    uint32_t flags = 0;
    ret = mbedtls_x509_crt_verify(&amp;amp;cert, &amp;amp;cert, NULL, NULL, &amp;amp;flags, NULL, NULL);
    if (ret == 0 &amp;amp;&amp;amp; flags == 0) {
        printk(&amp;quot;Certificate signature is VALID (self-signed)\n&amp;quot;);
    } else {
        printk(&amp;quot;Certificate verification FAILED, ret=%d, flags=0x%08x\n&amp;quot;, ret, flags);
    }

    // Extract and print public key (optional)
    mbedtls_pk_context *pk = &amp;amp;cert.pk;
    if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) 
    {
        mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
        const mbedtls_ecp_point *Q = &amp;amp;ec-&amp;gt;MBEDTLS_PRIVATE(Q);

        char buf[100]; // safe buffer
        size_t olen;

        // Print X
        ret = mbedtls_mpi_write_string(&amp;amp;Q-&amp;gt;MBEDTLS_PRIVATE(X), 16, buf, sizeof(buf), &amp;amp;olen);
        if (ret == 0) {
            printk(&amp;quot;Public key X: 0x%s\n&amp;quot;, buf);
        } else {
            printk(&amp;quot;Error printing X: %d\n&amp;quot;, ret);
        }

        // Print Y
        ret = mbedtls_mpi_write_string(&amp;amp;Q-&amp;gt;MBEDTLS_PRIVATE(Y), 16, buf, sizeof(buf), &amp;amp;olen);
        if (ret == 0) {
            printk(&amp;quot;Public key Y: 0x%s\n&amp;quot;, buf);
        } else {
            printk(&amp;quot;Error printing Y: %d\n&amp;quot;, ret);
        }
    }
   
    mbedtls_x509_crt_free(&amp;amp;cert);

    return 0;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using X509 certificates</title><link>https://devzone.nordicsemi.com/thread/548536?ContentTypeID=1</link><pubDate>Thu, 11 Sep 2025 19:03:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:87092bc3-9c19-4a7d-8c1d-4021b4e631c5</guid><dc:creator>Kazi Afroza Sultana</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you want the key in the DER format for later verifications of the generated JWT, use the&amp;nbsp;&lt;/span&gt;&lt;a title="(in Kconfig reference v&amp;amp;nbsp;)" href="https://docs.nordicsemi.com/bundle/ncs-latest/page/kconfig/index.html#CONFIG_APP_JWT_PRINT_EXPORTED_PUBKEY_DER"&gt;&lt;code&gt;&lt;span&gt;CONFIG_APP_JWT_PRINT_EXPORTED_PUBKEY_DER&lt;/span&gt;&lt;/code&gt;&lt;/a&gt;&lt;span&gt;&amp;nbsp;Kconfig option that prints the DER-formatted key to the debug terminal. You can convert the DER key into PEM format by encoding it in base64 and adding the PEM markers&amp;nbsp;&lt;/span&gt;&lt;code&gt;&lt;span&gt;-----BEGIN&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;PUBLIC&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;KEY-----&lt;/span&gt;&lt;/code&gt;&lt;span&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;code&gt;&lt;span&gt;-----END&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;PUBLIC&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;KEY-----&lt;/span&gt;&lt;/code&gt;&lt;span&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;source:&amp;nbsp;&lt;a href="https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/samples/app_jwt/README.html#testing"&gt;Application JWT&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using X509 certificates</title><link>https://devzone.nordicsemi.com/thread/548466?ContentTypeID=1</link><pubDate>Thu, 11 Sep 2025 09:37:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e5bcba2c-b2f5-4305-b83a-891b52b38300</guid><dc:creator>Portilha</dc:creator><description>&lt;p&gt;Hello,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Is it possible to use DER format?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using X509 certificates</title><link>https://devzone.nordicsemi.com/thread/548465?ContentTypeID=1</link><pubDate>Thu, 11 Sep 2025 09:36:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d17aeebe-68aa-47ff-a6a9-6cf6dd616266</guid><dc:creator>Kazi Afroza Sultana</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;In nRF Connect SDK, PEM is supported with the Nordic Security (mbedTLS) backend, you can look at AWS IoT sample to see how to use it.&lt;/p&gt;
&lt;p&gt;You can look at this previous case&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/support-private/support/327988"&gt;(+) Nordic DevZone&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>