Skip to content

Commit 47e5dd7

Browse files
author
Mika Leppänen
committed
Enabled DER coded certificate support to Wi-SUN mesh API
Wi-SUN mesh API uses now nanostack certificate interface with length parameters. This enables that either PEM or DER formatted certificates can be used. Using the length configuration for certificates and keys is optional, so existing applications using the PEM certificates do not require changes.
1 parent d0b5ba6 commit 47e5dd7

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

features/nanostack/mbed-mesh-api/mbed_lib.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,29 @@
159159
"value": null
160160
},
161161
"root-certificate": {
162-
"help": "Root certificate in PEM format (must be a null terminated c-string)",
162+
"help": "Root certificate; in PEM format must be a null terminated c-string, in DER format the root-certificate-len must be set",
163163
"value": null
164164
},
165+
"root-certificate-len": {
166+
"help": "Root certificate length; optional for PEM format, must be defined for DER format",
167+
"value": null
168+
},
165169
"own-certificate": {
166-
"help": "Own certificate in PEM format (must be a null terminated c-string)",
170+
"help": "Own certificate; in PEM format must be a null terminated c-string, in DER format the own-certificate-len must be set",
171+
"value": null
172+
},
173+
"own-certificate-len": {
174+
"help": "Own certificate length; optional for PEM format, must be defined for DER format",
167175
"value": null
168176
},
169177
"own-certificate-key": {
170-
"help": "Own certificate's key in PEM format (must be a null terminated c-string)",
178+
"help": "Own certificate's key; in PEM format must be a null terminated c-string, in DER format the own-certificate-key-len must be set",
171179
"value": null
172-
}
180+
},
181+
"own-certificate-key-len": {
182+
"help": "Own certificate's key length; optional for PEM format, must be defined for DER format",
183+
"value": null
184+
}
173185
},
174186
"target_overrides": {
175187
"KW24D": {

features/nanostack/mbed-mesh-api/source/wisun_tasklet.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,36 @@ static void wisun_tasklet_configure_and_connect_to_network(void)
269269
}
270270

271271
#if defined(MBED_CONF_MBED_MESH_API_CERTIFICATE_HEADER)
272-
arm_certificate_chain_entry_s chain_info;
273-
memset(&chain_info, 0, sizeof(arm_certificate_chain_entry_s));
274-
chain_info.cert_chain[0] = (const uint8_t *) MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE;
275-
chain_info.cert_len[0] = strlen((const char *) MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE) + 1;
276-
chain_info.cert_chain[1] = (const uint8_t *) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE;
277-
chain_info.cert_len[1] = strlen((const char *) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE) + 1;
278-
chain_info.key_chain[1] = (const uint8_t *) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY;
279-
chain_info.chain_length = 2;
280-
arm_network_certificate_chain_set((const arm_certificate_chain_entry_s *) &chain_info);
272+
arm_certificate_entry_s trusted_cert = {
273+
.cert = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE,
274+
.key = NULL,
275+
.cert_len = 0,
276+
.key_len = 0
277+
};
278+
#ifdef MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE_LEN
279+
trusted_cert.cert_len = MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE_LEN;
280+
#else
281+
trusted_cert.cert_len = strlen((const char *) MBED_CONF_MBED_MESH_API_ROOT_CERTIFICATE) + 1;
282+
#endif
283+
arm_network_trusted_certificate_add((const arm_certificate_entry_s *)&trusted_cert);
284+
285+
arm_certificate_entry_s own_cert = {
286+
.cert = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE,
287+
.key = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY,
288+
.cert_len = 0,
289+
.key_len = 0
290+
};
291+
#ifdef MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_LEN
292+
own_cert.cert_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_LEN;
293+
#else
294+
own_cert.cert_len = strlen((const char *) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE) + 1;
295+
#endif
296+
#ifdef MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY_LEN
297+
own_cert.key_len = MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY_LEN;
298+
#else
299+
own_cert.key_len = strlen((const char *) MBED_CONF_MBED_MESH_API_OWN_CERTIFICATE_KEY) + 1;
300+
#endif
301+
arm_network_own_certificate_add((const arm_certificate_entry_s *)&own_cert);
281302
#endif
282303

283304
status = arm_nwk_interface_up(wisun_tasklet_data_ptr->network_interface_id);

features/nanostack/sal-stack-nanostack/source/6LoWPAN/ws/ws_pae_controller.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,40 @@ int8_t ws_pae_controller_certificate_chain_set(const arm_certificate_chain_entry
769769
return 0;
770770
}
771771

772+
int8_t ws_pae_controller_own_certificate_add(const arm_certificate_entry_s *cert)
773+
{
774+
if (!cert) {
775+
return -1;
776+
}
777+
778+
int8_t ret = -1;
779+
780+
ns_list_foreach(pae_controller_t, entry, &pae_controller_list) {
781+
for (uint8_t i = 0; i < SEC_PROT_CERT_CHAIN_DEPTH; i++) {
782+
if (entry->certs.own_cert_chain.cert[i] == NULL) {
783+
sec_prot_certs_cert_set(&entry->certs.own_cert_chain, i, (uint8_t *) cert->cert, cert->cert_len);
784+
// Set private key if set for the certificate that is added
785+
if (cert->key && cert->key_len > 0) {
786+
sec_prot_certs_priv_key_set(&entry->certs.own_cert_chain, (uint8_t *) cert->key, cert->key_len);
787+
}
788+
ret = 0;
789+
break;
790+
}
791+
}
792+
}
793+
794+
return ret;
795+
}
796+
797+
int8_t ws_pae_controller_own_certificates_remove(void)
798+
{
799+
ns_list_foreach(pae_controller_t, entry, &pae_controller_list) {
800+
sec_prot_certs_chain_entry_init(&entry->certs.own_cert_chain);
801+
}
802+
803+
return 0;
804+
}
805+
772806
int8_t ws_pae_controller_trusted_certificate_add(const arm_certificate_entry_s *cert)
773807
{
774808
if (!cert) {
@@ -816,6 +850,15 @@ int8_t ws_pae_controller_trusted_certificate_remove(const arm_certificate_entry_
816850
return ret;
817851
}
818852

853+
int8_t ws_pae_controller_trusted_certificates_remove(void)
854+
{
855+
ns_list_foreach(pae_controller_t, entry, &pae_controller_list) {
856+
sec_prot_certs_chain_list_delete(&entry->certs.trusted_cert_chain_list);
857+
}
858+
859+
return 0;
860+
}
861+
819862
int8_t ws_pae_controller_certificate_revocation_list_add(const arm_cert_revocation_list_entry_s *crl)
820863
{
821864
if (!crl) {

features/nanostack/sal-stack-nanostack/source/6LoWPAN/ws/ws_pae_controller.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ int8_t ws_pae_controller_timing_adjust(uint8_t timing);
158158
*/
159159
int8_t ws_pae_controller_certificate_chain_set(const arm_certificate_chain_entry_s *chain);
160160

161+
/**
162+
* ws_pae_controller_own_certificate_add add own certificate to certificate chain
163+
*
164+
* \param cert own certificate
165+
*
166+
* \return < 0 failure
167+
* \return >= 0 success
168+
*
169+
*/
170+
int8_t ws_pae_controller_own_certificate_add(const arm_certificate_entry_s *cert);
171+
172+
/**
173+
* ws_pae_controller_own_certificates_remove removes own certificates
174+
*
175+
* \return < 0 failure
176+
* \return >= 0 success
177+
*
178+
*/
179+
int8_t ws_pae_controller_own_certificates_remove(void);
180+
161181
/**
162182
* ws_pae_controller_trusted_certificate_add add trusted certificate
163183
*
@@ -180,6 +200,15 @@ int8_t ws_pae_controller_trusted_certificate_add(const arm_certificate_entry_s *
180200
*/
181201
int8_t ws_pae_controller_trusted_certificate_remove(const arm_certificate_entry_s *cert);
182202

203+
/**
204+
* ws_pae_controller_trusted_certificates_remove removes trusted certificates
205+
*
206+
* \return < 0 failure
207+
* \return >= 0 success
208+
*
209+
*/
210+
int8_t ws_pae_controller_trusted_certificates_remove(void);
211+
183212
/**
184213
* ws_pae_controller_certificate_revocation_list_add add certification revocation list
185214
*

features/nanostack/sal-stack-nanostack/source/libNET/src/ns_net.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,18 +988,30 @@ int8_t arm_network_trusted_certificate_remove(const arm_certificate_entry_s *cer
988988

989989
int8_t arm_network_trusted_certificates_remove(void)
990990
{
991+
#ifdef HAVE_WS
992+
return ws_pae_controller_trusted_certificates_remove();
993+
#else
991994
return -1;
995+
#endif
992996
}
993997

994998
int8_t arm_network_own_certificate_add(const arm_certificate_entry_s *cert)
995999
{
1000+
#ifdef HAVE_WS
1001+
return ws_pae_controller_own_certificate_add(cert);
1002+
#else
9961003
(void) cert;
9971004
return -1;
1005+
#endif
9981006
}
9991007

10001008
extern int8_t arm_network_own_certificates_remove(void)
10011009
{
1010+
#ifdef HAVE_WS
1011+
return ws_pae_controller_own_certificates_remove();
1012+
#else
10021013
return -1;
1014+
#endif
10031015
}
10041016

10051017
int8_t arm_network_certificate_revocation_list_add(const arm_cert_revocation_list_entry_s *crl)

0 commit comments

Comments
 (0)