Skip to contentSkip to conversation

dpp-util: add DPP attribute building APIs

PATCH Applied James Prestwood 2 participants 10 messages Dec 17, 2021

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH v5 1/4 dpp-util: add DPP attribute building APIs

---
 src/dpp-util.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/dpp-util.h |  10 ++++
 src/dpp.c      |  10 ----
 3 files changed, 153 insertions(+), 10 deletions(-)

v5:
* Moved these APIs into dpp-util instead of dpp.c so they can be unit tested
* Changed so the caller supplies ad0/ad1. This takes the burden off these utilities to sort out what type of frame, and any quirks of how AAD is supposed to be used (see comments in authentication and configuration patches)

src/dpp-util.c +143 -0
index f41127c7..f281f346 100644
@@ -238,6 +238,149 @@ bool dpp_attr_iter_next(struct dpp_attr_iter *iter,
 	return true;
 }
 
+size_t dpp_append_attr(uint8_t *to, enum dpp_attribute_type type,
+				void *attr, size_t attr_len)
+{
+	l_put_le16(type, to);
+	l_put_le16(attr_len, to + 2);
+	memcpy(to + 4, attr, attr_len);
+
+	return attr_len + 4;
+}
+
+/*
+ * The use of ad0/ad1 differs with different protocol frame types, which is why
+ * this is left up to the caller to pass the correct AD bytes. The usage is
+ * defined in:
+ *
+ * 6.3.1.4 Protocol Conventions (for authentication)
+ * 6.4.1 Overview (for configuration)
+ *
+ */
+uint8_t *dpp_unwrap_attr(const void *ad0, size_t ad0_len, const void *ad1,
+				size_t ad1_len, const void *key, size_t key_len,
+				const void *wrapped, size_t wrapped_len,
+				size_t *unwrapped_len)
+{
+	struct iovec ad[2];
+	uint8_t *unwrapped;
+	size_t ad_size = 0;
+
+	if (ad0) {
+		ad[ad_size].iov_base = (void *) ad0;
+		ad[ad_size].iov_len = ad0_len;
+		ad_size++;
+	}
+
+	if (ad1) {
+		ad[ad_size].iov_base = (void *) ad1;
+		ad[ad_size].iov_len = ad1_len;
+		ad_size++;
+	}
+
+	unwrapped = l_malloc(wrapped_len - 16);
+
+	if (!aes_siv_decrypt(key, key_len, wrapped, wrapped_len, ad, 2,
+				unwrapped)) {
+		l_free(unwrapped);
+		return NULL;
+	}
+
+	*unwrapped_len = wrapped_len - 16;
+
+	return unwrapped;
+}
+
+/*
+ * Encrypt DPP attributes encapsulated in DPP wrapped data.
+ *
+ * ad0/ad0_len - frame specific AD0 component
+ * ad1/ad0_len - frame specific AD1 component
+ * to - buffer to encrypt data.
+ * to_len - size of 'to'
+ * key - key used to encrypt
+ * key_len - size of 'key'
+ * num_attrs - number of attributes listed (type, length, data triplets)
+ * ... - List of attributes, Type, Length, and data
+ */
+size_t dpp_append_wrapped_data(const void *ad0, size_t ad0_len,
+				const void *ad1, size_t ad1_len,
+				uint8_t *to, size_t to_len,
+				const void *key, size_t key_len,
+				size_t num_attrs, ...)
+{
+	size_t i;
+	size_t attrs_len = 0;
+	_auto_(l_free) uint8_t *plaintext = NULL;
+	uint8_t *ptr;
+	struct iovec ad[2];
+	size_t ad_size = 0;
+	va_list va;
+
+	va_start(va, num_attrs);
+
+	/* Count up total attributes length */
+	for (i = 0; i < num_attrs; i++) {
+		va_arg(va, enum dpp_attribute_type);
+		attrs_len += va_arg(va, size_t) + 4;
+		va_arg(va, void*);
+	}
+
+	if (to_len < attrs_len + 4 + 16)
+		return false;
+
+	plaintext = l_malloc(attrs_len);
+
+	ptr = plaintext;
+
+	va_end(va);
+
+	va_start(va, num_attrs);
+
+	/* Build up plaintext attributes */
+	for (i = 0; i < num_attrs; i++) {
+		enum dpp_attribute_type type = va_arg(va,
+						enum dpp_attribute_type);
+		size_t l = va_arg(va, size_t);
+		void *p = va_arg(va, void *);
+
+		l_put_le16(type, ptr);
+		ptr += 2;
+		l_put_le16(l, ptr);
+		ptr += 2;
+		memcpy(ptr, p, l);
+		ptr += l;
+	}
+
+	va_end(va);
+
+	ptr = to;
+
+	l_put_le16(DPP_ATTR_WRAPPED_DATA, ptr);
+	ptr += 2;
+	l_put_le16(attrs_len + 16, ptr);
+	ptr += 2;
+
+	if (ad0) {
+		ad[ad_size].iov_base = (void *) ad0;
+		ad[ad_size].iov_len = ad0_len;
+		ad_size++;
+	}
+
+	if (ad1) {
+		ad[ad_size].iov_base = (void *) ad1;
+		ad[ad_size].iov_len = ad1_len;
+		ad_size++;
+	}
+
+	if (!aes_siv_encrypt(key, key_len, plaintext, attrs_len,
+				ad, ad_size, ptr))
+		return 0;
+
+	return attrs_len + 4 + 16;
+}
+
+
 /*
  * EasyConnect 2.0 Table 3. Key and Nonce Length Dependency on Prime Length
  */
