coap-client: Support using CA or Root CAs, but not Cert files

By defining the CA file or Root CAs in the client, the server's certificate
can be validated by the options in coap_dtls_pki_t parameter even if the client
does not define a certificate.
[The server must be configured not to require a Client Cert]

examples/client.c:

Add in the support to the coap-client to allow any of Root CA, CA and Cert
to be defined to set up PKI checking.

src/coap_gnutls.c:
src/coap_openssl.c:

Remove the checks in the OpenSSL and GnuTLS underlying support that previously
used to enforce that both CA and Cert had to be set in the client.

Correct some of the PKI setup error messages to relect whether the failure is
on the client or the server.

include/coap2/coap_dtls.h:
src/coap_gnutls.c:
src/coap_notls.c:
src/coap_openssl.c:
src/coap_tinydtls.c:

Convert COAP_DTLS_ROLE_SERVER and COAP_DTLS_ROLE_CLIENT to enum
coap_dtls_role_t and use as appropriate to differentiate between the roles
in the different DTLS libraries
This commit is contained in:
Jon Shallow committed 2019-02-15 18:00:52 +00:00
1 parent 459e43a48c
commit 829145cbba
6 files changed
+109 -56

No files matched your search

+22 -16
View File
@@ -1074,17 +1074,31 @@ verify_cn_callback(const char *cn,
}
static coap_dtls_pki_t *
setup_pki(void) {
setup_pki(coap_context_t *ctx) {
static coap_dtls_pki_t dtls_pki;
static char client_sni[256];
/* If general root CAs are defined */
if (root_ca_file) {
struct stat stbuf;
if ((stat(root_ca_file, &stbuf) == 0) && S_ISDIR(stbuf.st_mode)) {
coap_context_set_pki_root_cas(ctx, NULL, root_ca_file);
} else {
coap_context_set_pki_root_cas(ctx, root_ca_file, NULL);
}
}
memset (&dtls_pki, 0, sizeof(dtls_pki));
dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION;
if (ca_file) {
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 = 1;
dtls_pki.require_peer_cert = 1;
@@ -1130,16 +1144,6 @@ get_session(
) {
coap_session_t *session = NULL;
/* If general root CAs are defined */
if (root_ca_file) {
struct stat stbuf;
if ((stat(root_ca_file, &stbuf) == 0) && S_ISDIR(stbuf.st_mode)) {
coap_context_set_pki_root_cas(ctx, NULL, root_ca_file);
} else {
coap_context_set_pki_root_cas(ctx, root_ca_file, NULL);
}
}
if ( local_addr ) {
int s;
struct addrinfo hints;
@@ -1163,8 +1167,9 @@ get_session(
coap_address_init( &bind_addr );
bind_addr.size = rp->ai_addrlen;
memcpy( &bind_addr.addr, rp->ai_addr, rp->ai_addrlen );
if (cert_file && (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS)) {
coap_dtls_pki_t *dtls_pki = setup_pki();
if ((root_ca_file || ca_file || cert_file) &&
(proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS)) {
coap_dtls_pki_t *dtls_pki = setup_pki(ctx);
session = coap_new_client_session_pki(ctx, &bind_addr, dst, proto, dtls_pki);
}
else if ((identity || key) &&
@@ -1181,8 +1186,9 @@ get_session(
}
freeaddrinfo( result );
} else {
if (cert_file && (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS)) {
coap_dtls_pki_t *dtls_pki = setup_pki();
if ((root_ca_file || ca_file || cert_file) &&
(proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS)) {
coap_dtls_pki_t *dtls_pki = setup_pki(ctx);
session = coap_new_client_session_pki(ctx, NULL, dst, proto, dtls_pki);
}
else if ((identity || key) &&
+6 -4
View File
@@ -261,8 +261,10 @@ typedef struct coap_dtls_pki_t {
void *
coap_dtls_new_context(struct coap_context_t *coap_context);
#define COAP_DTLS_ROLE_CLIENT 0 /**< Internal function invoked for client */
#define COAP_DTLS_ROLE_SERVER 1 /**< Internal function invoked for server */
typedef enum coap_dtls_role_t {
COAP_DTLS_ROLE_CLIENT, /**< Internal function invoked for client */
COAP_DTLS_ROLE_SERVER /**< Internal function invoked for server */
} coap_dtls_role_t;
/**
* Set the DTLS context's default PSK information.
@@ -288,7 +290,7 @@ coap_dtls_new_context(struct coap_context_t *coap_context);
int
coap_dtls_context_set_psk(struct coap_context_t *coap_context,
const char *identity_hint,
int role);
coap_dtls_role_t role);
/**
* Set the DTLS context's default server PKI information.
@@ -312,7 +314,7 @@ coap_dtls_context_set_psk(struct coap_context_t *coap_context,
int
coap_dtls_context_set_pki(struct coap_context_t *coap_context,
coap_dtls_pki_t *setup_data,
int role);
coap_dtls_role_t role);
/**
* Set the dtls context's default Root CA information for a client or server.
+42 -13
View File
@@ -187,7 +187,7 @@ coap_gnutls_log_func(int level, const char* text)
int
coap_dtls_context_set_pki(coap_context_t *c_context,
coap_dtls_pki_t* setup_data,
int role UNUSED)
coap_dtls_role_t role UNUSED)
{
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
@@ -252,7 +252,7 @@ coap_dtls_context_set_pki_root_cas(struct coap_context_t *c_context,
int
coap_dtls_context_set_psk(coap_context_t *c_context,
const char *identity_hint UNUSED,
int role UNUSED
coap_dtls_role_t role UNUSED
) {
coap_gnutls_context_t *g_context =
((coap_gnutls_context_t *)c_context->dtls_context);
@@ -605,7 +605,7 @@ static int cert_verify_callback_gnutls(gnutls_session_t g_session)
static int
setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
coap_gnutls_context_t *g_context,
coap_dtls_pki_t *setup_data)
coap_dtls_pki_t *setup_data, coap_dtls_role_t role)
{
int ret;
@@ -624,14 +624,19 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
GNUTLS_X509_FMT_PEM),
"gnutls_certificate_set_x509_key_file");
}
else {
else if (role == COAP_DTLS_ROLE_SERVER) {
coap_log(LOG_ERR,
"***setup_pki: (D)TLS: No Client Certificate + Private "
"Key defined\n");
"***setup_pki: (D)TLS: No %s Certificate + Private "
"Key defined\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
if (setup_data->pki_key.key.pem.ca_file &&
setup_data->pki_key.key.pem.ca_file[0]) {
if (!*pki_credentials) {
G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
"gnutls_certificate_allocate_credentials");
}
G_CHECK(gnutls_certificate_set_x509_trust_file(*pki_credentials,
setup_data->pki_key.key.pem.ca_file,
GNUTLS_X509_FMT_PEM),
@@ -660,10 +665,11 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
GNUTLS_X509_FMT_DER),
"gnutls_certificate_set_x509_key_mem");
}
else {
else if (role == COAP_DTLS_ROLE_SERVER) {
coap_log(LOG_ERR,
"***setup_pki: (D)TLS: No Client Certificate + Private "
"Key defined\n");
"***setup_pki: (D)TLS: No %s Certificate + Private "
"Key defined\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
if (setup_data->pki_key.key.asn1.ca_cert &&
@@ -674,6 +680,10 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
memcpy(&ca_cert.data, &setup_data->pki_key.key.asn1.ca_cert,
sizeof(ca_cert.data));
ca_cert.size = setup_data->pki_key.key.asn1.ca_cert_len;
if (!*pki_credentials) {
G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
"gnutls_certificate_allocate_credentials");
}
G_CHECK(gnutls_certificate_set_x509_trust_mem(*pki_credentials,
&ca_cert,
GNUTLS_X509_FMT_DER),
@@ -688,6 +698,10 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
}
if (g_context->root_ca_file) {
if (!*pki_credentials) {
G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
"gnutls_certificate_allocate_credentials");
}
G_CHECK(gnutls_certificate_set_x509_trust_file(*pki_credentials,
g_context->root_ca_file,
GNUTLS_X509_FMT_PEM),
@@ -695,6 +709,10 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials,
}
if (g_context->root_ca_path) {
#if (GNUTLS_VERSION_NUMBER >= 0x030306)
if (!*pki_credentials) {
G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials),
"gnutls_certificate_allocate_credentials");
}
G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials,
g_context->root_ca_path,
GNUTLS_X509_FMT_PEM),
@@ -819,7 +837,7 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session)
if ((ret = setup_pki_credentials(
&g_context->sni_entry_list[i].pki_credentials,
g_context,
&sni_setup_data)) < 0) {
&sni_setup_data, COAP_DTLS_ROLE_CLIENT)) < 0) {
int keep_ret = ret;
G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL,
GNUTLS_A_BAD_CERTIFICATE));
@@ -890,7 +908,7 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env)
if (g_context->psk_pki_enabled & IS_PKI) {
coap_dtls_pki_t *setup_data = &g_context->setup_data;
G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_context,
setup_data),
setup_data, COAP_DTLS_ROLE_CLIENT),
"setup_pki_credentials");
G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE,
@@ -986,7 +1004,7 @@ setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env)
if (g_context->psk_pki_enabled & IS_PKI) {
coap_dtls_pki_t *setup_data = &g_context->setup_data;
G_CHECK(setup_pki_credentials(&g_env->pki_credentials, g_context,
setup_data),
setup_data, COAP_DTLS_ROLE_SERVER),
"setup_pki_credentials");
if (setup_data->require_peer_cert) {
@@ -1228,6 +1246,7 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
"Insufficient credentials provided.\n");
ret = -1;
break;
case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET:
case GNUTLS_E_FATAL_ALERT_RECEIVED:
log_last_alert(g_env->g_session);
c_session->dtls_event = COAP_EVENT_DTLS_CLOSED;
@@ -1238,6 +1257,16 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
c_session->dtls_event = COAP_EVENT_DTLS_ERROR;
ret = 0;
break;
case GNUTLS_E_NO_CERTIFICATE_FOUND:
coap_log(LOG_WARNING,
"do_gnutls_handshake: session establish "
"returned %d: '%s'\n",
ret, gnutls_strerror(ret));
G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL,
GNUTLS_A_BAD_CERTIFICATE));
c_session->dtls_event = COAP_EVENT_DTLS_CLOSED;
ret = -1;
break;
case GNUTLS_E_DECRYPTION_FAILED:
coap_log(LOG_WARNING,
"do_gnutls_handshake: session establish "
@@ -1249,7 +1278,7 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) {
ret = -1;
break;
case GNUTLS_E_UNKNOWN_CIPHER_SUITE:
/* fall through */
/* fall through */
case GNUTLS_E_TIMEDOUT:
c_session->dtls_event = COAP_EVENT_DTLS_CLOSED;
ret = -1;
+2 -2
View File
@@ -40,7 +40,7 @@ coap_get_tls_library_version(void) {
int
coap_dtls_context_set_pki(coap_context_t *ctx UNUSED,
coap_dtls_pki_t* setup_data UNUSED,
int server UNUSED
coap_dtls_role_t role UNUSED
) {
return 0;
}
@@ -56,7 +56,7 @@ coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx UNUSED,
int
coap_dtls_context_set_psk(coap_context_t *ctx UNUSED,
const char *hint UNUSED,
int server UNUSED
coap_dtls_role_t role UNUSED
) {
return 0;
}
+35 -19
View File
@@ -559,7 +559,7 @@ error:
int
coap_dtls_context_set_psk(coap_context_t *ctx,
const char *identity_hint,
int role
coap_dtls_role_t role
) {
coap_openssl_context_t *context = ((coap_openssl_context_t *)ctx->dtls_context);
BIO *bio;
@@ -814,7 +814,7 @@ setup_pki_server(SSL_CTX *ctx,
static int
setup_pki_ssl(SSL *ssl,
coap_dtls_pki_t* setup_data, int isserver
coap_dtls_pki_t* setup_data, coap_dtls_role_t role
) {
switch (setup_data->pki_key.key_type) {
case COAP_PKI_KEY_PEM:
@@ -825,14 +825,18 @@ setup_pki_ssl(SSL *ssl,
SSL_FILETYPE_PEM))) {
coap_log(LOG_WARNING,
"*** setup_pki: (D)TLS: %s: Unable to configure "
"Client Certificate\n",
setup_data->pki_key.key.pem.public_cert);
"%s Certificate\n",
setup_data->pki_key.key.pem.public_cert,
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
}
else {
else if (role == COAP_DTLS_ROLE_SERVER ||
(setup_data->pki_key.key.pem.private_key &&
setup_data->pki_key.key.pem.private_key[0])) {
coap_log(LOG_ERR,
"*** setup_pki: (D)TLS: No Client Certificate defined\n");
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
if (setup_data->pki_key.key.pem.private_key &&
@@ -847,9 +851,12 @@ setup_pki_ssl(SSL *ssl,
return 0;
}
}
else {
else if (role == COAP_DTLS_ROLE_SERVER ||
(setup_data->pki_key.key.pem.public_cert &&
setup_data->pki_key.key.pem.public_cert[0])) {
coap_log(LOG_ERR,
"*** setup_pki: (D)TLS: No Client Private Key defined\n");
"*** setup_pki: (D)TLS: No %s Private Key defined\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
if (setup_data->pki_key.key.pem.ca_file &&
@@ -860,7 +867,7 @@ setup_pki_ssl(SSL *ssl,
char *rw_var = NULL;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
if (isserver) {
if (role == COAP_DTLS_ROLE_SERVER) {
STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(setup_data->pki_key.key.pem.ca_file);
if (cert_names != NULL)
@@ -868,8 +875,9 @@ setup_pki_ssl(SSL *ssl,
else {
coap_log(LOG_WARNING,
"*** setup_pki: (D)TLS: %s: Unable to configure "
"client CA File\n",
setup_data->pki_key.key.pem.ca_file);
"%s CA File\n",
setup_data->pki_key.key.pem.ca_file,
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
}
@@ -902,14 +910,18 @@ setup_pki_ssl(SSL *ssl,
setup_data->pki_key.key.asn1.public_cert_len))) {
coap_log(LOG_WARNING,
"*** setup_pki: (D)TLS: %s: Unable to configure "
"Client Certificate\n",
"%s Certificate\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client",
"ASN1");
return 0;
}
}
else {
else if (role == COAP_DTLS_ROLE_SERVER ||
(setup_data->pki_key.key.asn1.private_key &&
setup_data->pki_key.key.asn1.private_key[0])) {
coap_log(LOG_ERR,
"*** setup_pki: (D)TLS: No Client Certificate defined\n");
"*** setup_pki: (D)TLS: No %s Certificate defined\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
if (setup_data->pki_key.key.asn1.private_key &&
@@ -920,14 +932,18 @@ setup_pki_ssl(SSL *ssl,
setup_data->pki_key.key.asn1.private_key_len))) {
coap_log(LOG_WARNING,
"*** setup_pki: (D)TLS: %s: Unable to configure "
"Client Private Key\n",
"%s Private Key\n",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client",
"ASN1");
return 0;
}
}
else {
else if (role == COAP_DTLS_ROLE_SERVER ||
(setup_data->pki_key.key.asn1.public_cert &&
setup_data->pki_key.key.asn1.public_cert_len > 0)) {
coap_log(LOG_ERR,
"*** setup_pki: (D)TLS: No Client Private Key defined\n");
"*** setup_pki: (D)TLS: No %s Private Key defined",
role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client");
return 0;
}
if (setup_data->pki_key.key.asn1.ca_cert &&
@@ -938,7 +954,7 @@ setup_pki_ssl(SSL *ssl,
X509_STORE *st;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
if (isserver) {
if (role == COAP_DTLS_ROLE_SERVER) {
if (!x509 || !SSL_add_client_CA(ssl, x509)) {
coap_log(LOG_WARNING,
"*** setup_pki: (D)TLS: %s: Unable to configure "
@@ -1504,7 +1520,7 @@ is_x509:
int
coap_dtls_context_set_pki(coap_context_t *ctx,
coap_dtls_pki_t *setup_data,
int role
coap_dtls_role_t role
) {
coap_openssl_context_t *context =
((coap_openssl_context_t *)ctx->dtls_context);
+2 -2
View File
@@ -490,7 +490,7 @@ coap_get_tls_library_version(void) {
int
coap_dtls_context_set_pki(coap_context_t *ctx UNUSED,
coap_dtls_pki_t* setup_data UNUSED,
int server UNUSED
coap_dtls_role_t role UNUSED
) {
return 0;
}
@@ -506,7 +506,7 @@ coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx UNUSED,
int
coap_dtls_context_set_psk(coap_context_t *ctx UNUSED,
const char *hint UNUSED,
int server UNUSED
coap_dtls_role_t role UNUSED
) {
return 1;
}