TLV encoding to replace JSON format message
diff --git a/src/protocol-detail/challenge.cpp b/src/protocol-detail/challenge.cpp
new file mode 100644
index 0000000..f1fed27
--- /dev/null
+++ b/src/protocol-detail/challenge.cpp
@@ -0,0 +1,44 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "challenge.hpp"
+#include "../ndncert-common.hpp"
+#include "../certificate-request.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+Block
+CHALLENGE::encodeDataPayload(const CertificateRequest& request)
+{
+ Block response = makeEmptyBlock(tlv_encrypted_payload);
+ makeNonNegativeIntegerBlock(tlv_status, request.m_status);
+ makeStringBlock(tlv_challenge_status, request.m_challengeStatus);
+ makeNonNegativeIntegerBlock(tlv_remaining_tries, request.m_remainingTries);
+ makeNonNegativeIntegerBlock(tlv_remaining_time, request.m_remainingTime);
+ response.parse();
+ return response;
+}
+
+} // namespace ndncert
+} // namespace ndn
+
+
+
diff --git a/src/protocol-detail/challenge.hpp b/src/protocol-detail/challenge.hpp
new file mode 100644
index 0000000..1ff74fd
--- /dev/null
+++ b/src/protocol-detail/challenge.hpp
@@ -0,0 +1,39 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_PROTOCOL_DETAIL_CHALLENGE_HPP
+#define NDNCERT_PROTOCOL_DETAIL_CHALLENGE_HPP
+
+#include "../certificate-request.hpp"
+#include <ndn-cxx/encoding/block.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+class CHALLENGE {
+public:
+ static Block
+ encodeDataPayload(const CertificateRequest& request);
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_PROTOCOL_DETAIL_HPP
\ No newline at end of file
diff --git a/src/protocol-detail/info.cpp b/src/protocol-detail/info.cpp
index 93bd7b7..1463e04 100644
--- a/src/protocol-detail/info.cpp
+++ b/src/protocol-detail/info.cpp
@@ -28,7 +28,14 @@
{
auto content = makeEmptyBlock(tlv::Content);
content.push_back(makeNestedBlock(tlv_ca_prefix, caConfig.m_caPrefix));
- content.push_back(makeStringBlock(tlv_ca_info, caConfig.m_caInfo));
+ std::string caInfo = "";
+ if (caConfig.m_caInfo == "") {
+ caInfo = "Issued by " + certificate.getSignature().getKeyLocator().getName().toUri();
+ } else {
+ caInfo = caConfig.m_caInfo;
+ }
+ content.push_back(makeStringBlock(tlv_ca_info, caInfo));
+
for (const auto& key : caConfig.m_probeParameterKeys) {
content.push_back(makeStringBlock(tlv_parameter_key, key));
}
diff --git a/src/protocol-detail/info.hpp b/src/protocol-detail/info.hpp
index 89a907b..9c190f2 100644
--- a/src/protocol-detail/info.hpp
+++ b/src/protocol-detail/info.hpp
@@ -18,8 +18,8 @@
* See AUTHORS.md for complete list of ndncert authors and contributors.
*/
-#ifndef NDNCERT_PROTOCOL_DETAIL_HPP
-#define NDNCERT_PROTOCOL_DETAIL_HPP
+#ifndef NDNCERT_PROTOCOL_DETAIL_INFO_HPP
+#define NDNCERT_PROTOCOL_DETAIL_INFO_HPP
#include "../ca-config.hpp"
#include "../client-config.hpp"
diff --git a/src/protocol-detail/new.cpp b/src/protocol-detail/new.cpp
new file mode 100644
index 0000000..27bbf0a
--- /dev/null
+++ b/src/protocol-detail/new.cpp
@@ -0,0 +1,75 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "new.hpp"
+#include "../logging.hpp"
+#include "../ndncert-common.hpp"
+
+#include <ndn-cxx/security/transform/base64-encode.hpp>
+#include <ndn-cxx/security/transform/buffer-source.hpp>
+#include <ndn-cxx/security/transform/stream-sink.hpp>
+#include <ndn-cxx/util/logger.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+_LOG_INIT(ndncert.client);
+
+Block
+NEW::encodeApplicationParameters(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
+ const shared_ptr<Data>& probeToken)
+{
+ Block request = makeEmptyBlock(tlv::ApplicationParameters);
+ std::stringstream ss;
+ try {
+ security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
+ >> security::transform::base64Encode(false)
+ >> security::transform::streamSink(ss);
+ }
+ catch (const security::transform::Error& e) {
+ _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
+ return request;
+ }
+
+ request.push_back(makeStringBlock(tlv_ecdh_pub, ecdhPub));
+ request.push_back(makeNestedBlock(tlv_cert_request, certRequest));
+ request.parse();
+ return request;
+}
+
+Block
+NEW::encodeDataContent(const std::string& ecdhKey, const std::string& salt,
+ const CertificateRequest& request,
+ const std::list<std::string>& challenges)
+{
+ Block response = makeEmptyBlock(tlv::Content);
+ response.push_back(makeStringBlock(tlv_ecdh_pub, ecdhKey));
+ response.push_back(makeStringBlock(tlv_salt, salt));
+ response.push_back(makeStringBlock(tlv_request_id, request.m_requestId));
+ response.push_back(makeNonNegativeIntegerBlock(tlv_status, request.m_status));
+ for (const auto& entry: challenges) {
+ response.push_back(makeStringBlock(tlv_challenge, entry));
+ }
+ response.parse();
+ return response;
+}
+
+} // namespace ndncert
+} // namespace ndn
\ No newline at end of file
diff --git a/src/protocol-detail/new.hpp b/src/protocol-detail/new.hpp
new file mode 100644
index 0000000..ae95a15
--- /dev/null
+++ b/src/protocol-detail/new.hpp
@@ -0,0 +1,46 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_PROTOCOL_DETAIL_NEW_HPP
+#define NDNCERT_PROTOCOL_DETAIL_NEW_HPP
+
+#include "../certificate-request.hpp"
+#include "ndn-cxx/encoding/block.hpp"
+#include <ndn-cxx/security/v2/certificate.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+class NEW {
+public:
+ static Block
+ encodeApplicationParameters(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
+ const shared_ptr<Data>& probeToken);
+
+ static Block
+ encodeDataContent(const std::string& ecdhKey, const std::string& salt,
+ const CertificateRequest& request,
+ const std::list<std::string>& challenges);
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_PROTOCOL_DETAIL_HPP
\ No newline at end of file
diff --git a/src/protocol-detail/probe.cpp b/src/protocol-detail/probe.cpp
new file mode 100644
index 0000000..b8034b1
--- /dev/null
+++ b/src/protocol-detail/probe.cpp
@@ -0,0 +1,99 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "probe.hpp"
+#include "boost/throw_exception.hpp"
+#include "../logging.hpp"
+#include <ndn-cxx/encoding/tlv.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+// For Client
+std::vector<std::string>
+PROBE::parseProbeComponents(const std::string& probe)
+{
+ std::vector<std::string> components;
+ std::string delimiter = ":";
+ size_t last = 0;
+ size_t next = 0;
+ while ((next = probe.find(delimiter, last)) != std::string::npos) {
+ components.push_back(probe.substr(last, next - last));
+ last = next + 1;
+ }
+ components.push_back(probe.substr(last));
+ return components;
+}
+
+Block
+PROBE::encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo)
+{
+ auto content = makeEmptyBlock(tlv::ApplicationParameters);
+
+ std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
+ std::vector<std::string> arguments = parseProbeComponents(probeInfo);;
+
+ if (arguments.size() != fields.size()) {
+ BOOST_THROW_EXCEPTION(Error("Error in genProbeRequestJson: argument list does not match field list in the config file."));
+ }
+
+ for (size_t i = 0; i < fields.size(); ++i) {
+ content.push_back(
+ makeStringBlock(tlv_parameter_key, fields.at(i))
+ );
+ content.push_back(
+ makeStringBlock(tlv_parameter_value, arguments.at(i))
+ );
+ }
+ content.parse();
+ return content;
+}
+
+// For CA
+Block
+PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV)
+{
+ std::vector<std::string> fields;
+ std::string delimiter = ":";
+ size_t last = 0;
+ size_t next = 0;
+ while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
+ fields.push_back(m_probe.substr(last, next - last));
+ last = next + 1;
+ }
+ fields.push_back(m_probe.substr(last));
+
+ Block content = makeEmptyBlock(tlv::Content);
+
+ // TODO: Currently have no mechanism to utilize the given params to determine name
+ //for (size_t i = 0; i < fields.size(); ++i) {
+ // root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
+ //}
+
+ content.push_back(makeNestedBlock(tlv_probe_response, identifier));
+
+ // TODO: Must be determined based on CA config
+ content.push_back(makeEmptyBlock(tlv_allow_longer_name));
+ content.parse();
+ return content;
+}
+
+} // namespace ndncert
+} // namespace ndn
\ No newline at end of file
diff --git a/src/protocol-detail/probe.hpp b/src/protocol-detail/probe.hpp
new file mode 100644
index 0000000..6fbca8e
--- /dev/null
+++ b/src/protocol-detail/probe.hpp
@@ -0,0 +1,56 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_PROTOCOL_DETAIL_PROBE_HPP
+#define NDNCERT_PROTOCOL_DETAIL_PROBE_HPP
+
+#include "../ca-config.hpp"
+#include "../client-config.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+class PROBE {
+
+public:
+ /**
+ * @brief Error that can be thrown from PROBE
+ */
+ class Error : public std::runtime_error
+ {
+ public:
+ using std::runtime_error::runtime_error;
+ };
+
+public:
+ static std::vector<std::string>
+ parseProbeComponents(const std::string& probe);
+
+ static Block
+ encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo);
+
+ static Block
+ encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV);
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_PROTOCOL_DETAIL_HPP
\ No newline at end of file