Back to index

tor  0.2.3.19-rc
tor-checkkey.c
Go to the documentation of this file.
00001 /* Copyright (c) 2008-2012, The Tor Project, Inc. */
00002 /* See LICENSE for licensing information */
00003 
00004 #define CRYPTO_PRIVATE
00005 
00006 #include "orconfig.h"
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include "crypto.h"
00011 #include "torlog.h"
00012 #include "../common/util.h"
00013 #include "compat.h"
00014 #include <openssl/bn.h>
00015 #include <openssl/rsa.h>
00016 
00017 int
00018 main(int c, char **v)
00019 {
00020   crypto_pk_t *env;
00021   char *str;
00022   RSA *rsa;
00023   int wantdigest=0;
00024   int fname_idx;
00025   char *fname=NULL;
00026   init_logging();
00027 
00028   if (c < 2) {
00029     fprintf(stderr, "Hi. I'm tor-checkkey.  Tell me a filename that "
00030             "has a PEM-encoded RSA public key (like in a cert) and I'll "
00031             "dump the modulus.  Use the --digest option too and I'll "
00032             "dump the digest.\n");
00033     return 1;
00034   }
00035 
00036   if (crypto_global_init(0, NULL, NULL)) {
00037     fprintf(stderr, "Couldn't initialize crypto library.\n");
00038     return 1;
00039   }
00040 
00041   if (!strcmp(v[1], "--digest")) {
00042     wantdigest = 1;
00043     fname_idx = 2;
00044     if (c<3) {
00045       fprintf(stderr, "too few arguments");
00046       return 1;
00047     }
00048   } else {
00049     wantdigest = 0;
00050     fname_idx = 1;
00051   }
00052 
00053   fname = expand_filename(v[fname_idx]);
00054   str = read_file_to_str(fname, 0, NULL);
00055   tor_free(fname);
00056   if (!str) {
00057     fprintf(stderr, "Couldn't read %s\n", v[fname_idx]);
00058     return 1;
00059   }
00060 
00061   env = crypto_pk_new();
00062   if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
00063     fprintf(stderr, "Couldn't parse key.\n");
00064     return 1;
00065   }
00066   tor_free(str);
00067 
00068   if (wantdigest) {
00069     char digest[HEX_DIGEST_LEN+1];
00070     if (crypto_pk_get_fingerprint(env, digest, 0)<0)
00071       return 1;
00072     printf("%s\n",digest);
00073   } else {
00074     rsa = _crypto_pk_get_rsa(env);
00075     str = BN_bn2hex(rsa->n);
00076 
00077     printf("%s\n", str);
00078   }
00079 
00080   return 0;
00081 }
00082