Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/eap_server/eap_server_methods.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * EAP server method registration
3
 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "includes.h"
10
11
#include "common.h"
12
#include "eap_i.h"
13
#include "eap_methods.h"
14
15
16
static struct eap_method *eap_methods;
17
18
19
/**
20
 * eap_server_get_eap_method - Get EAP method based on type number
21
 * @vendor: EAP Vendor-Id (0 = IETF)
22
 * @method: EAP type number
23
 * Returns: Pointer to EAP method or %NULL if not found
24
 */
25
const struct eap_method * eap_server_get_eap_method(int vendor,
26
                enum eap_type method)
27
0
{
28
0
  struct eap_method *m;
29
0
  for (m = eap_methods; m; m = m->next) {
30
0
    if (m->vendor == vendor && m->method == method)
31
0
      return m;
32
0
  }
33
0
  return NULL;
34
0
}
35
36
37
/**
38
 * eap_server_get_type - Get EAP type for the given EAP method name
39
 * @name: EAP method name, e.g., TLS
40
 * @vendor: Buffer for returning EAP Vendor-Id
41
 * Returns: EAP method type or %EAP_TYPE_NONE if not found
42
 *
43
 * This function maps EAP type names into EAP type numbers based on the list of
44
 * EAP methods included in the build.
45
 */
46
enum eap_type eap_server_get_type(const char *name, int *vendor)
47
0
{
48
0
  struct eap_method *m;
49
0
  for (m = eap_methods; m; m = m->next) {
50
0
    if (os_strcmp(m->name, name) == 0) {
51
0
      *vendor = m->vendor;
52
0
      return m->method;
53
0
    }
54
0
  }
55
0
  *vendor = EAP_VENDOR_IETF;
56
0
  return EAP_TYPE_NONE;
57
0
}
58
59
60
/**
61
 * eap_server_method_alloc - Allocate EAP server method structure
62
 * @version: Version of the EAP server method interface (set to
63
 * EAP_SERVER_METHOD_INTERFACE_VERSION)
64
 * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
65
 * @method: EAP type number (EAP_TYPE_*)
66
 * @name: Name of the method (e.g., "TLS")
67
 * Returns: Allocated EAP method structure or %NULL on failure
68
 *
69
 * The returned structure should be freed with eap_server_method_free() when it
70
 * is not needed anymore.
71
 */
72
struct eap_method * eap_server_method_alloc(int version, int vendor,
73
              enum eap_type method,
74
              const char *name)
75
0
{
76
0
  struct eap_method *eap;
77
0
  eap = os_zalloc(sizeof(*eap));
78
0
  if (eap == NULL)
79
0
    return NULL;
80
0
  eap->version = version;
81
0
  eap->vendor = vendor;
82
0
  eap->method = method;
83
0
  eap->name = name;
84
0
  return eap;
85
0
}
86
87
88
/**
89
 * eap_server_method_free - Free EAP server method structure
90
 * @method: Method structure allocated with eap_server_method_alloc()
91
 */
92
static void eap_server_method_free(struct eap_method *method)
93
0
{
94
0
  os_free(method);
95
0
}
96
97
98
/**
99
 * eap_server_method_register - Register an EAP server method
100
 * @method: EAP method to register from eap_server_method_alloc()
101
 * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
102
 * has already been registered
103
 *
104
 * Each EAP server method needs to call this function to register itself as a
105
 * supported EAP method. The caller must not free the allocated method data
106
 * regardless of the return value.
107
 */
108
int eap_server_method_register(struct eap_method *method)
109
0
{
110
0
  struct eap_method *m, *last = NULL;
111
112
0
  if (method == NULL || method->name == NULL ||
113
0
      method->version != EAP_SERVER_METHOD_INTERFACE_VERSION) {
114
0
    eap_server_method_free(method);
115
0
    return -1;
116
0
  }
117
118
0
  for (m = eap_methods; m; m = m->next) {
119
0
    if ((m->vendor == method->vendor &&
120
0
         m->method == method->method) ||
121
0
        os_strcmp(m->name, method->name) == 0) {
122
0
      eap_server_method_free(method);
123
0
      return -2;
124
0
    }
125
0
    last = m;
126
0
  }
127
128
0
  if (last)
129
0
    last->next = method;
130
0
  else
131
0
    eap_methods = method;
132
133
0
  return 0;
134
0
}
135
136
137
/**
138
 * eap_server_unregister_methods - Unregister EAP server methods
139
 *
140
 * This function is called at program termination to unregister all EAP server
141
 * methods.
142
 */
143
void eap_server_unregister_methods(void)
144
0
{
145
0
  struct eap_method *m;
146
147
0
  while (eap_methods) {
148
0
    m = eap_methods;
149
0
    eap_methods = eap_methods->next;
150
151
0
    if (m->free)
152
0
      m->free(m);
153
0
    else
154
0
      eap_server_method_free(m);
155
0
  }
156
0
}
157
158
159
/**
160
 * eap_server_get_name - Get EAP method name for the given EAP type
161
 * @vendor: EAP Vendor-Id (0 = IETF)
162
 * @type: EAP method type
163
 * Returns: EAP method name, e.g., TLS, or "unknown" if not found
164
 *
165
 * This function maps EAP type numbers into EAP type names based on the list of
166
 * EAP methods included in the build.
167
 */
168
const char * eap_server_get_name(int vendor, enum eap_type type)
169
0
{
170
0
  struct eap_method *m;
171
0
  if (vendor == EAP_VENDOR_IETF && type == EAP_TYPE_EXPANDED)
172
0
    return "expanded";
173
0
  for (m = eap_methods; m; m = m->next) {
174
0
    if (m->vendor == vendor && m->method == type)
175
0
      return m->name;
176
0
  }
177
0
  return "unknown";
178
0
}