blob: 1f95504441b592a7c486a4ebd224aac1470ff42c [file] [log] [blame]
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc2649492020-12-22 21:43:35 -05002/*
Alexander Afanasyev5181d892020-06-06 18:05:47 -04003 * Copyright (c) 2014-2020, Regents of the University of California
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -04004 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#ifndef NDN_NAC_COMMON_HPP
21#define NDN_NAC_COMMON_HPP
22
23#include "config.hpp"
24
Davide Pesavento27680ae2019-04-06 19:40:01 -040025#ifdef NAC_WITH_TESTS
26#define NAC_VIRTUAL_WITH_TESTS virtual
27#define NAC_PUBLIC_WITH_TESTS_ELSE_PROTECTED public
28#define NAC_PUBLIC_WITH_TESTS_ELSE_PRIVATE public
29#define NAC_PROTECTED_WITH_TESTS_ELSE_PRIVATE protected
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040030#else
Davide Pesavento27680ae2019-04-06 19:40:01 -040031#define NAC_VIRTUAL_WITH_TESTS
32#define NAC_PUBLIC_WITH_TESTS_ELSE_PROTECTED protected
33#define NAC_PUBLIC_WITH_TESTS_ELSE_PRIVATE private
34#define NAC_PROTECTED_WITH_TESTS_ELSE_PRIVATE private
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040035#endif
36
37#include <cstddef>
38#include <list>
39#include <map>
40#include <queue>
41#include <set>
42#include <unordered_map>
43#include <unordered_set>
44#include <vector>
45
46#include <ndn-cxx/data.hpp>
47#include <ndn-cxx/encoding/buffer-stream.hpp>
48#include <ndn-cxx/face.hpp>
49#include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
50#include <ndn-cxx/interest.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040051#include <ndn-cxx/security/key-chain.hpp>
Davide Pesaventoc2649492020-12-22 21:43:35 -050052#include <ndn-cxx/security/signing-info.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040053#include <ndn-cxx/security/transform/public-key.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040054#include <ndn-cxx/security/validation-callback.hpp>
55#include <ndn-cxx/security/validation-error.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040056#include <ndn-cxx/security/validator-null.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040057#include <ndn-cxx/security/validator.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040058#include <ndn-cxx/util/random.hpp>
59#include <ndn-cxx/util/signal.hpp>
60
61#include <boost/algorithm/string.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040062#include <boost/assert.hpp>
63#include <boost/lexical_cast.hpp>
64#include <boost/noncopyable.hpp>
65
66namespace ndn {
67namespace nac {
68
Alexander Afanasyev5181d892020-06-06 18:05:47 -040069using security::Certificate;
70using security::DataValidationFailureCallback;
71using security::DataValidationSuccessCallback;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040072using security::Identity;
73using security::Key;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040074using security::SafeBag;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040075using security::SigningInfo;
76using security::ValidationError;
77using security::Validator;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040078using security::ValidatorNull;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040079using security::extractKeyNameFromCertName;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040080using security::transform::PublicKey;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040081
82namespace tlv {
83using namespace ndn::tlv;
84
85enum {
86 EncryptedContent = 130,
87 EncryptedPayload = 132,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040088 InitializationVector = 133,
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040089 EncryptedPayloadKey = 134,
90};
91
92} // namespace tlv
93
94const name::Component ENCRYPTED_BY("ENCRYPTED-BY");
95const name::Component NAC("NAC");
96const name::Component KEK("KEK");
97const name::Component KDK("KDK");
98const name::Component CK("CK");
99
100const size_t AES_KEY_SIZE = 32;
101const size_t AES_IV_SIZE = 16;
102
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400103const time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
104const time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
105const time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
106
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400107const time::seconds RETRY_DELAY_AFTER_NACK = 1_s;
108const time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s;
109
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400110enum class ErrorCode {
111 KekRetrievalFailure = 1,
112 KekRetrievalTimeout = 2,
113 KekInvalidName = 3,
114
115 KdkRetrievalFailure = 11,
116 KdkRetrievalTimeout = 12,
117 KdkInvalidName = 13,
118 KdkDecryptionFailure = 14,
119
120 CkRetrievalFailure = 21,
121 CkRetrievalTimeout = 22,
122 CkInvalidName = 23,
123
124 MissingRequiredKeyLocator = 101,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400125 TpmKeyNotFound = 102,
126 EncryptionFailure = 103
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400127};
128
Davide Pesaventoc2649492020-12-22 21:43:35 -0500129using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400130
131class Error : public std::runtime_error
132{
133public:
134 using std::runtime_error::runtime_error;
135};
136
137/**
138 * @brief Convert KEK name to KDK prefix:
139 *
140 * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>`
141 */
142Name
143convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure);
144
145/**
146 * @brief Extract KDK information from name of CK data packet name
147 *
148 * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify
149 * KDK private/key pair in KeyChain
150 */
151std::tuple<Name, Name, Name>
152extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
153
154} // namespace nac
155} // namespace ndn
156
157#endif // NDN_NAC_COMMON_HPP