Merge branch 'maint-0.2.1' into maint-0.2.2
[tor/rransom.git] / src / tools / tor-checkkey.c
blob94c8cbd44cac6d22e0fe294510d35a7134cd10d4
2 #define CRYPTO_PRIVATE
4 #include "orconfig.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "crypto.h"
9 #include "torlog.h"
10 #include "../common/util.h"
11 #include "compat.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
15 int
16 main(int c, char **v)
18 crypto_pk_env_t *env;
19 char *str;
20 RSA *rsa;
21 int wantdigest=0;
22 int fname_idx;
23 char *fname=NULL;
24 init_logging();
26 if (c < 2) {
27 fprintf(stderr, "Hi. I'm tor-checkkey. Tell me a filename that "
28 "has a PEM-encoded RSA public key (like in a cert) and I'll "
29 "dump the modulus. Use the --digest option too and I'll "
30 "dump the digest.\n");
31 return 1;
34 if (crypto_global_init(0, NULL, NULL)) {
35 fprintf(stderr, "Couldn't initialize crypto library.\n");
36 return 1;
39 if (!strcmp(v[1], "--digest")) {
40 wantdigest = 1;
41 fname_idx = 2;
42 if (c<3) {
43 fprintf(stderr, "too few arguments");
44 return 1;
46 } else {
47 wantdigest = 0;
48 fname_idx = 1;
51 fname = expand_filename(v[fname_idx]);
52 str = read_file_to_str(fname, 0, NULL);
53 tor_free(fname);
54 if (!str) {
55 fprintf(stderr, "Couldn't read %s\n", v[fname_idx]);
56 return 1;
59 env = crypto_new_pk_env();
60 if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
61 fprintf(stderr, "Couldn't parse key.\n");
62 return 1;
64 tor_free(str);
66 if (wantdigest) {
67 char digest[HEX_DIGEST_LEN+1];
68 if (crypto_pk_get_fingerprint(env, digest, 0)<0)
69 return 1;
70 printf("%s\n",digest);
71 } else {
72 rsa = _crypto_pk_env_get_rsa(env);
73 str = BN_bn2hex(rsa->n);
75 printf("%s\n", str);
78 return 0;