Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * IEEE 802.1X-2010 KaY Interface
 * Copyright (c) 2019, The Linux Foundation
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include "utils/includes.h"

#include "utils/common.h"
#include "pae/ieee802_1x_key.h"
#include "pae/ieee802_1x_kay.h"
#include "hostapd.h"
#include "sta_info.h"
#include "wpa_auth_kay.h"
#include "ieee802_1x.h"


#define DEFAULT_KEY_LEN		16
/* secure Connectivity Association Key Name (CKN) */
#define DEFAULT_CKN_LEN		16


static int hapd_macsec_init(void *priv, struct macsec_init_params *params)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->macsec_init)
		return -1;
	return hapd->driver->macsec_init(hapd->drv_priv, params);
}


static int hapd_macsec_deinit(void *priv)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->macsec_deinit)
		return -1;
	return hapd->driver->macsec_deinit(hapd->drv_priv);
}


static int hapd_macsec_get_capability(void *priv, enum macsec_cap *cap)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->macsec_get_capability)
		return -1;
	return hapd->driver->macsec_get_capability(hapd->drv_priv, cap);
}


static int hapd_enable_protect_frames(void *priv, Boolean enabled)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->enable_protect_frames)
		return -1;
	return hapd->driver->enable_protect_frames(hapd->drv_priv, enabled);
}


static int hapd_enable_encrypt(void *priv, Boolean enabled)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->enable_encrypt)
		return -1;
	return hapd->driver->enable_encrypt(hapd->drv_priv, enabled);
}


static int hapd_set_replay_protect(void *priv, Boolean enabled, u32 window)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->set_replay_protect)
		return -1;
	return hapd->driver->set_replay_protect(hapd->drv_priv, enabled,
						 window);
}


static int hapd_set_current_cipher_suite(void *priv, u64 cs)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->set_current_cipher_suite)
		return -1;
	return hapd->driver->set_current_cipher_suite(hapd->drv_priv, cs);
}


static int hapd_enable_controlled_port(void *priv, Boolean enabled)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->enable_controlled_port)
		return -1;
	return hapd->driver->enable_controlled_port(hapd->drv_priv, enabled);
}


static int hapd_get_receive_lowest_pn(void *priv, struct receive_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->get_receive_lowest_pn)
		return -1;
	return hapd->driver->get_receive_lowest_pn(hapd->drv_priv, sa);
}


static int hapd_get_transmit_next_pn(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->get_transmit_next_pn)
		return -1;
	return hapd->driver->get_transmit_next_pn(hapd->drv_priv, sa);
}


static int hapd_set_transmit_next_pn(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->set_transmit_next_pn)
		return -1;
	return hapd->driver->set_transmit_next_pn(hapd->drv_priv, sa);
}


static unsigned int conf_offset_val(enum confidentiality_offset co)
{
	switch (co) {
	case CONFIDENTIALITY_OFFSET_30:
		return 30;
		break;
	case CONFIDENTIALITY_OFFSET_50:
		return 50;
	default:
		return 0;
	}
}


static int hapd_create_receive_sc(void *priv, struct receive_sc *sc,
				  enum validate_frames vf,
				  enum confidentiality_offset co)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->create_receive_sc)
		return -1;
	return hapd->driver->create_receive_sc(hapd->drv_priv, sc,
					       conf_offset_val(co), vf);
}


static int hapd_delete_receive_sc(void *priv, struct receive_sc *sc)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->delete_receive_sc)
		return -1;
	return hapd->driver->delete_receive_sc(hapd->drv_priv, sc);
}


static int hapd_create_receive_sa(void *priv, struct receive_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->create_receive_sa)
		return -1;
	return hapd->driver->create_receive_sa(hapd->drv_priv, sa);
}


static int hapd_delete_receive_sa(void *priv, struct receive_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->delete_receive_sa)
		return -1;
	return hapd->driver->delete_receive_sa(hapd->drv_priv, sa);
}


static int hapd_enable_receive_sa(void *priv, struct receive_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->enable_receive_sa)
		return -1;
	return hapd->driver->enable_receive_sa(hapd->drv_priv, sa);
}


static int hapd_disable_receive_sa(void *priv, struct receive_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->disable_receive_sa)
		return -1;
	return hapd->driver->disable_receive_sa(hapd->drv_priv, sa);
}


static int
hapd_create_transmit_sc(void *priv, struct transmit_sc *sc,
			enum confidentiality_offset co)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->create_transmit_sc)
		return -1;
	return hapd->driver->create_transmit_sc(hapd->drv_priv, sc,
						conf_offset_val(co));
}


static int hapd_delete_transmit_sc(void *priv, struct transmit_sc *sc)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->delete_transmit_sc)
		return -1;
	return hapd->driver->delete_transmit_sc(hapd->drv_priv, sc);
}


static int hapd_create_transmit_sa(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->create_transmit_sa)
		return -1;
	return hapd->driver->create_transmit_sa(hapd->drv_priv, sa);
}


