diff --git a/examples/client.c b/examples/client.c index c12a0bf0..3f688ffa 100644 --- a/examples/client.c +++ b/examples/client.c @@ -83,6 +83,10 @@ 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 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 *ca_mem = NULL; /* CA for cert checking in PEM_BUF */ +static size_t cert_mem_len = 0; +static size_t ca_mem_len = 0; typedef struct ih_def_t { char* hint_match; @@ -1308,6 +1312,37 @@ static int cmdline_read_hint_check(const char *arg) { return valid_ihs.count > 0; } +static uint8_t *read_file_mem(const char* filename, size_t *length) { + FILE *f; + uint8_t *buf; + struct stat statbuf; + + *length = 0; + if (!filename || !(f = fopen(filename, "r"))) + return NULL; + + if (fstat(fileno(f), &statbuf) == -1) { + fclose(f); + return NULL; + } + + buf = coap_malloc(statbuf.st_size+1); + if (!buf) { + fclose(f); + return NULL; + } + + if (fread(buf, 1, statbuf.st_size, f) != (size_t)statbuf.st_size) { + fclose(f); + coap_free(buf); + return NULL; + } + buf[statbuf.st_size] = '\000'; + *length = (size_t)(statbuf.st_size + 1); + fclose(f); + return buf; +} + static int verify_cn_callback(const char *cn, const uint8_t *asn1_public_cert UNUSED_PARAM, @@ -1397,10 +1432,12 @@ setup_pki(coap_context_t *ctx) { dtls_pki.validate_sni_call_back = NULL; dtls_pki.sni_call_back_arg = NULL; } - if (uri.host.length && memcmp(uri.host.s, "::1", 3) != 0) + if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) != 0) || + (uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) != 0)) memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1)); else memcpy(client_sni, "localhost", 9); + dtls_pki.is_rpk_not_cert = is_rpk_not_cert; dtls_pki.client_sni = client_sni; if ((key_file && strncasecmp (key_file, "pkcs11:", 7) == 0) || @@ -1413,12 +1450,24 @@ setup_pki(coap_context_t *ctx) { dtls_pki.pki_key.key.pkcs11.ca = ca_file; dtls_pki.pki_key.key.pkcs11.user_pin = pkcs11_pin; } - else { + else if (!is_rpk_not_cert) { dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM; dtls_pki.pki_key.key.pem.public_cert = cert_file; dtls_pki.pki_key.key.pem.private_key = key_file ? key_file : cert_file; dtls_pki.pki_key.key.pem.ca_file = ca_file; } + else { + /* Map file into memory */ + ca_mem = read_file_mem(ca_file, &ca_mem_len); + cert_mem = read_file_mem(cert_file, &cert_mem_len); + dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM_BUF; + dtls_pki.pki_key.key.pem_buf.ca_cert = ca_mem; + dtls_pki.pki_key.key.pem_buf.public_cert = cert_mem; + dtls_pki.pki_key.key.pem_buf.private_key = cert_mem; + dtls_pki.pki_key.key.pem_buf.ca_cert_len = ca_mem_len; + dtls_pki.pki_key.key.pem_buf.public_cert_len = cert_mem_len; + dtls_pki.pki_key.key.pem_buf.private_key_len = cert_mem_len; + } return &dtls_pki; } @@ -1859,6 +1908,10 @@ main(int argc, char **argv) { finish: + if (ca_mem) + coap_free(ca_mem); + if (cert_mem) + coap_free(cert_mem); for (i = 0; i < valid_ihs.count; i++) { free(valid_ihs.ih_list[i].hint_match); coap_delete_bin_const(valid_ihs.ih_list[i].new_identity); diff --git a/examples/coap-server.c b/examples/coap-server.c index 702b5bfd..c806c987 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1011,7 +1011,7 @@ static uint8_t *read_file_mem(const char* file, size_t *length) { return NULL; } - buf = malloc(statbuf.st_size+1); + buf = coap_malloc(statbuf.st_size+1); if (!buf) { fclose(f); return NULL; @@ -1019,7 +1019,7 @@ static uint8_t *read_file_mem(const char* file, size_t *length) { if (fread(buf, 1, statbuf.st_size, f) != (size_t)statbuf.st_size) { fclose(f); - free(buf); + coap_free(buf); return NULL; } buf[statbuf.st_size] = '\000'; @@ -1196,25 +1196,24 @@ fill_keystore(coap_context_t *ctx) { } dtls_pki.require_peer_cert = require_peer_cert; dtls_pki.is_rpk_not_cert = is_rpk_not_cert; - if (!use_pem_buf) { - if ((key_file && strncasecmp (key_file, "pkcs11:", 7) == 0) || - (cert_file && strncasecmp (cert_file, "pkcs11:", 7) == 0) || - (ca_file && strncasecmp (ca_file, "pkcs11:", 7) == 0)) { - dtls_pki.pki_key.key_type = COAP_PKI_KEY_PKCS11; - dtls_pki.pki_key.key.pkcs11.public_cert = cert_file; - dtls_pki.pki_key.key.pkcs11.private_key = key_file ? - key_file : cert_file; - dtls_pki.pki_key.key.pkcs11.ca = ca_file; - dtls_pki.pki_key.key.pkcs11.user_pin = pkcs11_pin; - } - else { - dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM; - dtls_pki.pki_key.key.pem.public_cert = cert_file; - dtls_pki.pki_key.key.pem.private_key = key_file ? key_file : cert_file; - dtls_pki.pki_key.key.pem.ca_file = ca_file; - } + if ((key_file && strncasecmp (key_file, "pkcs11:", 7) == 0) || + (cert_file && strncasecmp (cert_file, "pkcs11:", 7) == 0) || + (ca_file && strncasecmp (ca_file, "pkcs11:", 7) == 0)) { + dtls_pki.pki_key.key_type = COAP_PKI_KEY_PKCS11; + dtls_pki.pki_key.key.pkcs11.public_cert = cert_file; + dtls_pki.pki_key.key.pkcs11.private_key = key_file ? + key_file : cert_file; + dtls_pki.pki_key.key.pkcs11.ca = ca_file; + dtls_pki.pki_key.key.pkcs11.user_pin = pkcs11_pin; + } + else if (!use_pem_buf && !is_rpk_not_cert) { + dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM; + dtls_pki.pki_key.key.pem.public_cert = cert_file; + dtls_pki.pki_key.key.pem.private_key = key_file ? key_file : cert_file; + dtls_pki.pki_key.key.pem.ca_file = ca_file; } else { + /* Map file into memory */ ca_mem = read_file_mem(ca_file, &ca_mem_len); cert_mem = read_file_mem(cert_file, &cert_mem_len); dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM_BUF; @@ -1863,9 +1862,9 @@ main(int argc, char **argv) { } if (ca_mem) - free(ca_mem); + coap_free(ca_mem); if (cert_mem) - free(cert_mem); + coap_free(cert_mem); for (i = 0; i < valid_psk_snis.count; i++) { free(valid_psk_snis.psk_sni_list[i].sni_match); coap_delete_bin_const(valid_psk_snis.psk_sni_list[i].new_hint); diff --git a/include/coap2/coap_dtls.h b/include/coap2/coap_dtls.h index 591a41a6..148083b2 100644 --- a/include/coap2/coap_dtls.h +++ b/include/coap2/coap_dtls.h @@ -105,7 +105,7 @@ typedef int (*coap_dtls_security_setup_t)(void* tls_session, * but the application needs to check that the CN is allowed. * CN is the SubjectAltName in the cert, if not present, then the leftmost * Common Name (CN) component of the subject name. - * NOTE: If using RPK, then the Public Key does not contain a CN, but the + * NOTE: If using RPK, then the Public Key does not contain a CN, but the * content of COAP_DTLS_RPK_CERT_CN is presented for the @p cn parameter. * * @param cn The determined CN from the certificate @@ -164,11 +164,8 @@ typedef enum coap_pki_key_t { */ typedef struct coap_pki_key_pem_t { const char *ca_file; /**< File location of Common CA in PEM format */ - const char *public_cert; /**< File location of Public Cert, or Public - Key if RPK, in PEM format */ - const char *private_key; /**< File location of Private Key in PEM format. - If RPK and 'EC PRIVATE KEY' this can be used - for both the public_cert and private_key */ + const char *public_cert; /**< File location of Public Cert */ + const char *private_key; /**< File location of Private Key in PEM format */ } coap_pki_key_pem_t; /** @@ -183,7 +180,9 @@ typedef struct coap_pki_key_pem_t { typedef struct coap_pki_key_pem_buf_t { const uint8_t *ca_cert; /**< PEM buffer Common CA Cert */ const uint8_t *public_cert; /**< PEM buffer Public Cert, or Public Key if RPK */ - const uint8_t *private_key; /**< PEM buffer Private Key */ + const uint8_t *private_key; /**< PEM buffer Private Key + If RPK and 'EC PRIVATE KEY' this can be used + for both the public_cert and private_key */ size_t ca_cert_len; /**< PEM buffer CA Cert length */ size_t public_cert_len; /**< PEM buffer Public Cert length */ size_t private_key_len; /**< PEM buffer Private Key length */ @@ -263,9 +262,11 @@ typedef struct coap_dtls_pki_t { uint8_t check_cert_revocation; /**< 1 if revocation checks wanted */ uint8_t allow_no_crl; /**< 1 ignore if CRL not there */ uint8_t allow_expired_crl; /**< 1 if expired crl is allowed */ - uint8_t allow_bad_md_hash; /**< 1 if unsupported MD hashes are allowed */ - uint8_t allow_short_rsa_length; /**< 1 if small RSA keysizes are allowed */ - uint8_t is_rpk_not_cert; /**< 1 is RPK instead of Public Certificate */ + uint8_t allow_bad_md_hash; /**< 1 if unsupported MD hashes are allowed */ + uint8_t allow_short_rsa_length; /**< 1 if small RSA keysizes are allowed */ + uint8_t is_rpk_not_cert; /**< 1 is RPK instead of Public Certificate. + * If set, PKI key format type cannot be + * COAP_PKI_KEY_PEM */ uint8_t reserved[3]; /**< Reserved - must be set to 0 for future compatibility */ /* Size of 3 chosen to align to next diff --git a/man/coap_encryption.txt.in b/man/coap_encryption.txt.in index bbc4fdef..1205bc60 100644 --- a/man/coap_encryption.txt.in +++ b/man/coap_encryption.txt.in @@ -427,7 +427,9 @@ typedef struct coap_dtls_pki_t { uint8_t allow_expired_crl; /* 1 if expired crl is allowed */ uint8_t allow_bad_md_hash; /* 1 if unsupported MD hashes are allowed */ uint8_t allow_short_rsa_length; /* 1 if small RSA keysizes are allowed */ - uint8_t is_rpk_not_cert; /* 1 if RPK instead of Public Certificate */ + uint8_t is_rpk_not_cert; /* 1 is RPK instead of Public Certificate. + * If set, PKI key format type cannot be + * COAP_PKI_KEY_PEM */ uint8_t reserved[3]; /* Reserved - must be set to 0 for future compatibility */ @@ -522,6 +524,7 @@ definition to be valid, else 0. *allow_short_rsa_length* Set to 1 if small RSA keysizes are allowed, else 0. *is_rpk_not_cert* Set to 1 if the Certificate is actually a Raw Public Key. +If set, PKI key format type cannot be COAP_PKI_KEY_PEM. *SECTION: PKI/RPK: coap_dtls_pki_t: Reserved* @@ -539,7 +542,7 @@ these reserved definitions. * but the application needs to check that the CN is allowed. * CN is the SubjectAltName in the cert, if not present, then the leftmost * Common Name (CN) component of the subject name. - * NOTE: If using RPK, then the Public Key does not contain a CN, but the + * NOTE: If using RPK, then the Public Key does not contain a CN, but the * content of COAP_DTLS_RPK_CERT_CN is presented for the @p cn parameter. * * @param cn The determined CN from the certificate @@ -579,7 +582,8 @@ typedef struct coap_dtls_key_t { union { coap_pki_key_pem_t pem; /* for PEM file keys */ coap_pki_key_pem_buf_t pem_buf; /* for PEM memory keys */ - coap_pki_key_asn1_t asn1; /* for ASN.1 (DER) file keys */ + coap_pki_key_asn1_t asn1; /* for ASN.1 (DER) memory keys */ + coap_pki_key_pkcs11_t pkcs11; /* for PKCS11 keys */ } key; } coap_dtls_key_t; @@ -662,16 +666,13 @@ typedef enum coap_pki_key_t { This can be COAP_PKI_KEY_PEM, COAP_PKI_KEY_PEM_BUF, COAP_PKI_KEY_ASN1 or COAP_PKI_KEY_PKCS11. -*SECTION: PKI/RPK: coap_dtls_pki_t: PEM Key Definitions* +*SECTION: PKI: coap_dtls_pki_t: PEM Key Definitions* [source, c] ---- typedef struct coap_pki_key_pem_t { const char *ca_file; /* File location of Common CA in PEM format */ - const char *public_cert; /* File location of Public Cert, or Public - Key if RPK, in PEM format */ - const char *private_key; /* File location of Private Key in PEM format. - If RPK and 'EC PRIVATE KEY' this can be used - for both the public_cert and private_key */ + const char *public_cert; /* File location of Public Cert */ + const char *private_key; /* File location of Private Key in PEM format */ } coap_pki_key_pem_t; ---- @@ -681,8 +682,8 @@ public certificate) as this is passed from the server to the client when requesting the client's certificate. This certificate is also added into the valid root CAs list if not already present. -*key.pem.public_cert* points to the public certificate (or public key if RPK) -location on disk which will be in PEM format. +*key.pem.public_cert* points to the public certificate location on disk which +will be in PEM format. *key.pem.private_key* points to the private key location on disk which will be in PEM format. This file cannot be password protected. @@ -694,6 +695,8 @@ typedef struct coap_pki_key_pem_buf_t { const uint8_t *ca_cert; /* PEM buffer Common CA Cert */ const uint8_t *public_cert; /* PEM buffer Public Cert, or Public Key if RPK */ const uint8_t *private_key; /* PEM buffer Private Key */ + If RPK and 'EC PRIVATE KEY' this can be used + for both the public_cert and private_key */ size_t ca_cert_len; /* PEM buffer CA Cert length */ size_t public_cert_len; /* PEM buffer Public Cert length */ size_t private_key_len; /* PEM buffer Private Key length */ @@ -714,7 +717,8 @@ RPK) location in memory which will be in PEM format. *key.pem_buf.public_cert_len* is the length of the public certificate. *key.pem_buf.private_key* points to the private key location in memory which -will be in PEM format. This data cannot be password protected. +will be in PEM format. This data cannot be password protected. If RPK and +'EC PRIVATE KEY' this can be used for both the public_cert and private_key. *key.pem_buf.private_key* is the length of the private key. diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index 2bb400bc..9dd73ca8 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -576,7 +576,7 @@ typedef struct { /* * return Type of certificate and SAN or CN if appropriate derived from - * certificate. COAP_GNUTLS_CRT_ERROR if failure. + * certificate. GNUTLS_CRT_UNKNOWN if failure. */ static gnutls_certificate_type_t get_san_or_cn(gnutls_session_t g_session, coap_gnutls_certificate_info_t *cert_info) @@ -674,7 +674,7 @@ static int cert_verify_gnutls(gnutls_session_t g_session) coap_gnutls_certificate_info_t cert_info; gnutls_certificate_type_t cert_type; - cert_info.san_or_cn = NULL; + memset(&cert_info, 0, sizeof(cert_info)); G_CHECK(gnutls_certificate_verify_peers(g_session, NULL, 0, &status), "gnutls_certificate_verify_peers"); @@ -978,75 +978,9 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, setup_data->pki_key.key.pem.private_key && setup_data->pki_key.key.pem.private_key[0]) { if (setup_data->is_rpk_not_cert) { -#if (GNUTLS_VERSION_NUMBER >= 0x030606) - FILE *f = fopen(setup_data->pki_key.key.pem.private_key, "r"); - uint8_t *buf; - struct stat statbuf; - int have_done_key = 0; - - if (f) { - if (fstat(fileno(f), &statbuf) == -1) { - fclose(f); - goto bad_file_pem; - } - - buf = gnutls_malloc(statbuf.st_size+1); - if (!buf) { - fclose(f); - goto bad_file_pem; - } - - if (fread(buf, 1, statbuf.st_size, f) != (size_t)statbuf.st_size) { - fclose(f); - gnutls_free(buf); - goto bad_file_pem; - } - buf[statbuf.st_size] = '\000'; - fclose(f); - - if (strstr ((char*)buf, "-----BEGIN EC PRIVATE KEY-----")) { - gnutls_datum_t der_private; - gnutls_datum_t key; - - key.data = buf; - key.size = statbuf.st_size; - if (gnutls_pem_base64_decode2("EC PRIVATE KEY", &key, - &der_private) == 0) { - gnutls_datum_t *spki = get_asn1_spki(der_private.data, - der_private.size); - - if (spki) { - ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials, - spki, - &der_private, - GNUTLS_X509_FMT_DER, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, 0); - if (ret >= 0) { - have_done_key = 1; - } - gnutls_free(spki); - } - gnutls_free(der_private.data); - } - } - gnutls_free(buf); - } -bad_file_pem: - if (!have_done_key) { - G_CHECK(gnutls_certificate_set_rawpk_key_file(*pki_credentials, - setup_data->pki_key.key.pem.public_cert, - setup_data->pki_key.key.pem.private_key, - GNUTLS_X509_FMT_PEM, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, GNUTLS_PKCS_PLAIN, 0), - "gnutls_certificate_set_rawpk_key_file"); - } -#else /* GNUTLS_VERSION_NUMBER < 0x030606 */ - coap_log(LOG_ERR, - "RPK Support not available (needs gnutls 3.6.6 or later)\n"); + coap_log(LOG_WARNING, + "RPK keys cannot be in COAP_PKI_KEY_PEM format\n"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; -#endif /* GNUTLS_VERSION_NUMBER < 0x030606 */ } else { G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials, diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index beb3aea0..ce49b388 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -1061,60 +1061,6 @@ pem_decode_mem_asn1(const char *begstr, const uint8_t *str) return NULL; } -#include -#include - -static uint8_t *read_file_mem(const char* file, size_t *length) { - FILE *f = fopen(file, "r"); - uint8_t *buf; - struct stat statbuf; - - *length = 0; - if (!f) - return NULL; - - if (fstat(fileno(f), &statbuf) == -1) { - fclose(f); - return NULL; - } - - buf = coap_malloc(statbuf.st_size+1); - if (!buf) - return NULL; - - if (fread(buf, 1, statbuf.st_size, f) != (size_t)statbuf.st_size) { - fclose(f); - free(buf); - return NULL; - } - buf[statbuf.st_size] = '\000'; - *length = (size_t)(statbuf.st_size + 1); - fclose(f); - return buf; -} - -static coap_binary_t * -pem_decode_file_asn1(const char *begstr, const char *file) -{ - size_t length; - uint8_t *str = read_file_mem(file, &length); - char *bcp; - char *tcp; - coap_binary_t *decoded = NULL; - - if (!str) { - return NULL; - } - - bcp = strstr((const char*)str, begstr); - tcp = bcp ? strstr(bcp, "-----END ") : NULL; - if (bcp && tcp) { - bcp += strlen(begstr); - decoded = pem_base64_decode ((const uint8_t *)bcp, tcp - bcp); - } - coap_free(str); - return decoded; -} #endif /* DTLS_ECC */ int @@ -1155,67 +1101,7 @@ coap_dtls_context_set_pki(coap_context_t *ctx, /* All should be RPK only now */ switch (setup_data->pki_key.key_type) { case COAP_PKI_KEY_PEM: - if (setup_data->pki_key.key.pem.private_key && - setup_data->pki_key.key.pem.private_key[0]) { - /* Need to read in the PEM information and convert to binary */ - asn1_priv = pem_decode_file_asn1("-----BEGIN EC PRIVATE KEY-----", - setup_data->pki_key.key.pem.private_key); - if (!asn1_priv) { - asn1_priv = pem_decode_file_asn1("-----BEGIN PRIVATE KEY-----", - setup_data->pki_key.key.pem.private_key); - if (!asn1_priv) { - coap_log(LOG_INFO, "Private Key (RPK) invalid\n"); - return 0; - } - asn1_temp = ec_abstract_pkcs8_asn1(asn1_priv->s, asn1_priv->length); - if (!asn1_temp) { - coap_log(LOG_INFO, "PKCS#8 Private Key (RPK) invalid\n"); - coap_delete_binary(asn1_priv); - return 0; - } - coap_delete_binary(asn1_priv); - asn1_priv = asn1_temp; - is_pkcs8 = 1; - } - /* See if public key has been explicitly provided */ - asn1_pub = pem_decode_file_asn1( "-----BEGIN PUBLIC KEY-----", - setup_data->pki_key.key.pem.public_cert); - if (!asn1_pub) { - /* Else use public key embedded into EC Private key */ - asn1_pub = pem_decode_file_asn1("-----BEGIN EC PRIVATE KEY-----", - setup_data->pki_key.key.pem.private_key); - if (!asn1_pub) { - asn1_pub = pem_decode_file_asn1("-----BEGIN PRIVATE KEY-----", - setup_data->pki_key.key.pem.private_key); - if (!asn1_pub) { - coap_log(LOG_INFO, "Public Key (RPK) invalid\n"); - coap_delete_binary(asn1_priv); - return 0; - } - asn1_temp = ec_abstract_pkcs8_asn1(asn1_pub->s, asn1_pub->length); - if (!asn1_temp) { - coap_log(LOG_INFO, "PKCS#8 Private Key (RPK) invalid\n"); - coap_delete_binary(asn1_priv); - coap_delete_binary(asn1_pub); - return 0; - } - coap_delete_binary(asn1_pub); - asn1_pub = asn1_temp; - is_pkcs8 = 1; - } - } - - if (!asn1_derive_keys(t_context, asn1_priv->s, asn1_priv->length, - asn1_pub->s, asn1_pub->length, is_pkcs8)) { - coap_log(LOG_INFO, "Unable to derive Public/Private Keys\n"); - coap_delete_binary(asn1_priv); - coap_delete_binary(asn1_pub); - return 0; - } - coap_delete_binary(asn1_priv); - coap_delete_binary(asn1_pub); - return 1; - } + coap_log(LOG_WARNING, "RPK keys cannot be in COAP_PKI_KEY_PEM format\n"); break; case COAP_PKI_KEY_PEM_BUF: if (setup_data->pki_key.key.pem_buf.public_cert && @@ -1326,6 +1212,8 @@ coap_dtls_context_set_pki(coap_context_t *ctx, } break; case COAP_PKI_KEY_PKCS11: + coap_log(LOG_WARNING, "RPK keys cannot be in COAP_PKI_KEY_PCKS11 format\n"); + break; default: break; }