dpp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * DPP functionality shared between hostapd and wpa_supplicant
  3. * Copyright (c) 2017, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #ifndef DPP_H
  9. #define DPP_H
  10. #include <openssl/x509.h>
  11. #include "utils/list.h"
  12. #include "common/wpa_common.h"
  13. #include "crypto/sha256.h"
  14. #define DPP_HDR_LEN (4 + 2) /* OUI, OUI Type, Crypto Suite, DPP frame type */
  15. enum dpp_public_action_frame_type {
  16. DPP_PA_AUTHENTICATION_REQ = 0,
  17. DPP_PA_AUTHENTICATION_RESP = 1,
  18. DPP_PA_AUTHENTICATION_CONF = 2,
  19. DPP_PA_PEER_DISCOVERY_REQ = 5,
  20. DPP_PA_PEER_DISCOVERY_RESP = 6,
  21. DPP_PA_PKEX_EXCHANGE_REQ = 7,
  22. DPP_PA_PKEX_EXCHANGE_RESP = 8,
  23. DPP_PA_PKEX_COMMIT_REVEAL_REQ = 9,
  24. DPP_PA_PKEX_COMMIT_REVEAL_RESP = 10,
  25. };
  26. enum dpp_attribute_id {
  27. DPP_ATTR_STATUS = 0x1000,
  28. DPP_ATTR_I_BOOTSTRAP_KEY_HASH = 0x1001,
  29. DPP_ATTR_R_BOOTSTRAP_KEY_HASH = 0x1002,
  30. DPP_ATTR_I_PROTOCOL_KEY = 0x1003,
  31. DPP_ATTR_WRAPPED_DATA = 0x1004,
  32. DPP_ATTR_I_NONCE = 0x1005,
  33. DPP_ATTR_I_CAPABILITIES = 0x1006,
  34. DPP_ATTR_R_NONCE = 0x1007,
  35. DPP_ATTR_R_CAPABILITIES = 0x1008,
  36. DPP_ATTR_R_PROTOCOL_KEY = 0x1009,
  37. DPP_ATTR_I_AUTH_TAG = 0x100A,
  38. DPP_ATTR_R_AUTH_TAG = 0x100B,
  39. DPP_ATTR_CONFIG_OBJ = 0x100C,
  40. DPP_ATTR_CONNECTOR = 0x100D,
  41. DPP_ATTR_CONFIG_ATTR_OBJ = 0x100E,
  42. DPP_ATTR_BOOTSTRAP_KEY = 0x100F,
  43. DPP_ATTR_OWN_NET_NK_HASH = 0x1011,
  44. DPP_ATTR_FINITE_CYCLIC_GROUP = 0x1012,
  45. DPP_ATTR_ENCRYPTED_KEY = 0x1013,
  46. DPP_ATTR_ENROLLEE_NONCE = 0x1014,
  47. DPP_ATTR_CODE_IDENTIFIER = 0x1015,
  48. DPP_ATTR_TRANSACTION_ID = 0x1016,
  49. DPP_ATTR_BOOTSTRAP_INFO = 0x1017,
  50. DPP_ATTR_CHANNEL = 0x1018,
  51. DPP_ATTR_TESTING = 0x10ff, /* not defined in the DPP tech spec */
  52. };
  53. enum dpp_status_error {
  54. DPP_STATUS_OK = 0,
  55. DPP_STATUS_NOT_COMPATIBLE = 1,
  56. DPP_STATUS_AUTH_FAILURE = 2,
  57. DPP_STATUS_UNWRAP_FAILURE = 3,
  58. DPP_STATUS_BAD_GROUP = 4,
  59. DPP_STATUS_CONFIGURE_FAILURE = 5,
  60. DPP_STATUS_RESPONSE_PENDING = 6,
  61. DPP_STATUS_INVALID_CONNECTOR = 7,
  62. DPP_STATUS_NO_MATCH = 8,
  63. };
  64. #define DPP_CAPAB_ENROLLEE BIT(0)
  65. #define DPP_CAPAB_CONFIGURATOR BIT(1)
  66. #define DPP_CAPAB_ROLE_MASK (BIT(0) | BIT(1))
  67. #define DPP_BOOTSTRAP_MAX_FREQ 30
  68. #define DPP_MAX_NONCE_LEN 32
  69. #define DPP_MAX_HASH_LEN 64
  70. #define DPP_MAX_SHARED_SECRET_LEN 66
  71. struct dpp_curve_params {
  72. const char *name;
  73. size_t hash_len;
  74. size_t aes_siv_key_len;
  75. size_t nonce_len;
  76. size_t prime_len;
  77. const char *jwk_crv;
  78. u16 ike_group;
  79. const char *jws_alg;
  80. };
  81. enum dpp_bootstrap_type {
  82. DPP_BOOTSTRAP_QR_CODE,
  83. DPP_BOOTSTRAP_PKEX,
  84. };
  85. struct dpp_bootstrap_info {
  86. struct dl_list list;
  87. unsigned int id;
  88. enum dpp_bootstrap_type type;
  89. char *uri;
  90. u8 mac_addr[ETH_ALEN];
  91. char *info;
  92. unsigned int freq[DPP_BOOTSTRAP_MAX_FREQ];
  93. unsigned int num_freq;
  94. int own;
  95. EVP_PKEY *pubkey;
  96. u8 pubkey_hash[SHA256_MAC_LEN];
  97. const struct dpp_curve_params *curve;
  98. };
  99. struct dpp_pkex {
  100. void *msg_ctx;
  101. unsigned int initiator:1;
  102. unsigned int exchange_done:1;
  103. struct dpp_bootstrap_info *own_bi;
  104. u8 own_mac[ETH_ALEN];
  105. u8 peer_mac[ETH_ALEN];
  106. char *identifier;
  107. char *code;
  108. EVP_PKEY *x;
  109. EVP_PKEY *y;
  110. u8 Mx[DPP_MAX_SHARED_SECRET_LEN];
  111. u8 Nx[DPP_MAX_SHARED_SECRET_LEN];
  112. u8 z[DPP_MAX_HASH_LEN];
  113. EVP_PKEY *peer_bootstrap_key;
  114. struct wpabuf *exchange_req;
  115. struct wpabuf *exchange_resp;
  116. };
  117. struct dpp_configuration {
  118. u8 ssid[32];
  119. size_t ssid_len;
  120. int dpp; /* whether to use DPP or legacy configuration */
  121. /* For DPP configuration (connector) */
  122. os_time_t netaccesskey_expiry;
  123. /* TODO: groups */
  124. /* For legacy configuration */
  125. char *passphrase;
  126. u8 psk[32];
  127. };
  128. struct dpp_authentication {
  129. void *msg_ctx;
  130. const struct dpp_curve_params *curve;
  131. struct dpp_bootstrap_info *peer_bi;
  132. struct dpp_bootstrap_info *own_bi;
  133. u8 waiting_pubkey_hash[SHA256_MAC_LEN];
  134. int response_pending;
  135. enum dpp_status_error auth_resp_status;
  136. u8 peer_mac_addr[ETH_ALEN];
  137. u8 i_nonce[DPP_MAX_NONCE_LEN];
  138. u8 r_nonce[DPP_MAX_NONCE_LEN];
  139. u8 e_nonce[DPP_MAX_NONCE_LEN];
  140. u8 i_capab;
  141. u8 r_capab;
  142. EVP_PKEY *own_protocol_key;
  143. EVP_PKEY *peer_protocol_key;
  144. struct wpabuf *req_msg;
  145. struct wpabuf *resp_msg;
  146. unsigned int curr_freq;
  147. unsigned int neg_freq;
  148. size_t secret_len;
  149. u8 Mx[DPP_MAX_SHARED_SECRET_LEN];
  150. u8 Nx[DPP_MAX_SHARED_SECRET_LEN];
  151. u8 Lx[DPP_MAX_SHARED_SECRET_LEN];
  152. u8 k1[DPP_MAX_HASH_LEN];
  153. u8 k2[DPP_MAX_HASH_LEN];
  154. u8 ke[DPP_MAX_HASH_LEN];
  155. int initiator;
  156. int configurator;
  157. int remove_on_tx_status;
  158. int auth_success;
  159. struct wpabuf *conf_req;
  160. struct dpp_configuration *conf_ap;
  161. struct dpp_configuration *conf_sta;
  162. struct dpp_configurator *conf;
  163. char *connector; /* received signedConnector */
  164. u8 ssid[SSID_MAX_LEN];
  165. u8 ssid_len;
  166. char passphrase[64];
  167. u8 psk[PMK_LEN];
  168. int psk_set;
  169. struct wpabuf *net_access_key;
  170. os_time_t net_access_key_expiry;
  171. struct wpabuf *c_sign_key;
  172. #ifdef CONFIG_TESTING_OPTIONS
  173. char *config_obj_override;
  174. char *discovery_override;
  175. char *groups_override;
  176. unsigned int ignore_netaccesskey_mismatch:1;
  177. #endif /* CONFIG_TESTING_OPTIONS */
  178. };
  179. struct dpp_configurator {
  180. struct dl_list list;
  181. unsigned int id;
  182. int own;
  183. EVP_PKEY *csign;
  184. char *kid;
  185. const struct dpp_curve_params *curve;
  186. };
  187. struct dpp_introduction {
  188. u8 pmkid[PMKID_LEN];
  189. u8 pmk[PMK_LEN_MAX];
  190. size_t pmk_len;
  191. };
  192. #ifdef CONFIG_TESTING_OPTIONS
  193. enum dpp_test_behavior {
  194. DPP_TEST_DISABLED = 0,
  195. DPP_TEST_AFTER_WRAPPED_DATA_AUTH_REQ = 1,
  196. DPP_TEST_AFTER_WRAPPED_DATA_AUTH_RESP = 2,
  197. DPP_TEST_AFTER_WRAPPED_DATA_AUTH_CONF = 3,
  198. DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_REQ = 4,
  199. DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_RESP = 5,
  200. DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ = 6,
  201. DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP = 7,
  202. DPP_TEST_ZERO_I_CAPAB = 8,
  203. DPP_TEST_ZERO_R_CAPAB = 9,
  204. DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_REQ = 10,
  205. DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_REQ = 11,
  206. DPP_TEST_NO_I_PROTO_KEY_AUTH_REQ = 12,
  207. DPP_TEST_NO_I_NONCE_AUTH_REQ = 13,
  208. DPP_TEST_NO_I_CAPAB_AUTH_REQ = 14,
  209. DPP_TEST_NO_WRAPPED_DATA_AUTH_REQ = 15,
  210. DPP_TEST_NO_STATUS_AUTH_RESP = 16,
  211. DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_RESP = 17,
  212. DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_RESP = 18,
  213. DPP_TEST_NO_R_PROTO_KEY_AUTH_RESP = 19,
  214. DPP_TEST_NO_R_NONCE_AUTH_RESP = 20,
  215. DPP_TEST_NO_I_NONCE_AUTH_RESP = 21,
  216. DPP_TEST_NO_R_CAPAB_AUTH_RESP = 22,
  217. DPP_TEST_NO_R_AUTH_AUTH_RESP = 23,
  218. DPP_TEST_NO_WRAPPED_DATA_AUTH_RESP = 24,
  219. DPP_TEST_NO_STATUS_AUTH_CONF = 25,
  220. DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_CONF = 26,
  221. DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_CONF = 27,
  222. DPP_TEST_NO_I_AUTH_AUTH_CONF = 28,
  223. DPP_TEST_NO_WRAPPED_DATA_AUTH_CONF = 29,
  224. DPP_TEST_I_NONCE_MISMATCH_AUTH_RESP = 30,
  225. DPP_TEST_INCOMPATIBLE_R_CAPAB_AUTH_RESP = 31,
  226. DPP_TEST_R_AUTH_MISMATCH_AUTH_RESP = 32,
  227. DPP_TEST_I_AUTH_MISMATCH_AUTH_CONF = 33,
  228. DPP_TEST_NO_FINITE_CYCLIC_GROUP_PKEX_EXCHANGE_REQ = 34,
  229. DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_REQ = 35,
  230. DPP_TEST_NO_STATUS_PKEX_EXCHANGE_RESP = 36,
  231. DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_RESP = 37,
  232. DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_REQ = 38,
  233. DPP_TEST_NO_I_AUTH_TAG_PKEX_CR_REQ = 39,
  234. DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_REQ = 40,
  235. DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_RESP = 41,
  236. DPP_TEST_NO_R_AUTH_TAG_PKEX_CR_RESP = 42,
  237. DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_RESP = 43,
  238. };
  239. extern enum dpp_test_behavior dpp_test;
  240. #endif /* CONFIG_TESTING_OPTIONS */
  241. void dpp_bootstrap_info_free(struct dpp_bootstrap_info *info);
  242. const char * dpp_bootstrap_type_txt(enum dpp_bootstrap_type type);
  243. int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi);
  244. int dpp_parse_uri_chan_list(struct dpp_bootstrap_info *bi,
  245. const char *chan_list);
  246. int dpp_parse_uri_mac(struct dpp_bootstrap_info *bi, const char *mac);
  247. int dpp_parse_uri_info(struct dpp_bootstrap_info *bi, const char *info);
  248. struct dpp_bootstrap_info * dpp_parse_qr_code(const char *uri);
  249. char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
  250. const u8 *privkey, size_t privkey_len);
  251. struct dpp_authentication * dpp_auth_init(void *msg_ctx,
  252. struct dpp_bootstrap_info *peer_bi,
  253. struct dpp_bootstrap_info *own_bi,
  254. int configurator,
  255. unsigned int neg_freq);
  256. struct dpp_authentication *
  257. dpp_auth_req_rx(void *msg_ctx, u8 dpp_allowed_roles, int qr_mutual,
  258. struct dpp_bootstrap_info *peer_bi,
  259. struct dpp_bootstrap_info *own_bi,
  260. unsigned int freq, const u8 *hdr, const u8 *attr_start,
  261. size_t attr_len);
  262. struct wpabuf *
  263. dpp_auth_resp_rx(struct dpp_authentication *auth, const u8 *hdr,
  264. const u8 *attr_start, size_t attr_len);
  265. struct wpabuf * dpp_build_conf_req(struct dpp_authentication *auth,
  266. const char *json);
  267. int dpp_auth_conf_rx(struct dpp_authentication *auth, const u8 *hdr,
  268. const u8 *attr_start, size_t attr_len);
  269. int dpp_notify_new_qr_code(struct dpp_authentication *auth,
  270. struct dpp_bootstrap_info *peer_bi);
  271. void dpp_configuration_free(struct dpp_configuration *conf);
  272. void dpp_auth_deinit(struct dpp_authentication *auth);
  273. struct wpabuf *
  274. dpp_conf_req_rx(struct dpp_authentication *auth, const u8 *attr_start,
  275. size_t attr_len);
  276. int dpp_conf_resp_rx(struct dpp_authentication *auth,
  277. const struct wpabuf *resp);
  278. struct wpabuf * dpp_alloc_msg(enum dpp_public_action_frame_type type,
  279. size_t len);
  280. const u8 * dpp_get_attr(const u8 *buf, size_t len, u16 req_id, u16 *ret_len);
  281. int dpp_check_attrs(const u8 *buf, size_t len);
  282. int dpp_key_expired(const char *timestamp, os_time_t *expiry);
  283. void dpp_configurator_free(struct dpp_configurator *conf);
  284. struct dpp_configurator *
  285. dpp_keygen_configurator(const char *curve, const u8 *privkey,
  286. size_t privkey_len);
  287. int dpp_configurator_own_config(struct dpp_authentication *auth,
  288. const char *curve);
  289. enum dpp_status_error
  290. dpp_peer_intro(struct dpp_introduction *intro, const char *own_connector,
  291. const u8 *net_access_key, size_t net_access_key_len,
  292. const u8 *csign_key, size_t csign_key_len,
  293. const u8 *peer_connector, size_t peer_connector_len,
  294. os_time_t *expiry);
  295. struct dpp_pkex * dpp_pkex_init(void *msg_ctx, struct dpp_bootstrap_info *bi,
  296. const u8 *own_mac,
  297. const char *identifier,
  298. const char *code);
  299. struct dpp_pkex * dpp_pkex_rx_exchange_req(void *msg_ctx,
  300. struct dpp_bootstrap_info *bi,
  301. const u8 *own_mac,
  302. const u8 *peer_mac,
  303. const char *identifier,
  304. const char *code,
  305. const u8 *buf, size_t len);
  306. struct wpabuf * dpp_pkex_rx_exchange_resp(struct dpp_pkex *pkex,
  307. const u8 *buf, size_t len);
  308. struct wpabuf * dpp_pkex_rx_commit_reveal_req(struct dpp_pkex *pkex,
  309. const u8 *hdr,
  310. const u8 *buf, size_t len);
  311. int dpp_pkex_rx_commit_reveal_resp(struct dpp_pkex *pkex, const u8 *hdr,
  312. const u8 *buf, size_t len);
  313. void dpp_pkex_free(struct dpp_pkex *pkex);
  314. #endif /* DPP_H */