Fuzzing module for various string operations, currently focusing on
[tor.git] / src / tools / tor-print-ed-signing-cert.c
blob9bb4db0a6eeff7da6f7e55323d9d2e94f3dcf882
1 /* Copyright (c) 2007-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <time.h>
9 #include "trunnel/ed25519_cert.h"
10 #include "lib/cc/torint.h" /* TOR_PRIdSZ */
11 #include "lib/crypt_ops/crypto_format.h"
12 #include "lib/malloc/malloc.h"
14 int
15 main(int argc, char **argv)
17 ed25519_cert_t *cert = NULL;
19 if (argc != 2) {
20 fprintf(stderr, "Usage:\n");
21 fprintf(stderr, "%s <path to ed25519_signing_cert file>\n", argv[0]);
22 return -1;
25 const char *filepath = argv[1];
26 char *got_tag = NULL;
28 uint8_t certbuf[256];
29 ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
30 filepath, "ed25519v1-cert",
31 &got_tag, certbuf, sizeof(certbuf));
33 if (cert_body_len <= 0) {
34 fprintf(stderr, "crypto_read_tagged_contents_from_file failed with "
35 "error: %s\n", strerror(errno));
36 return -2;
39 if (!got_tag) {
40 fprintf(stderr, "Found no tag\n");
41 return -3;
44 if (strcmp(got_tag, "type4") != 0) {
45 fprintf(stderr, "Wrong tag: %s\n", got_tag);
46 return -4;
49 tor_free(got_tag);
51 ssize_t parsed = ed25519_cert_parse(&cert, certbuf, cert_body_len);
52 if (parsed <= 0) {
53 fprintf(stderr, "ed25519_cert_parse failed with return value %" TOR_PRIdSZ
54 "\n", parsed);
55 return -5;
58 time_t expires_at = (time_t)cert->exp_field * 60 * 60;
60 printf("Expires at: %s", ctime(&expires_at));
62 ed25519_cert_free(cert);
64 return 0;