Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2017, Regents of the University of California. |
| 3 | * |
| 4 | * This file is part of ndncert, a certificate management system based on NDN. |
| 5 | * |
| 6 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 7 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 8 | * version 3 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received copies of the GNU General Public License along with |
| 15 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "challenge-credential.hpp" |
| 21 | #include "../logging.hpp" |
| 22 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 23 | #include <ndn-cxx/util/io.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
| 28 | _LOG_INIT(ndncert.ChallengeCredential); |
| 29 | |
| 30 | NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential"); |
| 31 | |
| 32 | const std::string ChallengeCredential::FAILURE_INVALID_FORMAT = "failure-invalid-format"; |
| 33 | const std::string ChallengeCredential::FAILURE_INVALID_CREDENTIAL = "failure-invalid-credential"; |
| 34 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 35 | const std::string ChallengeCredential::JSON_CREDENTIAL_CERT = "issued-cert"; |
| 36 | const std::string ChallengeCredential::JSON_CREDENTIAL_SELF = "self-signed"; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 37 | |
| 38 | ChallengeCredential::ChallengeCredential(const std::string& configPath) |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 39 | : ChallengeModule("Credential") |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 40 | { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 41 | if (configPath == "") { |
| 42 | m_configFile = std::string(SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
| 43 | } |
| 44 | else |
| 45 | m_configFile = configPath; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | ChallengeCredential::parseConfigFile() |
| 50 | { |
| 51 | JsonSection config; |
| 52 | try { |
| 53 | boost::property_tree::read_json(m_configFile, config); |
| 54 | } |
| 55 | catch (const boost::property_tree::info_parser_error& error) { |
| 56 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + m_configFile + |
| 57 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 58 | } |
| 59 | |
| 60 | if (config.begin() == config.end()) { |
| 61 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + m_configFile + " no data")); |
| 62 | } |
| 63 | |
| 64 | m_trustAnchors.clear(); |
| 65 | auto anchorList = config.get_child("anchor-list"); |
| 66 | auto it = anchorList.begin(); |
| 67 | for (; it != anchorList.end(); it++) { |
| 68 | std::istringstream ss(it->second.get<std::string>("certificate")); |
| 69 | security::v2::Certificate cert = *(io::load<security::v2::Certificate>(ss)); |
| 70 | m_trustAnchors.push_back(cert); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | JsonSection |
| 75 | ChallengeCredential::processSelectInterest(const Interest& interest, CertificateRequest& request) |
| 76 | { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 77 | if (m_trustAnchors.empty()) { |
| 78 | parseConfigFile(); |
| 79 | } |
| 80 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 81 | // interest format: /caName/CA/_SELECT/{"request-id":"id"}/CREDENTIAL/{"credential":"..."}/<signature> |
| 82 | request.setChallengeType(CHALLENGE_TYPE); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 83 | JsonSection credentialJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 84 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 85 | // load credential parameters |
| 86 | std::istringstream ss1(credentialJson.get<std::string>(JSON_CREDENTIAL_CERT)); |
| 87 | security::v2::Certificate cert; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 88 | try { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 89 | cert = *(io::load<security::v2::Certificate>(ss1)); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 90 | } |
| 91 | catch (const std::exception& e) { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 92 | _LOG_TRACE("Cannot load credential parameter: cert" << e.what()); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 93 | request.setStatus(FAILURE_INVALID_FORMAT); |
| 94 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT); |
| 95 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 96 | ss1.str(""); |
| 97 | ss1.clear(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 98 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 99 | std::istringstream ss2(credentialJson.get<std::string>(JSON_CREDENTIAL_SELF)); |
| 100 | security::v2::Certificate self; |
| 101 | try { |
| 102 | self = *(io::load<security::v2::Certificate>(ss2)); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 103 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 104 | catch (const std::exception& e) { |
| 105 | _LOG_TRACE("Cannot load credential parameter: self-signed cert" << e.what()); |
| 106 | request.setStatus(FAILURE_INVALID_FORMAT); |
| 107 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT); |
| 108 | } |
| 109 | ss2.str(""); |
| 110 | ss2.clear(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 111 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 112 | // verify two parameters |
| 113 | Name signingKeyName = cert.getSignature().getKeyLocator().getName(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 114 | for (auto anchor : m_trustAnchors) { |
| 115 | if (anchor.getKeyName() == signingKeyName) { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 116 | if (security::verifySignature(cert, anchor) && security::verifySignature(self, cert)) { |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 117 | request.setStatus(SUCCESS); |
| 118 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS); |
| 119 | } |
| 120 | } |
| 121 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 122 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 123 | request.setStatus(FAILURE_INVALID_CREDENTIAL); |
| 124 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_CREDENTIAL); |
| 125 | } |
| 126 | |
| 127 | JsonSection |
| 128 | ChallengeCredential::processValidateInterest(const Interest& interest, CertificateRequest& request) |
| 129 | { |
| 130 | // there is no validate request here, do nothing |
| 131 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT); |
| 132 | } |
| 133 | |
| 134 | std::list<std::string> |
| 135 | ChallengeCredential::getSelectRequirements() |
| 136 | { |
| 137 | std::list<std::string> result; |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 138 | result.push_back("Please input the bytes of a certificate issued by the trusted CA"); |
| 139 | result.push_back("Please input the bytes of a self-signed certificate for the corresponding key"); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 140 | return result; |
| 141 | } |
| 142 | |
| 143 | std::list<std::string> |
| 144 | ChallengeCredential::getValidateRequirements(const std::string& status) |
| 145 | { |
| 146 | // there is no validate request here, do nothing |
| 147 | std::list<std::string> result; |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | JsonSection |
| 152 | ChallengeCredential::doGenSelectParamsJson(const std::string& status, |
| 153 | const std::list<std::string>& paramList) |
| 154 | { |
| 155 | JsonSection result; |
| 156 | BOOST_ASSERT(status == WAIT_SELECTION); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame^] | 157 | BOOST_ASSERT(paramList.size() == 2); |
| 158 | result.put(JSON_CREDENTIAL_CERT, paramList.front()); |
| 159 | result.put(JSON_CREDENTIAL_SELF, paramList.back()); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 160 | return result; |
| 161 | } |
| 162 | |
| 163 | JsonSection |
| 164 | ChallengeCredential::doGenValidateParamsJson(const std::string& status, |
| 165 | const std::list<std::string>& paramList) |
| 166 | { |
| 167 | JsonSection result; |
| 168 | BOOST_ASSERT(paramList.size() == 0); |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | } // namespace ndncert |
| 173 | } // namespace ndn |