blob: 73060840569685c7dae7c5434495b1567c0d3b9c [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>
40#include <ndn-cxx/data.hpp>
41#include <ndn-cxx/encoding/block.hpp>
42#include <ndn-cxx/face.hpp>
43#include <ndn-cxx/interest.hpp>
44#include <ndn-cxx/link.hpp>
45#include <ndn-cxx/lp/nack.hpp>
46#include <ndn-cxx/name.hpp>
47#include <ndn-cxx/security/key-chain.hpp>
48#include <ndn-cxx/security/v2/certificate.hpp>
Zhiyi Zhang523f0c22020-09-29 14:19:20 -070049#include <ndn-cxx/util/logger.hpp>
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070050#include <boost/algorithm/string.hpp>
51#include <boost/assert.hpp>
52#include <boost/noncopyable.hpp>
53#include <boost/throw_exception.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080054
55namespace ndn {
56namespace ndncert {
57
Zhiyi Zhang8617a792017-01-17 16:45:56 -080058using boost::noncopyable;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070059using ndn::Block;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080060using ndn::Data;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070061using ndn::Interest;
62using ndn::make_unique;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080063using ndn::Name;
64using ndn::PartialName;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080065using ndn::time::system_clock;
66using ndn::time::toUnixTimestamp;
Zhiyi Zhang48f23782020-09-28 12:11:24 -070067using std::bind;
68using std::enable_shared_from_this;
69using std::function;
70using std::make_shared;
71using std::shared_ptr;
72using std::size_t;
73using std::unique_ptr;
74using std::weak_ptr;
Zhiyi Zhang8617a792017-01-17 16:45:56 -080075
Zhiyi Zhang523f0c22020-09-29 14:19:20 -070076#define _LOG_INIT(name) NDN_LOG_INIT(ndncert.name)
77#define _LOG_DEBUG(x) NDN_LOG_DEBUG(__FILE__ << ":" << __LINE__ << ":" << " " << x)
78#define _LOG_TRACE(x) NDN_LOG_TRACE(__FILE__ << ":" << __LINE__ << ":" << " " << x)
79#define _LOG_ERROR(x) NDN_LOG_ERROR(x)
80
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081enum : uint32_t {
82 tlv_ca_prefix = 129,
83 tlv_ca_info = 131,
84 tlv_parameter_key = 133,
85 tlv_parameter_value = 135,
86 tlv_ca_certificate = 137,
87 tlv_max_validity_period = 139,
Zhiyi Zhang48f23782020-09-28 12:11:24 -070088 tlv_max_suffix_length = 177,
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070089 tlv_probe_response = 141,
90 tlv_allow_longer_name = 143,
91 tlv_ecdh_pub = 145,
92 tlv_cert_request = 147,
93 tlv_salt = 149,
94 tlv_request_id = 151,
95 tlv_challenge = 153,
96 tlv_status = 155,
97 tlv_initialization_vector = 157,
98 tlv_encrypted_payload = 159,
99 tlv_selected_challenge = 161,
100 tlv_challenge_status = 163,
101 tlv_remaining_tries = 165,
102 tlv_remaining_time = 167,
103 tlv_issued_cert_name = 169,
104 tlv_error_code = 171,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700105 tlv_error_info = 173,
tylerliu182bc532020-09-25 01:54:45 -0700106 tlv_authentication_tag = 175,
tylerliu9a831352020-09-28 11:16:01 -0700107 tlv_cert_to_revoke = 179
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700108};
109
110// Parse CA Configuration file
111const std::string CONFIG_CA_PREFIX = "ca-prefix";
112const std::string CONFIG_CA_INFO = "ca-info";
113const std::string CONFIG_MAX_VALIDITY_PERIOD = "max-validity-period";
114const std::string CONFIG_PROBE_PARAMETERS = "probe-parameters";
115const std::string CONFIG_PROBE_PARAMETER = "probe-parameter-key";
116const std::string CONFIG_SUPPORTED_CHALLENGES = "supported-challenges";
117const std::string CONFIG_CHALLENGE = "challenge";
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700118const std::string CONFIG_MAX_SUFFIX_LENGTH = "max-suffix-length";
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700119
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700120// // JSON format for Certificate Issuer (CA)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121const std::string JSON_CA_NAME = "name";
122const std::string JSON_CA_CONFIG = "ca-config";
123const std::string JSON_CA_ECDH = "ecdh-pub";
124const std::string JSON_CA_SALT = "salt";
Zhiyi Zhangff4bcb62019-09-08 12:57:42 -0700125const std::string JSON_CA_REQUEST_ID = "request-id";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700126const std::string JSON_CA_STATUS = "status";
127const std::string JSON_CA_CHALLENGES = "challenges";
128const std::string JSON_CA_CHALLENGE_ID = "challenge-id";
129const std::string JSON_CA_CERT_ID = "certificate-id";
130
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700131// // JSON format for Challenge Module
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700132const std::string JSON_CHALLENGE_STATUS = "challenge-status";
133const std::string JSON_CHALLENGE_REMAINING_TRIES = "remaining-tries";
134const std::string JSON_CHALLENGE_REMAINING_TIME = "remaining-time";
swa770cf1d8f72020-04-21 23:12:39 -0700135const std::string JSON_CHALLENGE_ISSUED_CERT_NAME = "issued-cert-name";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700137// // JSON format for Certificate Requester
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138const std::string JSON_CLIENT_PROBE_INFO = "probe-info";
139const std::string JSON_CLIENT_ECDH = "ecdh-pub";
140const std::string JSON_CLIENT_CERT_REQ = "cert-request";
141const std::string JSON_CLIENT_SELECTED_CHALLENGE = "selected-challenge";
tylerliu9a831352020-09-28 11:16:01 -0700142const std::string JSON_CLIENT_CERT_TO_REVOKE = "cert-to-revoke";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700143
144// NDNCERT Status Enum
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700145enum class Status : uint16_t {
146 BEFORE_CHALLENGE = 0,
147 CHALLENGE = 1,
148 PENDING = 2,
149 SUCCESS = 3,
150 FAILURE = 4,
151 NOT_STARTED = 5,
152 ENDED = 6
153};
154
155std::string statusToString(Status status);
156
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700157enum class ErrorCode : uint16_t {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700158 NO_ERROR = 0,
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700159 BAD_INTEREST_FORMAT = 1,
160 BAD_PARAMETER_FORMAT = 2,
161 BAD_SIGNATURE = 3,
162 INVALID_PARAMETER = 4,
163 NAME_NOT_ALLOWED = 5,
164 BAD_VALIDITY_PERIOD = 6,
165 OUT_OF_TRIES = 7,
166 OUT_OF_TIME = 8,
167 NO_AVAILABLE_NAMES = 9
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700168};
169
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700170enum class RequestType : uint16_t {
171 NOTINITIALIZED = 0,
172 NEW = 1,
173 RENEW = 2,
174 REVOKE = 3
175};
176
177std::string requestTypeToString(RequestType type);
178
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700179// Pre-defined challenge status
180const std::string CHALLENGE_STATUS_SUCCESS = "success";
181const std::string CHALLENGE_STATUS_FAILURE_TIMEOUT = "failure-timeout";
182const std::string CHALLENGE_STATUS_FAILURE_MAXRETRY = "failure-max-retry";
183const std::string CHALLENGE_STATUS_UNKNOWN_CHALLENGE = "unknown-challenge";
184
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700185} // namespace ndncert
186} // namespace ndn
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800187
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700188#endif // NDNCERT_NDNCERT_COMMON_HPP