blob: 24df62fbbb6cce822327863699c321f32e9eb605 [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
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040037#include <functional>
38#include <stdexcept>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040039
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 Pesavento2e5b7b12022-09-19 23:30:44 -040045#include <ndn-cxx/security/certificate.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040046#include <ndn-cxx/security/key-chain.hpp>
Davide Pesaventoc2649492020-12-22 21:43:35 -050047#include <ndn-cxx/security/signing-info.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040048#include <ndn-cxx/security/transform/public-key.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040049#include <ndn-cxx/security/validation-callback.hpp>
50#include <ndn-cxx/security/validation-error.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040051#include <ndn-cxx/security/validator.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040052
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040053#include <boost/assert.hpp>
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040054#include <boost/core/noncopyable.hpp>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040055
Davide Pesaventobde084f2022-04-17 00:21:35 -040056namespace ndn::nac {
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040057
Alexander Afanasyev5181d892020-06-06 18:05:47 -040058using security::Certificate;
59using security::DataValidationFailureCallback;
60using security::DataValidationSuccessCallback;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040061using security::Identity;
62using security::Key;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040063using security::SafeBag;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040064using security::SigningInfo;
65using security::ValidationError;
66using security::Validator;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040067using security::extractKeyNameFromCertName;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040068using security::transform::PublicKey;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040069
70namespace tlv {
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040071
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040072using namespace ndn::tlv;
73
74enum {
75 EncryptedContent = 130,
76 EncryptedPayload = 132,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040077 InitializationVector = 133,
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040078 EncryptedPayloadKey = 134,
79};
80
81} // namespace tlv
82
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040083inline const name::Component ENCRYPTED_BY{"ENCRYPTED-BY"};
84inline const name::Component NAC{"NAC"};
85inline const name::Component KEK{"KEK"};
86inline const name::Component KDK{"KDK"};
87inline const name::Component CK{"CK"};
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040088
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040089inline constexpr size_t AES_KEY_SIZE = 32;
90inline constexpr size_t AES_IV_SIZE = 16;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040091
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040092inline constexpr time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
93inline constexpr time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
94inline constexpr time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040095
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040096inline constexpr time::seconds RETRY_DELAY_AFTER_NACK = 1_s;
97inline constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s;
Alexander Afanasyevda366d82018-06-29 18:18:02 -040098
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040099enum class ErrorCode {
100 KekRetrievalFailure = 1,
101 KekRetrievalTimeout = 2,
102 KekInvalidName = 3,
103
104 KdkRetrievalFailure = 11,
105 KdkRetrievalTimeout = 12,
106 KdkInvalidName = 13,
107 KdkDecryptionFailure = 14,
108
109 CkRetrievalFailure = 21,
110 CkRetrievalTimeout = 22,
111 CkInvalidName = 23,
112
113 MissingRequiredKeyLocator = 101,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400114 TpmKeyNotFound = 102,
115 EncryptionFailure = 103
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400116};
117
Davide Pesaventoc2649492020-12-22 21:43:35 -0500118using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400119
120class Error : public std::runtime_error
121{
122public:
123 using std::runtime_error::runtime_error;
124};
125
126/**
127 * @brief Convert KEK name to KDK prefix:
128 *
129 * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>`
130 */
131Name
132convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure);
133
134/**
135 * @brief Extract KDK information from name of CK data packet name
136 *
137 * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify
138 * KDK private/key pair in KeyChain
139 */
140std::tuple<Name, Name, Name>
141extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
142
Davide Pesaventobde084f2022-04-17 00:21:35 -0400143} // namespace ndn::nac
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400144
145#endif // NDN_NAC_COMMON_HPP