src/dpp-util.h +10 -0
index 84404128..543cf9f8 100644
@@ -122,6 +122,16 @@ void dpp_attr_iter_init(struct dpp_attr_iter *iter, const uint8_t *pdu,
 bool dpp_attr_iter_next(struct dpp_attr_iter *iter,
 			enum dpp_attribute_type *type, size_t *len,
 			const uint8_t **data);
+uint8_t *dpp_unwrap_attr(const void *ad0, size_t ad0_len, const void *ad1,
+				size_t ad1_len, const void *key, size_t key_len,
+				const void *wrapped, size_t wrapped_len,
+				size_t *unwrapped_len);
+size_t dpp_append_attr(uint8_t *to, enum dpp_attribute_type type,
+				void *attr, size_t attr_len);
+size_t dpp_append_wrapped_data(const void *ad0, size_t ad0_len, const void *ad1,
+				size_t ad1_len, uint8_t *to, size_t to_len,
+				const void *key, size_t key_len,
+				size_t num_attrs, ...);
 
 char *dpp_generate_uri(const uint8_t *asn1, size_t asn1_len, uint8_t version,
 			const uint8_t *mac, const uint32_t *freqs,
src/dpp.c +0 -10
index 0a42e321..d9666ead 100644
@@ -143,16 +143,6 @@ static void dpp_send_frame(uint64_t wdev_id, struct iovec *iov, size_t iov_len,
 		l_error("Could not send CMD_FRAME");
 }
 
-static size_t dpp_append_attr(uint8_t *to, enum dpp_attribute_type type,
-				void *attr, size_t attr_len)
-{
-	l_put_le16(type, to);
-	l_put_le16(attr_len, to + 2);
-	memcpy(to + 4, attr, attr_len);
-
-	return attr_len + 4;
-}
-
 static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest,
 				enum dpp_frame_type type,
 				uint8_t buf[static 32])
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v5 2/4 dpp: add DPP authentication protocol

This implements the DPP protocol used to authenticate to a DPP configurator.

Note this is not a full implementation of the protocol and there are a few missing features which will be added as needed:

- Mutual authentication (needed for BLE bootstrapping)
- Configurator support
- Initiator role

---
 src/dpp.c | 491 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 491 insertions(+)

v5:
* Updated use of wrap/unwrap APIs.
* Better commented how AAD is used with wrapping/unwrapping

src/dpp.c +491 -0
index d9666ead..dd17cc47 100644
@@ -41,6 +41,9 @@
 #include "src/ie.h"
 #include "src/iwd.h"
 #include "src/util.h"
+#include "src/crypto.h"
+#include "src/mpdu.h"
+#include "ell/useful.h"
 
 static uint32_t netdev_watch;
 static struct l_genl_family *nl80211;
@@ -49,6 +52,7 @@ static uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 enum dpp_state {
 	DPP_STATE_NOTHING,
 	DPP_STATE_PRESENCE,
+	DPP_STATE_AUTHENTICATING,
 };
 
 struct dpp_sm {
@@ -76,8 +80,38 @@ struct dpp_sm {
 	struct scan_freq_set *presence_list;
 
 	uint32_t offchannel_id;
+
+	uint8_t auth_addr[6];
+	uint8_t r_nonce[32];
+	uint8_t i_nonce[32];
+
+	uint64_t ke[L_ECC_MAX_DIGITS];
+	uint64_t k2[L_ECC_MAX_DIGITS];
+
+	struct l_ecc_scalar *proto_private;
+	struct l_ecc_point *proto_public;
+
+	struct l_ecc_point *i_proto_public;
 };
 
+static void dpp_free_auth_data(struct dpp_sm *dpp)
+{
+	if (dpp->proto_public) {
+		l_ecc_point_free(dpp->proto_public);
+		dpp->proto_public = NULL;
+	}
+
+	if (dpp->proto_private) {
+		l_ecc_scalar_free(dpp->proto_private);
+		dpp->proto_private = NULL;
+	}
+
+	if (dpp->i_proto_public) {
+		l_ecc_point_free(dpp->i_proto_public);
+		dpp->i_proto_public = NULL;
+	}
+}
+
 static void dpp_reset(struct dpp_sm *dpp)
 {
 	if (dpp->uri) {
@@ -96,6 +130,8 @@ static void dpp_reset(struct dpp_sm *dpp)
 	}
 
 	dpp->state = DPP_STATE_NOTHING;
+
+	dpp_free_auth_data(dpp);
 }
 
 static void dpp_free(struct dpp_sm *dpp)
@@ -167,6 +203,456 @@ static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest,
 	return ptr - buf;
 }
 
+/*
+ * The Authentication protocol has a consistent use of AD components, and this
+ * use is defined in 6.3.1.4:
+ *
+ * "Invocations of AES-SIV in the DPP Authentication protocol that produce
+ * ciphertext that is part of an additional AES-SIV invocation do not use AAD;
+ * in other words, the number of AAD components is set to zero. All other
+ * invocations of AES-SIV in the DPP Authentication protocol shall pass a vector
+ * of AAD having two components of AAD in the following order: (1) the DPP
+ * header, as defined in Table 30, from the OUI field (inclusive) to the DPP
+ * Frame Type field (inclusive); and (2) all octets in a DPP Public Action frame
+ * after the DPP Frame Type field up to and including the last octet of the last
+ * attribute before the Wrapped Data attribute"
+ *
+ * In practice you see this as AD0 being some offset in the frame (offset to the
+ * OUI). For outgoing packets this is 26 bytes offset since the header is built
+ * manually. For incoming packets the offset is 2 bytes. The length is always
+ * 6 bytes for AD0.
+ *
+ * The AD1 data is always the start of the attributes, and length is the number
+ * of bytes from these attributes to wrapped data. e.g.
+ *
+ * ad1 = attrs
+ * ad1_len = ptr - attrs
+ */
+static void send_authenticate_response(struct dpp_sm *dpp, void *r_auth)
+{
+	uint8_t hdr[32];
+	uint8_t attrs[256];
+	uint8_t *ptr = attrs;
+	uint8_t status = DPP_STATUS_OK;
+	uint64_t r_proto_key[L_ECC_MAX_DIGITS * 2];
+	uint8_t version = 2;
+	uint8_t r_capabilities = 0x01;
+	struct iovec iov[3];
+	uint8_t wrapped2_plaintext[dpp->key_len + 4];
+	uint8_t wrapped2[dpp->key_len + 16 + 8];
+	size_t wrapped2_len;
+
+	l_ecc_point_get_data(dpp->proto_public, r_proto_key,
+				sizeof(r_proto_key));
+
+	iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev),
+				dpp->auth_addr,
+				DPP_FRAME_AUTHENTICATION_RESPONSE, hdr);
+	iov[0].iov_base = hdr;
+
+	ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1);
+	ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH,
+				dpp->pub_boot_hash, 32);
+	ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_PROTOCOL_KEY,
+				r_proto_key, dpp->key_len * 2);
+	ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, &version, 1);
+
+	/* Wrap up secondary data (R-Auth) */
+	wrapped2_len = dpp_append_attr(wrapped2_plaintext,
+					DPP_ATTR_RESPONDER_AUTH_TAG,
+					r_auth, dpp->key_len);
+	/*
+	 * "Invocations of AES-SIV in the DPP Authentication protocol that
+	 * produce ciphertext that is part of an additional AES-SIV invocation
+	 * do not use AAD; in other words, the number of AAD components is set
+	 * to zero.""
+	 */
+	aes_siv_encrypt(dpp->ke, dpp->key_len, wrapped2_plaintext,
+					dpp->key_len + 4, NULL, 0, wrapped2);
+
+	wrapped2_len += 16;
+
+	ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs,
+			ptr, sizeof(attrs), dpp->k2, dpp->key_len, 4,
+			DPP_ATTR_RESPONDER_NONCE, dpp->nonce_len, dpp->r_nonce,
+			DPP_ATTR_INITIATOR_NONCE, dpp->nonce_len, dpp->i_nonce,
+			DPP_ATTR_RESPONDER_CAPABILITIES, 1, &r_capabilities,
+			DPP_ATTR_WRAPPED_DATA, wrapped2_len, wrapped2);
+
+	iov[1].iov_base = attrs;
+	iov[1].iov_len = ptr - attrs;
+
+	dpp_send_frame(netdev_get_wdev_id(dpp->netdev), iov, 2,
+				dpp->current_freq);
+}
+
+static void authenticate_confirm(struct dpp_sm *dpp, const uint8_t *from,
+					const uint8_t *body, size_t body_len)
+{
+	struct dpp_attr_iter iter;
+	enum dpp_attribute_type type;
+	size_t len;
+	const uint8_t *data;
+	int status = -1;
+	const uint8_t *r_boot_hash = NULL;
+	const void *wrapped = NULL;
+	const uint8_t *i_auth = NULL;
+	size_t i_auth_len;
+	_auto_(l_free) uint8_t *unwrapped = NULL;
+	size_t wrapped_len = 0;
+	uint64_t i_auth_check[L_ECC_MAX_DIGITS];
+	const void *unwrap_key;
+	const void *ad0 = body + 2;
+	const void *ad1 = body + 8;
+
+	if (dpp->state != DPP_STATE_AUTHENTICATING)
+		return;
+
+	if (memcmp(from, dpp->auth_addr, 6))
+		return;
+
+	l_debug("authenticate confirm");
+
+	dpp_attr_iter_init(&iter, body + 8, body_len - 8);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_STATUS:
+			status = l_get_u8(data);
+			break;
+		case DPP_ATTR_RESPONDER_BOOT_KEY_HASH:
+			r_boot_hash = data;
+			/*
+			 * Spec requires this, but does not mention if anything
+			 * is to be done with it.
+			 */
+			break;
+		case DPP_ATTR_INITIATOR_BOOT_KEY_HASH:
+			/* No mutual authentication */
+			break;
+		case DPP_ATTR_WRAPPED_DATA:
+			wrapped = data;
+			wrapped_len = len;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (!r_boot_hash || !wrapped) {
+		l_debug("Attributes missing from authenticate confirm");
+		return;
+	}
+
+	/*
+	 * "The Responder obtains the DPP Authentication Confirm frame and
+	 * checks the value of the DPP Status field. If the value of the DPP
+	 * Status field is STATUS_NOT_COMPATIBLE or STATUS_AUTH_FAILURE, the
+	 * Responder unwraps the wrapped data portion of the frame using k2"
+	 */
+	if (status == DPP_STATUS_OK)
+		unwrap_key = dpp->ke;
+	else if (status == DPP_STATUS_NOT_COMPATIBLE ||
+				status == DPP_STATUS_AUTH_FAILURE)
+		unwrap_key = dpp->k2;
+	else
+		goto auth_confirm_failed;
+
+	unwrapped = dpp_unwrap_attr(ad0, 6, ad1, wrapped - 4 - ad1,
+			unwrap_key, dpp->key_len, wrapped, wrapped_len,
+			&wrapped_len);
+	if (!unwrapped)
+		goto auth_confirm_failed;
+
+	if (status != DPP_STATUS_OK) {
+		/*
+		 * "If unwrapping is successful, the Responder should generate
+		 * an alert indicating the reason for the protocol failure."
+		 */
+		l_debug("Authentication failed due to status %s",
+				status == DPP_STATUS_NOT_COMPATIBLE ?
+				"NOT_COMPATIBLE" : "AUTH_FAILURE");
+		goto auth_confirm_failed;
+	}
+
+	dpp_attr_iter_init(&iter, unwrapped, wrapped_len);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_INITIATOR_AUTH_TAG:
+			i_auth = data;
+			i_auth_len = len;
+			break;
+		case DPP_ATTR_RESPONDER_NONCE:
+			/* Only if error */
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (!i_auth || i_auth_len != dpp->key_len) {
+		l_debug("I-Auth missing from wrapped data");
+		goto auth_confirm_failed;
+	}
+
+	dpp_derive_i_auth(dpp->r_nonce, dpp->i_nonce, dpp->nonce_len,
+				dpp->proto_public, dpp->i_proto_public,
+				dpp->boot_public, i_auth_check);
+
+	if (memcmp(i_auth, i_auth_check, i_auth_len)) {
+		l_error("I-Auth did not verify");
+		goto auth_confirm_failed;
+	}
+
+	l_debug("Authentication successful");
+
+	return;
+
+auth_confirm_failed:
+	dpp->state = DPP_STATE_PRESENCE;
+	dpp_free_auth_data(dpp);
+}
+
+static void dpp_auth_request_failed(struct dpp_sm *dpp,
+					enum dpp_status status,
+					void *k1)
+{
+	uint8_t hdr[32];
+	uint8_t attrs[128];
+	uint8_t *ptr = attrs;
+	uint8_t version = 2;
+	uint8_t r_capabilities = 0x01;
+	uint8_t s = status;
+	struct iovec iov[2];
+
+	iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev),
+				dpp->auth_addr,
+				DPP_FRAME_AUTHENTICATION_RESPONSE, hdr);
+	iov[0].iov_base = hdr;
+
+	ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &s, 1);
+	ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH,
+				dpp->pub_boot_hash, 32);
+
+	ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, &version, 1);
+
+	ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs,
+			ptr, sizeof(attrs) - (ptr - attrs), k1, dpp->key_len, 2,
+			DPP_ATTR_INITIATOR_NONCE, dpp->nonce_len, dpp->i_nonce,
+			DPP_ATTR_RESPONDER_CAPABILITIES, 1, &r_capabilities);
+
+	iov[1].iov_base = attrs;
+	iov[1].iov_len = ptr - attrs;
+
+	dpp_send_frame(netdev_get_wdev_id(dpp->netdev), iov, 2,
+				dpp->current_freq);
+}
+
+static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
+					const uint8_t *body, size_t body_len)
+{
+	struct dpp_attr_iter iter;
+	enum dpp_attribute_type type;
+	size_t len;
+	const uint8_t *data;
+	const uint8_t *r_boot = NULL;
+	const uint8_t *i_boot = NULL;
+	const uint8_t *i_proto = NULL;
+	const void *wrapped = NULL;
+	const uint8_t *i_nonce = NULL;
+	size_t r_boot_len = 0, i_proto_len = 0, wrapped_len = 0;
+	size_t i_nonce_len = 0;
+	_auto_(l_free) uint8_t *unwrapped = NULL;
+	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *m = NULL;
+	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL;
+	uint64_t k1[L_ECC_MAX_DIGITS];
+	uint64_t r_auth[L_ECC_MAX_DIGITS];
+	const void *ad0 = body + 2;
+	const void *ad1 = body + 8;
+
+	if (dpp->state != DPP_STATE_PRESENCE)
+		return;
+
+	l_debug("authenticate request");
+
+	dpp_attr_iter_init(&iter, body + 8, body_len - 8);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_INITIATOR_BOOT_KEY_HASH:
+			i_boot = data;
+			/*
+			 * This attribute is required by the spec, but only
+			 * used for mutual authentication.
+			 */
+			break;
+		case DPP_ATTR_RESPONDER_BOOT_KEY_HASH:
+			r_boot = data;
+			r_boot_len = len;
+			break;
+		case DPP_ATTR_INITIATOR_PROTOCOL_KEY:
+			i_proto = data;
+			i_proto_len = len;
+			break;
+		case DPP_ATTR_WRAPPED_DATA:
+			/* I-Nonce/I-Capabilities part of wrapped data */
+			wrapped = data;
+			wrapped_len = len;
+			break;
+
+		/* Optional attributes */
+		case DPP_ATTR_PROTOCOL_VERSION:
+			if (l_get_u8(data) != 2) {
+				l_debug("Protocol version did not match");
+				return;
+			}
+
+			break;
+		/*
+		 * TODO: Go on this channel for remainder of auth protocol.
+		 *
+		 * "the Responder determines whether it can use the requested
+		 * channel for the following exchanges. If so, it sends the DPP
+		 * Authentication Response frame on that channel. If not, it
+		 * discards the DPP Authentication Request frame without
+		 * replying to it."
+		 *
+		 * For the time being this feature is not being implemented and
+		 * the frame will be dropped.
+		 */
+		case DPP_ATTR_CHANNEL:
+			return;
+		default:
+			break;
+		}
+	}
+
+	if (!r_boot || !i_boot || !i_proto || !wrapped)
+		goto auth_request_failed;
+
+	if (r_boot_len != 32 || memcmp(dpp->pub_boot_hash,
+					r_boot, r_boot_len)) {
+		l_debug("Responder boot key hash failed to verify");
+		goto auth_request_failed;
+	}
+
+	dpp->i_proto_public = l_ecc_point_from_data(dpp->curve,
+						L_ECC_POINT_TYPE_FULL,
+						i_proto, i_proto_len);
+	if (!dpp->i_proto_public) {
+		l_debug("Initiators protocol key invalid");
+		goto auth_request_failed;
+	}
+
+	m = dpp_derive_k1(dpp->i_proto_public, dpp->boot_private, k1);
+	if (!m)
+		goto auth_request_failed;
+
+	unwrapped = dpp_unwrap_attr(ad0, 6, ad1, wrapped - 4 - ad1,
+			k1, dpp->key_len, wrapped, wrapped_len, &wrapped_len);
+	if (!unwrapped)
+		goto auth_request_failed;
+
+	dpp_attr_iter_init(&iter, unwrapped, wrapped_len);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_INITIATOR_NONCE:
+			i_nonce = data;
+			i_nonce_len = len;
+			break;
+		case DPP_ATTR_INITIATOR_CAPABILITIES:
+			/*
+			 * "If the Responder is not capable of supporting the
+			 * role indicated by the Initiator, it shall respond
+			 * with a DPP Authentication Response frame indicating
+			 * failure by adding the DPP Status field set to
+			 * STATUS_NOT_COMPATIBLE"
+			 */
+			if (!(l_get_u8(data) & 0x2)) {
+				l_debug("Initiator is not configurator");
+
+				dpp_auth_request_failed(dpp,
+						DPP_STATUS_NOT_COMPATIBLE, k1);
+				goto auth_request_failed;
+			}
+
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (i_nonce_len != dpp->nonce_len) {
+		l_debug("I-Nonce has unexpected length %lu", i_nonce_len);
+		goto auth_request_failed;
+	}
+
+	memcpy(dpp->i_nonce, i_nonce, i_nonce_len);
+
+	/* Derive keys k2, ke, and R-Auth for authentication response */
+
+	l_ecdh_generate_key_pair(dpp->curve, &dpp->proto_private,
+					&dpp->proto_public);
+
+	n = dpp_derive_k2(dpp->i_proto_public, dpp->proto_private, dpp->k2);
+	if (!n)
+		goto auth_request_failed;
+
+	l_getrandom(dpp->r_nonce, dpp->nonce_len);
+
+	if (!dpp_derive_ke(dpp->i_nonce, dpp->r_nonce, m, n, dpp->ke))
+		goto auth_request_failed;
+
+	if (!dpp_derive_r_auth(dpp->i_nonce, dpp->r_nonce, dpp->nonce_len,
+				dpp->i_proto_public, dpp->proto_public,
+				dpp->boot_public, r_auth))
+		goto auth_request_failed;
+
+	memcpy(dpp->auth_addr, from, 6);
+
+	dpp->state = DPP_STATE_AUTHENTICATING;
+
+	send_authenticate_response(dpp, r_auth);
+
+	return;
+
+auth_request_failed:
+	dpp->state = DPP_STATE_PRESENCE;
+	dpp_free_auth_data(dpp);
+}
+
+static void dpp_handle_auth_frame(const struct mmpdu_header *frame,
+				const void *body, size_t body_len,
+				int rssi, void *user_data)
+{
+	struct dpp_sm *dpp = user_data;
+	const uint8_t *ptr;
+
+	/*
+	 * Both handlers offset by 8 bytes to reach the beginning of the DPP
+	 * attributes. Easier checking this in one place, which also covers the
+	 * frame type byte.
+	 */
+	if (body_len < 8)
+		return;
+
+	ptr = body + 7;
+
+	switch (*ptr) {
+	case DPP_FRAME_AUTHENTICATION_REQUEST:
+		authenticate_request(dpp, frame->address_2, body, body_len);
+		break;
+	case DPP_FRAME_AUTHENTICATION_CONFIRM:
+		authenticate_confirm(dpp, frame->address_2, body, body_len);
+		break;
+	default:
+		l_debug("Unhandled DPP frame %u", *ptr);
+		break;
+	}
+}
+
 static void dpp_presence_announce(struct dpp_sm *dpp)
 {
 	struct netdev *netdev = dpp->netdev;
@@ -206,6 +692,7 @@ static void dpp_create(struct netdev *netdev)
 {
 	struct l_dbus *dbus = dbus_get_bus();
 	struct dpp_sm *dpp = l_new(struct dpp_sm, 1);
+	uint8_t dpp_prefix[] = { 0x04, 0x09, 0x50, 0x6f, 0x9a, 0x1a, 0x01 };
 
 	dpp->netdev = netdev;
 	dpp->state = DPP_STATE_NOTHING;
@@ -224,6 +711,10 @@ static void dpp_create(struct netdev *netdev)
 
 	l_dbus_object_add_interface(dbus, netdev_get_path(netdev),
 					IWD_DPP_INTERFACE, dpp);
+
+	frame_watch_add(netdev_get_wdev_id(netdev), 0, 0x00d0, dpp_prefix,
+				sizeof(dpp_prefix), dpp_handle_auth_frame,
+				dpp, NULL);
 }
 
 static void dpp_netdev_watch(struct netdev *netdev,
Signature · 1 line
2.31.1
Applied
#2 lore raw

Re: [PATCH v5 2/4] dpp: add DPP authentication protocol

Hi James,

Quoted 18 lines · James Prestwood

This implements the DPP protocol used to authenticate to a DPP configurator.

Note this is not a full implementation of the protocol and there are a few missing features which will be added as needed:

- Mutual authentication (needed for BLE bootstrapping)
- Configurator support
- Initiator role

---
  src/dpp.c | 491 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 491 insertions(+)

v5:
* Updated use of wrap/unwrap APIs.
* Better commented how AAD is used with wrapping/unwrapping

<snip>

Quoted 6 lines
src/dpp.c +6 -0
+static void dpp_free_auth_data(struct dpp_sm *dpp)
+{
+	if (dpp->proto_public) {
+		l_ecc_point_free(dpp->proto_public);
+		dpp->proto_public = NULL;
+	}

src/dpp.c struct dpp_sm { · line 112

Quoted 10 lines
src/dpp.c +10 -0
+
+	if (dpp->proto_private) {
+		l_ecc_scalar_free(dpp->proto_private);
+		dpp->proto_private = NULL;
+	}
+
+	if (dpp->i_proto_public) {
+		l_ecc_point_free(dpp->i_proto_public);
+		dpp->i_proto_public = NULL;
+	}

Should we be paranoid and explicit_bzero the nonces and keys?

Applied, thanks.

Regards,
-Denis

author
#3 lore raw

Re: [PATCH v5 2/4] dpp: add DPP authentication protocol

Quoted 35 lines · Denis Kenzior

Hi James,

Quoted 19 lines · James Prestwood

This implements the DPP protocol used to authenticate to a DPP configurator.

Note this is not a full implementation of the protocol and there are a few missing features which will be added as needed:

  - Mutual authentication (needed for BLE bootstrapping)
  - Configurator support
  - Initiator role
---
  src/dpp.c | 491
++++++++++++++++++++++++++++++++++++++++++++++++++++++

  1 file changed, 491 insertions(+)

v5:
  * Updated use of wrap/unwrap APIs.
  * Better commented how AAD is used with wrapping/unwrapping

<snip>

Quoted 10 lines
src/dpp.c +10 -0
+static void dpp_free_auth_data(struct dpp_sm *dpp)
+{
+       if (dpp->proto_public) {
+               l_ecc_point_free(dpp->proto_public);
+               dpp->proto_public = NULL;
+       }
+
+       if (dpp->proto_private) {
+               l_ecc_scalar_free(dpp->proto_private);
+               dpp->proto_private = NULL;
Quoted 8 lines
Quoted 6 lines

+       }
+
+       if (dpp->i_proto_public) {
+               l_ecc_point_free(dpp->i_proto_public); +               dpp->i_proto_public = NULL;
+       }

Should we be paranoid and explicit_bzero the nonces and keys?

Yes, for keys/nonces we can do this. I'm thinking we should also add explicit_bzero to the l_ecc_point_free, as well as for scalars.

Quoted 5 lines

Applied, thanks.

Regards,
-Denis

author
#4 lore raw

PATCH v5 3/4 dpp: add support for configuration protocol

In reply to James Prestwood

This is a minimal implementation only supporting legacy network configuration, i.e. only SSID and PSK/passphrase are supported.

Missing features include:
- Fragmentation/comeback delay support
- DPP AKM support
- 8021x/PKEX support

---
 src/dpp.c | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 328 insertions(+)

v5:
* Updated use of wrap/unwrap APIs
* Added comments about how AAD is used with configuration

src/dpp.c +328 -0
index dd17cc47..cf322e7d 100644
@@ -44,6 +44,8 @@
 #include "src/crypto.h"
 #include "src/mpdu.h"
 #include "ell/useful.h"
+#include "src/common.h"
+#include "src/storage.h"
 
 static uint32_t netdev_watch;
 static struct l_genl_family *nl80211;
@@ -53,6 +55,7 @@ enum dpp_state {
 	DPP_STATE_NOTHING,
 	DPP_STATE_PRESENCE,
 	DPP_STATE_AUTHENTICATING,
+	DPP_STATE_CONFIGURING,
 };
 
 struct dpp_sm {
@@ -84,6 +87,7 @@ struct dpp_sm {
 	uint8_t auth_addr[6];
 	uint8_t r_nonce[32];
 	uint8_t i_nonce[32];
+	uint8_t e_nonce[32];
 
 	uint64_t ke[L_ECC_MAX_DIGITS];
 	uint64_t k2[L_ECC_MAX_DIGITS];
@@ -92,6 +96,8 @@ struct dpp_sm {
 	struct l_ecc_point *proto_public;
 
 	struct l_ecc_point *i_proto_public;
+
+	uint8_t diag_token;
 };
 
 static void dpp_free_auth_data(struct dpp_sm *dpp)
@@ -203,6 +209,321 @@ static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest,
 	return ptr - buf;
 }
 
+static size_t dpp_build_config_header(const uint8_t *src, const uint8_t *dest,
+					uint8_t diag_token,
+					uint8_t buf[static 37])
+{
+	uint8_t *ptr = buf + 24;
+
+	memset(buf, 0, 37);
+
+	l_put_le16(0x00d0, buf);
+	memcpy(buf + 4, dest, 6);
+	memcpy(buf + 10, src, 6);
+	memcpy(buf + 16, broadcast, 6);
+
+	*ptr++ = 0x04; /* Public */
+	*ptr++ = 0x0a; /* Action */
+	*ptr++ = diag_token;
+
+	*ptr++ = IE_TYPE_ADVERTISEMENT_PROTOCOL;
+	*ptr++ = 8; /* len */
+	*ptr++ = 0x00;
+	*ptr++ = IE_TYPE_VENDOR_SPECIFIC;
+	*ptr++ = 5;
+	memcpy(ptr, wifi_alliance_oui, 3);
+	ptr += 3;
+	*ptr++ = 0x1a;
+	*ptr++ = 1;
+
+	return ptr - buf;
+}
+
+/*
+ * The configuration protocols use of AD components is somewhat confusing
+ * since the request/response frames are of a different format than the rest.
+ * In addition there are situations where the components length is zero yet it
+ * is still passed as such to AES-SIV.
+ *
+ * For the configuration request/response frames:
+ *
+ * "AAD for use with AES-SIV for protected messages in the DPP Configuration
+ * protocol shall consist of all octets in the Query Request and Query Response
+ * fields up to the first octet of the Wrapped Data attribute, which is the last
+ * attribute in a DPP Configuration frame. When the number of octets of AAD is
+ * zero, the number of components of AAD passed to AES-SIV is zero."
+ *
+ *  - For configuration requests the optional query request field is not
+ *    included, therefore no AAD data is passed. (dpp_configuration_start)
+ *
+ *  - The configuration response does contain a query response field which is
+ *    5 bytes. (dpp_handle_config_response_frame)
+ *
+ * For the configuration result/status, the same rules are used as the
+ * authentication protocol. This is reiterated in section 6.4.1.
+ *
+ *  - For the configuration result there is some confusion as to exactly how the
+ *    second AAD component should be passed (since the spec specifically
+ *    mentions using two components). There are no attributes prior to the
+ *    wrapped data component meaning the length would be zero.
+ *    Hostapd/wpa_supplicant pass a zero length AAD component to AES-SIV which
+ *    does effect the resulting encryption/decryption so this is also what IWD
+ *    will do to remain compliant with it.
+ */
+static void dpp_configuration_start(struct dpp_sm *dpp, const uint8_t *addr)
+{
+	const char *json = "{\"name\":\"IWD\",\"wi-fi_tech\":\"infra\","
+				"\"netRole\":\"sta\"}";
+	struct iovec iov[3];
+	uint8_t hdr[37];
+	uint8_t attrs[512];
+	size_t json_len = strlen(json);
+	uint8_t *ptr = attrs;
+
+	l_getrandom(&dpp->diag_token, 1);
+
+	iov[0].iov_len = dpp_build_config_header(
+					netdev_get_address(dpp->netdev),
+					addr, dpp->diag_token, hdr);
+	iov[0].iov_base = hdr;
+
+	l_getrandom(dpp->e_nonce, dpp->nonce_len);
+
+	/* length */
+	ptr += 2;
+
+	/*
+	 * "AAD for use with AES-SIV for protected messages in the DPP
+	 * Configuration protocol shall consist of all octets in the Query
+	 * Request and Query Response fields up to the first octet of the
+	 * Wrapped Data attribute"
+	 *
+	 * In this case there is no query request/response fields, nor any
+	 * attributes besides wrapped data meaning zero AD components.
+	 */
+	ptr += dpp_append_wrapped_data(NULL, 0, NULL, 0, ptr, sizeof(attrs),
+			dpp->ke, dpp->key_len, 2,
+			DPP_ATTR_ENROLLEE_NONCE, dpp->nonce_len, dpp->e_nonce,
+			DPP_ATTR_CONFIGURATION_REQUEST, json_len, json);
+
+	l_put_le16(ptr - attrs - 2, attrs);
+
+	iov[1].iov_base = attrs;
+	iov[1].iov_len = ptr - attrs;
+
+	dpp->state = DPP_STATE_CONFIGURING;
+
+	dpp_send_frame(dpp->wdev_id, iov, 2, dpp->current_freq);
+}
+
+static void send_config_result(struct dpp_sm *dpp, const uint8_t *to)
+{
+	uint8_t hdr[32];
+	struct iovec iov[2];
+	uint8_t attrs[256];
+	uint8_t *ptr = attrs;
+	uint8_t zero = 0;
+
+	iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), to,
+					DPP_FRAME_CONFIGURATION_RESULT, hdr);
+	iov[0].iov_base = hdr;
+
+	ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, 0, ptr,
+			sizeof(attrs), dpp->ke, dpp->key_len, 2,
+			DPP_ATTR_STATUS, 1, &zero,
+			DPP_ATTR_ENROLLEE_NONCE, dpp->nonce_len, dpp->e_nonce);
+
+	iov[1].iov_base = attrs;
+	iov[1].iov_len = ptr - attrs;
+
+	dpp_send_frame(dpp->wdev_id, iov, 2, dpp->current_freq);
+}
+
+static void dpp_write_config(struct dpp_configuration *config)
+{
+	_auto_(l_free) char *ssid = l_malloc(config->ssid_len + 1);
+	_auto_(l_settings_free) struct l_settings *settings = l_settings_new();
+	_auto_(l_free) char *path;
+
+	memcpy(ssid, config->ssid, config->ssid_len);
+	ssid[config->ssid_len] = '\0';
+
+	path = storage_get_network_file_path(SECURITY_PSK, ssid);
+
+	if (l_settings_load_from_file(settings, path)) {
+		/* Remove any existing Security keys */
+		l_settings_remove_group(settings, "Security");
+	}
+
+	if (config->passphrase)
+		l_settings_set_string(settings, "Security", "Passphrase",
+				config->passphrase);
+	else if (config->psk)
+		l_settings_set_string(settings, "Security", "PreSharedKey",
+				config->psk);
+
+	l_debug("Storing credential for '%s(%s)'", ssid,
+						security_to_str(SECURITY_PSK));
+	storage_network_sync(SECURITY_PSK, ssid, settings);
+}
+
+static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
+				const void *body, size_t body_len,
+				int rssi, void *user_data)
+{
+	struct dpp_sm *dpp = user_data;
+	const uint8_t *ptr = body;
+	uint16_t status;
+	uint16_t fragmented; /* Fragmented/Comeback delay field */
+	uint8_t adv_protocol_element[] = { 0x6C, 0x08, 0x7F };
+	uint8_t adv_protocol_id[] = { 0xDD, 0x05, 0x50, 0x6F,
+					0x9A, 0x1A, 0x01 };
+	uint16_t query_len;
+	struct dpp_attr_iter iter;
+	enum dpp_attribute_type type;
+	size_t len;
+	const uint8_t *data;
+	const char *json = NULL;
+	size_t json_len = 0;
+	int dstatus = -1;
+	const uint8_t *wrapped = NULL;
+	const uint8_t *e_nonce = NULL;
+	size_t wrapped_len = 0;
+	_auto_(l_free) uint8_t *unwrapped = NULL;
+	struct dpp_configuration *config;
+	uint8_t ad0[] = { 0x00, 0x10, 0x01, 0x00, 0x05 };
+
+	if (dpp->state != DPP_STATE_CONFIGURING)
+		return;
+
+	ptr += 2;
+
+	/*
+	 * Can a configuration request come from someone other than who you
+	 * authenticated to?
+	 */
+	if (memcmp(dpp->auth_addr, frame->address_2, 6))
+		return;
+
+	if (*ptr++ != dpp->diag_token)
+		return;
+
+	status = l_get_le16(ptr);
+	ptr += 2;
+
+	if (status != 0) {
+		l_debug("Bad configuration status %u", status);
+		return;
+	}
+
+	fragmented = l_get_le16(ptr);
+	ptr += 2;
+
+	/*
+	 * TODO: handle 0x0001 (fragmented), as well as comeback delay.
+	 */
+	if (fragmented != 0) {
+		l_debug("Fragmented messages not currently supported");
+		return;
+	}
+
+	if (memcmp(ptr, adv_protocol_element, sizeof(adv_protocol_element))) {
+		l_debug("Invalid Advertisement protocol element");
+		return;
+	}
+
+	ptr += sizeof(adv_protocol_element);
+
+	if (memcmp(ptr, adv_protocol_id, sizeof(adv_protocol_id))) {
+		l_debug("Invalid Advertisement protocol ID");
+		return;
+	}
+
+	ptr += sizeof(adv_protocol_id);
+
+	query_len = l_get_le16(ptr);
+	ptr += 2;
+
+	dpp_attr_iter_init(&iter, ptr, query_len);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_STATUS:
+			dstatus = l_get_u8(data);
+			break;
+		case DPP_ATTR_WRAPPED_DATA:
+			wrapped = data;
+			wrapped_len = len;
+			break;
+		default:
+			/*
+			 * TODO: CSR Attribute
+			 */
+			break;
+		}
+	}
+
+	if (dstatus != DPP_STATUS_OK || !wrapped) {
+		l_debug("Bad status or missing attributes");
+		return;
+	}
+
+	unwrapped = dpp_unwrap_attr(ad0, sizeof(ad0), NULL, 0, dpp->ke,
+					dpp->key_len, wrapped, wrapped_len,
+					&wrapped_len);
+	if (!unwrapped) {
+		l_debug("Failed to unwrap");
+		return;
+	}
+
+	dpp_attr_iter_init(&iter, unwrapped, wrapped_len);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_ENROLLEE_NONCE:
+			if (len != dpp->nonce_len)
+				break;
+
+			if (memcmp(data, dpp->e_nonce, dpp->nonce_len))
+				break;
+
+			e_nonce = data;
+			break;
+		case DPP_ATTR_CONFIGURATION_OBJECT:
+			json = (const char *)data;
+			json_len = len;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (!json || !e_nonce) {
+		l_debug("No configuration object in response");
+		return;
+	}
+
+	config = dpp_parse_configuration_object(json, json_len);
+	if (!config) {
+		l_error("Configuration object did not parse");
+		return;
+	}
+
+	dpp_write_config(config);
+	/*
+	 * TODO: Depending on the info included in the configuration object a
+	 * limited scan could be issued to get autoconnect to trigger faster.
+	 * In addition this network may already be in past scan results and
+	 * could be joined immediately.
+	 *
+	 * For now just wait for autoconnect.
+	 */
+
+	dpp_configuration_free(config);
+
+	send_config_result(dpp, dpp->auth_addr);
+}
+
 /*
  * The Authentication protocol has a consistent use of AD components, and this
  * use is defined in 6.3.1.4:
@@ -407,6 +728,8 @@ static void authenticate_confirm(struct dpp_sm *dpp, const uint8_t *from,
 
 	l_debug("Authentication successful");
 
+	dpp_configuration_start(dpp, from);
+
 	return;
 
 auth_confirm_failed:
@@ -693,6 +1016,7 @@ static void dpp_create(struct netdev *netdev)
 	struct l_dbus *dbus = dbus_get_bus();
 	struct dpp_sm *dpp = l_new(struct dpp_sm, 1);
 	uint8_t dpp_prefix[] = { 0x04, 0x09, 0x50, 0x6f, 0x9a, 0x1a, 0x01 };
+	uint8_t dpp_conf_response_prefix[] = { 0x04, 0x0b };
 
 	dpp->netdev = netdev;
 	dpp->state = DPP_STATE_NOTHING;
@@ -715,6 +1039,10 @@ static void dpp_create(struct netdev *netdev)
 	frame_watch_add(netdev_get_wdev_id(netdev), 0, 0x00d0, dpp_prefix,
 				sizeof(dpp_prefix), dpp_handle_auth_frame,
 				dpp, NULL);
+	frame_watch_add(netdev_get_wdev_id(netdev), 0, 0x00d0,
+				dpp_conf_response_prefix,
+				sizeof(dpp_conf_response_prefix),
+				dpp_handle_config_response_frame, dpp, NULL);
 }
 
 static void dpp_netdev_watch(struct netdev *netdev,
Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH v5 3/4] dpp: add support for configuration protocol

Hi James,

Quoted 15 lines · James Prestwood

This is a minimal implementation only supporting legacy network configuration, i.e. only SSID and PSK/passphrase are supported.

Missing features include:
- Fragmentation/comeback delay support
- DPP AKM support
- 8021x/PKEX support

---
  src/dpp.c | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 328 insertions(+)

v5:
* Updated use of wrap/unwrap APIs
* Added comments about how AAD is used with configuration

<snip>

Quoted 27 lines
src/dpp.c +27 -0
+static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
+				const void *body, size_t body_len,
+				int rssi, void *user_data)
+{
+	struct dpp_sm *dpp = user_data;
+	const uint8_t *ptr = body;
+	uint16_t status;
+	uint16_t fragmented; /* Fragmented/Comeback delay field */
+	uint8_t adv_protocol_element[] = { 0x6C, 0x08, 0x7F };
+	uint8_t adv_protocol_id[] = { 0xDD, 0x05, 0x50, 0x6F,
+					0x9A, 0x1A, 0x01 };
+	uint16_t query_len;
+	struct dpp_attr_iter iter;
+	enum dpp_attribute_type type;
+	size_t len;
+	const uint8_t *data;
+	const char *json = NULL;
+	size_t json_len = 0;
+	int dstatus = -1;
+	const uint8_t *wrapped = NULL;
+	const uint8_t *e_nonce = NULL;
+	size_t wrapped_len = 0;
+	_auto_(l_free) uint8_t *unwrapped = NULL;
+	struct dpp_configuration *config;
+	uint8_t ad0[] = { 0x00, 0x10, 0x01, 0x00, 0x05 };
+
+	if (dpp->state != DPP_STATE_CONFIGURING)

