Since dump_microdescriptor() can return -1; make its type ssize_t
[tor.git] / contrib / id_to_fp.c
blob55b025dfafc0793a323413c619100e25806cf7cb
1 /* Copyright 2006 Nick Mathewson; see LICENSE for licensing information */
3 /* id_to_fp.c : Helper for directory authority ops. When somebody sends us
4 * a private key, this utility converts the private key into a fingerprint
5 * so you can de-list that fingerprint.
6 */
8 #include <openssl/rsa.h>
9 #include <openssl/bio.h>
10 #include <openssl/sha.h>
11 #include <openssl/pem.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #define die(s) do { fprintf(stderr, "%s\n", s); goto err; } while (0)
19 int
20 main(int argc, char **argv)
22 BIO *b = NULL;
23 RSA *key = NULL;
24 unsigned char *buf = NULL, *bufp;
25 int len, i;
26 unsigned char digest[20];
27 int status = 1;
29 if (argc < 2) {
30 fprintf(stderr, "Reading key from stdin...\n");
31 if (!(b = BIO_new_fp(stdin, BIO_NOCLOSE)))
32 die("couldn't read from stdin");
33 } else if (argc == 2) {
34 if (strcmp(argv[1], "-h") == 0 ||
35 strcmp(argv[1], "--help") == 0) {
36 fprintf(stdout, "Usage: %s [keyfile]\n", argv[0]);
37 status = 0;
38 goto err;
39 } else {
40 if (!(b = BIO_new_file(argv[1], "r")))
41 die("couldn't open file");
43 } else {
44 fprintf(stderr, "Usage: %s [keyfile]\n", argv[0]);
45 goto err;
47 if (!(key = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL)))
48 die("couldn't parse key");
50 len = i2d_RSAPublicKey(key, NULL);
51 if (len < 0)
52 die("Bizarre key");
53 bufp = buf = malloc(len+1);
54 if (!buf)
55 die("Out of memory");
56 len = i2d_RSAPublicKey(key, &bufp);
57 if (len < 0)
58 die("Bizarre key");
60 SHA1(buf, len, digest);
61 for (i=0; i < 20; i += 2) {
62 printf("%02X%02X ", (int)digest[i], (int)digest[i+1]);
64 printf("\n");
66 status = 0;
68 err:
69 if (buf)
70 free(buf);
71 if (key)
72 RSA_free(key);
73 if (b)
74 BIO_free(b);
75 return status;