]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - src/common/dpp.c
DPP2: Chirping in wpa_supplicant Enrollee
[thirdparty/hostap.git] / src / common / dpp.c
index 12a5a983be2bc2b207c87f9f2bdac651a006093e..56abe507f2377e0ca07473440377ee0c62270ae4 100644 (file)
@@ -131,6 +131,7 @@ struct dpp_global {
        struct dl_list tcp_init; /* struct dpp_connection */
        void *cb_ctx;
        int (*process_conf_obj)(void *ctx, struct dpp_authentication *auth);
+       void (*remove_bi)(void *ctx, struct dpp_bootstrap_info *bi);
 #endif /* CONFIG_DPP2 */
 };
 
@@ -894,6 +895,7 @@ void dpp_bootstrap_info_free(struct dpp_bootstrap_info *info)
        os_free(info->chan);
        os_free(info->pk);
        EVP_PKEY_free(info->pubkey);
+       str_clear_free(info->configurator_params);
        os_free(info);
 }
 
@@ -1055,6 +1057,32 @@ static const struct dpp_curve_params * dpp_get_curve_nid(int nid)
 }
 
 
+static int dpp_bi_pubkey_hash(struct dpp_bootstrap_info *bi,
+                             const u8 *data, size_t data_len)
+{
+       const u8 *addr[2];
+       size_t len[2];
+
+       addr[0] = data;
+       len[0] = data_len;
+       if (sha256_vector(1, addr, len, bi->pubkey_hash) < 0)
+               return -1;
+       wpa_hexdump(MSG_DEBUG, "DPP: Public key hash",
+                   bi->pubkey_hash, SHA256_MAC_LEN);
+
+       addr[0] = (const u8 *) "chirp";
+       len[0] = 5;
+       addr[1] = data;
+       len[1] = data_len;
+       if (sha256_vector(2, addr, len, bi->pubkey_hash_chirp) < 0)
+               return -1;
+       wpa_hexdump(MSG_DEBUG, "DPP: Public key hash (chirp)",
+                   bi->pubkey_hash_chirp, SHA256_MAC_LEN);
+
+       return 0;
+}
+
+
 static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
 {
        const char *end;
@@ -1093,14 +1121,11 @@ static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
        wpa_hexdump(MSG_DEBUG, "DPP: Base64 decoded URI public-key",
                    data, data_len);
 
-       if (sha256_vector(1, (const u8 **) &data, &data_len,
-                         bi->pubkey_hash) < 0) {
+       if (dpp_bi_pubkey_hash(bi, data, data_len) < 0) {
                wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
                os_free(data);
                return -1;
        }
-       wpa_hexdump(MSG_DEBUG, "DPP: Public key hash",
-                   bi->pubkey_hash, SHA256_MAC_LEN);
 
        /* DER encoded ASN.1 SubjectPublicKeyInfo
         *
@@ -1518,27 +1543,19 @@ fail:
 }
 
 
-int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi)
+static int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi)
 {
        struct wpabuf *der;
        int res;
-       const u8 *addr[1];
-       size_t len[1];
 
        der = dpp_bootstrap_key_der(bi->pubkey);
        if (!der)
                return -1;
        wpa_hexdump_buf(MSG_DEBUG, "DPP: Compressed public key (DER)",
                        der);
-
-       addr[0] = wpabuf_head(der);
-       len[0] = wpabuf_len(der);
-       res = sha256_vector(1, addr, len, bi->pubkey_hash);
+       res = dpp_bi_pubkey_hash(bi, wpabuf_head(der), wpabuf_len(der));
        if (res < 0)
                wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
-       else
-               wpa_hexdump(MSG_DEBUG, "DPP: Public key hash", bi->pubkey_hash,
-                           SHA256_MAC_LEN);
        wpabuf_free(der);
        return res;
 }
@@ -1551,8 +1568,6 @@ static int dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
        char *pos, *end;
        size_t len;
        struct wpabuf *der = NULL;
-       const u8 *addr[1];
-       int res;
 
        if (!curve) {
                bi->curve = &dpp_curves[0];
@@ -1578,15 +1593,10 @@ static int dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
        wpa_hexdump_buf(MSG_DEBUG, "DPP: Compressed public key (DER)",
                        der);
 
-       addr[0] = wpabuf_head(der);
-       len = wpabuf_len(der);
-       res = sha256_vector(1, addr, &len, bi->pubkey_hash);
-       if (res < 0) {
+       if (dpp_bi_pubkey_hash(bi, wpabuf_head(der), wpabuf_len(der)) < 0) {
                wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
                goto fail;
        }
-       wpa_hexdump(MSG_DEBUG, "DPP: Public key hash", bi->pubkey_hash,
-                   SHA256_MAC_LEN);
 
        base64 = base64_encode(wpabuf_head(der), wpabuf_len(der), &len);
        wpabuf_free(der);
@@ -2335,20 +2345,22 @@ fail:
 }
 
 
-static struct dpp_authentication * dpp_alloc_auth(void *msg_ctx)
+struct dpp_authentication *
+dpp_alloc_auth(struct dpp_global *dpp, void *msg_ctx)
 {
        struct dpp_authentication *auth;
 
        auth = os_zalloc(sizeof(*auth));
        if (!auth)
                return NULL;
+       auth->global = dpp;
        auth->msg_ctx = msg_ctx;
        auth->conf_resp_status = 255;
        return auth;
 }
 
 
-struct dpp_authentication * dpp_auth_init(void *msg_ctx,
+struct dpp_authentication * dpp_auth_init(struct dpp_global *dpp, void *msg_ctx,
                                          struct dpp_bootstrap_info *peer_bi,
                                          struct dpp_bootstrap_info *own_bi,
                                          u8 dpp_allowed_roles,
@@ -2365,9 +2377,12 @@ struct dpp_authentication * dpp_auth_init(void *msg_ctx,
        u8 test_hash[SHA256_MAC_LEN];
 #endif /* CONFIG_TESTING_OPTIONS */
 
-       auth = dpp_alloc_auth(msg_ctx);
+       auth = dpp_alloc_auth(dpp, msg_ctx);
        if (!auth)
                return NULL;
+       if (peer_bi->configurator_params &&
+           dpp_set_configurator(auth, peer_bi->configurator_params) < 0)
+               goto fail;
        auth->initiator = 1;
        auth->waiting_auth_resp = 1;
        auth->allowed_roles = dpp_allowed_roles;
@@ -3259,8 +3274,8 @@ static int dpp_auth_build_resp_status(struct dpp_authentication *auth,
 
 
 struct dpp_authentication *
-dpp_auth_req_rx(void *msg_ctx, u8 dpp_allowed_roles, int qr_mutual,
-               struct dpp_bootstrap_info *peer_bi,
+dpp_auth_req_rx(struct dpp_global *dpp, void *msg_ctx, u8 dpp_allowed_roles,
+               int qr_mutual, struct dpp_bootstrap_info *peer_bi,
                struct dpp_bootstrap_info *own_bi,
                unsigned int freq, const u8 *hdr, const u8 *attr_start,
                size_t attr_len)
@@ -3301,9 +3316,12 @@ dpp_auth_req_rx(void *msg_ctx, u8 dpp_allowed_roles, int qr_mutual,
                    wrapped_data, wrapped_data_len);
        attr_len = wrapped_data - 4 - attr_start;
 
-       auth = dpp_alloc_auth(msg_ctx);
+       auth = dpp_alloc_auth(dpp, msg_ctx);
        if (!auth)
                goto fail;
+       if (peer_bi && peer_bi->configurator_params &&
+           dpp_set_configurator(auth, peer_bi->configurator_params) < 0)
+               goto fail;
        auth->peer_bi = peer_bi;
        auth->own_bi = own_bi;
        auth->curve = own_bi->curve;
@@ -4517,6 +4535,10 @@ static int dpp_configuration_parse_helper(struct dpp_authentication *auth,
                conf = conf_ap;
        }
 
+       pos = os_strstr(cmd, " conf=configurator");
+       if (pos)
+               auth->provision_configurator = 1;
+
        if (!conf)
                return 0;
 
@@ -4671,25 +4693,38 @@ dpp_configurator_get_id(struct dpp_global *dpp, unsigned int id)
 }
 
 
-int dpp_set_configurator(struct dpp_global *dpp, void *msg_ctx,
-                        struct dpp_authentication *auth,
-                        const char *cmd)
+int dpp_set_configurator(struct dpp_authentication *auth, const char *cmd)
 {
        const char *pos;
+       char *tmp = NULL;
+       int ret = -1;
 
-       if (!cmd)
+       if (!cmd || auth->configurator_set)
                return 0;
+       auth->configurator_set = 1;
+
+       if (cmd[0] != ' ') {
+               size_t len;
+
+               len = os_strlen(cmd);
+               tmp = os_malloc(len + 2);
+               if (!tmp)
+                       goto fail;
+               tmp[0] = ' ';
+               os_memcpy(tmp + 1, cmd, len + 1);
+               cmd = tmp;
+       }
 
        wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
 
        pos = os_strstr(cmd, " configurator=");
        if (pos) {
                pos += 14;
-               auth->conf = dpp_configurator_get_id(dpp, atoi(pos));
+               auth->conf = dpp_configurator_get_id(auth->global, atoi(pos));
                if (!auth->conf) {
                        wpa_printf(MSG_INFO,
                                   "DPP: Could not find the specified configurator");
-                       return -1;
+                       goto fail;
                }
        }
 
@@ -4706,11 +4741,14 @@ int dpp_set_configurator(struct dpp_global *dpp, void *msg_ctx,
        }
 
        if (dpp_configuration_parse(auth, cmd) < 0) {
-               wpa_msg(msg_ctx, MSG_INFO,
+               wpa_msg(auth->msg_ctx, MSG_INFO,
                        "DPP: Failed to set configurator parameters");
-               return -1;
+               goto fail;
        }
-       return 0;
+       ret = 0;
+fail:
+       os_free(tmp);
+       return ret;
 }
 
 
@@ -5210,7 +5248,7 @@ static struct wpabuf * dpp_build_conf_params(void)
 
        asn1_put_utf8string(buf, conf_template);
        if (connector_template)
-               asn1_put_utf8string(buf, conf_template);
+               asn1_put_utf8string(buf, connector_template);
        return asn1_encaps(buf, ASN1_CLASS_UNIVERSAL, ASN1_TAG_SEQUENCE);
 }
 
@@ -5355,7 +5393,6 @@ fail:
        return asn1_encaps(asn1_encaps(key,
                                       ASN1_CLASS_UNIVERSAL, ASN1_TAG_SEQUENCE),
                           ASN1_CLASS_UNIVERSAL, ASN1_TAG_SEQUENCE);
-       return key;
 }
 
 
@@ -5566,7 +5603,7 @@ dpp_build_enc_cont_info(struct dpp_authentication *auth, size_t hash_len,
 
        /* encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
         * EncryptedContent ::= OCTET STRING */
-       asn1_put_hdr(enc_cont_info, ASN1_CLASS_CONTEXT_SPECIFIC, 1, 0,
+       asn1_put_hdr(enc_cont_info, ASN1_CLASS_CONTEXT_SPECIFIC, 0, 0,
                     wpabuf_len(enc_cont));
        wpabuf_put_buf(enc_cont_info, enc_cont);
 
@@ -5605,6 +5642,12 @@ static struct wpabuf * dpp_build_enveloped_data(struct dpp_authentication *auth)
                return NULL;
        }
 
+       if (!auth->provision_configurator) {
+               wpa_printf(MSG_DEBUG,
+                          "DPP: Configurator provisioning not allowed");
+               return NULL;
+       }
+
        wpa_printf(MSG_DEBUG, "DPP: Building DPPEnvelopedData");
 
        hash_len = auth->conf->curve->hash_len;
@@ -10291,6 +10334,10 @@ static int dpp_bootstrap_del(struct dpp_global *dpp, unsigned int id)
                if (id && bi->id != id)
                        continue;
                found = 1;
+#ifdef CONFIG_DPP2
+               if (dpp->remove_bi)
+                       dpp->remove_bi(dpp->cb_ctx, bi);
+#endif /* CONFIG_DPP2 */
                dl_list_del(&bi->list);
                dpp_bootstrap_info_free(bi);
        }
@@ -10498,6 +10545,26 @@ int dpp_bootstrap_info(struct dpp_global *dpp, int id,
 }
 
 
+int dpp_bootstrap_set(struct dpp_global *dpp, int id, const char *params)
+{
+       struct dpp_bootstrap_info *bi;
+
+       bi = dpp_bootstrap_get_id(dpp, id);
+       if (!bi)
+               return -1;
+
+       str_clear_free(bi->configurator_params);
+
+       if (params) {
+               bi->configurator_params = os_strdup(params);
+               return bi->configurator_params ? 0 : -1;
+       }
+
+       bi->configurator_params = NULL;
+       return 0;
+}
+
+
 void dpp_bootstrap_find_pair(struct dpp_global *dpp, const u8 *i_bootstrap,
                             const u8 *r_bootstrap,
                             struct dpp_bootstrap_info **own_bi,
@@ -10841,6 +10908,7 @@ struct dpp_global * dpp_global_init(struct dpp_global_config *config)
 #ifdef CONFIG_DPP2
        dpp->cb_ctx = config->cb_ctx;
        dpp->process_conf_obj = config->process_conf_obj;
+       dpp->remove_bi = config->remove_bi;
 #endif /* CONFIG_DPP2 */
 
        dl_list_init(&dpp->bootstrap);
@@ -11376,7 +11444,8 @@ static int dpp_controller_rx_auth_req(struct dpp_connection *conn,
                return 0;
        }
 
-       conn->auth = dpp_auth_req_rx(conn->ctrl->global->msg_ctx,
+       conn->auth = dpp_auth_req_rx(conn->ctrl->global,
+                                    conn->ctrl->global->msg_ctx,
                                     conn->ctrl->allowed_roles,
                                     conn->ctrl->qr_mutual,
                                     peer_bi, own_bi, -1, hdr, buf, len);
@@ -11385,8 +11454,7 @@ static int dpp_controller_rx_auth_req(struct dpp_connection *conn,
                return -1;
        }
 
-       if (dpp_set_configurator(conn->ctrl->global, conn->ctrl->global->msg_ctx,
-                                conn->auth,
+       if (dpp_set_configurator(conn->auth,
                                 conn->ctrl->configurator_params) < 0) {
                dpp_connection_remove(conn);
                return -1;
@@ -12157,4 +12225,22 @@ void dpp_controller_stop(struct dpp_global *dpp)
        }
 }
 
+
+struct wpabuf * dpp_build_presence_announcement(struct dpp_bootstrap_info *bi)
+{
+       struct wpabuf *msg;
+
+       wpa_printf(MSG_DEBUG, "DPP: Build Presence Announcement frame");
+
+       msg = dpp_alloc_msg(DPP_PA_PRESENCE_ANNOUNCEMENT, 4 + SHA256_MAC_LEN);
+       if (!msg)
+               return NULL;
+
+       /* Responder Bootstrapping Key Hash */
+       dpp_build_attr_r_bootstrap_key_hash(msg, bi->pubkey_hash_chirp);
+       wpa_hexdump_buf(MSG_DEBUG,
+                       "DPP: Presence Announcement frame attributes", msg);
+       return msg;
+}
+
 #endif /* CONFIG_DPP2 */