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" |
| 22 | #include <random> |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | |
| 27 | const std::string ChallengeModule::WAIT_SELECTION = "wait-selection"; |
| 28 | const std::string ChallengeModule::SUCCESS = "success"; |
| 29 | const std::string ChallengeModule::PENDING = "pending"; |
| 30 | |
| 31 | ChallengeModule::ChallengeModule(const std::string& uniqueType) |
| 32 | : CHALLENGE_TYPE(uniqueType) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | unique_ptr<ChallengeModule> |
| 37 | ChallengeModule::createChallengeModule(const std::string& canonicalName) |
| 38 | { |
| 39 | ChallengeFactory& factory = getFactory(); |
| 40 | auto i = factory.find(canonicalName); |
| 41 | return i == factory.end() ? nullptr : i->second(); |
| 42 | } |
| 43 | |
| 44 | JsonSection |
| 45 | ChallengeModule::handleChallengeRequest(const Interest& interest, CertificateRequest& request) |
| 46 | { |
| 47 | int pos = request.getCaName().size(); |
| 48 | const Name& interestName = interest.getName(); |
| 49 | std::string interestType = interestName.get(pos).toUri(); |
| 50 | if (interestType == "_SELECT") { |
| 51 | return processSelectInterest(interest, request); |
| 52 | } |
| 53 | else if (interestType == "_VALIDATE"){ |
| 54 | return processValidateInterest(interest, request); |
| 55 | } |
| 56 | else { |
| 57 | return processStatusInterest(interest, request); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | std::list<std::string> |
| 62 | ChallengeModule::getRequirementForSelect() |
| 63 | { |
| 64 | return getSelectRequirements(); |
| 65 | } |
| 66 | |
| 67 | std::list<std::string> |
| 68 | ChallengeModule::getRequirementForValidate(const std::string& status) |
| 69 | { |
| 70 | return getValidateRequirements(status); |
| 71 | } |
| 72 | |
| 73 | JsonSection |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 74 | ChallengeModule::genSelectParamsJson(const std::string& status, |
| 75 | const std::list<std::string>& paramList) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 76 | { |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 77 | return doGenSelectParamsJson(status, paramList); |
| 78 | } |
| 79 | |
| 80 | JsonSection |
| 81 | ChallengeModule::genValidateParamsJson(const std::string& status, |
| 82 | const std::list<std::string>& paramList) |
| 83 | { |
| 84 | return doGenValidateParamsJson(status, paramList); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | JsonSection |
| 88 | ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request) |
| 89 | { |
| 90 | // interest format: /CA/_STATUS/{"request-id":"id"}/<signature> |
| 91 | if (request.getStatus() == SUCCESS) { |
| 92 | Name downloadName = genDownloadName(request.getCaName(), request.getStatus()); |
| 93 | return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(), |
| 94 | SUCCESS, downloadName); |
| 95 | } |
| 96 | else |
| 97 | return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(), |
| 98 | request.getStatus()); |
| 99 | } |
| 100 | |
| 101 | JsonSection |
| 102 | ChallengeModule::getJsonFromNameComponent(const Name& name, int pos) |
| 103 | { |
| 104 | std::string jsonString = encoding::readString(name.get(pos)); |
| 105 | std::istringstream ss(jsonString); |
| 106 | JsonSection json; |
| 107 | boost::property_tree::json_parser::read_json(ss, json); |
| 108 | return json; |
| 109 | } |
| 110 | |
| 111 | Name |
| 112 | ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId) |
| 113 | { |
| 114 | JsonSection json; |
| 115 | json.put(JSON_REQUEST_ID, requestId); |
| 116 | std::stringstream ss; |
| 117 | boost::property_tree::write_json(ss, json); |
| 118 | Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str()); |
| 119 | Name name = caName; |
| 120 | name.append("_DOWNLOAD").append(jsonBlock); |
| 121 | return name; |
| 122 | } |
| 123 | |
| 124 | ChallengeModule::ChallengeFactory& |
| 125 | ChallengeModule::getFactory() |
| 126 | { |
| 127 | static ChallengeModule::ChallengeFactory factory; |
| 128 | return factory; |
| 129 | } |
| 130 | |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 131 | std::string |
| 132 | ChallengeModule::generateSecretCode() |
| 133 | { |
| 134 | uint32_t securityCode = 0; |
| 135 | do { |
| 136 | securityCode = random::generateSecureWord32(); |
| 137 | } |
| 138 | while (securityCode >= 4294000000); |
| 139 | securityCode /= 4294; |
| 140 | std::string result = std::to_string(securityCode); |
| 141 | while (result.length() < 6) { |
| 142 | result = "0" + result; |
| 143 | } |
| 144 | return result; |
| 145 | } |
| 146 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 147 | } // namespace ndncert |
| 148 | } // namespace ndn |