Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "challenge-module.hpp" |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 22 | #include "logging.hpp" |
Junxiao Shi | 7c06803 | 2017-05-28 13:40:47 +0000 | [diff] [blame] | 23 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 28 | _LOG_INIT(ndncert.challenge-module); |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 29 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 30 | const std::string ChallengeModule::WAIT_SELECTION = "wait-selection"; |
| 31 | const std::string ChallengeModule::SUCCESS = "success"; |
| 32 | const std::string ChallengeModule::PENDING = "pending"; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 33 | const std::string ChallengeModule::FAILURE = "failure"; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 34 | |
| 35 | ChallengeModule::ChallengeModule(const std::string& uniqueType) |
| 36 | : CHALLENGE_TYPE(uniqueType) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | unique_ptr<ChallengeModule> |
| 41 | ChallengeModule::createChallengeModule(const std::string& canonicalName) |
| 42 | { |
| 43 | ChallengeFactory& factory = getFactory(); |
| 44 | auto i = factory.find(canonicalName); |
| 45 | return i == factory.end() ? nullptr : i->second(); |
| 46 | } |
| 47 | |
| 48 | JsonSection |
| 49 | ChallengeModule::handleChallengeRequest(const Interest& interest, CertificateRequest& request) |
| 50 | { |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 51 | int pos = request.getCaName().size() + 1; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 52 | const Name& interestName = interest.getName(); |
| 53 | std::string interestType = interestName.get(pos).toUri(); |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 54 | |
| 55 | _LOG_TRACE("Incoming challenge request. type: " << interestType); |
| 56 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 57 | if (interestType == "_SELECT") { |
| 58 | return processSelectInterest(interest, request); |
| 59 | } |
| 60 | else if (interestType == "_VALIDATE"){ |
| 61 | return processValidateInterest(interest, request); |
| 62 | } |
| 63 | else { |
| 64 | return processStatusInterest(interest, request); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | std::list<std::string> |
| 69 | ChallengeModule::getRequirementForSelect() |
| 70 | { |
| 71 | return getSelectRequirements(); |
| 72 | } |
| 73 | |
| 74 | std::list<std::string> |
| 75 | ChallengeModule::getRequirementForValidate(const std::string& status) |
| 76 | { |
| 77 | return getValidateRequirements(status); |
| 78 | } |
| 79 | |
| 80 | JsonSection |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 81 | ChallengeModule::genSelectParamsJson(const std::string& status, |
| 82 | const std::list<std::string>& paramList) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 83 | { |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 84 | return doGenSelectParamsJson(status, paramList); |
| 85 | } |
| 86 | |
| 87 | JsonSection |
| 88 | ChallengeModule::genValidateParamsJson(const std::string& status, |
| 89 | const std::list<std::string>& paramList) |
| 90 | { |
| 91 | return doGenValidateParamsJson(status, paramList); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | JsonSection |
| 95 | ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request) |
| 96 | { |
| 97 | // interest format: /CA/_STATUS/{"request-id":"id"}/<signature> |
| 98 | if (request.getStatus() == SUCCESS) { |
| 99 | Name downloadName = genDownloadName(request.getCaName(), request.getStatus()); |
| 100 | return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(), |
| 101 | SUCCESS, downloadName); |
| 102 | } |
| 103 | else |
| 104 | return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(), |
| 105 | request.getStatus()); |
| 106 | } |
| 107 | |
| 108 | JsonSection |
| 109 | ChallengeModule::getJsonFromNameComponent(const Name& name, int pos) |
| 110 | { |
| 111 | std::string jsonString = encoding::readString(name.get(pos)); |
| 112 | std::istringstream ss(jsonString); |
| 113 | JsonSection json; |
| 114 | boost::property_tree::json_parser::read_json(ss, json); |
| 115 | return json; |
| 116 | } |
| 117 | |
| 118 | Name |
| 119 | ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId) |
| 120 | { |
| 121 | JsonSection json; |
| 122 | json.put(JSON_REQUEST_ID, requestId); |
| 123 | std::stringstream ss; |
| 124 | boost::property_tree::write_json(ss, json); |
| 125 | Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str()); |
| 126 | Name name = caName; |
| 127 | name.append("_DOWNLOAD").append(jsonBlock); |
| 128 | return name; |
| 129 | } |
| 130 | |
| 131 | ChallengeModule::ChallengeFactory& |
| 132 | ChallengeModule::getFactory() |
| 133 | { |
| 134 | static ChallengeModule::ChallengeFactory factory; |
| 135 | return factory; |
| 136 | } |
| 137 | |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 138 | std::string |
| 139 | ChallengeModule::generateSecretCode() |
| 140 | { |
| 141 | uint32_t securityCode = 0; |
| 142 | do { |
| 143 | securityCode = random::generateSecureWord32(); |
| 144 | } |
| 145 | while (securityCode >= 4294000000); |
| 146 | securityCode /= 4294; |
| 147 | std::string result = std::to_string(securityCode); |
| 148 | while (result.length() < 6) { |
| 149 | result = "0" + result; |
| 150 | } |
| 151 | return result; |
| 152 | } |
| 153 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 154 | } // namespace ndncert |
| 155 | } // namespace ndn |