diff --git a/examples/coap-client.c b/examples/coap-client.c index bc8f7741..3389da5b 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -109,6 +109,7 @@ static char *pkcs11_pin = NULL; /* PKCS11 pin to unlock access to token */ static char *ca_file = NULL; /* CA for cert_file - for cert checking in PEM, DER or PKCS11 URI */ static char *root_ca_file = NULL; /* List of trusted Root CAs in PEM */ +static int no_trust_store = 0; /* Trust store not to be installed. */ static int is_rpk_not_cert = 0; /* Cert is RPK if set */ static uint8_t *cert_mem = NULL; /* certificate and private key in PEM_BUF */ static uint8_t *key_mem = NULL; /* private key in PEM_BUF */ @@ -515,7 +516,7 @@ usage(const char *program, const char *version) { "\t\t[[-d count]]\n" "\t\t[[h match_hint_file] [-k key] [-u user] [-2]]\n" "\t\t[[-c certfile] [-j keyfile] [-n] [-C cafile]\n" - "\t\t[-J pkcs11_pin] [-M raw_pk] [-R trust_casfile]] URI\n" + "\t\t[-J pkcs11_pin] [-M raw_pk] [-R trust_casfile] [-Y]] URI\n" "\tURI can be an absolute URI or a URI prefixed with scheme and host\n\n" "General Options\n" "\t-a addr\t\tThe local interface address to use\n" @@ -612,6 +613,13 @@ usage(const char *program, const char *version) { "\t-c certfile\tPEM file or PKCS11 URI for the certificate. The private\n" "\t \t\tkey can also be in the PEM file, or has the same PKCS11\n" "\t \t\tURI. If not, the private key is defined by '-j keyfile'\n" + "\t \t\tIf both the '-c certfile' and '-k key' options are not\n" + "\t \t\tprovided, but the protocol is using encryption (e.g.\n" + "\t \t\tcoaps), then the client logic will use internally\n" + "\t \t\tgenerated certificates (as do web browsers) but will\n" + "\t \t\tcheck the server certificate based on the trust store\n" + "\t \t\t(or the '-R trust_casfile' option) unless the '-n'\n" + "\t \t\toption is specified\n" "\t-j keyfile\tPEM file or PKCS11 URI for the private key for the\n" "\t \t\tcertificate in '-c certfile' if the parameter is\n" "\t \t\tdifferent from certfile in '-c certfile'\n" @@ -641,6 +649,8 @@ usage(const char *program, const char *version) { "\t \t\tUsing the -C or -R options will trigger the\n" "\t \t\tvalidation of the server certificate unless overridden\n" "\t \t\tby the -n option\n" + "\t-Y\n" + "\t \t\tDo not load the default system Trusted Root CA Store\n" ); fprintf(stderr, "Examples:\n" @@ -1395,31 +1405,37 @@ setup_pki(coap_context_t *ctx) { coap_context_set_pki_root_cas(ctx, root_ca_file, NULL); } } + /* + * If trust store CAs are to be defined + * + * If need to verify server certificate + * If Trust Store load is not disabled + * If not mutually validating same CA signed client & server certs + */ + if (verify_peer_cert && !no_trust_store && !ca_file && !root_ca_file) { + coap_context_load_pki_trust_store(ctx); + } memset(&dtls_pki, 0, sizeof(dtls_pki)); dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; - if (ca_file || root_ca_file) { - /* - * Add in additional certificate checking. - * This list of enabled can be tuned for the specific - * requirements - see 'man coap_encryption'. - * - * Note: root_ca_file is setup separately using - * coap_context_set_pki_root_cas(), but this is used to define what - * checking actually takes place. - */ - dtls_pki.verify_peer_cert = verify_peer_cert; - dtls_pki.check_common_ca = !root_ca_file; - dtls_pki.allow_self_signed = 1; - dtls_pki.allow_expired_certs = 1; - dtls_pki.cert_chain_validation = 1; - dtls_pki.cert_chain_verify_depth = 2; - dtls_pki.check_cert_revocation = 1; - dtls_pki.allow_no_crl = 1; - dtls_pki.allow_expired_crl = 1; - } else if (is_rpk_not_cert) { - dtls_pki.verify_peer_cert = verify_peer_cert; - } + /* + * Add in additional certificate checking. + * This list of enabled can be tuned for the specific + * requirements - see 'man coap_encryption'. + * + * Note: root_ca_file is setup separately using + * coap_context_set_pki_root_cas(), as well as the trust store is added in + * but this is used to define what checking actually takes place. + */ + dtls_pki.verify_peer_cert = verify_peer_cert; + dtls_pki.check_common_ca = !root_ca_file; + dtls_pki.allow_self_signed = 1; + dtls_pki.allow_expired_certs = 1; + dtls_pki.cert_chain_validation = 1; + dtls_pki.cert_chain_verify_depth = 2; + dtls_pki.check_cert_revocation = 1; + dtls_pki.allow_no_crl = 1; + dtls_pki.allow_expired_crl = 1; dtls_pki.is_rpk_not_cert = is_rpk_not_cert; dtls_pki.use_cid = setup_cid; dtls_pki.validate_cn_call_back = verify_cn_callback; @@ -1683,7 +1699,7 @@ main(int argc, char **argv) { coap_startup(); while ((opt = getopt(argc, argv, - "a:b:c:d:e:f:h:j:k:l:m:no:p:q:rs:t:u:v:wx:A:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:2")) != -1) { + "a:b:c:d:e:f:h:j:k:l:m:no:p:q:rs:t:u:v:wx:A:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:Y2")) != -1) { switch (opt) { case 'a': strncpy(node_str, optarg, NI_MAXHOST - 1); @@ -1702,6 +1718,9 @@ main(int argc, char **argv) { case 'C': ca_file = optarg; break; + case 'Y': + no_trust_store = 1; + break; case 'R': root_ca_file = optarg; break; diff --git a/examples/coap-server.c b/examples/coap-server.c index 35db0324..74f88cc8 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -120,6 +120,7 @@ static size_t cert_mem_len = 0; static size_t key_mem_len = 0; static size_t ca_mem_len = 0; static int verify_peer_cert = 1; /* PKI granularity - by default set */ +static int no_trust_store = 0; /* Trust store not to be installed. */ #define MAX_KEY 64 /* Maximum length of a pre-shared key in bytes. */ static uint8_t *key = NULL; static ssize_t key_length = 0; @@ -1417,6 +1418,11 @@ static coap_dtls_pki_t * setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *client_sni) { static coap_dtls_pki_t dtls_pki; + /* If trust store CAs are to be defined */ + if (verify_peer_cert && !no_trust_store && !ca_file) { + coap_context_load_pki_trust_store(ctx); + } + /* If general root CAs are defined */ if (role == COAP_DTLS_ROLE_SERVER && root_ca_file) { struct stat stbuf; @@ -1429,28 +1435,24 @@ setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *client_sni) { memset(&dtls_pki, 0, sizeof(dtls_pki)); dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; - if (ca_file || root_ca_file) { - /* - * Add in additional certificate checking. - * This list of enabled can be tuned for the specific - * requirements - see 'man coap_encryption'. - * - * Note: root_ca_file is setup separately using - * coap_context_set_pki_root_cas(), but this is used to define what - * checking actually takes place. - */ - dtls_pki.verify_peer_cert = verify_peer_cert; - dtls_pki.check_common_ca = !root_ca_file; - dtls_pki.allow_self_signed = 1; - dtls_pki.allow_expired_certs = 1; - dtls_pki.cert_chain_validation = 1; - dtls_pki.cert_chain_verify_depth = 2; - dtls_pki.check_cert_revocation = 1; - dtls_pki.allow_no_crl = 1; - dtls_pki.allow_expired_crl = 1; - } else if (is_rpk_not_cert) { - dtls_pki.verify_peer_cert = verify_peer_cert; - } + /* + * Add in additional certificate checking. + * This list of enabled can be tuned for the specific + * requirements - see 'man coap_encryption'. + * + * Note: root_ca_file is setup separately using + * coap_context_set_pki_root_cas(), but this is used to define what + * checking actually takes place. + */ + dtls_pki.verify_peer_cert = verify_peer_cert; + dtls_pki.check_common_ca = !root_ca_file; + dtls_pki.allow_self_signed = 1; + dtls_pki.allow_expired_certs = 1; + dtls_pki.cert_chain_validation = 1; + dtls_pki.cert_chain_verify_depth = 2; + dtls_pki.check_cert_revocation = 1; + dtls_pki.allow_no_crl = 1; + dtls_pki.allow_expired_crl = 1; dtls_pki.is_rpk_not_cert = is_rpk_not_cert; dtls_pki.validate_cn_call_back = verify_cn_callback; dtls_pki.cn_call_back_arg = (void *)role; @@ -1585,7 +1587,7 @@ usage(const char *program, const char *version) { "\t\t[-s match_psk_sni_file] [-u user] [-2]]\n" "\t\t[[-c certfile] [-j keyfile] [-m] [-n] [-C cafile]\n" "\t\t[-J pkcs11_pin] [-M rpk_file] [-R trust_casfile]\n" - "\t\t[-S match_pki_sni_file]]\n" + "\t\t[-S match_pki_sni_file] [-Y]]\n" "General Options\n" "\t-a priority\tSend logging output to syslog at priority (0-7) level\n" "\t-b max_block_size\n" @@ -1756,6 +1758,8 @@ usage(const char *program, const char *version) { "\t \t\t sni_to_match,new_cert_file,new_ca_file\n" "\t \t\tNote: -c and -C still need to be defined for the default\n" "\t \t\tcase\n" + "\t-Y\n" + "\t \t\tDo not load the default system Trusted Root CA Store\n" ); } @@ -2366,7 +2370,7 @@ main(int argc, char **argv) { clock_offset = time(NULL); while ((opt = getopt(argc, argv, - "a:b:c:d:ef:g:h:i:j:k:l:mnp:q:rs:tu:v:w:A:C:E:G:J:L:M:NP:R:S:T:U:V:X:2")) != -1) { + "a:b:c:d:ef:g:h:i:j:k:l:mnp:q:rs:tu:v:w:A:C:E:G:J:L:M:NP:R:S:T:U:V:X:Y2")) != -1) { switch (opt) { #ifndef _WIN32 case 'a': @@ -2550,6 +2554,9 @@ main(int argc, char **argv) { case 'X': csm_max_message_size = strtol(optarg, NULL, 10); break; + case 'Y': + no_trust_store = 1; + break; case '2': ec_jpake = 1; break; diff --git a/include/coap3/coap_dtls_internal.h b/include/coap3/coap_dtls_internal.h index a069c3c0..cfc4356d 100644 --- a/include/coap3/coap_dtls_internal.h +++ b/include/coap3/coap_dtls_internal.h @@ -135,6 +135,16 @@ int coap_dtls_context_set_pki_root_cas(coap_context_t *coap_context, const char *ca_file, const char *ca_dir); +/** + * Load dtls context's default trusted CAs for a client or server. + * + * @param coap_context The current coap_context_t object. + * + * @return @c 1 if successful, else @c 0. + */ + +int coap_dtls_context_load_pki_trust_store(coap_context_t *coap_context); + /** * Check whether one of the coap_dtls_context_set_{psk|pki}() functions have * been called. diff --git a/include/coap3/coap_net.h b/include/coap3/coap_net.h index e979c279..fe2ebb87 100644 --- a/include/coap3/coap_net.h +++ b/include/coap3/coap_net.h @@ -212,6 +212,15 @@ COAP_API int coap_context_set_pki_root_cas(coap_context_t *context, const char *ca_file, const char *ca_dir); +/** + * Load the hosts's default trusted CAs for a client or server. + * + * @param context The current coap_context_t object. + * + * @return @c 1 if successful, else @c 0. + */ +COAP_API int coap_context_load_pki_trust_store(coap_context_t *context); + /** * Set the context keepalive timer for sessions. * A keepalive message will be sent after if a session has been inactive, diff --git a/include/coap3/coap_net_internal.h b/include/coap3/coap_net_internal.h index fb2ccef7..eb179e77 100644 --- a/include/coap3/coap_net_internal.h +++ b/include/coap3/coap_net_internal.h @@ -586,6 +586,17 @@ int coap_context_set_pki_root_cas_lkd(coap_context_t *context, const char *ca_file, const char *ca_dir); +/** + * Load the context's default trusted CAs for a client or server. + * + * Note: This function must be called in the locked state. + * + * @param context The current coap_context_t object. + * + * @return @c 1 if successful, else @c 0. + */ +int coap_context_load_pki_trust_store_lkd(coap_context_t *context); + /** * Set the context's default PSK hint and/or key for a server. * diff --git a/libcoap-3.map b/libcoap-3.map index ecb979fd..fbd21aa0 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -53,6 +53,7 @@ global: coap_context_get_max_handshake_sessions; coap_context_get_max_idle_sessions; coap_context_get_session_timeout; + coap_context_load_pki_trust_store; coap_context_oscore_server; coap_context_set_app_data2; coap_context_set_app_data; diff --git a/libcoap-3.sym b/libcoap-3.sym index f7904a74..5872048f 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -51,6 +51,7 @@ coap_context_get_csm_timeout_ms coap_context_get_max_handshake_sessions coap_context_get_max_idle_sessions coap_context_get_session_timeout +coap_context_load_pki_trust_store coap_context_oscore_server coap_context_set_app_data coap_context_set_app_data2 diff --git a/man/coap-client.txt.in b/man/coap-client.txt.in index d57ffad4..365ef08b 100644 --- a/man/coap-client.txt.in +++ b/man/coap-client.txt.in @@ -30,7 +30,7 @@ SYNOPSIS [*-d* value] [[*-h* match_hint_file] [*-k* key] [*-u* user] [*-2*]] [[*-c* certfile] [*-j* keyfile] [*-n*] [*-C* cafile] - [*-J* pkcs11_pin] [*-M* rpk_file] [*-R* trust_casfile]] URI + [*-J* pkcs11_pin] [*-M* rpk_file] [*-R* trust_casfile] [*-Y*]] URI For *coap-client* versions that use libcoap compiled for different (D)TLS libraries, *coap-client-notls*, *coap-client-gnutls*, @@ -162,7 +162,8 @@ OPTIONS - General *-L* value:: Sum of one or more COAP_BLOCK_* flag values for different block handling - methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP). + methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP). This vlue can be set as a + hex value - e.g. 0x13. COAP_BLOCK_USE_LIBCOAP 1 COAP_BLOCK_SINGLE_BODY 2 @@ -245,7 +246,11 @@ definitions have to be in DER, not PEM, format. Otherwise all of *-c* certfile:: PEM file or PKCS11 URI for the certificate. The private key can also be in the PEM file, or has the same PKCS11 URI. If not, the private key is defined - by *-j keyfile*. + by *-j keyfile*. If both the *-c certfile* and *-k key* options are not + provided, but the protocol is using encryption (e.g. coaps), then the client + logic will use internally generated certificates (as do web browsers) but + check the server certificate based on the trust store (or the '-R + trust_casfile' option) unless the '-n' option is specified. *-j* keyfile:: PEM file or PKCS11 URI for the private key for the certificate in *-c @@ -257,8 +262,8 @@ definitions have to be in DER, not PEM, format. Otherwise all of *-C* cafile:: PEM file or PKCS11 URI for the CA certificate and any intermediate CAs that was used to sign the server - certfile. Ideally the client certificate should be signed by the same CA so - that mutual authentication can take place. The contents of cafile are added + certfile. Ideally the client certificate should be signed by the same CA so that + mutual authentication can take place. The contents of cafile are added to the trusted store of root CAs. Using the *-C* or *-R* options will trigger the validation of the server certificate unless overridden by the *-n* option. @@ -280,6 +285,9 @@ PEM file or PKCS11 URI for the CA certificate and any intermediate CAs that was trigger the validation of the server certificate unless overridden by the *-n* option. +*-Y* :: + Do not load the default system Trusted Root CA Store. + EXAMPLES -------- * Example diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index c8c38a99..deaab9a2 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -31,7 +31,7 @@ SYNOPSIS [*-s* match_psk_sni_file] [*-u* user] [*-2*]] [[*-c* certfile] [*-j* keyfile] [*-n*] [*-C* cafile] [*-J* pkcs11_pin] [*-M* rpk_file] [*-R* trust_casfile] - [*-S* match_pki_sni_file]] + [*-S* match_pki_sni_file] [*-Y*]] For *coap-server* versions that use libcoap compiled for different (D)TLS libraries, *coap-server-notls*, *coap-server-gnutls*, @@ -276,6 +276,9 @@ definitions have to be in DER, not PEM, format. Otherwise all of Note: *-c certfile* and *-C cafile* still needs to be defined for the default case +*-Y* :: + Do not load the default system Trusted Root CA Store. + EXAMPLES -------- * Example diff --git a/man/coap_encryption.txt.in b/man/coap_encryption.txt.in index 7241164c..5d0ad533 100644 --- a/man/coap_encryption.txt.in +++ b/man/coap_encryption.txt.in @@ -13,7 +13,9 @@ NAME coap_encryption, coap_dtls_cpsk_t, coap_dtls_spsk_t, -coap_dtls_pki_t +coap_dtls_pki_t, +coap_context_set_pki_root_cas, +coap_context_load_pki_trust_store - Work with CoAP TLS/DTLS SYNOPSIS @@ -26,6 +28,11 @@ SYNOPSIS *struct coap_dtls_pki_t;* +*int coap_context_set_pki_root_cas(coap_context_t *_context_, +const char *_ca_file_, const char *_ca_dir_);* + +*int coap_context_load_pki_trust_store(coap_context_t *_context_);* + For specific (D)TLS library support, link with *-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, *-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*, @@ -97,6 +104,7 @@ In principle the set-up sequence for CoAP Servers looks like ---- coap_new_context() coap_context_set_pki_root_cas() - if the root CAs need to be updated and PKI +coap_context_load_pki_trust_store() - if the CA trust store is to be loaded and PKI coap_context_set_pki() and/or coap_context_set_psk2() - if encryption is required coap_new_endpoint() ---- @@ -110,6 +118,7 @@ In principle the set-up sequence for CoAP Clients looks like ---- coap_new_context() coap_context_set_pki_root_cas() - if the root CAs need to be updated and PKI +coap_context_load_pki_trust_store() - if the CA trust store is to be loaded and PKI coap_new_client_session(), coap_new_client_session_pki() or coap_new_client_session_psk2() ---- @@ -122,6 +131,29 @@ different configuration options passed in by the *coap_context_set_pki*(), *coap_new_client_session_pki*(), *coap_context_set_psk2*() and *coap_new_client_session_psk2*() functions. +FUNCTIONS +--------- + +*Function: coap_context_set_pki_root_cas()* + +The *coap_context_set_pki_root_cas*() function is used to define a set of +root CAs to be used instead of the default set of root CAs provided as a part +of the TLS library. _ca_file_ points to a PEM encoded file containing the +list of CAs. _ca_file_ can be NULL. _ca_dir_ points to a directory +containing a set of PEM encoded files containing rootCAs. _ca_dir_ can be +NULL. One or both of _ca_file_ and _ca_dir_ must be set. + +*NOTE:* Some TLS libraries send the full list of CAs added by this function +during the (D)TLS session setup handshakes. To stop this, either provide a +single CA using the _ca_file_ definition in _pki_key_ in _setup_data_ variable +when calling *coap_context_set_pki*(3), or set _check_common_ca_ to 0 in +_setup_data_ variable. + +*Function: coap_context_load_pki_trust_store()* + +The *coap_context_load_pki_trust_store*() function will load in the hosts's +default trust store for PKI verification use. Not all TLS libraries or +platform types are supported. + PSK CLIENT INFORMATION ---------------------- @@ -298,7 +330,7 @@ typedef struct coap_dtls_spsk_t { void *id_call_back_arg; /* Passed in to the Identity callback function */ /** SNI check callback function. - * If not @p NULL, called if the SNI is not previously seen and exexuted + * If not @p NULL, called if the SNI is not previously seen and executed * prior to sending an Identity Hint back to the client so that the * appropriate PSK information can be used based on the requesting SNI. */ @@ -462,7 +494,6 @@ typedef struct coap_dtls_pki_t { uint8_t verify_peer_cert; /* 1 if peer cert is to be verified */ uint8_t check_common_ca; /* 1 if peer cert is to be signed by * the same CA as the local cert */ - uint8_t allow_self_signed; /* 1 if self-signed certs are allowed */ uint8_t allow_self_signed; /* 1 if self-signed certs are allowed. * Ignored if check_common_ca set */ uint8_t allow_expired_certs; /* 1 if expired certs are allowed */ @@ -986,6 +1017,12 @@ performance reasons (to save a potential buffer copy) and the length include this NULL terminator. It is not a requirement to have the NULL terminator though and the length must then reflect the actual data size. +RETURN VALUES +------------- + +*coap_context_set_pki_root_cas*() and *coap_context_load_pki_trust_store*() +return 1 on success, 0 on failure. + EXAMPLES -------- *CoAP Server DTLS PKI Setup* diff --git a/man/coap_endpoint_server.txt.in b/man/coap_endpoint_server.txt.in index 7a6784d3..84a54fec 100644 --- a/man/coap_endpoint_server.txt.in +++ b/man/coap_endpoint_server.txt.in @@ -12,7 +12,6 @@ NAME ---- coap_endpoint_server, coap_context_set_pki, -coap_context_set_pki_root_cas, coap_context_set_psk2, coap_new_endpoint, coap_free_endpoint, @@ -28,9 +27,6 @@ SYNOPSIS *int coap_context_set_pki(coap_context_t *_context_, const coap_dtls_pki_t *_setup_data_);* -*int coap_context_set_pki_root_cas(coap_context_t *_context_, -const char *_ca_file_, const char *_ca_dir_);* - *int coap_context_set_psk2(coap_context_t *_context_, coap_dtls_spsk_t *setup_data);* @@ -89,6 +85,7 @@ In principle the set-up sequence for CoAP Servers looks like ---- coap_new_context() coap_context_set_pki_root_cas() - if the root CAs need to be updated and using PKI +coap_context_load_pki_trust_store() - if the CA trust store is to be loaded and PKI coap_context_set_pki() and/or coap_context_set_psk2() - if encryption is required coap_new_endpoint() ---- @@ -109,20 +106,6 @@ configure the (D)TLS context using the _setup_data_ PKI variables as defined in the coap_dtls_pki_t structure - see *coap_encryption*(3). This should not be used for setting up client sessions. -*Function: coap_context_set_pki_root_cas()* - -The *coap_context_set_pki_root_cas*() function is used to define a set of -root CAs to be used instead of the default set of root CAs provided as a part -of the TLS library. _ca_file_ points to a PEM encoded file containing the -list of CAs. _ca_file_ can be NULL. _ca_dir_ points to a directory -containing a set of PEM encoded files containing rootCAs. _ca_dir_ can be -NULL. One or both of _ca_file_ and _ca_dir_ must be set. + -*NOTE:* Some TLS libraries send the full list of CAs added by this function -during the (D)TLS session setup handshakes. To stop this, either provide a -single CA using the _ca_file_ definition in _pki_key_ in _setup_data_ variable -when calling *coap_context_set_pki*(), or set _check_common_ca_ to 0 in -_setup_data_ variable. See *coap_encryption*(3). - *Function: coap_context_set_psk2()* The *coap_context_set_psk2*() function is used to configure the (D)TLS context @@ -203,7 +186,7 @@ definitions when setting up each resource. See *coap_resource*(3). RETURN VALUES ------------- -*coap_context_set_pki*(), *coap_context_set_pki_root_cas*() and +*coap_context_set_pki*() and *coap_context_set_psk2*() return 1 on success, 0 on failure. *coap_new_endpoint*() returns a newly created endpoint or diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index a4f9c2bf..c0e41bb6 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -131,6 +131,7 @@ typedef struct coap_gnutls_context_t { gnutls_datum_t alpn_proto; /* Will be "coap", but that is a const */ char *root_ca_file; char *root_ca_path; + int trust_store_defined; gnutls_priority_t priority_cache; } coap_gnutls_context_t; @@ -400,6 +401,30 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *c_context, return 1; } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_context_load_pki_trust_store(coap_context_t *c_context) { + coap_gnutls_context_t *g_context = + ((coap_gnutls_context_t *)c_context->dtls_context); + if (!g_context) { + coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment " + "not set up\n"); + return 0; + } + +#if (GNUTLS_VERSION_NUMBER >= 0x030020) + g_context->trust_store_defined = 1; + return 1; +#else + coap_log_warn("coap_context_set_pki_trust_store(): (D)TLS environment " + "not supported for GnuTLS < v3.0.20\n"); + return 0; +#endif +} + #if COAP_SERVER_SUPPORT /* * return 0 failed @@ -837,6 +862,8 @@ cert_verify_gnutls(gnutls_session_t g_session) { G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status), "gnutls_certificate_verify_peers"); + coap_dtls_log(COAP_LOG_DEBUG, "error %x cert '%s'\n", + status, cert_info.san_or_cn); if (status) { status &= ~(GNUTLS_CERT_INVALID); if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) { @@ -1130,7 +1157,7 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, if ((ret = gnutls_certificate_set_x509_key_file(*pki_credentials, key.key.define.public_cert.s_byte, key.key.define.private_key.s_byte, - GNUTLS_X509_FMT_PEM) < 0)) { + GNUTLS_X509_FMT_PEM)) < 0) { return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, COAP_DEFINE_FAIL_BAD, &key, role, ret); @@ -1332,7 +1359,7 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, /* * Configure the CA */ - if (setup_data->check_common_ca && key.key.define.ca.u_byte && + if (key.key.define.ca.u_byte && key.key.define.ca.u_byte[0]) { switch (key.key.define.ca_def) { case COAP_PKI_KEY_DEF_PEM: @@ -1418,6 +1445,12 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, } } +#if (GNUTLS_VERSION_NUMBER >= 0x030020) + if (g_context->trust_store_defined) { + G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials), + "gnutls_certificate_set_x509_system_trust"); + } +#endif if (g_context->root_ca_file) { ret = gnutls_certificate_set_x509_trust_file(*pki_credentials, g_context->root_ca_file, @@ -1436,11 +1469,13 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, } gnutls_certificate_send_x509_rdn_sequence(g_session, setup_data->check_common_ca ? 0 : 1); - if (!(g_context->psk_pki_enabled & IS_PKI)) { +#if (GNUTLS_VERSION_NUMBER >= 0x030020) + if (!(g_context->psk_pki_enabled & IS_PKI) && !g_context->trust_store_defined) { /* No PKI defined at all - still need a trust set up for 3.6.0 or later */ G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials), "gnutls_certificate_set_x509_system_trust"); } +#endif /* Verify Peer */ gnutls_certificate_set_verify_function(*pki_credentials, @@ -1786,6 +1821,21 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) { * This works providing COAP_PKI_KEY_PEM has a value of 0. */ coap_dtls_pki_t *setup_data = &g_context->setup_data; + + if (!(g_context->psk_pki_enabled & IS_PKI)) { + /* PKI not defined - set up some defaults */ + setup_data->verify_peer_cert = 1; + setup_data->check_common_ca = 0; + setup_data->allow_self_signed = 1; + setup_data->allow_expired_certs = 1; + setup_data->cert_chain_validation = 1; + setup_data->cert_chain_verify_depth = 2; + setup_data->check_cert_revocation = 1; + setup_data->allow_no_crl = 1; + setup_data->allow_expired_crl = 1; + setup_data->is_rpk_not_cert = 0; + setup_data->use_cid = 0; + } G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_env->g_session, g_context, setup_data, COAP_DTLS_ROLE_CLIENT), diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index f507d775..d60cc6a7 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -163,6 +163,7 @@ typedef struct coap_mbedtls_context_t { psk_sni_entry *psk_sni_entry_list; char *root_ca_file; char *root_ca_path; + int trust_store_defined; int psk_pki_enabled; } coap_mbedtls_context_t; @@ -744,9 +745,6 @@ setup_pki_credentials(mbedtls_x509_crt *cacert, * Configure the CA */ if ( -#if MBEDTLS_VERSION_NUMBER < 0x03060000 - setup_data->check_common_ca && -#endif /* MBEDTLS_VERSION_NUMBER < 0x03060000 */ key.key.define.ca.u_byte && key.key.define.ca.u_byte[0]) { switch (key.key.define.ca_def) { @@ -836,7 +834,7 @@ setup_pki_credentials(mbedtls_x509_crt *cacert, mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL); } if (m_context->root_ca_path) { - ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_path); + ret = mbedtls_x509_crt_parse_path(cacert, m_context->root_ca_path); if (ret < 0) { key.key.define.ca_def = COAP_PKI_KEY_DEF_PEM; return coap_dtls_define_issue(COAP_DEFINE_KEY_ROOT_CA, @@ -845,6 +843,42 @@ setup_pki_credentials(mbedtls_x509_crt *cacert, } mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL); } + if (m_context->trust_store_defined) { + /* Until Trust Store is implemented in MbedTLS */ + const char *trust_list[] = { + "/etc/ssl/ca-bundle.pem", + "/etc/ssl/certs/ca-certificates.crt", + "/etc/pki/tls/cert.pem", + "/usr/local/share/certs/ca-root-nss.crt", + "/etc/ssl/cert.pem" + }; + static const char *trust_file_found = NULL; + static int trust_file_done = 0; + unsigned int i; + + if (trust_file_found) { + ret = mbedtls_x509_crt_parse_file(cacert, trust_file_found); + if (ret >= 0) { + mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL); + } else { + coap_log_warn("Unable to load trusted root CAs (%s)\n", + trust_file_found); + } + } else if (!trust_file_done) { + trust_file_done = 1; + for (i = 0; i < sizeof(trust_list)/sizeof(trust_list[0]); i++) { + ret = mbedtls_x509_crt_parse_file(cacert, trust_list[i]); + if (ret >= 0) { + mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL); + trust_file_found = trust_list[i]; + break; + } + } + if (i == sizeof(trust_list)/sizeof(trust_list[0])) { + coap_log_warn("Unable to load trusted root CAs\n"); + } + } + } #else /* ! MBEDTLS_FS_IO */ (void)m_context; return coap_dtls_define_issue(COAP_DEFINE_KEY_ROOT_CA, @@ -1363,10 +1397,26 @@ setup_client_ssl_session(coap_session_t *c_session, * If neither PSK or PKI have been set up, use PKI basics. * This works providing COAP_PKI_KEY_PEM has a value of 0. */ + coap_dtls_pki_t *setup_data = &m_context->setup_data; + + if (!(m_context->psk_pki_enabled & IS_PKI)) { + /* PKI not defined - set up some defaults */ + setup_data->verify_peer_cert = 1; + setup_data->check_common_ca = 0; + setup_data->allow_self_signed = 1; + setup_data->allow_expired_certs = 1; + setup_data->cert_chain_validation = 1; + setup_data->cert_chain_verify_depth = 2; + setup_data->check_cert_revocation = 1; + setup_data->allow_no_crl = 1; + setup_data->allow_expired_crl = 1; + setup_data->is_rpk_not_cert = 0; + setup_data->use_cid = 0; + } mbedtls_ssl_conf_authmode(&m_env->conf, MBEDTLS_SSL_VERIFY_OPTIONAL); ret = setup_pki_credentials(&m_env->cacert, &m_env->public_cert, &m_env->private_key, m_env, m_context, - c_session, &m_context->setup_data, + c_session, setup_data, COAP_DTLS_ROLE_CLIENT); if (ret < 0) { coap_log_err("PKI setup failed\n"); @@ -2110,6 +2160,27 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *c_context, return 1; } +/* + * return 0 failed + * 1 passed + */ +int +coap_dtls_context_load_pki_trust_store(coap_context_t *c_context) { + coap_mbedtls_context_t *m_context = + ((coap_mbedtls_context_t *)c_context->dtls_context); + + if (!m_context) { + coap_log_warn("coap_context_load_pki_trust_store: (D)TLS environment " + "not set up\n"); + return 0; + } + m_context->trust_store_defined = 1; + + /* No proper support for this in MbedTLS at this point */ + return 1; +} + + int coap_dtls_context_check_keys_enabled(coap_context_t *c_context) { coap_mbedtls_context_t *m_context = diff --git a/src/coap_net.c b/src/coap_net.c index 35de7e1c..c9814eb5 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -454,6 +454,25 @@ coap_context_set_pki_root_cas_lkd(coap_context_t *ctx, return 0; } +COAP_API int +coap_context_load_pki_trust_store(coap_context_t *ctx) { + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_context_load_pki_trust_store_lkd(ctx); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_context_load_pki_trust_store_lkd(coap_context_t *ctx) { + if (coap_dtls_is_supported() || coap_tls_is_supported()) { + return coap_dtls_context_load_pki_trust_store(ctx); + } + return 0; +} + + void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds) { context->ping_timeout = seconds; diff --git a/src/coap_notls.c b/src/coap_notls.c index 1303a2d7..c8f4f740 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -120,6 +120,11 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, return 0; } +int +coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED) { + return 0; +} + #if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED, diff --git a/src/coap_openssl.c b/src/coap_openssl.c index e1aa0ab5..affcbce0 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -1669,12 +1669,22 @@ setup_pki_server(SSL_CTX *ctx, key.key.define.public_cert.u_byte[0]) { switch (key.key.define.public_cert_def) { case COAP_PKI_KEY_DEF_PEM: /* define public cert */ - if (!(SSL_CTX_use_certificate_file(ctx, - key.key.define.public_cert.s_byte, - SSL_FILETYPE_PEM))) { - return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, - COAP_DEFINE_FAIL_BAD, - &key, COAP_DTLS_ROLE_SERVER, 0); + if (key.key.define.public_cert.u_byte && + key.key.define.public_cert.u_byte[0]) { + if (!(SSL_use_certificate_file(ssl, + key.key.define.public_cert.s_byte, + SSL_FILETYPE_PEM))) { + return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, + COAP_DEFINE_FAIL_BAD, + &key, role, 0); + } + } else { + if (!SSL_use_certificate_chain_file(ssl, + key.key.define.public_cert.s_byte)) { + return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, + COAP_DEFINE_FAIL_BAD, + &key, role, 0); + } } break; case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */ @@ -1761,7 +1771,7 @@ setup_pki_server(SSL_CTX *ctx, /* * Configure the CA */ - if (setup_data->check_common_ca && key.key.define.ca.u_byte && + if (key.key.define.ca.u_byte && key.key.define.ca.u_byte[0]) { switch (key.key.define.ca_def) { case COAP_PKI_KEY_DEF_PEM: @@ -2107,12 +2117,23 @@ setup_pki_ssl(SSL *ssl, key.key.define.public_cert.u_byte[0]) { switch (key.key.define.public_cert_def) { case COAP_PKI_KEY_DEF_PEM: /* define public cert */ - if (!(SSL_use_certificate_file(ssl, - key.key.define.public_cert.s_byte, - SSL_FILETYPE_PEM))) { - return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, - COAP_DEFINE_FAIL_BAD, - &key, role, 0); + if (key.key.define.ca.u_byte && + key.key.define.ca.u_byte[0]) { + /* If CA is separately defined */ + if (!(SSL_use_certificate_file(ssl, + key.key.define.public_cert.s_byte, + SSL_FILETYPE_PEM))) { + return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, + COAP_DEFINE_FAIL_BAD, + &key, role, 0); + } + } else { + if (!SSL_use_certificate_chain_file(ssl, + key.key.define.public_cert.s_byte)) { + return coap_dtls_define_issue(COAP_DEFINE_KEY_PUBLIC, + COAP_DEFINE_FAIL_BAD, + &key, role, 0); + } } break; case COAP_PKI_KEY_DEF_PEM_BUF: /* define public cert */ @@ -2202,7 +2223,7 @@ setup_pki_ssl(SSL *ssl, /* * Configure the CA */ - if (setup_data->check_common_ca && key.key.define.ca.u_byte && + if (key.key.define.ca.u_byte && key.key.define.ca.u_byte[0]) { switch (key.key.define.ca_def) { case COAP_PKI_KEY_DEF_PEM: @@ -2376,6 +2397,8 @@ tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) { char *cn = get_san_or_cn_from_cert(x509); int keep_preverify_ok = preverify_ok; + coap_dtls_log(COAP_LOG_DEBUG, "depth %d error %x preverify %d cert '%s'\n", + depth, err, preverify_ok, cn); if (!preverify_ok) { switch (err) { case X509_V_ERR_CERT_NOT_YET_VALID: @@ -3231,7 +3254,7 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx, ((coap_openssl_context_t *)ctx->dtls_context); if (context->dtls.ctx) { if (!SSL_CTX_load_verify_locations(context->dtls.ctx, ca_file, ca_dir)) { - coap_log_warn("Unable to install root CAs (%s/%s)\n", + coap_log_warn("Unable to install root CAs (%s : %s)\n", ca_file ? ca_file : "NULL", ca_dir ? ca_dir : "NULL"); return 0; } @@ -3239,7 +3262,7 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx, #if !COAP_DISABLE_TCP if (context->tls.ctx) { if (!SSL_CTX_load_verify_locations(context->tls.ctx, ca_file, ca_dir)) { - coap_log_warn("Unable to install root CAs (%s/%s)\n", + coap_log_warn("Unable to install root CAs (%s : %s)\n", ca_file ? ca_file : "NULL", ca_dir ? ca_dir : "NULL"); return 0; } @@ -3248,6 +3271,34 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx, return 1; } +int +coap_dtls_context_load_pki_trust_store(coap_context_t *ctx) { +#if OPENSSL_VERSION_NUMBER >= 0x30000000L + coap_openssl_context_t *context = + ((coap_openssl_context_t *)ctx->dtls_context); + if (context->dtls.ctx) { + if (!SSL_CTX_set_default_verify_store(context->dtls.ctx)) { + coap_log_warn("Unable to load trusted root CAs\n"); + return 0; + } + } +#if !COAP_DISABLE_TCP + if (context->tls.ctx) { + if (!SSL_CTX_set_default_verify_store(context->tls.ctx)) { + coap_log_warn("Unable to load trusted root CAs\n"); + return 0; + } + } +#endif /* !COAP_DISABLE_TCP */ + return 1; +#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */ + (void)ctx; + coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment " + "not supported for OpenSSL < v3.0.0\n"); + return 0; +#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ +} + int coap_dtls_context_check_keys_enabled(coap_context_t *ctx) { coap_openssl_context_t *context = @@ -3393,8 +3444,28 @@ setup_client_ssl_session(coap_session_t *session, SSL *ssl coap_log_debug("CoAP Client restricted to (D)TLS1.2 with Identity Hint callback\n"); } } - if (context->psk_pki_enabled & IS_PKI) { + if ((context->psk_pki_enabled & IS_PKI) || + (context->psk_pki_enabled & (IS_PSK | IS_PKI)) == 0) { + /* + * If neither PSK or PKI have been set up, use PKI basics. + * This works providing COAP_PKI_KEY_PEM has a value of 0. + */ coap_dtls_pki_t *setup_data = &context->setup_data; + + if (!(context->psk_pki_enabled & IS_PKI)) { + /* PKI not defined - set up some defaults */ + setup_data->verify_peer_cert = 1; + setup_data->check_common_ca = 0; + setup_data->allow_self_signed = 1; + setup_data->allow_expired_certs = 1; + setup_data->cert_chain_validation = 1; + setup_data->cert_chain_verify_depth = 2; + setup_data->check_cert_revocation = 1; + setup_data->allow_no_crl = 1; + setup_data->allow_expired_crl = 1; + setup_data->is_rpk_not_cert = 0; + setup_data->use_cid = 0; + } if (!setup_pki_ssl(ssl, setup_data, COAP_DTLS_ROLE_CLIENT)) return 0; /* libcoap is managing (D)TLS connection based on setup_data options */ @@ -3523,6 +3594,7 @@ coap_dtls_send(coap_session_t *session, assert(ssl != NULL); session->dtls_event = -1; + ERR_clear_error(); r = SSL_write(ssl, data, (int)data_len); if (r <= 0) { @@ -3530,11 +3602,19 @@ coap_dtls_send(coap_session_t *session, if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { r = 0; } else { - coap_log_warn("coap_dtls_send: cannot send PDU\n"); if (err == SSL_ERROR_ZERO_RETURN) session->dtls_event = COAP_EVENT_DTLS_CLOSED; - else if (err == SSL_ERROR_SSL) + else if (err == SSL_ERROR_SSL) { + unsigned long e = ERR_get_error(); + + coap_log_info("***%s: coap_dtls_send: cannot send PDU: %d: %s\n", + coap_session_str(session), + ERR_GET_REASON(e), ERR_reason_error_string(e)); session->dtls_event = COAP_EVENT_DTLS_ERROR; + } else { + coap_log_info("***%s: coap_dtls_send: cannot send PDU: %d\n", + coap_session_str(session), err); + } r = -1; } } @@ -3644,6 +3724,9 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) coap_ssl_data *ssl_data; SSL *ssl = (SSL *)session->tls; int r; +#if OPENSSL_VERSION_NUMBER >= 0x30000000L + int retry = 0; +#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ assert(ssl != NULL); @@ -3660,6 +3743,10 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) ssl_data->pdu_len = (unsigned)data_len; session->dtls_event = -1; +#if OPENSSL_VERSION_NUMBER >= 0x30000000L +retry: +#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ + ERR_clear_error(); r = SSL_read(ssl, pdu, (int)sizeof(pdu)); if (r > 0) { coap_log_debug("* %s: dtls: recv %4d bytes\n", @@ -3679,8 +3766,25 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) } else { if (err == SSL_ERROR_ZERO_RETURN) /* Got a close notify alert from the remote side */ session->dtls_event = COAP_EVENT_DTLS_CLOSED; - else if (err == SSL_ERROR_SSL) + else if (err == SSL_ERROR_SSL) { + unsigned long e = ERR_get_error(); + +#if OPENSSL_VERSION_NUMBER >= 0x30000000L +#include + if (ERR_GET_REASON(e) == PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES && !retry) { + /* Loading trust store - first access causes a directory read error */ + retry = 1; + goto retry; + } +#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ + coap_log_info("***%s: coap_dtls_receive: cannot recv PDU: %d: %s\n", + coap_session_str(session), + ERR_GET_REASON(e), ERR_reason_error_string(e)); session->dtls_event = COAP_EVENT_DTLS_ERROR; + } else { + coap_log_info("***%s: coap_dtls_receive: cannot send PDU %d\n", + coap_session_str(session), err); + } r = -1; } if (session->dtls_event >= 0) { @@ -3958,12 +4062,19 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { } r = 0; } else { - coap_log_info("***%s: coap_tls_write: cannot send PDU\n", - coap_session_str(session)); if (err == SSL_ERROR_ZERO_RETURN) session->dtls_event = COAP_EVENT_DTLS_CLOSED; - else if (err == SSL_ERROR_SSL) + else if (err == SSL_ERROR_SSL) { + unsigned long e = ERR_get_error(); + + coap_log_info("***%s: coap_tls_write: cannot send PDU: %d: %s\n", + coap_session_str(session), + ERR_GET_REASON(e), ERR_reason_error_string(e)); session->dtls_event = COAP_EVENT_DTLS_ERROR; + } else { + coap_log_info("***%s: coap_tls_send: cannot send PDU: %d\n", + coap_session_str(session), err); + } r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { @@ -4039,8 +4150,17 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { } else { if (err == SSL_ERROR_ZERO_RETURN) /* Got a close notify alert from the remote side */ session->dtls_event = COAP_EVENT_DTLS_CLOSED; - else if (err == SSL_ERROR_SSL) + else if (err == SSL_ERROR_SSL) { + unsigned long e = ERR_get_error(); + + coap_log_info("***%s: coap_tls_read: cannot recv PDU: %d: %s\n", + coap_session_str(session), + ERR_GET_REASON(e), ERR_reason_error_string(e)); session->dtls_event = COAP_EVENT_DTLS_ERROR; + } else { + coap_log_info("***%s: coap_tls_read: cannot read PDU %d\n", + coap_session_str(session), err); + } r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { diff --git a/src/coap_session.c b/src/coap_session.c index 034c9d5e..79ce1da0 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -981,7 +981,7 @@ coap_handle_nack(coap_session_t *session, const coap_nack_reason_t reason, const coap_mid_t mid) { if (session->context->nack_handler) { - coap_bin_const_t token; + coap_bin_const_t token = {0, NULL}; if (sent) { coap_check_update_token(session, sent); diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index a4f72dc6..d8c6b11e 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -1549,6 +1549,11 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, return 0; } +int +coap_dtls_context_load_pki_trust_store(coap_context_t *ctx COAP_UNUSED) { + return 0; +} + #if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *coap_context, diff --git a/src/coap_wolfssl.c b/src/coap_wolfssl.c index e19030d6..aee215b0 100644 --- a/src/coap_wolfssl.c +++ b/src/coap_wolfssl.c @@ -120,6 +120,7 @@ typedef struct coap_wolfssl_context_t { int psk_pki_enabled; char *root_ca_file; char *root_ca_dir; + int trust_store_defined; } coap_wolfssl_context_t; typedef struct coap_ssl_data_t { @@ -932,13 +933,21 @@ setup_dtls_context(coap_wolfssl_context_t *w_context) { #ifdef WOLFSSL_DTLS_MTU wolfSSL_CTX_dtls_set_mtu(w_context->dtls.ctx, COAP_DEFAULT_MTU); #endif /* WOLFSSL_DTLS_MTU */ +#ifdef WOLFSSL_SYS_CA_CERTS + if (w_context->trust_store_defined) { + if (!wolfSSL_CTX_load_system_CA_certs(w_context->dtls.ctx)) { + coap_log_warn("Unable to load trusted root CAs\n"); + goto error; + } + } +#endif if (w_context->root_ca_file || w_context->root_ca_dir) { if (!wolfSSL_CTX_load_verify_locations_ex(w_context->dtls.ctx, w_context->root_ca_file, w_context->root_ca_dir, w_context->setup_data.allow_expired_certs ? WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) { - coap_log_warn("Unable to install root CAs (%s/%s)\n", + coap_log_warn("Unable to install root CAs (%s : %s)\n", w_context->root_ca_file ? w_context->root_ca_file : "NULL", w_context->root_ca_dir ? w_context->root_ca_dir : "NULL"); goto error; @@ -989,13 +998,21 @@ setup_tls_context(coap_wolfssl_context_t *w_context) { w_context->root_ca_dir, w_context->setup_data.allow_expired_certs ? WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY : 0)) { - coap_log_warn("Unable to install root CAs (%s/%s)\n", + coap_log_warn("Unable to install root CAs (%s : %s)\n", w_context->root_ca_file ? w_context->root_ca_file : "NULL", w_context->root_ca_dir ? w_context->root_ca_dir : "NULL"); goto error; } } /* Verify Peer */ +#ifdef WOLFSSL_SYS_CA_CERTS + if (w_context->trust_store_defined) { + if (!wolfSSL_CTX_load_system_CA_certs(w_context->tls.ctx)) { + coap_log_warn("Unable to load trusted root CAs\n"); + goto error; + } + } +#endif if (w_context->setup_data.verify_peer_cert) wolfSSL_CTX_set_verify(w_context->tls.ctx, WOLFSSL_VERIFY_PEER | @@ -1344,7 +1361,7 @@ setup_pki_ssl(WOLFSSL *ssl, /* * Configure the CA */ - if (setup_data->check_common_ca && key.key.define.ca.u_byte && + if (key.key.define.ca.u_byte && key.key.define.ca.u_byte[0]) { switch (key.key.define.ca_def) { case COAP_PKI_KEY_DEF_PEM: @@ -1839,6 +1856,25 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx, return 1; } +int +coap_dtls_context_load_pki_trust_store(coap_context_t *ctx) { + coap_wolfssl_context_t *w_context = + ((coap_wolfssl_context_t *)ctx->dtls_context); + + if (!w_context) { + coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment " + "not set up\n"); + return 0; + } +#ifdef WOLFSSL_SYS_CA_CERTS + w_context->trust_store_defined = 1; + return 1; +#else /* LIBWOLFSSL_VERSION_HEX < 0x05005002 */ + coap_log_warn("coap_context_set_pki_trust_store: (D)TLS environment " + "not supported for wolfSSL < v5.5.2 or –enable-sys-ca-certs not defined\n"); +#endif /* WOLFSSL_SYS_CA_CERTS */ +} + int coap_dtls_context_check_keys_enabled(coap_context_t *ctx) { coap_wolfssl_context_t *w_context = @@ -1846,7 +1882,6 @@ coap_dtls_context_check_keys_enabled(coap_context_t *ctx) { return w_context->psk_pki_enabled ? 1 : 0; } - void coap_dtls_free_context(void *handle) { coap_wolfssl_context_t *w_context = (coap_wolfssl_context_t *)handle;