1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) "PKCS7: "fmt
13#include <linux/kernel.h>
14#include <linux/export.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include <linux/asn1.h>
18#include <linux/key.h>
19#include <keys/asymmetric-type.h>
20#include <crypto/public_key.h>
21#include "pkcs7_parser.h"
22
23
24
25
26static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
27 struct pkcs7_signed_info *sinfo,
28 struct key *trust_keyring)
29{
30 struct public_key_signature *sig = sinfo->sig;
31 struct x509_certificate *x509, *last = NULL, *p;
32 struct key *key;
33 int ret;
34
35 kenter(",%u,", sinfo->index);
36
37 if (sinfo->unsupported_crypto) {
38 kleave(" = -ENOPKG [cached]");
39 return -ENOPKG;
40 }
41
42 for (x509 = sinfo->signer; x509; x509 = x509->signer) {
43 if (x509->seen) {
44 if (x509->verified)
45 goto verified;
46 kleave(" = -ENOKEY [cached]");
47 return -ENOKEY;
48 }
49 x509->seen = true;
50
51
52
53
54 key = find_asymmetric_key(trust_keyring,
55 x509->id, x509->skid, false);
56 if (!IS_ERR(key)) {
57
58
59
60
61
62 pr_devel("sinfo %u: Cert %u as key %x\n",
63 sinfo->index, x509->index, key_serial(key));
64 goto matched;
65 }
66 if (key == ERR_PTR(-ENOMEM))
67 return -ENOMEM;
68
69
70
71
72 if (x509->next == x509) {
73 kleave(" = -ENOKEY [unknown self-signed]");
74 return -ENOKEY;
75 }
76
77 might_sleep();
78 last = x509;
79 sig = last->sig;
80 }
81
82
83
84
85 if (last && (last->sig->auth_ids[0] || last->sig->auth_ids[1])) {
86 key = find_asymmetric_key(trust_keyring,
87 last->sig->auth_ids[0],
88 last->sig->auth_ids[1],
89 false);
90 if (!IS_ERR(key)) {
91 x509 = last;
92 pr_devel("sinfo %u: Root cert %u signer is key %x\n",
93 sinfo->index, x509->index, key_serial(key));
94 goto matched;
95 }
96 if (PTR_ERR(key) != -ENOKEY)
97 return PTR_ERR(key);
98 }
99
100
101
102
103 key = find_asymmetric_key(trust_keyring,
104 sinfo->sig->auth_ids[0], NULL, false);
105 if (!IS_ERR(key)) {
106 pr_devel("sinfo %u: Direct signer is key %x\n",
107 sinfo->index, key_serial(key));
108 x509 = NULL;
109 goto matched;
110 }
111 if (PTR_ERR(key) != -ENOKEY)
112 return PTR_ERR(key);
113
114 kleave(" = -ENOKEY [no backref]");
115 return -ENOKEY;
116
117matched:
118 ret = verify_signature(key, sig);
119 key_put(key);
120 if (ret < 0) {
121 if (ret == -ENOMEM)
122 return ret;
123 kleave(" = -EKEYREJECTED [verify %d]", ret);
124 return -EKEYREJECTED;
125 }
126
127verified:
128 if (x509) {
129 x509->verified = true;
130 for (p = sinfo->signer; p != x509; p = p->signer)
131 p->verified = true;
132 }
133 kleave(" = 0");
134 return 0;
135}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
162 struct key *trust_keyring)
163{
164 struct pkcs7_signed_info *sinfo;
165 struct x509_certificate *p;
166 int cached_ret = -ENOKEY;
167 int ret;
168
169 for (p = pkcs7->certs; p; p = p->next)
170 p->seen = false;
171
172 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
173 ret = pkcs7_validate_trust_one(pkcs7, sinfo, trust_keyring);
174 switch (ret) {
175 case -ENOKEY:
176 continue;
177 case -ENOPKG:
178 if (cached_ret == -ENOKEY)
179 cached_ret = -ENOPKG;
180 continue;
181 case 0:
182 cached_ret = 0;
183 continue;
184 default:
185 return ret;
186 }
187 }
188
189 return cached_ret;
190}
191EXPORT_SYMBOL_GPL(pkcs7_validate_trust);
192