tor-print-ed-signing-cert.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include "trunnel/ed25519_cert.h"
  8. #include "lib/cc/torint.h" /* TOR_PRIdSZ */
  9. #include "lib/crypt_ops/crypto_format.h"
  10. #include "lib/malloc/malloc.h"
  11. #include "lib/encoding/time_fmt.h"
  12. int
  13. main(int argc, char **argv)
  14. {
  15. ed25519_cert_t *cert = NULL;
  16. char rfc1123_buf[RFC1123_TIME_LEN+1] = "";
  17. if (argc != 2) {
  18. fprintf(stderr, "Usage:\n");
  19. fprintf(stderr, "%s <path to ed25519_signing_cert file>\n", argv[0]);
  20. return -1;
  21. }
  22. const char *filepath = argv[1];
  23. char *got_tag = NULL;
  24. uint8_t certbuf[256];
  25. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  26. filepath, "ed25519v1-cert",
  27. &got_tag, certbuf, sizeof(certbuf));
  28. if (cert_body_len <= 0) {
  29. fprintf(stderr, "crypto_read_tagged_contents_from_file failed with "
  30. "error: %s\n", strerror(errno));
  31. return -2;
  32. }
  33. if (!got_tag) {
  34. fprintf(stderr, "Found no tag\n");
  35. return -3;
  36. }
  37. if (strcmp(got_tag, "type4") != 0) {
  38. fprintf(stderr, "Wrong tag: %s\n", got_tag);
  39. return -4;
  40. }
  41. tor_free(got_tag);
  42. ssize_t parsed = ed25519_cert_parse(&cert, certbuf, cert_body_len);
  43. if (parsed <= 0) {
  44. fprintf(stderr, "ed25519_cert_parse failed with return value %" TOR_PRIdSZ
  45. "\n", parsed);
  46. return -5;
  47. }
  48. time_t expires_at = (time_t)cert->exp_field * 60 * 60;
  49. printf("Expires at: %s", ctime(&expires_at));
  50. format_rfc1123_time(rfc1123_buf, expires_at);
  51. printf("RFC 1123 timestamp: %s\n", rfc1123_buf);
  52. printf("UNIX timestamp: %ld\n", (long int)expires_at);
  53. ed25519_cert_free(cert);
  54. return 0;
  55. }