blob: 0e5a0536db6aa5bce32bd1c2428c3b29f132727d [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
35const std::string ChallengeCredential::JSON_CREDENTIAL = "signed-cert";
36
37ChallengeCredential::ChallengeCredential(const std::string& configPath)
38 : ChallengeModule("CREDENTIAL")
39 , m_configFile(configPath)
40{
41 parseConfigFile();
42}
43
44void
45ChallengeCredential::parseConfigFile()
46{
47 JsonSection config;
48 try {
49 boost::property_tree::read_json(m_configFile, config);
50 }
51 catch (const boost::property_tree::info_parser_error& error) {
52 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + m_configFile +
53 " " + error.message() + " line " + std::to_string(error.line())));
54 }
55
56 if (config.begin() == config.end()) {
57 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + m_configFile + " no data"));
58 }
59
60 m_trustAnchors.clear();
61 auto anchorList = config.get_child("anchor-list");
62 auto it = anchorList.begin();
63 for (; it != anchorList.end(); it++) {
64 std::istringstream ss(it->second.get<std::string>("certificate"));
65 security::v2::Certificate cert = *(io::load<security::v2::Certificate>(ss));
66 m_trustAnchors.push_back(cert);
67 }
68}
69
70JsonSection
71ChallengeCredential::processSelectInterest(const Interest& interest, CertificateRequest& request)
72{
73 // interest format: /caName/CA/_SELECT/{"request-id":"id"}/CREDENTIAL/{"credential":"..."}/<signature>
74 request.setChallengeType(CHALLENGE_TYPE);
75 JsonSection credentialJson = getJsonFromNameComponent(interest.getName(),
76 request.getCaName().size() + 4);
77 std::istringstream ss(credentialJson.get<std::string>(JSON_CREDENTIAL));
78
79 security::v2::Certificate credential;
80 try {
81 credential = *(io::load<security::v2::Certificate>(ss));
82 }
83 catch (const std::exception& e) {
84 _LOG_TRACE("Cannot load credential parameter" << e.what());
85 request.setStatus(FAILURE_INVALID_FORMAT);
86 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
87 }
88
89 if (credential.getContent() != request.getCert().getContent()
90 || credential.getKeyName() != request.getCert().getKeyName()) {
91 request.setStatus(FAILURE_INVALID_CREDENTIAL);
92 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_CREDENTIAL);
93 }
94 Name signingKeyName = credential.getSignature().getKeyLocator().getName();
95
96 for (auto anchor : m_trustAnchors) {
97 if (anchor.getKeyName() == signingKeyName) {
98 if (security::verifySignature(credential, anchor)) {
99 request.setStatus(SUCCESS);
100 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS);
101 }
102 }
103 }
104 request.setStatus(FAILURE_INVALID_CREDENTIAL);
105 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_CREDENTIAL);
106}
107
108JsonSection
109ChallengeCredential::processValidateInterest(const Interest& interest, CertificateRequest& request)
110{
111 // there is no validate request here, do nothing
112 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
113}
114
115std::list<std::string>
116ChallengeCredential::getSelectRequirements()
117{
118 std::list<std::string> result;
119 result.push_back("Please input the bytes of a same key certificate signed by trust anchor");
120 return result;
121}
122
123std::list<std::string>
124ChallengeCredential::getValidateRequirements(const std::string& status)
125{
126 // there is no validate request here, do nothing
127 std::list<std::string> result;
128 return result;
129}
130
131JsonSection
132ChallengeCredential::doGenSelectParamsJson(const std::string& status,
133 const std::list<std::string>& paramList)
134{
135 JsonSection result;
136 BOOST_ASSERT(status == WAIT_SELECTION);
137 BOOST_ASSERT(paramList.size() == 1);
138 result.put(JSON_CREDENTIAL, paramList.front());
139 return result;
140}
141
142JsonSection
143ChallengeCredential::doGenValidateParamsJson(const std::string& status,
144 const std::list<std::string>& paramList)
145{
146 JsonSection result;
147 BOOST_ASSERT(paramList.size() == 0);
148 return result;
149}
150
151} // namespace ndncert
152} // namespace ndn