"Fossies" - the Fresh Open Source Software Archive

Member "tor-0.4.1.6/src/tools/tor-print-ed-signing-cert.c" (10 Jun 2019, 1542 Bytes) of package /linux/misc/tor-0.4.1.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tor-print-ed-signing-cert.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.4.1.6_vs_0.4.2.4-rc.

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