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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2007-2018, 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 "ed25519_cert.h"
  8. #include "lib/crypt_ops/crypto_format.h"
  9. #include "lib/malloc/util_malloc.h"
  10. int
  11. main(int argc, char **argv)
  12. {
  13. ed25519_cert_t *cert = NULL;
  14. if (argc != 2) {
  15. fprintf(stderr, "Usage:\n");
  16. fprintf(stderr, "%s <path to ed25519_signing_cert file>\n", argv[0]);
  17. return -1;
  18. }
  19. const char *filepath = argv[1];
  20. char *got_tag = NULL;
  21. uint8_t certbuf[256];
  22. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  23. filepath, "ed25519v1-cert",
  24. &got_tag, certbuf, sizeof(certbuf));
  25. if (cert_body_len <= 0) {
  26. fprintf(stderr, "crypto_read_tagged_contents_from_file failed with "
  27. "error: %s\n", strerror(errno));
  28. return -2;
  29. }
  30. if (!got_tag) {
  31. fprintf(stderr, "Found no tag\n");
  32. return -3;
  33. }
  34. if (strcmp(got_tag, "type4") != 0) {
  35. fprintf(stderr, "Wrong tag: %s\n", got_tag);
  36. return -4;
  37. }
  38. tor_free(got_tag);
  39. ssize_t parsed = ed25519_cert_parse(&cert, certbuf, cert_body_len);
  40. if (parsed <= 0) {
  41. fprintf(stderr, "ed25519_cert_parse failed with return value %zd\n",
  42. parsed);
  43. return -5;
  44. }
  45. time_t expires_at = (time_t)cert->exp_field * 60 * 60;
  46. printf("Expires at: %s", ctime(&expires_at));
  47. ed25519_cert_free(cert);
  48. return 0;
  49. }