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 | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 38 | #include <boost/algorithm/string.hpp> |
| 39 | #include <boost/assert.hpp> |
| 40 | #include <boost/noncopyable.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 41 | #include <boost/throw_exception.hpp> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 42 | #include <cstddef> |
| 43 | #include <cstdint> |
| 44 | #include <ndn-cxx/data.hpp> |
| 45 | #include <ndn-cxx/encoding/block.hpp> |
| 46 | #include <ndn-cxx/face.hpp> |
| 47 | #include <ndn-cxx/interest.hpp> |
| 48 | #include <ndn-cxx/link.hpp> |
| 49 | #include <ndn-cxx/lp/nack.hpp> |
| 50 | #include <ndn-cxx/name.hpp> |
| 51 | #include <ndn-cxx/security/key-chain.hpp> |
| 52 | #include <ndn-cxx/security/v2/certificate.hpp> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 53 | |
| 54 | namespace ndn { |
| 55 | namespace ndncert { |
| 56 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 57 | using boost::noncopyable; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 58 | using ndn::Block; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 59 | using ndn::Data; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 60 | using ndn::Interest; |
| 61 | using ndn::make_unique; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 62 | using ndn::Name; |
| 63 | using ndn::PartialName; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 64 | using ndn::time::system_clock; |
| 65 | using ndn::time::toUnixTimestamp; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 66 | using std::bind; |
| 67 | using std::enable_shared_from_this; |
| 68 | using std::function; |
| 69 | using std::make_shared; |
| 70 | using std::shared_ptr; |
| 71 | using std::size_t; |
| 72 | using std::unique_ptr; |
| 73 | using std::weak_ptr; |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 74 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 75 | enum : uint32_t { |
| 76 | tlv_ca_prefix = 129, |
| 77 | tlv_ca_info = 131, |
| 78 | tlv_parameter_key = 133, |
| 79 | tlv_parameter_value = 135, |
| 80 | tlv_ca_certificate = 137, |
| 81 | tlv_max_validity_period = 139, |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 82 | tlv_max_suffix_length = 177, |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 83 | tlv_probe_response = 141, |
| 84 | tlv_allow_longer_name = 143, |
| 85 | tlv_ecdh_pub = 145, |
| 86 | tlv_cert_request = 147, |
| 87 | tlv_salt = 149, |
| 88 | tlv_request_id = 151, |
| 89 | tlv_challenge = 153, |
| 90 | tlv_status = 155, |
| 91 | tlv_initialization_vector = 157, |
| 92 | tlv_encrypted_payload = 159, |
| 93 | tlv_selected_challenge = 161, |
| 94 | tlv_challenge_status = 163, |
| 95 | tlv_remaining_tries = 165, |
| 96 | tlv_remaining_time = 167, |
| 97 | tlv_issued_cert_name = 169, |
| 98 | tlv_error_code = 171, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 99 | tlv_error_info = 173, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 100 | tlv_authentication_tag = 175, |
tylerliu | 9a83135 | 2020-09-28 11:16:01 -0700 | [diff] [blame] | 101 | tlv_cert_to_revoke = 179 |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // Parse CA Configuration file |
| 105 | const std::string CONFIG_CA_PREFIX = "ca-prefix"; |
| 106 | const std::string CONFIG_CA_INFO = "ca-info"; |
| 107 | const std::string CONFIG_MAX_VALIDITY_PERIOD = "max-validity-period"; |
| 108 | const std::string CONFIG_PROBE_PARAMETERS = "probe-parameters"; |
| 109 | const std::string CONFIG_PROBE_PARAMETER = "probe-parameter-key"; |
| 110 | const std::string CONFIG_SUPPORTED_CHALLENGES = "supported-challenges"; |
| 111 | const std::string CONFIG_CHALLENGE = "challenge"; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 112 | const std::string CONFIG_MAX_SUFFIX_LENGTH = "max-suffix-length"; |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 113 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 114 | // // JSON format for Certificate Issuer (CA) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 115 | const std::string JSON_CA_NAME = "name"; |
| 116 | const std::string JSON_CA_CONFIG = "ca-config"; |
| 117 | const std::string JSON_CA_ECDH = "ecdh-pub"; |
| 118 | const std::string JSON_CA_SALT = "salt"; |
Zhiyi Zhang | ff4bcb6 | 2019-09-08 12:57:42 -0700 | [diff] [blame] | 119 | const std::string JSON_CA_REQUEST_ID = "request-id"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | const std::string JSON_CA_STATUS = "status"; |
| 121 | const std::string JSON_CA_CHALLENGES = "challenges"; |
| 122 | const std::string JSON_CA_CHALLENGE_ID = "challenge-id"; |
| 123 | const std::string JSON_CA_CERT_ID = "certificate-id"; |
| 124 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 125 | // // JSON format for Challenge Module |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 126 | const std::string JSON_CHALLENGE_STATUS = "challenge-status"; |
| 127 | const std::string JSON_CHALLENGE_REMAINING_TRIES = "remaining-tries"; |
| 128 | const std::string JSON_CHALLENGE_REMAINING_TIME = "remaining-time"; |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 129 | const std::string JSON_CHALLENGE_ISSUED_CERT_NAME = "issued-cert-name"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 130 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 131 | // // JSON format for Certificate Requester |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | const std::string JSON_CLIENT_PROBE_INFO = "probe-info"; |
| 133 | const std::string JSON_CLIENT_ECDH = "ecdh-pub"; |
| 134 | const std::string JSON_CLIENT_CERT_REQ = "cert-request"; |
| 135 | const std::string JSON_CLIENT_SELECTED_CHALLENGE = "selected-challenge"; |
tylerliu | 9a83135 | 2020-09-28 11:16:01 -0700 | [diff] [blame] | 136 | const std::string JSON_CLIENT_CERT_TO_REVOKE = "cert-to-revoke"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 137 | |
| 138 | // NDNCERT Status Enum |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 139 | enum class Status : uint16_t { |
| 140 | BEFORE_CHALLENGE = 0, |
| 141 | CHALLENGE = 1, |
| 142 | PENDING = 2, |
| 143 | SUCCESS = 3, |
| 144 | FAILURE = 4, |
| 145 | NOT_STARTED = 5, |
| 146 | ENDED = 6 |
| 147 | }; |
| 148 | |
| 149 | std::string statusToString(Status status); |
| 150 | |
| 151 | enum class Error : uint16_t { |
| 152 | BAD_INTEREST_FORMAT = 1, |
| 153 | BAD_PARAMETER_FORMAT = 2, |
| 154 | BAD_SIGNATURE = 3, |
| 155 | INVALID_PARAMETER = 4, |
| 156 | NAME_NOT_ALLOWED = 5, |
| 157 | BAD_VALIDITY_PERIOD = 6, |
| 158 | OUT_OF_TRIES = 7, |
| 159 | OUT_OF_TIME = 8, |
| 160 | NO_AVAILABLE_NAMES = 9 |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // Pre-defined challenge status |
| 164 | const std::string CHALLENGE_STATUS_SUCCESS = "success"; |
| 165 | const std::string CHALLENGE_STATUS_FAILURE_TIMEOUT = "failure-timeout"; |
| 166 | const std::string CHALLENGE_STATUS_FAILURE_MAXRETRY = "failure-max-retry"; |
| 167 | const std::string CHALLENGE_STATUS_UNKNOWN_CHALLENGE = "unknown-challenge"; |
| 168 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 169 | } // namespace ndncert |
| 170 | } // namespace ndn |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 171 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 172 | #endif // NDNCERT_NDNCERT_COMMON_HPP |