• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hidl interface for wpa_supplicant daemon
3  * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #ifndef MISC_UTILS_H_
11 #define MISC_UTILS_H_
12 
13 #include <iostream>
14 
15 extern "C"
16 {
17 #include "wpabuf.h"
18 }
19 
20 namespace {
21 constexpr size_t kWpsPinNumDigits = 8;
22 // Custom deleter for wpabuf.
freeWpaBuf(wpabuf * ptr)23 void freeWpaBuf(wpabuf *ptr) { wpabuf_free(ptr); }
24 }  // namespace
25 
26 namespace android {
27 namespace hardware {
28 namespace wifi {
29 namespace supplicant {
30 namespace V1_4 {
31 namespace implementation {
32 namespace misc_utils {
33 using wpabuf_unique_ptr = std::unique_ptr<wpabuf, void (*)(wpabuf *)>;
34 
35 // Creates a unique_ptr for wpabuf ptr with a custom deleter.
createWpaBufUniquePtr(struct wpabuf * raw_ptr)36 inline wpabuf_unique_ptr createWpaBufUniquePtr(struct wpabuf *raw_ptr)
37 {
38 	return {raw_ptr, freeWpaBuf};
39 }
40 
41 // Creates a wpabuf ptr with a custom deleter copying the data from the provided
42 // vector.
convertVectorToWpaBuf(const std::vector<uint8_t> & data)43 inline wpabuf_unique_ptr convertVectorToWpaBuf(const std::vector<uint8_t> &data)
44 {
45 	return createWpaBufUniquePtr(
46 	    wpabuf_alloc_copy(data.data(), data.size()));
47 }
48 
49 // Copies the provided wpabuf contents to a std::vector.
convertWpaBufToVector(const struct wpabuf * buf)50 inline std::vector<uint8_t> convertWpaBufToVector(const struct wpabuf *buf)
51 {
52 	if (buf) {
53 		return std::vector<uint8_t>(
54 		    wpabuf_head_u8(buf), wpabuf_head_u8(buf) + wpabuf_len(buf));
55 	} else {
56 		return std::vector<uint8_t>();
57 	}
58 }
59 
60 // Returns a string holding the wps pin.
convertWpsPinToString(int pin)61 inline std::string convertWpsPinToString(int pin)
62 {
63 	char pin_str[kWpsPinNumDigits + 1];
64 	snprintf(pin_str, sizeof(pin_str), "%08d", pin);
65 	return pin_str;
66 }
67 
serializePmkCacheEntry(std::stringstream & ss,struct rsn_pmksa_cache_entry * pmksa_entry)68 inline std::stringstream& serializePmkCacheEntry(
69     std::stringstream &ss, struct rsn_pmksa_cache_entry *pmksa_entry) {
70 	ss.write((char *) &pmksa_entry->pmk_len, sizeof(pmksa_entry->pmk_len));
71 	ss.write((char *) pmksa_entry->pmk, pmksa_entry->pmk_len);
72 	ss.write((char *) pmksa_entry->pmkid, PMKID_LEN);
73 	ss.write((char *) pmksa_entry->aa, ETH_ALEN);
74 	// Omit wpa_ssid field because the network is created on connecting to a access point.
75 	ss.write((char *) &pmksa_entry->akmp, sizeof(pmksa_entry->akmp));
76 	ss.write((char *) &pmksa_entry->reauth_time, sizeof(pmksa_entry->reauth_time));
77 	ss.write((char *) &pmksa_entry->expiration, sizeof(pmksa_entry->expiration));
78 	ss.write((char *) &pmksa_entry->opportunistic, sizeof(pmksa_entry->opportunistic));
79 	char byte = (pmksa_entry->fils_cache_id_set) ? 1 : 0;
80 	ss.write((char *) &byte, sizeof(byte));
81 	ss.write((char *) pmksa_entry->fils_cache_id, FILS_CACHE_ID_LEN);
82 	ss << std::flush;
83 	return ss;
84 }
85 
deserializePmkCacheEntry(std::stringstream & ss,struct rsn_pmksa_cache_entry * pmksa_entry)86 inline std::stringstream& deserializePmkCacheEntry(
87     std::stringstream &ss, struct rsn_pmksa_cache_entry *pmksa_entry) {
88 	ss.seekg(0);
89 	ss.read((char *) &pmksa_entry->pmk_len, sizeof(pmksa_entry->pmk_len));
90 	ss.read((char *) pmksa_entry->pmk, pmksa_entry->pmk_len);
91 	ss.read((char *) pmksa_entry->pmkid, PMKID_LEN);
92 	ss.read((char *) pmksa_entry->aa, ETH_ALEN);
93 	// Omit wpa_ssid field because the network is created on connecting to a access point.
94 	ss.read((char *) &pmksa_entry->akmp, sizeof(pmksa_entry->akmp));
95 	ss.read((char *) &pmksa_entry->reauth_time, sizeof(pmksa_entry->reauth_time));
96 	ss.read((char *) &pmksa_entry->expiration, sizeof(pmksa_entry->expiration));
97 	ss.read((char *) &pmksa_entry->opportunistic, sizeof(pmksa_entry->opportunistic));
98 	char byte = 0;
99 	ss.read((char *) &byte, sizeof(byte));
100 	pmksa_entry->fils_cache_id_set = (byte) ? 1 : 0;
101 	ss.read((char *) pmksa_entry->fils_cache_id, FILS_CACHE_ID_LEN);
102 	return ss;
103 }
104 }  // namespace misc_utils
105 }  // namespace implementation
106 }  // namespace V1_4
107 }  // namespace supplicant
108 }  // namespace wifi
109 }  // namespace hardware
110 }  // namespace android
111 #endif  // MISC_UTILS_H_
112