TargetedCertificateManager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "TargetedCertificateManager.hpp"
  20. #include <openssl/pem.h>
  21. #include <openssl/conf.h>
  22. #include <openssl/x509v3.h>
  23. #include <openssl/ssl.h>
  24. #include <openssl/err.h>
  25. #include <openssl/rand.h>
  26. #include <iostream>
  27. #include <string>
  28. #include <boost/filesystem.hpp>
  29. using namespace boost::filesystem;
  30. TargetedCertificateManager::TargetedCertificateManager(std::string &directory,
  31. std::string &chain)
  32. {
  33. path certDir(directory);
  34. path chainPath(chain);
  35. if (!exists(certDir)) throw NoCertificateDirectoryException();
  36. if (!chain.empty()) {
  37. Certificate *chain = readCredentialsFromFile(chainPath, false);
  38. chainList.push_back(chain);
  39. }
  40. directory_iterator end_itr;
  41. for ( directory_iterator itr( certDir ); itr != end_itr; ++itr ) {
  42. if ( !is_directory(itr->status()) ) {
  43. Certificate *target = readCredentialsFromFile(itr->path(), true);
  44. if (target->isWildcard()) certificates.push_back(target);
  45. else certificates.push_front(target);
  46. }
  47. }
  48. if (certificates.empty()) throw NoCertificateDirectoryException();
  49. }
  50. bool TargetedCertificateManager::isOCSPAddress(boost::asio::ip::tcp::endpoint &endpoint) {
  51. boost::asio::ip::address address = endpoint.address();
  52. std::list<Certificate*>::iterator i = certificates.begin();
  53. std::list<Certificate*>::iterator end = certificates.end();
  54. for ( ; i != end; i++) {
  55. if ((*i)->isOCSPAddress(address))
  56. return true;
  57. }
  58. return false;
  59. }
  60. bool TargetedCertificateManager::isValidTarget(boost::asio::ip::tcp::endpoint &endpoint,
  61. bool wildcardOK) {
  62. boost::asio::ip::address address = endpoint.address();
  63. std::list<Certificate*>::iterator i = certificates.begin();
  64. std::list<Certificate*>::iterator end = certificates.end();
  65. for ( ; i != end; i++) {
  66. if ((*i)->isValidTarget(address, wildcardOK)) return true;
  67. }
  68. return false;
  69. }
  70. void TargetedCertificateManager::getCertificateForTarget(boost::asio::ip::tcp::endpoint &endpoint,
  71. bool wildcardOK,
  72. X509 *serverCertificate,
  73. Certificate **cert,
  74. std::list<Certificate*> **chainList)
  75. {
  76. boost::asio::ip::address address = endpoint.address();
  77. *chainList = &(this->chainList);
  78. // *chain = this->chain;
  79. std::list<Certificate*>::iterator i = certificates.begin();
  80. std::list<Certificate*>::iterator end = certificates.end();
  81. for ( ; i != end; i++) {
  82. if ((*i)->isValidTarget(address, wildcardOK)) {
  83. *cert = (*i);
  84. return;
  85. }
  86. }
  87. *cert = NULL;
  88. return;
  89. }
  90. void TargetedCertificateManager::dump() {
  91. std::list<Certificate*>::iterator i;
  92. for(i=certificates.begin(); i != certificates.end(); ++i)
  93. std::cout << "Certificate: " << (*i)->getCert()->name << std::endl;
  94. }