Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 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 "config.hpp" |
| 24 | |
Davide Pesavento | 27680ae | 2019-04-06 19:40:01 -0400 | [diff] [blame] | 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 |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 30 | #else |
Davide Pesavento | 27680ae | 2019-04-06 19:40:01 -0400 | [diff] [blame] | 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 |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 35 | #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 Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 51 | #include <ndn-cxx/security/key-chain.hpp> |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 52 | #include <ndn-cxx/security/signing-info.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 53 | #include <ndn-cxx/security/transform/public-key.hpp> |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 54 | #include <ndn-cxx/security/validation-callback.hpp> |
| 55 | #include <ndn-cxx/security/validation-error.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 56 | #include <ndn-cxx/security/validator-null.hpp> |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 57 | #include <ndn-cxx/security/validator.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 58 | #include <ndn-cxx/util/random.hpp> |
| 59 | #include <ndn-cxx/util/signal.hpp> |
| 60 | |
| 61 | #include <boost/algorithm/string.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 62 | #include <boost/assert.hpp> |
| 63 | #include <boost/lexical_cast.hpp> |
| 64 | #include <boost/noncopyable.hpp> |
| 65 | |
| 66 | namespace ndn { |
| 67 | namespace nac { |
| 68 | |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 69 | using security::Certificate; |
| 70 | using security::DataValidationFailureCallback; |
| 71 | using security::DataValidationSuccessCallback; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 72 | using security::Identity; |
| 73 | using security::Key; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 74 | using security::SafeBag; |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 75 | using security::SigningInfo; |
| 76 | using security::ValidationError; |
| 77 | using security::Validator; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 78 | using security::ValidatorNull; |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 79 | using security::extractKeyNameFromCertName; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 80 | using security::transform::PublicKey; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 81 | |
| 82 | namespace tlv { |
| 83 | using namespace ndn::tlv; |
| 84 | |
| 85 | enum { |
| 86 | EncryptedContent = 130, |
| 87 | EncryptedPayload = 132, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 88 | InitializationVector = 133, |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 89 | EncryptedPayloadKey = 134, |
| 90 | }; |
| 91 | |
| 92 | } // namespace tlv |
| 93 | |
| 94 | const name::Component ENCRYPTED_BY("ENCRYPTED-BY"); |
| 95 | const name::Component NAC("NAC"); |
| 96 | const name::Component KEK("KEK"); |
| 97 | const name::Component KDK("KDK"); |
| 98 | const name::Component CK("CK"); |
| 99 | |
| 100 | const size_t AES_KEY_SIZE = 32; |
| 101 | const size_t AES_IV_SIZE = 16; |
| 102 | |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 103 | const time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h; |
| 104 | const time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h; |
| 105 | const time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h; |
| 106 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 107 | const time::seconds RETRY_DELAY_AFTER_NACK = 1_s; |
| 108 | const time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s; |
| 109 | |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 110 | enum 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 Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 125 | TpmKeyNotFound = 102, |
| 126 | EncryptionFailure = 103 |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 127 | }; |
| 128 | |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 129 | using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 130 | |
| 131 | class Error : public std::runtime_error |
| 132 | { |
| 133 | public: |
| 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 | */ |
| 142 | Name |
| 143 | convertKekNameToKdkPrefix(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 | */ |
| 151 | std::tuple<Name, Name, Name> |
| 152 | extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure); |
| 153 | |
| 154 | } // namespace nac |
| 155 | } // namespace ndn |
| 156 | |
| 157 | #endif // NDN_NAC_COMMON_HPP |