src/dpp.c static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, · line 406

Quoted 10 lines
src/dpp.c +10 -0
+		return;
+
+	ptr += 2;
+
+	/*
+	 * Can a configuration request come from someone other than who you
+	 * authenticated to?
+	 */
+	if (memcmp(dpp->auth_addr, frame->address_2, 6))
+		return;

Hmm, I'm not seeing any length checking?

Quoted 30 lines
src/dpp.c +30 -0
+
+	if (*ptr++ != dpp->diag_token)
+		return;
+
+	status = l_get_le16(ptr);
+	ptr += 2;
+
+	if (status != 0) {
+		l_debug("Bad configuration status %u", status);
+		return;
+	}
+
+	fragmented = l_get_le16(ptr);
+	ptr += 2;
+
+	/*
+	 * TODO: handle 0x0001 (fragmented), as well as comeback delay.
+	 */
+	if (fragmented != 0) {
+		l_debug("Fragmented messages not currently supported");
+		return;
+	}
+
+	if (memcmp(ptr, adv_protocol_element, sizeof(adv_protocol_element))) {
+		l_debug("Invalid Advertisement protocol element");
+		return;
+	}
+
+	ptr += sizeof(adv_protocol_element);
+

src/dpp.c static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, · line 446

Quoted 10 lines
src/dpp.c +10 -0
+	if (memcmp(ptr, adv_protocol_id, sizeof(adv_protocol_id))) {
+		l_debug("Invalid Advertisement protocol ID");
+		return;
+	}
+
+	ptr += sizeof(adv_protocol_id);
+
+	query_len = l_get_le16(ptr);
+	ptr += 2;
+

