diff --git a/Makefile.am b/Makefile.am index 51ebde83..8798b70c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,7 @@ EXTRA_DIST = \ examples/getopt.c \ include/coap$(LIBCOAP_API_VERSION)/coap_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_riot.h \ + include/coap$(LIBCOAP_API_VERSION)/coap_asn1_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_cache_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_session_internal.h \ include/coap$(LIBCOAP_API_VERSION)/coap_subscribe_internal.h \ @@ -76,6 +77,7 @@ libcoap_@LIBCOAP_NAME_SUFFIX@_la_SOURCES = \ src/address.c \ src/async.c \ src/block.c \ + src/coap_asn1.c \ src/coap_cache.c \ src/coap_debug.c \ src/coap_event.c \ diff --git a/include/coap2/coap_asn1_internal.h b/include/coap2/coap_asn1_internal.h new file mode 100644 index 00000000..657bcd83 --- /dev/null +++ b/include/coap2/coap_asn1_internal.h @@ -0,0 +1,78 @@ +/* + * coap_asn1_internal.h -- ASN.1 functions for libcoap + * + * Copyright (C) 2020 Jon Shallow + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +/** + * @file coap_asn1_internal.h + * @brief COAP ASN.1 internal information + */ + +#ifndef COAP_ASN1_INTERNAL_H_ +#define COAP_ASN1_INTERNAL_H_ + + +/** + * @defgroup asn1 ASN.1 Support (Internal) + * API functions for CoAP ASN.1 + * @{ + */ + +typedef enum { + COAP_ASN1_NONE = 0, + COAP_ASN1_INTEGER = 2, + COAP_ASN1_BITSTRING = 3, + COAP_ASN1_OCTETSTRING = 4, + COAP_ASN1_IDENTIFIER = 6, +} coap_asn1_tag_t; + +/** + * Callback to validate the asn1 tag and data. + * + * @param data The start of the tag and data + * @param size The size of the tag and data + * + * @return @c 1 if pass, else @c 0 if fail + */ +typedef int (*asn1_validate)(const uint8_t *data, size_t size); + +/** + * Get the asn1 length from the current @p ptr. + * + * @param ptr The current asn.1 object length pointer + * + * @return The length of the asn.1 object. @p ptr is udated to past the length. + */ +size_t asn1_len(const uint8_t **ptr); + +/** + * Get the asn1 tag from the current @p ptr. + * + * @param ptr The current asn.1 object tag pointer + * @param constructed 1 if current tag is constructed + * @param class The current class of the tag + * + * @return The tag value.@p ptr is udated to pass the tag. + */ +coap_asn1_tag_t asn1_tag_c(const uint8_t **ptr, int *constructed, int *class); + +/** + * Get the asn1 tag and data from the current @p ptr. + * + * @param ltag The tag to look for + * @param ptr The current asn.1 object pointer + * @param tlen The remaining size oof the asn.1 data + * @param validate Call validate to verify tag data or @c NULL + * + * @return The asn.1 tag and data or @c NULL if not found + */ +coap_binary_t *get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, + size_t tlen, asn1_validate validate); + +/** @} */ + +#endif /* COAP_ASN1_INTERNAL_H_ */ diff --git a/include/coap2/coap_internal.h b/include/coap2/coap_internal.h index b1821168..e0808b6a 100644 --- a/include/coap2/coap_internal.h +++ b/include/coap2/coap_internal.h @@ -46,6 +46,7 @@ #include "coap_mutex.h" /* Specifically defined internal .h files */ +#include "coap_asn1_internal.h" #include "coap_cache_internal.h" #include "coap_session_internal.h" #include "coap_subscribe_internal.h" diff --git a/src/coap_asn1.c b/src/coap_asn1.c new file mode 100644 index 00000000..1147e8e6 --- /dev/null +++ b/src/coap_asn1.c @@ -0,0 +1,100 @@ +/* coap_asn1.c -- ASN.1 handling functions +* +* Copyright (C) 2020 Jon Shallow +* +* This file is part of the CoAP library libcoap. Please see +* README for terms of use. +*/ + +#include "coap_internal.h" + +size_t +asn1_len(const uint8_t **ptr) +{ + size_t len = 0; + + if ((**ptr) & 0x80) { + size_t octets = (**ptr) & 0x7f; + (*ptr)++; + while (octets) { + len = (len << 8) + (**ptr); + (*ptr)++; + octets--; + } + } + else { + len = (**ptr) & 0x7f; + (*ptr)++; + } + return len; +} + +coap_asn1_tag_t +asn1_tag_c(const uint8_t **ptr, int *constructed, int *class) +{ + coap_asn1_tag_t tag = 0; + uint8_t byte; + + byte = (**ptr); + *constructed = (byte & 0x20) ? 1 : 0; + *class = byte >> 6; + tag = byte & 0x1F; + (*ptr)++; + if (tag < 0x1F) + return tag; + + /* Tag can be one byte or more based on B8 */ + byte = (**ptr); + while (byte & 0x80) { + tag = (tag << 7) + (byte & 0x7F); + (*ptr)++; + byte = (**ptr); + } + /* Do the final one */ + tag = (tag << 7) + (byte & 0x7F); + (*ptr)++; + return tag; +} + +/* caller must free off returned coap_binary_t* */ +coap_binary_t * +get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, + asn1_validate validate) +{ + int constructed; + int class; + const uint8_t *acp = ptr; + uint8_t tag = asn1_tag_c(&acp, &constructed, &class); + size_t len = asn1_len(&acp); + coap_binary_t *tag_data; + + while (tlen > 0 && len <= tlen) { + if (class == 2 && constructed == 1) { + /* Skip over element description */ + tag = asn1_tag_c(&acp, &constructed, &class); + len = asn1_len(&acp); + } + if (tag == ltag) { + if (!validate || validate(acp, len)) { + tag_data = coap_new_binary(len); + if (tag_data == NULL) + return NULL; + tag_data->length = len; + memcpy(tag_data->s, acp, len); + return tag_data; + } + } + if (tag == 0x10 && constructed == 1) { + /* SEQUENCE or SEQUENCE OF */ + tag_data = get_asn1_tag(ltag, acp, len, validate); + if (tag_data) + return tag_data; + } + acp += len; + tlen -= len; + tag = asn1_tag_c(&acp, &constructed, &class); + len = asn1_len(&acp); + } + return NULL; +} + diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index 9dd73ca8..17cfc608 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -57,8 +57,6 @@ #include #include #if (GNUTLS_VERSION_NUMBER >= 0x030606) -#include -#include #define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \ GNUTLS_KEY_NON_REPUDIATION | \ GNUTLS_KEY_KEY_ENCIPHERMENT | \ @@ -813,96 +811,6 @@ pin_callback(void *user_data, int attempt, return -1; } #if (GNUTLS_VERSION_NUMBER >= 0x030606) -static size_t -asn1_len(const uint8_t **ptr) -{ - size_t len = 0; - - if ((**ptr) & 0x80) { - size_t octets = (**ptr) & 0x7f; - (*ptr)++; - while (octets) { - len = (len << 8) + (**ptr); - (*ptr)++; - octets--; - } - } - else { - len = (**ptr) & 0x7f; - (*ptr)++; - } - return len; -} - -static size_t -asn1_tag_c(const uint8_t **ptr, int *constructed, int *class) -{ - size_t tag = 0; - uint8_t byte; - - byte = (**ptr); - *constructed = (byte & 0x20) ? 1 : 0; - *class = byte >> 6; - tag = byte & 0x1F; - (*ptr)++; - if (tag < 0x1F) - return tag; - - /* Tag can be one byte or more based on B8 */ - byte = (**ptr); - while (byte & 0x80) { - tag = (tag << 7) + (byte & 0x7F); - (*ptr)++; - byte = (**ptr); - } - /* Do the final one */ - tag = (tag << 7) + (byte & 0x7F); - (*ptr)++; - return tag; -} - -/* caller must free off returned gnutls_datum_t* */ -static gnutls_datum_t * -get_asn1_tag(uint8_t ltag, const uint8_t *ptr, size_t tlen) -{ - int constructed; - int class; - const uint8_t *acp = ptr; - uint8_t tag = asn1_tag_c(&acp, &constructed, &class); - size_t len = asn1_len(&acp); - gnutls_datum_t *tag_data; - - while (tlen > 0 && len <= tlen) { - if (class == 2 && constructed == 1) { - /* Skip over element description */ - tag = asn1_tag_c(&acp, &constructed, &class); - len = asn1_len(&acp); - } - if (tag == ltag) { - uint8_t *tmp = gnutls_malloc(sizeof(gnutls_datum_t) + - len); - tag_data = (gnutls_datum_t *)tmp; - if (tag_data == NULL) - return NULL; - tag_data->size = len; - tag_data->data = &tmp[sizeof(gnutls_datum_t)]; - memcpy(tag_data->data, acp, len); - return tag_data; - } - if (tag == 0x10 && constructed == 1) { - /* SEQUENCE or SEQUENCE OF */ - tag_data = get_asn1_tag(ltag, acp, len); - if (tag_data) - return tag_data; - } - acp += len; - tlen -= len; - tag = asn1_tag_c(&acp, &constructed, &class); - len = asn1_len(&acp); - } - return NULL; -} - /* first part of Raw public key, this is the start of the Subject Public Key */ static const unsigned char cert_asn1_header1[] = { 0x30, 0x59, /* SEQUENCE, length 89 bytes */ @@ -923,37 +831,36 @@ static const unsigned char cert_asn1_header2[] = { static gnutls_datum_t * get_asn1_spki(const uint8_t *data, size_t size) { - gnutls_datum_t *pub_key = get_asn1_tag(ASN1_BITSTRING, data, size); - gnutls_datum_t *prime = get_asn1_tag(ASN1_IDENTIFIER, data, size); + coap_binary_t *pub_key = get_asn1_tag(COAP_ASN1_BITSTRING, data, size, NULL); + coap_binary_t *prime = get_asn1_tag(COAP_ASN1_IDENTIFIER, data, size, NULL); + gnutls_datum_t *spki = NULL; if (pub_key && prime) { size_t header_size = sizeof(cert_asn1_header1) + 2 + - prime->size + + prime->length + sizeof(cert_asn1_header2); - uint8_t *tmp = gnutls_realloc(pub_key, sizeof(gnutls_datum_t) + - header_size + - pub_key->size); + uint8_t *tmp = gnutls_malloc(sizeof(gnutls_datum_t) + + header_size + + pub_key->length); if (tmp) { - pub_key = (gnutls_datum_t *)tmp; - pub_key->data = &tmp[sizeof(gnutls_datum_t)]; - memmove(&pub_key->data[header_size], pub_key->data, pub_key->size); - memcpy(pub_key->data, cert_asn1_header1, sizeof(cert_asn1_header1)); - pub_key->data[sizeof(cert_asn1_header1)] = ASN1_IDENTIFIER; - pub_key->data[sizeof(cert_asn1_header1)+1] = prime->size; - memcpy(&pub_key->data[sizeof(cert_asn1_header1)+2], - prime->data, prime->size); - memcpy(&pub_key->data[sizeof(cert_asn1_header1)+2+prime->size], + spki = (gnutls_datum_t *)tmp; + spki->data = &tmp[sizeof(gnutls_datum_t)]; + memcpy(&spki->data[header_size], pub_key->s, pub_key->length); + memcpy(spki->data, cert_asn1_header1, sizeof(cert_asn1_header1)); + spki->data[sizeof(cert_asn1_header1)] = COAP_ASN1_IDENTIFIER; + spki->data[sizeof(cert_asn1_header1)+1] = prime->length; + memcpy(&spki->data[sizeof(cert_asn1_header1)+2], + prime->s, prime->length); + memcpy(&spki->data[sizeof(cert_asn1_header1)+2+prime->length], cert_asn1_header2, sizeof(cert_asn1_header2)); - pub_key->size += header_size; - gnutls_free(prime); - return pub_key; + spki->size = header_size + pub_key->length; } } - if (pub_key) gnutls_free(pub_key); - if (prime) gnutls_free(prime); - return NULL; + if (pub_key) coap_delete_binary(pub_key); + if (prime) coap_delete_binary(prime); + return spki; } #endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */ diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index ce49b388..c1d0210b 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -786,61 +786,11 @@ end: return decoded; } -static size_t -asn1_len(const uint8_t **ptr) -{ - size_t len = 0; - - if ((**ptr) & 0x80) { - size_t octets = (**ptr) & 0x7f; - (*ptr)++; - while (octets) { - len = (len << 8) + (**ptr); - (*ptr)++; - octets--; - } - } - else { - len = (**ptr) & 0x7f; - (*ptr)++; - } - return len; -} - -static size_t -asn1_tag_c(const uint8_t **ptr, int *constructed, int *class) -{ - size_t tag = 0; - uint8_t byte; - - byte = (**ptr); - *constructed = (byte & 0x20) ? 1 : 0; - *class = byte >> 6; - tag = byte & 0x1F; - (*ptr)++; - if (tag < 0x1F) - return tag; - - /* Tag can be one byte or more based on B8 */ - byte = (**ptr); - while (byte & 0x80) { - tag = (tag << 7) + (byte & 0x7F); - (*ptr)++; - byte = (**ptr); - } - /* Do the final one */ - tag = (tag << 7) + (byte & 0x7F); - (*ptr)++; - return tag; -} - typedef coap_binary_t * (*asn1_callback)(const uint8_t *data, size_t size); -static coap_binary_t * -asn1_callback_privkey(const uint8_t *data, size_t size) +static int +asn1_verify_privkey(const uint8_t *data, size_t size) { - coap_binary_t *priv_key; - /* Check if we have the private key (with optional leading 0x00) */ /* skip leading 0x00 */ if (size - 1 == DTLS_EC_KEY_SIZE && *data == '\000') { @@ -850,38 +800,27 @@ asn1_callback_privkey(const uint8_t *data, size_t size) /* Check if we have the private key */ if (size != DTLS_EC_KEY_SIZE) - return NULL; + return 0; - priv_key = coap_new_binary(DTLS_EC_KEY_SIZE); - if (!priv_key) - return NULL; - - memcpy(priv_key->s, data, DTLS_EC_KEY_SIZE); - return priv_key; + return 1; } -static coap_binary_t * -asn1_callback_pubkey(const uint8_t *data, size_t size) +static int +asn1_verify_pubkey(const uint8_t *data, size_t size) { - coap_binary_t *pub_key; + (void)data; /* We have the public key (with a leading 0x00 (no unused bits) 0x04 (not compressed() */ if (size - 2 != 2 * DTLS_EC_KEY_SIZE) - return NULL; + return 0; - pub_key = coap_new_binary(size-2); - if (!pub_key) - return NULL; - - memcpy(pub_key->s, data+2, size-2); - return pub_key; + return 1; } -static coap_binary_t * -asn1_callback_curve(const uint8_t *data, size_t size) +static int +asn1_verify_curve(const uint8_t *data, size_t size) { - coap_binary_t *curve_key; static uint8_t prime256v1_oid[] = /* OID 1.2.840.10045.3.1.7 */ { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 }; @@ -889,29 +828,24 @@ asn1_callback_curve(const uint8_t *data, size_t size) /* Check that we have the correct EC (only one supported) */ if (size != sizeof(prime256v1_oid) || memcmp(data, prime256v1_oid, size) != 0) - return NULL; + return 0; - curve_key = coap_new_binary(0); - return curve_key; + return 1; } -static coap_binary_t * -asn1_callback_pkcs8_version(const uint8_t *data, size_t size) +static int +asn1_verify_pkcs8_version(const uint8_t *data, size_t size) { - coap_binary_t *version_key; - /* Check that we have the version */ if (size != 1 || *data != 0) - return NULL; + return 0; - version_key = coap_new_binary(0); - return version_key; + return 1; } -static coap_binary_t * -asn1_callback_ec_identifier(const uint8_t *data, size_t size) +static int +asn1_verify_ec_identifier(const uint8_t *data, size_t size) { - coap_binary_t *id_key; static uint8_t ec_public_key_oid[] = /* OID 1.2.840.10045.2.1 */ { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 }; @@ -919,62 +853,20 @@ asn1_callback_ec_identifier(const uint8_t *data, size_t size) /* Check that we have the correct ecPublicKey */ if (size != sizeof(ec_public_key_oid) || memcmp(data, ec_public_key_oid, size) != 0) - return NULL; + return 0; - id_key = coap_new_binary(0); - return id_key; + return 1; } -static coap_binary_t * -asn1_callback_ec_key(const uint8_t *data, size_t size) +static int +asn1_verify_ec_key(const uint8_t *data, size_t size) { - coap_binary_t *ec_key; + (void)data; if (size == 0) - return NULL; + return 0; - ec_key = coap_new_binary(size); - if (!ec_key) - return NULL; - - memcpy(ec_key->s, data, size); - return ec_key; -} - -/* caller must free off returned coap_binary_t* */ -static coap_binary_t * -asn1_abstract_tag(const uint8_t *ptr, size_t tlen, uint8_t ltag, asn1_callback callback) -{ - int constructed; - int class; - const uint8_t *acp = ptr; - uint8_t tag = asn1_tag_c(&acp, &constructed, &class); - size_t len = asn1_len(&acp); - coap_binary_t *tag_data; - - while (tlen > 0 && len <= tlen) { - if (class == 2 && constructed == 1) { - /* Skip over element description */ - tag = asn1_tag_c(&acp, &constructed, &class); - len = asn1_len(&acp); - } - if (tag == ltag) { - tag_data = callback(acp, len); - if (tag_data) - return tag_data; - } - if (tag == 0x10 && constructed == 1) { - /* SEQUENCE or SEQUENCE OF */ - tag_data = asn1_abstract_tag(acp, len, ltag, callback); - if (tag_data) - return tag_data; - } - acp += len; - tlen -= len; - tag = asn1_tag_c(&acp, &constructed, &class); - len = asn1_len(&acp); - } - return NULL; + return 1; } static int @@ -985,17 +877,23 @@ asn1_derive_keys(coap_tiny_context_t *t_context, { coap_binary_t *test; - t_context->priv_key = asn1_abstract_tag(priv_data, priv_len, - 0x04, asn1_callback_privkey); + t_context->priv_key = get_asn1_tag(COAP_ASN1_OCTETSTRING, priv_data, + priv_len, asn1_verify_privkey); if (!t_context->priv_key) { coap_log(LOG_INFO, "EC Private Key (RPK) invalid\n"); return 0; } + /* skip leading 0x00 */ + if (t_context->priv_key->length - 1 == DTLS_EC_KEY_SIZE && + t_context->priv_key->s[0] == '\000') { + t_context->priv_key->length--; + t_context->priv_key->s++; + } if (!is_pkcs8) { /* pkcs8 abstraction tested for valid eliptic curve */ - test = asn1_abstract_tag(priv_data, priv_len, - 0x06, asn1_callback_curve); + test = get_asn1_tag(COAP_ASN1_IDENTIFIER, priv_data, priv_len, + asn1_verify_curve); if (!test) { coap_log(LOG_INFO, "EC Private Key (RPK) invalid elliptic curve\n"); coap_delete_binary(t_context->priv_key); @@ -1005,14 +903,17 @@ asn1_derive_keys(coap_tiny_context_t *t_context, coap_delete_binary(test); } - t_context->pub_key = asn1_abstract_tag(pub_data, pub_len, - 0x03, asn1_callback_pubkey); + t_context->pub_key = get_asn1_tag(COAP_ASN1_BITSTRING, pub_data, pub_len, + asn1_verify_pubkey); if (!t_context->pub_key) { coap_log(LOG_INFO, "EC Public Key (RPK) invalid\n"); coap_delete_binary(t_context->priv_key); t_context->priv_key = NULL; return 0; } + /* Drop leading 0x00 and 0x04 */ + t_context->pub_key->s += 2; + t_context->pub_key->length -= 2; dtls_set_handler(t_context->dtls_context, &ec_cb); return 1; } @@ -1022,29 +923,29 @@ ec_abstract_pkcs8_asn1(const uint8_t *asn1_ptr, size_t asn1_length) { coap_binary_t *test; - test = asn1_abstract_tag(asn1_ptr, asn1_length, - 0x02, asn1_callback_pkcs8_version); + test = get_asn1_tag(COAP_ASN1_INTEGER, asn1_ptr, asn1_length, + asn1_verify_pkcs8_version); if (!test) return 0; coap_delete_binary(test); - test = asn1_abstract_tag(asn1_ptr, asn1_length, - 0x06, asn1_callback_ec_identifier); + test = get_asn1_tag(COAP_ASN1_IDENTIFIER, asn1_ptr, asn1_length, + asn1_verify_ec_identifier); if (!test) return 0; coap_delete_binary(test); - test = asn1_abstract_tag(asn1_ptr, asn1_length, - 0x06, asn1_callback_curve); + test = get_asn1_tag(COAP_ASN1_IDENTIFIER, asn1_ptr, asn1_length, + asn1_verify_curve); if (!test) { coap_log(LOG_INFO, "EC Private Key (RPK) invalid elliptic curve\n"); return 0; } coap_delete_binary(test); - test = asn1_abstract_tag(asn1_ptr, asn1_length, - 0x04, asn1_callback_ec_key); + test = get_asn1_tag(COAP_ASN1_OCTETSTRING, asn1_ptr, asn1_length, + asn1_verify_ec_key); return test; }