id_to_fp.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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"); return 1; } while (0)
  14. int
  15. main(int argc, char **argv)
  16. {
  17. BIO *b;
  18. RSA *key;
  19. unsigned char *buf, *bufp;
  20. int len, i;
  21. unsigned char digest[20];
  22. if (argc != 2)
  23. die("I want a filename");
  24. if (!(b = BIO_new_file(argv[1], "r")))
  25. die("couldn't open file");
  26. if (!(key = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL)))
  27. die("couldn't parse key");
  28. len = i2d_RSAPublicKey(key, NULL);
  29. bufp = buf = malloc(len+1);
  30. len = i2d_RSAPublicKey(key, &bufp);
  31. if (len < 0)
  32. die("Bizarre key");
  33. SHA1(buf, len, digest);
  34. for (i=0; i < 20; i += 2) {
  35. printf("%02X%02X ", (int)digest[i], (int)digest[i+1]);
  36. }
  37. printf("\n");
  38. return 0;
  39. }