dpp-util: use ell/asn1-private.h for ASN1 generation
James Prestwood
PATCH 1/12 dpp-util: use ell/asn1-private.h for ASN1 generation
ASN1 parsing will soon be required which will need some utilities in asn1-private.h. To avoid duplication include this private header and replace the OID's with the defined structures as well as remove the duplicated macros.
--- src/dpp-util.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-)
src/dpp-util.c +25 -24
@@ -36,6 +36,7 @@#include "src/crypto.h" #include "src/json.h" #include "ell/useful.h" +#include "ell/asn1-private.h" #include "src/ie.h" static void append_freqs(struct l_string *uri,
@@ -694,32 +695,34 @@ bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce,return hkdf_expand (sha, bk, key_len,"DPP Key" , ke, key_len); } -#define ASN1_ID (class, pc, tag) (((class) << 6) | ((pc) << 5) | (tag)) - -#define ASN1_ID_SEQUENCE ASN1_ID(0, 1, 0x10) -#define ASN1_ID_BIT_STRING ASN1_ID(0, 0, 0x03) -#define ASN1_ID_OID ASN1_ID(0, 0, 0x06) -/* * Values derived from OID definitions in https://www.secg.org/sec2-v2.pdf * Appendix A.2.1 * * 1.2.840.10045.2.1 (ecPublicKey) */ -static uint8_t ec_oid[] = {0x2a ,0x86 ,0x48 ,0xce ,0x3d ,0x02 ,0x01 }; +static struct asn1_oid ec_oid = { + .asn1_len =7 , + .asn1 = {0x2a ,0x86 ,0x48 ,0xce ,0x3d ,0x02 ,0x01 } +};/* 1.2.840.10045.3.1.7 (prime256v1) */ -static uint8_t ec_p256_oid[] = {0x2a ,0x86 ,0x48 ,0xce , -0x3d ,0x03 ,0x01 ,0x07 }; +static struct asn1_oid ec_p256_oid = { + .asn1_len =8 , + .asn1 = {0x2a ,0x86 ,0x48 ,0xce ,0x3d ,0x03 ,0x01 ,0x07 } +}; +/* 1.3.132.0.34 (secp384r1) */ -static uint8_t ec_p384_oid[] = {0x2B ,0x81 ,0x04 ,0x00 ,0x22 }; +static struct asn1_oid ec_p384_oid = { + .asn1_len =5 , + .asn1 = {0x2B ,0x81 ,0x04 ,0x00 ,0x22 } +};uint8_t *dpp_point_to_asn1 (const struct l_ecc_point *p,size_t *len_out) {uint8_t *asn1;uint8_t *ptr; -uint8_t *type_oid; -size_t type_oid_len; +struct asn1_oid *key_type;const struct l_ecc_curve *curve =l_ecc_point_get_curve (p);ssize_t key_size =l_ecc_curve_get_scalar_bytes (curve);uint64_t x[L_ECC_MAX_DIGITS ];
@@ -729,12 +732,10 @@ uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out)switch (key_size) {case 32 : - type_oid = ec_p256_oid; - type_oid_len =sizeof (ec_p256_oid); + key_type = &ec_p256_oid;break ;case 48 : - type_oid = ec_p384_oid; - type_oid_len =sizeof (ec_p384_oid); + key_type = &ec_p384_oid;break ;default :return NULL ;
@@ -744,7 +745,7 @@ uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out)if (ret <0 || ret != key_size)return NULL ; - len =2 +sizeof (ec_oid) +2 + type_oid_len +2 + key_size +4 ; + len =2 + ec_oid.asn1_len +2 + key_type->asn1_len +2 + key_size +4 ;/* * Set the type to whatever avoids doing p - y when reading in the
@@ -769,19 +770,19 @@ uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out) *ptr++ =ASN1_ID_SEQUENCE ; - len =sizeof (ec_oid) + type_oid_len +4 ; + len = ec_oid.asn1_len + key_type->asn1_len +4 ; *ptr++ = len; *ptr++ =ASN1_ID_OID ; - *ptr++ =sizeof (ec_oid); -memcpy (ptr, ec_oid,sizeof (ec_oid)); - ptr +=sizeof (ec_oid); + *ptr++ = ec_oid.asn1_len; +memcpy (ptr, ec_oid.asn1, ec_oid.asn1_len); + ptr += ec_oid.asn1_len; *ptr++ =ASN1_ID_OID ; - *ptr++ = type_oid_len; -memcpy (ptr, type_oid, type_oid_len); - ptr += type_oid_len; + *ptr++ = key_type->asn1_len; +memcpy (ptr, key_type->asn1, key_type->asn1_len); + ptr += key_type->asn1_len; *ptr++ =ASN1_ID_BIT_STRING ; *ptr++ = key_size +2 ;
Signature · 1 line
2.31.1
James Prestwood
PATCH 2/12 dpp-util: add dpp_point_from_asn1()
Given an ASN1 blob of the right form, parse and create an l_ecc_point object. The form used is specific to DPP hence why this isn't general purpose and put into dpp-util.
--- src/dpp-util.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/dpp-util.h | 1 + 2 files changed, 70 insertions(+)
src/dpp-util.c +69 -0
@@ -796,3 +796,72 @@ uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out)return asn1; } + +/* +* Only checking for the ASN.1 form: +* +* SEQUENCE { +* SEQUENCE { +* OBJECT IDENTIFIER ecPublicKey +* OBJECT IDENTIFIER key type (p256/p384) +* } +* BITSTRING (key data) +* } +*/ +struct l_ecc_point *dpp_point_from_asn1 (const uint8_t *asn1,size_t len) +{ + +const uint8_t *outer_seq; +size_t outer_len; +const uint8_t *inner_seq; +size_t inner_len; +const uint8_t *elem; +const uint8_t *key_data; +size_t elen =0 ; +uint8_t tag; +unsigned int curve_num; +const struct l_ecc_curve *curve; + +/* SEQUENCE */ + outer_seq =asn1_der_find_elem (asn1, len,0 , &tag, &outer_len); +if (!outer_seq || tag !=ASN1_ID_SEQUENCE ) +return NULL ; + +/* SEQUENCE */ + inner_seq =asn1_der_find_elem (outer_seq, outer_len,0 , &tag, &inner_len); +if (!inner_seq || tag !=ASN1_ID_SEQUENCE ) +return NULL ; + +/* OBJECT IDENTIFIER (ecPublicKey) */ + elem =asn1_der_find_elem (inner_seq, inner_len,0 , &tag, &elen); +if (!elem || tag !=ASN1_ID_OID ) +return NULL ; + +/* Check that this OID is ecPublicKey */ +if (!asn1_oid_eq (&ec_oid, elen, elem)) +return NULL ; + + elem =asn1_der_find_elem (inner_seq, inner_len,1 , &tag, &elen); +if (!elem || tag !=ASN1_ID_OID ) +return NULL ; + +/* Check if ELL supports this curve */ +if (asn1_oid_eq (&ec_p256_oid, elen, elem)) + curve_num =19 ; +else if (asn1_oid_eq (&ec_p384_oid, elen, elem)) + curve_num =20 ; +else +return NULL ; + +/* BITSTRING */ + key_data =asn1_der_find_elem (outer_seq, outer_len,1 , &tag, &elen); +if (!key_data || tag !=ASN1_ID_BIT_STRING || elen <34 ) +return NULL ; + + curve =l_ecc_curve_from_ike_group (curve_num); +if (!curve) +return NULL ; + +return l_ecc_point_from_data (curve, key_data[1 ], + key_data +2 , elen -2 ); +}
src/dpp-util.h +1 -0
@@ -167,3 +167,4 @@ bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce, void *ke);uint8_t *dpp_point_to_asn1 (const struct l_ecc_point *p,size_t *len_out); +struct l_ecc_point *dpp_point_from_asn1 (const uint8_t *asn1,size_t len);
Signature · 1 line
2.31.1
Denis Kenzior
Re: [PATCH 02/12] dpp-util: add dpp_point_from_asn1()
Hi James,
Quoted 8 lines · James Prestwood
Given an ASN1 blob of the right form, parse and create an
l_ecc_pointobject. The form used is specific to DPP hence why this isn't general purpose and put into dpp-util.--- src/dpp-util.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/dpp-util.h | 1 + 2 files changed, 70 insertions(+)
<snip>
src/dpp-util.c uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out) · line 858
Quoted 3 lines
src/dpp-util.c +3
-0+/* BITSTRING */ + key_data =asn1_der_find_elem (outer_seq, outer_len,1 , &tag, &elen); +if (!key_data || tag !=ASN1_ID_BIT_STRING || elen <34 )
Why 34? Aren't you only concerned that elen >= 2?
src/dpp-util.c uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out) · line 863
Quoted 5 lines
src/dpp-util.c +5
-0+return NULL ; + + curve =l_ecc_curve_from_ike_group (curve_num); +if (!curve) +return NULL ;
Perhaps this check should be moved up?
src/dpp-util.c uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out) · line 866
Quoted 3 lines
src/dpp-util.c +3
-0+ +return l_ecc_point_from_data (curve, key_data[1 ], + key_data +2 , elen -2 );
Aren't you relying on ecc_point_from_data to validate the length? Hence the unnecessary '34' magic number above?
Should ecc_point_from_data also validate that all of the data passed in was consumed? Or maybe return the number of bytes consumed so that we can validate it here?
Regards,
-Denis
James Prestwood
Re: [PATCH 02/12] dpp-util: add dpp_point_from_asn1()
Quoted 13 lines · Denis Kenzior
Hi James,
Quoted 9 lines · James Prestwood
Given an ASN1 blob of the right form, parse and create an
l_ecc_pointobject. The form used is specific to DPP hence why this isn't general purpose and put into dpp-util.
---
src/dpp-util.c| 69
++++++++++++++++++++++++++++++++++++++++++++++++++
src/dpp-util.h| 1 +2 files changed, 70 insertions(+)
Quoted 8 lines
<snip>
Quoted 4 lines
+ /* BITSTRING */
+ key_data =asn1_der_find_elem(outer_seq, outer_len, 1, &tag, &elen);
+ if (!key_data || tag !=ASN1_ID_BIT_STRING|| elen < 34)Why 34? Aren't you only concerned that elen >= 2?
Yes, thats right.
Quoted 17 lines
Quoted 6 lines
+ return NULL;
+
+ curve =l_ecc_curve_from_ike_group(curve_num); + if (!curve)
+ return NULL;Perhaps this check should be moved up?
Quoted 3 lines
+
+ return l_ecc_point_from_data(curve, key_data[1], + key_data + 2, elen - 2);Aren't you relying on
ecc_point_from_datato validate the length? Hence the
unnecessary '34' magic number above?
Quoted 5 lines
Should
ecc_point_from_dataalso validate that all of the data passed in was
consumed? Or maybe return the number of bytes consumed so that we can validate
it here?
I think changing point_from_data to require a strict length is fine, and probably the best route. Only other way would be to manually check before calling it.
Quoted 3 lines
Regards,
-Denis
James Prestwood
PATCH 3/12 dpp-util: add URI parsing
In reply to James PrestwoodParses K (key), M (mac), C (class/channels), and V (version) tokens into a new structure dpp_uri_info. H/I are not parsed since there currently isn't any use for them.
--- src/dpp-util.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++ src/dpp-util.h | 13 +++ 2 files changed, 230 insertions(+)
src/dpp-util.c +217 -0
@@ -865,3 +865,220 @@ struct l_ecc_point *dpp_point_from_asn1(const uint8_t *asn1, size_t len)return l_ecc_point_from_data (curve, key_data[1 ], key_data +2 , elen -2 ); } + +/* +* Advances 'p' to the next character 'sep' plus one. strchr can be trusted to +* find the next character, but we do need to check that the next character + 1 +* isn't the NULL terminator, i.e. that data actually exists past this point. +*/ +#define TOKEN_NEXT (p, sep) \ +({ \ + const char *_next = strchr((p), (sep)); \ + if (_next) { \ + if (*(_next + 1) == '\0') \ + _next = NULL; \ + else \ + _next++; \ + } \ + _next; \ +}) + +/* +* Finds the length of the current token (characters until next 'sep'). If no +* 'sep' is found zero is returned. +*/ +#define TOKEN_LEN (p, sep) \ +({ \ + const char *_next = strchr((p), (sep)); \ + if (!_next) \ + _next = (p); \ + (_next - (p)); \ +}) + +/* +* Ensures 'p' points to something resembling a single character followed by +* ':' followed by at least one non-null byte of data. This allows the parse +* loop to safely advance the pointer to each tokens data (pos + 2) +*/ +#define TOKEN_OK (p) \ + ((p) && (p)[0] != '\0' && (p)[1] == ':' && (p)[2] != '\0') \ + +static struct scan_freq_set *dpp_parse_class_and_channel (const char *token) +{ +const char *pos = token; +char *end; +struct scan_freq_set *freqs =scan_freq_set_new (); + +/* Checking for <operclass>/<channel>,<operclass>/<channel>,... */ +for (; pos; pos =TOKEN_NEXT (pos,',' )) { +uint8_t channel; +uint8_t oper_class =strtol (pos, &end,10 ); + +if (!end || end == pos || (end && *end !='/' )) + goto free_set; + + pos = end +1 ; + channel =strtol (pos, &end,10 ); + +/* Either another pair (,) or end of this token (;) */ +if (!end || end == pos || (*end !=',' && *end !=';' )) + goto free_set; + +scan_freq_set_add (freqs,oci_to_frequency (oper_class, channel)); + } + +if (scan_freq_set_isempty (freqs)) { +free_set: +scan_freq_set_free (freqs); +return NULL ; + } + +return freqs; +} + +static int dpp_parse_mac (const char *str,uint8_t *mac_out) +{ +uint8_t mac[6 ]; +unsigned int i; + +for (i =0 ; i <12 ; i +=2 ) { +if (!l_ascii_isxdigit (str[i])) +return -EINVAL ; + +if (!l_ascii_isxdigit (str[i +1 ])) +return -EINVAL ; + } + +if (sscanf (str,"%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx" , + &mac[0 ], &mac[1 ], &mac[2 ], + &mac[3 ], &mac[4 ], &mac[5 ]) !=6 ) +return -EINVAL ; + +if (!util_is_valid_sta_address (mac)) +return -EINVAL ; + +memcpy (mac_out, mac,6 ); + +return 0 ; +} + +static int dpp_parse_version (const char *str,uint8_t *version_out) +{ +char *end; +uint8_t version; +size_t len =TOKEN_LEN (str,';' ); + +if (len !=1 ) +return -EINVAL ; + + version =strtol (str, &end,10 ); + +if (version !=1 && version !=2 ) +return -EINVAL ; + + *version_out = version; + +return 0 ; +} + +static struct l_ecc_point *dpp_parse_key (const char *str) +{ + _auto_(l_free) uint8_t *decoded = NULL; +size_t decoded_len; +size_t len =TOKEN_LEN (str,';' ); + +if (!len) +return NULL ; + + decoded =l_base64_decode (str, len, &decoded_len); +if (!decoded) +return NULL ; + +return dpp_point_from_asn1 (decoded, decoded_len); +} + +/* +* Parse a bootstrapping URI. This parses the tokens defined in the Easy Connect +* spec, and verifies they are the correct syntax. Some values have extra +* verification: +* - The bootstrapping key is base64 decoded and converted to an l_ecc_point +* - The operating class and channels are checked against the OCI table. +* - The version is checked to be either 1 or 2, as defined by the spec. +* - The MAC is verified to be a valid station address. +*/ +struct dpp_uri_info *dpp_parse_uri (const char *uri) +{ +struct dpp_uri_info *info; +const char *pos = uri; +const char *end = uri +strlen (uri) -1 ; +int ret =0 ; + +if (strncmp (pos,"DPP:" ,4 )) +return NULL ; + + info = l_new(struct dpp_uri_info, 1); + + pos +=4 ; + +/* EasyConnect 5.2.1 - Bootstrapping information format */ +for (;TOKEN_OK (pos); pos =TOKEN_NEXT (pos,';' )) { +switch (*pos) { +case 'C' : + info->freqs =dpp_parse_class_and_channel (pos +2 ); +if (!info->freqs) + goto free_info; +break ; +case 'M' : + ret =dpp_parse_mac (pos +2 , info->mac); +if (ret <0 ) + goto free_info; +break ; +case 'V' : + ret =dpp_parse_version (pos +2 , &info->version); +if (ret <0 ) + goto free_info; +break ; +case 'K' : + info->boot_public =dpp_parse_key (pos +2 ); +if (!info->boot_public) + goto free_info; +break ; +case 'H' : +case 'I' : +break ; +default : + goto free_info; + } + } + +/* Extra data found after last token */ +if (pos != end) + goto free_info; + +/* The public bootstrapping key is the only required token */ +if (!info->boot_public) + goto free_info; + +return info; + +free_info: +dpp_free_uri_info (info); +return NULL ; +} + +void dpp_free_uri_info (struct dpp_uri_info *info) +{ +if (info->freqs) +scan_freq_set_free (info->freqs); + +if (info->boot_public) +l_ecc_point_free (info->boot_public); + +if (info->information) +l_free (info->information); + +if (info->host) +l_free (info->host); + +l_free (info); +}
src/dpp-util.h +13 -0
@@ -22,6 +22,16 @@struct l_ecc_point ;struct l_ecc_scalar ;enum ie_rsn_akm_suite ; +struct scan_freq_set ; + +struct dpp_uri_info { +struct scan_freq_set *freqs; +struct l_ecc_point *boot_public; +uint8_t mac[6 ]; +char *information; +uint8_t version; +char *host; +};enum dpp_frame_type {DPP_FRAME_AUTHENTICATION_REQUEST =0 ,
@@ -168,3 +178,6 @@ bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce,uint8_t *dpp_point_to_asn1 (const struct l_ecc_point *p,size_t *len_out);struct l_ecc_point *dpp_point_from_asn1 (const uint8_t *asn1,size_t len); + +struct dpp_uri_info *dpp_parse_uri (const char *uri); +void dpp_free_uri_info (struct dpp_uri_info *info);
Signature · 1 line
2.31.1
James Prestwood
PATCH 4/12 unit: add DPP unit tests for URI parsing
In reply to James Prestwood--- unit/test-dpp.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+)
unit/test-dpp.c +142 -0
@@ -33,6 +33,134 @@#include "src/util.h" #include "ell/useful.h" +struct dpp_test_info { +const char *uri; +bool expect_fail; +uint32_t expected_freqs[10 ]; +struct dpp_uri_info result; +}; + +struct dpp_test_info all_values = { + .uri ="DPP:C:81/1,115/36;I:SN=4774LH2b4044;M:5254005828e5;V:2;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .result = { + .mac = {0x52 ,0x54 ,0x00 ,0x58 ,0x28 ,0xe5 }, + .version =2 , + }, + .expected_freqs = {2412 ,5180 ,0 } +}; + +struct dpp_test_info no_type = { + .uri ="C:81/1;K:shouldnotmatter;;" , + .expect_fail = true +}; + +struct dpp_test_info empty = { + .uri ="DPP:" , + .expect_fail = true +}; + +struct dpp_test_info no_key = { + .uri ="DPP:C:81/1,115/36;I:SN=4774LH2b4044;M:5254005828e5;V:2;;" , + .expect_fail = true +}; + +struct dpp_test_info data_after_terminator = { + .uri ="DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;C:81/1;;" , + .expect_fail = true +}; + +struct dpp_test_info single_terminator = { + .uri ="DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;" , + .expect_fail = true +}; + +struct dpp_test_info no_terminator = { + .uri ="DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=" , + .expect_fail = true +}; + +struct dpp_test_info bad_key = { + .uri ="DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0;;" , + .expect_fail = true +}; + +struct dpp_test_info unexpected_id = { + .uri ="DPP:Z:somedata;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true +}; + +struct dpp_test_info bad_channels[] = { + { + .uri ="DPP:C:;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, + { + .uri ="DPP:C:81;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, + { + .uri ="DPP:C:81/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, + { + .uri ="DPP:C:81/1,;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, + { + .uri ="DPP:C:81/1,81/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, + { + .uri ="DPP:C:81/1,/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;" , + .expect_fail = true + }, +}; + +static bool verify_info (const struct dpp_uri_info *parsed, +const struct dpp_test_info *result) +{ +const struct dpp_uri_info *expected = &result->result; +uint32_t i; + +assert (!memcmp (parsed->mac, expected->mac,6 )); +assert (parsed->version == expected->version); +assert (parsed->boot_public !=NULL ); + +for (i =0 ; result->expected_freqs[i]; i++) +assert (scan_freq_set_contains (parsed->freqs, + result->expected_freqs[i])); + +return true; +} + +static void test_uri_parse (const void *data) +{ +const struct dpp_test_info *test_info = data; +struct dpp_uri_info *info; +int ret; + + info =dpp_parse_uri (test_info->uri); +if (test_info->expect_fail) { +assert (info ==NULL ); +return ; + } + +assert (ret ==0 ); + +assert (verify_info (info, test_info)); + +dpp_free_uri_info (info); +} + + +static void test_bad_channels (const void *data) +{ +unsigned int i; + +for (i =0 ; i <L_ARRAY_SIZE (bad_channels); i++) +test_uri_parse (&bad_channels[i]); +} +/* * B.2 Test Vectors for DPP Authentication Using P-256 for * Responder-only Authentication
@@ -148,5 +276,19 @@ int main(int argc, char *argv[])l_test_add ("DPP test key derivation" , test_key_derivation,NULL ); +l_test_add ("DPP URI parse" , test_uri_parse, &all_values); +l_test_add ("DPP URI no type" , test_uri_parse, &no_type); +l_test_add ("DPP URI empty" , test_uri_parse, &empty); +l_test_add ("DPP URI no key" , test_uri_parse, &no_key); +l_test_add ("DPP URI data after terminator" , test_uri_parse, + &data_after_terminator); +l_test_add ("DPP URI single terminator" , test_uri_parse, + &single_terminator); +l_test_add ("DPP URI no terminator" , test_uri_parse, + &no_terminator); +l_test_add ("DPP URI bad key" , test_uri_parse, &bad_key); +l_test_add ("DPP URI bad channels" , test_bad_channels, &bad_channels); +l_test_add ("DPP URI unexpected ID" , test_uri_parse, &unexpected_id); +return l_test_run (); }
Signature · 1 line
2.31.1
James Prestwood
PATCH 5/12 dpp: use own/peer naming rather than i/r
In reply to James PrestwoodSeveral members are named based on initiator/responder (i/r) terminology. Eventually both initiator and responder will be supported so rename these members to use own/peer naming instead.
--- src/dpp.c | 70 +++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 35 deletions(-)
src/dpp.c +35 -35
@@ -81,9 +81,9 @@ struct dpp_sm {uint64_t wdev_id; -uint8_t *pub_asn1; -size_t pub_asn1_len; -uint8_t pub_boot_hash[32 ]; +uint8_t *own_asn1; +size_t own_asn1_len; +uint8_t own_boot_hash[32 ];const struct l_ecc_curve *curve;size_t key_len;size_t nonce_len;
@@ -109,12 +109,12 @@ struct dpp_sm {uint64_t ke[L_ECC_MAX_DIGITS ];uint64_t k2[L_ECC_MAX_DIGITS ]; -uint64_t r_auth[L_ECC_MAX_DIGITS ]; +uint64_t auth_tag[L_ECC_MAX_DIGITS ];struct l_ecc_scalar *proto_private; -struct l_ecc_point *proto_public; +struct l_ecc_point *own_proto_public; -struct l_ecc_point *i_proto_public; +struct l_ecc_point *peer_proto_public;uint8_t diag_token;
@@ -131,9 +131,9 @@ struct dpp_sm {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->own_proto_public) { +l_ecc_point_free (dpp->own_proto_public); + dpp->own_proto_public =NULL ; }if (dpp->proto_private) {
@@ -141,9 +141,9 @@ static void dpp_free_auth_data(struct dpp_sm *dpp) dpp->proto_private =NULL ; } -if (dpp->i_proto_public) { -l_ecc_point_free (dpp->i_proto_public); - dpp->i_proto_public =NULL ; +if (dpp->peer_proto_public) { +l_ecc_point_free (dpp->peer_proto_public); + dpp->peer_proto_public =NULL ; } }
@@ -189,7 +189,7 @@ static void dpp_reset(struct dpp_sm *dpp)explicit_bzero (dpp->e_nonce, dpp->nonce_len);explicit_bzero (dpp->ke, dpp->key_len);explicit_bzero (dpp->k2, dpp->key_len); -explicit_bzero (dpp->r_auth, dpp->key_len); +explicit_bzero (dpp->auth_tag, dpp->key_len);dpp_free_auth_data (dpp); }
@@ -198,9 +198,9 @@ static void dpp_free(struct dpp_sm *dpp) {dpp_reset (dpp); -if (dpp->pub_asn1) { -l_free (dpp->pub_asn1); - dpp->pub_asn1 =NULL ; +if (dpp->own_asn1) { +l_free (dpp->own_asn1); + dpp->own_asn1 =NULL ; }if (dpp->boot_public) {
@@ -1011,7 +1011,7 @@ static void send_authenticate_response(struct dpp_sm *dpp)uint8_t wrapped2[dpp->key_len +16 +8 ];size_t wrapped2_len; -l_ecc_point_get_data (dpp->proto_public, r_proto_key, +l_ecc_point_get_data (dpp->own_proto_public, r_proto_key,sizeof (r_proto_key)); iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev),
@@ -1021,7 +1021,7 @@ static void send_authenticate_response(struct dpp_sm *dpp) 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 ); + dpp->own_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 );
@@ -1029,7 +1029,7 @@ static void send_authenticate_response(struct dpp_sm *dpp)/* Wrap up secondary data (R-Auth) */ wrapped2_len =dpp_append_attr (wrapped2_plaintext,DPP_ATTR_RESPONDER_AUTH_TAG , - dpp->r_auth, dpp->key_len); + dpp->auth_tag, dpp->key_len);/* * "Invocations of AES-SIV in the DPP Authentication protocol that * produce ciphertext that is part of an additional AES-SIV invocation
@@ -1165,7 +1165,7 @@ static void authenticate_confirm(struct dpp_sm *dpp, const uint8_t *from, }dpp_derive_i_auth (dpp->r_nonce, dpp->i_nonce, dpp->nonce_len, - dpp->proto_public, dpp->i_proto_public, + dpp->own_proto_public, dpp->peer_proto_public, dpp->boot_public, i_auth_check);if (memcmp (i_auth, i_auth_check, i_auth_len)) {
@@ -1205,7 +1205,7 @@ static void dpp_auth_request_failed(struct dpp_sm *dpp, 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 ); + dpp->own_boot_hash,32 ); ptr +=dpp_append_attr (ptr,DPP_ATTR_PROTOCOL_VERSION , &version,1 );
@@ -1247,7 +1247,7 @@ static void dpp_presence_announce(struct dpp_sm *dpp) iov[0 ].iov_base = hdr;dpp_hash (L_CHECKSUM_SHA256 , hash,2 ,"chirp" ,strlen ("chirp" ), - dpp->pub_asn1, dpp->pub_asn1_len); + dpp->own_asn1, dpp->own_asn1_len); ptr +=dpp_append_attr (ptr,DPP_ATTR_RESPONDER_BOOT_KEY_HASH , hash,32 );
@@ -1463,21 +1463,21 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,if (!r_boot || !i_boot || !i_proto || !wrapped) goto auth_request_failed; -if (r_boot_len !=32 ||memcmp (dpp->pub_boot_hash, +if (r_boot_len !=32 ||memcmp (dpp->own_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, + dpp->peer_proto_public =l_ecc_point_from_data (dpp->curve,L_ECC_POINT_TYPE_FULL , i_proto, i_proto_len); -if (!dpp->i_proto_public) { +if (!dpp->peer_proto_public) {l_debug ("Initiators protocol key invalid" ); goto auth_request_failed; } - m =dpp_derive_k1 (dpp->i_proto_public, dpp->boot_private, k1); + m =dpp_derive_k1 (dpp->peer_proto_public, dpp->boot_private, k1);if (!m) goto auth_request_failed;
@@ -1527,9 +1527,9 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,/* Derive keys k2, ke, and R-Auth for authentication response */ l_ecdh_generate_key_pair (dpp->curve, &dpp->proto_private, - &dpp->proto_public); + &dpp->own_proto_public); - n =dpp_derive_k2 (dpp->i_proto_public, dpp->proto_private, dpp->k2); + n =dpp_derive_k2 (dpp->peer_proto_public, dpp->proto_private, dpp->k2);if (!n) goto auth_request_failed;
@@ -1539,8 +1539,8 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from, 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, dpp->r_auth)) + dpp->peer_proto_public, dpp->own_proto_public, + dpp->boot_public, dpp->auth_tag)) goto auth_request_failed;memcpy (dpp->auth_addr, from,6 );
@@ -1664,10 +1664,10 @@ static void dpp_create(struct netdev *netdev)l_ecdh_generate_key_pair (dpp->curve, &dpp->boot_private, &dpp->boot_public); - dpp->pub_asn1 =dpp_point_to_asn1 (dpp->boot_public, &dpp->pub_asn1_len); + dpp->own_asn1 =dpp_point_to_asn1 (dpp->boot_public, &dpp->own_asn1_len); -dpp_hash (L_CHECKSUM_SHA256 , dpp->pub_boot_hash,1 , - dpp->pub_asn1, dpp->pub_asn1_len); +dpp_hash (L_CHECKSUM_SHA256 , dpp->own_boot_hash,1 , + dpp->own_asn1, dpp->own_asn1_len);l_dbus_object_add_interface (dbus,netdev_get_path (netdev),IWD_DPP_INTERFACE , dpp);
@@ -1791,7 +1791,7 @@ static struct l_dbus_message *dpp_dbus_start_enrollee(struct l_dbus *dbus, }else if (!station)l_debug ("No station device, continuing anyways..." ); - dpp->uri =dpp_generate_uri (dpp->pub_asn1, dpp->pub_asn1_len,2 , + dpp->uri =dpp_generate_uri (dpp->own_asn1, dpp->own_asn1_len,2 ,netdev_get_address (dpp->netdev), &freq,1 ,NULL ,NULL );
@@ -1848,7 +1848,7 @@ static struct l_dbus_message *dpp_dbus_start_configurator(struct l_dbus *dbus,if (dpp->state !=DPP_STATE_NOTHING )return dbus_error_busy (message); - dpp->uri =dpp_generate_uri (dpp->pub_asn1, dpp->pub_asn1_len,2 , + dpp->uri =dpp_generate_uri (dpp->own_asn1, dpp->own_asn1_len,2 ,netdev_get_address (dpp->netdev), &bss->frequency,1 ,NULL ,NULL );
Signature · 1 line
2.31.1
Denis Kenzior
AppliedRe: [PATCH 05/12] dpp: use own/peer naming rather than i/r
Hi James,
Quoted 8 lines · James Prestwood
Several members are named based on initiator/responder (i/r) terminology. Eventually both initiator and responder will be supported so rename these members to use own/peer naming instead.
--- src/dpp.c | 70 +++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 35 deletions(-)
Applied, thanks.
Regards,
-Denis
James Prestwood
PATCH 6/12 frame-xchg: set allow multicast flag
In reply to James PrestwoodReceiving multicast frames is required by DPP
--- src/frame-xchg.c | 1 + 1 file changed, 1 insertion(+)
src/frame-xchg.c +1 -0
@@ -611,6 +611,7 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,l_genl_msg_append_attr (msg,NL80211_ATTR_WDEV ,8 , &wdev_id);l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_TYPE ,2 , &frame_type); +l_genl_msg_append_attr (msg,NL80211_ATTR_RECEIVE_MULTICAST ,0 ,NULL );l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_MATCH , prefix_len, prefix);
Signature · 1 line
2.31.1
Denis Kenzior
Re: [PATCH 06/12] frame-xchg: set allow multicast flag
Hi James,
Quoted 10 lines · James Prestwood
Receiving multicast frames is required by DPP
--- src/frame-xchg.c | 1 + 1 file changed, 1 insertion(+)src/frame-xchg.c +0
-0
src/frame-xchg.c bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type, · line 614
Quoted 4 lines
src/frame-xchg.c +1
-0@@ -611,6 +611,7 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,l_genl_msg_append_attr (msg,NL80211_ATTR_WDEV ,8 , &wdev_id);l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_TYPE ,2 , &frame_type); +l_genl_msg_append_attr (msg,NL80211_ATTR_RECEIVE_MULTICAST ,0 ,NULL );
Should this be opt-in? You may want to audit all uses of frame_watch_add to see if they can deal with multicast frames.
src/frame-xchg.c bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type, · line 617
Quoted 4 lines
src/frame-xchg.c +0
-0l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_MATCH , prefix_len, prefix);
Regards,
-Denis
James Prestwood
Re: [PATCH 06/12] frame-xchg: set allow multicast flag
Quoted 17 lines · Denis Kenzior
Hi James,
Quoted 14 lines · James Prestwood
Receiving multicast frames is required by DPP
---
src/frame-xchg.c| 1 +1 file changed, 1 insertion(+)src/frame-xchg.c +0
-0@@ -611,6 +611,7 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,
l_genl_msg_append_attr(msg,NL80211_ATTR_WDEV, 8,&wdev_id);
Quoted 8 lines
Quoted 4 lines
l_genl_msg_append_attr(msg,
NL80211_ATTR_FRAME_TYPE, 2,&frame_type);
+ l_genl_msg_append_attr(msg,NL80211_ATTR_RECEIVE_MULTICAST, 0, NULL);Should this be opt-in? You may want to audit all uses of
frame_watch_addto see
if they can deal with multicast frames.
I can make it dependent on a bool argument. Better than unnecissarily passing on broadcasts to modules that don't care.
Quoted 8 lines
Quoted 5 lines
l_genl_msg_append_attr(msg,
NL80211_ATTR_FRAME_MATCH,prefix_len, prefix);Regards,
-Denis
James Prestwood
PATCH 7/12 dpp: add support for initiating as a configurator
In reply to James PrestwoodThe difference between the existing code is that IWD will send the authentication request, making it the initiator.
This handles the use case where IWD is provided a peers URI containing its bootstrapping key rather than IWD always providing its own URI. A new DBus API was added, ConfigureEnrollee().
Using ConfigureEnrollee() IWD will act as a configurator but begin by traversing a channel list (URI provided or default) and waiting for presence announcements. When an announcement is received IWD will send an authentication request to the peer, receive its reply, and send an authentication confirm.
As with being a responder, IWD only supports configuration to the currently connected BSS and will request the enrollee switch to this BSS's frequency to preserve network performance.
--- src/dpp.c | 442 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 439 insertions(+), 3 deletions(-)
src/dpp.c +439 -3
@@ -83,20 +83,33 @@ struct dpp_sm {uint8_t *own_asn1;size_t own_asn1_len; +uint8_t *peer_asn1; +size_t peer_asn1_len;uint8_t own_boot_hash[32 ]; +uint8_t peer_boot_hash[32 ];const struct l_ecc_curve *curve;size_t key_len;size_t nonce_len;struct l_ecc_scalar *boot_private;struct l_ecc_point *boot_public; +struct l_ecc_point *peer_boot_public;enum dpp_state state; +/* +* List of frequencies to jump between. The presence of this list is +* also used to signify that a configurator is an initiator vs responder +*/ uint32_t *freqs;size_t freqs_len;size_t freqs_idx;uint32_t dwell;uint32_t current_freq; +/* +* Frequency the remainder of the auth/config protocol will run on. +* For an initiating configurator this is the frequency of the currently +* connected BSS. +*/ uint32_t new_freq;struct scan_freq_set *presence_list;
@@ -107,7 +120,9 @@ struct dpp_sm {uint8_t i_nonce[32 ];uint8_t e_nonce[32 ]; +struct l_ecc_scalar *m;uint64_t ke[L_ECC_MAX_DIGITS ]; +uint64_t k1[L_ECC_MAX_DIGITS ];uint64_t k2[L_ECC_MAX_DIGITS ];uint64_t auth_tag[L_ECC_MAX_DIGITS ];
@@ -145,6 +160,16 @@ static void dpp_free_auth_data(struct dpp_sm *dpp)l_ecc_point_free (dpp->peer_proto_public); dpp->peer_proto_public =NULL ; } + +if (dpp->peer_boot_public) { +l_ecc_point_free (dpp->peer_boot_public); + dpp->peer_boot_public =NULL ; + } + +if (dpp->m) { +l_ecc_scalar_free (dpp->m); + dpp->m =NULL ; + } }static void dpp_reset (struct dpp_sm *dpp)
@@ -188,6 +213,7 @@ static void dpp_reset(struct dpp_sm *dpp)explicit_bzero (dpp->i_nonce, dpp->nonce_len);explicit_bzero (dpp->e_nonce, dpp->nonce_len);explicit_bzero (dpp->ke, dpp->key_len); +explicit_bzero (dpp->k1, dpp->key_len);explicit_bzero (dpp->k2, dpp->key_len);explicit_bzero (dpp->auth_tag, dpp->key_len);
@@ -1260,6 +1286,49 @@ static void dpp_presence_announce(struct dpp_sm *dpp)dpp_send_frame (dpp, iov,2 , dpp->current_freq); } +static void dpp_send_authenticate_request (struct dpp_sm *dpp) +{ +uint8_t hdr[32 ]; +uint8_t attrs[256 ]; +uint8_t *ptr = attrs; +uint64_t i_proto_key[L_ECC_MAX_DIGITS *2 ]; +uint8_t version =2 ; +struct iovec iov[2 ]; + +l_ecc_point_get_data (dpp->own_proto_public, i_proto_key, +sizeof (i_proto_key)); + + iov[0 ].iov_len =dpp_build_header (netdev_get_address (dpp->netdev), + dpp->auth_addr, +DPP_FRAME_AUTHENTICATION_REQUEST , hdr); + iov[0 ].iov_base = hdr; + + ptr +=dpp_append_attr (ptr,DPP_ATTR_RESPONDER_BOOT_KEY_HASH , + dpp->peer_boot_hash,32 ); + ptr +=dpp_append_attr (ptr,DPP_ATTR_INITIATOR_BOOT_KEY_HASH , + dpp->own_boot_hash,32 ); + ptr +=dpp_append_attr (ptr,DPP_ATTR_INITIATOR_PROTOCOL_KEY , + i_proto_key, dpp->key_len *2 ); + ptr +=dpp_append_attr (ptr,DPP_ATTR_PROTOCOL_VERSION , &version,1 ); + +if (dpp->current_freq != dpp->new_freq) { +uint8_t pair[2 ] = {81 , +band_freq_to_channel (dpp->new_freq,NULL ) }; + + ptr +=dpp_append_attr (ptr,DPP_ATTR_CHANNEL , pair,2 ); + } + + ptr +=dpp_append_wrapped_data (hdr +26 ,6 , attrs, ptr - attrs, + ptr,sizeof (attrs), dpp->k1, dpp->key_len,2 , +DPP_ATTR_INITIATOR_NONCE , dpp->nonce_len, dpp->i_nonce, +DPP_ATTR_INITIATOR_CAPABILITIES ,1 , &dpp->role); + + iov[1 ].iov_base = attrs; + iov[1 ].iov_len = ptr - attrs; + +dpp_send_frame (dpp, iov,2 , dpp->current_freq); +} +static void dpp_roc_started (void *user_data) {struct dpp_sm *dpp = user_data;
@@ -1559,6 +1628,300 @@ auth_request_failed:dpp_free_auth_data (dpp); } +static void dpp_send_authenticate_confirm (struct dpp_sm *dpp) +{ +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), + dpp->auth_addr, +DPP_FRAME_AUTHENTICATION_CONFIRM , hdr); + iov[0 ].iov_base = hdr; + + ptr +=dpp_append_attr (ptr,DPP_ATTR_STATUS , &zero,1 ); + ptr +=dpp_append_attr (ptr,DPP_ATTR_RESPONDER_BOOT_KEY_HASH , + dpp->peer_boot_hash,32 ); + + ptr +=dpp_append_wrapped_data (hdr +26 ,6 , attrs, ptr - attrs, ptr, +sizeof (attrs), dpp->ke, dpp->key_len,1 , +DPP_ATTR_INITIATOR_AUTH_TAG , dpp->key_len, + dpp->auth_tag); + + iov[1 ].iov_base = attrs; + iov[1 ].iov_len = ptr - attrs; + +dpp_send_frame (dpp, iov,2 , dpp->current_freq); +} + +static void authenticate_response (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 void *r_boot_hash =NULL ; +const void *r_proto =NULL ; +size_t r_proto_len =0 ; +const void *wrapped =NULL ; +size_t wrapped_len; +uint8_t *unwrapped; +const void *r_nonce =NULL ; +const void *i_nonce =NULL ; +const void *r_auth =NULL ; +struct l_ecc_point *r_proto_key; +struct l_ecc_scalar *n; +const void *ad0 = body +2 ; +const void *ad1 = body +8 ; +uint64_t r_auth_derived[L_ECC_MAX_DIGITS ]; + +l_debug ("Authenticate response" ); + +if (dpp->state !=DPP_STATE_AUTHENTICATING ) +return ; + +if (dpp->role !=DPP_CAPABILITY_CONFIGURATOR ) +return ; + +if (!dpp->freqs) +return ; + +if (memcmp (from, dpp->auth_addr,6 )) +return ; + +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 : +if (len !=1 ) +return ; + + status =l_get_u8 (data); +break ; +case DPP_ATTR_RESPONDER_BOOT_KEY_HASH : + r_boot_hash = data; +break ; +case DPP_ATTR_RESPONDER_PROTOCOL_KEY : + r_proto = data; + r_proto_len = len; +break ; +case DPP_ATTR_PROTOCOL_VERSION : +if (len !=1 ||l_get_u8 (data) !=2 ) +return ; +break ; +case DPP_ATTR_WRAPPED_DATA : + wrapped = data; + wrapped_len = len; +break ; +default : +break ; + } + } + +if (status !=DPP_STATUS_OK || !r_boot_hash || !r_proto ) { +l_debug ("Auth response bad status or missing attributes" ); +return ; + } + + r_proto_key =l_ecc_point_from_data (dpp->curve,L_ECC_POINT_TYPE_FULL , + r_proto, r_proto_len); +if (!r_proto_key) { +l_debug ("Peers protocol key was invalid" ); +return ; + } + + n =dpp_derive_k2 (r_proto_key, dpp->proto_private, dpp->k2); + + unwrapped =dpp_unwrap_attr (ad0,6 , ad1, wrapped -4 - ad1, dpp->k2, + dpp->key_len, wrapped, wrapped_len, + &wrapped_len); +if (!unwrapped) { +l_debug ("Failed to unwrap primary data" ); +return ; + } + + wrapped =NULL ; + +dpp_attr_iter_init (&iter, unwrapped, wrapped_len); + +while (dpp_attr_iter_next (&iter, &type, &len, &data)) { +switch (type) { +case DPP_ATTR_RESPONDER_NONCE : +if (len != dpp->nonce_len) +return ; + + r_nonce = data; +break ; +case DPP_ATTR_INITIATOR_NONCE : +if (len != dpp->nonce_len) +return ; + + i_nonce = data; +break ; +case DPP_ATTR_RESPONDER_CAPABILITIES : +break ; +case DPP_ATTR_WRAPPED_DATA : + wrapped = data; + wrapped_len = len; +break ; +default : +break ; + } + } + +if (!r_nonce || !i_nonce || !wrapped) { +l_debug ("Wrapped data missing attributes" ); +return ; + } + +if (!dpp_derive_ke (i_nonce, r_nonce, dpp->m, n, dpp->ke)) { +l_debug ("Failed to derive ke" ); +return ; + } + + unwrapped =dpp_unwrap_attr (NULL ,0 ,NULL ,0 , dpp->ke, dpp->key_len, + wrapped, wrapped_len, &wrapped_len); +if (!unwrapped) { +l_debug ("Failed to unwrap secondary data" ); +return ; + } + +dpp_attr_iter_init (&iter, unwrapped, wrapped_len); + +while (dpp_attr_iter_next (&iter, &type, &len, &data)) { +switch (type) { +case DPP_ATTR_RESPONDER_AUTH_TAG : +if (len != dpp->key_len) +return ; + + r_auth = data; +break ; +default : +break ; + } + } + +if (!r_auth) { +l_debug ("R-Auth was not in secondary wrapped data" ); +return ; + } + +if (!dpp_derive_r_auth (i_nonce, r_nonce, dpp->nonce_len, + dpp->own_proto_public, r_proto_key, + dpp->peer_boot_public, r_auth_derived)) { +l_debug ("Failed to derive r_auth" ); +return ; + } + +if (memcmp (r_auth, r_auth_derived, dpp->key_len)) { +l_debug ("R-Auth did not verify" ); +return ; + } + +if (!dpp_derive_i_auth (r_nonce, i_nonce, dpp->nonce_len, + r_proto_key, dpp->own_proto_public, + dpp->peer_boot_public, dpp->auth_tag)) { +l_debug ("Could not derive I-Auth" ); +return ; + } + + dpp->current_freq = dpp->new_freq; + +dpp_send_authenticate_confirm (dpp); +} + +static void dpp_handle_presence_announcement (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 void *r_boot =NULL ; +size_t r_boot_len =0 ; +uint8_t hash[32 ]; + +l_debug ("Presence announcement " MAC ,MAC_STR (from)); + +/* Must be a configurator, in an initiator role, in PRESENCE state */ +if (dpp->state !=DPP_STATE_PRESENCE ) +return ; + +if (dpp->role !=DPP_CAPABILITY_CONFIGURATOR ) +return ; + +if (!dpp->freqs) +return ; + +/* +* The URI may not have contained a MAC address, if this announcement +* verifies set auth_addr then. +*/ +if (!l_memeqzero (dpp->auth_addr,6 ) && +memcmp (from, dpp->auth_addr,6 )) { +l_debug ("Unexpected source " MAC " expected " MAC ,MAC_STR (from), +MAC_STR (dpp->auth_addr)); +return ; + } + +dpp_attr_iter_init (&iter, body +8 , body_len -8 ); + +while (dpp_attr_iter_next (&iter, &type, &len, &data)) { +switch (type) { +case DPP_ATTR_RESPONDER_BOOT_KEY_HASH : + r_boot = data; + r_boot_len = len; +break ; +default : +break ; + } + } + +if (!r_boot || r_boot_len !=32 ) { +l_debug ("No responder boot hash" ); +return ; + } + +/* Hash what we have for the peer and check its our enrollee */ +dpp_hash (L_CHECKSUM_SHA256 , hash,2 ,"chirp" ,strlen ("chirp" ), + dpp->peer_asn1, dpp->peer_asn1_len); + +if (memcmp (hash, r_boot,sizeof (hash))) { +l_debug ("Peers boot hash did not match" ); +return ; + } + +/* +* This is the peer we expected, save away the address and derive the +* initial keys. +*/ +memcpy (dpp->auth_addr, from,6 ); + +l_ecdh_generate_key_pair (dpp->curve, &dpp->proto_private, + &dpp->own_proto_public); +l_getrandom (dpp->i_nonce, dpp->nonce_len); + + dpp->m =dpp_derive_k1 (dpp->peer_boot_public, dpp->proto_private, + dpp->k1); + + dpp->state =DPP_STATE_AUTHENTICATING ; + +dpp_send_authenticate_request (dpp); + +/* +* Should we wait for an ACK then go offchannel? +*/ +if (dpp->current_freq != dpp->new_freq) +dpp_start_offchannel (dpp, dpp->new_freq); +} +static void dpp_handle_frame (const struct mmpdu_header *frame,const void *body,size_t body_len,int rssi,void *user_data)
@@ -1580,6 +1943,9 @@ static void dpp_handle_frame(const struct mmpdu_header *frame,case DPP_FRAME_AUTHENTICATION_REQUEST :authenticate_request (dpp, frame->address_2, body, body_len);break ; +case DPP_FRAME_AUTHENTICATION_RESPONSE : +authenticate_response (dpp, frame->address_2, body, body_len); +break ;case DPP_FRAME_AUTHENTICATION_CONFIRM :authenticate_confirm (dpp, frame->address_2, body, body_len);break ;
@@ -1587,6 +1953,10 @@ static void dpp_handle_frame(const struct mmpdu_header *frame,dpp_handle_config_result_frame (dpp, frame->address_2, body, body_len);break ; +case DPP_FRAME_PRESENCE_ANNOUNCEMENT : +dpp_handle_presence_announcement (dpp, frame->address_2, + body, body_len); +break ;default :l_debug ("Unhandled DPP frame %u" , *ptr);break ;
@@ -1814,9 +2184,48 @@ static struct l_dbus_message *dpp_dbus_start_enrollee(struct l_dbus *dbus,return NULL ; } -static struct l_dbus_message *dpp_dbus_start_configurator (struct l_dbus *dbus, +/* +* Set up the configurator for an initiator role. The configurator +* will go offchannel to frequencies advertised by the enrollees URI or, +* if no channels are provided, use a default channel list. +*/ +static bool dpp_configurator_start_presence (struct dpp_sm *dpp,const char *uri) +{ + _auto_(l_free) uint32_t *freqs = NULL; +size_t freqs_len =0 ; + L_AUTO_FREE_VAR(struct dpp_uri_info *, info) = NULL; + + info =dpp_parse_uri (uri); +if (!info) +return false; + +if (!l_memeqzero (info->mac,6 )) +memcpy (dpp->auth_addr, info->mac,6 ); + + dpp->peer_boot_public =l_ecc_point_clone (info->boot_public); + dpp->peer_asn1 =dpp_point_to_asn1 (info->boot_public, + &dpp->peer_asn1_len); +if (!dpp->peer_asn1) { +l_debug ("Peer boot key did not convert to asn1" ); +return false; + } + +dpp_hash (L_CHECKSUM_SHA256 , dpp->peer_boot_hash,1 , dpp->peer_asn1, + dpp->peer_asn1_len); + +if (info->freqs) + freqs =scan_freq_set_to_fixed_array (info->freqs, &freqs_len); + +dpp_start_presence (dpp, freqs, freqs_len); + +return true; +} + +static struct l_dbus_message *dpp_start_configurator_common ( +struct l_dbus *dbus,struct l_dbus_message *message, -void *user_data) +void *user_data, +bool responder) {struct dpp_sm *dpp = user_data;struct l_dbus_message *reply;
@@ -1825,6 +2234,7 @@ static struct l_dbus_message *dpp_dbus_start_configurator(struct l_dbus *dbus,struct network *network;struct l_settings *settings;struct handshake_state *hs =netdev_get_handshake (dpp->netdev); +const char *uri;/* * For now limit the configurator to only configuring enrollees to the
@@ -1848,13 +2258,23 @@ static struct l_dbus_message *dpp_dbus_start_configurator(struct l_dbus *dbus,if (dpp->state !=DPP_STATE_NOTHING )return dbus_error_busy (message); +if (!responder) { +if (!l_dbus_message_get_arguments (message,"s" , &uri)) +return dbus_error_invalid_args (message); + +if (!dpp_configurator_start_presence (dpp, uri)) +return dbus_error_invalid_args (message); + + dpp->new_freq = bss->frequency; + }else + dpp->current_freq = bss->frequency; + dpp->uri =dpp_generate_uri (dpp->own_asn1, dpp->own_asn1_len,2 ,netdev_get_address (dpp->netdev), &bss->frequency,1 ,NULL ,NULL ); dpp->state =DPP_STATE_PRESENCE ; dpp->role =DPP_CAPABILITY_CONFIGURATOR ; - dpp->current_freq = bss->frequency; dpp->config =dpp_configuration_new (settings,network_get_ssid (network), hs->akm_suite);
@@ -1870,6 +2290,20 @@ static struct l_dbus_message *dpp_dbus_start_configurator(struct l_dbus *dbus,return reply; } +static struct l_dbus_message *dpp_dbus_start_configurator (struct l_dbus *dbus, +struct l_dbus_message *message, +void *user_data) +{ +return dpp_start_configurator_common (dbus, message, user_data, true); +} + +static struct l_dbus_message *dpp_dbus_configure_enrollee (struct l_dbus *dbus, +struct l_dbus_message *message, +void *user_data) +{ +return dpp_start_configurator_common (dbus, message, user_data, false); +} +static struct l_dbus_message *dpp_dbus_stop (struct l_dbus *dbus,struct l_dbus_message *message,void *user_data)
@@ -1887,6 +2321,8 @@ static void dpp_setup_interface(struct l_dbus_interface *interface) dpp_dbus_start_enrollee, "s", "", "uri");l_dbus_interface_method (interface,"StartConfigurator" ,0 , dpp_dbus_start_configurator,"s" ,"" ,"uri" ); +l_dbus_interface_method (interface,"ConfigureEnrollee" ,0 , + dpp_dbus_configure_enrollee,"" ,"s" ,"uri" );l_dbus_interface_method (interface,"Stop" ,0 , dpp_dbus_stop,"" ,"" ); }
Signature · 1 line
2.31.1
Denis Kenzior
Re: [PATCH 07/12] dpp: add support for initiating as a configurator
Quoted 7 lines · James Prestwood
The difference between the existing code is that IWD will send the authentication request, making it the initiator.
This handles the use case where IWD is provided a peers URI containing its bootstrapping key rather than IWD always providing its own URI. A new DBus API was added,
ConfigureEnrollee().
Is a patch for doc/device-provisioning-api.txt missing?
Regards,
-Denis
James Prestwood
PATCH 8/12 auto-t: iwd.py: allow passing uri to start_configurator
In reply to James PrestwoodIf a URI is passed this will envoke ConfigureEnrollee()
--- autotests/util/iwd.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
autotests/util/iwd.py +7 -4
@@ -280,8 +280,11 @@ class DeviceProvisioning(IWDDBusAbstract): def start_enrollee(self): return self._iface.StartEnrollee() - def start_configurator(self): - return self._iface.StartConfigurator() + def start_configurator(self, uri=None): + if uri: + return self._iface.ConfigureEnrollee(uri) + else: + return self._iface.StartConfigurator() def stop(self): self._iface.Stop()
@@ -667,8 +670,8 @@ class Device(IWDDBusAbstract): def dpp_start_enrollee(self): return self._device_provisioning.start_enrollee() - def dpp_start_configurator(self): - return self._device_provisioning.start_configurator() + def dpp_start_configurator(self, uri=None): + return self._device_provisioning.start_configurator(uri) def dpp_stop(self): return self._device_provisioning.stop()
Signature · 1 line
2.31.1
James Prestwood
PATCH 9/12 auto-t: wpas.py: allow arbitrary events to be handled
In reply to James PrestwoodIf an event is in response to some command which is returning an unexpected value (unexpected with respect to wpas.py) handle_eow would raise an exception.
Specifically with DPP this was being hit when the URI was being returned.
--- autotests/util/wpas.py | 2 -- 1 file changed, 2 deletions(-)
autotests/util/wpas.py +0 -2
@@ -88,8 +88,6 @@ class Wpas: def handle_eow(): nonlocal key, value, count, event if count == 0: - if key is not None or not value: - raise Exception('Bad event name') key = 'event' elif key is None: if not value:
Signature · 1 line
2.31.1
James Prestwood
PATCH 10/12 auto-t: wpas.py: handle enrollee as responder
In reply to James PrestwoodThis adds support for wpa_supplicant to generate its own URI and start presence announcements (DPP_CHIRP).
--- autotests/util/wpas.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
autotests/util/wpas.py +14 -3
@@ -239,10 +239,17 @@ class Wpas: def set(self, key, value, **kwargs): self._ctrl_request('SET ' + key + ' ' + value, **kwargs) - def dpp_enrollee_start(self, uri=None): + def dpp_enrollee_start(self, uri=None, oper_and_channel=None): + if not oper_and_channel: + oper_and_channel = '81/1' + self._rx_data = [] - self._ctrl_request('DPP_BOOTSTRAP_GEN type=qrcode') - self.wait_for_result() + self._ctrl_request('DPP_BOOTSTRAP_GEN type=qrcode chan=%s' % oper_and_channel) + self._dpp_qr_id = self.wait_for_result() + self._ctrl_request('DPP_BOOTSTRAP_GET_URI %s' % self._dpp_qr_id) + self._dpp_uri = self.wait_for_result() + + print("DPP Enrollee QR: %s" % self._dpp_uri) if uri: self._rx_data = []
@@ -250,6 +257,10 @@ class Wpas: self._dpp_qr_id = self.wait_for_result() self._rx_data = [] self._ctrl_request('DPP_AUTH_INIT peer=%s role=enrollee' % self._dpp_qr_id) + else: + self._ctrl_request('DPP_CHIRP own=%s iter=100' % self._dpp_qr_id) + + return self._dpp_uri def dpp_configurator_create(self, uri): self._rx_data = []
Signature · 1 line
2.31.1
James Prestwood
PATCH 11/12 auto-t: add test for IWD as configurator + initiator
In reply to James Prestwood--- autotests/testDPP/connection_test.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
autotests/testDPP/connection_test.py +16 -0
@@ -82,6 +82,22 @@ class Test(unittest.TestCase): self.hapd.wait_for_event('AP-STA-CONNECTED 42:00:00:00:00:00') + def test_iwd_as_configurator_initiator(self): + self.hapd.reload() + self.hapd.wait_for_event('AP-ENABLED') + + IWD.copy_to_storage('ssidCCMP.psk') + self.device.autoconnect = True + + condition = 'obj.state == DeviceState.connected' + self.wd.wait_for_object_condition(self.device, condition) + + uri = self.wpas.dpp_enrollee_start(oper_and_channel='81/2') + + self.device.dpp_start_configurator(uri) + + self.hapd.wait_for_event('AP-STA-CONNECTED 42:00:00:00:00:00') + def setUp(self): self.wd = IWD(True) self.device = self.wd.list_devices(1)[0]
Signature · 1 line
2.31.1
James Prestwood
PATCH 12/12 dpp: fix dpp_offchannel_start corner cases
In reply to James PrestwoodThe purpose of this was to have a single utility to both cancel an existing offchannel operation (if one exists) and start a new one.
The problem was the previous offchannel operation was being canceled first which opened up the radio work queue to other items. This is not desireable as, for example, a scan would end up breaking the DPP protocol most likely.
Starting the new offchannel then canceling is the correct order of operations but to do this required saving the new ID, canceling, then setting offchannel_id to the new ID so dpp_presence_timeout wouldn't overwrite the new ID to zero.
This also removes an explicit call to offchannel_cancel which is already done by dpp_offchannel_start.
--- src/dpp.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
src/dpp.c +19 -4
@@ -1422,12 +1422,28 @@ protocol_failed:static void dpp_start_offchannel (struct dpp_sm *dpp,uint32_t freq) { +/* +* This needs to be handled carefully for a few reasons: +* +* First, the next offchannel operation needs to be started prior to +* canceling an existing one. This is so the offchannel work can +* continue uninterrupted without any other work items starting in +* between canceling and starting the next (e.g. if a scan request is +* sitting in the queue). +* +* Second, dpp_presence_timeout resets dpp->offchannel_id to zero which +* is why the new ID is saved and only set to dpp->offchannel_id once +* the previous offchannel work is cancelled (i.e. destroy() has been +* called). +*/ +uint32_t id =offchannel_start (netdev_get_wdev_id (dpp->netdev), + freq, dpp->dwell, dpp_roc_started, + dpp, dpp_presence_timeout); +if (dpp->offchannel_id)offchannel_cancel (dpp->wdev_id, dpp->offchannel_id); - dpp->offchannel_id =offchannel_start (netdev_get_wdev_id (dpp->netdev), - freq, dpp->dwell, dpp_roc_started, - dpp, dpp_presence_timeout); + dpp->offchannel_id = id; } static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
@@ -1520,7 +1536,6 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,l_debug ("Configurator requested a new frequency %u" , dpp->new_freq); -offchannel_cancel (dpp->wdev_id, dpp->offchannel_id);dpp_start_offchannel (dpp, dpp->new_freq);break ;
Signature · 1 line
2.31.1
Denis Kenzior
AppliedRe: [PATCH 12/12] dpp: fix dpp_offchannel_start corner cases
Hi James,
Quoted 13 lines · James Prestwood
The purpose of this was to have a single utility to both cancel an existing offchannel operation (if one exists) and start a new one.
The problem was the previous offchannel operation was being canceled first which opened up the radio work queue to other items. This is not desireable as, for example, a scan would end up breaking the DPP protocol most likely.
Starting the new offchannel then canceling is the correct order of operations but to do this required saving the new ID, canceling, then setting
offchannel_idto the new ID sodpp_presence_timeoutwouldn't overwrite the new ID to zero.
Quoted 6 lines
This also removes an explicit call to
offchannel_cancelwhich is already done bydpp_offchannel_start.--- src/dpp.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
Applied, thanks.
Regards,
-Denis
Denis Kenzior
AppliedHi James,
Quoted 8 lines · James Prestwood
ASN1 parsing will soon be required which will need some utilities in
asn1-private.h. To avoid duplication include this private header and replace the OID's with the defined structures as well as remove the duplicated macros.--- src/dpp-util.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-)
Applied, thanks.
Regards,
-Denis