~khurshid-alam/unity-control-center/libnm-port-test

« back to all changes in this revision

Viewing changes to panels/network/wireless-security/eap-method.c

  • Committer: Khurshid Alam
  • Date: 2018-06-05 14:47:56 UTC
  • Revision ID: khurshid.alam@linuxmail.org-20180605144756-or0tftb2kddhvhq2
Update connection editor pages and wireless security from Gnome-3.26

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include "utils.h"
35
35
#include "helpers.h"
36
36
 
37
 
GType
38
 
eap_method_get_type (void)
39
 
{
40
 
        static GType type_id = 0;
41
 
 
42
 
        if (!type_id) {
43
 
                type_id = g_boxed_type_register_static ("CcEAPMethod",
44
 
                                                        (GBoxedCopyFunc) eap_method_ref,
45
 
                                                        (GBoxedFreeFunc) eap_method_unref);
46
 
        }
47
 
 
48
 
        return type_id;
49
 
}
 
37
G_DEFINE_BOXED_TYPE (EAPMethod, eap_method, eap_method_ref, eap_method_unref)
50
38
 
51
39
GtkWidget *
52
40
eap_method_get_widget (EAPMethod *method)
66
54
        g_assert (method->validate);
67
55
        result = (*(method->validate)) (method, error);
68
56
        if (!result && error && !*error)
69
 
                g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("undefined error in 802.1x security (wpa-eap)"));
 
57
                g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("undefined error in 802.1X security (wpa-eap)"));
70
58
        return result;
71
59
}
72
60
 
167
155
 
168
156
        method->builder = gtk_builder_new ();
