-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcert.c
More file actions
229 lines (174 loc) · 5.38 KB
/
Copy pathcert.c
File metadata and controls
229 lines (174 loc) · 5.38 KB
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
/*
* Copyright (c) 2025 Golioth, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>
#include <stdlib.h>
#include <mbedtls/x509_crt.h>
#include <psa/crypto.h>
#include <pouch_gateway/cert.h>
#include <golioth/gateway.h>
#include <golioth/golioth_status.h>
#include <zephyr/sys/atomic_types.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(cert);
static struct golioth_client *_client;
static uint8_t server_crt_buf[CONFIG_POUCH_GATEWAY_SERVER_CERT_MAX_LEN];
static atomic_t server_crt_len;
static uint8_t server_crt_serial[CERT_SERIAL_MAXLEN];
static atomic_t server_crt_serial_len;
static atomic_t server_crt_id;
struct pouch_gateway_device_cert_context
{
size_t len;
uint8_t buf[CONFIG_POUCH_GATEWAY_DEVICE_CERT_MAX_LEN];
};
struct pouch_gateway_server_cert_context
{
atomic_val_t id;
size_t offset;
};
struct pouch_gateway_device_cert_context *pouch_gateway_device_cert_start(void)
{
struct pouch_gateway_device_cert_context *context =
malloc(sizeof(struct pouch_gateway_device_cert_context));
if (context == NULL)
{
return NULL;
}
context->len = 0;
return context;
}
int pouch_gateway_device_cert_push(struct pouch_gateway_device_cert_context *context,
const void *data,
size_t len)
{
if (context->len + len > CONFIG_POUCH_GATEWAY_DEVICE_CERT_MAX_LEN)
{
return -ENOSPC;
}
memcpy(&context->buf[context->len], data, len);
context->len += len;
return 0;
}
void pouch_gateway_device_cert_abort(struct pouch_gateway_device_cert_context *context)
{
free(context);
}
int pouch_gateway_device_cert_finish(struct pouch_gateway_device_cert_context *context)
{
enum golioth_status status;
if (IS_ENABLED(CONFIG_POUCH_GATEWAY_CLOUD))
{
status = golioth_gateway_device_cert_set(_client, context->buf, context->len, 5);
if (status != GOLIOTH_OK)
{
LOG_ERR("Failed to finish device cert: %d", status);
return -EIO;
}
}
pouch_gateway_device_cert_abort(context);
return 0;
}
struct pouch_gateway_server_cert_context *pouch_gateway_server_cert_start(void)
{
struct pouch_gateway_server_cert_context *context =
malloc(sizeof(struct pouch_gateway_server_cert_context));
if (context == NULL)
{
return NULL;
}
context->id = atomic_get(&server_crt_id);
context->offset = 0;
return context;
}
bool pouch_gateway_server_cert_is_newest(const struct pouch_gateway_server_cert_context *context)
{
return context->id == atomic_get(&server_crt_id);
}
static int server_crt_update(size_t len)
{
mbedtls_x509_crt cert_chain;
mbedtls_x509_crt_init(&cert_chain);
int ret = mbedtls_x509_crt_parse(&cert_chain, server_crt_buf, len);
if (ret < 0)
{
LOG_ERR("Failed to parse certificate: 0x%x", -ret);
mbedtls_x509_crt_free(&cert_chain);
return -EIO;
}
LOG_HEXDUMP_DBG(cert_chain.serial.p, cert_chain.serial.len, "cert_chain.serial");
memcpy(server_crt_serial, cert_chain.serial.p, cert_chain.serial.len);
atomic_set(&server_crt_serial_len, cert_chain.serial.len);
atomic_set(&server_crt_len, len);
atomic_inc(&server_crt_id);
mbedtls_x509_crt_free(&cert_chain);
return 0;
}
bool pouch_gateway_server_cert_is_complete(const struct pouch_gateway_server_cert_context *context)
{
return context->offset >= atomic_get(&server_crt_len);
}
int pouch_gateway_server_cert_get_data(struct pouch_gateway_server_cert_context *context,
void *dst,
size_t *dst_len,
bool *is_last)
{
size_t len = atomic_get(&server_crt_len);
*is_last = false;
if (context->offset >= len)
{
return -ENODATA;
}
if (*dst_len > len - context->offset)
{
*dst_len = len - context->offset;
}
memcpy(dst, &server_crt_buf[context->offset], *dst_len);
context->offset += *dst_len;
if (context->offset >= len)
{
*is_last = true;
}
return 0;
}
void pouch_gateway_server_cert_get_serial(void *dst, size_t *dst_len)
{
size_t len = atomic_get(&server_crt_serial_len);
if (*dst_len > len)
{
*dst_len = len;
}
memcpy(dst, server_crt_serial, *dst_len);
}
void pouch_gateway_server_cert_abort(struct pouch_gateway_server_cert_context *context)
{
free(context);
}
void pouch_gateway_cert_module_on_connected(struct golioth_client *client)
{
enum golioth_status status;
_client = client;
if (IS_ENABLED(CONFIG_POUCH_GATEWAY_CLOUD))
{
size_t len = sizeof(server_crt_buf);
status = golioth_gateway_server_cert_get(client, server_crt_buf, &len);
if (status != GOLIOTH_OK)
{
LOG_ERR("Failed to download server certificate: %d", status);
return;
}
server_crt_update(len);
}
else if (IS_ENABLED(CONFIG_POUCH_GATEWAY_SERVER_CERT_BUILTIN))
{
static const uint8_t server_crt_offline[] = {
#include "pouch_gateway_server.pem.inc"
};
memcpy(server_crt_buf, server_crt_offline, sizeof(server_crt_offline));
server_crt_update(sizeof(server_crt_offline));
LOG_INF("Loaded builtin server cert");
}
LOG_HEXDUMP_DBG(server_crt_buf, atomic_get(&server_crt_len), "Server certificate");
}