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