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 print_blob(const char *name
, const unsigned char *buf
, size_t len
)
39 printf("\nstatic const unsigned char %s[] = {", name
);
40 for (u
= 0; u
< len
; u
++) {
49 printf("0x%02X", buf
[u
]);
58 "usage: brssl chain [ options ] file...\n");
62 " -q suppress verbose messages\n");
67 do_chain(int argc
, char *argv
[])
77 for (i
= 0; i
< argc
; i
++) {
86 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
88 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
91 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
93 goto chain_exit_error
;
97 fprintf(stderr
, "ERROR: no certificate file provided\n");
99 goto chain_exit_error
;
103 for (i
= 0; i
< argc
; i
++) {
105 br_x509_certificate
*xcs
;
113 fprintf(stderr
, "Reading file '%s': ", fname
);
116 xcs
= read_certificates(fname
, &num
);
118 goto chain_exit_error
;
121 fprintf(stderr
, "%lu certificate%s\n",
122 (unsigned long)num
, num
> 1 ? "s" : "");
124 for (u
= 0; u
< num
; u
++) {
127 sprintf(tmp
, "CERT%ld", ctr
++);
128 print_blob(tmp
, xcs
[u
].data
, xcs
[u
].data_len
);
134 printf("\nstatic const br_x509_certificate CHAIN[] = {");
135 for (k
= 0; k
< ctr
; k
++) {
139 printf("\n\t{ (unsigned char *)CERT%ld, sizeof CERT%ld }",
143 printf("\n#define CHAIN_LEN %ld\n", ctr
);
146 * Release allocated structures.