No need to check for NULL, free_private_key() already does that.
[BearSSL] / tools / files.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <errno.h>
30
31 #include "brssl.h"
32
33 /* see brssl.h */
34 unsigned char *
35 read_file(const char *fname, size_t *len)
36 {
37 bvector vbuf = VEC_INIT;
38 FILE *f;
39
40 *len = 0;
41 f = fopen(fname, "rb");
42 if (f == NULL) {
43 fprintf(stderr,
44 "ERROR: could not open file '%s' for reading\n", fname);
45 return NULL;
46 }
47 for (;;) {
48 unsigned char tmp[1024];
49 size_t rlen;
50
51 rlen = fread(tmp, 1, sizeof tmp, f);
52 if (rlen == 0) {
53 unsigned char *buf;
54
55 if (ferror(f)) {
56 fprintf(stderr,
57 "ERROR: read error on file '%s'\n",
58 fname);
59 fclose(f);
60 return NULL;
61 }
62 buf = VEC_TOARRAY(vbuf);
63 *len = VEC_LEN(vbuf);
64 VEC_CLEAR(vbuf);
65 fclose(f);
66 return buf;
67 }
68 VEC_ADDMANY(vbuf, tmp, rlen);
69 }
70 }
71
72 /* see brssl.h */
73 int
74 write_file(const char *fname, const void *data, size_t len)
75 {
76 FILE *f;
77 const unsigned char *buf;
78
79 f = fopen(fname, "wb");
80 if (f == NULL) {
81 fprintf(stderr,
82 "ERROR: could not open file '%s' for reading\n", fname);
83 return -1;
84 }
85 buf = data;
86 while (len > 0) {
87 size_t wlen;
88
89 wlen = fwrite(buf, 1, len, f);
90 if (wlen == 0) {
91 fprintf(stderr,
92 "ERROR: could not write all bytes to '%s'\n",
93 fname);
94 fclose(f);
95 return -1;
96 }
97 buf += wlen;
98 len -= wlen;
99 }
100 if (ferror(f)) {
101 fprintf(stderr, "ERROR: write error on file '%s'\n", fname);
102 fclose(f);
103 return -1;
104 }
105 fclose(f);
106 return 0;
107 }
108
109 /* see brssl.h */
110 int
111 looks_like_DER(const unsigned char *buf, size_t len)
112 {
113 int fb;
114 size_t dlen;
115
116 if (len < 2) {
117 return 0;
118 }
119 if (*buf ++ != 0x30) {
120 return 0;
121 }
122 fb = *buf ++;
123 len -= 2;
124 if (fb < 0x80) {
125 return (size_t)fb == len;
126 } else if (fb == 0x80) {
127 return 0;
128 } else {
129 fb -= 0x80;
130 if (len < (size_t)fb + 2) {
131 return 0;
132 }
133 len -= (size_t)fb;
134 dlen = 0;
135 while (fb -- > 0) {
136 if (dlen > (len >> 8)) {
137 return 0;
138 }
139 dlen = (dlen << 8) + (size_t)*buf ++;
140 }
141 return dlen == len;
142 }
143 }
144
145 static void
146 vblob_append(void *cc, const void *data, size_t len)
147 {
148 bvector *bv;
149
150 bv = cc;
151 VEC_ADDMANY(*bv, data, len);
152 }
153
154 /* see brssl.h */
155 void
156 free_pem_object_contents(pem_object *po)
157 {
158 if (po != NULL) {
159 xfree(po->name);
160 xfree(po->data);
161 }
162 }
163
164 /* see brssl.h */
165 pem_object *
166 decode_pem(const void *src, size_t len, size_t *num)
167 {
168 VECTOR(pem_object) pem_list = VEC_INIT;
169 br_pem_decoder_context pc;
170 pem_object po, *pos;
171 const unsigned char *buf;
172 bvector bv = VEC_INIT;
173 int inobj;
174
175 br_pem_decoder_init(&pc);
176 buf = src;
177 inobj = 0;
178 po.name = NULL;
179 po.data = NULL;
180 po.data_len = 0;
181 while (len > 0) {
182 size_t tlen;
183
184 tlen = br_pem_decoder_push(&pc, buf, len);
185 buf += tlen;
186 len -= tlen;
187 switch (br_pem_decoder_event(&pc)) {
188
189 case BR_PEM_BEGIN_OBJ:
190 po.name = xstrdup(br_pem_decoder_name(&pc));
191 br_pem_decoder_setdest(&pc, vblob_append, &bv);
192 inobj = 1;
193 break;
194
195 case BR_PEM_END_OBJ:
196 if (inobj) {
197 po.data = VEC_TOARRAY(bv);
198 po.data_len = VEC_LEN(bv);
199 VEC_ADD(pem_list, po);
200 VEC_CLEAR(bv);
201 po.name = NULL;
202 po.data = NULL;
203 po.data_len = 0;
204 inobj = 0;
205 }
206 break;
207
208 case BR_PEM_ERROR:
209 xfree(po.name);
210 VEC_CLEAR(bv);
211 fprintf(stderr,
212 "ERROR: invalid PEM encoding\n");
213 VEC_CLEAREXT(pem_list, &free_pem_object_contents);
214 return NULL;
215 }
216 }
217 if (inobj) {
218 fprintf(stderr, "ERROR: unfinished PEM object\n");
219 xfree(po.name);
220 VEC_CLEAR(bv);
221 VEC_CLEAREXT(pem_list, &free_pem_object_contents);
222 return NULL;
223 }
224
225 *num = VEC_LEN(pem_list);
226 VEC_ADD(pem_list, po);
227 pos = VEC_TOARRAY(pem_list);
228 VEC_CLEAR(pem_list);
229 return pos;
230 }
231
232 /* see brssl.h */
233 br_x509_certificate *
234 read_certificates(const char *fname, size_t *num)
235 {
236 VECTOR(br_x509_certificate) cert_list = VEC_INIT;
237 unsigned char *buf;
238 size_t len;
239 pem_object *pos;
240 size_t u, num_pos;
241 br_x509_certificate *xcs;
242 br_x509_certificate dummy;
243
244 *num = 0;
245
246 /*
247 * TODO: reading the whole file is crude; we could parse them
248 * in a streamed fashion. But it does not matter much in practice.
249 */
250 buf = read_file(fname, &len);
251 if (buf == NULL) {
252 return NULL;
253 }
254
255 /*
256 * Check for a DER-encoded certificate.
257 */
258 if (looks_like_DER(buf, len)) {
259 xcs = xmalloc(2 * sizeof *xcs);
260 xcs[0].data = buf;
261 xcs[0].data_len = len;
262 xcs[1].data = NULL;
263 xcs[1].data_len = 0;
264 *num = 1;
265 return xcs;
266 }
267
268 pos = decode_pem(buf, len, &num_pos);
269 xfree(buf);
270 for (u = 0; u < num_pos; u ++) {
271 if (eqstr(pos[u].name, "CERTIFICATE")
272 || eqstr(pos[u].name, "X509 CERTIFICATE"))
273 {
274 br_x509_certificate xc;
275
276 xc.data = pos[u].data;
277 xc.data_len = pos[u].data_len;
278 pos[u].data = NULL;
279 VEC_ADD(cert_list, xc);
280 }
281 }
282 for (u = 0; u < num_pos; u ++) {
283 free_pem_object_contents(&pos[u]);
284 }
285 xfree(pos);
286
287 if (VEC_LEN(cert_list) == 0) {
288 fprintf(stderr, "ERROR: no certificate in file '%s'\n", fname);
289 return NULL;
290 }
291 *num = VEC_LEN(cert_list);
292 dummy.data = NULL;
293 dummy.data_len = 0;
294 VEC_ADD(cert_list, dummy);
295 xcs = VEC_TOARRAY(cert_list);
296 VEC_CLEAR(cert_list);
297 return xcs;
298 }