id_to_fp.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright 2006 Nick Mathewson; see LICENSE for licensing information */
  2. /* $Id$ */
  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. */
  7. #include <openssl/rsa.h>
  8. #include <openssl/bio.h>
  9. #include <openssl/sha.h>
  10. #include <openssl/pem.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #define die(s) do { fprintf(stderr, "%s\n", s); goto err; } while (0)
  14. int
  15. main(int argc, char **argv)
  16. {
  17. BIO *b = NULL;
  18. RSA *key = NULL;
  19. unsigned char *buf = NULL, *bufp;
  20. int len, i;
  21. unsigned char digest[20];
  22. int status = 1;
  23. if (argc != 2)
  24. die("I want a filename");
  25. if (!(b = BIO_new_file(argv[1], "r")))
  26. die("couldn't open file");
  27. if (!(key = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL)))
  28. die("couldn't parse key");
  29. len = i2d_RSAPublicKey(key, NULL);
  30. if (len < 0)
  31. die("Bizarre key");
  32. bufp = buf = malloc(len+1);
  33. if (!buf)
  34. die("Out of memory");
  35. len = i2d_RSAPublicKey(key, &bufp);
  36. if (len < 0)
  37. die("Bizarre key");
  38. SHA1(buf, len, digest);
  39. for (i=0; i < 20; i += 2) {
  40. printf("%02X%02X ", (int)digest[i], (int)digest[i+1]);
  41. }
  42. printf("\n");
  43. status = 0;
  44. err:
  45. if (buf)
  46. free(buf);
  47. if (key)
  48. RSA_free(key);
  49. if (b)
  50. BIO_free(b);
  51. return status;
  52. }