loadkeys.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* NOCW */
  2. /*
  3. Please read the README file for condition of use, before
  4. using this software.
  5. Maurice Gittens <mgittens@gits.nl> January 1997
  6. */
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <netinet/in.h>
  10. #include <fcntl.h>
  11. #include <strings.h>
  12. #include <stdlib.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/err.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/ssl.h>
  20. EVP_PKEY * ReadPublicKey(const char *certfile)
  21. {
  22. FILE *fp = fopen (certfile, "r");
  23. X509 *x509;
  24. EVP_PKEY *pkey;
  25. if (!fp)
  26. return NULL;
  27. x509 = PEM_read_X509(fp, NULL, 0, NULL);
  28. if (x509 == NULL)
  29. {
  30. ERR_print_errors_fp (stderr);
  31. return NULL;
  32. }
  33. fclose (fp);
  34. pkey=X509_extract_key(x509);
  35. X509_free(x509);
  36. if (pkey == NULL)
  37. ERR_print_errors_fp (stderr);
  38. return pkey;
  39. }
  40. EVP_PKEY *ReadPrivateKey(const char *keyfile)
  41. {
  42. FILE *fp = fopen(keyfile, "r");
  43. EVP_PKEY *pkey;
  44. if (!fp)
  45. return NULL;
  46. pkey = PEM_read_PrivateKey(fp, NULL, 0, NULL);
  47. fclose (fp);
  48. if (pkey == NULL)
  49. ERR_print_errors_fp (stderr);
  50. return pkey;
  51. }