blob: 2a35395019a0a2ef47e8ac61b737dd06c633f876 [file] [log] [blame]
Zhiyi Zhang0a89b722017-04-28 17:56:01 -07001/**
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
25namespace ndn {
26namespace ndncert {
27
28_LOG_INIT(ndncert.ChallengeCredential);
29
30NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential");
31
32const std::string ChallengeCredential::FAILURE_INVALID_FORMAT = "failure-invalid-format";
33const std::string ChallengeCredential::FAILURE_INVALID_CREDENTIAL = "failure-invalid-credential";
34
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070035const std::string ChallengeCredential::JSON_CREDENTIAL_CERT = "issued-cert";
36const std::string ChallengeCredential::JSON_CREDENTIAL_SELF = "self-signed";
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070037
38ChallengeCredential::ChallengeCredential(const std::string& configPath)
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070039 : ChallengeModule("Credential")
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070040{
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070041 if (configPath == "") {
42 m_configFile = std::string(SYSCONFDIR) + "/ndncert/challenge-credential.conf";
43 }
44 else
45 m_configFile = configPath;
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070046}
47
48void
49ChallengeCredential::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
74JsonSection
75ChallengeCredential::processSelectInterest(const Interest& interest, CertificateRequest& request)
76{
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070077 if (m_trustAnchors.empty()) {
78 parseConfigFile();
79 }
80
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070081 // interest format: /caName/CA/_SELECT/{"request-id":"id"}/CREDENTIAL/{"credential":"..."}/<signature>
82 request.setChallengeType(CHALLENGE_TYPE);
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070083 JsonSection credentialJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070084
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070085 // load credential parameters
86 std::istringstream ss1(credentialJson.get<std::string>(JSON_CREDENTIAL_CERT));
87 security::v2::Certificate cert;
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070088 try {
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070089 cert = *(io::load<security::v2::Certificate>(ss1));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070090 }
91 catch (const std::exception& e) {
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070092 _LOG_TRACE("Cannot load credential parameter: cert" << e.what());
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070093 request.setStatus(FAILURE_INVALID_FORMAT);
94 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
95 }
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070096 ss1.str("");
97 ss1.clear();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070098
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070099 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 Zhang0a89b722017-04-28 17:56:01 -0700103 }
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700104 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 Zhang0a89b722017-04-28 17:56:01 -0700111
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700112 // verify two parameters
113 Name signingKeyName = cert.getSignature().getKeyLocator().getName();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700114 for (auto anchor : m_trustAnchors) {
115 if (anchor.getKeyName() == signingKeyName) {
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700116 if (security::verifySignature(cert, anchor) && security::verifySignature(self, cert)) {
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700117 request.setStatus(SUCCESS);
118 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS);
119 }
120 }
121 }
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700122
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700123 request.setStatus(FAILURE_INVALID_CREDENTIAL);
124 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_CREDENTIAL);
125}
126
127JsonSection
128ChallengeCredential::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
134std::list<std::string>
135ChallengeCredential::getSelectRequirements()
136{
137 std::list<std::string> result;
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700138 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 Zhang0a89b722017-04-28 17:56:01 -0700140 return result;
141}
142
143std::list<std::string>
144ChallengeCredential::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
151JsonSection
152ChallengeCredential::doGenSelectParamsJson(const std::string& status,
153 const std::list<std::string>& paramList)
154{
155 JsonSection result;
156 BOOST_ASSERT(status == WAIT_SELECTION);
Zhiyi Zhang70fe2582017-05-19 15:01:03 -0700157 BOOST_ASSERT(paramList.size() == 2);
158 result.put(JSON_CREDENTIAL_CERT, paramList.front());
159 result.put(JSON_CREDENTIAL_SELF, paramList.back());
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700160 return result;
161}
162
163JsonSection
164ChallengeCredential::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