2006-09-12 20:05:54 +02:00
|
|
|
/* Copyright 2006 Nick Mathewson; see LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* id_to_fp.c : Helper for directory authority ops. When somebody sends us
|
|
|
|
* a private key, this utility converts the private key into a fingerprint
|
|
|
|
* so you can de-list that fingerprint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-09-12 21:00:55 +02:00
|
|
|
#define die(s) do { fprintf(stderr, "%s\n", s); goto err; } while (0)
|
2006-09-12 20:05:54 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2006-09-12 21:00:55 +02:00
|
|
|
BIO *b = NULL;
|
|
|
|
RSA *key = NULL;
|
|
|
|
unsigned char *buf = NULL, *bufp;
|
2006-09-12 20:05:54 +02:00
|
|
|
int len, i;
|
|
|
|
unsigned char digest[20];
|
2006-09-12 21:00:55 +02:00
|
|
|
int status = 1;
|
2006-09-12 20:05:54 +02:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
die("I want a filename");
|
|
|
|
if (!(b = BIO_new_file(argv[1], "r")))
|
|
|
|
die("couldn't open file");
|
|
|
|
|
|
|
|
if (!(key = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL)))
|
|
|
|
die("couldn't parse key");
|
|
|
|
|
|
|
|
len = i2d_RSAPublicKey(key, NULL);
|
2006-09-12 21:00:55 +02:00
|
|
|
if (len < 0)
|
|
|
|
die("Bizarre key");
|
2006-09-12 20:05:54 +02:00
|
|
|
bufp = buf = malloc(len+1);
|
2006-09-12 21:00:55 +02:00
|
|
|
if (!buf)
|
|
|
|
die("Out of memory");
|
2006-09-12 20:05:54 +02:00
|
|
|
len = i2d_RSAPublicKey(key, &bufp);
|
|
|
|
if (len < 0)
|
|
|
|
die("Bizarre key");
|
|
|
|
|
|
|
|
SHA1(buf, len, digest);
|
|
|
|
for (i=0; i < 20; i += 2) {
|
|
|
|
printf("%02X%02X ", (int)digest[i], (int)digest[i+1]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2006-09-12 21:00:55 +02:00
|
|
|
status = 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
if (key)
|
|
|
|
RSA_free(key);
|
|
|
|
if (b)
|
|
|
|
BIO_free(b);
|
|
|
|
return status;
|
2006-09-12 20:05:54 +02:00
|
|
|
}
|
|
|
|
|