common.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, Regents of the University of California
4  *
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 "detail/config.hpp"
24 
25 #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
30 #else
31 #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
35 #endif
36 
37 #include <functional>
38 #include <stdexcept>
39 
40 #include <ndn-cxx/data.hpp>
41 #include <ndn-cxx/encoding/buffer-stream.hpp>
42 #include <ndn-cxx/face.hpp>
43 #include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
44 #include <ndn-cxx/interest.hpp>
45 #include <ndn-cxx/security/certificate.hpp>
46 #include <ndn-cxx/security/key-chain.hpp>
47 #include <ndn-cxx/security/signing-info.hpp>
48 #include <ndn-cxx/security/transform/public-key.hpp>
49 #include <ndn-cxx/security/validation-callback.hpp>
50 #include <ndn-cxx/security/validation-error.hpp>
51 #include <ndn-cxx/security/validator.hpp>
52 
53 #include <boost/assert.hpp>
54 
55 namespace ndn::nac {
56 
57 using security::Certificate;
58 using security::DataValidationFailureCallback;
59 using security::DataValidationSuccessCallback;
60 using security::Identity;
61 using security::Key;
62 using security::SafeBag;
63 using security::SigningInfo;
64 using security::ValidationError;
65 using security::Validator;
66 using security::extractKeyNameFromCertName;
67 using security::transform::PublicKey;
68 
69 namespace tlv {
70 
71 using namespace ndn::tlv;
72 
73 enum {
78 };
79 
80 } // namespace tlv
81 
82 inline const name::Component ENCRYPTED_BY{"ENCRYPTED-BY"};
83 inline const name::Component NAC{"NAC"};
84 inline const name::Component KEK{"KEK"};
85 inline const name::Component KDK{"KDK"};
86 inline const name::Component CK{"CK"};
87 
88 inline constexpr size_t AES_KEY_SIZE = 32;
89 inline constexpr size_t AES_IV_SIZE = 16;
90 
91 inline constexpr time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
92 inline constexpr time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
93 inline constexpr time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
94 
95 inline constexpr time::seconds RETRY_DELAY_AFTER_NACK = 1_s;
96 inline constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s;
97 
98 enum class ErrorCode {
101  KekInvalidName = 3,
102 
103  KdkRetrievalFailure = 11,
104  KdkRetrievalTimeout = 12,
105  KdkInvalidName = 13,
107 
108  CkRetrievalFailure = 21,
109  CkRetrievalTimeout = 22,
110  CkInvalidName = 23,
111 
113  TpmKeyNotFound = 102,
114  EncryptionFailure = 103
115 };
116 
117 using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>;
118 
119 class Error : public std::runtime_error
120 {
121 public:
122  using std::runtime_error::runtime_error;
123 };
124 
130 Name
131 convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure);
132 
139 std::tuple<Name, Name, Name>
140 extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
141 
142 } // namespace ndn::nac
143 
144 #endif // NDN_NAC_COMMON_HPP
@ EncryptedPayload
Definition: common.hpp:75
@ InitializationVector
Definition: common.hpp:76
@ EncryptedPayloadKey
Definition: common.hpp:77
const name::Component KDK
Definition: common.hpp:85
std::tuple< Name, Name, Name > extractKdkInfoFromCkName(const Name &ckDataName, const Name &ckName, const ErrorCallback &onFailure)
Extract KDK information from name of CK data packet name.
Definition: common.cpp:37
constexpr time::seconds DEFAULT_KEK_FRESHNESS_PERIOD
Definition: common.hpp:91
Name convertKekNameToKdkPrefix(const Name &kekName, const ErrorCallback &onFailure)
Convert KEK name to KDK prefix:
Definition: common.cpp:25
constexpr time::seconds DEFAULT_CK_FRESHNESS_PERIOD
Definition: common.hpp:93
constexpr time::seconds DEFAULT_KDK_FRESHNESS_PERIOD
Definition: common.hpp:92
std::function< void(const ErrorCode &, const std::string &)> ErrorCallback
Definition: common.hpp:117
constexpr size_t AES_IV_SIZE
Definition: common.hpp:89
const name::Component ENCRYPTED_BY
Definition: common.hpp:82
const name::Component CK
Definition: common.hpp:86
const name::Component KEK
Definition: common.hpp:84
constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL
Definition: common.hpp:96
constexpr size_t AES_KEY_SIZE
Definition: common.hpp:88
const name::Component NAC
Definition: common.hpp:83
constexpr time::seconds RETRY_DELAY_AFTER_NACK
Definition: common.hpp:95