static int hapd_delete_transmit_sa(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->delete_transmit_sa)
		return -1;
	return hapd->driver->delete_transmit_sa(hapd->drv_priv, sa);
}


static int hapd_enable_transmit_sa(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->enable_transmit_sa)
		return -1;
	return hapd->driver->enable_transmit_sa(hapd->drv_priv, sa);
}


static int hapd_disable_transmit_sa(void *priv, struct transmit_sa *sa)
{
	struct hostapd_data *hapd = priv;

	if (!hapd->driver->disable_transmit_sa)
		return -1;
	return hapd->driver->disable_transmit_sa(hapd->drv_priv, sa);
}


int ieee802_1x_alloc_kay_sm_hapd(struct hostapd_data *hapd,
				 struct sta_info *sta)
{
	struct ieee802_1x_kay_ctx *kay_ctx;
	struct ieee802_1x_kay *res = NULL;
	enum macsec_policy policy;

	ieee802_1x_dealloc_kay_sm_hapd(hapd);

	if (!hapd->conf || hapd->conf->macsec_policy == 0)
		return 0;

	if (hapd->conf->macsec_policy == 1) {
		if (hapd->conf->macsec_integ_only == 1)
			policy = SHOULD_SECURE;
		else
			policy = SHOULD_ENCRYPT;
	} else {
		policy = DO_NOT_SECURE;
	}

	wpa_printf(MSG_DEBUG, "%s: if_name=%s", __func__, hapd->conf->iface);
	kay_ctx = os_zalloc(sizeof(*kay_ctx));
	if (!kay_ctx)
		return -1;

	kay_ctx->ctx = hapd;

	kay_ctx->macsec_init = hapd_macsec_init;
	kay_ctx->macsec_deinit = hapd_macsec_deinit;
	kay_ctx->macsec_get_capability = hapd_macsec_get_capability;
	kay_ctx->enable_protect_frames = hapd_enable_protect_frames;
	kay_ctx->enable_encrypt = hapd_enable_encrypt;
	kay_ctx->set_replay_protect = hapd_set_replay_protect;
	kay_ctx->set_current_cipher_suite = hapd_set_current_cipher_suite;
	kay_ctx->enable_controlled_port = hapd_enable_controlled_port;
	kay_ctx->get_receive_lowest_pn = hapd_get_receive_lowest_pn;
	kay_ctx->get_transmit_next_pn = hapd_get_transmit_next_pn;
	kay_ctx->set_transmit_next_pn = hapd_set_transmit_next_pn;
	kay_ctx->create_receive_sc = hapd_create_receive_sc;
	kay_ctx->delete_receive_sc = hapd_delete_receive_sc;
	kay_ctx->create_receive_sa = hapd_create_receive_sa;
	kay_ctx->delete_receive_sa = hapd_delete_receive_sa;
	kay_ctx->enable_receive_sa = hapd_enable_receive_sa;
	kay_ctx->disable_receive_sa = hapd_disable_receive_sa;
	kay_ctx->create_transmit_sc = hapd_create_transmit_sc;
	kay_ctx->delete_transmit_sc = hapd_delete_transmit_sc;
	kay_ctx->create_transmit_sa = hapd_create_transmit_sa;
	kay_ctx->delete_transmit_sa = hapd_delete_transmit_sa;
	kay_ctx->enable_transmit_sa = hapd_enable_transmit_sa;
	kay_ctx->disable_transmit_sa = hapd_disable_transmit_sa;

	res = ieee802_1x_kay_init(kay_ctx, policy,
				  hapd->conf->macsec_replay_protect,
				  hapd->conf->macsec_replay_window,
				  hapd->conf->macsec_port,
				  hapd->conf->mka_priority, hapd->conf->iface,
				  hapd->own_addr);
	/* ieee802_1x_kay_init() frees kay_ctx on failure */
	if (!res)
		return -1;

	hapd->kay = res;

	return 0;
}


void ieee802_1x_dealloc_kay_sm_hapd(struct hostapd_data *hapd)
{
	if (!hapd->kay)
		return;

	ieee802_1x_kay_deinit(hapd->kay);
	hapd->kay = NULL;
}


static int ieee802_1x_auth_get_session_id(struct hostapd_data *hapd,
					  struct sta_info *sta, u8 *sid,
					  size_t *len)
{
	const u8 *session_id;
	size_t id_len, need_len;

	session_id = ieee802_1x_get_session_id(sta->eapol_sm, &id_len);
	if (!session_id) {
		wpa_printf(MSG_DEBUG,
			   "MACsec: Failed to get SessionID from EAPOL state machines");
		return -1;
	}

	need_len = 1 + 2 * 32 /* random size */;
	if (need_len > id_len) {
		wpa_printf(MSG_DEBUG, "EAP Session-Id not long enough");
		return -1;
	}

	os_memcpy(sid, session_id, need_len);
	*len = need_len;

	return 0;
}


static int ieee802_1x_auth_get_msk(struct hostapd_data *hapd,
				   struct sta_info *sta, u8 *msk, size_t *len)
{
	const u8 *key;
	size_t keylen;

