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