blob: 629463b3cce377f2a269753f99f4de7c67449064 [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 Pesavento8d60e642023-04-17 02:36:03 -04003 * Copyright (c) 2014-2023, 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
Davide Pesavento3bf126f2023-09-14 20:35:02 -040023#include "detail/config.hpp"
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040024
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>
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040054
Davide Pesaventobde084f2022-04-17 00:21:35 -040055namespace ndn::nac {
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040056
Alexander Afanasyev5181d892020-06-06 18:05:47 -040057using security::Certificate;
58using security::DataValidationFailureCallback;
59using security::DataValidationSuccessCallback;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040060using security::Identity;
61using security::Key;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040062using security::SafeBag;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040063using security::SigningInfo;
64using security::ValidationError;
65using security::Validator;
Alexander Afanasyev5181d892020-06-06 18:05:47 -040066using security::extractKeyNameFromCertName;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040067using security::transform::PublicKey;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040068
69namespace tlv {
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040070
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040071using namespace ndn::tlv;
72
73enum {
74 EncryptedContent = 130,
75 EncryptedPayload = 132,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040076 InitializationVector = 133,
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040077 EncryptedPayloadKey = 134,
78};
79
80} // namespace tlv
81
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040082inline const name::Component ENCRYPTED_BY{"ENCRYPTED-BY"};
83inline const name::Component NAC{"NAC"};
84inline const name::Component KEK{"KEK"};
85inline const name::Component KDK{"KDK"};
86inline const name::Component CK{"CK"};
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040087
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040088inline constexpr size_t AES_KEY_SIZE = 32;
89inline constexpr size_t AES_IV_SIZE = 16;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040090
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040091inline constexpr time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
92inline constexpr time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
93inline constexpr time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040094
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040095inline constexpr time::seconds RETRY_DELAY_AFTER_NACK = 1_s;
96inline constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL = 60_s;
Alexander Afanasyevda366d82018-06-29 18:18:02 -040097
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040098enum 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 Afanasyev1a21e102018-06-13 20:33:21 -0400113 TpmKeyNotFound = 102,
114 EncryptionFailure = 103
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400115};
116
Davide Pesaventoc2649492020-12-22 21:43:35 -0500117using ErrorCallback = std::function<void(const ErrorCode&, const std::string&)>;
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400118
119class Error : public std::runtime_error
120{
121public:
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 */
130Name
131convertKekNameToKdkPrefix(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 */
139std::tuple<Name, Name, Name>
140extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
141
Davide Pesaventobde084f2022-04-17 00:21:35 -0400142} // namespace ndn::nac
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400143
144#endif // NDN_NAC_COMMON_HPP