AuthorityCertificateManager.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "AuthorityCertificateManager.hpp"
  20. AuthorityCertificateManager::AuthorityCertificateManager(std::string &file, std::string &chain) {
  21. path certPath(file);
  22. path chainPath(chain);
  23. this->authority = readCredentialsFromFile(certPath, false);
  24. chainList.push_back(this->authority);
  25. if (!chain.empty()) {
  26. Certificate *chain = readCredentialsFromFile(chainPath, false);
  27. chainList.push_back(chain);
  28. }
  29. this->leafPair = buildKeysForClient();
  30. }
  31. bool AuthorityCertificateManager::isOCSPAddress(boost::asio::ip::tcp::endpoint &endpoint) {
  32. boost::asio::ip::address address = endpoint.address();
  33. return this->authority->isOCSPAddress(address);
  34. }
  35. bool AuthorityCertificateManager::isValidTarget(boost::asio::ip::tcp::endpoint &end,
  36. bool wildcardOK)
  37. {
  38. return true;
  39. }
  40. void AuthorityCertificateManager::getCertificateForTarget(boost::asio::ip::tcp::endpoint &endpoint,
  41. bool wildcardOK,
  42. X509 *serverCertificate,
  43. Certificate **cert,
  44. std::list<Certificate*> **chainList)
  45. {
  46. X509_NAME *serverName = X509_get_subject_name(serverCertificate);
  47. X509_NAME *issuerName = X509_get_subject_name(authority->getCert());
  48. X509 *request = X509_new();
  49. X509_set_version(request, 3);
  50. X509_set_subject_name(request, serverName);
  51. X509_set_issuer_name(request, issuerName);
  52. ASN1_INTEGER_set(X509_get_serialNumber(request), generateRandomSerial());
  53. X509_gmtime_adj(X509_get_notBefore(request), -365);
  54. X509_gmtime_adj(X509_get_notAfter(request), (long)60*60*24*365);
  55. X509_set_pubkey(request, this->leafPair);
  56. X509_sign(request, authority->getKey(), EVP_sha1());
  57. Certificate *leaf = new Certificate();
  58. leaf->setCert(request);
  59. leaf->setKey(this->leafPair);
  60. *cert = leaf;
  61. *chainList = &(this->chainList);
  62. // *chain = this->authority;
  63. }
  64. unsigned int AuthorityCertificateManager::generateRandomSerial() {
  65. unsigned int serial;
  66. RAND_bytes((unsigned char*)&serial, sizeof(serial));
  67. return serial;
  68. }
  69. EVP_PKEY* AuthorityCertificateManager::buildKeysForClient() {
  70. RSA *rsaKeyPair = RSA_generate_key(1024, RSA_F4, NULL, NULL);
  71. EVP_PKEY *rsaKeyPairSpec = EVP_PKEY_new();
  72. EVP_PKEY_assign_RSA(rsaKeyPairSpec, rsaKeyPair);
  73. return rsaKeyPairSpec;
  74. }