KEYS: Separate the kernel signature checking keyring from module signing
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / system_keyring.c
1 /* System trusted keyring for trusted public keys
2  *
3  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/cred.h>
16 #include <linux/err.h>
17 #include <keys/asymmetric-type.h>
18 #include <keys/system_keyring.h>
19 #include "module-internal.h"
20
21 struct key *system_trusted_keyring;
22 EXPORT_SYMBOL_GPL(system_trusted_keyring);
23
24 extern __initconst const u8 system_certificate_list[];
25 extern __initconst const u8 system_certificate_list_end[];
26
27 /*
28  * Load the compiled-in keys
29  */
30 static __init int system_trusted_keyring_init(void)
31 {
32         pr_notice("Initialise system trusted keyring\n");
33
34         system_trusted_keyring =
35                 keyring_alloc(".system_keyring",
36                               KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
37                               ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
38                                KEY_USR_VIEW | KEY_USR_READ),
39                               KEY_ALLOC_NOT_IN_QUOTA, NULL);
40         if (IS_ERR(system_trusted_keyring))
41                 panic("Can't allocate system trusted keyring\n");
42
43         return 0;
44 }
45
46 /*
47  * Must be initialised before we try and load the keys into the keyring.
48  */
49 device_initcall(system_trusted_keyring_init);
50
51 /*
52  * Load the compiled-in list of X.509 certificates.
53  */
54 static __init int load_system_certificate_list(void)
55 {
56         key_ref_t key;
57         const u8 *p, *end;
58         size_t plen;
59
60         pr_notice("Loading compiled-in X.509 certificates\n");
61
62         end = system_certificate_list_end;
63         p = system_certificate_list;
64         while (p < end) {
65                 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
66                  * than 256 bytes in size.
67                  */
68                 if (end - p < 4)
69                         goto dodgy_cert;
70                 if (p[0] != 0x30 &&
71                     p[1] != 0x82)
72                         goto dodgy_cert;
73                 plen = (p[2] << 8) | p[3];
74                 plen += 4;
75                 if (plen > end - p)
76                         goto dodgy_cert;
77
78                 key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
79                                            "asymmetric",
80                                            NULL,
81                                            p,
82                                            plen,
83                                            (KEY_POS_ALL & ~KEY_POS_SETATTR) |
84                                            KEY_USR_VIEW,
85                                            KEY_ALLOC_NOT_IN_QUOTA);
86                 if (IS_ERR(key)) {
87                         pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
88                                PTR_ERR(key));
89                 } else {
90                         pr_notice("Loaded X.509 cert '%s'\n",
91                                   key_ref_to_ptr(key)->description);
92                         key_ref_put(key);
93                 }
94                 p += plen;
95         }
96
97         return 0;
98
99 dodgy_cert:
100         pr_err("Problem parsing in-kernel X.509 certificate list\n");
101         return 0;
102 }
103 late_initcall(load_system_certificate_list);