Commit | Line | Data |
---|---|---|
b56e5a17 DH |
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> | |
b56e5a17 DH |
19 | |
20 | struct key *system_trusted_keyring; | |
21 | EXPORT_SYMBOL_GPL(system_trusted_keyring); | |
22 | ||
23 | extern __initconst const u8 system_certificate_list[]; | |
62226983 | 24 | extern __initconst const unsigned long system_certificate_list_size; |
b56e5a17 DH |
25 | |
26 | /* | |
27 | * Load the compiled-in keys | |
28 | */ | |
29 | static __init int system_trusted_keyring_init(void) | |
30 | { | |
31 | pr_notice("Initialise system trusted keyring\n"); | |
32 | ||
33 | system_trusted_keyring = | |
34 | keyring_alloc(".system_keyring", | |
35 | KUIDT_INIT(0), KGIDT_INIT(0), current_cred(), | |
36 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | | |
af34cb0c | 37 | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH), |
b56e5a17 DH |
38 | KEY_ALLOC_NOT_IN_QUOTA, NULL); |
39 | if (IS_ERR(system_trusted_keyring)) | |
40 | panic("Can't allocate system trusted keyring\n"); | |
41 | ||
008643b8 | 42 | set_bit(KEY_FLAG_TRUSTED_ONLY, &system_trusted_keyring->flags); |
b56e5a17 DH |
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 | ||
b56e5a17 | 62 | p = system_certificate_list; |
62226983 | 63 | end = p + system_certificate_list_size; |
b56e5a17 DH |
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, | |
af34cb0c MZ |
83 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | |
84 | KEY_USR_VIEW | KEY_USR_READ), | |
008643b8 DH |
85 | KEY_ALLOC_NOT_IN_QUOTA | |
86 | KEY_ALLOC_TRUSTED); | |
b56e5a17 DH |
87 | if (IS_ERR(key)) { |
88 | pr_err("Problem loading in-kernel X.509 certificate (%ld)\n", | |
89 | PTR_ERR(key)); | |
90 | } else { | |
32c4741c | 91 | set_bit(KEY_FLAG_BUILTIN, &key_ref_to_ptr(key)->flags); |
b56e5a17 DH |
92 | pr_notice("Loaded X.509 cert '%s'\n", |
93 | key_ref_to_ptr(key)->description); | |
94 | key_ref_put(key); | |
95 | } | |
96 | p += plen; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | ||
101 | dodgy_cert: | |
102 | pr_err("Problem parsing in-kernel X.509 certificate list\n"); | |
103 | return 0; | |
104 | } | |
105 | late_initcall(load_system_certificate_list); |