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 rsa_bit_length(const br_rsa_public_key
*pk
)
40 for (u
= 0; u
< pk
->nlen
; u
++) {
48 bl
= (unsigned)(pk
->nlen
- u
- 1) << 3;
58 print_rsa(const br_rsa_public_key
*pk
, int print_text
, int print_C
)
64 for (u
= 0; u
< pk
->nlen
; u
++) {
65 printf("%02X", pk
->n
[u
]);
69 for (u
= 0; u
< pk
->elen
; u
++) {
70 printf("%02X", pk
->e
[u
]);
77 printf("\nstatic const unsigned char RSA_N[] = {");
78 for (u
= 0; u
< pk
->nlen
; u
++) {
87 printf("0x%02X", pk
->n
[u
]);
90 printf("\nstatic const unsigned char RSA_E[] = {");
91 for (u
= 0; u
< pk
->elen
; u
++) {
100 printf("0x%02X", pk
->e
[u
]);
103 printf("\nstatic const br_rsa_public_key RSA = {\n");
104 printf("\t(unsigned char *)RSA_N, sizeof RSA_N,\n");
105 printf("\t(unsigned char *)RSA_E, sizeof RSA_E\n");
111 print_ec(const br_ec_public_key
*pk
, int print_text
, int print_C
)
117 for (u
= 0; u
< pk
->qlen
; u
++) {
118 printf("%02X", pk
->q
[u
]);
125 printf("\nstatic const unsigned char EC_Q[] = {");
126 for (u
= 0; u
< pk
->qlen
; u
++) {
135 printf("0x%02X", pk
->q
[u
]);
138 printf("\nstatic const br_ec_public_key EC = {\n");
139 printf("\t%d,\n", pk
->curve
);
140 printf("\t(unsigned char *)EC_Q, sizeof EC_Q\n");
149 "usage: brssl verify [ options ] file...\n");
153 " -q suppress verbose messages\n");
155 " -sni name check presence of a specific server name\n");
157 " -CA file add certificates in 'file' to trust anchors\n");
159 " -text print public key details (human-readable)\n");
161 " -C print public key details (C code)\n");
164 typedef VECTOR(br_x509_certificate
) cert_list
;
167 free_cert_contents(br_x509_certificate
*xc
)
174 do_verify(int argc
, char *argv
[])
180 anchor_list anchors
= VEC_INIT
;
181 cert_list chain
= VEC_INIT
;
183 br_x509_minimal_context mc
;
185 int print_text
, print_C
;
187 const br_x509_pkey
*tpk
;
196 for (i
= 0; i
< argc
; i
++) {
201 br_x509_certificate
*xcs
;
204 xcs
= read_certificates(arg
, &num
);
207 goto verify_exit_error
;
209 VEC_ADDMANY(chain
, xcs
, num
);
213 if (eqstr(arg
, "-v") || eqstr(arg
, "-verbose")) {
215 } else if (eqstr(arg
, "-q") || eqstr(arg
, "-quiet")) {
217 } else if (eqstr(arg
, "-sni")) {
220 "ERROR: no argument for '-sni'\n");
222 goto verify_exit_error
;
225 fprintf(stderr
, "ERROR: duplicate SNI\n");
227 goto verify_exit_error
;
231 } else if (eqstr(arg
, "-CA")) {
234 "ERROR: no argument for '-CA'\n");
236 goto verify_exit_error
;
239 if (read_trust_anchors(&anchors
, arg
) == 0) {
241 goto verify_exit_error
;
244 } else if (eqstr(arg
, "-text")) {
246 } else if (eqstr(arg
, "-C")) {
249 fprintf(stderr
, "ERROR: unknown option: '%s'\n", arg
);
251 goto verify_exit_error
;
254 if (VEC_LEN(chain
) == 0) {
255 fprintf(stderr
, "ERROR: no certificate chain provided\n");
257 goto verify_exit_error
;
259 br_x509_minimal_init(&mc
, &br_sha256_vtable
,
260 &VEC_ELT(anchors
, 0), VEC_LEN(anchors
));
261 br_x509_minimal_set_hash(&mc
, br_sha1_ID
, &br_sha1_vtable
);
262 br_x509_minimal_set_hash(&mc
, br_sha224_ID
, &br_sha224_vtable
);
263 br_x509_minimal_set_hash(&mc
, br_sha256_ID
, &br_sha256_vtable
);
264 br_x509_minimal_set_hash(&mc
, br_sha384_ID
, &br_sha384_vtable
);
265 br_x509_minimal_set_hash(&mc
, br_sha512_ID
, &br_sha512_vtable
);
266 br_x509_minimal_set_rsa(&mc
, &br_rsa_i31_pkcs1_vrfy
);
267 br_x509_minimal_set_ecdsa(&mc
,
268 &br_ec_prime_i31
, &br_ecdsa_i31_vrfy_asn1
);
270 mc
.vtable
->start_chain(&mc
.vtable
, sni
);
271 for (u
= 0; u
< VEC_LEN(chain
); u
++) {
272 br_x509_certificate
*xc
;
274 xc
= &VEC_ELT(chain
, u
);
275 mc
.vtable
->start_cert(&mc
.vtable
, xc
->data_len
);
276 mc
.vtable
->append(&mc
.vtable
, xc
->data
, xc
->data_len
);
277 mc
.vtable
->end_cert(&mc
.vtable
);
279 err
= mc
.vtable
->end_chain(&mc
.vtable
);
280 tpk
= mc
.vtable
->get_pkey(&mc
.vtable
, &usages
);
289 fprintf(stderr
, "Validation success; usages:");
291 if (usages
& BR_KEYTYPE_KEYX
) {
292 fprintf(stderr
, " key exchange");
295 if (usages
& BR_KEYTYPE_SIGN
) {
297 fprintf(stderr
, ",");
299 fprintf(stderr
, " signature");
301 fprintf(stderr
, "\n");
305 const char *errname
, *errmsg
;
307 fprintf(stderr
, "Validation failed, err = %d", err
);
308 errname
= find_error_name(err
, &errmsg
);
309 if (errname
!= NULL
) {
310 fprintf(stderr
, " (%s): %s\n", errname
, errmsg
);
312 fprintf(stderr
, " (unknown)\n");
318 switch (pk
->key_type
) {
321 fprintf(stderr
, "Key type: RSA (%u bits)\n",
322 rsa_bit_length(&pk
->key
.rsa
));
324 print_rsa(&pk
->key
.rsa
, print_text
, print_C
);
328 fprintf(stderr
, "Key type: EC (%s)\n",
329 ec_curve_name(pk
->key
.ec
.curve
));
331 print_ec(&pk
->key
.ec
, print_text
, print_C
);
335 fprintf(stderr
, "Unknown key type\n");
342 * Release allocated structures.
345 VEC_CLEAREXT(anchors
, &free_ta_contents
);
346 VEC_CLEAREXT(chain
, &free_cert_contents
);