a29aae01094b1a128d2d6709784df67db530c42c
[BearSSL] / ta.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 #include "bearssl.h"
33
34 static const char *
35 curve_to_sym(int curve)
36 {
37 switch (curve) {
38 case BR_EC_sect163k1: return "BR_EC_sect163k1";
39 case BR_EC_sect163r1: return "BR_EC_sect163r1";
40 case BR_EC_sect163r2: return "BR_EC_sect163r2";
41 case BR_EC_sect193r1: return "BR_EC_sect193r1";
42 case BR_EC_sect193r2: return "BR_EC_sect193r2";
43 case BR_EC_sect233k1: return "BR_EC_sect233k1";
44 case BR_EC_sect233r1: return "BR_EC_sect233r1";
45 case BR_EC_sect239k1: return "BR_EC_sect239k1";
46 case BR_EC_sect283k1: return "BR_EC_sect283k1";
47 case BR_EC_sect283r1: return "BR_EC_sect283r1";
48 case BR_EC_sect409k1: return "BR_EC_sect409k1";
49 case BR_EC_sect409r1: return "BR_EC_sect409r1";
50 case BR_EC_sect571k1: return "BR_EC_sect571k1";
51 case BR_EC_sect571r1: return "BR_EC_sect571r1";
52 case BR_EC_secp160k1: return "BR_EC_secp160k1";
53 case BR_EC_secp160r1: return "BR_EC_secp160r1";
54 case BR_EC_secp160r2: return "BR_EC_secp160r2";
55 case BR_EC_secp192k1: return "BR_EC_secp192k1";
56 case BR_EC_secp192r1: return "BR_EC_secp192r1";
57 case BR_EC_secp224k1: return "BR_EC_secp224k1";
58 case BR_EC_secp224r1: return "BR_EC_secp224r1";
59 case BR_EC_secp256k1: return "BR_EC_secp256k1";
60 case BR_EC_secp256r1: return "BR_EC_secp256r1";
61 case BR_EC_secp384r1: return "BR_EC_secp384r1";
62 case BR_EC_secp521r1: return "BR_EC_secp521r1";
63 case BR_EC_brainpoolP256r1: return "BR_EC_brainpoolP256r1";
64 case BR_EC_brainpoolP384r1: return "BR_EC_brainpoolP384r1";
65 case BR_EC_brainpoolP512r1: return "BR_EC_brainpoolP512r1";
66 }
67 return NULL;
68 }
69
70 static void
71 print_blob(const char *name, const unsigned char *buf, size_t len)
72 {
73 size_t u;
74
75 printf("\nstatic const unsigned char %s[] = {", name);
76 for (u = 0; u < len; u ++) {
77 if (u != 0) {
78 printf(",");
79 }
80 if (u % 12 == 0) {
81 printf("\n\t");
82 } else {
83 printf(" ");
84 }
85 printf("0x%02X", buf[u]);
86 }
87 printf("\n};\n");
88 }
89
90 static int
91 print_ta_internals(br_x509_trust_anchor *ta, long ctr)
92 {
93 char tmp[25];
94
95 sprintf(tmp, "TA%ld_DN", ctr);
96 print_blob(tmp, ta->dn, ta->dn_len);
97 switch (ta->pkey.key_type) {
98 case BR_KEYTYPE_RSA:
99 sprintf(tmp, "TA%ld_RSA_N", ctr);
100 print_blob(tmp, ta->pkey.key.rsa.n, ta->pkey.key.rsa.nlen);
101 sprintf(tmp, "TA%ld_RSA_E", ctr);
102 print_blob(tmp, ta->pkey.key.rsa.e, ta->pkey.key.rsa.elen);
103 break;
104 case BR_KEYTYPE_EC:
105 sprintf(tmp, "TA%ld_EC_Q", ctr);
106 print_blob(tmp, ta->pkey.key.ec.q, ta->pkey.key.ec.qlen);
107 break;
108 default:
109 fprintf(stderr, "ERROR: unknown anchor key type '%d'\n",
110 ta->pkey.key_type);
111 return -1;
112 }
113 return 0;
114 }
115
116 static void
117 print_ta(br_x509_trust_anchor *ta, long ctr)
118 {
119 char tmp[25];
120
121 printf("\t{\n");
122 printf("\t\t(unsigned char *)TA%ld_DN, sizeof TA%ld_DN,\n", ctr, ctr);
123 printf("\t\t%s,\n", (ta->flags & BR_X509_TA_CA)
124 ? "BR_X509_TA_CA" : "0");
125 printf("\t\t{\n");
126 switch (ta->pkey.key_type) {
127 const char *cname;
128
129 case BR_KEYTYPE_RSA:
130 printf("\t\t\tBR_KEYTYPE_RSA,\n");
131 printf("\t\t\t{ .rsa = {\n");
132 printf("\t\t\t\t(unsigned char *)TA%ld_RSA_N,"
133 " sizeof TA%ld_RSA_N,\n", ctr, ctr);
134 printf("\t\t\t\t(unsigned char *)TA%ld_RSA_E,"
135 " sizeof TA%ld_RSA_E,\n", ctr, ctr);
136 printf("\t\t\t} }\n");
137 break;
138 case BR_KEYTYPE_EC:
139 printf("\t\t\tBR_KEYTYPE_EC,\n");
140 printf("\t\t\t{ .ec = {\n");
141 cname = curve_to_sym(ta->pkey.key.ec.curve);
142 if (cname == NULL) {
143 sprintf(tmp, "%d", ta->pkey.key.ec.curve);
144 cname = tmp;
145 }
146 printf("\t\t\t\t%s,\n", cname);
147 printf("\t\t\t\t(unsigned char *)TA%ld_EC_Q,"
148 " sizeof TA%ld_EC_Q,\n", ctr, ctr);
149 printf("\t\t\t} }\n");
150 }
151 printf("\t\t}\n");
152 printf("\t}");
153 }
154
155 static void
156 usage_ta(void)
157 {
158 fprintf(stderr,
159 "usage: brssl ta [ options ] file...\n");
160 fprintf(stderr,
161 "options:\n");
162 fprintf(stderr,
163 " -q suppress verbose messages\n");
164 }
165
166 /* see brssl.h */
167 int
168 do_ta(int argc, char *argv[])
169 {
170 int retcode;
171 int verbose;
172 int i, num_files;
173 anchor_list tas = VEC_INIT;
174 size_t u, num;
175
176 retcode = 0;
177 verbose = 1;
178 num_files = 0;
179 for (i = 0; i < argc; i ++) {
180 const char *arg;
181
182 arg = argv[i];
183 if (arg[0] != '-') {
184 num_files ++;
185 continue;
186 }
187 argv[i] = NULL;
188 if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
189 verbose = 1;
190 } else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
191 verbose = 0;
192 } else {
193 fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
194 usage_ta();
195 goto ta_exit_error;
196 }
197 }
198 if (num_files == 0) {
199 fprintf(stderr, "ERROR: no certificate file provided\n");
200 usage_ta();
201 goto ta_exit_error;
202 }
203
204 for (i = 0; i < argc; i ++) {
205 const char *fname;
206 size_t len1, len2;
207
208 fname = argv[i];
209 if (fname == NULL) {
210 continue;
211 }
212 if (verbose) {
213 fprintf(stderr, "Reading file '%s': ", fname);
214 fflush(stderr);
215 }
216 len1 = VEC_LEN(tas);
217 if (read_trust_anchors(&tas, fname) == 0) {
218 goto ta_exit_error;
219 }
220 len2 = VEC_LEN(tas) - len1;
221 if (verbose) {
222 fprintf(stderr, "%lu trust anchor%s\n",
223 (unsigned long)len2, len2 > 1 ? "s" : "");
224 }
225 }
226 num = VEC_LEN(tas);
227 for (u = 0; u < num; u ++) {
228 if (print_ta_internals(&VEC_ELT(tas, u), u) < 0) {
229 goto ta_exit_error;
230 }
231 }
232 printf("\nstatic const br_x509_trust_anchor TAs[%ld] = {", (long)num);
233 for (u = 0; u < num; u ++) {
234 if (u != 0) {
235 printf(",");
236 }
237 printf("\n");
238 print_ta(&VEC_ELT(tas, u), u);
239 }
240 printf("\n};\n");
241 printf("\n#define TAs_NUM %ld\n", (long)num);
242
243 /*
244 * Release allocated structures.
245 */
246 ta_exit:
247 VEC_CLEAREXT(tas, free_ta_contents);
248 return retcode;
249
250 ta_exit_error:
251 retcode = -1;
252 goto ta_exit;
253 }