Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -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 | 8617a79 | 2017-01-17 16:45:56 -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 | |
Zhiyi Zhang | b6fab0f | 2017-09-21 16:26:27 -0700 | [diff] [blame] | 21 | #ifndef NDNCERT_NDNCERT_COMMON_HPP |
| 22 | #define NDNCERT_NDNCERT_COMMON_HPP |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 23 | |
| 24 | #include "ndncert-config.hpp" |
| 25 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 26 | #ifdef HAVE_TESTS |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 27 | #define VIRTUAL_WITH_TESTS virtual |
| 28 | #define PUBLIC_WITH_TESTS_ELSE_PROTECTED public |
| 29 | #define PUBLIC_WITH_TESTS_ELSE_PRIVATE public |
| 30 | #define PROTECTED_WITH_TESTS_ELSE_PRIVATE protected |
| 31 | #else |
| 32 | #define VIRTUAL_WITH_TESTS |
| 33 | #define PUBLIC_WITH_TESTS_ELSE_PROTECTED protected |
| 34 | #define PUBLIC_WITH_TESTS_ELSE_PRIVATE private |
| 35 | #define PROTECTED_WITH_TESTS_ELSE_PRIVATE private |
| 36 | #endif |
| 37 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 38 | #include <cstddef> |
| 39 | #include <cstdint> |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 40 | #include <tuple> |
| 41 | #include <ndn-cxx/encoding/tlv.hpp> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 42 | #include <ndn-cxx/data.hpp> |
| 43 | #include <ndn-cxx/encoding/block.hpp> |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame^] | 44 | #include <ndn-cxx/encoding/block-helpers.hpp> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 45 | #include <ndn-cxx/face.hpp> |
| 46 | #include <ndn-cxx/interest.hpp> |
| 47 | #include <ndn-cxx/link.hpp> |
| 48 | #include <ndn-cxx/lp/nack.hpp> |
| 49 | #include <ndn-cxx/name.hpp> |
| 50 | #include <ndn-cxx/security/key-chain.hpp> |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 51 | #include <ndn-cxx/security/certificate.hpp> |
Zhiyi Zhang | 523f0c2 | 2020-09-29 14:19:20 -0700 | [diff] [blame] | 52 | #include <ndn-cxx/util/logger.hpp> |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 53 | #include <boost/algorithm/string.hpp> |
| 54 | #include <boost/assert.hpp> |
| 55 | #include <boost/noncopyable.hpp> |
| 56 | #include <boost/throw_exception.hpp> |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 57 | #include <boost/property_tree/info_parser.hpp> |
| 58 | #include <boost/property_tree/json_parser.hpp> |
| 59 | #include <boost/property_tree/ptree.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 60 | |
| 61 | namespace ndn { |
| 62 | namespace ndncert { |
| 63 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 64 | using boost::noncopyable; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 65 | using ndn::Block; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 66 | using ndn::Data; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 67 | using ndn::Interest; |
| 68 | using ndn::make_unique; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 69 | using ndn::Name; |
| 70 | using ndn::PartialName; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 71 | using ndn::time::system_clock; |
| 72 | using ndn::time::toUnixTimestamp; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 73 | using std::bind; |
| 74 | using std::enable_shared_from_this; |
| 75 | using std::function; |
| 76 | using std::make_shared; |
| 77 | using std::shared_ptr; |
| 78 | using std::size_t; |
| 79 | using std::unique_ptr; |
| 80 | using std::weak_ptr; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 81 | |
Zhiyi Zhang | 523f0c2 | 2020-09-29 14:19:20 -0700 | [diff] [blame] | 82 | #define _LOG_INIT(name) NDN_LOG_INIT(ndncert.name) |
| 83 | #define _LOG_DEBUG(x) NDN_LOG_DEBUG(__FILE__ << ":" << __LINE__ << ":" << " " << x) |
| 84 | #define _LOG_TRACE(x) NDN_LOG_TRACE(__FILE__ << ":" << __LINE__ << ":" << " " << x) |
| 85 | #define _LOG_ERROR(x) NDN_LOG_ERROR(x) |
| 86 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 87 | enum : uint32_t { |
| 88 | tlv_ca_prefix = 129, |
| 89 | tlv_ca_info = 131, |
| 90 | tlv_parameter_key = 133, |
| 91 | tlv_parameter_value = 135, |
| 92 | tlv_ca_certificate = 137, |
| 93 | tlv_max_validity_period = 139, |
| 94 | tlv_probe_response = 141, |
Zhiyi Zhang | 38e1a9e | 2020-10-01 15:12:15 -0700 | [diff] [blame] | 95 | tlv_max_suffix_length = 143, |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 96 | tlv_ecdh_pub = 145, |
| 97 | tlv_cert_request = 147, |
| 98 | tlv_salt = 149, |
| 99 | tlv_request_id = 151, |
| 100 | tlv_challenge = 153, |
| 101 | tlv_status = 155, |
| 102 | tlv_initialization_vector = 157, |
| 103 | tlv_encrypted_payload = 159, |
| 104 | tlv_selected_challenge = 161, |
| 105 | tlv_challenge_status = 163, |
| 106 | tlv_remaining_tries = 165, |
| 107 | tlv_remaining_time = 167, |
| 108 | tlv_issued_cert_name = 169, |
| 109 | tlv_error_code = 171, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 110 | tlv_error_info = 173, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 111 | tlv_authentication_tag = 175, |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 112 | tlv_cert_to_revoke = 177, |
| 113 | tlv_probe_redirect = 179 |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // Parse CA Configuration file |
| 117 | const std::string CONFIG_CA_PREFIX = "ca-prefix"; |
| 118 | const std::string CONFIG_CA_INFO = "ca-info"; |
| 119 | const std::string CONFIG_MAX_VALIDITY_PERIOD = "max-validity-period"; |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 120 | const std::string CONFIG_MAX_SUFFIX_LENGTH = "max-suffix-length"; |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 121 | const std::string CONFIG_PROBE_PARAMETERS = "probe-parameters"; |
| 122 | const std::string CONFIG_PROBE_PARAMETER = "probe-parameter-key"; |
| 123 | const std::string CONFIG_SUPPORTED_CHALLENGES = "supported-challenges"; |
| 124 | const std::string CONFIG_CHALLENGE = "challenge"; |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 125 | const std::string CONFIG_CERTIFICATE = "certificate"; |
| 126 | const std::string CONFIG_REDIRECTION = "redirect-to"; |
tylerliu | 8d9e712 | 2020-10-06 18:57:06 -0700 | [diff] [blame] | 127 | const std::string CONFIG_NAME_ASSIGNMENT = "name-assignment"; |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 128 | typedef boost::property_tree::ptree JsonSection; |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 129 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 130 | // JSON and string translation for Config file parsing |
| 131 | std::string |
| 132 | convertJson2String(const JsonSection& json); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 133 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 134 | JsonSection |
| 135 | convertString2Json(const std::string& jsonContent); |
| 136 | |
| 137 | // NDNCERT Request status enumeration |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 138 | enum class Status : uint16_t { |
| 139 | BEFORE_CHALLENGE = 0, |
| 140 | CHALLENGE = 1, |
| 141 | PENDING = 2, |
| 142 | SUCCESS = 3, |
| 143 | FAILURE = 4, |
| 144 | NOT_STARTED = 5, |
| 145 | ENDED = 6 |
| 146 | }; |
| 147 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 148 | // Convert request status to string |
| 149 | std::string |
| 150 | statusToString(Status status); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 151 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 152 | // NDNCERT error code |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 153 | enum class ErrorCode : uint16_t { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 154 | NO_ERROR = 0, |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 155 | BAD_INTEREST_FORMAT = 1, |
| 156 | BAD_PARAMETER_FORMAT = 2, |
| 157 | BAD_SIGNATURE = 3, |
| 158 | INVALID_PARAMETER = 4, |
| 159 | NAME_NOT_ALLOWED = 5, |
| 160 | BAD_VALIDITY_PERIOD = 6, |
| 161 | OUT_OF_TRIES = 7, |
| 162 | OUT_OF_TIME = 8, |
| 163 | NO_AVAILABLE_NAMES = 9 |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 166 | // Convert error code to string |
| 167 | std::string |
| 168 | errorCodeToString(ErrorCode code); |
| 169 | |
| 170 | // NDNCERT request type |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 171 | enum class RequestType : uint16_t { |
| 172 | NOTINITIALIZED = 0, |
| 173 | NEW = 1, |
| 174 | RENEW = 2, |
| 175 | REVOKE = 3 |
| 176 | }; |
| 177 | |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 178 | // Convert request type to string |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 179 | std::string |
Zhiyi Zhang | 4b11809 | 2020-10-10 10:28:38 -0700 | [diff] [blame] | 180 | requestTypeToString(RequestType type); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 181 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 182 | } // namespace ndncert |
| 183 | } // namespace ndn |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 184 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 185 | #endif // NDNCERT_NDNCERT_COMMON_HPP |