How do we know query_len is valid for

src/dpp.c static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, · line 447

Quoted 1 line
src/dpp.c +1 -0
+	dpp_attr_iter_init(&iter, ptr, query_len);

this invocation?

Quoted 72 lines
src/dpp.c +72 -0
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_STATUS:
+			dstatus = l_get_u8(data);
+			break;
+		case DPP_ATTR_WRAPPED_DATA:
+			wrapped = data;
+			wrapped_len = len;
+			break;
+		default:
+			/*
+			 * TODO: CSR Attribute
+			 */
+			break;
+		}
+	}
+
+	if (dstatus != DPP_STATUS_OK || !wrapped) {
+		l_debug("Bad status or missing attributes");
+		return;
+	}
+
+	unwrapped = dpp_unwrap_attr(ad0, sizeof(ad0), NULL, 0, dpp->ke,
+					dpp->key_len, wrapped, wrapped_len,
+					&wrapped_len);
+	if (!unwrapped) {
+		l_debug("Failed to unwrap");
+		return;
+	}
+
+	dpp_attr_iter_init(&iter, unwrapped, wrapped_len);
+
+	while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+		switch (type) {
+		case DPP_ATTR_ENROLLEE_NONCE:
+			if (len != dpp->nonce_len)
+				break;
+
+			if (memcmp(data, dpp->e_nonce, dpp->nonce_len))
+				break;
+
+			e_nonce = data;
+			break;
+		case DPP_ATTR_CONFIGURATION_OBJECT:
+			json = (const char *)data;
+			json_len = len;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (!json || !e_nonce) {
+		l_debug("No configuration object in response");
+		return;
+	}
+
+	config = dpp_parse_configuration_object(json, json_len);
+	if (!config) {
+		l_error("Configuration object did not parse");
+		return;
+	}
+
+	dpp_write_config(config);
+	/*
+	 * TODO: Depending on the info included in the configuration object a
+	 * limited scan could be issued to get autoconnect to trigger faster.
+	 * In addition this network may already be in past scan results and
+	 * could be joined immediately.
+	 *
+	 * For now just wait for autoconnect.

src/dpp.c static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, · line 529

Quoted 10 lines
src/dpp.c +7 -0
+	 */
+
+	dpp_configuration_free(config);
+
+	send_config_result(dpp, dpp->auth_addr);
+}
+
  /*
   * The Authentication protocol has a consistent use of AD components, and this
   * use is defined in 6.3.1.4:

Regards,
-Denis

author
#6 lore raw

Re: [PATCH v5 3/4] dpp: add support for configuration protocol

Quoted 55 lines · Denis Kenzior

Hi James,

Quoted 16 lines · James Prestwood

This is a minimal implementation only supporting legacy network configuration, i.e. only SSID and PSK/passphrase are supported.

Missing features include:
  - Fragmentation/comeback delay support
  - DPP AKM support
  - 8021x/PKEX support
---
  src/dpp.c | 328
++++++++++++++++++++++++++++++++++++++++++++++++++++++

  1 file changed, 328 insertions(+)

v5:
  * Updated use of wrap/unwrap APIs
  * Added comments about how AAD is used with configuration

<snip>

Quoted 33 lines

+static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
+                               const void *body, size_t body_len, +                               int rssi, void *user_data)

src/dpp.c +29 -0
+{
+       struct dpp_sm *dpp = user_data;
+       const uint8_t *ptr = body;
+       uint16_t status;
+       uint16_t fragmented; /* Fragmented/Comeback delay field */
+       uint8_t adv_protocol_element[] = { 0x6C, 0x08, 0x7F };
+       uint8_t adv_protocol_id[] = { 0xDD, 0x05, 0x50, 0x6F,
+                                       0x9A, 0x1A, 0x01 };
+       uint16_t query_len;
+       struct dpp_attr_iter iter;
+       enum dpp_attribute_type type;
+       size_t len;
+       const uint8_t *data;
+       const char *json = NULL;
+       size_t json_len = 0;
+       int dstatus = -1;
+       const uint8_t *wrapped = NULL;
+       const uint8_t *e_nonce = NULL;
+       size_t wrapped_len = 0;
+       _auto_(l_free) uint8_t *unwrapped = NULL;
+       struct dpp_configuration *config;
+       uint8_t ad0[] = { 0x00, 0x10, 0x01, 0x00, 0x05 };
+
+       if (dpp->state != DPP_STATE_CONFIGURING)
+               return;
+
+       ptr += 2;
+
+       /*
Quoted 8 lines
Quoted 6 lines

+        * Can a configuration request come from someone other than who you
+        * authenticated to?
+        */
+       if (memcmp(dpp->auth_addr, frame->address_2, 6)) +               return;

