<?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>Certificate generation with %KEYGEN</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/87774/certificate-generation-with-keygen</link><description>Hi, 
 
 For an MQTT application, we need to generate client certificates in the Nordic (nRF9160) to simplify the production phase. Ideally we want to push a CA certificate with the command %CMNG to a security tag and call a command to generate a client</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 13 May 2022 08:56:40 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/87774/certificate-generation-with-keygen" /><item><title>RE: Certificate generation with %KEYGEN</title><link>https://devzone.nordicsemi.com/thread/367768?ContentTypeID=1</link><pubDate>Fri, 13 May 2022 08:56:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5770617f-c603-4460-aa3a-a4916d081a4d</guid><dc:creator>Albrecht Markus Schellenberger</dc:creator><description>&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Hello again Elisa,&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user=""]1. I see that an AT command exists for key generation (%KEYGEN) that can create a certificate signing request (CSR). Are there&amp;nbsp;or will be a command to generate a client certificate from this CSR?&amp;nbsp; If not, do you recommend to use the command to generate the CSR and do the signing ourselves in the code or to do everything ourselves ? We looked at the library MBEDTLS for that.&amp;nbsp;[/quote]
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;We have some guidance available in the following links:&lt;br /&gt;&lt;br /&gt;&lt;a href="https://docs.nrfcloud.com/Guides/GettingStarted/Devices/#securely-generating-credentials-on-the-nrf9160" rel="noopener noreferrer" target="_blank"&gt;https://docs.nrfcloud.com/Guides/GettingStarted/Devices/#securely-generating-credentials-on-the-nrf9160&lt;/a&gt;&lt;br /&gt; &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/nrf9160/lwm2m_client/provisioning.html" rel="noopener noreferrer" target="_blank"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/nrf9160/lwm2m_client/provisioning.html&lt;br /&gt;&lt;br /&gt;&lt;/a&gt;Another alternative would be to use &lt;a href="https://www.openssl.org/"&gt;openssl&lt;/a&gt;, as demonstrated below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Generate root certificate for my devices

openssl genrsa -out ca.key 2048
&amp;#39;openssl req -new -x509 -key ca.key -out ca.crt -batch
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then use AT commands to generate CSR:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;AT%KEYGEN={SEC_TAG},2,0&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then from the output, take all until the first dot. Run it through BASE64URL decoder and save it to &lt;b&gt;csr.der.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Then generate the client certificate:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Covert DER to PEM

openssl req -inform der -in csr.der -out csr.pem

# Generate certificate

openssl x509 -req -in csr.pem -CA ca.crt -CAkey ca.key -CAcreateserial
&lt;/pre&gt;&lt;/p&gt;
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Then the produced certificate is written back to the modem:&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;AT%CMNG=0,{SEC_TAG},1,&amp;quot;{crt}&amp;quot;&lt;/pre&gt;&lt;/p&gt;
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;I hope I could answer your questions to your satisfaction :-)&lt;br /&gt;&lt;br /&gt;Regards,&lt;/p&gt;
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Markus&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Certificate generation with %KEYGEN</title><link>https://devzone.nordicsemi.com/thread/367506?ContentTypeID=1</link><pubDate>Thu, 12 May 2022 03:54:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:762be288-67be-4b59-95cd-7f7fc8eeea1c</guid><dc:creator>Albrecht Markus Schellenberger</dc:creator><description>&lt;p&gt;Hello Elisa,&lt;/p&gt;
[quote user=""]2. It&amp;nbsp;is recommended to have 2 CA certificates if one is revoked. But only one CA certificate is associate to a security tag. Do you have a way do deal with lists of CA certificate ? Or do we need to change the CA certificate in the corresponding tag if it is not valid anymore?&amp;nbsp;&lt;br /&gt;Also, it&amp;nbsp;looks like there is not a specific error returned for an invalid CA certificate during the mqtt connection. There is an error&amp;nbsp;that could correspond to various problems. Am I right ? If yes, how can we know that a new CA certificate is needed ?&amp;nbsp;[/quote]
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Several Root CAs can be stored to the modem file system. Security tags are used to separate the CA when opening a TLS connection. An application may open a TLS connection using the CA in any security tag.&lt;br /&gt;&lt;br /&gt;All internal TLS specific socket API error codes are translated to errno &lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/zephyr-v2.7.1/lib/libc/minimal/include/errno.h#L96"&gt;ECONNREFUSED&lt;/a&gt;. There is no errno for these cases that is specific enough to distinguish why connect() failed. The best the application could do is provide more than one security tag.&lt;br /&gt;&lt;br /&gt;Regards,&lt;/p&gt;
[quote user=""]1. I see that an AT command exists for key generation (%KEYGEN) that can create a certificate signing request (CSR). Are there&amp;nbsp;or will be a command to generate a client certificate from this CSR?&amp;nbsp; If not, do you recommend to use the command to generate the CSR and do the signing ourselves in the code or to do everything ourselves ? We looked at the library MBEDTLS for that.&amp;nbsp;[/quote]
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;I will come back to you on this.&lt;br /&gt;&lt;br /&gt;Regards,&lt;/p&gt;
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Markus&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Certificate generation with %KEYGEN</title><link>https://devzone.nordicsemi.com/thread/367371?ContentTypeID=1</link><pubDate>Wed, 11 May 2022 11:02:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd1df585-f639-4359-86ea-6eee77e9f51f</guid><dc:creator>Albrecht Markus Schellenberger</dc:creator><description>&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Hello Elisa,&lt;br /&gt;&lt;br /&gt;I need to check some details around your questions with our modem team. I will come back to you as soon as I have an update to share.&lt;br /&gt;&lt;br /&gt;Regards,&lt;/p&gt;
&lt;p style="line-height:100%;margin-bottom:0;" lang="en-GB"&gt;Markus&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>