Passed
Push — release_2_0 ( db561b...cc9048 )
by Tomasz
10:42
created

EAP::isPasswordRequired()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 9
nop 0
dl 0
loc 14
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This file contains the EAP class and some constants for EAP types.
24
 *
25
 * @author Stefan Winter <[email protected]>
26
 * @author Tomasz Wolniewicz <[email protected]>
27
 *
28
 * @package Developer
29
 * 
30
 */
31
32
namespace core\common;
33
34
use \Exception;
35
36
/**
37
 * Convenience functions for EAP types
38
 *
39
 * @author Stefan Winter <[email protected]>
40
 * @author Tomasz Wolniewicz <[email protected]>
41
 *
42
 * @license see LICENSE file in root directory
43
 *
44
 * @package Developer
45
 */
46
class EAP extends Entity {
47
48
    /**
49
     * some EAP-related constants.
50
     */
51
    const PEAP = 25;
52
    const MSCHAP2 = 26;
53
    const TTLS = 21;
54
    const TLS = 13;
55
    const NONE = 0;
56
    const GTC = 6;
57
    const FAST = 43;
58
    const PWD = 52;
59
    const NE_PAP = 1;
60
    const NE_MSCHAP = 2;
61
    const NE_MSCHAP2 = 3;
62
    const NE_SILVERBULLET = 999;
63
    const INTEGER_TTLS_PAP = 1;
64
    const INTEGER_PEAP_MSCHAP2 = 2;
65
    const INTEGER_TLS = 3;
66
    const INTEGER_FAST_GTC = 4;
67
    const INTEGER_TTLS_GTC = 5;
68
    const INTEGER_TTLS_MSCHAP2 = 6;
69
    const INTEGER_EAP_PWD = 7;
70
    const INTEGER_SILVERBULLET = 8;
71
72
// PHP7 allows to define constants with arrays as value. Hooray! This makes
73
// lots of public static members of the EAP class obsolete
74
75
    /**
76
     * PEAP-MSCHAPv2: Outer EAP Type = 25, Inner EAP Type = 26
77
     */
78
    const EAPTYPE_PEAP_MSCHAP2 = ["OUTER" => EAP::PEAP, "INNER" => EAP::MSCHAP2];
79
80
    /**
81
     * EAP-TLS: Outer EAP Type = 13, no inner EAP
82
     */
83
    const EAPTYPE_TLS = ["OUTER" => EAP::TLS, "INNER" => EAP::NONE];
84
85
    /**
86
     * EAP-TLS: Outer EAP Type = 13, no inner EAP
87
     */
88
    const EAPTYPE_SILVERBULLET = ["OUTER" => EAP::TLS, "INNER" => EAP::NE_SILVERBULLET];
89
90
    /**
91
     * TTLS-PAP: Outer EAP type = 21, no inner EAP, inner non-EAP = 1
92
     */
93
    const EAPTYPE_TTLS_PAP = ["OUTER" => EAP::TTLS, "INNER" => EAP::NONE];
94
95
    /**
96
     * TTLS-MSCHAP-v2: Outer EAP type = 21, no inner EAP, inner non-EAP = 3
97
     */
98
    const EAPTYPE_TTLS_MSCHAP2 = ["OUTER" => EAP::TTLS, "INNER" => EAP::MSCHAP2];
99
100
    /**
101
     * TTLS-GTC: Outer EAP type = 21, Inner EAP Type = 6
102
     */
103
    const EAPTYPE_TTLS_GTC = ["OUTER" => EAP::TTLS, "INNER" => EAP::GTC];
104
105
    /**
106
     * EAP-FAST (GTC): Outer EAP type = 43, Inner EAP Type = 6
107
     */
108
    const EAPTYPE_FAST_GTC = ["OUTER" => EAP::FAST, "INNER" => EAP::GTC];
109
110
    /**
111
     * PWD: Outer EAP type = 52, no inner EAP
112
     */
113
    const EAPTYPE_PWD = ["OUTER" => EAP::PWD, "INNER" => EAP::NONE];
114
115
    /**
116
     * NULL: no outer EAP, no inner EAP
117
     */
118
    const EAPTYPE_NONE = ["OUTER" => EAP::NONE, "INNER" => EAP::NONE];
119
120
    /**
121
     *  ANY: not really an EAP method, but the term to use when needing to express "any EAP method we know"
122
     */
123
    const EAPTYPE_ANY = ["OUTER" => 255, "INNER" => 255];
124
125
    /**
126
     * conversion table between array and integer representations
127
     */
128
    const EAPTYPES_CONVERSION = [
129
        EAP::INTEGER_FAST_GTC => EAP::EAPTYPE_FAST_GTC,
130
        EAP::INTEGER_PEAP_MSCHAP2 => EAP::EAPTYPE_PEAP_MSCHAP2,
131
        EAP::INTEGER_EAP_PWD => EAP::EAPTYPE_PWD,
132
        EAP::INTEGER_TLS => EAP::EAPTYPE_TLS,
133
        EAP::INTEGER_TTLS_GTC => EAP::EAPTYPE_TTLS_GTC,
134
        EAP::INTEGER_TTLS_MSCHAP2 => EAP::EAPTYPE_TTLS_MSCHAP2,
135
        EAP::INTEGER_TTLS_PAP => EAP::EAPTYPE_TTLS_PAP,
136
        EAP::INTEGER_SILVERBULLET => EAP::EAPTYPE_SILVERBULLET,
137
    ];
138
139
    /**
140
     * The array representation of the EAP type
141
     * @var array
142
     */
143
    private $arrayRep;
144
145
    /**
146
     * The integer representation of the EAP type
147
     * @var integer
148
     */
149
    private $intRep;
150
151
    /**
152
     * Instantiates the EAP class for a concrete EAP type. Only call it to 
153
     * instantiate *real* EAP types, i.e. not EAPTYPE::ANY or EAPTYPE::NONE
154
     * 
155
     * @param mixed $eapType the EAP type, either in its integer or array representation
156
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
157
    public function __construct($eapType) {
158
        if (is_numeric($eapType) && array_key_exists($eapType, EAP::EAPTYPES_CONVERSION)) {
159
            $key = array_keys(EAP::EAPTYPES_CONVERSION, EAP::EAPTYPES_CONVERSION[$eapType]);
160
            $this->intRep = $key[0];
161
            $this->arrayRep = EAP::EAPTYPES_CONVERSION[$this->intRep];
162
            return;
163
        }
164
        if (is_array($eapType)) {
165
            $key = array_search($eapType, EAP::EAPTYPES_CONVERSION);
166
            if ($key !== FALSE) {
167
                // add a type cast to int to make Scrutinizer realise that the key found is always an integer
168
                $this->intRep = (int)$key; // array index is always an integer
169
                $this->arrayRep = EAP::EAPTYPES_CONVERSION[(int)$key];
170
                return;
171
            }
172
        }
173
        throw new Exception("Unable to instantiate the EAP class - the EAP type is bogus.");
174
    }
175
176
    /**
177
     * Is this a password-based EAP method?
178
     * @return boolean
179
     * @throws Exception
180
     */
181
    public function isPasswordRequired() {
182
        switch ($this->intRep) {
183
            case EAP::INTEGER_EAP_PWD:
184
            case EAP::INTEGER_FAST_GTC:
185
            case EAP::INTEGER_PEAP_MSCHAP2:
186
            case EAP::INTEGER_TTLS_GTC:
187
            case EAP::INTEGER_TTLS_MSCHAP2:
188
            case EAP::INTEGER_TTLS_PAP:
189
                return TRUE;
190
            case EAP::INTEGER_TLS:
191
            case EAP::INTEGER_SILVERBULLET:
192
                return FALSE;
193
            default:
194
                throw new Exception("Unable to determine if the EAP type required a password or not!");
195
        }
196
    }
197
198
    /**
199
     * There could be EAP methods which have an optional need for a password.
200
     * Not aware of any, so this is a simple function :-)
201
     * @return boolean
202
     */
203
    public function isPasswordOptional() {
204
        return FALSE;
205
    }
206
207
    /**
208
     * Is this a certificate-based EAP method?
209
     * @return boolean
210
     * @throws Exception
211
     */
212
    public function isClientCertRequired() {
213
        switch ($this->intRep) {
214
            case EAP::INTEGER_EAP_PWD:
215
            case EAP::INTEGER_FAST_GTC:
216
            case EAP::INTEGER_PEAP_MSCHAP2:
217
            case EAP::INTEGER_TTLS_GTC:
218
            case EAP::INTEGER_TTLS_MSCHAP2:
219
            case EAP::INTEGER_TTLS_PAP:
220
                return FALSE;
221
            case EAP::INTEGER_TLS:
222
            case EAP::INTEGER_SILVERBULLET:
223
                return TRUE;
224
            default:
225
                throw new Exception("Unable to determine if the EAP type requires client-certificates or not!");
226
        }
227
    }
228
229
    /**
230
     * Does an EAP type optionally allow to send a client certificate?
231
     * @return boolean
232
     * @throws Exception
233
     */
234
    public function isClientCertOptional() {
235
        switch ($this->intRep) {
236
            case EAP::INTEGER_EAP_PWD:
237
            case EAP::INTEGER_TLS:
238
            case EAP::INTEGER_SILVERBULLET:
239
                return FALSE;
240
            case EAP::INTEGER_FAST_GTC:
241
            case EAP::INTEGER_PEAP_MSCHAP2:
242
            case EAP::INTEGER_TTLS_GTC:
243
            case EAP::INTEGER_TTLS_MSCHAP2:
244
            case EAP::INTEGER_TTLS_PAP:
245
                return TRUE;
246
            default:
247
                throw new Exception("Unable to determine if the EAP type has optional client-certificates or not!");
248
        }
249
    }
250
251
    /**
252
     * Does the EAP type require the specification of trusted CAs to be secure?
253
     * @return boolean
254
     * @throws Exception
255
     */
256
    public function needsServerCACert() {
257
        switch ($this->intRep) {
258
            case EAP::INTEGER_EAP_PWD:
259
                return FALSE;
260
            case EAP::INTEGER_FAST_GTC:
261
            case EAP::INTEGER_PEAP_MSCHAP2:
262
            case EAP::INTEGER_TTLS_GTC:
263
            case EAP::INTEGER_TTLS_MSCHAP2:
264
            case EAP::INTEGER_TTLS_PAP:
265
            case EAP::INTEGER_TLS:
266
            case EAP::INTEGER_SILVERBULLET:
267
                return TRUE;
268
            default:
269
                throw new Exception("Unable to determine if the EAP type requires a CA trust base for secure functioning or not!");
270
        }
271
    }
272
273
    /**
274
     * Does the EAP type require the specification of a server name to be secure?
275
     * EAP-pwd has one, but it is not really required.
276
     * @return boolean
277
     * @throws Exception
278
     */
279
    public function needsServerName() {
280
        switch ($this->intRep) {
281
            case EAP::INTEGER_FAST_GTC:
282
            case EAP::INTEGER_PEAP_MSCHAP2:
283
            case EAP::INTEGER_TTLS_GTC:
284
            case EAP::INTEGER_TTLS_MSCHAP2:
285
            case EAP::INTEGER_TTLS_PAP:
286
            case EAP::INTEGER_TLS:
287
            case EAP::INTEGER_SILVERBULLET:
288
                return TRUE;
289
            case EAP::INTEGER_EAP_PWD:
290
                return FALSE;
291
            default:
292
                throw new Exception("Unable to determine if the EAP type requires a server name trust base for secure functioning or not!");
293
        }
294
    }
295
296
    /**
297
     * Returns the Array representation of the EAP type.
298
     * 
299
     * @return array
300
     */
301
    public function getArrayRep() {
302
        return $this->arrayRep;
303
    }
304
305
    /**
306
     * Returns the int representation of the EAP type.
307
     * 
308
     * @return int
309
     */
310
    public function getIntegerRep() {
311
        return $this->intRep;
312
    }
313
314
    /**
315
     * This function takes the EAP method in array representation (OUTER/INNER) and returns it in a custom format for the
316
     * Linux installers (not numbers, but strings as values).
317
     * @param array $eap EAP method in array representation (OUTER/INNER)
318
     * @return array EAP method in array representation (OUTER as string/INNER as string)
319
     */
320
    public static function eapDisplayName($eap) {
321
        $eapDisplayName = [];
322
        $eapDisplayName[serialize(EAP::EAPTYPE_PEAP_MSCHAP2)] = ["OUTER" => 'PEAP', "INNER" => 'MSCHAPV2'];
323
        $eapDisplayName[serialize(EAP::EAPTYPE_TLS)] = ["OUTER" => 'TLS', "INNER" => ''];
324
        $eapDisplayName[serialize(EAP::EAPTYPE_TTLS_PAP)] = ["OUTER" => 'TTLS', "INNER" => 'PAP'];
325
        $eapDisplayName[serialize(EAP::EAPTYPE_TTLS_MSCHAP2)] = ["OUTER" => 'TTLS', "INNER" => 'MSCHAPV2'];
326
        $eapDisplayName[serialize(EAP::EAPTYPE_TTLS_GTC)] = ["OUTER" => 'TTLS', "INNER" => 'GTC'];
327
        $eapDisplayName[serialize(EAP::EAPTYPE_FAST_GTC)] = ["OUTER" => 'FAST', "INNER" => 'GTC'];
328
        $eapDisplayName[serialize(EAP::EAPTYPE_PWD)] = ["OUTER" => 'PWD', "INNER" => ''];
329
        $eapDisplayName[serialize(EAP::EAPTYPE_NONE)] = ["OUTER" => '', "INNER" => ''];
330
        $eapDisplayName[serialize(EAP::EAPTYPE_SILVERBULLET)] = ["OUTER" => 'TLS', "INNER" => 'SILVERBULLET'];
331
        $eapDisplayName[serialize(EAP::EAPTYPE_ANY)] = ["OUTER" => 'PEAP TTLS TLS', "INNER" => 'MSCHAPV2 PAP GTC'];
332
        return($eapDisplayName[serialize($eap)]);
333
    }
334
335
336
    /**
337
     * This function enumerates all known EAP types and returns them as array
338
     * 
339
     * @return array of all EAP types the CAT knows about, as objects
340
     */
341
    public static function listKnownEAPTypes() {
342
        $retval = [];
343
344
        foreach (array_values(EAP::EAPTYPES_CONVERSION) as $oneArrayRep) {
345
            $retval[] = new EAP($oneArrayRep);
346
        }
347
        return $retval;
348
    }
349
350
    /**
351
     * returns a printable ("pretty-print") version of the EAP type
352
     * @return string
353
     */
354
    public function getPrintableRep() {
355
        Entity::intoThePotatoes();
356
        $nameMapping = [
357
            _("PEAP-MSCHAPv2") => \core\common\EAP::EAPTYPE_PEAP_MSCHAP2,
358
            _("TLS") => \core\common\EAP::EAPTYPE_TLS,
359
            _("TTLS-PAP") => \core\common\EAP::EAPTYPE_TTLS_PAP,
360
            _("TTLS-MSCHAPv2") => \core\common\EAP::EAPTYPE_TTLS_MSCHAP2,
361
            _("TTLS-GTC") => \core\common\EAP::EAPTYPE_TTLS_GTC,
362
            _("FAST-GTC") => \core\common\EAP::EAPTYPE_FAST_GTC,
363
            _("EAP-pwd") => \core\common\EAP::EAPTYPE_PWD,
364
            \core\ProfileSilverbullet::PRODUCTNAME => \core\common\EAP::EAPTYPE_SILVERBULLET,
365
        ];
366
        $find = array_keys($nameMapping, $this->arrayRep, TRUE);
367
        Entity::outOfThePotatoes();
368
        return $find[0];
369
    }
370
371
}
372