Hmm, I'm not seeing any length checking?

Yeah, I will add a check for this entire header since its pretty static.

Quoted 48 lines
Quoted 39 lines
src/dpp.c +17 -0
+
+       if (*ptr++ != dpp->diag_token)
+               return;
+
+       status = l_get_le16(ptr);
+       ptr += 2;
+
+       if (status != 0) {
+               l_debug("Bad configuration status %u", status);
+               return;
+       }
+
+       fragmented = l_get_le16(ptr);
+       ptr += 2;
+
+       /*
+        * TODO: handle 0x0001 (fragmented), as well as comeback

delay.
+        */
+       if (fragmented != 0) {
+               l_debug("Fragmented messages not currently supported");
+               return;
+       }
+
+       if (memcmp(ptr, adv_protocol_element, sizeof(adv_protocol_element))) {
+               l_debug("Invalid Advertisement protocol element"); +               return;
+       }

src/dpp.c +4 -0
+
+       ptr += sizeof(adv_protocol_element);
+
+       if (memcmp(ptr, adv_protocol_id, sizeof(adv_protocol_id)))

{
+               l_debug("Invalid Advertisement protocol ID"); +               return;
+       }

src/dpp.c static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, · line 446

Quoted 6 lines
src/dpp.c +6 -0
+
+       ptr += sizeof(adv_protocol_id);
+
+       query_len = l_get_le16(ptr);
+       ptr += 2;
+

