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