CertificateManager.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "CertificateManager.hpp"
  20. Certificate* CertificateManager::readCredentialsFromFile(const path &file, bool resolve) {
  21. X509 *cert = loadCertificateFromFile(system_complete(file).string().c_str());
  22. EVP_PKEY *key = loadKeyFromFile(system_complete(file).string().c_str());
  23. if (!cert || !key) throw BadCertificateException();
  24. return new Certificate(cert, key, resolve);
  25. }
  26. X509* CertificateManager::loadCertificateFromFile(const char* file) {
  27. SSL_CTX *context = SSL_CTX_new(SSLv23_server_method());
  28. SSL_CTX_use_certificate_file(context, file, SSL_FILETYPE_PEM);
  29. return SSL_get_certificate(SSL_new(context));
  30. }
  31. EVP_PKEY* CertificateManager::loadKeyFromFile(const char* file) {
  32. SSL_CTX *context = SSL_CTX_new(SSLv23_server_method());
  33. SSL_CTX_use_PrivateKey_file(context, file, SSL_FILETYPE_PEM);
  34. return SSL_get_privatekey(SSL_new(context));
  35. }