blob: b37f50c70c37f43d35119b151d19fe911c03a695 [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,
93 InitialVector = 133,
94 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
108enum class ErrorCode {
109 KekRetrievalFailure = 1,
110 KekRetrievalTimeout = 2,
111 KekInvalidName = 3,
112
113 KdkRetrievalFailure = 11,
114 KdkRetrievalTimeout = 12,
115 KdkInvalidName = 13,
116 KdkDecryptionFailure = 14,
117
118 CkRetrievalFailure = 21,
119 CkRetrievalTimeout = 22,
120 CkInvalidName = 23,
121
122 MissingRequiredKeyLocator = 101,
123 TpmKeyNotFound = 102
124};
125
126using ErrorCallback = std::function<void (const ErrorCode&, const std::string&)>;
127
128
129class Error : public std::runtime_error
130{
131public:
132 using std::runtime_error::runtime_error;
133};
134
135/**
136 * @brief Convert KEK name to KDK prefix:
137 *
138 * `<identity>/NAC/KEK/<key-id>` =>> `<identity>/NAC/KDK/<key-id>`
139 */
140Name
141convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure);
142
143/**
144 * @brief Extract KDK information from name of CK data packet name
145 *
146 * @return tuple of (KDK prefix, KDK identity, and KDK key id). The last two identify
147 * KDK private/key pair in KeyChain
148 */
149std::tuple<Name, Name, Name>
150extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure);
151
152} // namespace nac
153} // namespace ndn
154
155#endif // NDN_NAC_COMMON_HPP