2 * EAP peer method: EAP-FAST PAC file processing
3 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "eap_config.h"
14 #include "eap_fast_pac.h"
16 /* TODO: encrypt PAC-Key in the PAC file */
19 /* Text data format */
20 static const char *pac_file_hdr =
21 "wpa_supplicant EAP-FAST PAC file - version 1";
25 * 4-octet magic value: 6A E4 92 0C
26 * 2-octet version (big endian)
27 * <version specific data>
30 * Sequence of PAC entries:
31 * 2-octet PAC-Type (big endian)
33 * 2-octet PAC-Opaque length (big endian)
34 * <variable len> PAC-Opaque data (length bytes)
35 * 2-octet PAC-Info length (big endian)
36 * <variable len> PAC-Info data (length bytes)
39 #define EAP_FAST_PAC_BINARY_MAGIC 0x6ae4920c
40 #define EAP_FAST_PAC_BINARY_FORMAT_VERSION 0
44 * eap_fast_free_pac - Free PAC data
45 * @pac: Pointer to the PAC entry
47 * Note that the PAC entry must not be in a list since this function does not
48 * remove the list links.
50 void eap_fast_free_pac(struct eap_fast_pac *pac)
52 os_free(pac->pac_opaque);
53 os_free(pac->pac_info);
56 os_free(pac->a_id_info);
62 * eap_fast_get_pac - Get a PAC entry based on A-ID
63 * @pac_root: Pointer to root of the PAC list
64 * @a_id: A-ID to search for
65 * @a_id_len: Length of A-ID
66 * @pac_type: PAC-Type to search for
67 * Returns: Pointer to the PAC entry, or %NULL if A-ID not found
69 struct eap_fast_pac * eap_fast_get_pac(struct eap_fast_pac *pac_root,
70 const u8 *a_id, size_t a_id_len,
73 struct eap_fast_pac *pac = pac_root;
76 if (pac->pac_type == pac_type && pac->a_id_len == a_id_len &&
77 os_memcmp(pac->a_id, a_id, a_id_len) == 0) {
86 static void eap_fast_remove_pac(struct eap_fast_pac **pac_root,
87 struct eap_fast_pac **pac_current,
88 const u8 *a_id, size_t a_id_len, u16 pac_type)
90 struct eap_fast_pac *pac, *prev;
96 if (pac->pac_type == pac_type && pac->a_id_len == a_id_len &&
97 os_memcmp(pac->a_id, a_id, a_id_len) == 0) {
99 *pac_root = pac->next;
101 prev->next = pac->next;
102 if (*pac_current == pac)
104 eap_fast_free_pac(pac);
113 static int eap_fast_copy_buf(u8 **dst, size_t *dst_len,
114 const u8 *src, size_t src_len)
117 *dst = os_malloc(src_len);
120 os_memcpy(*dst, src, src_len);
128 * eap_fast_add_pac - Add a copy of a PAC entry to a list
129 * @pac_root: Pointer to PAC list root pointer
130 * @pac_current: Pointer to the current PAC pointer
131 * @entry: New entry to clone and add to the list
132 * Returns: 0 on success, -1 on failure
134 * This function makes a clone of the given PAC entry and adds this copied
135 * entry to the list (pac_root). If an old entry for the same A-ID is found,
136 * it will be removed from the PAC list and in this case, pac_current entry
137 * is set to %NULL if it was the removed entry.
139 int eap_fast_add_pac(struct eap_fast_pac **pac_root,
140 struct eap_fast_pac **pac_current,
141 struct eap_fast_pac *entry)
143 struct eap_fast_pac *pac;
145 if (entry == NULL || entry->a_id == NULL)
148 /* Remove a possible old entry for the matching A-ID. */
149 eap_fast_remove_pac(pac_root, pac_current,
150 entry->a_id, entry->a_id_len, entry->pac_type);
152 /* Allocate a new entry and add it to the list of PACs. */
153 pac = os_zalloc(sizeof(*pac));
157 pac->pac_type = entry->pac_type;
158 os_memcpy(pac->pac_key, entry->pac_key, EAP_FAST_PAC_KEY_LEN);
159 if (eap_fast_copy_buf(&pac->pac_opaque, &pac->pac_opaque_len,
160 entry->pac_opaque, entry->pac_opaque_len) < 0 ||
161 eap_fast_copy_buf(&pac->pac_info, &pac->pac_info_len,
162 entry->pac_info, entry->pac_info_len) < 0 ||
163 eap_fast_copy_buf(&pac->a_id, &pac->a_id_len,
164 entry->a_id, entry->a_id_len) < 0 ||
165 eap_fast_copy_buf(&pac->i_id, &pac->i_id_len,
166 entry->i_id, entry->i_id_len) < 0 ||
167 eap_fast_copy_buf(&pac->a_id_info, &pac->a_id_info_len,
168 entry->a_id_info, entry->a_id_info_len) < 0) {
169 eap_fast_free_pac(pac);
173 pac->next = *pac_root;
180 struct eap_fast_read_ctx {
189 static int eap_fast_read_line(struct eap_fast_read_ctx *rc, char **value)
195 if (fgets(rc->buf, rc->buf_len, rc->f) == NULL)
200 if (rc->pos >= rc->end)
203 while (l_end < rc->end && *l_end != '\n')
205 len = l_end - rc->pos;
206 if (len >= rc->buf_len)
207 len = rc->buf_len - 1;
208 os_memcpy(rc->buf, rc->pos, len);
213 rc->buf[rc->buf_len - 1] = '\0';
215 while (*pos != '\0') {
216 if (*pos == '\n' || *pos == '\r') {
223 pos = os_strchr(rc->buf, '=');
232 static u8 * eap_fast_parse_hex(const char *value, size_t *len)
239 hlen = os_strlen(value);
243 buf = os_malloc(*len);
246 if (hexstr2bin(value, buf, *len)) {
254 static int eap_fast_init_pac_data(struct eap_sm *sm, const char *pac_file,
255 struct eap_fast_read_ctx *rc)
257 os_memset(rc, 0, sizeof(*rc));
260 rc->buf = os_malloc(rc->buf_len);
264 if (os_strncmp(pac_file, "blob://", 7) == 0) {
265 const struct wpa_config_blob *blob;
266 blob = eap_get_config_blob(sm, pac_file + 7);
268 wpa_printf(MSG_INFO, "EAP-FAST: No PAC blob '%s' - "
269 "assume no PAC entries have been "
270 "provisioned", pac_file + 7);
274 rc->pos = (char *) blob->data;
275 rc->end = (char *) blob->data + blob->len;
277 rc->f = fopen(pac_file, "rb");
279 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file '%s' - "
280 "assume no PAC entries have been "
281 "provisioned", pac_file);
291 static void eap_fast_deinit_pac_data(struct eap_fast_read_ctx *rc)
299 static const char * eap_fast_parse_start(struct eap_fast_pac **pac)
302 return "START line without END";
304 *pac = os_zalloc(sizeof(struct eap_fast_pac));
306 return "No memory for PAC entry";
307 (*pac)->pac_type = PAC_TYPE_TUNNEL_PAC;
312 static const char * eap_fast_parse_end(struct eap_fast_pac **pac_root,
313 struct eap_fast_pac **pac)
316 return "END line without START";
318 struct eap_fast_pac *end = *pac_root;
330 static const char * eap_fast_parse_pac_type(struct eap_fast_pac *pac,
334 return "Cannot parse pac type";
335 pac->pac_type = atoi(pos);
336 if (pac->pac_type != PAC_TYPE_TUNNEL_PAC &&
337 pac->pac_type != PAC_TYPE_USER_AUTHORIZATION &&
338 pac->pac_type != PAC_TYPE_MACHINE_AUTHENTICATION)
339 return "Unrecognized PAC-Type";
345 static const char * eap_fast_parse_pac_key(struct eap_fast_pac *pac, char *pos)
350 key = eap_fast_parse_hex(pos, &key_len);
351 if (key == NULL || key_len != EAP_FAST_PAC_KEY_LEN) {
353 return "Invalid PAC-Key";
356 os_memcpy(pac->pac_key, key, EAP_FAST_PAC_KEY_LEN);
363 static const char * eap_fast_parse_pac_opaque(struct eap_fast_pac *pac,
366 os_free(pac->pac_opaque);
367 pac->pac_opaque = eap_fast_parse_hex(pos, &pac->pac_opaque_len);
368 if (pac->pac_opaque == NULL)
369 return "Invalid PAC-Opaque";
374 static const char * eap_fast_parse_a_id(struct eap_fast_pac *pac, char *pos)
377 pac->a_id = eap_fast_parse_hex(pos, &pac->a_id_len);
378 if (pac->a_id == NULL)
379 return "Invalid A-ID";
384 static const char * eap_fast_parse_i_id(struct eap_fast_pac *pac, char *pos)
387 pac->i_id = eap_fast_parse_hex(pos, &pac->i_id_len);
388 if (pac->i_id == NULL)
389 return "Invalid I-ID";
394 static const char * eap_fast_parse_a_id_info(struct eap_fast_pac *pac,
397 os_free(pac->a_id_info);
398 pac->a_id_info = eap_fast_parse_hex(pos, &pac->a_id_info_len);
399 if (pac->a_id_info == NULL)
400 return "Invalid A-ID-Info";
406 * eap_fast_load_pac - Load PAC entries (text format)
407 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
408 * @pac_root: Pointer to root of the PAC list (to be filled)
409 * @pac_file: Name of the PAC file/blob to load
410 * Returns: 0 on success, -1 on failure
412 int eap_fast_load_pac(struct eap_sm *sm, struct eap_fast_pac **pac_root,
413 const char *pac_file)
415 struct eap_fast_read_ctx rc;
416 struct eap_fast_pac *pac = NULL;
419 const char *err = NULL;
421 if (pac_file == NULL)
424 if (eap_fast_init_pac_data(sm, pac_file, &rc) < 0)
427 if (eap_fast_read_line(&rc, &pos) < 0) {
428 /* empty file - assume it is fine to overwrite */
429 eap_fast_deinit_pac_data(&rc);
432 if (os_strcmp(pac_file_hdr, rc.buf) != 0)
433 err = "Unrecognized header line";
435 while (!err && eap_fast_read_line(&rc, &pos) == 0) {
436 if (os_strcmp(rc.buf, "START") == 0)
437 err = eap_fast_parse_start(&pac);
438 else if (os_strcmp(rc.buf, "END") == 0) {
439 err = eap_fast_parse_end(pac_root, &pac);
442 err = "Unexpected line outside START/END block";
443 else if (os_strcmp(rc.buf, "PAC-Type") == 0)
444 err = eap_fast_parse_pac_type(pac, pos);
445 else if (os_strcmp(rc.buf, "PAC-Key") == 0)
446 err = eap_fast_parse_pac_key(pac, pos);
447 else if (os_strcmp(rc.buf, "PAC-Opaque") == 0)
448 err = eap_fast_parse_pac_opaque(pac, pos);
449 else if (os_strcmp(rc.buf, "A-ID") == 0)
450 err = eap_fast_parse_a_id(pac, pos);
451 else if (os_strcmp(rc.buf, "I-ID") == 0)
452 err = eap_fast_parse_i_id(pac, pos);
453 else if (os_strcmp(rc.buf, "A-ID-Info") == 0)
454 err = eap_fast_parse_a_id_info(pac, pos);
458 err = "PAC block not terminated with END";
459 eap_fast_free_pac(pac);
462 eap_fast_deinit_pac_data(&rc);
465 wpa_printf(MSG_INFO, "EAP-FAST: %s in '%s:%d'",
466 err, pac_file, rc.line);
470 wpa_printf(MSG_DEBUG, "EAP-FAST: Read %d PAC entries from '%s'",
477 static void eap_fast_write(char **buf, char **pos, size_t *buf_len,
478 const char *field, const u8 *data,
485 if (data == NULL || buf == NULL || *buf == NULL ||
486 pos == NULL || *pos == NULL || *pos < *buf)
489 need = os_strlen(field) + len * 2 + 30;
491 need += os_strlen(field) + len + 20;
493 if (*pos - *buf + need > *buf_len) {
494 char *nbuf = os_realloc(*buf, *buf_len + need);
500 *pos = nbuf + (*pos - *buf);
504 end = *buf + *buf_len;
506 ret = os_snprintf(*pos, end - *pos, "%s=", field);
507 if (os_snprintf_error(end - *pos, ret))
510 *pos += wpa_snprintf_hex(*pos, end - *pos, data, len);
511 ret = os_snprintf(*pos, end - *pos, "\n");
512 if (os_snprintf_error(end - *pos, ret))
517 ret = os_snprintf(*pos, end - *pos, "%s-txt=", field);
518 if (os_snprintf_error(end - *pos, ret))
521 for (i = 0; i < len; i++) {
522 ret = os_snprintf(*pos, end - *pos, "%c", data[i]);
523 if (os_snprintf_error(end - *pos, ret))
527 ret = os_snprintf(*pos, end - *pos, "\n");
528 if (os_snprintf_error(end - *pos, ret))
535 static int eap_fast_write_pac(struct eap_sm *sm, const char *pac_file,
536 char *buf, size_t len)
538 if (os_strncmp(pac_file, "blob://", 7) == 0) {
539 struct wpa_config_blob *blob;
540 blob = os_zalloc(sizeof(*blob));
543 blob->data = (u8 *) buf;
546 blob->name = os_strdup(pac_file + 7);
547 if (blob->name == NULL) {
551 eap_set_config_blob(sm, blob);
554 f = fopen(pac_file, "wb");
556 wpa_printf(MSG_INFO, "EAP-FAST: Failed to open PAC "
557 "file '%s' for writing", pac_file);
560 if (fwrite(buf, 1, len, f) != len) {
561 wpa_printf(MSG_INFO, "EAP-FAST: Failed to write all "
562 "PACs into '%s'", pac_file);
574 static int eap_fast_add_pac_data(struct eap_fast_pac *pac, char **buf,
575 char **pos, size_t *buf_len)
579 ret = os_snprintf(*pos, *buf + *buf_len - *pos,
580 "START\nPAC-Type=%d\n", pac->pac_type);
581 if (os_snprintf_error(*buf + *buf_len - *pos, ret))
585 eap_fast_write(buf, pos, buf_len, "PAC-Key",
586 pac->pac_key, EAP_FAST_PAC_KEY_LEN, 0);
587 eap_fast_write(buf, pos, buf_len, "PAC-Opaque",
588 pac->pac_opaque, pac->pac_opaque_len, 0);
589 eap_fast_write(buf, pos, buf_len, "PAC-Info",
590 pac->pac_info, pac->pac_info_len, 0);
591 eap_fast_write(buf, pos, buf_len, "A-ID",
592 pac->a_id, pac->a_id_len, 0);
593 eap_fast_write(buf, pos, buf_len, "I-ID",
594 pac->i_id, pac->i_id_len, 1);
595 eap_fast_write(buf, pos, buf_len, "A-ID-Info",
596 pac->a_id_info, pac->a_id_info_len, 1);
598 wpa_printf(MSG_DEBUG, "EAP-FAST: No memory for PAC "
602 ret = os_snprintf(*pos, *buf + *buf_len - *pos, "END\n");
603 if (os_snprintf_error(*buf + *buf_len - *pos, ret))
612 * eap_fast_save_pac - Save PAC entries (text format)
613 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
614 * @pac_root: Root of the PAC list
615 * @pac_file: Name of the PAC file/blob
616 * Returns: 0 on success, -1 on failure
618 int eap_fast_save_pac(struct eap_sm *sm, struct eap_fast_pac *pac_root,
619 const char *pac_file)
621 struct eap_fast_pac *pac;
626 if (pac_file == NULL)
630 pos = buf = os_malloc(buf_len);
634 ret = os_snprintf(pos, buf + buf_len - pos, "%s\n", pac_file_hdr);
635 if (os_snprintf_error(buf + buf_len - pos, ret)) {
643 if (eap_fast_add_pac_data(pac, &buf, &pos, &buf_len)) {
651 if (eap_fast_write_pac(sm, pac_file, buf, pos - buf)) {
656 wpa_printf(MSG_DEBUG, "EAP-FAST: Wrote %d PAC entries into '%s'",
664 * eap_fast_pac_list_truncate - Truncate a PAC list to the given length
665 * @pac_root: Root of the PAC list
666 * @max_len: Maximum length of the list (>= 1)
667 * Returns: Number of PAC entries removed
669 size_t eap_fast_pac_list_truncate(struct eap_fast_pac *pac_root,
672 struct eap_fast_pac *pac, *prev;
687 if (count <= max_len || prev == NULL)
696 eap_fast_free_pac(prev);
704 static void eap_fast_pac_get_a_id(struct eap_fast_pac *pac)
710 end = pos + pac->pac_info_len;
712 while (pos + 4 < end) {
713 type = WPA_GET_BE16(pos);
715 len = WPA_GET_BE16(pos);
717 if (len > (unsigned int) (end - pos))
720 if (type == PAC_TYPE_A_ID) {
722 pac->a_id = os_malloc(len);
723 if (pac->a_id == NULL)
725 os_memcpy(pac->a_id, pos, len);
729 if (type == PAC_TYPE_A_ID_INFO) {
730 os_free(pac->a_id_info);
731 pac->a_id_info = os_malloc(len);
732 if (pac->a_id_info == NULL)
734 os_memcpy(pac->a_id_info, pos, len);
735 pac->a_id_info_len = len;
744 * eap_fast_load_pac_bin - Load PAC entries (binary format)
745 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
746 * @pac_root: Pointer to root of the PAC list (to be filled)
747 * @pac_file: Name of the PAC file/blob to load
748 * Returns: 0 on success, -1 on failure
750 int eap_fast_load_pac_bin(struct eap_sm *sm, struct eap_fast_pac **pac_root,
751 const char *pac_file)
753 const struct wpa_config_blob *blob = NULL;
755 size_t len, count = 0;
756 struct eap_fast_pac *pac, *prev;
760 if (pac_file == NULL)
763 if (os_strncmp(pac_file, "blob://", 7) == 0) {
764 blob = eap_get_config_blob(sm, pac_file + 7);
766 wpa_printf(MSG_INFO, "EAP-FAST: No PAC blob '%s' - "
767 "assume no PAC entries have been "
768 "provisioned", pac_file + 7);
774 buf = (u8 *) os_readfile(pac_file, &len);
776 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file '%s' - "
777 "assume no PAC entries have been "
778 "provisioned", pac_file);
789 if (len < 6 || WPA_GET_BE32(buf) != EAP_FAST_PAC_BINARY_MAGIC ||
790 WPA_GET_BE16(buf + 4) != EAP_FAST_PAC_BINARY_FORMAT_VERSION) {
791 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC file '%s' (bin)",
804 if (end - pos < 2 + EAP_FAST_PAC_KEY_LEN + 2 + 2)
807 pac = os_zalloc(sizeof(*pac));
811 pac->pac_type = WPA_GET_BE16(pos);
813 os_memcpy(pac->pac_key, pos, EAP_FAST_PAC_KEY_LEN);
814 pos += EAP_FAST_PAC_KEY_LEN;
815 val = WPA_GET_BE16(pos);
819 pac->pac_opaque_len = val;
820 pac->pac_opaque = os_malloc(pac->pac_opaque_len);
821 if (pac->pac_opaque == NULL)
823 os_memcpy(pac->pac_opaque, pos, pac->pac_opaque_len);
824 pos += pac->pac_opaque_len;
827 val = WPA_GET_BE16(pos);
831 pac->pac_info_len = val;
832 pac->pac_info = os_malloc(pac->pac_info_len);
833 if (pac->pac_info == NULL)
835 os_memcpy(pac->pac_info, pos, pac->pac_info_len);
836 pos += pac->pac_info_len;
837 eap_fast_pac_get_a_id(pac);
850 wpa_printf(MSG_DEBUG, "EAP-FAST: Read %lu PAC entries from '%s' (bin)",
851 (unsigned long) count, pac_file);
856 wpa_printf(MSG_INFO, "EAP-FAST: Failed to parse PAC file '%s' (bin)",
861 eap_fast_free_pac(pac);
867 * eap_fast_save_pac_bin - Save PAC entries (binary format)
868 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
869 * @pac_root: Root of the PAC list
870 * @pac_file: Name of the PAC file/blob
871 * Returns: 0 on success, -1 on failure
873 int eap_fast_save_pac_bin(struct eap_sm *sm, struct eap_fast_pac *pac_root,
874 const char *pac_file)
876 size_t len, count = 0;
877 struct eap_fast_pac *pac;
883 if (pac->pac_opaque_len > 65535 ||
884 pac->pac_info_len > 65535)
886 len += 2 + EAP_FAST_PAC_KEY_LEN + 2 + pac->pac_opaque_len +
887 2 + pac->pac_info_len;
891 buf = os_malloc(len);
896 WPA_PUT_BE32(pos, EAP_FAST_PAC_BINARY_MAGIC);
898 WPA_PUT_BE16(pos, EAP_FAST_PAC_BINARY_FORMAT_VERSION);
903 WPA_PUT_BE16(pos, pac->pac_type);
905 os_memcpy(pos, pac->pac_key, EAP_FAST_PAC_KEY_LEN);
906 pos += EAP_FAST_PAC_KEY_LEN;
907 WPA_PUT_BE16(pos, pac->pac_opaque_len);
909 os_memcpy(pos, pac->pac_opaque, pac->pac_opaque_len);
910 pos += pac->pac_opaque_len;
911 WPA_PUT_BE16(pos, pac->pac_info_len);
913 os_memcpy(pos, pac->pac_info, pac->pac_info_len);
914 pos += pac->pac_info_len;
920 if (eap_fast_write_pac(sm, pac_file, (char *) buf, len)) {
925 wpa_printf(MSG_DEBUG, "EAP-FAST: Wrote %lu PAC entries into '%s' "
926 "(bin)", (unsigned long) count, pac_file);