1 | /* |
2 | SPDX-FileCopyrightText: Pranav Gade <pranavgade20@gmail.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "macsecsetting.h" |
8 | #include "macsecsetting_p.h" |
9 | |
10 | #include <QDebug> |
11 | |
12 | #if !NM_CHECK_VERSION(1, 6, 0) |
13 | #define NM_SETTING_MACSEC_SETTING_NAME "macsec" |
14 | |
15 | #define NM_SETTING_MACSEC_PARENT "parent" |
16 | #define NM_SETTING_MACSEC_MODE "mode" |
17 | #define NM_SETTING_MACSEC_ENCRYPT "encrypt" |
18 | #define NM_SETTING_MACSEC_MKA_CAK "mka-cak" |
19 | #define NM_SETTING_MACSEC_MKA_CAK_FLAGS "mka-cak-flags" |
20 | #define NM_SETTING_MACSEC_MKA_CKN "mka-ckn" |
21 | #define NM_SETTING_MACSEC_PORT "port" |
22 | #define NM_SETTING_MACSEC_VALIDATION "validation" |
23 | #endif |
24 | |
25 | #if !NM_CHECK_VERSION(1, 12, 0) |
26 | #define NM_SETTING_MACSEC_SEND_SCI "send-sci" |
27 | #endif |
28 | |
29 | NetworkManager::MacsecSettingPrivate::MacsecSettingPrivate() |
30 | : name(NM_SETTING_MACSEC_SETTING_NAME) |
31 | , encrypt(true) |
32 | , mode(NetworkManager::MacsecSetting::Psk) |
33 | , port(1) |
34 | , sendSci(true) |
35 | , validation(NetworkManager::MacsecSetting::Strict) |
36 | { |
37 | } |
38 | |
39 | NetworkManager::MacsecSetting::MacsecSetting() |
40 | : Setting(Setting::Macsec) |
41 | , d_ptr(new MacsecSettingPrivate()) |
42 | { |
43 | } |
44 | |
45 | NetworkManager::MacsecSetting::MacsecSetting(const Ptr &other) |
46 | : Setting(other) |
47 | , d_ptr(new MacsecSettingPrivate()) |
48 | { |
49 | setEncrypt(other->encrypt()); |
50 | setMkaCak(other->mkaCak()); |
51 | setMkaCkn(other->mkaCkn()); |
52 | setMode(other->mode()); |
53 | setParent(other->parent()); |
54 | setPort(other->port()); |
55 | setSendSci(other->sendSci()); |
56 | setValidation(other->validation()); |
57 | setMkaCakFlags(other->mkaCakFlags()); |
58 | } |
59 | |
60 | NetworkManager::MacsecSetting::~MacsecSetting() |
61 | { |
62 | delete d_ptr; |
63 | } |
64 | |
65 | QString NetworkManager::MacsecSetting::name() const |
66 | { |
67 | Q_D(const MacsecSetting); |
68 | |
69 | return d->name; |
70 | } |
71 | |
72 | void NetworkManager::MacsecSetting::setEncrypt(bool encrypt) |
73 | { |
74 | Q_D(MacsecSetting); |
75 | |
76 | d->encrypt = encrypt; |
77 | } |
78 | |
79 | bool NetworkManager::MacsecSetting::encrypt() const |
80 | { |
81 | Q_D(const MacsecSetting); |
82 | |
83 | return d->encrypt; |
84 | } |
85 | |
86 | void NetworkManager::MacsecSetting::setMkaCak(const QString &mkaCak) |
87 | { |
88 | Q_D(MacsecSetting); |
89 | |
90 | d->mkaCak = mkaCak; |
91 | } |
92 | |
93 | QString NetworkManager::MacsecSetting::mkaCak() const |
94 | { |
95 | Q_D(const MacsecSetting); |
96 | |
97 | return d->mkaCak; |
98 | } |
99 | |
100 | void NetworkManager::MacsecSetting::setMkaCkn(const QString &mkaCkn) |
101 | { |
102 | Q_D(MacsecSetting); |
103 | |
104 | d->mkaCkn = mkaCkn; |
105 | } |
106 | |
107 | QString NetworkManager::MacsecSetting::mkaCkn() const |
108 | { |
109 | Q_D(const MacsecSetting); |
110 | |
111 | return d->mkaCkn; |
112 | } |
113 | |
114 | void NetworkManager::MacsecSetting::setMode(Mode mode) |
115 | { |
116 | Q_D(MacsecSetting); |
117 | |
118 | d->mode = mode; |
119 | } |
120 | |
121 | NetworkManager::MacsecSetting::Mode NetworkManager::MacsecSetting::mode() const |
122 | { |
123 | Q_D(const MacsecSetting); |
124 | |
125 | return d->mode; |
126 | } |
127 | |
128 | void NetworkManager::MacsecSetting::setParent(const QString &parent) |
129 | { |
130 | Q_D(MacsecSetting); |
131 | |
132 | d->parent = parent; |
133 | } |
134 | |
135 | QString NetworkManager::MacsecSetting::parent() const |
136 | { |
137 | Q_D(const MacsecSetting); |
138 | |
139 | return d->parent; |
140 | } |
141 | |
142 | void NetworkManager::MacsecSetting::setPort(qint32 port) |
143 | { |
144 | Q_D(MacsecSetting); |
145 | |
146 | d->port = port; |
147 | } |
148 | |
149 | qint32 NetworkManager::MacsecSetting::port() const |
150 | { |
151 | Q_D(const MacsecSetting); |
152 | |
153 | return d->port; |
154 | } |
155 | |
156 | void NetworkManager::MacsecSetting::setSendSci(bool sendSci) |
157 | { |
158 | Q_D(MacsecSetting); |
159 | |
160 | d->sendSci = sendSci; |
161 | } |
162 | |
163 | bool NetworkManager::MacsecSetting::sendSci() const |
164 | { |
165 | Q_D(const MacsecSetting); |
166 | |
167 | return d->sendSci; |
168 | } |
169 | |
170 | void NetworkManager::MacsecSetting::setValidation(Validation validation) |
171 | { |
172 | Q_D(MacsecSetting); |
173 | |
174 | d->validation = validation; |
175 | } |
176 | |
177 | NetworkManager::MacsecSetting::Validation NetworkManager::MacsecSetting::validation() const |
178 | { |
179 | Q_D(const MacsecSetting); |
180 | |
181 | return d->validation; |
182 | } |
183 | |
184 | void NetworkManager::MacsecSetting::setMkaCakFlags(NetworkManager::Setting::SecretFlags flags) |
185 | { |
186 | Q_D(MacsecSetting); |
187 | |
188 | d->mkaCakFlags = flags; |
189 | } |
190 | |
191 | NetworkManager::Setting::SecretFlags NetworkManager::MacsecSetting::mkaCakFlags() const |
192 | { |
193 | Q_D(const MacsecSetting); |
194 | |
195 | return d->mkaCakFlags; |
196 | } |
197 | |
198 | QStringList NetworkManager::MacsecSetting::needSecrets(bool requestNew) const |
199 | { |
200 | QStringList secrets; |
201 | |
202 | if ((mkaCak().isEmpty() || requestNew) && !mkaCakFlags().testFlag(flag: Setting::NotRequired)) { |
203 | secrets << QLatin1String(NM_SETTING_MACSEC_MKA_CAK); |
204 | } |
205 | |
206 | return secrets; |
207 | } |
208 | |
209 | void NetworkManager::MacsecSetting::secretsFromMap(const QVariantMap &secrets) |
210 | { |
211 | if (secrets.contains(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK))) { |
212 | setMkaCak(secrets.value(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK)).toString()); |
213 | } |
214 | } |
215 | |
216 | QVariantMap NetworkManager::MacsecSetting::secretsToMap() const |
217 | { |
218 | QVariantMap secrets; |
219 | |
220 | if (!mkaCak().isEmpty()) { |
221 | secrets.insert(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK), value: mkaCak()); |
222 | } |
223 | |
224 | return secrets; |
225 | } |
226 | |
227 | void NetworkManager::MacsecSetting::fromMap(const QVariantMap &setting) |
228 | { |
229 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_ENCRYPT))) { |
230 | setEncrypt(setting.value(key: QLatin1String(NM_SETTING_MACSEC_ENCRYPT)).toBool()); |
231 | } |
232 | |
233 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK))) { |
234 | setMkaCak(setting.value(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK)).toString()); |
235 | } |
236 | |
237 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_MKA_CKN))) { |
238 | setMkaCkn(setting.value(key: QLatin1String(NM_SETTING_MACSEC_MKA_CKN)).toString()); |
239 | } |
240 | |
241 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_MODE))) { |
242 | setMode((Mode)setting.value(key: QLatin1String(NM_SETTING_MACSEC_MODE)).toInt()); |
243 | } |
244 | |
245 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_PARENT))) { |
246 | setParent(setting.value(key: QLatin1String(NM_SETTING_MACSEC_PARENT)).toString()); |
247 | } |
248 | |
249 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_PORT))) { |
250 | setPort(setting.value(key: QLatin1String(NM_SETTING_MACSEC_PORT)).toInt()); |
251 | } |
252 | |
253 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_SEND_SCI))) { |
254 | setSendSci(setting.value(key: QLatin1String(NM_SETTING_MACSEC_SEND_SCI)).toBool()); |
255 | } |
256 | |
257 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_VALIDATION))) { |
258 | setValidation((Validation)setting.value(key: QLatin1String(NM_SETTING_MACSEC_VALIDATION)).toInt()); |
259 | } |
260 | |
261 | if (setting.contains(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK_FLAGS))) { |
262 | setMkaCakFlags((Setting::SecretFlags)setting.value(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK_FLAGS)).toUInt()); |
263 | } |
264 | } |
265 | |
266 | QVariantMap NetworkManager::MacsecSetting::toMap() const |
267 | { |
268 | QVariantMap setting; |
269 | |
270 | if (!encrypt()) { |
271 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_ENCRYPT), value: encrypt()); |
272 | } |
273 | |
274 | if (!mkaCak().isEmpty()) { |
275 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK), value: mkaCak()); |
276 | } |
277 | |
278 | if (!mkaCkn().isEmpty()) { |
279 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_MKA_CKN), value: mkaCkn()); |
280 | } |
281 | |
282 | if (mode() > NetworkManager::MacsecSetting::Psk) { |
283 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_MODE), value: (int)mode()); |
284 | } |
285 | |
286 | if (!parent().isEmpty()) { |
287 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_PARENT), value: parent()); |
288 | } |
289 | |
290 | if (port() > 1) { |
291 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_PORT), value: port()); |
292 | } |
293 | |
294 | if (!sendSci()) { |
295 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_SEND_SCI), value: sendSci()); |
296 | } |
297 | |
298 | if (validation() != NetworkManager::MacsecSetting::Strict) { |
299 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_VALIDATION), value: (int)validation()); |
300 | } |
301 | |
302 | setting.insert(key: QLatin1String(NM_SETTING_MACSEC_MKA_CAK_FLAGS), value: (int)mkaCakFlags()); |
303 | |
304 | return setting; |
305 | } |
306 | |
307 | QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::MacsecSetting &setting) |
308 | { |
309 | dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n'; |
310 | dbg.nospace() << "initialized: " << !setting.isNull() << '\n'; |
311 | |
312 | dbg.nospace() << NM_SETTING_MACSEC_ENCRYPT << ": " << setting.encrypt() << '\n'; |
313 | dbg.nospace() << NM_SETTING_MACSEC_MKA_CAK << ": " << setting.mkaCak() << '\n'; |
314 | dbg.nospace() << NM_SETTING_MACSEC_MKA_CKN << ": " << setting.mkaCkn() << '\n'; |
315 | dbg.nospace() << NM_SETTING_MACSEC_MODE << ": " << setting.mode() << '\n'; |
316 | dbg.nospace() << NM_SETTING_MACSEC_PARENT << ": " << setting.parent() << '\n'; |
317 | dbg.nospace() << NM_SETTING_MACSEC_PORT << ": " << setting.port() << '\n'; |
318 | dbg.nospace() << NM_SETTING_MACSEC_SEND_SCI << ": " << setting.sendSci() << '\n'; |
319 | dbg.nospace() << NM_SETTING_MACSEC_VALIDATION << ": " << setting.validation() << '\n'; |
320 | dbg.nospace() << NM_SETTING_MACSEC_MKA_CAK_FLAGS << ": " << setting.mkaCakFlags() << '\n'; |
321 | |
322 | return dbg.maybeSpace(); |
323 | } |
324 | |