Jouni Malinen | ad08c36 | 2008-11-23 19:34:26 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Wi-Fi Protected Setup |
| 3 | * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "includes.h" |
| 16 | |
| 17 | #include "common.h" |
| 18 | #include "wps_i.h" |
| 19 | #include "wps_dev_attr.h" |
| 20 | |
| 21 | |
| 22 | struct wps_data * wps_init(const struct wps_config *cfg) |
| 23 | { |
| 24 | struct wps_data *data = os_zalloc(sizeof(*data)); |
| 25 | if (data == NULL) |
| 26 | return NULL; |
| 27 | data->authenticator = cfg->authenticator; |
| 28 | data->wps = cfg->wps; |
| 29 | data->registrar = cfg->registrar; |
| 30 | if (cfg->enrollee_mac_addr) |
| 31 | os_memcpy(data->mac_addr_e, cfg->enrollee_mac_addr, ETH_ALEN); |
| 32 | if (cfg->uuid) { |
| 33 | os_memcpy(cfg->registrar ? data->uuid_r : data->uuid_e, |
| 34 | cfg->uuid, WPS_UUID_LEN); |
| 35 | } |
| 36 | if (cfg->pin) { |
| 37 | data->dev_pw_id = DEV_PW_DEFAULT; |
| 38 | data->dev_password = os_malloc(cfg->pin_len); |
| 39 | if (data->dev_password == NULL) { |
| 40 | os_free(data); |
| 41 | return NULL; |
| 42 | } |
| 43 | os_memcpy(data->dev_password, cfg->pin, cfg->pin_len); |
| 44 | data->dev_password_len = cfg->pin_len; |
| 45 | } |
| 46 | |
| 47 | data->pbc = cfg->pbc; |
| 48 | if (cfg->pbc) { |
| 49 | /* Use special PIN '00000000' for PBC */ |
| 50 | data->dev_pw_id = DEV_PW_PUSHBUTTON; |
| 51 | os_free(data->dev_password); |
| 52 | data->dev_password = os_malloc(8); |
| 53 | if (data->dev_password == NULL) { |
| 54 | os_free(data); |
| 55 | return NULL; |
| 56 | } |
| 57 | os_memset(data->dev_password, '0', 8); |
| 58 | data->dev_password_len = 8; |
| 59 | } |
| 60 | |
| 61 | data->wps_cred_cb = cfg->wps_cred_cb; |
| 62 | data->cb_ctx = cfg->cb_ctx; |
| 63 | |
| 64 | data->state = data->registrar ? RECV_M1 : SEND_M1; |
| 65 | |
| 66 | return data; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void wps_deinit(struct wps_data *data) |
| 71 | { |
| 72 | if (data->wps_pin_revealed) { |
| 73 | wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and " |
| 74 | "negotiation failed"); |
| 75 | if (data->registrar) |
| 76 | wps_registrar_invalidate_pin(data->registrar, |
| 77 | data->uuid_e); |
| 78 | } else if (data->registrar) |
| 79 | wps_registrar_unlock_pin(data->registrar, data->uuid_e); |
| 80 | |
| 81 | wpabuf_free(data->dh_privkey); |
| 82 | wpabuf_free(data->dh_pubkey_e); |
| 83 | wpabuf_free(data->dh_pubkey_r); |
| 84 | wpabuf_free(data->last_msg); |
| 85 | os_free(data->dev_password); |
| 86 | os_free(data->new_psk); |
| 87 | wps_device_data_free(&data->peer_dev); |
| 88 | os_free(data); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | enum wps_process_res wps_process_msg(struct wps_data *wps, u8 op_code, |
| 93 | const struct wpabuf *msg) |
| 94 | { |
| 95 | if (wps->registrar) |
| 96 | return wps_registrar_process_msg(wps, op_code, msg); |
| 97 | else |
| 98 | return wps_enrollee_process_msg(wps, op_code, msg); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | struct wpabuf * wps_get_msg(struct wps_data *wps, u8 *op_code) |
| 103 | { |
| 104 | if (wps->registrar) |
| 105 | return wps_registrar_get_msg(wps, op_code); |
| 106 | else |
| 107 | return wps_enrollee_get_msg(wps, op_code); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | int wps_is_selected_pbc_registrar(const u8 *buf, size_t len) |
| 112 | { |
| 113 | struct wps_parse_attr attr; |
| 114 | struct wpabuf msg; |
| 115 | |
| 116 | wpabuf_set(&msg, buf, len); |
| 117 | if (wps_parse_msg(&msg, &attr) < 0 || |
| 118 | !attr.selected_registrar || *attr.selected_registrar == 0 || |
| 119 | !attr.sel_reg_config_methods || |
| 120 | !(WPA_GET_BE16(attr.sel_reg_config_methods) & |
| 121 | WPS_CONFIG_PUSHBUTTON) || |
| 122 | !attr.dev_password_id || |
| 123 | WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON) |
| 124 | return 0; |
| 125 | |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | int wps_is_selected_pin_registrar(const u8 *buf, size_t len) |
| 131 | { |
| 132 | struct wps_parse_attr attr; |
| 133 | struct wpabuf msg; |
| 134 | |
| 135 | wpabuf_set(&msg, buf, len); |
| 136 | if (wps_parse_msg(&msg, &attr) < 0 || |
| 137 | !attr.selected_registrar || *attr.selected_registrar == 0 || |
| 138 | !attr.sel_reg_config_methods || |
| 139 | !(WPA_GET_BE16(attr.sel_reg_config_methods) & |
| 140 | (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)) || |
| 141 | !attr.dev_password_id || |
| 142 | WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON) |
| 143 | return 0; |
| 144 | |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | const u8 * wps_get_uuid_e(const u8 *buf, size_t len) |
| 150 | { |
| 151 | struct wps_parse_attr attr; |
| 152 | struct wpabuf msg; |
| 153 | |
| 154 | wpabuf_set(&msg, buf, len); |
| 155 | if (wps_parse_msg(&msg, &attr) < 0) |
| 156 | return NULL; |
| 157 | return attr.uuid_e; |
| 158 | } |