Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, 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 "config.hpp" |
| 24 | |
| 25 | #ifdef NDN_NAC_HAVE_TESTS |
| 26 | #define VIRTUAL_WITH_TESTS virtual |
| 27 | #define PUBLIC_WITH_TESTS_ELSE_PROTECTED public |
| 28 | #define PUBLIC_WITH_TESTS_ELSE_PRIVATE public |
| 29 | #define PROTECTED_WITH_TESTS_ELSE_PRIVATE protected |
| 30 | #else |
| 31 | #define VIRTUAL_WITH_TESTS |
| 32 | #define PUBLIC_WITH_TESTS_ELSE_PROTECTED protected |
| 33 | #define PUBLIC_WITH_TESTS_ELSE_PRIVATE private |
| 34 | #define PROTECTED_WITH_TESTS_ELSE_PRIVATE private |
| 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> |
| 51 | #include <ndn-cxx/link.hpp> |
| 52 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 53 | #include <ndn-cxx/security/transform/block-cipher.hpp> |
| 54 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 55 | #include <ndn-cxx/security/transform/public-key.hpp> |
| 56 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
| 57 | #include <ndn-cxx/security/v2/key-chain.hpp> |
| 58 | #include <ndn-cxx/security/v2/validation-callback.hpp> |
| 59 | #include <ndn-cxx/security/v2/validation-error.hpp> |
| 60 | #include <ndn-cxx/security/v2/validator.hpp> |
| 61 | #include <ndn-cxx/security/validator-null.hpp> |
| 62 | #include <ndn-cxx/util/random.hpp> |
| 63 | #include <ndn-cxx/util/signal.hpp> |
| 64 | |
| 65 | #include <boost/algorithm/string.hpp> |
| 66 | #include <boost/asio.hpp> |
| 67 | #include <boost/assert.hpp> |
| 68 | #include <boost/lexical_cast.hpp> |
| 69 | #include <boost/noncopyable.hpp> |
| 70 | |
| 71 | namespace ndn { |
| 72 | namespace nac { |
| 73 | |
| 74 | using security::Identity; |
| 75 | using security::Key; |
| 76 | using security::SigningInfo; |
| 77 | using security::SafeBag; |
| 78 | using security::ValidatorNull; |
| 79 | using security::transform::PublicKey; |
| 80 | using security::v2::Certificate; |
| 81 | using security::v2::DataValidationFailureCallback; |
| 82 | using security::v2::DataValidationSuccessCallback; |
| 83 | using security::v2::ValidationError; |
| 84 | using security::v2::Validator; |
| 85 | using security::v2::extractKeyNameFromCertName; |
| 86 | |
| 87 | namespace tlv { |
| 88 | using namespace ndn::tlv; |
| 89 | |
| 90 | enum { |
| 91 | EncryptedContent = 130, |
| 92 | EncryptedPayload = 132, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 93 | InitializationVector = 133, |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 94 | EncryptedPayloadKey = 134, |
| 95 | }; |
| 96 | |
| 97 | } // namespace tlv |
| 98 | |
| 99 | const name::Component ENCRYPTED_BY("ENCRYPTED-BY"); |
| 100 | const name::Component NAC("NAC"); |
| 101 | const name::Component KEK("KEK"); |
| 102 | const name::Component KDK("KDK"); |
| 103 | const name::Component CK("CK"); |
| 104 | |
| 105 | const size_t AES_KEY_SIZE = 32; |
| 106 | const size_t AES_IV_SIZE = 16; |
| 107 | |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 108 | const time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h; |
| 109 | const time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h; |
| 110 | const time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h; |
| 111 | |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 112 | enum class ErrorCode { |
| 113 | KekRetrievalFailure = 1, |
| 114 | KekRetrievalTimeout = 2, |
| 115 | KekInvalidName = 3, |
| 116 | |
| 117 | KdkRetrievalFailure = 11, |
| 118 | KdkRetrievalTimeout = 12, |
| 119 | KdkInvalidName = 13, |
| 120 | KdkDecryptionFailure = 14, |
| 121 | |
| 122 | CkRetrievalFailure = 21, |
| 123 | CkRetrievalTimeout = 22, |
| 124 | CkInvalidName = 23, |
| 125 | |
| 126 | MissingRequiredKeyLocator = 101, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 127 | TpmKeyNotFound = 102, |
| 128 | EncryptionFailure = 103 |
Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | using ErrorCallback = std::function<void (const ErrorCode&, const std::string&)>; |
| 132 | |
| 133 | |
| 134 | class Error : public std::runtime_error |
| 135 | { |
| 136 | public: |
| 137 | using std::runtime_error::runtime_error; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * @brief Convert KEK name to KDK prefix: |
| 142 | * |
| 143 | * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>` |
| 144 | */ |
| 145 | Name |
| 146 | convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure); |
| 147 | |
| 148 | /** |
| 149 | * @brief Extract KDK information from name of CK data packet name |
| 150 | * |
| 151 | * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify |
| 152 | * KDK private/key pair in KeyChain |
| 153 | */ |
| 154 | std::tuple<Name, Name, Name> |
| 155 | extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure); |
| 156 | |
| 157 | } // namespace nac |
| 158 | } // namespace ndn |
| 159 | |
| 160 | #endif // NDN_NAC_COMMON_HPP |