blob: b8523e95f3b9b35734e2c243a01141c02c7fde9d [file] [log] [blame]
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -04001/* -*- 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
71namespace ndn {
72namespace nac {
73
74using security::Identity;
75using security::Key;
76using security::SigningInfo;
77using security::SafeBag;
78using security::ValidatorNull;
79using security::transform::PublicKey;
80using security::v2::Certificate;
81using security::v2::DataValidationFailureCallback;
82using security::v2::DataValidationSuccessCallback;
83using security::v2::ValidationError;
84using security::v2::Validator;
85using security::v2::extractKeyNameFromCertName;
86
87namespace tlv {
88using namespace ndn::tlv;
89
90enum {
91 EncryptedContent = 130,
92 EncryptedPayload = 132,
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040093 InitializationVector = 133,
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040094 EncryptedPayloadKey = 134,
95};
96
97} // namespace tlv
98
99const name::Component ENCRYPTED_BY("ENCRYPTED-BY");
100const name::Component NAC("NAC");
101const name::Component KEK("KEK");
102const name::Component KDK("KDK");
103const name::Component CK("CK");
104
105const size_t AES_KEY_SIZE = 32;
106const size_t AES_IV_SIZE = 16;
107
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400108const time::seconds DEFAULT_KEK_FRESHNESS_PERIOD = 1_h;
109const time::seconds DEFAULT_KDK_FRESHNESS_PERIOD = 1_h;
110const time::seconds DEFAULT_CK_FRESHNESS_PERIOD = 1_h;
111
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400112enum 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 Afanasyev1a21e102018-06-13 20:33:21 -0400127 TpmKeyNotFound = 102,
128 EncryptionFailure = 103
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -0400129};
130
131using ErrorCallback = std::function<void (const ErrorCode&, const std::string&)>;
132
133
134class Error : public std::runtime_error
135{
136public:
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 */
145Name
146convertKekNameToKdkPrefix(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 */
154std::tuple<Name, Name, Name>
155extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
156
157} // namespace nac
158} // namespace ndn
159
160#endif // NDN_NAC_COMMON_HPP