Add changes file for bug40642.
[tor.git] / src / tools / tor-print-ed-signing-cert.c
blob7d4e0b194415cec76403ea7137b6c6f660767046
1 /* Copyright (c) 2007-2021, 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"
13 #include "lib/encoding/time_fmt.h"
15 int
16 main(int argc, char **argv)
18 ed25519_cert_t *cert = NULL;
19 char rfc1123_buf[RFC1123_TIME_LEN+1] = "";
21 if (argc != 2) {
22 fprintf(stderr, "Usage:\n");
23 fprintf(stderr, "%s <path to ed25519_signing_cert file>\n", argv[0]);
24 return -1;
27 const char *filepath = argv[1];
28 char *got_tag = NULL;
30 uint8_t certbuf[256];
31 ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
32 filepath, "ed25519v1-cert",
33 &got_tag, certbuf, sizeof(certbuf));
35 if (cert_body_len <= 0) {
36 fprintf(stderr, "crypto_read_tagged_contents_from_file failed with "
37 "error: %s\n", strerror(errno));
38 return -2;
41 if (!got_tag) {
42 fprintf(stderr, "Found no tag\n");
43 return -3;
46 if (strcmp(got_tag, "type4") != 0) {
47 fprintf(stderr, "Wrong tag: %s\n", got_tag);
48 return -4;
51 tor_free(got_tag);
53 ssize_t parsed = ed25519_cert_parse(&cert, certbuf, cert_body_len);
54 if (parsed <= 0) {
55 fprintf(stderr, "ed25519_cert_parse failed with return value %" TOR_PRIdSZ
56 "\n", parsed);
57 return -5;
60 time_t expires_at = (time_t)cert->exp_field * 60 * 60;
62 printf("Expires at: %s", ctime(&expires_at));
64 format_rfc1123_time(rfc1123_buf, expires_at);
65 printf("RFC 1123 timestamp: %s\n", rfc1123_buf);
67 printf("UNIX timestamp: %ld\n", (long int)expires_at);
69 ed25519_cert_free(cert);
71 return 0;