How do we know query_len is valid for

Quoted 3 lines
Quoted 1 line

+       dpp_attr_iter_init(&iter, ptr, query_len);

this invocation?

yeah, that was the idea. But body_len needs to come into play here since query_len could just as well be 0xFFFF.

Quoted 93 lines
Quoted 86 lines
src/dpp.c +24 -0
+
+       while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+               switch (type) {
+               case DPP_ATTR_STATUS:
+                       dstatus = l_get_u8(data);
+                       break;
+               case DPP_ATTR_WRAPPED_DATA:
+                       wrapped = data;
+                       wrapped_len = len;
+                       break;
+               default:
+                       /*
+                        * TODO: CSR Attribute
+                        */
+                       break;
+               }
+       }
+
+       if (dstatus != DPP_STATUS_OK || !wrapped) {
+               l_debug("Bad status or missing attributes");
+               return;
+       }
+
+       unwrapped = dpp_unwrap_attr(ad0, sizeof(ad0), NULL, 0, dpp-
Quoted 1 line

ke,

+                                       dpp->key_len, wrapped, wrapped_len,
+                                       &wrapped_len); +       if (!unwrapped) {
+               l_debug("Failed to unwrap");
+               return;
+       }

src/dpp.c +10 -0
+
+       dpp_attr_iter_init(&iter, unwrapped, wrapped_len);
+
+       while (dpp_attr_iter_next(&iter, &type, &len, &data)) {
+               switch (type) {
+               case DPP_ATTR_ENROLLEE_NONCE:
+                       if (len != dpp->nonce_len)
+                               break;
+
+                       if (memcmp(data, dpp->e_nonce, dpp-
Quoted 1 line

nonce_len))

+                               break;

src/dpp.c +26 -0
+
+                       e_nonce = data;
+                       break;
+               case DPP_ATTR_CONFIGURATION_OBJECT:
+                       json = (const char *)data;
+                       json_len = len;
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       if (!json || !e_nonce) {
+               l_debug("No configuration object in response");
+               return;
+       }
+
+       config = dpp_parse_configuration_object(json, json_len);
+       if (!config) {
+               l_error("Configuration object did not parse");
+               return;
+       }
+
+       dpp_write_config(config);
+       /*
+        * TODO: Depending on the info included in the

configuration object a
+        * limited scan could be issued to get autoconnect to trigger faster.
+        * In addition this network may already be in past scan results and
+        * could be joined immediately.
+        *
+        * For now just wait for autoconnect.
+        */

src/dpp.c +6 -0
+
+       dpp_configuration_free(config);
+
+       send_config_result(dpp, dpp->auth_addr);
+}
+
Quoted 4 lines

  /*
   * The Authentication protocol has a consistent use of AD components, and this
   * use is defined in 6.3.1.4:

Regards,
-Denis

author
#7 lore raw

PATCH v5 4/4 dpp: handle protocol errors in ROC timeout

In reply to James Prestwood

This is a standing TODO of properly handling these timeouts but for now just treat any ROC timeout as an error if authenticating or configuring.

---
 src/dpp.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
src/dpp.c +33 -3
index cf322e7d..f86ea6bd 100644
@@ -1070,10 +1070,34 @@ static void dpp_presence_timeout(int error, void *user_data)
 {
 	struct dpp_sm *dpp = user_data;
 
-	if (dpp->state != DPP_STATE_PRESENCE) {
-		l_debug("DPP state changed, stopping presence announcements");
-		dpp->freqs_idx = 0;
+	/*
+	 * If cancelled this is likely due to netdev going down or from Stop().
+	 * Otherwise there was some other problem which is probably not
+	 * recoverable.
+	 */
+	if (error == -ECANCELED)
 		return;
+	else if (error < 0)
+		goto protocol_failed;
+
+	switch (dpp->state) {
+	case DPP_STATE_PRESENCE:
+		break;
+	case DPP_STATE_NOTHING:
+		/* Protocol already terminated */
+		return;
+	case DPP_STATE_AUTHENTICATING:
+	case DPP_STATE_CONFIGURING:
+		/*
+		 * TODO: If either the auth or config protocol is running we
+		 * need to stay on channel until the specified timeouts.
+		 * Unfortunately the kernel makes this very inconvenient since
+		 * there is no way to stay on channel indefinitely or any way
+		 * of knowing what duration the kernel/card actually chooses.
+		 *
+		 * For now just treat this as a failure.
+		 */
+		goto protocol_failed;
 	}
 
 	dpp->freqs_idx++;
@@ -1091,6 +1115,12 @@ static void dpp_presence_timeout(int error, void *user_data)
 	dpp->offchannel_id = offchannel_start(netdev_get_wdev_id(dpp->netdev),
 			dpp->current_freq, dpp->dwell, dpp_roc_started,
 			dpp, dpp_presence_timeout);
+	return;
+
+protocol_failed:
+	dpp_reset(dpp);
+	dpp_free_auth_data(dpp);
+	return;
 }
 
 /*
Signature · 1 line
2.31.1
#8 lore raw

Re: [PATCH v5 4/4] dpp: handle protocol errors in ROC timeout

Hi James,

Quoted 8 lines · James Prestwood

This is a standing TODO of properly handling these timeouts but for now just treat any ROC timeout as an error if authenticating or configuring.

---
  src/dpp.c | 36 +++++++++++++++++++++++++++++++++---
  1 file changed, 33 insertions(+), 3 deletions(-)
src/dpp.c +0 -0

<snip>

src/dpp.c static void dpp_presence_timeout(int error, void *user_data) · line 1122

Quoted 9 lines
src/dpp.c +5 -0
@@ -1091,6 +1115,12 @@ static void dpp_presence_timeout(int error, void *user_data)
  	dpp->offchannel_id = offchannel_start(netdev_get_wdev_id(dpp->netdev),
  			dpp->current_freq, dpp->dwell, dpp_roc_started,
  			dpp, dpp_presence_timeout);
+	return;
+
+protocol_failed:
+	dpp_reset(dpp);
+	dpp_free_auth_data(dpp);

Isn't dpp_reset already calling dpp_free_auth_data?

src/dpp.c static void dpp_presence_timeout(int error, void *user_data) · line 1126

Quoted 5 lines
src/dpp.c +1 -0
+	return;
  }
 
  /*

Regards,
-Denis

Applied
#9 lore raw
In reply to James Prestwood

Hi James,

Quoted 6 lines · James Prestwood
---
  src/dpp-util.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++
  src/dpp-util.h |  10 ++++
  src/dpp.c      |  10 ----
  3 files changed, 153 insertions(+), 10 deletions(-)
Quoted 8 lines

v5:
* Moved these APIs into dpp-util instead of dpp.c so they can be unit tested
* Changed so the caller supplies ad0/ad1. This takes the burden off these utilities to sort out what type of frame, and any quirks of how AAD is supposed to be used (see comments in authentication and configuration patches)

Applied, thanks.

Regards,
-Denis