-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_certs.c
211 lines (183 loc) · 5.98 KB
/
read_certs.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "read_certs.h"
#include "translation.h"
struct append_dn_status {
uint8_t *dn;
size_t n, size;
};
void bearssl_read_certs_help(FILE *stream)
{
fprintf(stream,
" %s\n",
_("CA_CERT_SSL_FILE: certificates file, default is /etc/ssl/certs.pem")
);
}
void bearssl_free_certs(struct trust_anchors tas)
{
for (size_t i = 0; i < tas.n; i++) {
br_x509_trust_anchor ta = tas.ta[i];
free(ta.dn.data);
if (ta.pkey.key_type == BR_KEYTYPE_RSA) {
free(ta.pkey.key.rsa.n);
free(ta.pkey.key.rsa.e);
} else if (ta.pkey.key_type == BR_KEYTYPE_EC) {
free(ta.pkey.key.ec.q);
}
}
free(tas.ta);
}
static int append_ta(struct trust_anchors *ct, br_x509_trust_anchor ta)
{
if (ct->n + 1 > ct->size) {
size_t tmp = ct->size * 2;
if (tmp == 0) tmp = 64;
ct->ta = realloc(ct->ta, tmp * sizeof(ta));
if (ct->ta == NULL) {
perror("realloc()");
return -1;
}
ct->size = tmp;
}
ct->ta[ct->n++] = ta;
return 0;
}
static void append_dn(void *ctx, const void *buf, size_t len)
{
struct append_dn_status *s = ctx;
if (s->n + len > s->size) {
size_t tmp = s->size;
if (tmp == 0) tmp = 256;
while (s->n + len > tmp) {
tmp *= 2;
}
s->dn = realloc(s->dn, tmp);
if (s->dn == NULL) {
perror("realloc()");
return;
}
s->size = tmp;
}
memcpy(s->dn + s->n, buf, len);
s->n += len;
}
static void push_x509(void *dest_ctx, const void *src, size_t len)
{
br_x509_decoder_context *dest = dest_ctx;
br_x509_decoder_push(dest, src, len);
}
/*
* Reads certs from file if set, otherwise from default location.
*/
size_t bearssl_read_certs(struct trust_anchors *tas, FILE *file)
{
size_t rv = 0;
if (file == NULL) {
const char *cert_path = getenv("CA_CERT_SSL_FILE");
if (cert_path == NULL) {
cert_path = "/etc/ssl/certs.pem";
}
file = fopen(cert_path, "re");
if (file == NULL) {
perror("fopen()");
return rv;
}
}
br_pem_decoder_context pem;
br_x509_decoder_context x509;
br_pem_decoder_init(&pem);
br_pem_decoder_setdest(&pem, push_x509, &x509);
struct append_dn_status dn_status;
uint8_t data[512];
uint8_t *datap;
size_t len = 0;
while (1) {
if (len == 0) {
len = fread(data, 1, 512, file);
if (len == 0) {
break;
}
datap = data;
}
size_t pushed = br_pem_decoder_push(&pem, datap, len);
len -= pushed;
datap += pushed;
switch(br_pem_decoder_event(&pem)) {
const char *name;
int err;
case 0:
break;
case BR_PEM_BEGIN_OBJ:
name = br_pem_decoder_name(&pem);
if (strcmp(name, "CERTIFICATE") == 0) {
memset(&dn_status, 0, sizeof dn_status);
br_x509_decoder_init(&x509, append_dn, &dn_status);
}
break;
case BR_PEM_ERROR:
fputs("br_pem_error!\n", stderr);
break;
case BR_PEM_END_OBJ:
err = br_x509_decoder_last_error(&x509);
if (err) {
fprintf(stderr, _("X509 err code: %d\n"), err);
} else {
// decoded succesfully, now to get the data
br_x509_trust_anchor ta =
{.flags = br_x509_decoder_isCA(&x509) ? BR_X509_TA_CA : 0};
// DN
uint8_t *old = dn_status.dn;
// shorten the DN, if possible
dn_status.dn = realloc(dn_status.dn, dn_status.n);
if (dn_status.dn == NULL) {
perror("realloc()");
dn_status.dn = old;
}
br_x500_name dn = {.data = dn_status.dn, .len = dn_status.n};
// copy into final struct
ta.dn = dn;
br_x509_pkey *pkey = br_x509_decoder_get_pkey(&x509);
br_x509_pkey new_key = { 0 };
if (pkey->key_type == BR_KEYTYPE_RSA) {
br_rsa_public_key k = pkey->key.rsa;
br_rsa_public_key rsa =
{.n = malloc(k.nlen), .nlen = k.nlen,
.e = malloc(k.elen), .elen = k.elen};
if (rsa.n == NULL || rsa.e == NULL) {
perror("malloc()");
goto out;
}
memcpy(rsa.n, k.n, k.nlen);
memcpy(rsa.e, k.e, k.elen);
new_key.key_type = BR_KEYTYPE_RSA;
new_key.key.rsa = rsa;
} else if (pkey->key_type == BR_KEYTYPE_EC) {
br_ec_public_key k = pkey->key.ec;
br_ec_public_key ec =
{.curve = k.curve,
.q = malloc(k.qlen), .qlen = k.qlen};
if (ec.q == NULL) {
perror("malloc()");
goto out;
}
memcpy(ec.q, k.q, k.qlen);
new_key.key_type = BR_KEYTYPE_EC;
new_key.key.ec = ec;
} else {
fputs(_("non supported key\n"), stderr);
}
ta.pkey = new_key;
if (append_ta(tas, ta) == -1) {
goto out;
}
}
break;
}
}
rv = tas->n;
out:
fclose(file);
return rv;
}