metadata.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * \file metadata.c
  3. * \brief Certificate Metadata Probe.
  4. *
  5. */
  6. #include <openssl/pem.h>
  7. #include <openssl/rsa.h>
  8. #include <openssl/x509.h>
  9. #include "qa/questions/questions.h"
  10. /* taken from openssl's s_client app source */
  11. #define BUFSIZE 1024*8
  12. /* for some reasons this is commented into openssl's source code x509.h */
  13. #define X509_get_serialNumber(x) ((x)->cert_info->serialNumber)
  14. #define ISSUER "issuer"
  15. #define SUBJECT "subject"
  16. #define SERIAL "serial"
  17. #define BITLEN "bitlen"
  18. #define PKEY "public key"
  19. #define NBITLEN "N bits"
  20. #define EBITLEN "e bits"
  21. static BIO* out;
  22. static int
  23. metadata_question_setup(void)
  24. {
  25. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  26. return (out != NULL);
  27. }
  28. static int
  29. metadata_question_teardown(void)
  30. {
  31. return BIO_free(out);
  32. }
  33. static int
  34. metadata_question_ask_crt(X509* crt)
  35. {
  36. EVP_PKEY* pkey = NULL;
  37. BIGNUM *serial = NULL;
  38. char *sserial = NULL;
  39. char buf[BUFSIZE];
  40. /* subject informations: country, organization, common name */
  41. X509_NAME_oneline(X509_get_subject_name(crt), buf, sizeof(buf));
  42. BIO_printf(out, "%-10s: %s\n", SUBJECT, buf);
  43. /* issuer informations: country, organization, common name */
  44. X509_NAME_oneline(X509_get_issuer_name(crt), buf, sizeof(buf));
  45. BIO_printf(out, "%-10s: %s\n", ISSUER, buf);
  46. /* serial number */
  47. serial = ASN1_INTEGER_to_BN(X509_get_serialNumber(crt), NULL);
  48. sserial = BN_bn2hex(serial);
  49. BIO_printf(out, "%-10s: %s\n", SERIAL, sserial);
  50. OPENSSL_free(sserial);
  51. BN_free(serial);
  52. /* public key */
  53. pkey = X509_get_pubkey(crt);
  54. /* BIO_printf(out, "%-10s\n", PKEY); */
  55. /* PEM_write_bio_RSAPublicKey(out, pkey->pkey.rsa); */
  56. /* BIO_printf(out, "\r\n\r\n"); */
  57. /* public key bitlength */
  58. BIO_printf(out, "%-10s: %d\n", BITLEN,
  59. EVP_PKEY_bits(pkey));
  60. /* XXX. Compression. TLS version.
  61. * This needs access to the socket.
  62. * Therefore a design change has to be taken. :( */
  63. /* Note: debian builds withouth sslv2 support
  64. * <https://lists.debian.org/debian-devel/2011/04/msg00049.html> */
  65. EVP_PKEY_free(pkey);
  66. return 0;
  67. }
  68. RSA *metadata_question_ask_rsa(const RSA* rsa)
  69. {
  70. BIO_printf(out, "%-10s: %d\n", NBITLEN,
  71. BN_num_bits(rsa->n));
  72. BIO_printf(out, "%-10s: %d\n", EBITLEN,
  73. BN_num_bits(rsa->e));
  74. return NULL;
  75. }
  76. qa_question_t MetadataQuestion = {
  77. .name = "metadata",
  78. .pretty_name = "Metadata",
  79. .setup = metadata_question_setup,
  80. .teardown = metadata_question_teardown,
  81. .ask_crt = metadata_question_ask_crt,
  82. .ask_rsa = metadata_question_ask_rsa
  83. };