	if (!sta->eapol_sm)
		return -1;

	key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
	if (key == NULL) {
		wpa_printf(MSG_DEBUG,
			   "MACsec: Failed to get MSK from EAPOL state machines");
		return -1;
	}
	wpa_printf(MSG_DEBUG, "MACsec: Successfully fetched key (len=%lu)",
		   (unsigned long) keylen);
	wpa_hexdump_key(MSG_DEBUG, "MSK: ", key, keylen);

	if (keylen > *len)
		keylen = *len;
	os_memcpy(msk, key, keylen);
	*len = keylen;

	return 0;
}


void * ieee802_1x_notify_create_actor_hapd(struct hostapd_data *hapd,
					   struct sta_info *sta)
{
	u8 *sid;
	size_t sid_len = 128;
	struct mka_key_name *ckn;
	struct mka_key *cak;
	struct mka_key *msk;
	void *res = NULL;

	if (!hapd->kay || hapd->kay->policy == DO_NOT_SECURE)
		return NULL;

	wpa_printf(MSG_DEBUG,
		   "IEEE 802.1X: External notification - Create MKA for "
		   MACSTR, MAC2STR(sta->addr));

	msk = os_zalloc(sizeof(*msk));
	sid = os_zalloc(sid_len);
	ckn = os_zalloc(sizeof(*ckn));
	cak = os_zalloc(sizeof(*cak));
	if (!msk || !sid || !ckn || !cak)
		goto fail;

	msk->len = DEFAULT_KEY_LEN;
	if (ieee802_1x_auth_get_msk(hapd, sta, msk->key, &msk->len)) {
		wpa_printf(MSG_ERROR, "IEEE 802.1X: Could not get MSK");
		goto fail;
	}

	if (ieee802_1x_auth_get_session_id(hapd, sta, sid, &sid_len))
	{
		wpa_printf(MSG_ERROR,
			   "IEEE 802.1X: Could not get EAP Session Id");
		goto fail;
	}

	wpa_hexdump(MSG_DEBUG, "own_addr", hapd->own_addr, ETH_ALEN);
	wpa_hexdump(MSG_DEBUG, "sta_addr", sta->addr, ETH_ALEN);

	/* Derive CAK from MSK */
	cak->len = DEFAULT_KEY_LEN;
	if (ieee802_1x_cak_aes_cmac(msk->key, msk->len, hapd->own_addr,
				    sta->addr, cak->key, cak->len)) {
		wpa_printf(MSG_ERROR, "IEEE 802.1X: Deriving CAK failed");
		goto fail;
	}
	wpa_hexdump_key(MSG_DEBUG, "Derived CAK", cak->key, cak->len);

	/* Derive CKN from MSK */
	ckn->len = DEFAULT_CKN_LEN;
	if (ieee802_1x_ckn_aes_cmac(msk->key, msk->len, hapd->own_addr,
				    sta->addr, sid, sid_len, ckn->name)) {
		wpa_printf(MSG_ERROR, "IEEE 802.1X: Deriving CKN failed");
		goto fail;
	}
	wpa_hexdump(MSG_DEBUG, "Derived CKN", ckn->name, ckn->len);

	res = ieee802_1x_kay_create_mka(hapd->kay, ckn, cak, 0, EAP_EXCHANGE,
					TRUE);

fail:
	bin_clear_free(msk, sizeof(*msk));
	os_free(sid);
	os_free(ckn);
	bin_clear_free(cak, sizeof(*cak));

	return res;
}


void * ieee802_1x_create_preshared_mka_hapd(struct hostapd_data *hapd,
					    struct sta_info *sta)
{
	struct mka_key *cak;
	struct mka_key_name *ckn;
	void *res = NULL;

	if ((hapd->conf->mka_psk_set & MKA_PSK_SET) != MKA_PSK_SET)
		goto end;

	ckn = os_zalloc(sizeof(*ckn));
	if (!ckn)
		goto end;

	cak = os_zalloc(sizeof(*cak));
	if (!cak)
		goto free_ckn;

	if (ieee802_1x_alloc_kay_sm_hapd(hapd, sta) < 0 || !hapd->kay)
		goto free_cak;

	if (hapd->kay->policy == DO_NOT_SECURE)
		goto dealloc;

	cak->len = hapd->conf->mka_cak_len;
	os_memcpy(cak->key, hapd->conf->mka_cak, cak->len);

	ckn->len = hapd->conf->mka_ckn_len;;
	os_memcpy(ckn->name, hapd->conf->mka_ckn, ckn->len);

	res = ieee802_1x_kay_create_mka(hapd->kay, ckn, cak, 0, PSK, TRUE);
	if (res)
		goto free_cak;

dealloc:
	/* Failed to create MKA */
	ieee802_1x_dealloc_kay_sm_hapd(hapd);
free_cak:
	os_free(cak);
free_ckn:
	os_free(ckn);
end:
	return res;
}