169
157
        if (!gtk_builder_add_from_resource (method->builder, ui_resource, &error)) {
170
 
                g_warning ("Couldn't load UI builder file %s: %s",
 
158
                g_warning ("Couldn't load UI builder resource %s: %s",
171
159
                           ui_resource, error->message);
172
160
                eap_method_unref (method);
173
161
                return NULL;
283
271
}
284
272
 
285
273
static gboolean
 
274
file_has_extension (const char *filename, const char *extensions[])
 
275
{
 
276
        char *p, *ext;
 
277
        int i = 0;
 
278
        gboolean found = FALSE;
 
279
 
 
280
        p = strrchr (filename, '.');
 
281
        if (!p)
 
282
                return FALSE;
 
283
 
 
284
        ext = g_ascii_strdown (p, -1);
 
285
        if (ext) {
 
286
                while (extensions[i]) {
 
287
                        if (!strcmp (ext, extensions[i++])) {
 
288
                                found = TRUE;
 
289
                                break;
 
290
                        }
 
291
                }
 
292
        }
 
293
        g_free (ext);
 
294
 
 
295
        return found;
 
296
}
 
297
 
 
298
#if !LIBNM_BUILD
 
299
static const char *
 
300
find_tag (const char *tag, const char *buf, gsize len)
 
301
{
 
302
        gsize i, taglen;
 
303
 
 
304
        taglen = strlen (tag);
 
305
        if (len < taglen)
 
306
                return NULL;
 
307
 
 
308
        for (i = 0; i < len - taglen + 1; i++) {
 
309
                if (memcmp (buf + i, tag, taglen) == 0)
 
310
                        return buf + i;
 
311
        }
 
312
        return NULL;
 
313
}
 
314
 
 
315
static const char *pem_rsa_key_begin = "-----BEGIN RSA PRIVATE KEY-----";
 
316
static const char *pem_dsa_key_begin = "-----BEGIN DSA PRIVATE KEY-----";
 
317
static const char *pem_pkcs8_enc_key_begin = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
 
318
static const char *pem_pkcs8_dec_key_begin = "-----BEGIN PRIVATE KEY-----";
 
319
static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
 
320
static const char *proc_type_tag = "Proc-Type: 4,ENCRYPTED";
 
321
static const char *dek_info_tag = "DEK-Info:";
 
322
 
 
323
static gboolean
 
324
pem_file_is_encrypted (const char *buffer, gsize bytes_read)
 
325
{
 
326
        /* Check if the private key is encrypted or not by looking for the
 
327
         * old OpenSSL-style proc-type and dec-info tags.
 
328
         */
 
329
        if (find_tag (proc_type_tag, (const char *) buffer, bytes_read)) {
 
330
                if (find_tag (dek_info_tag, (const char *) buffer, bytes_read))
 
331
                        return TRUE;
 
332
        }
 
333
        return FALSE;
 
334
}
 
335
 
 
336
static gboolean
 
337
file_is_der_or_pem (const char *filename,
 
338
                    gboolean privkey,
 
339
                    gboolean *out_privkey_encrypted)
 
340
{
 
341
        int fd;
 
342
        unsigned char buffer[8192];
 
343
        ssize_t bytes_read;
 
344
        gboolean success = FALSE;
 
345
 
 
346
        fd = open (filename, O_RDONLY);
 
347
        if (fd < 0)
 
348
                return FALSE;
 
349
 
 
350
        bytes_read = read (fd, buffer, sizeof (buffer) - 1);
 
351
        if (bytes_read < 400)  /* needs to be lower? */
 
352
                goto out;
 
353
        buffer[bytes_read] = '\0';
 
354
 
 
355
        /* Check for DER signature */
 
356
        if (bytes_read > 2 && buffer[0] == 0x30 && buffer[1] == 0x82) {
 
357
                success = TRUE;
 
358
                goto out;
 
359
        }
 
360
 
 
361
        /* Check for PEM signatures */
 
362
        if (privkey) {
 
363
                if (find_tag (pem_rsa_key_begin, (const char *) buffer, bytes_read)) {
 
364
                        success = TRUE;
 
365
                        if (out_privkey_encrypted)
 
366
                                *out_privkey_encrypted = pem_file_is_encrypted ((const char *) buffer, bytes_read);
 
367
                        goto out;
 
368
                }
 
369
 
 
370
                if (find_tag (pem_dsa_key_begin, (const char *) buffer, bytes_read)) {
 
371
                        success = TRUE;
 
372
                        if (out_privkey_encrypted)
 
373
                                *out_privkey_encrypted = pem_file_is_encrypted ((const char *) buffer, bytes_read);
 
374
                        goto out;
 
375
                }
 
376
 
 
377
                if (find_tag (pem_pkcs8_enc_key_begin, (const char *) buffer, bytes_read)) {
 
378
                        success = TRUE;
 
379
                        if (out_privkey_encrypted)
 
380
                                *out_privkey_encrypted = TRUE;
 
381
                        goto out;
 
382
                }
 
383
 
 
384
                if (find_tag (pem_pkcs8_dec_key_begin, (const char *) buffer, bytes_read)) {
 
385
                        success = TRUE;
 
386
                        if (out_privkey_encrypted)
 
387
                                *out_privkey_encrypted = FALSE;
 
388
                        goto out;
 
389
                }
 
390
        } else {
 
391
                if (find_tag (pem_cert_begin, (const char *) buffer, bytes_read)) {
 
392
                        success = TRUE;
 
393
                        goto out;
 
394
                }
 
395
        }
 
396
 
 
397
out:
 
398
        close (fd);
 
399
        return success;
 
400
}
 
401
#endif
 
402
 
 
403
static gboolean
286
404
default_filter_privkey (const GtkFileFilterInfo *filter_info, gpointer user_data)
287
405
{
288
 
        gboolean require_encrypted = !!user_data;
289
 
        gboolean is_encrypted;
 
406
        const char *extensions[] = { ".der", ".pem", ".p12", ".key", NULL };
290
407
 
291
408
        if (!filter_info->filename)
292
409
                return FALSE;
293
410
 
294
 
        is_encrypted = FALSE;
295
 
        if (!nm_utils_file_is_private_key (filter_info->filename, &is_encrypted))
 
411
        if (!file_has_extension (filter_info->filename, extensions))
296
412
                return FALSE;
297
413
 
298
 
        return require_encrypted ? is_encrypted : TRUE;
 
414
        return TRUE;
299
415
}
300
416
 
301
417
static gboolean
302
418
default_filter_cert (const GtkFileFilterInfo *filter_info, gpointer user_data)
303
419
{
 
420
        const char *extensions[] = { ".der", ".pem", ".crt", ".cer", NULL };
 
421
 
304
422
        if (!filter_info->filename)
305
423
                return FALSE;
306
424
 
307
 
        if (!nm_utils_file_is_certificate (filter_info->filename))
 
425
        if (!file_has_extension (filter_info->filename, extensions))
308
426
                return FALSE;
309
427
 
310
428
        return TRUE;
330
448
eap_method_is_encrypted_private_key (const char *path)
331
449
{
332
450
        GtkFileFilterInfo info = { .filename = path };
333
 
 
334
 
        return default_filter_privkey (&info, (gpointer) TRUE);
 
451
        gboolean is_encrypted;
 
452
 
 
453
        if (!default_filter_privkey (&info, NULL))
 
454
                return FALSE;
 
455
 
 
456
#if LIBNM_BUILD
 
457
        is_encrypted = FALSE;
 
458
        if (!nm_utils_file_is_private_key (path, &is_encrypted))
 
459
                return FALSE;
 
460
#else
 
461
        is_encrypted = TRUE;
 
462
        if (   !file_is_der_or_pem (path, TRUE, &is_encrypted)
 
463
            && !nm_utils_file_is_pkcs12 (path))
 
464
                return FALSE;
 
465
#endif
 
466
        return is_encrypted;
335
467
}
336
468
 
337
469
/* Some methods (PEAP, TLS, TTLS) require a CA certificate. The user can choose