More optimisations for EC P-256 "i15" (specialised squaring function, mixed coordinat...
[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 *num = 0;
176 br_pem_decoder_init(&pc);
177 buf = src;
178 inobj = 0;
179 po.name = NULL;
180 po.data = NULL;
181 po.data_len = 0;
182 while (len > 0) {
183 size_t tlen;
184
185 tlen = br_pem_decoder_push(&pc, buf, len);
186 buf += tlen;
187 len -= tlen;
188 switch (br_pem_decoder_event(&pc)) {
189
190 case BR_PEM_BEGIN_OBJ:
191 po.name = xstrdup(br_pem_decoder_name(&pc));
192 br_pem_decoder_setdest(&pc, vblob_append, &bv);
193 inobj = 1;
194 break;
195
196 case BR_PEM_END_OBJ:
197 if (inobj) {
198 po.data = VEC_TOARRAY(bv);
199 po.data_len = VEC_LEN(bv);
200 VEC_ADD(pem_list, po);
201 VEC_CLEAR(bv);
202 po.name = NULL;
203 po.data = NULL;
204 po.data_len = 0;
205 inobj = 0;
206 }
207 break;
208
209 case BR_PEM_ERROR:
210 xfree(po.name);
211 VEC_CLEAR(bv);
212 fprintf(stderr,
213 "ERROR: invalid PEM encoding\n");
214 VEC_CLEAREXT(pem_list, &free_pem_object_contents);
215 return NULL;
216 }
217 }
218 if (inobj) {
219 fprintf(stderr, "ERROR: unfinished PEM object\n");
220 xfree(po.name);
221 VEC_CLEAR(bv);
222 VEC_CLEAREXT(pem_list, &free_pem_object_contents);
223 return NULL;
224 }
225
226 *num = VEC_LEN(pem_list);
227 VEC_ADD(pem_list, po);
228 pos = VEC_TOARRAY(pem_list);
229 VEC_CLEAR(pem_list);
230 return pos;
231 }
232
233 /* see brssl.h */
234 br_x509_certificate *
235 read_certificates(const char *fname, size_t *num)
236 {
237 VECTOR(br_x509_certificate) cert_list = VEC_INIT;
238 unsigned char *buf;
239 size_t len;
240 pem_object *pos;
241 size_t u, num_pos;
242 br_x509_certificate *xcs;
243 br_x509_certificate dummy;
244
245 *num = 0;
246
247 /*
248 * TODO: reading the whole file is crude; we could parse them
249 * in a streamed fashion. But it does not matter much in practice.
250 */
251 buf = read_file(fname, &len);
252 if (buf == NULL) {
253 return NULL;
254 }
255
256 /*
257 * Check for a DER-encoded certificate.
258 */
259 if (looks_like_DER(buf, len)) {
260 xcs = xmalloc(2 * sizeof *xcs);
261 xcs[0].data = buf;
262 xcs[0].data_len = len;
263 xcs[1].data = NULL;
264 xcs[1].data_len = 0;
265 *num = 1;
266 return xcs;
267 }
268
269 pos = decode_pem(buf, len, &num_pos);
270 xfree(buf);
271 if (pos == NULL) {
272 return NULL;
273 }
274 for (u = 0; u < num_pos; u ++) {
275 if (eqstr(pos[u].name, "CERTIFICATE")
276 || eqstr(pos[u].name, "X509 CERTIFICATE"))
277 {
278 br_x509_certificate xc;
279
280 xc.data = pos[u].data;
281 xc.data_len = pos[u].data_len;
282 pos[u].data = NULL;
283 VEC_ADD(cert_list, xc);
284 }
285 }
286 for (u = 0; u < num_pos; u ++) {
287 free_pem_object_contents(&pos[u]);
288 }
289 xfree(pos);
290
291 if (VEC_LEN(cert_list) == 0) {
292 fprintf(stderr, "ERROR: no certificate in file '%s'\n", fname);
293 return NULL;
294 }
295 *num = VEC_LEN(cert_list);
296 dummy.data = NULL;
297 dummy.data_len = 0;
298 VEC_ADD(cert_list, dummy);
299 xcs = VEC_TOARRAY(cert_list);
300 VEC_CLEAR(cert_list);
301 return xcs;
302 }
303
304 /* see brssl.h */
305 void
306 free_certificates(br_x509_certificate *certs, size_t num)
307 {
308 size_t u;
309
310 for (u = 0; u < num; u ++) {
311 xfree(certs[u].data);
312 }
313 xfree(certs);
314 }