Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 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 "client-module.hpp" |
| 22 | #include "logging.hpp" |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 23 | #include "challenge-module.hpp" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 24 | #include "crypto-support/enc-tlv.hpp" |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 25 | #include "protocol-detail/info.hpp" |
| 26 | #include "protocol-detail/probe.hpp" |
| 27 | #include "protocol-detail/new.hpp" |
| 28 | #include "protocol-detail/challenge.hpp" |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 29 | #include <ndn-cxx/util/io.hpp> |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 30 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 31 | #include <ndn-cxx/security/verification-helpers.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/util/random.hpp> |
| 33 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 34 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 35 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndncert { |
| 39 | |
| 40 | _LOG_INIT(ndncert.client); |
| 41 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | ClientModule::ClientModule(security::v2::KeyChain& keyChain) |
| 43 | : m_keyChain(keyChain) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 47 | ClientModule::~ClientModule() |
| 48 | { |
| 49 | endSession(); |
| 50 | } |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 51 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | shared_ptr<Interest> |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 53 | ClientModule::generateInfoInterest(const Name& caName) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 54 | { |
| 55 | Name interestName = caName; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | if (readString(caName.at(-1)) != "CA") |
| 57 | interestName.append("CA"); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 58 | interestName.append("INFO"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | auto interest = make_shared<Interest>(interestName); |
| 60 | interest->setMustBeFresh(true); |
| 61 | interest->setCanBePrefix(false); |
| 62 | return interest; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 63 | } |
| 64 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 65 | bool |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 66 | ClientModule::verifyInfoResponse(const Data& reply) |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 67 | { |
| 68 | // parse the ca item |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 69 | auto caItem = INFO::decodeClientConfigFromContent(reply.getContent()); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 70 | |
| 71 | // verify the probe Data's sig |
| 72 | if (!security::verifySignature(reply, caItem.m_anchor)) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 73 | _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri()); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 79 | void |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 80 | ClientModule::addCaFromInfoResponse(const Data& reply) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 81 | { |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 82 | const Block& contentBlock = reply.getContent(); |
| 83 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 84 | // parse the ca item |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 85 | auto caItem = INFO::decodeClientConfigFromContent(contentBlock); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 86 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 87 | // update the local config |
| 88 | bool findItem = false; |
| 89 | for (auto& item : m_config.m_caItems) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 90 | if (item.m_caPrefix == caItem.m_caPrefix) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 91 | findItem = true; |
| 92 | item = caItem; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 95 | if (!findItem) { |
| 96 | m_config.m_caItems.push_back(caItem); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 97 | } |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 100 | shared_ptr<Interest> |
| 101 | ClientModule::generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 102 | { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 103 | Name interestName = ca.m_caPrefix; |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 104 | interestName.append("CA").append("PROBE"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 105 | auto interest = make_shared<Interest>(interestName); |
| 106 | interest->setMustBeFresh(true); |
| 107 | interest->setCanBePrefix(false); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 108 | interest->setApplicationParameters( |
| 109 | PROBE::encodeApplicationParametersFromProbeInfo(ca, probeInfo) |
| 110 | ); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 111 | |
| 112 | // update local state |
| 113 | m_ca = ca; |
| 114 | return interest; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | ClientModule::onProbeResponse(const Data& reply) |
| 119 | { |
| 120 | if (!security::verifySignature(reply, m_ca.m_anchor)) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 121 | _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 122 | return; |
| 123 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 124 | |
| 125 | auto contentTLV = reply.getContent(); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 126 | contentTLV.parse(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 127 | |
| 128 | // read the available name and put it into the state |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 129 | if (contentTLV.get(tlv_probe_response).hasValue()) { |
| 130 | m_identityName.wireDecode(contentTLV.get(tlv_probe_response)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 131 | } |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 132 | else { |
| 133 | NDN_LOG_TRACE("The JSON_CA_NAME is empty."); |
| 134 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | shared_ptr<Interest> |
| 138 | ClientModule::generateNewInterest(const time::system_clock::TimePoint& notBefore, |
| 139 | const time::system_clock::TimePoint& notAfter, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 140 | const Name& identityName, const shared_ptr<Data>& probeToken) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | { |
| 142 | // Name requestedName = identityName; |
| 143 | if (!identityName.empty()) { // if identityName is not empty, find the corresponding CA |
| 144 | bool findCa = false; |
| 145 | for (const auto& caItem : m_config.m_caItems) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 146 | if (caItem.m_caPrefix.isPrefixOf(identityName)) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 147 | m_ca = caItem; |
| 148 | findCa = true; |
| 149 | } |
| 150 | } |
| 151 | if (!findCa) { // if cannot find, cannot proceed |
| 152 | return nullptr; |
| 153 | } |
| 154 | m_identityName = identityName; |
| 155 | } |
| 156 | else { // if identityName is empty, check m_identityName or generate a random name |
| 157 | if (!m_identityName.empty()) { |
| 158 | // do nothing |
| 159 | } |
| 160 | else { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 161 | NDN_LOG_TRACE("Randomly create a new name because m_identityName is empty and the param is empty."); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 162 | auto id = std::to_string(random::generateSecureWord64()); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 163 | m_identityName = m_ca.m_caPrefix; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | m_identityName.append(id); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // generate a newly key pair or use an existing key |
Zhiyi Zhang | 1013078 | 2018-02-01 18:28:49 -0800 | [diff] [blame] | 169 | const auto& pib = m_keyChain.getPib(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 170 | security::pib::Identity identity; |
Zhiyi Zhang | 1013078 | 2018-02-01 18:28:49 -0800 | [diff] [blame] | 171 | try { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 172 | identity = pib.getIdentity(m_identityName); |
Zhiyi Zhang | 1013078 | 2018-02-01 18:28:49 -0800 | [diff] [blame] | 173 | } |
| 174 | catch (const security::Pib::Error& e) { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 175 | identity = m_keyChain.createIdentity(m_identityName); |
| 176 | m_isNewlyCreatedIdentity = true; |
| 177 | m_isNewlyCreatedKey = true; |
| 178 | } |
| 179 | try { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 180 | m_key = identity.getDefaultKey(); |
Zhiyi Zhang | 1013078 | 2018-02-01 18:28:49 -0800 | [diff] [blame] | 181 | } |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 182 | catch (const security::Pib::Error& e) { |
| 183 | m_key = m_keyChain.createKey(identity); |
| 184 | m_isNewlyCreatedKey = true; |
| 185 | } |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 186 | |
| 187 | // generate certificate request |
| 188 | security::v2::Certificate certRequest; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 189 | certRequest.setName(Name(m_key.getName()).append("cert-request").appendVersion()); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 190 | certRequest.setContentType(tlv::ContentType_Key); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 191 | certRequest.setContent(m_key.getPublicKey().data(), m_key.getPublicKey().size()); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 192 | SignatureInfo signatureInfo; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 193 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
| 194 | m_keyChain.sign(certRequest, signingByKey(m_key.getName()).setSignatureInfo(signatureInfo)); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 195 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 196 | // generate Interest packet |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 197 | Name interestName = m_ca.m_caPrefix; |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 198 | interestName.append("CA").append("NEW"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 199 | auto interest = make_shared<Interest>(interestName); |
| 200 | interest->setMustBeFresh(true); |
| 201 | interest->setCanBePrefix(false); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 202 | interest->setApplicationParameters( |
| 203 | NEW::encodeApplicationParameters(m_ecdh.getBase64PubKey(), certRequest, probeToken) |
| 204 | ); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 205 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 206 | // sign the Interest packet |
| 207 | m_keyChain.sign(*interest, signingByKey(m_key.getName())); |
| 208 | return interest; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 211 | std::list<std::string> |
| 212 | ClientModule::onNewResponse(const Data& reply) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 213 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 214 | if (!security::verifySignature(reply, m_ca.m_anchor)) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 215 | _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 216 | return std::list<std::string>(); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 217 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 218 | auto contentTLV = reply.getContent(); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 219 | contentTLV.parse(); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 220 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 221 | // ECDH |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 222 | const auto& peerKeyBase64Str = readString(contentTLV.get(tlv_ecdh_pub)); |
| 223 | const auto& saltStr = readString(contentTLV.get(tlv_salt)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 224 | uint64_t saltInt = std::stoull(saltStr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 225 | m_ecdh.deriveSecret(peerKeyBase64Str); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 226 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 227 | // HKDF |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 228 | hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen, |
| 229 | (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey)); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 230 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 231 | // update state |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 232 | m_status = readNonNegativeInteger(contentTLV.get(tlv_status)); |
| 233 | m_requestId = readString(contentTLV.get(tlv_request_id)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 234 | m_challengeList.clear(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 235 | for (auto const& element : contentTLV.elements()) { |
| 236 | if (element.type() == tlv_challenge) { |
| 237 | m_challengeList.push_back(readString(element)); |
| 238 | } |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 239 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 240 | return m_challengeList; |
| 241 | } |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 242 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 243 | shared_ptr<Interest> |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 244 | ClientModule::generateChallengeInterest(const Block& challengeRequest) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 245 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 246 | challengeRequest.parse(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 247 | m_challengeType = readString(challengeRequest.get(tlv_selected_challenge)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 248 | |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 249 | Name interestName = m_ca.m_caPrefix; |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 250 | interestName.append("CA").append("CHALLENGE").append(m_requestId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 251 | auto interest = make_shared<Interest>(interestName); |
| 252 | interest->setMustBeFresh(true); |
| 253 | interest->setCanBePrefix(false); |
| 254 | |
| 255 | // encrypt the Interest parameters |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame^] | 256 | auto paramBlock = encodeBlockWithAesGcm128(tlv::ApplicationParameters, m_aesKey, |
| 257 | challengeRequest.value(), challengeRequest.value_size(), (const uint8_t*)"test", strlen("test")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 258 | interest->setApplicationParameters(paramBlock); |
| 259 | |
| 260 | m_keyChain.sign(*interest, signingByKey(m_key.getName())); |
| 261 | return interest; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 265 | ClientModule::onChallengeResponse(const Data& reply) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 266 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 267 | if (!security::verifySignature(reply, m_ca.m_anchor)) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 268 | _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri()); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 271 | auto result = decodeBlockWithAesGcm128(reply.getContent(), m_aesKey, (const uint8_t*)"test", strlen("test")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 272 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 273 | Block contentTLV = makeBinaryBlock(tlv_encrypted_payload, result.data(), result.size()); |
| 274 | contentTLV.parse(); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 275 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 276 | // update state |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 277 | m_status = readNonNegativeInteger(contentTLV.get(tlv_status)); |
| 278 | m_challengeStatus = readString(contentTLV.get(tlv_challenge_status)); |
| 279 | m_remainingTries = readNonNegativeInteger(contentTLV.get(tlv_remaining_tries)); |
| 280 | m_freshBefore = time::system_clock::now() + |
| 281 | time::seconds(readNonNegativeInteger(contentTLV.get(tlv_remaining_time))); |
| 282 | |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame^] | 283 | if (contentTLV.find(tlv_issued_cert_name) != contentTLV.elements_end()) { |
| 284 | Block issuedCertNameBlock = contentTLV.get(tlv_issued_cert_name); |
| 285 | issuedCertNameBlock.parse(); |
| 286 | m_issuedCertName.wireDecode(issuedCertNameBlock.get(tlv::Name)); |
| 287 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 288 | } |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 289 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 290 | shared_ptr<Interest> |
| 291 | ClientModule::generateDownloadInterest() |
| 292 | { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 293 | Name interestName = m_ca.m_caPrefix; |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 294 | interestName.append("CA").append("DOWNLOAD").append(m_requestId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 295 | auto interest = make_shared<Interest>(interestName); |
| 296 | interest->setMustBeFresh(true); |
| 297 | interest->setCanBePrefix(false); |
| 298 | return interest; |
| 299 | } |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 300 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 301 | shared_ptr<Interest> |
| 302 | ClientModule::generateCertFetchInterest() |
| 303 | { |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 304 | Name interestName = m_issuedCertName; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 305 | auto interest = make_shared<Interest>(interestName); |
| 306 | interest->setMustBeFresh(true); |
| 307 | interest->setCanBePrefix(false); |
| 308 | return interest; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 309 | } |
| 310 | |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 311 | void |
| 312 | ClientModule::onCertFetchResponse(const Data& reply) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 313 | { |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 314 | try { |
| 315 | security::v2::Certificate cert(reply.getContent().blockFromValue()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 316 | m_keyChain.addCertificate(m_key, cert); |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 317 | _LOG_TRACE("Fetched and installed the cert " << cert.getName()); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 318 | } |
| 319 | catch (const std::exception& e) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 320 | _LOG_ERROR("Cannot add replied certificate into the keychain " << e.what()); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 321 | return nullptr; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 322 | } |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 325 | JsonSection |
| 326 | ClientModule::getJsonFromData(const Data& data) |
| 327 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 328 | std::istringstream ss(encoding::readString(data.getContent())); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 329 | JsonSection json; |
| 330 | boost::property_tree::json_parser::read_json(ss, json); |
| 331 | return json; |
| 332 | } |
| 333 | |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 334 | std::vector<std::string> |
| 335 | ClientModule::parseProbeComponents(const std::string& probe) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 336 | { |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 337 | std::vector<std::string> components; |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 338 | std::string delimiter = ":"; |
| 339 | size_t last = 0; |
| 340 | size_t next = 0; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 341 | while ((next = probe.find(delimiter, last)) != std::string::npos) { |
| 342 | components.push_back(probe.substr(last, next - last)); |
| 343 | last = next + 1; |
| 344 | } |
| 345 | components.push_back(probe.substr(last)); |
| 346 | return components; |
| 347 | } |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 348 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 349 | JsonSection |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 350 | ClientModule::genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo) |
| 351 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 352 | JsonSection root; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 353 | std::vector<std::string> fields = parseProbeComponents(ca.m_probe); |
| 354 | std::vector<std::string> arguments = parseProbeComponents(probeInfo);; |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 355 | |
| 356 | if (arguments.size() != fields.size()) { |
| 357 | BOOST_THROW_EXCEPTION(Error("Error in genProbeRequestJson: argument list does not match field list in the config file.")); |
| 358 | } |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 359 | for (size_t i = 0; i < fields.size(); ++i) { |
| 360 | root.put(fields.at(i), arguments.at(i)); |
| 361 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 362 | return root; |
| 363 | } |
| 364 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 365 | JsonSection |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 366 | ClientModule::genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest, |
| 367 | const shared_ptr<Data>& probeToken) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 368 | { |
| 369 | JsonSection root; |
| 370 | std::stringstream ss; |
| 371 | try { |
| 372 | security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size()) |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 373 | >> security::transform::base64Encode(false) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 374 | >> security::transform::streamSink(ss); |
| 375 | } |
| 376 | catch (const security::transform::Error& e) { |
| 377 | _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what()); |
| 378 | return root; |
| 379 | } |
| 380 | root.put(JSON_CLIENT_ECDH, ecdhPub); |
| 381 | root.put(JSON_CLIENT_CERT_REQ, ss.str()); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 382 | if (probeToken != nullptr) { |
| 383 | // clear the stringstream |
| 384 | ss.str(""); |
| 385 | ss.clear(); |
| 386 | // transform the probe data into a base64 string |
| 387 | try { |
| 388 | security::transform::bufferSource(probeToken->wireEncode().wire(), probeToken->wireEncode().size()) |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 389 | >> security::transform::base64Encode(false) |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 390 | >> security::transform::streamSink(ss); |
| 391 | } |
| 392 | catch (const security::transform::Error& e) { |
| 393 | _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what()); |
| 394 | return root; |
| 395 | } |
| 396 | // add the token into the JSON |
| 397 | root.put("probe-token", ss.str()); |
| 398 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 399 | return root; |
| 400 | } |
| 401 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 402 | Block |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 403 | ClientModule::paramFromJson(const JsonSection& json) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 404 | { |
| 405 | std::stringstream ss; |
| 406 | boost::property_tree::write_json(ss, json); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 407 | return makeStringBlock(ndn::tlv::ApplicationParameters, ss.str()); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | } // namespace ndncert |
| 411 | } // namespace ndn |