Skip to content

Commit d108199

Browse files
author
Tero Heinonen
authored
Certificate set API changed. (ARMmbed#73)
One pointer only for certificate chain data.
1 parent 2d622e0 commit d108199

File tree

5 files changed

+16
-26
lines changed

5 files changed

+16
-26
lines changed

coap-service/coap_service_api.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,16 @@ extern int8_t coap_service_set_duplicate_message_buffer(int8_t service_id, uint8
316316
* Set DTLS certificates.
317317
*
318318
* \param service_id Id number of the current service.
319-
* \param root_cert Pointer to CA certificate
320-
* \param root_cert_len CA certificate length
321-
* \param own_cert pointer to own certificate
322-
* \param own_cert_len length of own certificate
319+
* \param cert Pointer to certificate chain
320+
* \param cert_len Certificate length
323321
* \param priv_key pointer to private key
324322
* \param priv_key_len length of private key
325323
*
326324
* \return -1 For failure
327325
*- 0 For success
328326
*/
329327

330-
extern int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *root_cert, uint16_t root_cert_len, const unsigned char *own_cert, uint16_t own_cert_len, const unsigned char *priv_key, uint8_t priv_key_len);
328+
extern int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert, uint16_t cert_len, const unsigned char *priv_key, uint8_t priv_key_len);
331329
#ifdef __cplusplus
332330
}
333331
#endif

source/coap_security_handler.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,8 @@ static int coap_security_handler_configure_keys (coap_security_t *sec, coap_secu
282282
switch( sec->_conn_mode ){
283283
case CERTIFICATE:{
284284
#if defined(MBEDTLS_X509_CRT_PARSE_C)
285-
if( mbedtls_x509_crt_parse( &sec->_cacert, keys._ca_cert,
286-
keys._ca_cert_len ) < 0 ){
287-
break;
288-
}
289-
if( mbedtls_x509_crt_parse( &sec->_owncert, keys._own_cert,
290-
keys._own_cert_len ) < 0 ){
285+
if( keys._cert && mbedtls_x509_crt_parse( &sec->_owncert, keys._cert,
286+
keys._cert_len ) < 0 ){
291287
break;
292288
}
293289
if( mbedtls_pk_parse_key(&sec->_pkey, keys._priv_key, keys._priv_key_len, NULL, 0) < 0){
@@ -302,15 +298,15 @@ static int coap_security_handler_configure_keys (coap_security_t *sec, coap_secu
302298
//TODO: add server certi
303299
}
304300
//TODO: use MBEDTLS_SSL_VERIFY_REQUIRED instead of optional
305-
mbedtls_ssl_conf_authmode( &sec->_conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
301+
mbedtls_ssl_conf_authmode( &sec->_conf, MBEDTLS_SSL_VERIFY_NONE );
306302
mbedtls_ssl_conf_ca_chain( &sec->_conf, &sec->_cacert, NULL );
307303
ret = 0;
308304
#endif
309305
break;
310306
}
311307
case PSK: {
312308
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
313-
if( 0 != mbedtls_ssl_conf_psk(&sec->_conf, keys._priv_key, keys._priv_key_len, keys._own_cert, keys._own_cert_len) ){
309+
if( 0 != mbedtls_ssl_conf_psk(&sec->_conf, keys._priv_key, keys._priv_key_len, keys._cert, keys._cert_len) ){
314310
break;
315311
}
316312
mbedtls_ssl_conf_ciphersuites(&sec->_conf, PSK_SUITES);
@@ -395,6 +391,7 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
395391
#endif
396392

397393
if (coap_security_handler_configure_keys(sec, keys, is_server) != 0) {
394+
tr_debug("security credential configure failed");
398395
return -1;
399396
}
400397

source/coap_service_api.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ uint16_t coap_service_id_find_by_socket(int8_t socket_id)
554554
return this ? this->service_id:0;
555555
}
556556

557-
int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *root_cert, uint16_t root_cert_len, const unsigned char *own_cert, uint16_t own_cert_len, const unsigned char *priv_key, uint8_t priv_key_len)
557+
int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert, uint16_t cert_len, const unsigned char *priv_key, uint8_t priv_key_len)
558558
{
559559
coap_service_t *this = service_find(service_id);
560560
if (!this) {
@@ -571,11 +571,8 @@ int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *root
571571

572572
memset(this->conn_handler->security_keys, 0, sizeof(coap_security_keys_t));
573573

574-
this->conn_handler->security_keys->_ca_cert = root_cert;
575-
this->conn_handler->security_keys->_ca_cert_len = root_cert_len;
576-
577-
this->conn_handler->security_keys->_own_cert = own_cert;
578-
this->conn_handler->security_keys->_own_cert_len = own_cert_len;
574+
this->conn_handler->security_keys->_cert = cert;
575+
this->conn_handler->security_keys->_cert_len = cert_len;
579576

580577
this->conn_handler->security_keys->_priv_key = priv_key;
581578
this->conn_handler->security_keys->_priv_key_len = priv_key_len;

source/include/coap_security_handler.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ typedef enum {
6060
typedef struct {
6161
SecureConnectionMode mode;
6262
/* Certificate pointers, not owned */
63-
const unsigned char *_ca_cert;
64-
uint16_t _ca_cert_len;
65-
const unsigned char *_own_cert;
66-
uint16_t _own_cert_len;
63+
const unsigned char *_cert;
64+
uint16_t _cert_len;
6765
const unsigned char *_priv_key;
6866
uint8_t _priv_key_len;
6967
/* Secure key pointer, owned */

test/coap-service/unittest/coap_service_api/test_coap_service_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ bool test_conn_handler_callbacks()
433433
bool test_certificate_set()
434434
{
435435
/* Service not found, return failure */
436-
if (-1 != coap_service_certificate_set(1, NULL, 0, NULL, 0, NULL, 0)) {
436+
if (-1 != coap_service_certificate_set(1, NULL, 0, NULL, 0)) {
437437
return false;
438438
}
439439

@@ -446,13 +446,13 @@ bool test_certificate_set()
446446
return false;
447447

448448
/* Allocation fails */
449-
if (-1 != coap_service_certificate_set(1, NULL, 0, NULL, 0, NULL, 0)) {
449+
if (-1 != coap_service_certificate_set(1, NULL, 0, NULL, 0)) {
450450
return false;
451451
}
452452

453453
/* All OK */
454454
nsdynmemlib_stub.returnCounter = 1;
455-
if (0 != coap_service_certificate_set(1, NULL, 0, NULL, 0, NULL, 0)) {
455+
if (0 != coap_service_certificate_set(1, NULL, 0, NULL, 0)) {
456456
return false;
457457
}
458458

0 commit comments

Comments
 (0)