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 | /* |
Davide Pesavento | 8d60e64 | 2023-04-17 02:36:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | 3bf126f | 2023-09-14 20:35:02 -0400 | [diff] [blame] | 23 | #include "detail/config.hpp" |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 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 | |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 37 | #include <functional> |
| 38 | #include <stdexcept> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 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> |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 45 | #include <ndn-cxx/security/certificate.hpp> |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 46 | #include <ndn-cxx/security/key-chain.hpp> |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 47 | #include <ndn-cxx/security/signing-info.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 48 | #include <ndn-cxx/security/transform/public-key.hpp> |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 49 | #include <ndn-cxx/security/validation-callback.hpp> |
| 50 | #include <ndn-cxx/security/validation-error.hpp> |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 51 | #include <ndn-cxx/security/validator.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 52 | |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 53 | #include <boost/assert.hpp> |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 54 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 55 | namespace ndn::nac { |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 56 | |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 57 | using security::Certificate; |
| 58 | using security::DataValidationFailureCallback; |
| 59 | using security::DataValidationSuccessCallback; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 60 | using security::Identity; |
| 61 | using security::Key; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 62 | using security::SafeBag; |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 63 | using security::SigningInfo; |
| 64 | using security::ValidationError; |
| 65 | using security::Validator; |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 66 | using security::extractKeyNameFromCertName; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 67 | using security::transform::PublicKey; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 68 | |
| 69 | namespace tlv { |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 70 | |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 71 | using namespace ndn::tlv; |
| 72 | |
| 73 | enum { |
| 74 | EncryptedContent = 130, |
| 75 | EncryptedPayload = 132, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 76 | InitializationVector = 133, |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 77 | EncryptedPayloadKey = 134, |
| 78 | }; |
| 79 | |
| 80 | } // namespace tlv |
| 81 | |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 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"}; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 87 | |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 88 | inline constexpr size_t AES_KEY_SIZE = 32; |
| 89 | inline constexpr size_t AES_IV_SIZE = 16; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 90 | |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 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; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 94 | |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 95 | inline constexpr time::seconds RETRY_DELAY_AFTER_NACK = 1_s; |
| 96 | inline constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s; |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 97 | |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 98 | enum class ErrorCode { |
| 99 | KekRetrievalFailure = 1, |
| 100 | KekRetrievalTimeout = 2, |
| 101 | KekInvalidName = 3, |
| 102 | |
| 103 | KdkRetrievalFailure = 11, |
| 104 | KdkRetrievalTimeout = 12, |
| 105 | KdkInvalidName = 13, |
| 106 | KdkDecryptionFailure = 14, |
| 107 | |
| 108 | CkRetrievalFailure = 21, |
| 109 | CkRetrievalTimeout = 22, |
| 110 | CkInvalidName = 23, |
| 111 | |
| 112 | MissingRequiredKeyLocator = 101, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 113 | TpmKeyNotFound = 102, |
| 114 | EncryptionFailure = 103 |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 117 | using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>; |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 118 | |
| 119 | class Error : public std::runtime_error |
| 120 | { |
| 121 | public: |
| 122 | using std::runtime_error::runtime_error; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * @brief Convert KEK name to KDK prefix: |
| 127 | * |
| 128 | * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>` |
| 129 | */ |
| 130 | Name |
| 131 | convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure); |
| 132 | |
| 133 | /** |
| 134 | * @brief Extract KDK information from name of CK data packet name |
| 135 | * |
| 136 | * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify |
| 137 | * KDK private/key pair in KeyChain |
| 138 | */ |
| 139 | std::tuple<Name, Name, Name> |
| 140 | extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure); |
| 141 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 142 | } // namespace ndn::nac |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 143 | |
| 144 | #endif // NDN_NAC_COMMON_HPP |