blob: 5aebdebca99e1b4cf1d4814e8c174c1cc7049783 [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/*
Davide Pesaventobde084f2022-04-17 00:21:35 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventobde084f2022-04-17 00:21:35 -040066namespace ndn::nac {
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040067
Alexander Afanasyev5181d892020-06-06 18:05:47 -040068using security::Certificate;
69using security::DataValidationFailureCallback;
70using security::DataValidationSuccessCallback;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040071using security::Identity;
72using security::Key;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040073using security::SafeBag;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040074using security::SigningInfo;
75using security::ValidationError;
76using security::Validator;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040077using security::ValidatorNull;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040078using security::extractKeyNameFromCertName;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040079using security::transform::PublicKey;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040080
81namespace tlv {
82using namespace ndn::tlv;
83
84enum {
85 EncryptedContent = 130,
86 EncryptedPayload = 132,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040087 InitializationVector = 133,
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040088 EncryptedPayloadKey = 134,
89};
90
91} // namespace tlv
92
93const name::Component ENCRYPTED_BY("ENCRYPTED-BY");
94const name::Component NAC("NAC");
95const name::Component KEK("KEK");
96const name::Component KDK("KDK");
97const name::Component CK("CK");
98
99const size_t AES_KEY_SIZE = 32;
100const size_t AES_IV_SIZE = 16;
101
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400102const time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
103const time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
104const time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
105
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400106const time::seconds RETRY_DELAY_AFTER_NACK = 1_s;
107const time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s;
108
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400109enum class ErrorCode {
110 KekRetrievalFailure = 1,
111 KekRetrievalTimeout = 2,
112 KekInvalidName = 3,
113
114 KdkRetrievalFailure = 11,
115 KdkRetrievalTimeout = 12,
116 KdkInvalidName = 13,
117 KdkDecryptionFailure = 14,
118
119 CkRetrievalFailure = 21,
120 CkRetrievalTimeout = 22,
121 CkInvalidName = 23,
122
123 MissingRequiredKeyLocator = 101,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400124 TpmKeyNotFound = 102,
125 EncryptionFailure = 103
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400126};
127
Davide Pesaventoc2649492020-12-22 21:43:35 -0500128using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400129
130class Error : public std::runtime_error
131{
132public:
133 using std::runtime_error::runtime_error;
134};
135
136/**
137 * @brief Convert KEK name to KDK prefix:
138 *
139 * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>`
140 */
141Name
142convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure);
143
144/**
145 * @brief Extract KDK information from name of CK data packet name
146 *
147 * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify
148 * KDK private/key pair in KeyChain
149 */
150std::tuple<Name, Name, Name>
151extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
152
Davide Pesaventobde084f2022-04-17 00:21:35 -0400153} // namespace ndn::nac
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400154
155#endif // NDN_NAC_COMMON_HPP