blob: c680923e153249d5232ae1b351bddaed680bdb48 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 *
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 Zhangb6fab0f2017-09-21 16:26:27 -070021#ifndef NDNCERT_NDNCERT_COMMON_HPP
22#define NDNCERT_NDNCERT_COMMON_HPP
Zhiyi Zhang8617a792017-01-17 16:45:56 -080023
24#include "ndncert-config.hpp"
25
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080026#ifdef HAVE_TESTS
Zhiyi Zhang8617a792017-01-17 16:45:56 -080027#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 Zhang48f23782020-09-28 12:11:24 -070038#include <cstddef>
39#include <cstdint>
Zhiyi Zhange537dd52020-10-01 18:02:24 -070040#include <tuple>
41#include <ndn-cxx/encoding/tlv.hpp>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070042#include <ndn-cxx/data.hpp>
43#include <ndn-cxx/encoding/block.hpp>
44#include <ndn-cxx/face.hpp>
45#include <ndn-cxx/interest.hpp>
46#include <ndn-cxx/link.hpp>
47#include <ndn-cxx/lp/nack.hpp>
48#include <ndn-cxx/name.hpp>
49#include <ndn-cxx/security/key-chain.hpp>
50#include <ndn-cxx/security/v2/certificate.hpp>
Zhiyi Zhang523f0c22020-09-29 14:19:20 -070051#include <ndn-cxx/util/logger.hpp>
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070052#include <boost/algorithm/string.hpp>
53#include <boost/assert.hpp>
54#include <boost/noncopyable.hpp>
55#include <boost/throw_exception.hpp>
Zhiyi Zhanga749f442020-09-29 17:19:51 -070056#include <boost/property_tree/info_parser.hpp>
57#include <boost/property_tree/json_parser.hpp>
58#include <boost/property_tree/ptree.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080059
60namespace ndn {
61namespace ndncert {
62
Zhiyi Zhang8617a792017-01-17 16:45:56 -080063using boost::noncopyable;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070064using ndn::Block;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080065using ndn::Data;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070066using ndn::Interest;
67using ndn::make_unique;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080068using ndn::Name;
69using ndn::PartialName;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080070using ndn::time::system_clock;
71using ndn::time::toUnixTimestamp;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070072using std::bind;
73using std::enable_shared_from_this;
74using std::function;
75using std::make_shared;
76using std::shared_ptr;
77using std::size_t;
78using std::unique_ptr;
79using std::weak_ptr;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080080
Zhiyi Zhang523f0c22020-09-29 14:19:20 -070081#define _LOG_INIT(name) NDN_LOG_INIT(ndncert.name)
82#define _LOG_DEBUG(x) NDN_LOG_DEBUG(__FILE__ << ":" << __LINE__ << ":" << " " << x)
83#define _LOG_TRACE(x) NDN_LOG_TRACE(__FILE__ << ":" << __LINE__ << ":" << " " << x)
84#define _LOG_ERROR(x) NDN_LOG_ERROR(x)
85
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070086enum : uint32_t {
87 tlv_ca_prefix = 129,
88 tlv_ca_info = 131,
89 tlv_parameter_key = 133,
90 tlv_parameter_value = 135,
91 tlv_ca_certificate = 137,
92 tlv_max_validity_period = 139,
93 tlv_probe_response = 141,
Zhiyi Zhang38e1a9e2020-10-01 15:12:15 -070094 tlv_max_suffix_length = 143,
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070095 tlv_ecdh_pub = 145,
96 tlv_cert_request = 147,
97 tlv_salt = 149,
98 tlv_request_id = 151,
99 tlv_challenge = 153,
100 tlv_status = 155,
101 tlv_initialization_vector = 157,
102 tlv_encrypted_payload = 159,
103 tlv_selected_challenge = 161,
104 tlv_challenge_status = 163,
105 tlv_remaining_tries = 165,
106 tlv_remaining_time = 167,
107 tlv_issued_cert_name = 169,
108 tlv_error_code = 171,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700109 tlv_error_info = 173,
tylerliu182bc532020-09-25 01:54:45 -0700110 tlv_authentication_tag = 175,
Zhiyi Zhange537dd52020-10-01 18:02:24 -0700111 tlv_cert_to_revoke = 177,
112 tlv_probe_redirect = 179
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700113};
114
115// Parse CA Configuration file
116const std::string CONFIG_CA_PREFIX = "ca-prefix";
117const std::string CONFIG_CA_INFO = "ca-info";
118const std::string CONFIG_MAX_VALIDITY_PERIOD = "max-validity-period";
Zhiyi Zhangfde50112020-10-01 16:36:33 -0700119const std::string CONFIG_MAX_SUFFIX_LENGTH = "max-suffix-length";
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700120const std::string CONFIG_PROBE_PARAMETERS = "probe-parameters";
121const std::string CONFIG_PROBE_PARAMETER = "probe-parameter-key";
122const std::string CONFIG_SUPPORTED_CHALLENGES = "supported-challenges";
123const std::string CONFIG_CHALLENGE = "challenge";
Zhiyi Zhangfde50112020-10-01 16:36:33 -0700124const std::string CONFIG_CERTIFICATE = "certificate";
125const std::string CONFIG_REDIRECTION = "redirect-to";
126
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700127
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128// NDNCERT Status Enum
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700129enum class Status : uint16_t {
130 BEFORE_CHALLENGE = 0,
131 CHALLENGE = 1,
132 PENDING = 2,
133 SUCCESS = 3,
134 FAILURE = 4,
135 NOT_STARTED = 5,
136 ENDED = 6
137};
138
139std::string statusToString(Status status);
140
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700141enum class ErrorCode : uint16_t {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700142 NO_ERROR = 0,
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700143 BAD_INTEREST_FORMAT = 1,
144 BAD_PARAMETER_FORMAT = 2,
145 BAD_SIGNATURE = 3,
146 INVALID_PARAMETER = 4,
147 NAME_NOT_ALLOWED = 5,
148 BAD_VALIDITY_PERIOD = 6,
149 OUT_OF_TRIES = 7,
150 OUT_OF_TIME = 8,
151 NO_AVAILABLE_NAMES = 9
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700152};
153
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700154enum class RequestType : uint16_t {
155 NOTINITIALIZED = 0,
156 NEW = 1,
157 RENEW = 2,
158 REVOKE = 3
159};
160
161std::string requestTypeToString(RequestType type);
162
tylerliu36d97f52020-09-30 22:32:54 -0700163std::string errorCodeToString(ErrorCode code);
164
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700165typedef boost::property_tree::ptree JsonSection;
166
167std::string
168convertJson2String(const JsonSection& json);
169
170JsonSection
171convertString2Json(const std::string& jsonContent);
172
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700173} // namespace ndncert
174} // namespace ndn
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800175
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700176#endif // NDNCERT_NDNCERT_COMMON_HPP