Remove doc/spec/Makefile.in from list of generated files
[tor/rransom.git] / src / tools / tor-checkkey.c
blobb29b52d8db05bdc0dfdb2653c2f296dfc69307bd
2 #define CRYPTO_PRIVATE
4 #include "orconfig.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "crypto.h"
9 #include "log.h"
10 #include "util.h"
11 #include "compat.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
15 int main(int c, char **v)
17 crypto_pk_env_t *env;
18 char *str;
19 RSA *rsa;
20 int wantdigest=0;
21 int fname_idx;
22 init_logging();
24 if (c < 2) {
25 fprintf(stderr, "Hi. I'm tor-checkkey. Tell me a filename that "
26 "has a PEM-encoded RSA public key (like in a cert) and I'll "
27 "dump the modulus. Use the --digest option too and I'll "
28 "dump the digest.\n");
29 return 1;
32 if (crypto_global_init(0)) {
33 fprintf(stderr, "Couldn't initialize crypto library.\n");
34 return 1;
37 if (!strcmp(v[1], "--digest")) {
38 wantdigest = 1;
39 fname_idx = 2;
40 if (c<3) {
41 fprintf(stderr, "too few arguments");
42 return 1;
44 } else {
45 wantdigest = 0;
46 fname_idx = 1;
49 str = read_file_to_str(v[fname_idx], 0, NULL);
50 if (!str) {
51 fprintf(stderr, "Couldn't read %s\n", v[fname_idx]);
52 return 1;
55 env = crypto_new_pk_env();
56 if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
57 fprintf(stderr, "Couldn't parse key.\n");
58 return 1;
60 tor_free(str);
62 if (wantdigest) {
63 char digest[HEX_DIGEST_LEN+1];
64 if (crypto_pk_get_fingerprint(env, digest, 0)<0)
65 return 1;
66 printf("%s\n",digest);
67 } else {
68 rsa = _crypto_pk_env_get_rsa(env);
69 str = BN_bn2hex(rsa->n);
71 printf("%s\n", str);
74 return 0;