This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cert_tools.c
244 lines (204 loc) · 6.36 KB
/
cert_tools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2023 Cisco and/or its affiliates. All rights reserved.
*
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*
* Licensed under the MIT license <LICENSE.MIT or https://opensource.org/licenses/MIT> or the GPLv2 license
* <LICENSE.GPL or https://opensource.org/license/gpl-2-0>, at your option. This file may not be copied,
* modified, or distributed except according to those terms.
*/
#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME
#include <linux/list.h>
#include <linux/slab.h>
#include "cert_tools.h"
#include "string.h"
#include "rsa_tools.h"
// Define the maximum number of elements inside the cache
#define MAX_CACHE_LENGTH 64
// certs that are in use or used once by a workload
static LIST_HEAD(cert_cache);
// lock for the above list to make it thread safe
static DEFINE_MUTEX(certificate_cache_lock);
static void cert_cache_lock(void)
{
mutex_lock(&certificate_cache_lock);
}
static void cert_cache_unlock(void)
{
mutex_unlock(&certificate_cache_lock);
}
size_t linkedlist_length(struct list_head *head)
{
struct list_head *pos;
int length = 0;
list_for_each(pos, head)
{
length++;
}
return length;
}
// add_cert_to_cache adds a certificate chain with a given trust anchor to a linked list. The key will identify this entry.
// the function is thread safe.
void add_cert_to_cache(char *key, x509_certificate *cert)
{
if (!key)
{
pr_err("invalid key");
return;
}
cert_with_key *new_entry = kzalloc(sizeof(cert_with_key), GFP_KERNEL);
if (!new_entry)
{
pr_err("memory allocation error");
return;
}
new_entry->key = strdup(key);
new_entry->cert = cert;
cert_cache_lock();
list_add(&new_entry->list, &cert_cache);
cert_cache_unlock();
}
// remove_unused_expired_certs_from_cache iterates over the whole cache and tries to clean up the unused/expired certificates.
// it works like a garbage collection which now runs before every add.
// TODO handle cases when cache length is maxed out but no expired certificate
void remove_unused_expired_certs_from_cache()
{
cert_with_key *cert_bundle, *cert_bundle_tmp;
cert_cache_lock();
if (linkedlist_length(&cert_cache) >= MAX_CACHE_LENGTH)
{
pr_debug("cache is full: remove the oldest element");
cert_with_key *last_entry = list_last_entry(&cert_cache, cert_with_key, list);
pr_debug("remove cache entry # key=[%s]", last_entry->key);
remove_cert_from_cache_locked(last_entry);
cert_cache_unlock();
return;
}
list_for_each_entry_safe_reverse(cert_bundle, cert_bundle_tmp, &cert_cache, list)
{
if (!validate_cert(cert_bundle->cert->validity))
{
remove_cert_from_cache_locked(cert_bundle);
}
}
cert_cache_unlock();
}
// find_cert_from_cache tries to find a certificate bundle for the given key. In case of failure it returns a NULL.
// this function also runs a garbage collection on the cache.
// the function is thread safe
cert_with_key *find_cert_from_cache(char *key)
{
remove_unused_expired_certs_from_cache();
cert_with_key *cert_bundle;
cert_cache_lock();
list_for_each_entry(cert_bundle, &cert_cache, list)
{
if (strcmp(cert_bundle->key, key) == 0)
{
x509_certificate_get(cert_bundle->cert);
cert_cache_unlock();
return cert_bundle;
}
}
cert_cache_unlock();
return 0;
}
// remove_cert_from_cache_locked removes a given certificate bundle from the cache
// the function is thread safe
void remove_cert_from_cache(cert_with_key *cert_bundle)
{
if (cert_bundle)
{
cert_cache_lock();
remove_cert_from_cache_locked(cert_bundle);
cert_cache_unlock();
}
}
// remove_cert_from_cache removes a given certificate bundle from the cache
void remove_cert_from_cache_locked(cert_with_key *cert_bundle)
{
if (cert_bundle)
{
list_del(&cert_bundle->list);
x509_certificate_put(cert_bundle->cert);
kfree(cert_bundle->key);
kfree(cert_bundle);
}
}
// set_cert_validity decodes the provided certificate and filling the validity seconds and days.
// if the decode fails it returns -1
int set_cert_validity(x509_certificate *x509_cert)
{
br_x509_decoder_context dc;
br_x509_decoder_init(&dc, 0, 0);
br_x509_decoder_push(&dc, x509_cert->chain->data, x509_cert->chain->data_len);
int err = br_x509_decoder_last_error(&dc);
if (err != 0)
{
pr_err("cert decode failed during setting cert validity # err[%d]", err);
return -1;
}
x509_cert->validity.notbefore_seconds = dc.notbefore_seconds;
x509_cert->validity.notbefore_days = dc.notbefore_days;
x509_cert->validity.notafter_seconds = dc.notafter_seconds;
x509_cert->validity.notafter_days = dc.notafter_days;
return 0;
}
// validate_cert validates the given certificate if it has expired or not.
bool validate_cert(x509_certificate_validity cert_validity)
{
bool result = false;
time64_t x = ktime_get_real_seconds();
uint32_t vd = (uint32_t)(x / 86400) + 719528;
uint32_t vs = (uint32_t)(x % 86400);
if (vd < cert_validity.notbefore_days || (vd == cert_validity.notbefore_days && vs < cert_validity.notbefore_seconds))
{
pr_debug("cert expired");
}
else if (vd > cert_validity.notafter_days || (vd == cert_validity.notafter_days && vs > cert_validity.notafter_seconds))
{
pr_debug("cert not valid yet");
}
else
{
result = true;
}
return result;
}
x509_certificate *x509_certificate_init(void)
{
x509_certificate *cert = kzalloc(sizeof(x509_certificate), GFP_KERNEL);
kref_init(&cert->kref);
return cert;
}
static void x509_certificate_free(x509_certificate *cert)
{
if (!cert)
{
return;
}
free_br_x509_certificate(cert->chain, cert->chain_len);
free_br_x509_trust_anchors(cert->trust_anchors, cert->trust_anchors_len);
kfree(cert);
}
static void x509_certificate_release(struct kref *kref)
{
x509_certificate *cert = container_of(kref, x509_certificate, kref);
x509_certificate_free(cert);
}
void x509_certificate_get(x509_certificate *cert)
{
if (!cert)
{
return;
}
kref_get(&cert->kref);
}
void x509_certificate_put(x509_certificate *cert)
{
if (!cert)
{
return;
}
kref_put(&cert->kref, x509_certificate_release);
}