Tor  0.4.6.0-alpha-dev
tor-print-ed-signing-cert.c
1 /* Copyright (c) 2007-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <time.h>
8 
9 #include "trunnel/ed25519_cert.h"
10 #include "lib/cc/torint.h" /* TOR_PRIdSZ */
12 #include "lib/malloc/malloc.h"
13 #include "lib/encoding/time_fmt.h"
14 
15 int
16 main(int argc, char **argv)
17 {
18  ed25519_cert_t *cert = NULL;
19  char rfc1123_buf[RFC1123_TIME_LEN+1] = "";
20 
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;
25  }
26 
27  const char *filepath = argv[1];
28  char *got_tag = NULL;
29 
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));
34 
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;
39  }
40 
41  if (!got_tag) {
42  fprintf(stderr, "Found no tag\n");
43  return -3;
44  }
45 
46  if (strcmp(got_tag, "type4") != 0) {
47  fprintf(stderr, "Wrong tag: %s\n", got_tag);
48  return -4;
49  }
50 
51  tor_free(got_tag);
52 
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;
58  }
59 
60  time_t expires_at = (time_t)cert->exp_field * 60 * 60;
61 
62  printf("Expires at: %s", ctime(&expires_at));
63 
64  format_rfc1123_time(rfc1123_buf, expires_at);
65  printf("RFC 1123 timestamp: %s\n", rfc1123_buf);
66 
67  printf("UNIX timestamp: %ld\n", (long int)expires_at);
68 
69  ed25519_cert_free(cert);
70 
71  return 0;
72 }
tor_free
#define tor_free(p)
Definition: malloc.h:52
crypto_read_tagged_contents_from_file
ssize_t crypto_read_tagged_contents_from_file(const char *fname, const char *typestring, char **tag_out, uint8_t *data_out, ssize_t data_out_len)
Definition: crypto_format.c:77
torint.h
Integer definitions used throughout Tor.
format_rfc1123_time
void format_rfc1123_time(char *buf, time_t t)
Definition: time_fmt.c:182
crypto_format.h
Header for crypto_format.c.
time.h
Definitions for timing-related constants.
malloc.h
Headers for util_malloc.c.
time_fmt.h
Header for time_fmt.c.
main
int main(int argc, char *argv[])
Definition: tor_main.c:25