a29aae01094b1a128d2d6709784df67db530c42c
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
35 curve_to_sym(int 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";
71 print_blob(const char *name
, const unsigned char *buf
, size_t len
)
75 printf("\nstatic const unsigned char %s[] = {", name
);
76 for (u
= 0; u
< len
; u
++) {
85 printf("0x%02X", buf
[u
]);
91 print_ta_internals(br_x509_trust_anchor
*ta
, long ctr
)
95 sprintf(tmp
, "TA%ld_DN", ctr
);
96 print_blob(tmp
, ta
->dn
, ta
->dn_len
);
97 switch (ta
->pkey
.key_type
) {
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
);
105 sprintf(tmp
, "TA%ld_EC_Q", ctr
);
106 print_blob(tmp
, ta
->pkey
.key
.ec
.q
, ta
->pkey
.key
.ec
.qlen
);
109 fprintf(stderr
, "ERROR: unknown anchor key type '%d'\n",
117 print_ta(br_x509_trust_anchor
*ta
, long ctr
)
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");
126 switch (ta
->pkey
.key_type
) {
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");
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
);
143 sprintf(tmp
, "%d", ta
->pkey
.key
.ec
.curve
);
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");
159 "usage: brssl ta [ options ] file...\n");
163 " -q suppress verbose messages\n");
168 do_ta(int argc
, char *argv
[])
173 anchor_list tas
= VEC_INIT
;
179 for (i
= 0; i
< argc
; i
++) {
188 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
190 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
193 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
198 if (num_files
== 0) {
199 fprintf(stderr
, "ERROR: no certificate file provided\n");
204 for (i
= 0; i
< argc
; i
++) {
213 fprintf(stderr
, "Reading file '%s': ", fname
);
217 if (read_trust_anchors(&tas
, fname
) == 0) {
220 len2
= VEC_LEN(tas
) - len1
;
222 fprintf(stderr
, "%lu trust anchor%s\n",
223 (unsigned long)len2
, len2
> 1 ? "s" : "");
227 for (u
= 0; u
< num
; u
++) {
228 if (print_ta_internals(&VEC_ELT(tas
, u
), u
) < 0) {
232 printf("\nstatic const br_x509_trust_anchor TAs[%ld] = {", (long)num
);
233 for (u
= 0; u
< num
; u
++) {
238 print_ta(&VEC_ELT(tas
, u
), u
);
241 printf("\n#define TAs_NUM %ld\n", (long)num
);
244 * Release allocated structures.
247 VEC_CLEAREXT(tas
, free_ta_contents
);