@@ -147,6 +147,30 @@ int nrf_cloud_credentials_configured_check(void)
147147 }
148148 }
149149
150+ if (IS_ENABLED (CONFIG_NRF_CLOUD_MQTT ) || IS_ENABLED (CONFIG_NRF_CLOUD_REST )) {
151+ if (!cs .ca_aws && cs .ca_coap ) {
152+ /* There is a CA, but not large enough to be correct */
153+ LOG_WRN ("Connection using MQTT or REST may fail as the size of the CA "
154+ "cert indicates it is a CoAP root CA." );
155+ ret = - ENOPROTOOPT ;
156+ }
157+ }
158+
159+ if (IS_ENABLED (CONFIG_NRF_CLOUD_COAP )) {
160+ if (!cs .ca_coap && cs .ca_aws ) {
161+ LOG_WRN ("Connection using CoAP "
162+ "may fail as the size of the CA cert indicates "
163+ "CoAP CA certificate is missing but AWS CA is present." );
164+ ret = - ENOPROTOOPT ;
165+ } else if (!IS_ENABLED (CONFIG_NRF_CLOUD_COAP_DOWNLOADS )) {
166+ if (cs .ca && (!cs .ca_coap || !cs .ca_aws )) {
167+ LOG_WRN ("Connection using CoAP and downloading using HTTP "
168+ "may fail as the size of the CA cert indicates both "
169+ "CoAP and AWS root CA certs are not present." );
170+ ret = - ENOPROTOOPT ;
171+ }
172+ }
173+ }
150174 return ret ;
151175}
152176
@@ -177,6 +201,34 @@ static int cred_exists(uint32_t sec_tag, int type, bool *exists)
177201 return err ;
178202}
179203
204+ static int cred_size_get (uint32_t sec_tag , int type , size_t * cred_sz )
205+ {
206+ int err ;
207+
208+ * cred_sz = 0 ; /* We just want to determine the size */
209+
210+ #if defined(CONFIG_NRF_CLOUD_CREDENTIALS_MGMT_MODEM )
211+ uint8_t buf [1 ];
212+
213+ err = modem_key_mgmt_read (sec_tag , (enum modem_key_mgmt_cred_type )cred_type [type ],
214+ buf , cred_sz );
215+ if (err && (err != - ENOMEM )) {
216+ LOG_ERR ("modem_key_mgmt_read() failed for type %d in sec tag %u, error: %d" ,
217+ (enum modem_key_mgmt_cred_type )cred_type [type ], sec_tag , err );
218+ } else {
219+ err = 0 ;
220+ }
221+ #else
222+ err = tls_credential_get (sec_tag , (enum tls_credential_type )cred_type [type ],
223+ NULL , cred_sz );
224+ if (err == - EFBIG ) { /* Error expected since we only want the size */
225+ err = 0 ;
226+ }
227+ #endif
228+
229+ return err ;
230+ }
231+
180232int nrf_cloud_credentials_check (struct nrf_cloud_credentials_status * const cs )
181233{
182234 if (!cs ) {
@@ -192,27 +244,56 @@ int nrf_cloud_credentials_check(struct nrf_cloud_credentials_status *const cs)
192244
193245 ret = cred_exists (cs -> sec_tag , CA_CERT , & exists );
194246 if (ret < 0 ) {
247+ LOG_ERR ("Error checking CA exists" );
195248 return - EIO ;
196249 }
197250 cs -> ca = exists ;
251+ if (exists ) {
252+ ret = cred_size_get (cs -> sec_tag , CA_CERT , & cs -> ca_size );
253+ if (ret < 0 ) {
254+ LOG_ERR ("Error checking CA size" );
255+ return - EIO ;
256+ }
257+ /* These flags are approximate and only useful for logging to help diagnose
258+ * possible provisioning mistakes.
259+ */
260+ size_t coap_min_sz = CONFIG_NRF_CLOUD_COAP_CA_CERT_SIZE_THRESHOLD ;
261+ size_t aws_min_sz = CONFIG_NRF_CLOUD_AWS_CA_CERT_SIZE_THRESHOLD ;
262+ size_t combined_min_sz = aws_min_sz + coap_min_sz ;
263+
264+ if (cs -> ca_size > combined_min_sz ) {
265+ cs -> ca_aws = true;
266+ cs -> ca_coap = true;
267+ } else if (cs -> ca_size > aws_min_sz ) {
268+ cs -> ca_aws = true;
269+ } else if (cs -> ca_size > coap_min_sz ) {
270+ cs -> ca_coap = true;
271+ }
272+ }
198273
199274 ret = cred_exists (cs -> sec_tag , CLIENT_CERT , & exists );
200275 if (ret < 0 ) {
276+ LOG_ERR ("Error checking client cert exists" );
201277 return - EIO ;
202278 }
203279 cs -> client_cert = exists ;
204280
205281 ret = cred_exists (cs -> sec_tag , PRIVATE_KEY , & exists );
206282 if (ret < 0 ) {
283+ LOG_ERR ("Error checking private key exists" );
207284 return - EIO ;
208285 }
209286 cs -> prv_key = exists ;
210287
211- LOG_DBG ("Sec Tag: %u, CA: %s, Client Cert: %s, Private Key: %s" ,
288+ LOG_INF ("Sec Tag: %u; CA: %s, Client Cert: %s, Private Key: %s" ,
212289 cs -> sec_tag ,
213290 cs -> ca ? "Yes" : "No" ,
214291 cs -> client_cert ? "Yes" : "No" ,
215292 cs -> prv_key ? "Yes" : "No" );
293+ LOG_INF ("CA Size: %zd, AWS: %s, CoAP: %s" ,
294+ cs -> ca_size ,
295+ cs -> ca_aws ? "Likely" : "Unlikely" ,
296+ cs -> ca_coap ? "Likely" : "Unlikely" );
216297
217298 return 0 ;
218299}
0 commit comments